diff options
author | Georg Ehrke <developer@georgehrke.com> | 2018-03-08 14:44:02 +0100 |
---|---|---|
committer | Thomas Citharel <tcit@tcit.fr> | 2019-01-16 13:58:33 +0100 |
commit | 4a2238c75be9b0ed4be17869878c04cb812d276f (patch) | |
tree | bce70e9c014390bb0d5c93774c32951d972239d8 /apps/dav/tests/unit/Command | |
parent | 943d48cb3e9ab2b97ad6a72c953df59318c2f17e (diff) | |
download | nextcloud-server-4a2238c75be9b0ed4be17869878c04cb812d276f.tar.gz nextcloud-server-4a2238c75be9b0ed4be17869878c04cb812d276f.zip |
add list-calendars command
Signed-off-by: Georg Ehrke <developer@georgehrke.com>
Rebase and delete URI suggestion part
Invite to use occ dav:list-calendars instead
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
Fix autoload
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
Use injection & test everything
And rebase
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
Add test for ListCalendars and refactoring
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
Fix indentation
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
Diffstat (limited to 'apps/dav/tests/unit/Command')
-rw-r--r-- | apps/dav/tests/unit/Command/ListCalendarsTest.php | 139 | ||||
-rw-r--r-- | apps/dav/tests/unit/Command/MoveCalendarTest.php | 248 |
2 files changed, 378 insertions, 9 deletions
diff --git a/apps/dav/tests/unit/Command/ListCalendarsTest.php b/apps/dav/tests/unit/Command/ListCalendarsTest.php new file mode 100644 index 00000000000..8d19c724917 --- /dev/null +++ b/apps/dav/tests/unit/Command/ListCalendarsTest.php @@ -0,0 +1,139 @@ +<?php +/** + * @author Thomas Citharel <tcit@tcit.fr> + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OCA\DAV\Tests\Command; + +use InvalidArgumentException; +use OCA\DAV\CalDAV\BirthdayService; +use OCA\DAV\CalDAV\CalDavBackend; +use OCA\DAV\Command\ListCalendars; +use OCP\IUserManager; +use Symfony\Component\Console\Tester\CommandTester; +use Test\TestCase; + + +/** + * Class ListCalendarsTest + * + * @package OCA\DAV\Tests\Command + */ +class ListCalendarsTest extends TestCase { + + /** @var \OCP\IUserManager|\PHPUnit_Framework_MockObject_MockObject $userManager */ + private $userManager; + + /** @var CalDavBackend|\PHPUnit_Framework_MockObject_MockObject $l10n */ + private $calDav; + + /** @var ListCalendars */ + private $command; + + const USERNAME = 'username'; + + protected function setUp() { + parent::setUp(); + + $this->userManager = $this->createMock(IUserManager::class); + $this->calDav = $this->createMock(CalDavBackend::class); + + $this->command = new ListCalendars( + $this->userManager, + $this->calDav + ); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testWithBadUser() + { + $this->userManager->expects($this->once()) + ->method('userExists') + ->with(self::USERNAME) + ->willReturn(false); + + $commandTester = new CommandTester($this->command); + $commandTester->execute([ + 'user' => self::USERNAME, + ]); + $this->assertContains("User <" . self::USERNAME . "> in unknown", $commandTester->getDisplay()); + } + + public function testWithCorrectUserWithNoCalendars() + { + $this->userManager->expects($this->once()) + ->method('userExists') + ->with(self::USERNAME) + ->willReturn(true); + + $this->calDav->expects($this->once()) + ->method('getCalendarsForUser') + ->with('principals/users/' . self::USERNAME) + ->willReturn([]); + + $commandTester = new CommandTester($this->command); + $commandTester->execute([ + 'user' => self::USERNAME, + ]); + $this->assertContains("User <" . self::USERNAME . "> has no calendars\n", $commandTester->getDisplay()); + } + + public function dataExecute() + { + return [ + [false, '✓'], + [true, 'x'] + ]; + } + + /** + * @dataProvider dataExecute + */ + public function testWithCorrectUser(bool $readOnly, string $output) + { + $this->userManager->expects($this->once()) + ->method('userExists') + ->with(self::USERNAME) + ->willReturn(true); + + $this->calDav->expects($this->once()) + ->method('getCalendarsForUser') + ->with('principals/users/' . self::USERNAME) + ->willReturn([ + [ + 'uri' => BirthdayService::BIRTHDAY_CALENDAR_URI, + ], + [ + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => $readOnly, + 'uri' => 'test', + '{DAV:}displayname' => 'dp', + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => 'owner-principal', + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname' => 'owner-dp', + ] + ]); + + $commandTester = new CommandTester($this->command); + $commandTester->execute([ + 'user' => self::USERNAME, + ]); + $this->assertContains($output, $commandTester->getDisplay()); + $this->assertNotContains(BirthdayService::BIRTHDAY_CALENDAR_URI, $commandTester->getDisplay()); + } +} diff --git a/apps/dav/tests/unit/Command/MoveCalendarTest.php b/apps/dav/tests/unit/Command/MoveCalendarTest.php index 12329fd4af1..5e5c1f150ee 100644 --- a/apps/dav/tests/unit/Command/MoveCalendarTest.php +++ b/apps/dav/tests/unit/Command/MoveCalendarTest.php @@ -21,8 +21,9 @@ namespace OCA\DAV\Tests\Command; use InvalidArgumentException; +use OCA\DAV\CalDAV\CalDavBackend; use OCA\DAV\Command\MoveCalendar; -use OCP\IDBConnection; +use OCP\IConfig; use OCP\IGroupManager; use OCP\IL10N; use OCP\IUserManager; @@ -34,7 +35,6 @@ use Test\TestCase; * Class MoveCalendarTest * * @package OCA\DAV\Tests\Command - * @group DB */ class MoveCalendarTest extends TestCase { @@ -44,12 +44,15 @@ class MoveCalendarTest extends TestCase { /** @var \OCP\IGroupManager|\PHPUnit_Framework_MockObject_MockObject $groupManager */ private $groupManager; - /** @var \OCP\IDBConnection|\PHPUnit_Framework_MockObject_MockObject $dbConnection */ - private $dbConnection; + /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject $l10n */ + private $config; - /** @var \OCP\IL10N|\PHPUnit_Framework_MockObject_MockObject $l10n */ + /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject $l10n */ private $l10n; + /** @var CalDavBackend|\PHPUnit_Framework_MockObject_MockObject $l10n */ + private $calDav; + /** @var MoveCalendar */ private $command; @@ -58,14 +61,16 @@ class MoveCalendarTest extends TestCase { $this->userManager = $this->createMock(IUserManager::class); $this->groupManager = $this->createMock(IGroupManager::class); - $this->dbConnection = $this->createMock(IDBConnection::class); + $this->config = $this->createMock(IConfig::class); $this->l10n = $this->createMock(IL10N::class); + $this->calDav = $this->createMock(CalDavBackend::class); $this->command = new MoveCalendar( $this->userManager, $this->groupManager, - $this->dbConnection, - $this->l10n + $this->config, + $this->l10n, + $this->calDav ); } @@ -118,4 +123,229 @@ class MoveCalendarTest extends TestCase { 'userdestination' => 'user2', ]); } -}
\ No newline at end of file + + /** + * @expectedException InvalidArgumentException + * @expectedExceptionMessage User <user> has no calendar named <personal>. You can run occ dav:list-calendars to list calendars URIs for this user. + */ + public function testMoveWithInexistantCalendar() + { + $this->userManager->expects($this->at(0)) + ->method('userExists') + ->with('user') + ->willReturn(true); + + $this->userManager->expects($this->at(1)) + ->method('userExists') + ->with('user2') + ->willReturn(true); + + $this->calDav->expects($this->once())->method('getCalendarByUri') + ->with('principals/users/user', 'personal') + ->willReturn(null); + + $commandTester = new CommandTester($this->command); + $commandTester->execute([ + 'name' => 'personal', + 'userorigin' => 'user', + 'userdestination' => 'user2', + ]); + } + + /** + * @expectedException InvalidArgumentException + * @expectedExceptionMessage User <user2> already has a calendar named <personal>. + */ + public function testMoveWithExistingDestinationCalendar() + { + $this->userManager->expects($this->at(0)) + ->method('userExists') + ->with('user') + ->willReturn(true); + + $this->userManager->expects($this->at(1)) + ->method('userExists') + ->with('user2') + ->willReturn(true); + + $this->calDav->expects($this->at(0))->method('getCalendarByUri') + ->with('principals/users/user', 'personal') + ->willReturn([ + 'id' => 1234, + ]); + + $this->calDav->expects($this->at(1))->method('getCalendarByUri') + ->with('principals/users/user2', 'personal') + ->willReturn([ + 'id' => 1234, + ]); + + $commandTester = new CommandTester($this->command); + $commandTester->execute([ + 'name' => 'personal', + 'userorigin' => 'user', + 'userdestination' => 'user2', + ]); + } + + public function testMove() + { + $this->userManager->expects($this->at(0)) + ->method('userExists') + ->with('user') + ->willReturn(true); + + $this->userManager->expects($this->at(1)) + ->method('userExists') + ->with('user2') + ->willReturn(true); + + $this->calDav->expects($this->at(0))->method('getCalendarByUri') + ->with('principals/users/user', 'personal') + ->willReturn([ + 'id' => 1234, + ]); + + $this->calDav->expects($this->at(1))->method('getCalendarByUri') + ->with('principals/users/user2', 'personal') + ->willReturn(null); + + $this->calDav->expects($this->once())->method('getShares') + ->with(1234) + ->willReturn([]); + + $commandTester = new CommandTester($this->command); + $commandTester->execute([ + 'name' => 'personal', + 'userorigin' => 'user', + 'userdestination' => 'user2', + ]); + + $this->assertContains("[OK] Calendar <personal> was moved from user <user> to <user2>", $commandTester->getDisplay()); + } + + /** + * @expectedException InvalidArgumentException + * @expectedExceptionMessage User <user2> is not part of the group <nextclouders> with whom the calendar <personal> was shared. You may use -f to move the calendar while deleting this share. + */ + public function testMoveWithDestinationNotPartOfGroup() + { + $this->userManager->expects($this->at(0)) + ->method('userExists') + ->with('user') + ->willReturn(true); + + $this->userManager->expects($this->at(1)) + ->method('userExists') + ->with('user2') + ->willReturn(true); + + $this->calDav->expects($this->at(0))->method('getCalendarByUri') + ->with('principals/users/user', 'personal') + ->willReturn([ + 'id' => 1234, + 'uri' => 'personal' + ]); + + $this->calDav->expects($this->at(1))->method('getCalendarByUri') + ->with('principals/users/user2', 'personal') + ->willReturn(null); + + $this->calDav->expects($this->once())->method('getShares') + ->with(1234) + ->willReturn([ + ['href' => 'principal:principals/groups/nextclouders'] + ]); + + $commandTester = new CommandTester($this->command); + $commandTester->execute([ + 'name' => 'personal', + 'userorigin' => 'user', + 'userdestination' => 'user2', + ]); + } + + public function testMoveWithDestinationPartOfGroup() + { + $this->userManager->expects($this->at(0)) + ->method('userExists') + ->with('user') + ->willReturn(true); + + $this->userManager->expects($this->at(1)) + ->method('userExists') + ->with('user2') + ->willReturn(true); + + $this->calDav->expects($this->at(0))->method('getCalendarByUri') + ->with('principals/users/user', 'personal') + ->willReturn([ + 'id' => 1234, + 'uri' => 'personal' + ]); + + $this->calDav->expects($this->at(1))->method('getCalendarByUri') + ->with('principals/users/user2', 'personal') + ->willReturn(null); + + $this->calDav->expects($this->once())->method('getShares') + ->with(1234) + ->willReturn([ + ['href' => 'principal:principals/groups/nextclouders'] + ]); + + $this->groupManager->expects($this->once())->method('isInGroup') + ->with('user2', 'nextclouders') + ->willReturn(true); + + $commandTester = new CommandTester($this->command); + $commandTester->execute([ + 'name' => 'personal', + 'userorigin' => 'user', + 'userdestination' => 'user2', + ]); + + $this->assertContains("[OK] Calendar <personal> was moved from user <user> to <user2>", $commandTester->getDisplay()); + } + + public function testMoveWithDestinationNotPartOfGroupAndForce() + { + $this->userManager->expects($this->at(0)) + ->method('userExists') + ->with('user') + ->willReturn(true); + + $this->userManager->expects($this->at(1)) + ->method('userExists') + ->with('user2') + ->willReturn(true); + + $this->calDav->expects($this->at(0))->method('getCalendarByUri') + ->with('principals/users/user', 'personal') + ->willReturn([ + 'id' => 1234, + 'uri' => 'personal', + '{DAV:}displayname' => 'personal' + ]); + + $this->calDav->expects($this->at(1))->method('getCalendarByUri') + ->with('principals/users/user2', 'personal') + ->willReturn(null); + + $this->calDav->expects($this->once())->method('getShares') + ->with(1234) + ->willReturn([ + ['href' => 'principal:principals/groups/nextclouders'] + ]); + + $commandTester = new CommandTester($this->command); + $commandTester->execute([ + 'name' => 'personal', + 'userorigin' => 'user', + 'userdestination' => 'user2', + '--force' => true + ]); + + $this->assertContains("[OK] Calendar <personal> was moved from user <user> to <user2>", $commandTester->getDisplay()); + } +} |