HookManagerTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 Robin Appelman <robin@icewind.nl>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Thomas Citharel <nextcloud@tcit.fr>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OCA\DAV\Tests\unit\DAV;
  31. use OCA\DAV\CalDAV\CalDavBackend;
  32. use OCA\DAV\CardDAV\CardDavBackend;
  33. use OCA\DAV\CardDAV\SyncService;
  34. use OCA\DAV\HookManager;
  35. use OCP\IL10N;
  36. use OCP\IUser;
  37. use OCP\IUserManager;
  38. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  39. use Test\TestCase;
  40. class HookManagerTest extends TestCase {
  41. /** @var IL10N */
  42. private $l10n;
  43. /** @var EventDispatcherInterface | \PHPUnit_Framework_MockObject_MockObject */
  44. private $eventDispatcher;
  45. protected function setUp(): void {
  46. parent::setUp();
  47. $this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
  48. $this->l10n = $this->createMock(IL10N::class);
  49. $this->l10n
  50. ->expects($this->any())
  51. ->method('t')
  52. ->willReturnCallback(function ($text, $parameters = []) {
  53. return vsprintf($text, $parameters);
  54. });
  55. }
  56. public function test() {
  57. $user = $this->getMockBuilder(IUser::class)
  58. ->disableOriginalConstructor()
  59. ->getMock();
  60. $user->expects($this->once())->method('getUID')->willReturn('newUser');
  61. /** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject $userManager */
  62. $userManager = $this->getMockBuilder(IUserManager::class)
  63. ->disableOriginalConstructor()
  64. ->getMock();
  65. /** @var SyncService | \PHPUnit_Framework_MockObject_MockObject $syncService */
  66. $syncService = $this->getMockBuilder(SyncService::class)
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. /** @var CalDavBackend | \PHPUnit_Framework_MockObject_MockObject $cal */
  70. $cal = $this->getMockBuilder(CalDavBackend::class)
  71. ->disableOriginalConstructor()
  72. ->getMock();
  73. $cal->expects($this->once())->method('getCalendarsForUserCount')->willReturn(0);
  74. $cal->expects($this->once())->method('createCalendar')->with(
  75. 'principals/users/newUser',
  76. 'personal', ['{DAV:}displayname' => 'Personal']);
  77. /** @var CardDavBackend | \PHPUnit_Framework_MockObject_MockObject $card */
  78. $card = $this->getMockBuilder(CardDavBackend::class)
  79. ->disableOriginalConstructor()
  80. ->getMock();
  81. $card->expects($this->once())->method('getAddressBooksForUserCount')->willReturn(0);
  82. $card->expects($this->once())->method('createAddressBook')->with(
  83. 'principals/users/newUser',
  84. 'contacts', ['{DAV:}displayname' => 'Contacts']);
  85. $hm = new HookManager($userManager, $syncService, $cal, $card, $this->eventDispatcher);
  86. $hm->firstLogin($user);
  87. }
  88. public function testWithExisting() {
  89. $user = $this->getMockBuilder(IUser::class)
  90. ->disableOriginalConstructor()
  91. ->getMock();
  92. $user->expects($this->once())->method('getUID')->willReturn('newUser');
  93. /** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject $userManager */
  94. $userManager = $this->getMockBuilder(IUserManager::class)
  95. ->disableOriginalConstructor()
  96. ->getMock();
  97. /** @var SyncService | \PHPUnit_Framework_MockObject_MockObject $syncService */
  98. $syncService = $this->getMockBuilder(SyncService::class)
  99. ->disableOriginalConstructor()
  100. ->getMock();
  101. /** @var CalDavBackend | \PHPUnit_Framework_MockObject_MockObject $cal */
  102. $cal = $this->getMockBuilder(CalDavBackend::class)
  103. ->disableOriginalConstructor()
  104. ->getMock();
  105. $cal->expects($this->once())->method('getCalendarsForUserCount')->willReturn(1);
  106. $cal->expects($this->never())->method('createCalendar');
  107. /** @var CardDavBackend | \PHPUnit_Framework_MockObject_MockObject $card */
  108. $card = $this->getMockBuilder(CardDavBackend::class)
  109. ->disableOriginalConstructor()
  110. ->getMock();
  111. $card->expects($this->once())->method('getAddressBooksForUserCount')->willReturn(1);
  112. $card->expects($this->never())->method('createAddressBook');
  113. $hm = new HookManager($userManager, $syncService, $cal, $card, $this->eventDispatcher);
  114. $hm->firstLogin($user);
  115. }
  116. public function testWithBirthdayCalendar() {
  117. $user = $this->getMockBuilder(IUser::class)
  118. ->disableOriginalConstructor()
  119. ->getMock();
  120. $user->expects($this->once())->method('getUID')->willReturn('newUser');
  121. /** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject $userManager */
  122. $userManager = $this->getMockBuilder(IUserManager::class)
  123. ->disableOriginalConstructor()
  124. ->getMock();
  125. /** @var SyncService | \PHPUnit_Framework_MockObject_MockObject $syncService */
  126. $syncService = $this->getMockBuilder(SyncService::class)
  127. ->disableOriginalConstructor()
  128. ->getMock();
  129. /** @var CalDavBackend | \PHPUnit_Framework_MockObject_MockObject $cal */
  130. $cal = $this->getMockBuilder(CalDavBackend::class)
  131. ->disableOriginalConstructor()
  132. ->getMock();
  133. $cal->expects($this->once())->method('getCalendarsForUserCount')->willReturn(0);
  134. $cal->expects($this->once())->method('createCalendar')->with(
  135. 'principals/users/newUser',
  136. 'personal', ['{DAV:}displayname' => 'Personal']);
  137. /** @var CardDavBackend | \PHPUnit_Framework_MockObject_MockObject $card */
  138. $card = $this->getMockBuilder(CardDavBackend::class)
  139. ->disableOriginalConstructor()
  140. ->getMock();
  141. $card->expects($this->once())->method('getAddressBooksForUserCount')->willReturn(0);
  142. $card->expects($this->once())->method('createAddressBook')->with(
  143. 'principals/users/newUser',
  144. 'contacts', ['{DAV:}displayname' => 'Contacts']);
  145. $hm = new HookManager($userManager, $syncService, $cal, $card, $this->eventDispatcher);
  146. $hm->firstLogin($user);
  147. }
  148. public function testDeleteCalendar() {
  149. $user = $this->getMockBuilder(IUser::class)
  150. ->disableOriginalConstructor()
  151. ->getMock();
  152. /** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject $userManager */
  153. $userManager = $this->getMockBuilder(IUserManager::class)
  154. ->disableOriginalConstructor()
  155. ->getMock();
  156. $userManager->expects($this->once())->method('get')->willReturn($user);
  157. /** @var SyncService | \PHPUnit_Framework_MockObject_MockObject $syncService */
  158. $syncService = $this->getMockBuilder(SyncService::class)
  159. ->disableOriginalConstructor()
  160. ->getMock();
  161. $syncService->expects($this->once())
  162. ->method('deleteUser');
  163. /** @var CalDavBackend | \PHPUnit_Framework_MockObject_MockObject $cal */
  164. $cal = $this->getMockBuilder(CalDavBackend::class)
  165. ->disableOriginalConstructor()
  166. ->getMock();
  167. $cal->expects($this->once())->method('getUsersOwnCalendars')->willReturn([
  168. ['id' => 'personal']
  169. ]);
  170. $cal->expects($this->once())->method('deleteCalendar');
  171. $cal->expects($this->once())->method('deleteAllSharesByUser');
  172. /** @var CardDavBackend | \PHPUnit_Framework_MockObject_MockObject $card */
  173. $card = $this->getMockBuilder(CardDavBackend::class)
  174. ->disableOriginalConstructor()
  175. ->getMock();
  176. $card->expects($this->once())->method('getUsersOwnAddressBooks')->willReturn([
  177. ['id' => 'personal']
  178. ]);
  179. $card->expects($this->once())->method('deleteAddressBook');
  180. $hm = new HookManager($userManager, $syncService, $cal, $card, $this->eventDispatcher);
  181. $hm->preDeleteUser(['uid' => 'newUser']);
  182. $hm->postDeleteUser(['uid' => 'newUser']);
  183. }
  184. }