1
0

HookManagerTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Tests\unit\DAV;
  8. use OCA\DAV\CalDAV\CalDavBackend;
  9. use OCA\DAV\CardDAV\CardDavBackend;
  10. use OCA\DAV\CardDAV\SyncService;
  11. use OCA\DAV\HookManager;
  12. use OCP\Defaults;
  13. use OCP\IL10N;
  14. use OCP\IUser;
  15. use OCP\IUserManager;
  16. use PHPUnit\Framework\MockObject\MockObject;
  17. use Test\TestCase;
  18. class HookManagerTest extends TestCase {
  19. /** @var IL10N */
  20. private $l10n;
  21. protected function setUp(): void {
  22. parent::setUp();
  23. $this->l10n = $this->createMock(IL10N::class);
  24. $this->l10n
  25. ->expects($this->any())
  26. ->method('t')
  27. ->willReturnCallback(function ($text, $parameters = []) {
  28. return vsprintf($text, $parameters);
  29. });
  30. }
  31. public function test(): void {
  32. $user = $this->getMockBuilder(IUser::class)
  33. ->disableOriginalConstructor()
  34. ->getMock();
  35. $user->expects($this->once())->method('getUID')->willReturn('newUser');
  36. /** @var IUserManager | MockObject $userManager */
  37. $userManager = $this->getMockBuilder(IUserManager::class)
  38. ->disableOriginalConstructor()
  39. ->getMock();
  40. /** @var SyncService | MockObject $syncService */
  41. $syncService = $this->getMockBuilder(SyncService::class)
  42. ->disableOriginalConstructor()
  43. ->getMock();
  44. /** @var Defaults | MockObject $syncService */
  45. $defaults = $this->getMockBuilder(Defaults::class)
  46. ->disableOriginalConstructor()
  47. ->getMock();
  48. $defaults->expects($this->once())->method('getColorPrimary')->willReturn('#745bca');
  49. /** @var CalDavBackend | MockObject $cal */
  50. $cal = $this->getMockBuilder(CalDavBackend::class)
  51. ->disableOriginalConstructor()
  52. ->getMock();
  53. $cal->expects($this->once())->method('getCalendarsForUserCount')->willReturn(0);
  54. $cal->expects($this->once())->method('createCalendar')->with(
  55. 'principals/users/newUser',
  56. 'personal', [
  57. '{DAV:}displayname' => 'Personal',
  58. '{http://apple.com/ns/ical/}calendar-color' => '#745bca',
  59. 'components' => 'VEVENT'
  60. ]);
  61. /** @var CardDavBackend | MockObject $card */
  62. $card = $this->getMockBuilder(CardDavBackend::class)
  63. ->disableOriginalConstructor()
  64. ->getMock();
  65. $card->expects($this->once())->method('getAddressBooksForUserCount')->willReturn(0);
  66. $card->expects($this->once())->method('createAddressBook')->with(
  67. 'principals/users/newUser',
  68. 'contacts', ['{DAV:}displayname' => 'Contacts']);
  69. $hm = new HookManager($userManager, $syncService, $cal, $card, $defaults);
  70. $hm->firstLogin($user);
  71. }
  72. public function testWithExisting(): void {
  73. $user = $this->getMockBuilder(IUser::class)
  74. ->disableOriginalConstructor()
  75. ->getMock();
  76. $user->expects($this->once())->method('getUID')->willReturn('newUser');
  77. /** @var IUserManager | MockObject $userManager */
  78. $userManager = $this->getMockBuilder(IUserManager::class)
  79. ->disableOriginalConstructor()
  80. ->getMock();
  81. /** @var SyncService | MockObject $syncService */
  82. $syncService = $this->getMockBuilder(SyncService::class)
  83. ->disableOriginalConstructor()
  84. ->getMock();
  85. /** @var Defaults | MockObject $syncService */
  86. $defaults = $this->getMockBuilder(Defaults::class)
  87. ->disableOriginalConstructor()
  88. ->getMock();
  89. /** @var CalDavBackend | MockObject $cal */
  90. $cal = $this->getMockBuilder(CalDavBackend::class)
  91. ->disableOriginalConstructor()
  92. ->getMock();
  93. $cal->expects($this->once())->method('getCalendarsForUserCount')->willReturn(1);
  94. $cal->expects($this->never())->method('createCalendar');
  95. /** @var CardDavBackend | MockObject $card */
  96. $card = $this->getMockBuilder(CardDavBackend::class)
  97. ->disableOriginalConstructor()
  98. ->getMock();
  99. $card->expects($this->once())->method('getAddressBooksForUserCount')->willReturn(1);
  100. $card->expects($this->never())->method('createAddressBook');
  101. $hm = new HookManager($userManager, $syncService, $cal, $card, $defaults);
  102. $hm->firstLogin($user);
  103. }
  104. public function testWithBirthdayCalendar(): void {
  105. $user = $this->getMockBuilder(IUser::class)
  106. ->disableOriginalConstructor()
  107. ->getMock();
  108. $user->expects($this->once())->method('getUID')->willReturn('newUser');
  109. /** @var IUserManager | MockObject $userManager */
  110. $userManager = $this->getMockBuilder(IUserManager::class)
  111. ->disableOriginalConstructor()
  112. ->getMock();
  113. /** @var SyncService | MockObject $syncService */
  114. $syncService = $this->getMockBuilder(SyncService::class)
  115. ->disableOriginalConstructor()
  116. ->getMock();
  117. /** @var Defaults | MockObject $syncService */
  118. $defaults = $this->getMockBuilder(Defaults::class)
  119. ->disableOriginalConstructor()
  120. ->getMock();
  121. $defaults->expects($this->once())->method('getColorPrimary')->willReturn('#745bca');
  122. /** @var CalDavBackend | MockObject $cal */
  123. $cal = $this->getMockBuilder(CalDavBackend::class)
  124. ->disableOriginalConstructor()
  125. ->getMock();
  126. $cal->expects($this->once())->method('getCalendarsForUserCount')->willReturn(0);
  127. $cal->expects($this->once())->method('createCalendar')->with(
  128. 'principals/users/newUser',
  129. 'personal', [
  130. '{DAV:}displayname' => 'Personal',
  131. '{http://apple.com/ns/ical/}calendar-color' => '#745bca',
  132. 'components' => 'VEVENT'
  133. ]);
  134. /** @var CardDavBackend | MockObject $card */
  135. $card = $this->getMockBuilder(CardDavBackend::class)
  136. ->disableOriginalConstructor()
  137. ->getMock();
  138. $card->expects($this->once())->method('getAddressBooksForUserCount')->willReturn(0);
  139. $card->expects($this->once())->method('createAddressBook')->with(
  140. 'principals/users/newUser',
  141. 'contacts', ['{DAV:}displayname' => 'Contacts']);
  142. $hm = new HookManager($userManager, $syncService, $cal, $card, $defaults);
  143. $hm->firstLogin($user);
  144. }
  145. public function testDeleteCalendar(): void {
  146. $user = $this->getMockBuilder(IUser::class)
  147. ->disableOriginalConstructor()
  148. ->getMock();
  149. /** @var IUserManager | MockObject $userManager */
  150. $userManager = $this->getMockBuilder(IUserManager::class)
  151. ->disableOriginalConstructor()
  152. ->getMock();
  153. $userManager->expects($this->once())->method('get')->willReturn($user);
  154. /** @var SyncService | MockObject $syncService */
  155. $syncService = $this->getMockBuilder(SyncService::class)
  156. ->disableOriginalConstructor()
  157. ->getMock();
  158. $syncService->expects($this->once())
  159. ->method('deleteUser');
  160. /** @var Defaults | MockObject $syncService */
  161. $defaults = $this->getMockBuilder(Defaults::class)
  162. ->disableOriginalConstructor()
  163. ->getMock();
  164. /** @var CalDavBackend | MockObject $cal */
  165. $cal = $this->getMockBuilder(CalDavBackend::class)
  166. ->disableOriginalConstructor()
  167. ->getMock();
  168. $cal->expects($this->once())->method('getUsersOwnCalendars')->willReturn([
  169. ['id' => 'personal']
  170. ]);
  171. $cal->expects($this->once())->method('getSubscriptionsForUser')->willReturn([
  172. ['id' => 'some-subscription']
  173. ]);
  174. $cal->expects($this->once())->method('deleteCalendar')->with('personal');
  175. $cal->expects($this->once())->method('deleteSubscription')->with('some-subscription');
  176. $cal->expects($this->once())->method('deleteAllSharesByUser');
  177. /** @var CardDavBackend | MockObject $card */
  178. $card = $this->getMockBuilder(CardDavBackend::class)
  179. ->disableOriginalConstructor()
  180. ->getMock();
  181. $card->expects($this->once())->method('getUsersOwnAddressBooks')->willReturn([
  182. ['id' => 'personal']
  183. ]);
  184. $card->expects($this->once())->method('deleteAddressBook');
  185. $hm = new HookManager($userManager, $syncService, $cal, $card, $defaults);
  186. $hm->preDeleteUser(['uid' => 'newUser']);
  187. $hm->postDeleteUser(['uid' => 'newUser']);
  188. }
  189. }