Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen committed Nov 3, 2016
1 parent 36b656e commit 7a0e003
Showing 1 changed file with 102 additions and 5 deletions.
107 changes: 102 additions & 5 deletions apps/dav/tests/unit/CalDAV/Activity/BackendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use OCA\DAV\CalDAV\Activity\Extension;
use OCP\Activity\IEvent;
use OCP\Activity\IManager;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\IUserSession;
Expand Down Expand Up @@ -181,13 +182,9 @@ public function testTriggerCalendarActivity($action, array $data, array $shares,

if ($author !== '') {
if ($currentUser !== '') {
$user = $this->createMock(IUser::class);
$this->userSession->expects($this->once())
->method('getUser')
->willReturn($user);
$user->expects($this->once())
->method('getUID')
->willReturn($currentUser);
->willReturn($this->getUserMock($currentUser));
} else {
$this->userSession->expects($this->once())
->method('getUser')
Expand Down Expand Up @@ -232,4 +229,104 @@ public function testTriggerCalendarActivity($action, array $data, array $shares,

$this->invokePrivate($backend, 'triggerCalendarActivity', [$action, $data, $shares, $changedProperties]);
}

public function dataGetUsersForShares() {
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) {
$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[]|\PHPUnit_Framework_MockObject_MockObject[]
*/
protected function getUsers(array $users) {
$list = [];
foreach ($users as $user) {
$list[] = $this->getUserMock($user);
}
return $list;
}

/**
* @param string $uid
* @return IUser|\PHPUnit_Framework_MockObject_MockObject
*/
protected function getUserMock($uid) {
$user = $this->createMock(IUser::class);
$user->expects($this->once())
->method('getUID')
->willReturn($uid);
return $user;
}
}

0 comments on commit 7a0e003

Please sign in to comment.