1
0

DeleteCalendarTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. *
  5. * @copyright Copyright (c) 2021, Mattia Narducci (mattianarducci1@gmail.com)
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\DAV\Tests\Command;
  24. use OCA\DAV\CalDAV\BirthdayService;
  25. use OCA\DAV\CalDAV\CalDavBackend;
  26. use OCA\DAV\Command\DeleteCalendar;
  27. use OCP\IConfig;
  28. use OCP\IL10N;
  29. use OCP\IUserManager;
  30. use PHPUnit\Framework\MockObject\MockObject;
  31. use Psr\Log\LoggerInterface;
  32. use Symfony\Component\Console\Tester\CommandTester;
  33. use Test\TestCase;
  34. /**
  35. * Class DeleteCalendarTest
  36. *
  37. * @package OCA\DAV\Tests\Command
  38. */
  39. class DeleteCalendarTest extends TestCase {
  40. public const USER = 'user';
  41. public const NAME = 'calendar';
  42. /** @var CalDavBackend|MockObject */
  43. private $calDav;
  44. /** @var IConfig|MockObject */
  45. private $config;
  46. /** @var IL10N|MockObject */
  47. private $l10n;
  48. /** @var IUserManager|MockObject */
  49. private $userManager;
  50. /** @var DeleteCalendar */
  51. private $command;
  52. /** @var MockObject|LoggerInterface */
  53. private $logger;
  54. protected function setUp(): void {
  55. parent::setUp();
  56. $this->calDav = $this->createMock(CalDavBackend::class);
  57. $this->config = $this->createMock(IConfig::class);
  58. $this->l10n = $this->createMock(IL10N::class);
  59. $this->userManager = $this->createMock(IUserManager::class);
  60. $this->logger = $this->createMock(LoggerInterface::class);
  61. $this->command = new DeleteCalendar(
  62. $this->calDav,
  63. $this->config,
  64. $this->l10n,
  65. $this->userManager,
  66. $this->logger
  67. );
  68. }
  69. public function testInvalidUser(): void {
  70. $this->expectException(\InvalidArgumentException::class);
  71. $this->expectExceptionMessage(
  72. 'User <' . self::USER . '> is unknown.');
  73. $this->userManager->expects($this->once())
  74. ->method('userExists')
  75. ->with(self::USER)
  76. ->willReturn(false);
  77. $commandTester = new CommandTester($this->command);
  78. $commandTester->execute([
  79. 'uid' => self::USER,
  80. 'name' => self::NAME,
  81. ]);
  82. }
  83. public function testNoCalendarName(): void {
  84. $this->expectException(\InvalidArgumentException::class);
  85. $this->expectExceptionMessage(
  86. 'Please specify a calendar name or --birthday');
  87. $this->userManager->expects($this->once())
  88. ->method('userExists')
  89. ->with(self::USER)
  90. ->willReturn(true);
  91. $commandTester = new CommandTester($this->command);
  92. $commandTester->execute([
  93. 'uid' => self::USER,
  94. ]);
  95. }
  96. public function testInvalidCalendar(): void {
  97. $this->expectException(\InvalidArgumentException::class);
  98. $this->expectExceptionMessage(
  99. 'User <' . self::USER . '> has no calendar named <' . self::NAME . '>.');
  100. $this->userManager->expects($this->once())
  101. ->method('userExists')
  102. ->with(self::USER)
  103. ->willReturn(true);
  104. $this->calDav->expects($this->once())
  105. ->method('getCalendarByUri')
  106. ->with(
  107. 'principals/users/' . self::USER,
  108. self::NAME
  109. )
  110. ->willReturn(null);
  111. $commandTester = new CommandTester($this->command);
  112. $commandTester->execute([
  113. 'uid' => self::USER,
  114. 'name' => self::NAME,
  115. ]);
  116. }
  117. public function testDelete(): void {
  118. $id = 1234;
  119. $calendar = [
  120. 'id' => $id,
  121. 'principaluri' => 'principals/users/' . self::USER,
  122. 'uri' => self::NAME
  123. ];
  124. $this->userManager->expects($this->once())
  125. ->method('userExists')
  126. ->with(self::USER)
  127. ->willReturn(true);
  128. $this->calDav->expects($this->once())
  129. ->method('getCalendarByUri')
  130. ->with(
  131. 'principals/users/' . self::USER,
  132. self::NAME
  133. )
  134. ->willReturn($calendar);
  135. $this->calDav->expects($this->once())
  136. ->method('deleteCalendar')
  137. ->with($id, false);
  138. $commandTester = new CommandTester($this->command);
  139. $commandTester->execute([
  140. 'uid' => self::USER,
  141. 'name' => self::NAME,
  142. ]);
  143. }
  144. public function testForceDelete(): void {
  145. $id = 1234;
  146. $calendar = [
  147. 'id' => $id,
  148. 'principaluri' => 'principals/users/' . self::USER,
  149. 'uri' => self::NAME
  150. ];
  151. $this->userManager->expects($this->once())
  152. ->method('userExists')
  153. ->with(self::USER)
  154. ->willReturn(true);
  155. $this->calDav->expects($this->once())
  156. ->method('getCalendarByUri')
  157. ->with(
  158. 'principals/users/' . self::USER,
  159. self::NAME
  160. )
  161. ->willReturn($calendar);
  162. $this->calDav->expects($this->once())
  163. ->method('deleteCalendar')
  164. ->with($id, true);
  165. $commandTester = new CommandTester($this->command);
  166. $commandTester->execute([
  167. 'uid' => self::USER,
  168. 'name' => self::NAME,
  169. '-f' => true
  170. ]);
  171. }
  172. public function testDeleteBirthday(): void {
  173. $id = 1234;
  174. $calendar = [
  175. 'id' => $id,
  176. 'principaluri' => 'principals/users/' . self::USER,
  177. 'uri' => BirthdayService::BIRTHDAY_CALENDAR_URI
  178. ];
  179. $this->userManager->expects($this->once())
  180. ->method('userExists')
  181. ->with(self::USER)
  182. ->willReturn(true);
  183. $this->calDav->expects($this->once())
  184. ->method('getCalendarByUri')
  185. ->with(
  186. 'principals/users/' . self::USER,
  187. BirthdayService::BIRTHDAY_CALENDAR_URI
  188. )
  189. ->willReturn($calendar);
  190. $this->calDav->expects($this->once())
  191. ->method('deleteCalendar')
  192. ->with($id);
  193. $commandTester = new CommandTester($this->command);
  194. $commandTester->execute([
  195. 'uid' => self::USER,
  196. '--birthday' => true,
  197. ]);
  198. }
  199. public function testBirthdayHasPrecedence(): void {
  200. $calendar = [
  201. 'id' => 1234,
  202. 'principaluri' => 'principals/users/' . self::USER,
  203. 'uri' => BirthdayService::BIRTHDAY_CALENDAR_URI
  204. ];
  205. $this->userManager->expects($this->once())
  206. ->method('userExists')
  207. ->with(self::USER)
  208. ->willReturn(true);
  209. $this->calDav->expects($this->once())
  210. ->method('getCalendarByUri')
  211. ->with(
  212. 'principals/users/' . self::USER,
  213. BirthdayService::BIRTHDAY_CALENDAR_URI
  214. )
  215. ->willReturn($calendar);
  216. $commandTester = new CommandTester($this->command);
  217. $commandTester->execute([
  218. 'uid' => self::USER,
  219. 'name' => self::NAME,
  220. '--birthday' => true,
  221. ]);
  222. }
  223. }