1
0

DeleteCalendarTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\Tests\Command;
  8. use OCA\DAV\CalDAV\BirthdayService;
  9. use OCA\DAV\CalDAV\CalDavBackend;
  10. use OCA\DAV\Command\DeleteCalendar;
  11. use OCP\IConfig;
  12. use OCP\IL10N;
  13. use OCP\IUserManager;
  14. use PHPUnit\Framework\MockObject\MockObject;
  15. use Psr\Log\LoggerInterface;
  16. use Symfony\Component\Console\Tester\CommandTester;
  17. use Test\TestCase;
  18. /**
  19. * Class DeleteCalendarTest
  20. *
  21. * @package OCA\DAV\Tests\Command
  22. */
  23. class DeleteCalendarTest extends TestCase {
  24. public const USER = 'user';
  25. public const NAME = 'calendar';
  26. /** @var CalDavBackend|MockObject */
  27. private $calDav;
  28. /** @var IConfig|MockObject */
  29. private $config;
  30. /** @var IL10N|MockObject */
  31. private $l10n;
  32. /** @var IUserManager|MockObject */
  33. private $userManager;
  34. /** @var DeleteCalendar */
  35. private $command;
  36. /** @var MockObject|LoggerInterface */
  37. private $logger;
  38. protected function setUp(): void {
  39. parent::setUp();
  40. $this->calDav = $this->createMock(CalDavBackend::class);
  41. $this->config = $this->createMock(IConfig::class);
  42. $this->l10n = $this->createMock(IL10N::class);
  43. $this->userManager = $this->createMock(IUserManager::class);
  44. $this->logger = $this->createMock(LoggerInterface::class);
  45. $this->command = new DeleteCalendar(
  46. $this->calDav,
  47. $this->config,
  48. $this->l10n,
  49. $this->userManager,
  50. $this->logger
  51. );
  52. }
  53. public function testInvalidUser(): void {
  54. $this->expectException(\InvalidArgumentException::class);
  55. $this->expectExceptionMessage(
  56. 'User <' . self::USER . '> is unknown.');
  57. $this->userManager->expects($this->once())
  58. ->method('userExists')
  59. ->with(self::USER)
  60. ->willReturn(false);
  61. $commandTester = new CommandTester($this->command);
  62. $commandTester->execute([
  63. 'uid' => self::USER,
  64. 'name' => self::NAME,
  65. ]);
  66. }
  67. public function testNoCalendarName(): void {
  68. $this->expectException(\InvalidArgumentException::class);
  69. $this->expectExceptionMessage(
  70. 'Please specify a calendar name or --birthday');
  71. $this->userManager->expects($this->once())
  72. ->method('userExists')
  73. ->with(self::USER)
  74. ->willReturn(true);
  75. $commandTester = new CommandTester($this->command);
  76. $commandTester->execute([
  77. 'uid' => self::USER,
  78. ]);
  79. }
  80. public function testInvalidCalendar(): void {
  81. $this->expectException(\InvalidArgumentException::class);
  82. $this->expectExceptionMessage(
  83. 'User <' . self::USER . '> has no calendar named <' . self::NAME . '>.');
  84. $this->userManager->expects($this->once())
  85. ->method('userExists')
  86. ->with(self::USER)
  87. ->willReturn(true);
  88. $this->calDav->expects($this->once())
  89. ->method('getCalendarByUri')
  90. ->with(
  91. 'principals/users/' . self::USER,
  92. self::NAME
  93. )
  94. ->willReturn(null);
  95. $commandTester = new CommandTester($this->command);
  96. $commandTester->execute([
  97. 'uid' => self::USER,
  98. 'name' => self::NAME,
  99. ]);
  100. }
  101. public function testDelete(): void {
  102. $id = 1234;
  103. $calendar = [
  104. 'id' => $id,
  105. 'principaluri' => 'principals/users/' . self::USER,
  106. 'uri' => self::NAME,
  107. ];
  108. $this->userManager->expects($this->once())
  109. ->method('userExists')
  110. ->with(self::USER)
  111. ->willReturn(true);
  112. $this->calDav->expects($this->once())
  113. ->method('getCalendarByUri')
  114. ->with(
  115. 'principals/users/' . self::USER,
  116. self::NAME
  117. )
  118. ->willReturn($calendar);
  119. $this->calDav->expects($this->once())
  120. ->method('deleteCalendar')
  121. ->with($id, false);
  122. $commandTester = new CommandTester($this->command);
  123. $commandTester->execute([
  124. 'uid' => self::USER,
  125. 'name' => self::NAME,
  126. ]);
  127. }
  128. public function testForceDelete(): void {
  129. $id = 1234;
  130. $calendar = [
  131. 'id' => $id,
  132. 'principaluri' => 'principals/users/' . self::USER,
  133. 'uri' => self::NAME
  134. ];
  135. $this->userManager->expects($this->once())
  136. ->method('userExists')
  137. ->with(self::USER)
  138. ->willReturn(true);
  139. $this->calDav->expects($this->once())
  140. ->method('getCalendarByUri')
  141. ->with(
  142. 'principals/users/' . self::USER,
  143. self::NAME
  144. )
  145. ->willReturn($calendar);
  146. $this->calDav->expects($this->once())
  147. ->method('deleteCalendar')
  148. ->with($id, true);
  149. $commandTester = new CommandTester($this->command);
  150. $commandTester->execute([
  151. 'uid' => self::USER,
  152. 'name' => self::NAME,
  153. '-f' => true
  154. ]);
  155. }
  156. public function testDeleteBirthday(): void {
  157. $id = 1234;
  158. $calendar = [
  159. 'id' => $id,
  160. 'principaluri' => 'principals/users/' . self::USER,
  161. 'uri' => BirthdayService::BIRTHDAY_CALENDAR_URI,
  162. '{DAV:}displayname' => 'Test',
  163. ];
  164. $this->userManager->expects($this->once())
  165. ->method('userExists')
  166. ->with(self::USER)
  167. ->willReturn(true);
  168. $this->calDav->expects($this->once())
  169. ->method('getCalendarByUri')
  170. ->with(
  171. 'principals/users/' . self::USER,
  172. BirthdayService::BIRTHDAY_CALENDAR_URI
  173. )
  174. ->willReturn($calendar);
  175. $this->calDav->expects($this->once())
  176. ->method('deleteCalendar')
  177. ->with($id);
  178. $commandTester = new CommandTester($this->command);
  179. $commandTester->execute([
  180. 'uid' => self::USER,
  181. '--birthday' => true,
  182. ]);
  183. }
  184. public function testBirthdayHasPrecedence(): void {
  185. $calendar = [
  186. 'id' => 1234,
  187. 'principaluri' => 'principals/users/' . self::USER,
  188. 'uri' => BirthdayService::BIRTHDAY_CALENDAR_URI,
  189. '{DAV:}displayname' => 'Test',
  190. ];
  191. $this->userManager->expects($this->once())
  192. ->method('userExists')
  193. ->with(self::USER)
  194. ->willReturn(true);
  195. $this->calDav->expects($this->once())
  196. ->method('getCalendarByUri')
  197. ->with(
  198. 'principals/users/' . self::USER,
  199. BirthdayService::BIRTHDAY_CALENDAR_URI
  200. )
  201. ->willReturn($calendar);
  202. $commandTester = new CommandTester($this->command);
  203. $commandTester->execute([
  204. 'uid' => self::USER,
  205. 'name' => self::NAME,
  206. '--birthday' => true,
  207. ]);
  208. }
  209. }