HookManagerTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author John Molakvoæ <skjnldsv@protonmail.com>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. * @author Thomas Citharel <nextcloud@tcit.fr>
  14. * @author Thomas Müller <thomas.mueller@tmit.eu>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. namespace OCA\DAV\Tests\unit\DAV;
  32. use OCA\DAV\CalDAV\CalDavBackend;
  33. use OCA\DAV\CardDAV\CardDavBackend;
  34. use OCA\DAV\CardDAV\SyncService;
  35. use OCA\DAV\HookManager;
  36. use OCP\Defaults;
  37. use OCP\IL10N;
  38. use OCP\IUser;
  39. use OCP\IUserManager;
  40. use PHPUnit\Framework\MockObject\MockObject;
  41. use Test\TestCase;
  42. class HookManagerTest extends TestCase {
  43. /** @var IL10N */
  44. private $l10n;
  45. protected function setUp(): void {
  46. parent::setUp();
  47. $this->l10n = $this->createMock(IL10N::class);
  48. $this->l10n
  49. ->expects($this->any())
  50. ->method('t')
  51. ->willReturnCallback(function ($text, $parameters = []) {
  52. return vsprintf($text, $parameters);
  53. });
  54. }
  55. public function test(): void {
  56. $user = $this->getMockBuilder(IUser::class)
  57. ->disableOriginalConstructor()
  58. ->getMock();
  59. $user->expects($this->once())->method('getUID')->willReturn('newUser');
  60. /** @var IUserManager | MockObject $userManager */
  61. $userManager = $this->getMockBuilder(IUserManager::class)
  62. ->disableOriginalConstructor()
  63. ->getMock();
  64. /** @var SyncService | MockObject $syncService */
  65. $syncService = $this->getMockBuilder(SyncService::class)
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. /** @var Defaults | MockObject $syncService */
  69. $defaults = $this->getMockBuilder(Defaults::class)
  70. ->disableOriginalConstructor()
  71. ->getMock();
  72. $defaults->expects($this->once())->method('getColorPrimary')->willReturn('#745bca');
  73. /** @var CalDavBackend | MockObject $cal */
  74. $cal = $this->getMockBuilder(CalDavBackend::class)
  75. ->disableOriginalConstructor()
  76. ->getMock();
  77. $cal->expects($this->once())->method('getCalendarsForUserCount')->willReturn(0);
  78. $cal->expects($this->once())->method('createCalendar')->with(
  79. 'principals/users/newUser',
  80. 'personal', [
  81. '{DAV:}displayname' => 'Personal',
  82. '{http://apple.com/ns/ical/}calendar-color' => '#745bca',
  83. 'components' => 'VEVENT'
  84. ]);
  85. /** @var CardDavBackend | MockObject $card */
  86. $card = $this->getMockBuilder(CardDavBackend::class)
  87. ->disableOriginalConstructor()
  88. ->getMock();
  89. $card->expects($this->once())->method('getAddressBooksForUserCount')->willReturn(0);
  90. $card->expects($this->once())->method('createAddressBook')->with(
  91. 'principals/users/newUser',
  92. 'contacts', ['{DAV:}displayname' => 'Contacts']);
  93. $hm = new HookManager($userManager, $syncService, $cal, $card, $defaults);
  94. $hm->firstLogin($user);
  95. }
  96. public function testWithExisting(): void {
  97. $user = $this->getMockBuilder(IUser::class)
  98. ->disableOriginalConstructor()
  99. ->getMock();
  100. $user->expects($this->once())->method('getUID')->willReturn('newUser');
  101. /** @var IUserManager | MockObject $userManager */
  102. $userManager = $this->getMockBuilder(IUserManager::class)
  103. ->disableOriginalConstructor()
  104. ->getMock();
  105. /** @var SyncService | MockObject $syncService */
  106. $syncService = $this->getMockBuilder(SyncService::class)
  107. ->disableOriginalConstructor()
  108. ->getMock();
  109. /** @var Defaults | MockObject $syncService */
  110. $defaults = $this->getMockBuilder(Defaults::class)
  111. ->disableOriginalConstructor()
  112. ->getMock();
  113. /** @var CalDavBackend | MockObject $cal */
  114. $cal = $this->getMockBuilder(CalDavBackend::class)
  115. ->disableOriginalConstructor()
  116. ->getMock();
  117. $cal->expects($this->once())->method('getCalendarsForUserCount')->willReturn(1);
  118. $cal->expects($this->never())->method('createCalendar');
  119. /** @var CardDavBackend | MockObject $card */
  120. $card = $this->getMockBuilder(CardDavBackend::class)
  121. ->disableOriginalConstructor()
  122. ->getMock();
  123. $card->expects($this->once())->method('getAddressBooksForUserCount')->willReturn(1);
  124. $card->expects($this->never())->method('createAddressBook');
  125. $hm = new HookManager($userManager, $syncService, $cal, $card, $defaults);
  126. $hm->firstLogin($user);
  127. }
  128. public function testWithBirthdayCalendar(): void {
  129. $user = $this->getMockBuilder(IUser::class)
  130. ->disableOriginalConstructor()
  131. ->getMock();
  132. $user->expects($this->once())->method('getUID')->willReturn('newUser');
  133. /** @var IUserManager | MockObject $userManager */
  134. $userManager = $this->getMockBuilder(IUserManager::class)
  135. ->disableOriginalConstructor()
  136. ->getMock();
  137. /** @var SyncService | MockObject $syncService */
  138. $syncService = $this->getMockBuilder(SyncService::class)
  139. ->disableOriginalConstructor()
  140. ->getMock();
  141. /** @var Defaults | MockObject $syncService */
  142. $defaults = $this->getMockBuilder(Defaults::class)
  143. ->disableOriginalConstructor()
  144. ->getMock();
  145. $defaults->expects($this->once())->method('getColorPrimary')->willReturn('#745bca');
  146. /** @var CalDavBackend | MockObject $cal */
  147. $cal = $this->getMockBuilder(CalDavBackend::class)
  148. ->disableOriginalConstructor()
  149. ->getMock();
  150. $cal->expects($this->once())->method('getCalendarsForUserCount')->willReturn(0);
  151. $cal->expects($this->once())->method('createCalendar')->with(
  152. 'principals/users/newUser',
  153. 'personal', [
  154. '{DAV:}displayname' => 'Personal',
  155. '{http://apple.com/ns/ical/}calendar-color' => '#745bca',
  156. 'components' => 'VEVENT'
  157. ]);
  158. /** @var CardDavBackend | MockObject $card */
  159. $card = $this->getMockBuilder(CardDavBackend::class)
  160. ->disableOriginalConstructor()
  161. ->getMock();
  162. $card->expects($this->once())->method('getAddressBooksForUserCount')->willReturn(0);
  163. $card->expects($this->once())->method('createAddressBook')->with(
  164. 'principals/users/newUser',
  165. 'contacts', ['{DAV:}displayname' => 'Contacts']);
  166. $hm = new HookManager($userManager, $syncService, $cal, $card, $defaults);
  167. $hm->firstLogin($user);
  168. }
  169. public function testDeleteCalendar(): void {
  170. $user = $this->getMockBuilder(IUser::class)
  171. ->disableOriginalConstructor()
  172. ->getMock();
  173. /** @var IUserManager | MockObject $userManager */
  174. $userManager = $this->getMockBuilder(IUserManager::class)
  175. ->disableOriginalConstructor()
  176. ->getMock();
  177. $userManager->expects($this->once())->method('get')->willReturn($user);
  178. /** @var SyncService | MockObject $syncService */
  179. $syncService = $this->getMockBuilder(SyncService::class)
  180. ->disableOriginalConstructor()
  181. ->getMock();
  182. $syncService->expects($this->once())
  183. ->method('deleteUser');
  184. /** @var Defaults | MockObject $syncService */
  185. $defaults = $this->getMockBuilder(Defaults::class)
  186. ->disableOriginalConstructor()
  187. ->getMock();
  188. /** @var CalDavBackend | MockObject $cal */
  189. $cal = $this->getMockBuilder(CalDavBackend::class)
  190. ->disableOriginalConstructor()
  191. ->getMock();
  192. $cal->expects($this->once())->method('getUsersOwnCalendars')->willReturn([
  193. ['id' => 'personal']
  194. ]);
  195. $cal->expects($this->once())->method('getSubscriptionsForUser')->willReturn([
  196. ['id' => 'some-subscription']
  197. ]);
  198. $cal->expects($this->once())->method('deleteCalendar')->with('personal');
  199. $cal->expects($this->once())->method('deleteSubscription')->with('some-subscription');
  200. $cal->expects($this->once())->method('deleteAllSharesByUser');
  201. /** @var CardDavBackend | MockObject $card */
  202. $card = $this->getMockBuilder(CardDavBackend::class)
  203. ->disableOriginalConstructor()
  204. ->getMock();
  205. $card->expects($this->once())->method('getUsersOwnAddressBooks')->willReturn([
  206. ['id' => 'personal']
  207. ]);
  208. $card->expects($this->once())->method('deleteAddressBook');
  209. $hm = new HookManager($userManager, $syncService, $cal, $card, $defaults);
  210. $hm->preDeleteUser(['uid' => 'newUser']);
  211. $hm->postDeleteUser(['uid' => 'newUser']);
  212. }
  213. }