HookManagerTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 Symfony\Component\EventDispatcher\EventDispatcherInterface;
  42. use Test\TestCase;
  43. class HookManagerTest extends TestCase {
  44. /** @var IL10N */
  45. private $l10n;
  46. /** @var EventDispatcherInterface | MockObject */
  47. private $eventDispatcher;
  48. protected function setUp(): void {
  49. parent::setUp();
  50. $this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
  51. $this->l10n = $this->createMock(IL10N::class);
  52. $this->l10n
  53. ->expects($this->any())
  54. ->method('t')
  55. ->willReturnCallback(function ($text, $parameters = []) {
  56. return vsprintf($text, $parameters);
  57. });
  58. }
  59. public function test(): void {
  60. $user = $this->getMockBuilder(IUser::class)
  61. ->disableOriginalConstructor()
  62. ->getMock();
  63. $user->expects($this->once())->method('getUID')->willReturn('newUser');
  64. /** @var IUserManager | MockObject $userManager */
  65. $userManager = $this->getMockBuilder(IUserManager::class)
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. /** @var SyncService | MockObject $syncService */
  69. $syncService = $this->getMockBuilder(SyncService::class)
  70. ->disableOriginalConstructor()
  71. ->getMock();
  72. /** @var Defaults | MockObject $syncService */
  73. $defaults = $this->getMockBuilder(Defaults::class)
  74. ->disableOriginalConstructor()
  75. ->getMock();
  76. $defaults->expects($this->once())->method('getColorPrimary')->willReturn('#745bca');
  77. /** @var CalDavBackend | MockObject $cal */
  78. $cal = $this->getMockBuilder(CalDavBackend::class)
  79. ->disableOriginalConstructor()
  80. ->getMock();
  81. $cal->expects($this->once())->method('getCalendarsForUserCount')->willReturn(0);
  82. $cal->expects($this->once())->method('createCalendar')->with(
  83. 'principals/users/newUser',
  84. 'personal', [
  85. '{DAV:}displayname' => 'Personal',
  86. '{http://apple.com/ns/ical/}calendar-color' => '#745bca',
  87. 'components' => 'VEVENT'
  88. ]);
  89. /** @var CardDavBackend | MockObject $card */
  90. $card = $this->getMockBuilder(CardDavBackend::class)
  91. ->disableOriginalConstructor()
  92. ->getMock();
  93. $card->expects($this->once())->method('getAddressBooksForUserCount')->willReturn(0);
  94. $card->expects($this->once())->method('createAddressBook')->with(
  95. 'principals/users/newUser',
  96. 'contacts', ['{DAV:}displayname' => 'Contacts']);
  97. $hm = new HookManager($userManager, $syncService, $cal, $card, $defaults, $this->eventDispatcher);
  98. $hm->firstLogin($user);
  99. }
  100. public function testWithExisting(): void {
  101. $user = $this->getMockBuilder(IUser::class)
  102. ->disableOriginalConstructor()
  103. ->getMock();
  104. $user->expects($this->once())->method('getUID')->willReturn('newUser');
  105. /** @var IUserManager | MockObject $userManager */
  106. $userManager = $this->getMockBuilder(IUserManager::class)
  107. ->disableOriginalConstructor()
  108. ->getMock();
  109. /** @var SyncService | MockObject $syncService */
  110. $syncService = $this->getMockBuilder(SyncService::class)
  111. ->disableOriginalConstructor()
  112. ->getMock();
  113. /** @var Defaults | MockObject $syncService */
  114. $defaults = $this->getMockBuilder(Defaults::class)
  115. ->disableOriginalConstructor()
  116. ->getMock();
  117. /** @var CalDavBackend | MockObject $cal */
  118. $cal = $this->getMockBuilder(CalDavBackend::class)
  119. ->disableOriginalConstructor()
  120. ->getMock();
  121. $cal->expects($this->once())->method('getCalendarsForUserCount')->willReturn(1);
  122. $cal->expects($this->never())->method('createCalendar');
  123. /** @var CardDavBackend | MockObject $card */
  124. $card = $this->getMockBuilder(CardDavBackend::class)
  125. ->disableOriginalConstructor()
  126. ->getMock();
  127. $card->expects($this->once())->method('getAddressBooksForUserCount')->willReturn(1);
  128. $card->expects($this->never())->method('createAddressBook');
  129. $hm = new HookManager($userManager, $syncService, $cal, $card, $defaults, $this->eventDispatcher);
  130. $hm->firstLogin($user);
  131. }
  132. public function testWithBirthdayCalendar(): void {
  133. $user = $this->getMockBuilder(IUser::class)
  134. ->disableOriginalConstructor()
  135. ->getMock();
  136. $user->expects($this->once())->method('getUID')->willReturn('newUser');
  137. /** @var IUserManager | MockObject $userManager */
  138. $userManager = $this->getMockBuilder(IUserManager::class)
  139. ->disableOriginalConstructor()
  140. ->getMock();
  141. /** @var SyncService | MockObject $syncService */
  142. $syncService = $this->getMockBuilder(SyncService::class)
  143. ->disableOriginalConstructor()
  144. ->getMock();
  145. /** @var Defaults | MockObject $syncService */
  146. $defaults = $this->getMockBuilder(Defaults::class)
  147. ->disableOriginalConstructor()
  148. ->getMock();
  149. $defaults->expects($this->once())->method('getColorPrimary')->willReturn('#745bca');
  150. /** @var CalDavBackend | MockObject $cal */
  151. $cal = $this->getMockBuilder(CalDavBackend::class)
  152. ->disableOriginalConstructor()
  153. ->getMock();
  154. $cal->expects($this->once())->method('getCalendarsForUserCount')->willReturn(0);
  155. $cal->expects($this->once())->method('createCalendar')->with(
  156. 'principals/users/newUser',
  157. 'personal', [
  158. '{DAV:}displayname' => 'Personal',
  159. '{http://apple.com/ns/ical/}calendar-color' => '#745bca',
  160. 'components' => 'VEVENT'
  161. ]);
  162. /** @var CardDavBackend | MockObject $card */
  163. $card = $this->getMockBuilder(CardDavBackend::class)
  164. ->disableOriginalConstructor()
  165. ->getMock();
  166. $card->expects($this->once())->method('getAddressBooksForUserCount')->willReturn(0);
  167. $card->expects($this->once())->method('createAddressBook')->with(
  168. 'principals/users/newUser',
  169. 'contacts', ['{DAV:}displayname' => 'Contacts']);
  170. $hm = new HookManager($userManager, $syncService, $cal, $card, $defaults, $this->eventDispatcher);
  171. $hm->firstLogin($user);
  172. }
  173. public function testDeleteCalendar(): void {
  174. $user = $this->getMockBuilder(IUser::class)
  175. ->disableOriginalConstructor()
  176. ->getMock();
  177. /** @var IUserManager | MockObject $userManager */
  178. $userManager = $this->getMockBuilder(IUserManager::class)
  179. ->disableOriginalConstructor()
  180. ->getMock();
  181. $userManager->expects($this->once())->method('get')->willReturn($user);
  182. /** @var SyncService | MockObject $syncService */
  183. $syncService = $this->getMockBuilder(SyncService::class)
  184. ->disableOriginalConstructor()
  185. ->getMock();
  186. $syncService->expects($this->once())
  187. ->method('deleteUser');
  188. /** @var Defaults | MockObject $syncService */
  189. $defaults = $this->getMockBuilder(Defaults::class)
  190. ->disableOriginalConstructor()
  191. ->getMock();
  192. /** @var CalDavBackend | MockObject $cal */
  193. $cal = $this->getMockBuilder(CalDavBackend::class)
  194. ->disableOriginalConstructor()
  195. ->getMock();
  196. $cal->expects($this->once())->method('getUsersOwnCalendars')->willReturn([
  197. ['id' => 'personal']
  198. ]);
  199. $cal->expects($this->once())->method('getSubscriptionsForUser')->willReturn([
  200. ['id' => 'some-subscription']
  201. ]);
  202. $cal->expects($this->once())->method('deleteCalendar')->with('personal');
  203. $cal->expects($this->once())->method('deleteSubscription')->with('some-subscription');
  204. $cal->expects($this->once())->method('deleteAllSharesByUser');
  205. /** @var CardDavBackend | MockObject $card */
  206. $card = $this->getMockBuilder(CardDavBackend::class)
  207. ->disableOriginalConstructor()
  208. ->getMock();
  209. $card->expects($this->once())->method('getUsersOwnAddressBooks')->willReturn([
  210. ['id' => 'personal']
  211. ]);
  212. $card->expects($this->once())->method('deleteAddressBook');
  213. $hm = new HookManager($userManager, $syncService, $cal, $card, $defaults, $this->eventDispatcher);
  214. $hm->preDeleteUser(['uid' => 'newUser']);
  215. $hm->postDeleteUser(['uid' => 'newUser']);
  216. }
  217. }