BackendTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Thomas Citharel <nextcloud@tcit.fr>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\DAV\Tests\unit\CardDAV\Activity;
  27. use OCA\DAV\CardDAV\Activity\Backend;
  28. use OCA\DAV\CardDAV\Activity\Provider\Addressbook;
  29. use OCA\DAV\CardDAV\Activity\Provider\Card;
  30. use OCP\Activity\IEvent;
  31. use OCP\Activity\IManager;
  32. use OCP\App\IAppManager;
  33. use OCP\IGroup;
  34. use OCP\IGroupManager;
  35. use OCP\IUser;
  36. use OCP\IUserManager;
  37. use OCP\IUserSession;
  38. use PHPUnit\Framework\MockObject\MockObject;
  39. use Test\TestCase;
  40. class BackendTest extends TestCase {
  41. /** @var IManager|MockObject */
  42. protected $activityManager;
  43. /** @var IGroupManager|MockObject */
  44. protected $groupManager;
  45. /** @var IUserSession|MockObject */
  46. protected $userSession;
  47. /** @var IAppManager|MockObject */
  48. protected $appManager;
  49. /** @var IUserManager|MockObject */
  50. protected $userManager;
  51. protected function setUp(): void {
  52. parent::setUp();
  53. $this->activityManager = $this->createMock(IManager::class);
  54. $this->groupManager = $this->createMock(IGroupManager::class);
  55. $this->userSession = $this->createMock(IUserSession::class);
  56. $this->appManager = $this->createMock(IAppManager::class);
  57. $this->userManager = $this->createMock(IUserManager::class);
  58. }
  59. /**
  60. * @param array $methods
  61. * @return Backend|MockObject
  62. */
  63. protected function getBackend(array $methods = []) {
  64. if (empty($methods)) {
  65. return new Backend(
  66. $this->activityManager,
  67. $this->groupManager,
  68. $this->userSession,
  69. $this->appManager,
  70. $this->userManager
  71. );
  72. } else {
  73. return $this->getMockBuilder(Backend::class)
  74. ->setConstructorArgs([
  75. $this->activityManager,
  76. $this->groupManager,
  77. $this->userSession,
  78. $this->appManager,
  79. $this->userManager
  80. ])
  81. ->onlyMethods($methods)
  82. ->getMock();
  83. }
  84. }
  85. public function dataCallTriggerAddressBookActivity(): array {
  86. return [
  87. ['onAddressbookCreate', [['data']], Addressbook::SUBJECT_ADD, [['data'], [], []]],
  88. ['onAddressbookUpdate', [['data'], ['shares'], ['changed-properties']], Addressbook::SUBJECT_UPDATE, [['data'], ['shares'], ['changed-properties']]],
  89. ['onAddressbookDelete', [['data'], ['shares']], Addressbook::SUBJECT_DELETE, [['data'], ['shares'], []]],
  90. ];
  91. }
  92. /**
  93. * @dataProvider dataCallTriggerAddressBookActivity
  94. */
  95. public function testCallTriggerAddressBookActivity(string $method, array $payload, string $expectedSubject, array $expectedPayload): void {
  96. $backend = $this->getBackend(['triggerAddressbookActivity']);
  97. $backend->expects($this->once())
  98. ->method('triggerAddressbookActivity')
  99. ->willReturnCallback(function () use ($expectedPayload, $expectedSubject): void {
  100. $arguments = func_get_args();
  101. $this->assertSame($expectedSubject, array_shift($arguments));
  102. $this->assertEquals($expectedPayload, $arguments);
  103. });
  104. call_user_func_array([$backend, $method], $payload);
  105. }
  106. public function dataTriggerAddressBookActivity(): array {
  107. return [
  108. // Add addressbook
  109. [Addressbook::SUBJECT_ADD, [], [], [], '', '', null, []],
  110. [Addressbook::SUBJECT_ADD, [
  111. 'principaluri' => 'principal/user/admin',
  112. 'id' => 42,
  113. 'uri' => 'this-uri',
  114. '{DAV:}displayname' => 'Name of addressbook',
  115. ], [], [], '', 'admin', null, ['admin']],
  116. [Addressbook::SUBJECT_ADD, [
  117. 'principaluri' => 'principal/user/admin',
  118. 'id' => 42,
  119. 'uri' => 'this-uri',
  120. '{DAV:}displayname' => 'Name of addressbook',
  121. ], [], [], 'test2', 'test2', null, ['admin']],
  122. // Update addressbook
  123. [Addressbook::SUBJECT_UPDATE, [], [], [], '', '', null, []],
  124. // No visible change - owner only
  125. [Addressbook::SUBJECT_UPDATE, [
  126. 'principaluri' => 'principal/user/admin',
  127. 'id' => 42,
  128. 'uri' => 'this-uri',
  129. '{DAV:}displayname' => 'Name of addressbook',
  130. ], ['shares'], [], '', 'admin', null, ['admin']],
  131. // Visible change
  132. [Addressbook::SUBJECT_UPDATE, [
  133. 'principaluri' => 'principal/user/admin',
  134. 'id' => 42,
  135. 'uri' => 'this-uri',
  136. '{DAV:}displayname' => 'Name of addressbook',
  137. ], ['shares'], ['{DAV:}displayname' => 'Name'], '', 'admin', ['user1'], ['user1', 'admin']],
  138. [Addressbook::SUBJECT_UPDATE, [
  139. 'principaluri' => 'principal/user/admin',
  140. 'id' => 42,
  141. 'uri' => 'this-uri',
  142. '{DAV:}displayname' => 'Name of addressbook',
  143. ], ['shares'], ['{DAV:}displayname' => 'Name'], 'test2', 'test2', ['user1'], ['user1', 'admin']],
  144. // Delete addressbook
  145. [Addressbook::SUBJECT_DELETE, [], [], [], '', '', null, []],
  146. [Addressbook::SUBJECT_DELETE, [
  147. 'principaluri' => 'principal/user/admin',
  148. 'id' => 42,
  149. 'uri' => 'this-uri',
  150. '{DAV:}displayname' => 'Name of addressbook',
  151. ], ['shares'], [], '', 'admin', [], ['admin']],
  152. [Addressbook::SUBJECT_DELETE, [
  153. 'principaluri' => 'principal/user/admin',
  154. 'id' => 42,
  155. 'uri' => 'this-uri',
  156. '{DAV:}displayname' => 'Name of addressbook',
  157. ], ['shares'], [], '', 'admin', ['user1'], ['user1', 'admin']],
  158. [Addressbook::SUBJECT_DELETE, [
  159. 'principaluri' => 'principal/user/admin',
  160. 'id' => 42,
  161. 'uri' => 'this-uri',
  162. '{DAV:}displayname' => 'Name of addressbook',
  163. ], ['shares'], [], 'test2', 'test2', ['user1'], ['user1', 'admin']],
  164. ];
  165. }
  166. /**
  167. * @dataProvider dataTriggerAddressBookActivity
  168. * @param string $action
  169. * @param array $data
  170. * @param array $shares
  171. * @param array $changedProperties
  172. * @param string $currentUser
  173. * @param string $author
  174. * @param string[]|null $shareUsers
  175. * @param string[] $users
  176. */
  177. public function testTriggerAddressBookActivity(string $action, array $data, array $shares, array $changedProperties, string $currentUser, string $author, ?array $shareUsers, array $users): void {
  178. $backend = $this->getBackend(['getUsersForShares']);
  179. if ($shareUsers === null) {
  180. $backend->expects($this->never())
  181. ->method('getUsersForShares');
  182. } else {
  183. $backend->expects($this->once())
  184. ->method('getUsersForShares')
  185. ->with($shares)
  186. ->willReturn($shareUsers);
  187. }
  188. if ($author !== '') {
  189. if ($currentUser !== '') {
  190. $this->userSession->expects($this->once())
  191. ->method('getUser')
  192. ->willReturn($this->getUserMock($currentUser));
  193. } else {
  194. $this->userSession->expects($this->once())
  195. ->method('getUser')
  196. ->willReturn(null);
  197. }
  198. $event = $this->createMock(IEvent::class);
  199. $this->activityManager->expects($this->once())
  200. ->method('generateEvent')
  201. ->willReturn($event);
  202. $event->expects($this->once())
  203. ->method('setApp')
  204. ->with('dav')
  205. ->willReturnSelf();
  206. $event->expects($this->once())
  207. ->method('setObject')
  208. ->with('addressbook', $data['id'])
  209. ->willReturnSelf();
  210. $event->expects($this->once())
  211. ->method('setType')
  212. ->with('contacts')
  213. ->willReturnSelf();
  214. $event->expects($this->once())
  215. ->method('setAuthor')
  216. ->with($author)
  217. ->willReturnSelf();
  218. $this->userManager->expects($action === Addressbook::SUBJECT_DELETE ? $this->exactly(sizeof($users)) : $this->never())
  219. ->method('userExists')
  220. ->willReturn(true);
  221. $event->expects($this->exactly(sizeof($users)))
  222. ->method('setAffectedUser')
  223. ->willReturnSelf();
  224. $event->expects($this->exactly(sizeof($users)))
  225. ->method('setSubject')
  226. ->willReturnSelf();
  227. $this->activityManager->expects($this->exactly(sizeof($users)))
  228. ->method('publish')
  229. ->with($event);
  230. } else {
  231. $this->activityManager->expects($this->never())
  232. ->method('generateEvent');
  233. }
  234. $this->invokePrivate($backend, 'triggerAddressbookActivity', [$action, $data, $shares, $changedProperties]);
  235. }
  236. public function testNoAddressbookActivityCreatedForSystemAddressbook(): void {
  237. $backend = $this->getBackend();
  238. $this->activityManager->expects($this->never())
  239. ->method('generateEvent');
  240. $this->assertEmpty($this->invokePrivate($backend, 'triggerAddressbookActivity', [Addressbook::SUBJECT_ADD, ['principaluri' => 'principals/system/system'], [], [], '', '', null, []]));
  241. }
  242. public function testUserDeletionDoesNotCreateActivity(): void {
  243. $backend = $this->getBackend();
  244. $this->userManager->expects($this->once())
  245. ->method('userExists')
  246. ->willReturn(false);
  247. $this->activityManager->expects($this->never())
  248. ->method('publish');
  249. $this->invokePrivate($backend, 'triggerAddressbookActivity', [Addressbook::SUBJECT_DELETE, [
  250. 'principaluri' => 'principal/user/admin',
  251. 'id' => 42,
  252. 'uri' => 'this-uri',
  253. '{DAV:}displayname' => 'Name of addressbook',
  254. ], [], []]);
  255. }
  256. public function dataTriggerCardActivity(): array {
  257. $cardData = "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 3.4.8//EN\r\nUID:test-user\r\nFN:test-user\r\nN:test-user;;;;\r\nEND:VCARD\r\n\r\n";
  258. return [
  259. // Add card
  260. [Card::SUBJECT_ADD, [], [], [], '', '', null, []],
  261. [Card::SUBJECT_ADD, [
  262. 'principaluri' => 'principal/user/admin',
  263. 'id' => 42,
  264. 'uri' => 'this-uri',
  265. '{DAV:}displayname' => 'Name of addressbook',
  266. ], [], [
  267. 'carddata' => $cardData
  268. ], '', 'admin', [], ['admin']],
  269. [Card::SUBJECT_ADD, [
  270. 'principaluri' => 'principal/user/admin',
  271. 'id' => 42,
  272. 'uri' => 'this-uri',
  273. '{DAV:}displayname' => 'Name of addressbook',
  274. ], [], ['carddata' => $cardData], 'test2', 'test2', [], ['admin']],
  275. // Update card
  276. [Card::SUBJECT_UPDATE, [], [], [], '', '', null, []],
  277. // No visible change - owner only
  278. [Card::SUBJECT_UPDATE, [
  279. 'principaluri' => 'principal/user/admin',
  280. 'id' => 42,
  281. 'uri' => 'this-uri',
  282. '{DAV:}displayname' => 'Name of addressbook',
  283. ], ['shares'], ['carddata' => $cardData], '', 'admin', [], ['admin']],
  284. // Visible change
  285. [Card::SUBJECT_UPDATE, [
  286. 'principaluri' => 'principal/user/admin',
  287. 'id' => 42,
  288. 'uri' => 'this-uri',
  289. '{DAV:}displayname' => 'Name of addressbook',
  290. ], ['shares'], ['carddata' => $cardData], '', 'admin', ['user1'], ['user1', 'admin']],
  291. [Card::SUBJECT_UPDATE, [
  292. 'principaluri' => 'principal/user/admin',
  293. 'id' => 42,
  294. 'uri' => 'this-uri',
  295. '{DAV:}displayname' => 'Name of addressbook',
  296. ], ['shares'], ['carddata' => $cardData], 'test2', 'test2', ['user1'], ['user1', 'admin']],
  297. // Delete card
  298. [Card::SUBJECT_DELETE, [], [], ['carddata' => $cardData], '', '', null, []],
  299. [Card::SUBJECT_DELETE, [
  300. 'principaluri' => 'principal/user/admin',
  301. 'id' => 42,
  302. 'uri' => 'this-uri',
  303. '{DAV:}displayname' => 'Name of addressbook',
  304. ], ['shares'], ['carddata' => $cardData], '', 'admin', [], ['admin']],
  305. [Card::SUBJECT_DELETE, [
  306. 'principaluri' => 'principal/user/admin',
  307. 'id' => 42,
  308. 'uri' => 'this-uri',
  309. '{DAV:}displayname' => 'Name of addressbook',
  310. ], ['shares'], ['carddata' => $cardData], '', 'admin', ['user1'], ['user1', 'admin']],
  311. [Card::SUBJECT_DELETE, [
  312. 'principaluri' => 'principal/user/admin',
  313. 'id' => 42,
  314. 'uri' => 'this-uri',
  315. '{DAV:}displayname' => 'Name of addressbook',
  316. ], ['shares'], ['carddata' => $cardData], 'test2', 'test2', ['user1'], ['user1', 'admin']],
  317. ];
  318. }
  319. /**
  320. * @dataProvider dataTriggerCardActivity
  321. * @param string $action
  322. * @param array $addressBookData
  323. * @param array $shares
  324. * @param array $cardData
  325. * @param string $currentUser
  326. * @param string $author
  327. * @param string[]|null $shareUsers
  328. * @param string[] $users
  329. */
  330. public function testTriggerCardActivity(string $action, array $addressBookData, array $shares, array $cardData, string $currentUser, string $author, ?array $shareUsers, array $users): void {
  331. $backend = $this->getBackend(['getUsersForShares']);
  332. if ($shareUsers === null) {
  333. $backend->expects($this->never())
  334. ->method('getUsersForShares');
  335. } else {
  336. $backend->expects($this->once())
  337. ->method('getUsersForShares')
  338. ->with($shares)
  339. ->willReturn($shareUsers);
  340. }
  341. if ($author !== '') {
  342. if ($currentUser !== '') {
  343. $this->userSession->expects($this->once())
  344. ->method('getUser')
  345. ->willReturn($this->getUserMock($currentUser));
  346. } else {
  347. $this->userSession->expects($this->once())
  348. ->method('getUser')
  349. ->willReturn(null);
  350. }
  351. $event = $this->createMock(IEvent::class);
  352. $this->activityManager->expects($this->once())
  353. ->method('generateEvent')
  354. ->willReturn($event);
  355. $event->expects($this->once())
  356. ->method('setApp')
  357. ->with('dav')
  358. ->willReturnSelf();
  359. $event->expects($this->once())
  360. ->method('setObject')
  361. ->with('addressbook', $addressBookData['id'])
  362. ->willReturnSelf();
  363. $event->expects($this->once())
  364. ->method('setType')
  365. ->with('contacts')
  366. ->willReturnSelf();
  367. $event->expects($this->once())
  368. ->method('setAuthor')
  369. ->with($author)
  370. ->willReturnSelf();
  371. $event->expects($this->exactly(sizeof($users)))
  372. ->method('setAffectedUser')
  373. ->willReturnSelf();
  374. $event->expects($this->exactly(sizeof($users)))
  375. ->method('setSubject')
  376. ->willReturnSelf();
  377. $this->activityManager->expects($this->exactly(sizeof($users)))
  378. ->method('publish')
  379. ->with($event);
  380. } else {
  381. $this->activityManager->expects($this->never())
  382. ->method('generateEvent');
  383. }
  384. $this->invokePrivate($backend, 'triggerCardActivity', [$action, $addressBookData, $shares, $cardData]);
  385. }
  386. public function testNoCardActivityCreatedForSystemAddressbook(): void {
  387. $backend = $this->getBackend();
  388. $this->activityManager->expects($this->never())
  389. ->method('generateEvent');
  390. $this->assertEmpty($this->invokePrivate($backend, 'triggerCardActivity', [Card::SUBJECT_UPDATE, ['principaluri' => 'principals/system/system'], [], []]));
  391. }
  392. public function dataGetUsersForShares(): array {
  393. return [
  394. [
  395. [],
  396. [],
  397. [],
  398. ],
  399. [
  400. [
  401. ['{http://owncloud.org/ns}principal' => 'principal/users/user1'],
  402. ['{http://owncloud.org/ns}principal' => 'principal/users/user2'],
  403. ['{http://owncloud.org/ns}principal' => 'principal/users/user2'],
  404. ['{http://owncloud.org/ns}principal' => 'principal/users/user2'],
  405. ['{http://owncloud.org/ns}principal' => 'principal/users/user3'],
  406. ],
  407. [],
  408. ['user1', 'user2', 'user3'],
  409. ],
  410. [
  411. [
  412. ['{http://owncloud.org/ns}principal' => 'principal/users/user1'],
  413. ['{http://owncloud.org/ns}principal' => 'principal/users/user2'],
  414. ['{http://owncloud.org/ns}principal' => 'principal/users/user2'],
  415. ['{http://owncloud.org/ns}principal' => 'principal/groups/group2'],
  416. ['{http://owncloud.org/ns}principal' => 'principal/groups/group3'],
  417. ],
  418. ['group2' => null, 'group3' => null],
  419. ['user1', 'user2'],
  420. ],
  421. [
  422. [
  423. ['{http://owncloud.org/ns}principal' => 'principal/users/user1'],
  424. ['{http://owncloud.org/ns}principal' => 'principal/users/user2'],
  425. ['{http://owncloud.org/ns}principal' => 'principal/users/user2'],
  426. ['{http://owncloud.org/ns}principal' => 'principal/groups/group2'],
  427. ['{http://owncloud.org/ns}principal' => 'principal/groups/group3'],
  428. ],
  429. ['group2' => ['user1', 'user2', 'user3'], 'group3' => ['user2', 'user3', 'user4']],
  430. ['user1', 'user2', 'user3', 'user4'],
  431. ],
  432. ];
  433. }
  434. /**
  435. * @dataProvider dataGetUsersForShares
  436. * @param array $shares
  437. * @param array $groups
  438. * @param array $expected
  439. */
  440. public function testGetUsersForShares(array $shares, array $groups, array $expected): void {
  441. $backend = $this->getBackend();
  442. $getGroups = [];
  443. foreach ($groups as $gid => $members) {
  444. if ($members === null) {
  445. $getGroups[] = [$gid, null];
  446. continue;
  447. }
  448. $group = $this->createMock(IGroup::class);
  449. $group->expects($this->once())
  450. ->method('getUsers')
  451. ->willReturn($this->getUsers($members));
  452. $getGroups[] = [$gid, $group];
  453. }
  454. $this->groupManager->expects($this->exactly(sizeof($getGroups)))
  455. ->method('get')
  456. ->willReturnMap($getGroups);
  457. $users = $this->invokePrivate($backend, 'getUsersForShares', [$shares]);
  458. sort($users);
  459. $this->assertEquals($expected, $users);
  460. }
  461. /**
  462. * @param string[] $users
  463. * @return IUser[]|MockObject[]
  464. */
  465. protected function getUsers(array $users): array {
  466. $list = [];
  467. foreach ($users as $user) {
  468. $list[] = $this->getUserMock($user);
  469. }
  470. return $list;
  471. }
  472. /**
  473. * @param string $uid
  474. * @return IUser|MockObject
  475. */
  476. protected function getUserMock(string $uid) {
  477. $user = $this->createMock(IUser::class);
  478. $user->expects($this->once())
  479. ->method('getUID')
  480. ->willReturn($uid);
  481. return $user;
  482. }
  483. }