DeleteCalendarTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. ];
  163. $this->userManager->expects($this->once())
  164. ->method('userExists')
  165. ->with(self::USER)
  166. ->willReturn(true);
  167. $this->calDav->expects($this->once())
  168. ->method('getCalendarByUri')
  169. ->with(
  170. 'principals/users/' . self::USER,
  171. BirthdayService::BIRTHDAY_CALENDAR_URI
  172. )
  173. ->willReturn($calendar);
  174. $this->calDav->expects($this->once())
  175. ->method('deleteCalendar')
  176. ->with($id);
  177. $commandTester = new CommandTester($this->command);
  178. $commandTester->execute([
  179. 'uid' => self::USER,
  180. '--birthday' => true,
  181. ]);
  182. }
  183. public function testBirthdayHasPrecedence(): void {
  184. $calendar = [
  185. 'id' => 1234,
  186. 'principaluri' => 'principals/users/' . self::USER,
  187. 'uri' => BirthdayService::BIRTHDAY_CALENDAR_URI
  188. ];
  189. $this->userManager->expects($this->once())
  190. ->method('userExists')
  191. ->with(self::USER)
  192. ->willReturn(true);
  193. $this->calDav->expects($this->once())
  194. ->method('getCalendarByUri')
  195. ->with(
  196. 'principals/users/' . self::USER,
  197. BirthdayService::BIRTHDAY_CALENDAR_URI
  198. )
  199. ->willReturn($calendar);
  200. $commandTester = new CommandTester($this->command);
  201. $commandTester->execute([
  202. 'uid' => self::USER,
  203. 'name' => self::NAME,
  204. '--birthday' => true,
  205. ]);
  206. }
  207. }