You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MoveCalendarTest.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Thomas Citharel <nextcloud@tcit.fr>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Citharel <nextcloud@tcit.fr>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\DAV\Tests\Command;
  28. use InvalidArgumentException;
  29. use OCA\DAV\CalDAV\CalDavBackend;
  30. use OCA\DAV\Command\MoveCalendar;
  31. use OCP\IConfig;
  32. use OCP\IGroupManager;
  33. use OCP\IL10N;
  34. use OCP\IUserManager;
  35. use OCP\Share\IManager;
  36. use PHPUnit\Framework\MockObject\MockObject;
  37. use Psr\Log\LoggerInterface;
  38. use Symfony\Component\Console\Tester\CommandTester;
  39. use Test\TestCase;
  40. /**
  41. * Class MoveCalendarTest
  42. *
  43. * @package OCA\DAV\Tests\Command
  44. */
  45. class MoveCalendarTest extends TestCase {
  46. /** @var \OCP\IUserManager|MockObject $userManager */
  47. private $userManager;
  48. /** @var \OCP\IGroupManager|MockObject $groupManager */
  49. private $groupManager;
  50. /** @var \OCP\Share\IManager|MockObject $shareManager */
  51. private $shareManager;
  52. /** @var IConfig|MockObject $l10n */
  53. private $config;
  54. /** @var IL10N|MockObject $l10n */
  55. private $l10n;
  56. /** @var CalDavBackend|MockObject $l10n */
  57. private $calDav;
  58. /** @var MoveCalendar */
  59. private $command;
  60. /** @var LoggerInterface|MockObject */
  61. private $logger;
  62. protected function setUp(): void {
  63. parent::setUp();
  64. $this->userManager = $this->createMock(IUserManager::class);
  65. $this->groupManager = $this->createMock(IGroupManager::class);
  66. $this->shareManager = $this->createMock(IManager::class);
  67. $this->config = $this->createMock(IConfig::class);
  68. $this->l10n = $this->createMock(IL10N::class);
  69. $this->calDav = $this->createMock(CalDavBackend::class);
  70. $this->logger = $this->createMock(LoggerInterface::class);
  71. $this->command = new MoveCalendar(
  72. $this->userManager,
  73. $this->groupManager,
  74. $this->shareManager,
  75. $this->config,
  76. $this->l10n,
  77. $this->calDav,
  78. $this->logger
  79. );
  80. }
  81. public function dataExecute() {
  82. return [
  83. [false, true],
  84. [true, false]
  85. ];
  86. }
  87. /**
  88. * @dataProvider dataExecute
  89. *
  90. * @param $userOriginExists
  91. * @param $userDestinationExists
  92. */
  93. public function testWithBadUserOrigin($userOriginExists, $userDestinationExists): void {
  94. $this->expectException(\InvalidArgumentException::class);
  95. $this->userManager->expects($this->exactly($userOriginExists ? 2 : 1))
  96. ->method('userExists')
  97. ->withConsecutive(
  98. ['user'],
  99. ['user2'],
  100. )
  101. ->willReturnOnConsecutiveCalls(
  102. $userOriginExists,
  103. $userDestinationExists,
  104. );
  105. $commandTester = new CommandTester($this->command);
  106. $commandTester->execute([
  107. 'name' => $this->command->getName(),
  108. 'sourceuid' => 'user',
  109. 'destinationuid' => 'user2',
  110. ]);
  111. }
  112. public function testMoveWithInexistantCalendar(): void {
  113. $this->expectException(\InvalidArgumentException::class);
  114. $this->expectExceptionMessage('User <user> has no calendar named <personal>. You can run occ dav:list-calendars to list calendars URIs for this user.');
  115. $this->userManager->expects($this->exactly(2))
  116. ->method('userExists')
  117. ->withConsecutive(
  118. ['user'],
  119. ['user2'],
  120. )
  121. ->willReturn(true);
  122. $this->calDav->expects($this->once())->method('getCalendarByUri')
  123. ->with('principals/users/user', 'personal')
  124. ->willReturn(null);
  125. $commandTester = new CommandTester($this->command);
  126. $commandTester->execute([
  127. 'name' => 'personal',
  128. 'sourceuid' => 'user',
  129. 'destinationuid' => 'user2',
  130. ]);
  131. }
  132. public function testMoveWithExistingDestinationCalendar(): void {
  133. $this->expectException(\InvalidArgumentException::class);
  134. $this->expectExceptionMessage('User <user2> already has a calendar named <personal>.');
  135. $this->userManager->expects($this->exactly(2))
  136. ->method('userExists')
  137. ->withConsecutive(
  138. ['user'],
  139. ['user2'],
  140. )
  141. ->willReturn(true);
  142. $this->calDav->expects($this->exactly(2))
  143. ->method('getCalendarByUri')
  144. ->withConsecutive(
  145. ['principals/users/user', 'personal'],
  146. ['principals/users/user2', 'personal'],
  147. )
  148. ->willReturn([
  149. 'id' => 1234,
  150. ]);
  151. $commandTester = new CommandTester($this->command);
  152. $commandTester->execute([
  153. 'name' => 'personal',
  154. 'sourceuid' => 'user',
  155. 'destinationuid' => 'user2',
  156. ]);
  157. }
  158. public function testMove(): void {
  159. $this->userManager->expects($this->exactly(2))
  160. ->method('userExists')
  161. ->withConsecutive(
  162. ['user'],
  163. ['user2'],
  164. )
  165. ->willReturn(true);
  166. $this->calDav->expects($this->exactly(2))
  167. ->method('getCalendarByUri')
  168. ->withConsecutive(
  169. ['principals/users/user', 'personal'],
  170. ['principals/users/user2', 'personal'],
  171. )
  172. ->willReturnOnConsecutiveCalls(
  173. [
  174. 'id' => 1234,
  175. ],
  176. null,
  177. );
  178. $this->calDav->expects($this->once())->method('getShares')
  179. ->with(1234)
  180. ->willReturn([]);
  181. $commandTester = new CommandTester($this->command);
  182. $commandTester->execute([
  183. 'name' => 'personal',
  184. 'sourceuid' => 'user',
  185. 'destinationuid' => 'user2',
  186. ]);
  187. $this->assertStringContainsString("[OK] Calendar <personal> was moved from user <user> to <user2>", $commandTester->getDisplay());
  188. }
  189. public function dataTestMoveWithDestinationNotPartOfGroup(): array {
  190. return [
  191. [true],
  192. [false]
  193. ];
  194. }
  195. /**
  196. * @dataProvider dataTestMoveWithDestinationNotPartOfGroup
  197. */
  198. public function testMoveWithDestinationNotPartOfGroup(bool $shareWithGroupMembersOnly): void {
  199. $this->userManager->expects($this->exactly(2))
  200. ->method('userExists')
  201. ->withConsecutive(
  202. ['user'],
  203. ['user2'],
  204. )
  205. ->willReturn(true);
  206. $this->calDav->expects($this->exactly(2))
  207. ->method('getCalendarByUri')
  208. ->withConsecutive(
  209. ['principals/users/user', 'personal'],
  210. ['principals/users/user2', 'personal'],
  211. )
  212. ->willReturnOnConsecutiveCalls(
  213. [
  214. 'id' => 1234,
  215. 'uri' => 'personal',
  216. ],
  217. null,
  218. );
  219. $this->shareManager->expects($this->once())->method('shareWithGroupMembersOnly')
  220. ->willReturn($shareWithGroupMembersOnly);
  221. $this->calDav->expects($this->once())->method('getShares')
  222. ->with(1234)
  223. ->willReturn([
  224. ['href' => 'principal:principals/groups/nextclouders']
  225. ]);
  226. if ($shareWithGroupMembersOnly === true) {
  227. $this->expectException(InvalidArgumentException::class);
  228. $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.");
  229. }
  230. $commandTester = new CommandTester($this->command);
  231. $commandTester->execute([
  232. 'name' => 'personal',
  233. 'sourceuid' => 'user',
  234. 'destinationuid' => 'user2',
  235. ]);
  236. }
  237. public function testMoveWithDestinationPartOfGroup(): void {
  238. $this->userManager->expects($this->exactly(2))
  239. ->method('userExists')
  240. ->withConsecutive(
  241. ['user'],
  242. ['user2'],
  243. )
  244. ->willReturn(true);
  245. $this->calDav->expects($this->exactly(2))
  246. ->method('getCalendarByUri')
  247. ->withConsecutive(
  248. ['principals/users/user', 'personal'],
  249. ['principals/users/user2', 'personal'],
  250. )
  251. ->willReturnOnConsecutiveCalls(
  252. [
  253. 'id' => 1234,
  254. 'uri' => 'personal',
  255. ],
  256. null,
  257. );
  258. $this->shareManager->expects($this->once())->method('shareWithGroupMembersOnly')
  259. ->willReturn(true);
  260. $this->calDav->expects($this->once())->method('getShares')
  261. ->with(1234)
  262. ->willReturn([
  263. ['href' => 'principal:principals/groups/nextclouders']
  264. ]);
  265. $this->groupManager->expects($this->once())->method('isInGroup')
  266. ->with('user2', 'nextclouders')
  267. ->willReturn(true);
  268. $commandTester = new CommandTester($this->command);
  269. $commandTester->execute([
  270. 'name' => 'personal',
  271. 'sourceuid' => 'user',
  272. 'destinationuid' => 'user2',
  273. ]);
  274. $this->assertStringContainsString("[OK] Calendar <personal> was moved from user <user> to <user2>", $commandTester->getDisplay());
  275. }
  276. public function testMoveWithDestinationNotPartOfGroupAndForce(): void {
  277. $this->userManager->expects($this->exactly(2))
  278. ->method('userExists')
  279. ->withConsecutive(
  280. ['user'],
  281. ['user2'],
  282. )
  283. ->willReturn(true);
  284. $this->calDav->expects($this->exactly(2))
  285. ->method('getCalendarByUri')
  286. ->withConsecutive(
  287. ['principals/users/user', 'personal'],
  288. ['principals/users/user2', 'personal'],
  289. )
  290. ->willReturnOnConsecutiveCalls(
  291. [
  292. 'id' => 1234,
  293. 'uri' => 'personal',
  294. '{DAV:}displayname' => 'Personal'
  295. ],
  296. null,
  297. );
  298. $this->shareManager->expects($this->once())->method('shareWithGroupMembersOnly')
  299. ->willReturn(true);
  300. $this->calDav->expects($this->once())->method('getShares')
  301. ->with(1234)
  302. ->willReturn([
  303. [
  304. 'href' => 'principal:principals/groups/nextclouders',
  305. '{DAV:}displayname' => 'Personal'
  306. ]
  307. ]);
  308. $this->calDav->expects($this->once())->method('updateShares');
  309. $commandTester = new CommandTester($this->command);
  310. $commandTester->execute([
  311. 'name' => 'personal',
  312. 'sourceuid' => 'user',
  313. 'destinationuid' => 'user2',
  314. '--force' => true
  315. ]);
  316. $this->assertStringContainsString("[OK] Calendar <personal> was moved from user <user> to <user2>", $commandTester->getDisplay());
  317. }
  318. public function dataTestMoveWithCalendarAlreadySharedToDestination(): array {
  319. return [
  320. [true],
  321. [false]
  322. ];
  323. }
  324. /**
  325. * @dataProvider dataTestMoveWithCalendarAlreadySharedToDestination
  326. */
  327. public function testMoveWithCalendarAlreadySharedToDestination(bool $force): void {
  328. $this->userManager->expects($this->exactly(2))
  329. ->method('userExists')
  330. ->withConsecutive(
  331. ['user'],
  332. ['user2'],
  333. )
  334. ->willReturn(true);
  335. $this->calDav->expects($this->exactly(2))
  336. ->method('getCalendarByUri')
  337. ->withConsecutive(
  338. ['principals/users/user', 'personal'],
  339. ['principals/users/user2', 'personal'],
  340. )
  341. ->willReturnOnConsecutiveCalls(
  342. [
  343. 'id' => 1234,
  344. 'uri' => 'personal',
  345. '{DAV:}displayname' => 'Personal'
  346. ],
  347. null,
  348. );
  349. $this->calDav->expects($this->once())->method('getShares')
  350. ->with(1234)
  351. ->willReturn([
  352. [
  353. 'href' => 'principal:principals/users/user2',
  354. '{DAV:}displayname' => 'Personal'
  355. ]
  356. ]);
  357. if ($force === false) {
  358. $this->expectException(InvalidArgumentException::class);
  359. $this->expectExceptionMessage("The calendar <personal> is already shared to user <user2>.You may use -f to move the calendar while deleting this share.");
  360. } else {
  361. $this->calDav->expects($this->once())->method('updateShares');
  362. }
  363. $commandTester = new CommandTester($this->command);
  364. $commandTester->execute([
  365. 'name' => 'personal',
  366. 'sourceuid' => 'user',
  367. 'destinationuid' => 'user2',
  368. '--force' => $force,
  369. ]);
  370. }
  371. }