2017-07-26 13:33:32 +03:00
< ? php
/**
2019-12-03 21:57:53 +03:00
*
*
* @ author Georg Ehrke < oc . list @ georgehrke . com >
* @ author Roeland Jago Douma < roeland @ famdouma . nl >
2017-07-26 13:33:32 +03:00
* @ author Thomas Citharel < tcit @ tcit . fr >
*
2019-12-03 21:57:53 +03:00
* @ license GNU AGPL version 3 or any later version
2017-07-26 13:33:32 +03:00
*
2019-12-03 21:57:53 +03:00
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation , either version 3 of the
* License , or ( at your option ) any later version .
2017-07-26 13:33:32 +03:00
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
2019-12-03 21:57:53 +03:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
2017-07-26 13:33:32 +03:00
* GNU Affero General Public License for more details .
*
2019-12-03 21:57:53 +03:00
* You should have received a copy of the GNU Affero General Public License
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2017-07-26 13:33:32 +03:00
*
*/
namespace OCA\DAV\Tests\Command ;
use InvalidArgumentException ;
2018-03-08 16:44:02 +03:00
use OCA\DAV\CalDAV\CalDavBackend ;
2017-07-26 13:33:32 +03:00
use OCA\DAV\Command\MoveCalendar ;
2018-03-08 16:44:02 +03:00
use OCP\IConfig ;
2017-07-26 13:33:32 +03:00
use OCP\IGroupManager ;
use OCP\IL10N ;
use OCP\IUserManager ;
2019-01-03 20:31:10 +03:00
use OCP\Share\IManager ;
2017-07-26 13:33:32 +03:00
use Symfony\Component\Console\Tester\CommandTester ;
use Test\TestCase ;
/**
* Class MoveCalendarTest
*
* @ package OCA\DAV\Tests\Command
*/
class MoveCalendarTest extends TestCase {
2019-01-15 15:59:54 +03:00
/** @var \OCP\IUserManager|\PHPUnit\Framework\MockObject\MockObject $userManager */
2017-07-26 13:33:32 +03:00
private $userManager ;
2019-01-15 15:59:54 +03:00
/** @var \OCP\IGroupManager|\PHPUnit\Framework\MockObject\MockObject $groupManager */
2017-07-26 13:33:32 +03:00
private $groupManager ;
2019-01-03 20:31:10 +03:00
/** @var \OCP\Share\IManager|\PHPUnit_Framework_MockObject_MockObject $shareManager */
private $shareManager ;
2018-03-08 16:44:02 +03:00
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject $l10n */
private $config ;
2017-07-26 13:33:32 +03:00
2018-03-08 16:44:02 +03:00
/** @var IL10N|\PHPUnit_Framework_MockObject_MockObject $l10n */
2017-07-26 13:33:32 +03:00
private $l10n ;
2018-03-08 16:44:02 +03:00
/** @var CalDavBackend|\PHPUnit_Framework_MockObject_MockObject $l10n */
private $calDav ;
2017-07-26 13:33:32 +03:00
/** @var MoveCalendar */
private $command ;
2019-11-21 18:40:38 +03:00
protected function setUp () : void {
2017-07-26 13:33:32 +03:00
parent :: setUp ();
$this -> userManager = $this -> createMock ( IUserManager :: class );
$this -> groupManager = $this -> createMock ( IGroupManager :: class );
2019-01-03 20:31:10 +03:00
$this -> shareManager = $this -> createMock ( IManager :: class );
2018-03-08 16:44:02 +03:00
$this -> config = $this -> createMock ( IConfig :: class );
2017-07-26 13:33:32 +03:00
$this -> l10n = $this -> createMock ( IL10N :: class );
2018-03-08 16:44:02 +03:00
$this -> calDav = $this -> createMock ( CalDavBackend :: class );
2017-07-26 13:33:32 +03:00
$this -> command = new MoveCalendar (
$this -> userManager ,
$this -> groupManager ,
2019-01-03 20:31:10 +03:00
$this -> shareManager ,
2018-03-08 16:44:02 +03:00
$this -> config ,
$this -> l10n ,
$this -> calDav
2017-07-26 13:33:32 +03:00
);
}
public function dataExecute () {
return [
[ false , true ],
[ true , false ]
];
}
/**
* @ dataProvider dataExecute
*
* @ param $userOriginExists
* @ param $userDestinationExists
*/
public function testWithBadUserOrigin ( $userOriginExists , $userDestinationExists )
{
2019-11-27 17:27:18 +03:00
$this -> expectException ( \InvalidArgumentException :: class );
2017-07-26 13:33:32 +03:00
$this -> userManager -> expects ( $this -> at ( 0 ))
-> method ( 'userExists' )
-> with ( 'user' )
-> willReturn ( $userOriginExists );
if ( ! $userDestinationExists ) {
$this -> userManager -> expects ( $this -> at ( 1 ))
-> method ( 'userExists' )
-> with ( 'user2' )
-> willReturn ( $userDestinationExists );
}
$commandTester = new CommandTester ( $this -> command );
$commandTester -> execute ([
'name' => $this -> command -> getName (),
2019-01-15 15:59:54 +03:00
'sourceuid' => 'user' ,
'destinationuid' => 'user2' ,
2017-07-26 13:33:32 +03:00
]);
}
2019-11-27 17:27:18 +03:00
2018-03-08 16:44:02 +03:00
public function testMoveWithInexistantCalendar ()
{
2019-11-27 17:27:18 +03: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.' );
2018-03-08 16:44:02 +03:00
$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' ,
2019-01-15 15:59:54 +03:00
'sourceuid' => 'user' ,
'destinationuid' => 'user2' ,
2018-03-08 16:44:02 +03:00
]);
}
2019-11-27 17:27:18 +03:00
2018-03-08 16:44:02 +03:00
public function testMoveWithExistingDestinationCalendar ()
{
2019-11-27 17:27:18 +03:00
$this -> expectException ( \InvalidArgumentException :: class );
$this -> expectExceptionMessage ( 'User <user2> already has a calendar named <personal>.' );
2018-03-08 16:44:02 +03:00
$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' ,
2019-01-15 15:59:54 +03:00
'sourceuid' => 'user' ,
'destinationuid' => 'user2' ,
2018-03-08 16:44:02 +03:00
]);
}
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' ,
2019-01-15 15:59:54 +03:00
'sourceuid' => 'user' ,
'destinationuid' => 'user2' ,
2018-03-08 16:44:02 +03:00
]);
$this -> assertContains ( " [OK] Calendar <personal> was moved from user <user> to <user2> " , $commandTester -> getDisplay ());
}
2019-01-03 20:31:10 +03:00
public function dataTestMoveWithDestinationNotPartOfGroup () : array
{
return [
[ true ],
[ false ]
];
}
2018-03-08 16:44:02 +03:00
/**
2019-01-03 20:31:10 +03:00
* @ dataProvider dataTestMoveWithDestinationNotPartOfGroup
2018-03-08 16:44:02 +03:00
*/
2019-01-03 20:31:10 +03:00
public function testMoveWithDestinationNotPartOfGroup ( bool $shareWithGroupMembersOnly )
2018-03-08 16:44:02 +03:00
{
$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 );
2019-01-03 20:31:10 +03:00
$this -> shareManager -> expects ( $this -> once ()) -> method ( 'shareWithGroupMembersOnly' )
-> willReturn ( $shareWithGroupMembersOnly );
2019-01-15 15:59:54 +03:00
$this -> calDav -> expects ( $this -> once ()) -> method ( 'getShares' )
-> with ( 1234 )
-> willReturn ([
[ 'href' => 'principal:principals/groups/nextclouders' ]
]);
2019-01-03 20:31:10 +03: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 16:44:02 +03:00
$commandTester = new CommandTester ( $this -> command );
$commandTester -> execute ([
'name' => 'personal' ,
2019-01-15 15:59:54 +03:00
'sourceuid' => 'user' ,
'destinationuid' => 'user2' ,
2018-03-08 16:44:02 +03:00
]);
}
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 );
2019-01-03 20:31:10 +03:00
$this -> shareManager -> expects ( $this -> once ()) -> method ( 'shareWithGroupMembersOnly' )
-> willReturn ( true );
2018-03-08 16:44:02 +03: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 15:59:54 +03:00
'sourceuid' => 'user' ,
'destinationuid' => 'user2' ,
2018-03-08 16:44:02 +03:00
]);
$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' ,
2019-01-15 15:59:54 +03:00
'{DAV:}displayname' => 'Personal'
2018-03-08 16:44:02 +03:00
]);
$this -> calDav -> expects ( $this -> at ( 1 )) -> method ( 'getCalendarByUri' )
-> with ( 'principals/users/user2' , 'personal' )
-> willReturn ( null );
2019-01-03 20:31:10 +03:00
$this -> shareManager -> expects ( $this -> once ()) -> method ( 'shareWithGroupMembersOnly' )
-> willReturn ( true );
2018-03-08 16:44:02 +03:00
$this -> calDav -> expects ( $this -> once ()) -> method ( 'getShares' )
-> with ( 1234 )
-> willReturn ([
2019-01-15 15:59:54 +03:00
[
'href' => 'principal:principals/groups/nextclouders' ,
'{DAV:}displayname' => 'Personal'
]
2018-03-08 16:44:02 +03:00
]);
2019-01-15 15:59:54 +03:00
$this -> calDav -> expects ( $this -> once ()) -> method ( 'updateShares' );
2018-03-08 16:44:02 +03:00
$commandTester = new CommandTester ( $this -> command );
$commandTester -> execute ([
'name' => 'personal' ,
2019-01-15 15:59:54 +03:00
'sourceuid' => 'user' ,
'destinationuid' => 'user2' ,
2018-03-08 16:44:02 +03:00
'--force' => true
]);
$this -> assertContains ( " [OK] Calendar <personal> was moved from user <user> to <user2> " , $commandTester -> getDisplay ());
}
2019-01-15 15:59:54 +03:00
public function dataTestMoveWithCalendarAlreadySharedToDestination () : array
{
return [
[ true ],
[ false ]
];
}
/**
* @ dataProvider dataTestMoveWithCalendarAlreadySharedToDestination
*/
public function testMoveWithCalendarAlreadySharedToDestination ( bool $force )
{
$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/users/user2' ,
'{DAV:}displayname' => 'Personal'
]
]);
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 16:44:02 +03:00
}