1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oro\Bundle\SecurityBundle\Tests\Unit\Owner; |
4
|
|
|
|
5
|
|
|
use Oro\Bundle\SecurityBundle\Owner\OwnerTree; |
6
|
|
|
|
7
|
|
|
class OwnerTreeTest extends \PHPUnit_Framework_TestCase |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @dataProvider addBusinessUnitRelationProvider |
11
|
|
|
*/ |
12
|
|
|
public function testAddBusinessUnitRelation($src, $expected) |
13
|
|
|
{ |
14
|
|
|
$tree = new OwnerTree(); |
15
|
|
|
foreach ($src as $item) { |
16
|
|
|
$tree->addBusinessUnitRelation($item[0], $item[1]); |
17
|
|
|
} |
18
|
|
|
$tree->buildTree(); |
19
|
|
|
|
20
|
|
|
foreach ($expected as $buId => $sBuIds) { |
21
|
|
|
$this->assertEquals( |
22
|
|
|
$sBuIds, |
23
|
|
|
$tree->getSubordinateBusinessUnitIds($buId), |
24
|
|
|
sprintf('Failed for %s', $buId) |
25
|
|
|
); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testAddBusinessUnitShouldSetOwningOrganizationIdEvenIfItIsNull() |
30
|
|
|
{ |
31
|
|
|
$tree = new OwnerTree(); |
32
|
|
|
|
33
|
|
|
$tree->addBusinessUnit('bu1', null); |
34
|
|
|
$this->assertNull($tree->getBusinessUnitOrganizationId('bu1')); |
35
|
|
|
|
36
|
|
|
$tree->addBusinessUnit('bu2', 'org'); |
37
|
|
|
$this->assertEquals('org', $tree->getBusinessUnitOrganizationId('bu2')); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testAddBusinessUnitShouldSetOrganizationBusinessUnitIdsOnlyIfOrganizationIsNotNull() |
41
|
|
|
{ |
42
|
|
|
$tree = new OwnerTree(); |
43
|
|
|
|
44
|
|
|
$tree->addBusinessUnit('bu1', null); |
45
|
|
|
$this->assertEquals(array(), $tree->getOrganizationBusinessUnitIds('bu1')); |
46
|
|
|
|
47
|
|
|
$tree->addBusinessUnit('bu2', 'org'); |
48
|
|
|
$this->assertEquals(array('bu2'), $tree->getOrganizationBusinessUnitIds('org')); |
49
|
|
|
|
50
|
|
|
$tree->addBusinessUnit('bu3', 'org'); |
51
|
|
|
$this->assertEquals(array('bu2', 'bu3'), $tree->getOrganizationBusinessUnitIds('org')); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testAddBusinessUnitShouldSetBusinessUnitUserIds() |
55
|
|
|
{ |
56
|
|
|
$tree = new OwnerTree(); |
57
|
|
|
|
58
|
|
|
$tree->addUser('user1', 'bu'); |
59
|
|
|
$tree->addUser('user2', 'bu'); |
60
|
|
|
|
61
|
|
|
$tree->addBusinessUnit('bu', null); |
62
|
|
|
$this->assertEquals(array('user1', 'user2'), $tree->getBusinessUnitUserIds('bu')); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testAddBusinessUnitShouldSetUserOwningOrganizationId() |
66
|
|
|
{ |
67
|
|
|
$tree = new OwnerTree(); |
68
|
|
|
|
69
|
|
|
$tree->addUser('user', 'bu'); |
70
|
|
|
|
71
|
|
|
$tree->addBusinessUnit('bu', 'org'); |
72
|
|
|
$this->assertEquals('org', $tree->getUserOrganizationId('user')); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testAddBusinessUnitShouldNotSetUserOrganizationIds() |
76
|
|
|
{ |
77
|
|
|
$tree = new OwnerTree(); |
78
|
|
|
|
79
|
|
|
$tree->addUser('user', 'bu'); |
80
|
|
|
|
81
|
|
|
$tree->addBusinessUnit('bu', 'org'); |
82
|
|
|
$this->assertEquals(array(), $tree->getUserOrganizationIds('user')); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testAddBusinessUnitShouldNotSetUserOrganizationIdsIfOrganizationIdIsNull() |
86
|
|
|
{ |
87
|
|
|
$tree = new OwnerTree(); |
88
|
|
|
|
89
|
|
|
$tree->addUser('user', 'bu'); |
90
|
|
|
|
91
|
|
|
$tree->addBusinessUnit('bu', null); |
92
|
|
|
$this->assertEquals(array(), $tree->getUserOrganizationIds('user')); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testAddUserShouldSetUserOwningBusinessUnitId() |
96
|
|
|
{ |
97
|
|
|
$tree = new OwnerTree(); |
98
|
|
|
|
99
|
|
|
$tree->addUser('user', 'bu'); |
100
|
|
|
$this->assertEquals('bu', $tree->getUserBusinessUnitId('user')); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testAddUserShouldSetUserOwningBusinessUnitIdEvenIfItIsNull() |
104
|
|
|
{ |
105
|
|
|
$tree = new OwnerTree(); |
106
|
|
|
|
107
|
|
|
$tree->addUser('user', null); |
108
|
|
|
$this->assertNull($tree->getUserBusinessUnitId('user')); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testAddUserShouldSetUserBusinessUnitIds() |
112
|
|
|
{ |
113
|
|
|
$tree = new OwnerTree(); |
114
|
|
|
|
115
|
|
|
$tree->addUser('user', null); |
116
|
|
|
$this->assertEquals(array(), $tree->getUserBusinessUnitIds('user')); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testAddUserShouldSetBusinessUnitUserIds() |
120
|
|
|
{ |
121
|
|
|
$tree = new OwnerTree(); |
122
|
|
|
|
123
|
|
|
$tree->addBusinessUnit('bu', null); |
124
|
|
|
|
125
|
|
|
$tree->addUser('user', 'bu'); |
126
|
|
|
$this->assertEquals(array('user'), $tree->getBusinessUnitUserIds('bu')); |
127
|
|
|
|
128
|
|
|
$tree->addUser('user1', 'bu'); |
129
|
|
|
$this->assertEquals(array('user', 'user1'), $tree->getBusinessUnitUserIds('bu')); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function testAddUserShouldNotSetUserOrganizationIds() |
133
|
|
|
{ |
134
|
|
|
$tree = new OwnerTree(); |
135
|
|
|
|
136
|
|
|
$tree->addBusinessUnit('bu', 'org'); |
137
|
|
|
|
138
|
|
|
$tree->addUser('user', 'bu'); |
139
|
|
|
$this->assertEquals(array(), $tree->getUserOrganizationIds('user')); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function testAddUserShouldNotSetUserOrganizationIdsIfOrganizationIdIsNull() |
143
|
|
|
{ |
144
|
|
|
$tree = new OwnerTree(); |
145
|
|
|
|
146
|
|
|
$tree->addBusinessUnit('bu', null); |
147
|
|
|
|
148
|
|
|
$tree->addUser('user', 'bu'); |
149
|
|
|
$this->assertEquals(array(), $tree->getUserOrganizationIds('user')); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function testAddUserShouldSetUserOwningOrganizationId() |
153
|
|
|
{ |
154
|
|
|
$tree = new OwnerTree(); |
155
|
|
|
|
156
|
|
|
$tree->addBusinessUnit('bu', 'org'); |
157
|
|
|
|
158
|
|
|
$tree->addUser('user', 'bu'); |
159
|
|
|
$this->assertEquals('org', $tree->getUserOrganizationId('user')); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function testAddUserShouldSetUserOwningOrganizationIdEvenIfOrganizationIdIsNull() |
163
|
|
|
{ |
164
|
|
|
$tree = new OwnerTree(); |
165
|
|
|
|
166
|
|
|
$tree->addBusinessUnit('bu', null); |
167
|
|
|
|
168
|
|
|
$tree->addUser('user', 'bu'); |
169
|
|
|
$this->assertNull($tree->getUserOrganizationId('user')); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @expectedException \LogicException |
174
|
|
|
*/ |
175
|
|
|
public function testAddUserBusinessUnitShouldThrowExceptionIfUserDoesNotSet() |
176
|
|
|
{ |
177
|
|
|
$tree = new OwnerTree(); |
178
|
|
|
|
179
|
|
|
$tree->addUserBusinessUnit('user', 'org1', null); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function testAddUserBusinessUnitShouldNotSetUserBusinessUnitIdsIfBusinessUnitIdIsNull() |
183
|
|
|
{ |
184
|
|
|
$tree = new OwnerTree(); |
185
|
|
|
|
186
|
|
|
$tree->addUser('user', null); |
187
|
|
|
|
188
|
|
|
$tree->addUserBusinessUnit('user', 'org1', null); |
189
|
|
|
$this->assertEquals(array(), $tree->getUserBusinessUnitIds('user')); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function testAddUserBusinessUnitShouldSetUserBusinessUnitIds() |
193
|
|
|
{ |
194
|
|
|
$tree = new OwnerTree(); |
195
|
|
|
|
196
|
|
|
$tree->addUser('user', null); |
197
|
|
|
|
198
|
|
|
$tree->addUserBusinessUnit('user', 'org1', 'bu'); |
199
|
|
|
$this->assertEquals(array('bu'), $tree->getUserBusinessUnitIds('user')); |
200
|
|
|
|
201
|
|
|
$tree->addUserBusinessUnit('user', 'org1', 'bu1'); |
202
|
|
|
$this->assertEquals(array('bu', 'bu1'), $tree->getUserBusinessUnitIds('user')); |
203
|
|
|
$this->assertEquals(array('bu', 'bu1'), $tree->getUserBusinessUnitIds('user', 'org1')); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function testAddUserBusinessUnitShouldNotSetUserOrganizationIdsIfOrganizationIdIsNull() |
207
|
|
|
{ |
208
|
|
|
$tree = new OwnerTree(); |
209
|
|
|
|
210
|
|
|
$tree->addBusinessUnit('bu', null); |
211
|
|
|
$tree->addUser('user', null); |
212
|
|
|
|
213
|
|
|
$tree->addUserBusinessUnit('user', 'org1', 'bu'); |
214
|
|
|
$this->assertEquals(array(), $tree->getUserOrganizationIds('user')); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function testAddUserBusinessUnitShouldSetUserOrganizationIds() |
218
|
|
|
{ |
219
|
|
|
$tree = new OwnerTree(); |
220
|
|
|
|
221
|
|
|
$tree->addBusinessUnit('bu', 'org'); |
222
|
|
|
$tree->addUser('user', null); |
223
|
|
|
|
224
|
|
|
$tree->addUserOrganization('user', 'org'); |
225
|
|
|
$this->assertEquals(array('org'), $tree->getUserOrganizationIds('user')); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public function testAddUserBusinessUnitBelongToDifferentOrganizations() |
229
|
|
|
{ |
230
|
|
|
$tree = new OwnerTree(); |
231
|
|
|
|
232
|
|
|
$tree->addUser('user', null); |
233
|
|
|
|
234
|
|
|
$tree->addBusinessUnit('bu1', null); |
235
|
|
|
$this->assertNull($tree->getBusinessUnitOrganizationId('bu1')); |
236
|
|
|
$tree->addBusinessUnit('bu2', 'org2'); |
237
|
|
|
$this->assertEquals('org2', $tree->getBusinessUnitOrganizationId('bu2')); |
238
|
|
|
$tree->addBusinessUnit('bu3', 'org3'); |
239
|
|
|
$this->assertEquals('org3', $tree->getBusinessUnitOrganizationId('bu3')); |
240
|
|
|
|
241
|
|
|
$tree->addUserBusinessUnit('user', null, null); |
242
|
|
|
$this->assertEquals(array(), $tree->getUserBusinessUnitIds('user')); |
243
|
|
|
$this->assertNull($tree->getUserOrganizationId('user')); |
244
|
|
|
$this->assertEquals(array(), $tree->getUserOrganizationIds('user')); |
245
|
|
|
$this->assertEquals(array(), $tree->getUserSubordinateBusinessUnitIds('user', 'org1')); |
246
|
|
|
$this->assertEquals(array(), $tree->getBusinessUnitsIdByUserOrganizations('user')); |
247
|
|
|
|
248
|
|
|
$tree->addUserBusinessUnit('user', 'org1', 'bu1'); |
249
|
|
|
$this->assertEquals(array('bu1'), $tree->getUserBusinessUnitIds('user')); |
250
|
|
|
$this->assertNull($tree->getUserOrganizationId('user')); |
251
|
|
|
$this->assertEquals(array(), $tree->getUserOrganizationIds('user')); |
252
|
|
|
$this->assertEquals(array('bu1'), $tree->getUserSubordinateBusinessUnitIds('user', 'org1')); |
253
|
|
|
$this->assertEquals(array(), $tree->getBusinessUnitsIdByUserOrganizations('user')); |
254
|
|
|
|
255
|
|
|
$tree->addUserBusinessUnit('user', 'org2', 'bu2'); |
256
|
|
|
$tree->addUserOrganization('user', 'org2'); |
257
|
|
|
$this->assertEquals(array('bu1', 'bu2'), $tree->getUserBusinessUnitIds('user')); |
258
|
|
|
$this->assertEquals(array('bu2'), $tree->getUserBusinessUnitIds('user', 'org2')); |
259
|
|
|
$this->assertNull($tree->getUserOrganizationId('user')); |
260
|
|
|
$this->assertEquals(array('org2'), $tree->getUserOrganizationIds('user')); |
261
|
|
|
$this->assertEquals(array('bu2'), $tree->getUserSubordinateBusinessUnitIds('user', 'org2')); |
262
|
|
|
$this->assertEquals(array('bu1', 'bu2'), $tree->getUserSubordinateBusinessUnitIds('user')); |
263
|
|
|
$this->assertEquals(array('bu2'), $tree->getBusinessUnitsIdByUserOrganizations('user', 'org2')); |
|
|
|
|
264
|
|
|
|
265
|
|
|
$tree->addUserBusinessUnit('user', 'org3', 'bu3'); |
266
|
|
|
$tree->addUserOrganization('user', 'org3'); |
267
|
|
|
$this->assertEquals(array('bu1', 'bu2', 'bu3'), $tree->getUserBusinessUnitIds('user')); |
268
|
|
|
$this->assertNull($tree->getUserOrganizationId('user')); |
269
|
|
|
$this->assertEquals(array('org2', 'org3'), $tree->getUserOrganizationIds('user')); |
270
|
|
|
$this->assertEquals(array('bu1', 'bu2', 'bu3'), $tree->getUserSubordinateBusinessUnitIds('user')); |
271
|
|
|
$this->assertEquals(array('bu3'), $tree->getUserSubordinateBusinessUnitIds('user', 'org3')); |
272
|
|
|
$this->assertEquals(array('bu2', 'bu3'), $tree->getBusinessUnitsIdByUserOrganizations('user')); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
public function testAddBusinessUsersAndGetAllBusinessUnitIds() |
276
|
|
|
{ |
277
|
|
|
$tree = new OwnerTree(); |
278
|
|
|
|
279
|
|
|
$tree->addBusinessUnit('bu1', 1); |
280
|
|
|
$tree->addBusinessUnit('bu2', 2); |
281
|
|
|
$tree->addBusinessUnit('bu3', 2); |
282
|
|
|
$tree->addBusinessUnit('bu4', 3); |
283
|
|
|
|
284
|
|
|
$this->assertEquals(['bu1', 'bu2', 'bu3', 'bu4'], $tree->getAllBusinessUnitIds()); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
public static function addBusinessUnitRelationProvider() |
288
|
|
|
{ |
289
|
|
|
return array( |
290
|
|
|
'1: [null]' => array( |
291
|
|
|
array( |
292
|
|
|
array('1', null), |
293
|
|
|
), |
294
|
|
|
array( |
295
|
|
|
'1' => array(), |
296
|
|
|
) |
297
|
|
|
), |
298
|
|
|
'1: [11, 12]' => array( |
299
|
|
|
array( |
300
|
|
|
array('1', null), |
301
|
|
|
array('11', '1'), |
302
|
|
|
array('12', '1'), |
303
|
|
|
), |
304
|
|
|
array( |
305
|
|
|
'1' => array('11', '12'), |
306
|
|
|
'11' => array(), |
307
|
|
|
'12' => array(), |
308
|
|
|
) |
309
|
|
|
), |
310
|
|
|
'1: [11, 12] reverse' => array( |
311
|
|
|
array( |
312
|
|
|
array('12', '1'), |
313
|
|
|
array('11', '1'), |
314
|
|
|
array('1', null), |
315
|
|
|
), |
316
|
|
|
array( |
317
|
|
|
'1' => array('12', '11'), |
318
|
|
|
'11' => array(), |
319
|
|
|
'12' => array(), |
320
|
|
|
) |
321
|
|
|
), |
322
|
|
|
'1: [11: [111], 12]' => array( |
323
|
|
|
array( |
324
|
|
|
array('1', null), |
325
|
|
|
array('11', '1'), |
326
|
|
|
array('111', '11'), |
327
|
|
|
array('12', '1'), |
328
|
|
|
), |
329
|
|
|
array( |
330
|
|
|
'1' => array('11', '12', '111'), |
331
|
|
|
'11' => array('111'), |
332
|
|
|
'111' => array(), |
333
|
|
|
'12' => array(), |
334
|
|
|
) |
335
|
|
|
), |
336
|
|
|
'1: [11: [111: [1111, 1112]], 12: [121, 122: [1221]]]' => array( |
337
|
|
|
array( |
338
|
|
|
array('1', null), |
339
|
|
|
array('11', '1'), |
340
|
|
|
array('111', '11'), |
341
|
|
|
array('1111', '111'), |
342
|
|
|
array('1112', '111'), |
343
|
|
|
array('12', '1'), |
344
|
|
|
array('121', '12'), |
345
|
|
|
array('122', '12'), |
346
|
|
|
array('1221', '122'), |
347
|
|
|
), |
348
|
|
|
array( |
349
|
|
|
'1' => array('11', '12', '111', '121', '122', '1111', '1112', '1221'), |
350
|
|
|
'11' => array('111', '1111', '1112'), |
351
|
|
|
'111' => array('1111', '1112'), |
352
|
|
|
'1111' => array(), |
353
|
|
|
'1112' => array(), |
354
|
|
|
'12' => array('121', '122', '1221'), |
355
|
|
|
'121' => array(), |
356
|
|
|
'122' => array('1221'), |
357
|
|
|
) |
358
|
|
|
), |
359
|
|
|
); |
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.