1
0

MoveCalendarTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?php
  2. /**
  3. *
  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 Symfony\Component\Console\Tester\CommandTester;
  37. use Test\TestCase;
  38. /**
  39. * Class MoveCalendarTest
  40. *
  41. * @package OCA\DAV\Tests\Command
  42. */
  43. class MoveCalendarTest extends TestCase {
  44. /** @var \OCP\IUserManager|\PHPUnit\Framework\MockObject\MockObject $userManager */
  45. private $userManager;
  46. /** @var \OCP\IGroupManager|\PHPUnit\Framework\MockObject\MockObject $groupManager */
  47. private $groupManager;
  48. /** @var \OCP\Share\IManager|\PHPUnit\Framework\MockObject\MockObject $shareManager */
  49. private $shareManager;
  50. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject $l10n */
  51. private $config;
  52. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject $l10n */
  53. private $l10n;
  54. /** @var CalDavBackend|\PHPUnit\Framework\MockObject\MockObject $l10n */
  55. private $calDav;
  56. /** @var MoveCalendar */
  57. private $command;
  58. protected function setUp(): void {
  59. parent::setUp();
  60. $this->userManager = $this->createMock(IUserManager::class);
  61. $this->groupManager = $this->createMock(IGroupManager::class);
  62. $this->shareManager = $this->createMock(IManager::class);
  63. $this->config = $this->createMock(IConfig::class);
  64. $this->l10n = $this->createMock(IL10N::class);
  65. $this->calDav = $this->createMock(CalDavBackend::class);
  66. $this->command = new MoveCalendar(
  67. $this->userManager,
  68. $this->groupManager,
  69. $this->shareManager,
  70. $this->config,
  71. $this->l10n,
  72. $this->calDav
  73. );
  74. }
  75. public function dataExecute() {
  76. return [
  77. [false, true],
  78. [true, false]
  79. ];
  80. }
  81. /**
  82. * @dataProvider dataExecute
  83. *
  84. * @param $userOriginExists
  85. * @param $userDestinationExists
  86. */
  87. public function testWithBadUserOrigin($userOriginExists, $userDestinationExists) {
  88. $this->expectException(\InvalidArgumentException::class);
  89. $this->userManager->expects($this->at(0))
  90. ->method('userExists')
  91. ->with('user')
  92. ->willReturn($userOriginExists);
  93. if (!$userDestinationExists) {
  94. $this->userManager->expects($this->at(1))
  95. ->method('userExists')
  96. ->with('user2')
  97. ->willReturn($userDestinationExists);
  98. }
  99. $commandTester = new CommandTester($this->command);
  100. $commandTester->execute([
  101. 'name' => $this->command->getName(),
  102. 'sourceuid' => 'user',
  103. 'destinationuid' => 'user2',
  104. ]);
  105. }
  106. public function testMoveWithInexistantCalendar() {
  107. $this->expectException(\InvalidArgumentException::class);
  108. $this->expectExceptionMessage('User <user> has no calendar named <personal>. You can run occ dav:list-calendars to list calendars URIs for this user.');
  109. $this->userManager->expects($this->at(0))
  110. ->method('userExists')
  111. ->with('user')
  112. ->willReturn(true);
  113. $this->userManager->expects($this->at(1))
  114. ->method('userExists')
  115. ->with('user2')
  116. ->willReturn(true);
  117. $this->calDav->expects($this->once())->method('getCalendarByUri')
  118. ->with('principals/users/user', 'personal')
  119. ->willReturn(null);
  120. $commandTester = new CommandTester($this->command);
  121. $commandTester->execute([
  122. 'name' => 'personal',
  123. 'sourceuid' => 'user',
  124. 'destinationuid' => 'user2',
  125. ]);
  126. }
  127. public function testMoveWithExistingDestinationCalendar() {
  128. $this->expectException(\InvalidArgumentException::class);
  129. $this->expectExceptionMessage('User <user2> already has a calendar named <personal>.');
  130. $this->userManager->expects($this->at(0))
  131. ->method('userExists')
  132. ->with('user')
  133. ->willReturn(true);
  134. $this->userManager->expects($this->at(1))
  135. ->method('userExists')
  136. ->with('user2')
  137. ->willReturn(true);
  138. $this->calDav->expects($this->at(0))->method('getCalendarByUri')
  139. ->with('principals/users/user', 'personal')
  140. ->willReturn([
  141. 'id' => 1234,
  142. ]);
  143. $this->calDav->expects($this->at(1))->method('getCalendarByUri')
  144. ->with('principals/users/user2', 'personal')
  145. ->willReturn([
  146. 'id' => 1234,
  147. ]);
  148. $commandTester = new CommandTester($this->command);
  149. $commandTester->execute([
  150. 'name' => 'personal',
  151. 'sourceuid' => 'user',
  152. 'destinationuid' => 'user2',
  153. ]);
  154. }
  155. public function testMove() {
  156. $this->userManager->expects($this->at(0))
  157. ->method('userExists')
  158. ->with('user')
  159. ->willReturn(true);
  160. $this->userManager->expects($this->at(1))
  161. ->method('userExists')
  162. ->with('user2')
  163. ->willReturn(true);
  164. $this->calDav->expects($this->at(0))->method('getCalendarByUri')
  165. ->with('principals/users/user', 'personal')
  166. ->willReturn([
  167. 'id' => 1234,
  168. ]);
  169. $this->calDav->expects($this->at(1))->method('getCalendarByUri')
  170. ->with('principals/users/user2', 'personal')
  171. ->willReturn(null);
  172. $this->calDav->expects($this->once())->method('getShares')
  173. ->with(1234)
  174. ->willReturn([]);
  175. $commandTester = new CommandTester($this->command);
  176. $commandTester->execute([
  177. 'name' => 'personal',
  178. 'sourceuid' => 'user',
  179. 'destinationuid' => 'user2',
  180. ]);
  181. $this->assertContains("[OK] Calendar <personal> was moved from user <user> to <user2>", $commandTester->getDisplay());
  182. }
  183. public function dataTestMoveWithDestinationNotPartOfGroup(): array {
  184. return [
  185. [true],
  186. [false]
  187. ];
  188. }
  189. /**
  190. * @dataProvider dataTestMoveWithDestinationNotPartOfGroup
  191. */
  192. public function testMoveWithDestinationNotPartOfGroup(bool $shareWithGroupMembersOnly) {
  193. $this->userManager->expects($this->at(0))
  194. ->method('userExists')
  195. ->with('user')
  196. ->willReturn(true);
  197. $this->userManager->expects($this->at(1))
  198. ->method('userExists')
  199. ->with('user2')
  200. ->willReturn(true);
  201. $this->calDav->expects($this->at(0))->method('getCalendarByUri')
  202. ->with('principals/users/user', 'personal')
  203. ->willReturn([
  204. 'id' => 1234,
  205. 'uri' => 'personal'
  206. ]);
  207. $this->calDav->expects($this->at(1))->method('getCalendarByUri')
  208. ->with('principals/users/user2', 'personal')
  209. ->willReturn(null);
  210. $this->shareManager->expects($this->once())->method('shareWithGroupMembersOnly')
  211. ->willReturn($shareWithGroupMembersOnly);
  212. $this->calDav->expects($this->once())->method('getShares')
  213. ->with(1234)
  214. ->willReturn([
  215. ['href' => 'principal:principals/groups/nextclouders']
  216. ]);
  217. if ($shareWithGroupMembersOnly === true) {
  218. $this->expectException(InvalidArgumentException::class);
  219. $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.");
  220. }
  221. $commandTester = new CommandTester($this->command);
  222. $commandTester->execute([
  223. 'name' => 'personal',
  224. 'sourceuid' => 'user',
  225. 'destinationuid' => 'user2',
  226. ]);
  227. }
  228. public function testMoveWithDestinationPartOfGroup() {
  229. $this->userManager->expects($this->at(0))
  230. ->method('userExists')
  231. ->with('user')
  232. ->willReturn(true);
  233. $this->userManager->expects($this->at(1))
  234. ->method('userExists')
  235. ->with('user2')
  236. ->willReturn(true);
  237. $this->calDav->expects($this->at(0))->method('getCalendarByUri')
  238. ->with('principals/users/user', 'personal')
  239. ->willReturn([
  240. 'id' => 1234,
  241. 'uri' => 'personal'
  242. ]);
  243. $this->calDav->expects($this->at(1))->method('getCalendarByUri')
  244. ->with('principals/users/user2', 'personal')
  245. ->willReturn(null);
  246. $this->shareManager->expects($this->once())->method('shareWithGroupMembersOnly')
  247. ->willReturn(true);
  248. $this->calDav->expects($this->once())->method('getShares')
  249. ->with(1234)
  250. ->willReturn([
  251. ['href' => 'principal:principals/groups/nextclouders']
  252. ]);
  253. $this->groupManager->expects($this->once())->method('isInGroup')
  254. ->with('user2', 'nextclouders')
  255. ->willReturn(true);
  256. $commandTester = new CommandTester($this->command);
  257. $commandTester->execute([
  258. 'name' => 'personal',
  259. 'sourceuid' => 'user',
  260. 'destinationuid' => 'user2',
  261. ]);
  262. $this->assertStringContainsString("[OK] Calendar <personal> was moved from user <user> to <user2>", $commandTester->getDisplay());
  263. }
  264. public function testMoveWithDestinationNotPartOfGroupAndForce() {
  265. $this->userManager->expects($this->at(0))
  266. ->method('userExists')
  267. ->with('user')
  268. ->willReturn(true);
  269. $this->userManager->expects($this->at(1))
  270. ->method('userExists')
  271. ->with('user2')
  272. ->willReturn(true);
  273. $this->calDav->expects($this->at(0))->method('getCalendarByUri')
  274. ->with('principals/users/user', 'personal')
  275. ->willReturn([
  276. 'id' => 1234,
  277. 'uri' => 'personal',
  278. '{DAV:}displayname' => 'Personal'
  279. ]);
  280. $this->calDav->expects($this->at(1))->method('getCalendarByUri')
  281. ->with('principals/users/user2', 'personal')
  282. ->willReturn(null);
  283. $this->shareManager->expects($this->once())->method('shareWithGroupMembersOnly')
  284. ->willReturn(true);
  285. $this->calDav->expects($this->once())->method('getShares')
  286. ->with(1234)
  287. ->willReturn([
  288. [
  289. 'href' => 'principal:principals/groups/nextclouders',
  290. '{DAV:}displayname' => 'Personal'
  291. ]
  292. ]);
  293. $this->calDav->expects($this->once())->method('updateShares');
  294. $commandTester = new CommandTester($this->command);
  295. $commandTester->execute([
  296. 'name' => 'personal',
  297. 'sourceuid' => 'user',
  298. 'destinationuid' => 'user2',
  299. '--force' => true
  300. ]);
  301. $this->assertStringContainsString("[OK] Calendar <personal> was moved from user <user> to <user2>", $commandTester->getDisplay());
  302. }
  303. public function dataTestMoveWithCalendarAlreadySharedToDestination(): array {
  304. return [
  305. [true],
  306. [false]
  307. ];
  308. }
  309. /**
  310. * @dataProvider dataTestMoveWithCalendarAlreadySharedToDestination
  311. */
  312. public function testMoveWithCalendarAlreadySharedToDestination(bool $force) {
  313. $this->userManager->expects($this->at(0))
  314. ->method('userExists')
  315. ->with('user')
  316. ->willReturn(true);
  317. $this->userManager->expects($this->at(1))
  318. ->method('userExists')
  319. ->with('user2')
  320. ->willReturn(true);
  321. $this->calDav->expects($this->at(0))->method('getCalendarByUri')
  322. ->with('principals/users/user', 'personal')
  323. ->willReturn([
  324. 'id' => 1234,
  325. 'uri' => 'personal',
  326. '{DAV:}displayname' => 'Personal',
  327. ]);
  328. $this->calDav->expects($this->at(1))->method('getCalendarByUri')
  329. ->with('principals/users/user2', 'personal')
  330. ->willReturn(null);
  331. $this->calDav->expects($this->once())->method('getShares')
  332. ->with(1234)
  333. ->willReturn([
  334. [
  335. 'href' => 'principal:principals/users/user2',
  336. '{DAV:}displayname' => 'Personal'
  337. ]
  338. ]);
  339. if ($force === false) {
  340. $this->expectException(InvalidArgumentException::class);
  341. $this->expectExceptionMessage("The calendar <personal> is already shared to user <user2>.You may use -f to move the calendar while deleting this share.");
  342. } else {
  343. $this->calDav->expects($this->once())->method('updateShares');
  344. }
  345. $commandTester = new CommandTester($this->command);
  346. $commandTester->execute([
  347. 'name' => 'personal',
  348. 'sourceuid' => 'user',
  349. 'destinationuid' => 'user2',
  350. '--force' => $force,
  351. ]);
  352. }
  353. }