123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511 |
- <?php
- /**
- * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
- * SPDX-License-Identifier: AGPL-3.0-or-later
- */
- namespace OCA\DAV\Tests\unit\CardDAV\Activity;
- use OCA\DAV\CardDAV\Activity\Backend;
- use OCA\DAV\CardDAV\Activity\Provider\Addressbook;
- use OCA\DAV\CardDAV\Activity\Provider\Card;
- use OCP\Activity\IEvent;
- use OCP\Activity\IManager;
- use OCP\App\IAppManager;
- use OCP\IGroup;
- use OCP\IGroupManager;
- use OCP\IUser;
- use OCP\IUserManager;
- use OCP\IUserSession;
- use PHPUnit\Framework\MockObject\MockObject;
- use Test\TestCase;
- class BackendTest extends TestCase {
- /** @var IManager|MockObject */
- protected $activityManager;
- /** @var IGroupManager|MockObject */
- protected $groupManager;
- /** @var IUserSession|MockObject */
- protected $userSession;
- /** @var IAppManager|MockObject */
- protected $appManager;
- /** @var IUserManager|MockObject */
- protected $userManager;
- protected function setUp(): void {
- parent::setUp();
- $this->activityManager = $this->createMock(IManager::class);
- $this->groupManager = $this->createMock(IGroupManager::class);
- $this->userSession = $this->createMock(IUserSession::class);
- $this->appManager = $this->createMock(IAppManager::class);
- $this->userManager = $this->createMock(IUserManager::class);
- }
- /**
- * @param array $methods
- * @return Backend|MockObject
- */
- protected function getBackend(array $methods = []) {
- if (empty($methods)) {
- return new Backend(
- $this->activityManager,
- $this->groupManager,
- $this->userSession,
- $this->appManager,
- $this->userManager
- );
- } else {
- return $this->getMockBuilder(Backend::class)
- ->setConstructorArgs([
- $this->activityManager,
- $this->groupManager,
- $this->userSession,
- $this->appManager,
- $this->userManager
- ])
- ->onlyMethods($methods)
- ->getMock();
- }
- }
- public function dataCallTriggerAddressBookActivity(): array {
- return [
- ['onAddressbookCreate', [['data']], Addressbook::SUBJECT_ADD, [['data'], [], []]],
- ['onAddressbookUpdate', [['data'], ['shares'], ['changed-properties']], Addressbook::SUBJECT_UPDATE, [['data'], ['shares'], ['changed-properties']]],
- ['onAddressbookDelete', [['data'], ['shares']], Addressbook::SUBJECT_DELETE, [['data'], ['shares'], []]],
- ];
- }
- /**
- * @dataProvider dataCallTriggerAddressBookActivity
- */
- public function testCallTriggerAddressBookActivity(string $method, array $payload, string $expectedSubject, array $expectedPayload): void {
- $backend = $this->getBackend(['triggerAddressbookActivity']);
- $backend->expects($this->once())
- ->method('triggerAddressbookActivity')
- ->willReturnCallback(function () use ($expectedPayload, $expectedSubject): void {
- $arguments = func_get_args();
- $this->assertSame($expectedSubject, array_shift($arguments));
- $this->assertEquals($expectedPayload, $arguments);
- });
- call_user_func_array([$backend, $method], $payload);
- }
- public function dataTriggerAddressBookActivity(): array {
- return [
- // Add addressbook
- [Addressbook::SUBJECT_ADD, [], [], [], '', '', null, []],
- [Addressbook::SUBJECT_ADD, [
- 'principaluri' => 'principal/user/admin',
- 'id' => 42,
- 'uri' => 'this-uri',
- '{DAV:}displayname' => 'Name of addressbook',
- ], [], [], '', 'admin', null, ['admin']],
- [Addressbook::SUBJECT_ADD, [
- 'principaluri' => 'principal/user/admin',
- 'id' => 42,
- 'uri' => 'this-uri',
- '{DAV:}displayname' => 'Name of addressbook',
- ], [], [], 'test2', 'test2', null, ['admin']],
- // Update addressbook
- [Addressbook::SUBJECT_UPDATE, [], [], [], '', '', null, []],
- // No visible change - owner only
- [Addressbook::SUBJECT_UPDATE, [
- 'principaluri' => 'principal/user/admin',
- 'id' => 42,
- 'uri' => 'this-uri',
- '{DAV:}displayname' => 'Name of addressbook',
- ], ['shares'], [], '', 'admin', null, ['admin']],
- // Visible change
- [Addressbook::SUBJECT_UPDATE, [
- 'principaluri' => 'principal/user/admin',
- 'id' => 42,
- 'uri' => 'this-uri',
- '{DAV:}displayname' => 'Name of addressbook',
- ], ['shares'], ['{DAV:}displayname' => 'Name'], '', 'admin', ['user1'], ['user1', 'admin']],
- [Addressbook::SUBJECT_UPDATE, [
- 'principaluri' => 'principal/user/admin',
- 'id' => 42,
- 'uri' => 'this-uri',
- '{DAV:}displayname' => 'Name of addressbook',
- ], ['shares'], ['{DAV:}displayname' => 'Name'], 'test2', 'test2', ['user1'], ['user1', 'admin']],
- // Delete addressbook
- [Addressbook::SUBJECT_DELETE, [], [], [], '', '', null, []],
- [Addressbook::SUBJECT_DELETE, [
- 'principaluri' => 'principal/user/admin',
- 'id' => 42,
- 'uri' => 'this-uri',
- '{DAV:}displayname' => 'Name of addressbook',
- ], ['shares'], [], '', 'admin', [], ['admin']],
- [Addressbook::SUBJECT_DELETE, [
- 'principaluri' => 'principal/user/admin',
- 'id' => 42,
- 'uri' => 'this-uri',
- '{DAV:}displayname' => 'Name of addressbook',
- ], ['shares'], [], '', 'admin', ['user1'], ['user1', 'admin']],
- [Addressbook::SUBJECT_DELETE, [
- 'principaluri' => 'principal/user/admin',
- 'id' => 42,
- 'uri' => 'this-uri',
- '{DAV:}displayname' => 'Name of addressbook',
- ], ['shares'], [], 'test2', 'test2', ['user1'], ['user1', 'admin']],
- ];
- }
- /**
- * @dataProvider dataTriggerAddressBookActivity
- * @param string $action
- * @param array $data
- * @param array $shares
- * @param array $changedProperties
- * @param string $currentUser
- * @param string $author
- * @param string[]|null $shareUsers
- * @param string[] $users
- */
- public function testTriggerAddressBookActivity(string $action, array $data, array $shares, array $changedProperties, string $currentUser, string $author, ?array $shareUsers, array $users): void {
- $backend = $this->getBackend(['getUsersForShares']);
- if ($shareUsers === null) {
- $backend->expects($this->never())
- ->method('getUsersForShares');
- } else {
- $backend->expects($this->once())
- ->method('getUsersForShares')
- ->with($shares)
- ->willReturn($shareUsers);
- }
- if ($author !== '') {
- if ($currentUser !== '') {
- $this->userSession->expects($this->once())
- ->method('getUser')
- ->willReturn($this->getUserMock($currentUser));
- } else {
- $this->userSession->expects($this->once())
- ->method('getUser')
- ->willReturn(null);
- }
- $event = $this->createMock(IEvent::class);
- $this->activityManager->expects($this->once())
- ->method('generateEvent')
- ->willReturn($event);
- $event->expects($this->once())
- ->method('setApp')
- ->with('dav')
- ->willReturnSelf();
- $event->expects($this->once())
- ->method('setObject')
- ->with('addressbook', $data['id'])
- ->willReturnSelf();
- $event->expects($this->once())
- ->method('setType')
- ->with('contacts')
- ->willReturnSelf();
- $event->expects($this->once())
- ->method('setAuthor')
- ->with($author)
- ->willReturnSelf();
- $this->userManager->expects($action === Addressbook::SUBJECT_DELETE ? $this->exactly(sizeof($users)) : $this->never())
- ->method('userExists')
- ->willReturn(true);
- $event->expects($this->exactly(sizeof($users)))
- ->method('setAffectedUser')
- ->willReturnSelf();
- $event->expects($this->exactly(sizeof($users)))
- ->method('setSubject')
- ->willReturnSelf();
- $this->activityManager->expects($this->exactly(sizeof($users)))
- ->method('publish')
- ->with($event);
- } else {
- $this->activityManager->expects($this->never())
- ->method('generateEvent');
- }
- $this->invokePrivate($backend, 'triggerAddressbookActivity', [$action, $data, $shares, $changedProperties]);
- }
- public function testNoAddressbookActivityCreatedForSystemAddressbook(): void {
- $backend = $this->getBackend();
- $this->activityManager->expects($this->never())
- ->method('generateEvent');
- $this->assertEmpty($this->invokePrivate($backend, 'triggerAddressbookActivity', [Addressbook::SUBJECT_ADD, ['principaluri' => 'principals/system/system'], [], [], '', '', null, []]));
- }
- public function testUserDeletionDoesNotCreateActivity(): void {
- $backend = $this->getBackend();
- $this->userManager->expects($this->once())
- ->method('userExists')
- ->willReturn(false);
- $this->activityManager->expects($this->never())
- ->method('publish');
- $this->invokePrivate($backend, 'triggerAddressbookActivity', [Addressbook::SUBJECT_DELETE, [
- 'principaluri' => 'principal/user/admin',
- 'id' => 42,
- 'uri' => 'this-uri',
- '{DAV:}displayname' => 'Name of addressbook',
- ], [], []]);
- }
- public function dataTriggerCardActivity(): array {
- $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";
- return [
- // Add card
- [Card::SUBJECT_ADD, [], [], [], '', '', null, []],
- [Card::SUBJECT_ADD, [
- 'principaluri' => 'principal/user/admin',
- 'id' => 42,
- 'uri' => 'this-uri',
- '{DAV:}displayname' => 'Name of addressbook',
- ], [], [
- 'carddata' => $cardData
- ], '', 'admin', [], ['admin']],
- [Card::SUBJECT_ADD, [
- 'principaluri' => 'principal/user/admin',
- 'id' => 42,
- 'uri' => 'this-uri',
- '{DAV:}displayname' => 'Name of addressbook',
- ], [], ['carddata' => $cardData], 'test2', 'test2', [], ['admin']],
- // Update card
- [Card::SUBJECT_UPDATE, [], [], [], '', '', null, []],
- // No visible change - owner only
- [Card::SUBJECT_UPDATE, [
- 'principaluri' => 'principal/user/admin',
- 'id' => 42,
- 'uri' => 'this-uri',
- '{DAV:}displayname' => 'Name of addressbook',
- ], ['shares'], ['carddata' => $cardData], '', 'admin', [], ['admin']],
- // Visible change
- [Card::SUBJECT_UPDATE, [
- 'principaluri' => 'principal/user/admin',
- 'id' => 42,
- 'uri' => 'this-uri',
- '{DAV:}displayname' => 'Name of addressbook',
- ], ['shares'], ['carddata' => $cardData], '', 'admin', ['user1'], ['user1', 'admin']],
- [Card::SUBJECT_UPDATE, [
- 'principaluri' => 'principal/user/admin',
- 'id' => 42,
- 'uri' => 'this-uri',
- '{DAV:}displayname' => 'Name of addressbook',
- ], ['shares'], ['carddata' => $cardData], 'test2', 'test2', ['user1'], ['user1', 'admin']],
- // Delete card
- [Card::SUBJECT_DELETE, [], [], ['carddata' => $cardData], '', '', null, []],
- [Card::SUBJECT_DELETE, [
- 'principaluri' => 'principal/user/admin',
- 'id' => 42,
- 'uri' => 'this-uri',
- '{DAV:}displayname' => 'Name of addressbook',
- ], ['shares'], ['carddata' => $cardData], '', 'admin', [], ['admin']],
- [Card::SUBJECT_DELETE, [
- 'principaluri' => 'principal/user/admin',
- 'id' => 42,
- 'uri' => 'this-uri',
- '{DAV:}displayname' => 'Name of addressbook',
- ], ['shares'], ['carddata' => $cardData], '', 'admin', ['user1'], ['user1', 'admin']],
- [Card::SUBJECT_DELETE, [
- 'principaluri' => 'principal/user/admin',
- 'id' => 42,
- 'uri' => 'this-uri',
- '{DAV:}displayname' => 'Name of addressbook',
- ], ['shares'], ['carddata' => $cardData], 'test2', 'test2', ['user1'], ['user1', 'admin']],
- ];
- }
- /**
- * @dataProvider dataTriggerCardActivity
- * @param string $action
- * @param array $addressBookData
- * @param array $shares
- * @param array $cardData
- * @param string $currentUser
- * @param string $author
- * @param string[]|null $shareUsers
- * @param string[] $users
- */
- public function testTriggerCardActivity(string $action, array $addressBookData, array $shares, array $cardData, string $currentUser, string $author, ?array $shareUsers, array $users): void {
- $backend = $this->getBackend(['getUsersForShares']);
- if ($shareUsers === null) {
- $backend->expects($this->never())
- ->method('getUsersForShares');
- } else {
- $backend->expects($this->once())
- ->method('getUsersForShares')
- ->with($shares)
- ->willReturn($shareUsers);
- }
- if ($author !== '') {
- if ($currentUser !== '') {
- $this->userSession->expects($this->once())
- ->method('getUser')
- ->willReturn($this->getUserMock($currentUser));
- } else {
- $this->userSession->expects($this->once())
- ->method('getUser')
- ->willReturn(null);
- }
- $event = $this->createMock(IEvent::class);
- $this->activityManager->expects($this->once())
- ->method('generateEvent')
- ->willReturn($event);
- $event->expects($this->once())
- ->method('setApp')
- ->with('dav')
- ->willReturnSelf();
- $event->expects($this->once())
- ->method('setObject')
- ->with('addressbook', $addressBookData['id'])
- ->willReturnSelf();
- $event->expects($this->once())
- ->method('setType')
- ->with('contacts')
- ->willReturnSelf();
- $event->expects($this->once())
- ->method('setAuthor')
- ->with($author)
- ->willReturnSelf();
- $event->expects($this->exactly(sizeof($users)))
- ->method('setAffectedUser')
- ->willReturnSelf();
- $event->expects($this->exactly(sizeof($users)))
- ->method('setSubject')
- ->willReturnSelf();
- $this->activityManager->expects($this->exactly(sizeof($users)))
- ->method('publish')
- ->with($event);
- } else {
- $this->activityManager->expects($this->never())
- ->method('generateEvent');
- }
- $this->invokePrivate($backend, 'triggerCardActivity', [$action, $addressBookData, $shares, $cardData]);
- }
- public function testNoCardActivityCreatedForSystemAddressbook(): void {
- $backend = $this->getBackend();
- $this->activityManager->expects($this->never())
- ->method('generateEvent');
- $this->assertEmpty($this->invokePrivate($backend, 'triggerCardActivity', [Card::SUBJECT_UPDATE, ['principaluri' => 'principals/system/system'], [], []]));
- }
- public function dataGetUsersForShares(): array {
- return [
- [
- [],
- [],
- [],
- ],
- [
- [
- ['{http://owncloud.org/ns}principal' => 'principal/users/user1'],
- ['{http://owncloud.org/ns}principal' => 'principal/users/user2'],
- ['{http://owncloud.org/ns}principal' => 'principal/users/user2'],
- ['{http://owncloud.org/ns}principal' => 'principal/users/user2'],
- ['{http://owncloud.org/ns}principal' => 'principal/users/user3'],
- ],
- [],
- ['user1', 'user2', 'user3'],
- ],
- [
- [
- ['{http://owncloud.org/ns}principal' => 'principal/users/user1'],
- ['{http://owncloud.org/ns}principal' => 'principal/users/user2'],
- ['{http://owncloud.org/ns}principal' => 'principal/users/user2'],
- ['{http://owncloud.org/ns}principal' => 'principal/groups/group2'],
- ['{http://owncloud.org/ns}principal' => 'principal/groups/group3'],
- ],
- ['group2' => null, 'group3' => null],
- ['user1', 'user2'],
- ],
- [
- [
- ['{http://owncloud.org/ns}principal' => 'principal/users/user1'],
- ['{http://owncloud.org/ns}principal' => 'principal/users/user2'],
- ['{http://owncloud.org/ns}principal' => 'principal/users/user2'],
- ['{http://owncloud.org/ns}principal' => 'principal/groups/group2'],
- ['{http://owncloud.org/ns}principal' => 'principal/groups/group3'],
- ],
- ['group2' => ['user1', 'user2', 'user3'], 'group3' => ['user2', 'user3', 'user4']],
- ['user1', 'user2', 'user3', 'user4'],
- ],
- ];
- }
- /**
- * @dataProvider dataGetUsersForShares
- * @param array $shares
- * @param array $groups
- * @param array $expected
- */
- public function testGetUsersForShares(array $shares, array $groups, array $expected): void {
- $backend = $this->getBackend();
- $getGroups = [];
- foreach ($groups as $gid => $members) {
- if ($members === null) {
- $getGroups[] = [$gid, null];
- continue;
- }
- $group = $this->createMock(IGroup::class);
- $group->expects($this->once())
- ->method('getUsers')
- ->willReturn($this->getUsers($members));
- $getGroups[] = [$gid, $group];
- }
- $this->groupManager->expects($this->exactly(sizeof($getGroups)))
- ->method('get')
- ->willReturnMap($getGroups);
- $users = $this->invokePrivate($backend, 'getUsersForShares', [$shares]);
- sort($users);
- $this->assertEquals($expected, $users);
- }
- /**
- * @param string[] $users
- * @return IUser[]|MockObject[]
- */
- protected function getUsers(array $users): array {
- $list = [];
- foreach ($users as $user) {
- $list[] = $this->getUserMock($user);
- }
- return $list;
- }
- /**
- * @param string $uid
- * @return IUser|MockObject
- */
- protected function getUserMock(string $uid) {
- $user = $this->createMock(IUser::class);
- $user->expects($this->once())
- ->method('getUID')
- ->willReturn($uid);
- return $user;
- }
- }
|