-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Expand file tree
/
Copy pathApplicableHelperTest.php
More file actions
170 lines (148 loc) · 5.79 KB
/
Copy pathApplicableHelperTest.php
File metadata and controls
170 lines (148 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Files_External\Tests;
use OCA\Files_External\Lib\ApplicableHelper;
use OCA\Files_External\Lib\StorageConfig;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\IUserManager;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
class ApplicableHelperTest extends TestCase {
private IUserManager|MockObject $userManager;
private IGroupManager|MockObject $groupManager;
/** @var list<string> */
private array $users = [];
/** @var array<string, list<string>> */
private array $groups = [];
private ApplicableHelper $applicableHelper;
protected function setUp(): void {
parent::setUp();
$this->userManager = $this->createMock(IUserManager::class);
$this->groupManager = $this->createMock(IGroupManager::class);
$this->userManager->method('get')
->willReturnCallback(function (string $id) {
$user = $this->createMock(IUser::class);
$user->method('getUID')->willReturn($id);
return $user;
});
$this->userManager->method('getSeenUsers')
->willReturnCallback(fn () => new \ArrayIterator(array_map($this->userManager->get(...), $this->users)));
$this->groupManager->method('get')
->willReturnCallback(function (string $id) {
$group = $this->createMock(IGroup::class);
$group->method('getGID')->willReturn($id);
$group->method('getUsers')
->willReturn(array_map($this->userManager->get(...), $this->groups[$id] ?: []));
return $group;
});
$this->groupManager->method('getUserGroupIds')
->willReturnCallback(function (IUser $user) {
$groups = [];
foreach ($this->groups as $group => $users) {
if (in_array($user->getUID(), $users)) {
$groups[] = $group;
}
}
return $groups;
});
$this->applicableHelper = new ApplicableHelper($this->userManager, $this->groupManager);
$this->users = ['user1', 'user2', 'user3', 'user4'];
$this->groups = [
'group1' => ['user1', 'user2'],
'group2' => ['user3'],
];
}
public static function usersForStorageProvider(): array {
return [
[[], [], ['user1', 'user2', 'user3', 'user4']],
[['user1'], [], ['user1']],
[['user1', 'user3'], [], ['user1', 'user3']],
[['user1'], ['group1'], ['user1', 'user2']],
[['user1'], ['group2'], ['user1', 'user3']],
];
}
#[DataProvider('usersForStorageProvider')]
public function testGetUsersForStorage(array $applicableUsers, array $applicableGroups, array $expected) {
$storage = $this->createMock(StorageConfig::class);
$storage->method('getApplicableUsers')
->willReturn($applicableUsers);
$storage->method('getApplicableGroups')
->willReturn($applicableGroups);
$result = iterator_to_array($this->applicableHelper->getUsersForStorage($storage));
$result = array_map(fn (IUser $user) => $user->getUID(), $result);
sort($result);
sort($expected);
$this->assertEquals($expected, $result);
}
public static function applicableProvider(): array {
return [
[[], [], 'user1', true],
[['user1'], [], 'user1', true],
[['user1'], [], 'user2', false],
[['user1', 'user3'], [], 'user1', true],
[['user1', 'user3'], [], 'user2', false],
[['user1'], ['group1'], 'user1', true],
[['user1'], ['group1'], 'user2', true],
[['user1'], ['group1'], 'user3', false],
[['user1'], ['group1'], 'user4', false],
[['user1'], ['group2'], 'user1', true],
[['user1'], ['group2'], 'user2', false],
[['user1'], ['group2'], 'user3', true],
[['user1'], ['group1'], 'user4', false],
];
}
#[DataProvider('applicableProvider')]
public function testIsApplicable(array $applicableUsers, array $applicableGroups, string $user, bool $expected) {
$storage = $this->createMock(StorageConfig::class);
$storage->method('getApplicableUsers')
->willReturn($applicableUsers);
$storage->method('getApplicableGroups')
->willReturn($applicableGroups);
$this->assertEquals($expected, $this->applicableHelper->isApplicableForUser($storage, $this->userManager->get($user)));
}
public static function diffProvider(): array {
return [
[[], [], [], [], []], // both all
[['user1'], [], [], [], []], // all added
[[], [], ['user1'], [], ['user2', 'user3', 'user4']], // all removed
[[], [], [], ['group1'], ['user3', 'user4']], // all removed
[[], [], ['user3'], ['group1'], ['user4']], // all removed
[['user1'], [], ['user1'], [], []],
[['user1'], [], ['user1', 'user2'], [], []],
[['user1'], [], ['user2'], [], ['user1']],
[['user1'], [], [], ['group1'], []],
[['user1'], [], [], ['group2'], ['user1']],
[[], ['group1'], [], ['group2'], ['user1', 'user2']],
[[], ['group1'], ['user1'], [], ['user2']],
[['user1'], ['group1'], ['user1'], [], ['user2']],
[['user1'], ['group1'], [], ['group1'], []],
[['user1'], ['group1'], [], ['group2'], ['user1', 'user2']],
[['user1'], ['group1'], ['user1'], ['group2'], ['user2']],
];
}
#[DataProvider('diffProvider')]
public function testDiff(array $applicableUsersA, array $applicableGroupsA, array $applicableUsersB, array $applicableGroupsB, array $expected) {
$storageA = $this->createMock(StorageConfig::class);
$storageA->method('getApplicableUsers')
->willReturn($applicableUsersA);
$storageA->method('getApplicableGroups')
->willReturn($applicableGroupsA);
$storageB = $this->createMock(StorageConfig::class);
$storageB->method('getApplicableUsers')
->willReturn($applicableUsersB);
$storageB->method('getApplicableGroups')
->willReturn($applicableGroupsB);
$result = iterator_to_array($this->applicableHelper->diffApplicable($storageA, $storageB));
$result = array_map(fn (IUser $user) => $user->getUID(), $result);
sort($result);
sort($expected);
$this->assertEquals($expected, $result);
}
}