HookManagerTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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) <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\IL10N;
  37. use OCP\IUser;
  38. use OCP\IUserManager;
  39. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  40. use Test\TestCase;
  41. class HookManagerTest extends TestCase {
  42. /** @var IL10N */
  43. private $l10n;
  44. /** @var EventDispatcherInterface | \PHPUnit\Framework\MockObject\MockObject */
  45. private $eventDispatcher;
  46. protected function setUp(): void {
  47. parent::setUp();
  48. $this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
  49. $this->l10n = $this->createMock(IL10N::class);
  50. $this->l10n
  51. ->expects($this->any())
  52. ->method('t')
  53. ->willReturnCallback(function ($text, $parameters = []) {
  54. return vsprintf($text, $parameters);
  55. });
  56. }
  57. public function test() {
  58. $user = $this->getMockBuilder(IUser::class)
  59. ->disableOriginalConstructor()
  60. ->getMock();
  61. $user->expects($this->once())->method('getUID')->willReturn('newUser');
  62. /** @var IUserManager | \PHPUnit\Framework\MockObject\MockObject $userManager */
  63. $userManager = $this->getMockBuilder(IUserManager::class)
  64. ->disableOriginalConstructor()
  65. ->getMock();
  66. /** @var SyncService | \PHPUnit\Framework\MockObject\MockObject $syncService */
  67. $syncService = $this->getMockBuilder(SyncService::class)
  68. ->disableOriginalConstructor()
  69. ->getMock();
  70. /** @var CalDavBackend | \PHPUnit\Framework\MockObject\MockObject $cal */
  71. $cal = $this->getMockBuilder(CalDavBackend::class)
  72. ->disableOriginalConstructor()
  73. ->getMock();
  74. $cal->expects($this->once())->method('getCalendarsForUserCount')->willReturn(0);
  75. $cal->expects($this->once())->method('createCalendar')->with(
  76. 'principals/users/newUser',
  77. 'personal', ['{DAV:}displayname' => 'Personal']);
  78. /** @var CardDavBackend | \PHPUnit\Framework\MockObject\MockObject $card */
  79. $card = $this->getMockBuilder(CardDavBackend::class)
  80. ->disableOriginalConstructor()
  81. ->getMock();
  82. $card->expects($this->once())->method('getAddressBooksForUserCount')->willReturn(0);
  83. $card->expects($this->once())->method('createAddressBook')->with(
  84. 'principals/users/newUser',
  85. 'contacts', ['{DAV:}displayname' => 'Contacts']);
  86. $hm = new HookManager($userManager, $syncService, $cal, $card, $this->eventDispatcher);
  87. $hm->firstLogin($user);
  88. }
  89. public function testWithExisting() {
  90. $user = $this->getMockBuilder(IUser::class)
  91. ->disableOriginalConstructor()
  92. ->getMock();
  93. $user->expects($this->once())->method('getUID')->willReturn('newUser');
  94. /** @var IUserManager | \PHPUnit\Framework\MockObject\MockObject $userManager */
  95. $userManager = $this->getMockBuilder(IUserManager::class)
  96. ->disableOriginalConstructor()
  97. ->getMock();
  98. /** @var SyncService | \PHPUnit\Framework\MockObject\MockObject $syncService */
  99. $syncService = $this->getMockBuilder(SyncService::class)
  100. ->disableOriginalConstructor()
  101. ->getMock();
  102. /** @var CalDavBackend | \PHPUnit\Framework\MockObject\MockObject $cal */
  103. $cal = $this->getMockBuilder(CalDavBackend::class)
  104. ->disableOriginalConstructor()
  105. ->getMock();
  106. $cal->expects($this->once())->method('getCalendarsForUserCount')->willReturn(1);
  107. $cal->expects($this->never())->method('createCalendar');
  108. /** @var CardDavBackend | \PHPUnit\Framework\MockObject\MockObject $card */
  109. $card = $this->getMockBuilder(CardDavBackend::class)
  110. ->disableOriginalConstructor()
  111. ->getMock();
  112. $card->expects($this->once())->method('getAddressBooksForUserCount')->willReturn(1);
  113. $card->expects($this->never())->method('createAddressBook');
  114. $hm = new HookManager($userManager, $syncService, $cal, $card, $this->eventDispatcher);
  115. $hm->firstLogin($user);
  116. }
  117. public function testWithBirthdayCalendar() {
  118. $user = $this->getMockBuilder(IUser::class)
  119. ->disableOriginalConstructor()
  120. ->getMock();
  121. $user->expects($this->once())->method('getUID')->willReturn('newUser');
  122. /** @var IUserManager | \PHPUnit\Framework\MockObject\MockObject $userManager */
  123. $userManager = $this->getMockBuilder(IUserManager::class)
  124. ->disableOriginalConstructor()
  125. ->getMock();
  126. /** @var SyncService | \PHPUnit\Framework\MockObject\MockObject $syncService */
  127. $syncService = $this->getMockBuilder(SyncService::class)
  128. ->disableOriginalConstructor()
  129. ->getMock();
  130. /** @var CalDavBackend | \PHPUnit\Framework\MockObject\MockObject $cal */
  131. $cal = $this->getMockBuilder(CalDavBackend::class)
  132. ->disableOriginalConstructor()
  133. ->getMock();
  134. $cal->expects($this->once())->method('getCalendarsForUserCount')->willReturn(0);
  135. $cal->expects($this->once())->method('createCalendar')->with(
  136. 'principals/users/newUser',
  137. 'personal', ['{DAV:}displayname' => 'Personal']);
  138. /** @var CardDavBackend | \PHPUnit\Framework\MockObject\MockObject $card */
  139. $card = $this->getMockBuilder(CardDavBackend::class)
  140. ->disableOriginalConstructor()
  141. ->getMock();
  142. $card->expects($this->once())->method('getAddressBooksForUserCount')->willReturn(0);
  143. $card->expects($this->once())->method('createAddressBook')->with(
  144. 'principals/users/newUser',
  145. 'contacts', ['{DAV:}displayname' => 'Contacts']);
  146. $hm = new HookManager($userManager, $syncService, $cal, $card, $this->eventDispatcher);
  147. $hm->firstLogin($user);
  148. }
  149. public function testDeleteCalendar() {
  150. $user = $this->getMockBuilder(IUser::class)
  151. ->disableOriginalConstructor()
  152. ->getMock();
  153. /** @var IUserManager | \PHPUnit\Framework\MockObject\MockObject $userManager */
  154. $userManager = $this->getMockBuilder(IUserManager::class)
  155. ->disableOriginalConstructor()
  156. ->getMock();
  157. $userManager->expects($this->once())->method('get')->willReturn($user);
  158. /** @var SyncService | \PHPUnit\Framework\MockObject\MockObject $syncService */
  159. $syncService = $this->getMockBuilder(SyncService::class)
  160. ->disableOriginalConstructor()
  161. ->getMock();
  162. $syncService->expects($this->once())
  163. ->method('deleteUser');
  164. /** @var CalDavBackend | \PHPUnit\Framework\MockObject\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('deleteCalendar');
  172. $cal->expects($this->once())->method('deleteAllSharesByUser');
  173. /** @var CardDavBackend | \PHPUnit\Framework\MockObject\MockObject $card */
  174. $card = $this->getMockBuilder(CardDavBackend::class)
  175. ->disableOriginalConstructor()
  176. ->getMock();
  177. $card->expects($this->once())->method('getUsersOwnAddressBooks')->willReturn([
  178. ['id' => 'personal']
  179. ]);
  180. $card->expects($this->once())->method('deleteAddressBook');
  181. $hm = new HookManager($userManager, $syncService, $cal, $card, $this->eventDispatcher);
  182. $hm->preDeleteUser(['uid' => 'newUser']);
  183. $hm->postDeleteUser(['uid' => 'newUser']);
  184. }
  185. }