2017-07-26 06:33:32 -04:00
< ? php
/**
2024-05-28 06:34:11 -04:00
* SPDX - FileCopyrightText : 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX - License - Identifier : AGPL - 3.0 - or - later
2017-07-26 06:33:32 -04:00
*/
namespace OCA\DAV\Tests\Command ;
use InvalidArgumentException ;
2018-03-08 08:44:02 -05:00
use OCA\DAV\CalDAV\CalDavBackend ;
2017-07-26 06:33:32 -04:00
use OCA\DAV\Command\MoveCalendar ;
2018-03-08 08:44:02 -05:00
use OCP\IConfig ;
2017-07-26 06:33:32 -04:00
use OCP\IGroupManager ;
use OCP\IL10N ;
use OCP\IUserManager ;
2019-01-03 12:31:10 -05:00
use OCP\Share\IManager ;
2021-12-06 14:01:22 -05:00
use PHPUnit\Framework\MockObject\MockObject ;
use Psr\Log\LoggerInterface ;
2017-07-26 06:33:32 -04:00
use Symfony\Component\Console\Tester\CommandTester ;
use Test\TestCase ;
/**
* Class MoveCalendarTest
*
* @ package OCA\DAV\Tests\Command
*/
class MoveCalendarTest extends TestCase {
2021-12-06 14:01:22 -05:00
/** @var \OCP\IUserManager|MockObject $userManager */
2017-07-26 06:33:32 -04:00
private $userManager ;
2021-12-06 14:01:22 -05:00
/** @var \OCP\IGroupManager|MockObject $groupManager */
2017-07-26 06:33:32 -04:00
private $groupManager ;
2021-12-06 14:01:22 -05:00
/** @var \OCP\Share\IManager|MockObject $shareManager */
2019-01-03 12:31:10 -05:00
private $shareManager ;
2021-12-06 14:01:22 -05:00
/** @var IConfig|MockObject $l10n */
2018-03-08 08:44:02 -05:00
private $config ;
2017-07-26 06:33:32 -04:00
2021-12-06 14:01:22 -05:00
/** @var IL10N|MockObject $l10n */
2017-07-26 06:33:32 -04:00
private $l10n ;
2021-12-06 14:01:22 -05:00
/** @var CalDavBackend|MockObject $l10n */
2018-03-08 08:44:02 -05:00
private $calDav ;
2017-07-26 06:33:32 -04:00
/** @var MoveCalendar */
private $command ;
2021-12-06 14:01:22 -05:00
/** @var LoggerInterface|MockObject */
private $logger ;
2019-11-21 10:40:38 -05:00
protected function setUp () : void {
2017-07-26 06:33:32 -04:00
parent :: setUp ();
$this -> userManager = $this -> createMock ( IUserManager :: class );
$this -> groupManager = $this -> createMock ( IGroupManager :: class );
2019-01-03 12:31:10 -05:00
$this -> shareManager = $this -> createMock ( IManager :: class );
2018-03-08 08:44:02 -05:00
$this -> config = $this -> createMock ( IConfig :: class );
2017-07-26 06:33:32 -04:00
$this -> l10n = $this -> createMock ( IL10N :: class );
2018-03-08 08:44:02 -05:00
$this -> calDav = $this -> createMock ( CalDavBackend :: class );
2021-12-06 14:01:22 -05:00
$this -> logger = $this -> createMock ( LoggerInterface :: class );
2017-07-26 06:33:32 -04:00
$this -> command = new MoveCalendar (
$this -> userManager ,
$this -> groupManager ,
2019-01-03 12:31:10 -05:00
$this -> shareManager ,
2018-03-08 08:44:02 -05:00
$this -> config ,
$this -> l10n ,
2021-12-06 14:01:22 -05:00
$this -> calDav ,
$this -> logger
2017-07-26 06:33:32 -04:00
);
}
public function dataExecute () {
return [
[ false , true ],
[ true , false ]
];
}
/**
* @ dataProvider dataExecute
*
* @ param $userOriginExists
* @ param $userDestinationExists
*/
2023-01-20 02:38:43 -05:00
public function testWithBadUserOrigin ( $userOriginExists , $userDestinationExists ) : void {
2019-11-27 09:27:18 -05:00
$this -> expectException ( \InvalidArgumentException :: class );
2023-01-05 12:25:31 -05:00
$this -> userManager -> expects ( $this -> exactly ( $userOriginExists ? 2 : 1 ))
2017-07-26 06:33:32 -04:00
-> method ( 'userExists' )
2023-01-05 12:25:31 -05:00
-> withConsecutive (
[ 'user' ],
[ 'user2' ],
)
-> willReturnOnConsecutiveCalls (
$userOriginExists ,
$userDestinationExists ,
);
2017-07-26 06:33:32 -04:00
$commandTester = new CommandTester ( $this -> command );
$commandTester -> execute ([
'name' => $this -> command -> getName (),
2019-01-15 07:59:54 -05:00
'sourceuid' => 'user' ,
'destinationuid' => 'user2' ,
2017-07-26 06:33:32 -04:00
]);
}
2020-07-23 07:36:32 -04:00
2023-01-20 02:38:43 -05:00
public function testMoveWithInexistantCalendar () : void {
2019-11-27 09:27:18 -05:00
$this -> expectException ( \InvalidArgumentException :: class );
$this -> expectExceptionMessage ( 'User <user> has no calendar named <personal>. You can run occ dav:list-calendars to list calendars URIs for this user.' );
2023-01-05 12:25:31 -05:00
$this -> userManager -> expects ( $this -> exactly ( 2 ))
2018-03-08 08:44:02 -05:00
-> method ( 'userExists' )
2023-01-05 12:25:31 -05:00
-> withConsecutive (
[ 'user' ],
[ 'user2' ],
)
2018-03-08 08:44:02 -05:00
-> 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' ,
2019-01-15 07:59:54 -05:00
'sourceuid' => 'user' ,
'destinationuid' => 'user2' ,
2018-03-08 08:44:02 -05:00
]);
}
2020-07-23 07:36:32 -04:00
2023-01-20 02:38:43 -05:00
public function testMoveWithExistingDestinationCalendar () : void {
2019-11-27 09:27:18 -05:00
$this -> expectException ( \InvalidArgumentException :: class );
$this -> expectExceptionMessage ( 'User <user2> already has a calendar named <personal>.' );
2023-01-05 12:25:31 -05:00
$this -> userManager -> expects ( $this -> exactly ( 2 ))
2018-03-08 08:44:02 -05:00
-> method ( 'userExists' )
2023-01-05 12:25:31 -05:00
-> withConsecutive (
[ 'user' ],
[ 'user2' ],
)
2018-03-08 08:44:02 -05:00
-> willReturn ( true );
2023-01-05 12:25:31 -05:00
$this -> calDav -> expects ( $this -> exactly ( 2 ))
-> method ( 'getCalendarByUri' )
-> withConsecutive (
[ 'principals/users/user' , 'personal' ],
[ 'principals/users/user2' , 'personal' ],
)
2018-03-08 08:44:02 -05:00
-> willReturn ([
'id' => 1234 ,
]);
$commandTester = new CommandTester ( $this -> command );
$commandTester -> execute ([
'name' => 'personal' ,
2019-01-15 07:59:54 -05:00
'sourceuid' => 'user' ,
'destinationuid' => 'user2' ,
2018-03-08 08:44:02 -05:00
]);
}
2023-01-20 02:38:43 -05:00
public function testMove () : void {
2023-01-05 12:25:31 -05:00
$this -> userManager -> expects ( $this -> exactly ( 2 ))
2018-03-08 08:44:02 -05:00
-> method ( 'userExists' )
2023-01-05 12:25:31 -05:00
-> withConsecutive (
[ 'user' ],
[ 'user2' ],
)
2018-03-08 08:44:02 -05:00
-> willReturn ( true );
2023-01-05 12:25:31 -05:00
$this -> calDav -> expects ( $this -> exactly ( 2 ))
-> method ( 'getCalendarByUri' )
-> withConsecutive (
[ 'principals/users/user' , 'personal' ],
[ 'principals/users/user2' , 'personal' ],
)
-> willReturnOnConsecutiveCalls (
[
'id' => 1234 ,
],
null ,
);
2018-03-08 08:44:02 -05:00
$this -> calDav -> expects ( $this -> once ()) -> method ( 'getShares' )
-> with ( 1234 )
-> willReturn ([]);
$commandTester = new CommandTester ( $this -> command );
$commandTester -> execute ([
'name' => 'personal' ,
2019-01-15 07:59:54 -05:00
'sourceuid' => 'user' ,
'destinationuid' => 'user2' ,
2018-03-08 08:44:02 -05:00
]);
2020-11-30 06:25:37 -05:00
$this -> assertStringContainsString ( '[OK] Calendar <personal> was moved from user <user> to <user2>' , $commandTester -> getDisplay ());
2018-03-08 08:44:02 -05:00
}
2020-04-10 08:19:56 -04:00
public function dataTestMoveWithDestinationNotPartOfGroup () : array {
2019-01-03 12:31:10 -05:00
return [
[ true ],
[ false ]
];
}
2018-03-08 08:44:02 -05:00
/**
2019-01-03 12:31:10 -05:00
* @ dataProvider dataTestMoveWithDestinationNotPartOfGroup
2018-03-08 08:44:02 -05:00
*/
2023-01-20 02:38:43 -05:00
public function testMoveWithDestinationNotPartOfGroup ( bool $shareWithGroupMembersOnly ) : void {
2023-01-05 12:25:31 -05:00
$this -> userManager -> expects ( $this -> exactly ( 2 ))
2018-03-08 08:44:02 -05:00
-> method ( 'userExists' )
2023-01-05 12:25:31 -05:00
-> withConsecutive (
[ 'user' ],
[ 'user2' ],
)
2018-03-08 08:44:02 -05:00
-> willReturn ( true );
2023-01-05 12:25:31 -05:00
$this -> calDav -> expects ( $this -> exactly ( 2 ))
-> method ( 'getCalendarByUri' )
-> withConsecutive (
[ 'principals/users/user' , 'personal' ],
[ 'principals/users/user2' , 'personal' ],
)
-> willReturnOnConsecutiveCalls (
[
'id' => 1234 ,
'uri' => 'personal' ,
],
null ,
);
2018-03-08 08:44:02 -05:00
2019-01-03 12:31:10 -05:00
$this -> shareManager -> expects ( $this -> once ()) -> method ( 'shareWithGroupMembersOnly' )
-> willReturn ( $shareWithGroupMembersOnly );
2019-01-15 07:59:54 -05:00
$this -> calDav -> expects ( $this -> once ()) -> method ( 'getShares' )
-> with ( 1234 )
-> willReturn ([
[ 'href' => 'principal:principals/groups/nextclouders' ]
2020-04-09 03:22:29 -04:00
]);
2019-01-03 12:31:10 -05:00
if ( $shareWithGroupMembersOnly === true ) {
$this -> expectException ( InvalidArgumentException :: class );
$this -> expectExceptionMessage ( '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.' );
}
2018-03-08 08:44:02 -05:00
$commandTester = new CommandTester ( $this -> command );
$commandTester -> execute ([
'name' => 'personal' ,
2019-01-15 07:59:54 -05:00
'sourceuid' => 'user' ,
'destinationuid' => 'user2' ,
2018-03-08 08:44:02 -05:00
]);
}
2023-01-20 02:38:43 -05:00
public function testMoveWithDestinationPartOfGroup () : void {
2023-01-05 12:25:31 -05:00
$this -> userManager -> expects ( $this -> exactly ( 2 ))
2018-03-08 08:44:02 -05:00
-> method ( 'userExists' )
2023-01-05 12:25:31 -05:00
-> withConsecutive (
[ 'user' ],
[ 'user2' ],
)
2018-03-08 08:44:02 -05:00
-> willReturn ( true );
2023-01-05 12:25:31 -05:00
$this -> calDav -> expects ( $this -> exactly ( 2 ))
-> method ( 'getCalendarByUri' )
-> withConsecutive (
[ 'principals/users/user' , 'personal' ],
[ 'principals/users/user2' , 'personal' ],
)
-> willReturnOnConsecutiveCalls (
[
'id' => 1234 ,
'uri' => 'personal' ,
],
null ,
);
2018-03-08 08:44:02 -05:00
2019-01-03 12:31:10 -05:00
$this -> shareManager -> expects ( $this -> once ()) -> method ( 'shareWithGroupMembersOnly' )
-> willReturn ( true );
2018-03-08 08:44:02 -05:00
$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' ,
2019-01-15 07:59:54 -05:00
'sourceuid' => 'user' ,
'destinationuid' => 'user2' ,
2018-03-08 08:44:02 -05:00
]);
2020-07-23 07:36:32 -04:00
$this -> assertStringContainsString ( '[OK] Calendar <personal> was moved from user <user> to <user2>' , $commandTester -> getDisplay ());
2018-03-08 08:44:02 -05:00
}
2023-01-20 02:38:43 -05:00
public function testMoveWithDestinationNotPartOfGroupAndForce () : void {
2023-01-05 12:25:31 -05:00
$this -> userManager -> expects ( $this -> exactly ( 2 ))
2018-03-08 08:44:02 -05:00
-> method ( 'userExists' )
2023-01-05 12:25:31 -05:00
-> withConsecutive (
[ 'user' ],
[ 'user2' ],
)
2018-03-08 08:44:02 -05:00
-> willReturn ( true );
2023-01-05 12:25:31 -05:00
$this -> calDav -> expects ( $this -> exactly ( 2 ))
-> method ( 'getCalendarByUri' )
-> withConsecutive (
[ 'principals/users/user' , 'personal' ],
[ 'principals/users/user2' , 'personal' ],
)
-> willReturnOnConsecutiveCalls (
[
'id' => 1234 ,
'uri' => 'personal' ,
'{DAV:}displayname' => 'Personal'
],
null ,
);
2018-03-08 08:44:02 -05:00
2019-01-03 12:31:10 -05:00
$this -> shareManager -> expects ( $this -> once ()) -> method ( 'shareWithGroupMembersOnly' )
-> willReturn ( true );
2018-03-08 08:44:02 -05:00
$this -> calDav -> expects ( $this -> once ()) -> method ( 'getShares' )
-> with ( 1234 )
-> willReturn ([
2019-01-15 07:59:54 -05:00
[
'href' => 'principal:principals/groups/nextclouders' ,
'{DAV:}displayname' => 'Personal'
]
2018-03-08 08:44:02 -05:00
]);
2019-01-15 07:59:54 -05:00
$this -> calDav -> expects ( $this -> once ()) -> method ( 'updateShares' );
2018-03-08 08:44:02 -05:00
$commandTester = new CommandTester ( $this -> command );
$commandTester -> execute ([
'name' => 'personal' ,
2019-01-15 07:59:54 -05:00
'sourceuid' => 'user' ,
'destinationuid' => 'user2' ,
2018-03-08 08:44:02 -05:00
'--force' => true
]);
2020-07-23 07:36:32 -04:00
$this -> assertStringContainsString ( '[OK] Calendar <personal> was moved from user <user> to <user2>' , $commandTester -> getDisplay ());
2018-03-08 08:44:02 -05:00
}
2019-01-15 07:59:54 -05:00
2020-04-10 08:19:56 -04:00
public function dataTestMoveWithCalendarAlreadySharedToDestination () : array {
2019-01-15 07:59:54 -05:00
return [
[ true ],
[ false ]
];
}
/**
* @ dataProvider dataTestMoveWithCalendarAlreadySharedToDestination
*/
2023-01-20 02:38:43 -05:00
public function testMoveWithCalendarAlreadySharedToDestination ( bool $force ) : void {
2023-01-05 12:25:31 -05:00
$this -> userManager -> expects ( $this -> exactly ( 2 ))
2019-01-15 07:59:54 -05:00
-> method ( 'userExists' )
2023-01-05 12:25:31 -05:00
-> withConsecutive (
[ 'user' ],
[ 'user2' ],
)
2019-01-15 07:59:54 -05:00
-> willReturn ( true );
2023-01-05 12:25:31 -05:00
$this -> calDav -> expects ( $this -> exactly ( 2 ))
-> method ( 'getCalendarByUri' )
-> withConsecutive (
[ 'principals/users/user' , 'personal' ],
[ 'principals/users/user2' , 'personal' ],
)
-> willReturnOnConsecutiveCalls (
[
'id' => 1234 ,
'uri' => 'personal' ,
'{DAV:}displayname' => 'Personal'
],
null ,
);
2019-01-15 07:59:54 -05:00
$this -> calDav -> expects ( $this -> once ()) -> method ( 'getShares' )
-> with ( 1234 )
-> willReturn ([
[
'href' => 'principal:principals/users/user2' ,
'{DAV:}displayname' => 'Personal'
]
2020-04-09 03:22:29 -04:00
]);
2019-01-15 07:59:54 -05:00
if ( $force === false ) {
$this -> expectException ( InvalidArgumentException :: class );
$this -> expectExceptionMessage ( 'The calendar <personal> is already shared to user <user2>.You may use -f to move the calendar while deleting this share.' );
} else {
$this -> calDav -> expects ( $this -> once ()) -> method ( 'updateShares' );
}
$commandTester = new CommandTester ( $this -> command );
$commandTester -> execute ([
'name' => 'personal' ,
'sourceuid' => 'user' ,
'destinationuid' => 'user2' ,
'--force' => $force ,
]);
}
2018-03-08 08:44:02 -05:00
}