1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elgg\Roles; |
4
|
|
|
|
5
|
|
|
use ElggUser; |
6
|
|
|
use PHPUnit_Framework_TestCase; |
7
|
|
|
|
8
|
|
|
class ApiTest extends PHPUnit_Framework_TestCase { |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var Api |
12
|
|
|
*/ |
13
|
|
|
protected $api; |
14
|
|
|
|
15
|
|
|
public function setUp() { |
16
|
|
|
$this->api = new Api(new DbMock()); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function testGetReservedRoleName() { |
20
|
|
|
$expected = array(Api::DEFAULT_ROLE, Api::ADMIN_ROLE, Api::VISITOR_ROLE); |
21
|
|
|
$this->assertEquals($expected, $this->api->getReservedRoleNames()); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function testIsReservedName() { |
25
|
|
|
$this->assertTrue($this->api->isReservedRoleName(Api::DEFAULT_ROLE)); |
26
|
|
|
$this->assertFalse($this->api->isReservedRoleName('foobar')); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testGetPermissions() { |
30
|
|
|
$expected = [ |
31
|
|
|
'bar/foo' => ['rule' => 'allow'], |
32
|
|
|
'baz' => array('rule' => 'deny', 'redirect' => 'bar/foo'), |
33
|
|
|
]; |
34
|
|
|
$this->assertEquals($expected, $this->api->getPermissions($this->api->getRoleByName('default'), 'actions')); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testGetPermissionsWithExtensions() { |
38
|
|
|
$expected = [ |
39
|
|
|
'baz' => array('rule' => 'deny', 'redirect' => 'bar/foo'), |
40
|
|
|
'foo/bar' => ['rule' => 'allow'], |
41
|
|
|
'foo/bar/baz' => ['rule' => 'deny'], |
42
|
|
|
'bar/foo' => ['rule' => 'deny'] |
43
|
|
|
]; |
44
|
|
|
$this->assertEquals($expected, $this->api->getPermissions($this->api->getRoleByName('tester1'), 'actions')); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testGetRoleByName() { |
48
|
|
|
|
49
|
|
|
$role = $this->api->getRoleByName('default'); |
50
|
|
|
$this->assertInstanceOf('\ElggRole', $role); |
51
|
|
|
$this->assertEquals('default', $role->name); |
52
|
|
|
|
53
|
|
|
$this->assertFalse($this->api->getRoleByName('foo')); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testGetSelectable() { |
57
|
|
|
|
58
|
|
|
$selectable = $this->api->getSelectable(); |
59
|
|
|
$this->assertContains($this->api->getRoleByName('tester1'), $selectable); |
60
|
|
|
$this->assertContains($this->api->getRoleByName('tester2'), $selectable); |
61
|
|
|
$this->assertNotContains($this->api->getRoleByName('default'), $selectable); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testFilterName() { |
65
|
|
|
|
66
|
|
|
$this->assertEquals(Api::VISITOR_ROLE, $this->api->filterName(Api::NO_ROLE)); |
67
|
|
|
$this->assertEquals(Api::DEFAULT_ROLE, $this->api->filterName(Api::DEFAULT_ROLE)); |
68
|
|
|
$this->assertEquals('tester1', $this->api->filterName('tester1')); |
69
|
|
|
|
70
|
|
|
$user = new ElggUser(); |
71
|
|
|
$this->assertEquals(Api::DEFAULT_ROLE, $this->api->filterName(Api::NO_ROLE, $user)); |
72
|
|
|
|
73
|
|
|
$admin = new ElggUser(); |
74
|
|
|
$admin->admin = true; |
75
|
|
|
$this->assertEquals(Api::ADMIN_ROLE, $this->api->filterName(Api::NO_ROLE, $admin)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testSetGetRole() { |
79
|
|
|
|
80
|
|
|
$user = new ElggUser(); |
81
|
|
|
|
82
|
|
|
$role = $this->api->getRoleByName('tester1'); |
83
|
|
|
$this->api->setRole($user, $role); |
|
|
|
|
84
|
|
|
$this->assertEquals($role, $this->api->getRole($user)); |
85
|
|
|
|
86
|
|
|
$role = $this->api->getRoleByName(Api::DEFAULT_ROLE); |
87
|
|
|
$this->api->setRole($user, $role); |
|
|
|
|
88
|
|
|
$this->assertEquals($role, $this->api->getRole($user)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testContext() { |
92
|
|
|
|
93
|
|
|
elgg_push_context('foo'); |
94
|
|
|
elgg_push_context('bar'); |
95
|
|
|
|
96
|
|
|
$this->assertFalse($this->api->checkContext(['context' => 'baz'])); |
|
|
|
|
97
|
|
|
$this->assertTrue($this->api->checkContext(['context' => 'foo'])); |
|
|
|
|
98
|
|
|
$this->assertTrue($this->api->checkContext(['context' => 'bar'])); |
|
|
|
|
99
|
|
|
$this->assertTrue($this->api->checkContext(['context' => ['foo', 'bar']])); |
|
|
|
|
100
|
|
|
$this->assertTrue($this->api->checkContext(['context' => ['bar']])); |
|
|
|
|
101
|
|
|
|
102
|
|
|
elgg_pop_context(); |
103
|
|
|
elgg_pop_context(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testContextStrict() { |
107
|
|
|
|
108
|
|
|
elgg_push_context('foo'); |
109
|
|
|
elgg_push_context('bar'); |
110
|
|
|
|
111
|
|
|
$this->assertTrue($this->api->checkContext(['context' => 'bar'], true)); |
|
|
|
|
112
|
|
|
$this->assertFalse($this->api->checkContext(['context' => 'baz'], true)); |
|
|
|
|
113
|
|
|
$this->assertFalse($this->api->checkContext(['context' => 'foo'], true)); |
|
|
|
|
114
|
|
|
$this->assertTrue($this->api->checkContext(['context' => ['foo', 'bar']], true)); |
|
|
|
|
115
|
|
|
$this->assertTrue($this->api->checkContext(['context' => ['bar']], true)); |
|
|
|
|
116
|
|
|
|
117
|
|
|
elgg_pop_context(); |
118
|
|
|
elgg_pop_context(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function testDenyView() { |
122
|
|
|
$role = $this->api->getRoleByName('deny'); |
123
|
|
|
$this->api->setupViews($role); |
|
|
|
|
124
|
|
|
$this->assertEquals('', elgg_view('foo/bar')); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function testAllowView() { |
128
|
|
|
$role = $this->api->getRoleByName('allow'); |
129
|
|
|
$this->api->setupViews($role); |
|
|
|
|
130
|
|
|
$this->assertEquals('bar', elgg_view('foo/bar')); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function testExtendView() { |
134
|
|
|
$role = $this->api->getRoleByName('extend'); |
135
|
|
|
$this->api->setupViews($role); |
|
|
|
|
136
|
|
|
$this->assertEquals('bazbar', elgg_view('foo/bar')); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function testReplaceView() { |
140
|
|
|
$role = $this->api->getRoleByName('replace'); |
141
|
|
|
$this->api->setupViews($role); |
|
|
|
|
142
|
|
|
$this->assertEquals('baz2', elgg_view('foo/baz')); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function testDenyAction() { |
146
|
|
|
$role = $this->api->getRoleByName('deny'); |
147
|
|
|
$this->assertFalse($this->api->actionGatekeeper($role, 'foo/bar')); |
|
|
|
|
148
|
|
|
$this->assertNull($this->api->actionGatekeeper($role, 'foo/baz')); |
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function testAllowAction() { |
152
|
|
|
$role = $this->api->getRoleByName('allow'); |
153
|
|
|
$this->assertNull($this->api->actionGatekeeper($role, 'foo/bar')); |
|
|
|
|
154
|
|
|
$this->assertNull($this->api->actionGatekeeper($role, 'foo/baz')); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function testDenyPage() { |
158
|
|
|
$role = $this->api->getRoleByName('deny'); |
159
|
|
|
$this->assertEquals(['forward' => REFERRER, 'error' => true], $this->api->pageGatekeeper($role, 'foo/bar')); |
|
|
|
|
160
|
|
|
$this->assertEquals(['forward' => false, 'error' => false], $this->api->pageGatekeeper($role, 'foo/baz')); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function testAllowPage() { |
164
|
|
|
$role = $this->api->getRoleByName('allow'); |
165
|
|
|
$this->assertEquals(['forward' => false, 'error' => false], $this->api->pageGatekeeper($role, 'foo/bar')); |
|
|
|
|
166
|
|
|
$this->assertEquals(['forward' => false, 'error' => false], $this->api->pageGatekeeper($role, 'foo/baz')); |
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function testRedirectPage() { |
170
|
|
|
$role = $this->api->getRoleByName('replace'); |
171
|
|
|
$this->assertEquals(['forward' => 'foo/baz', 'error' => false], $this->api->pageGatekeeper($role, 'foo/bar')); |
|
|
|
|
172
|
|
|
$this->assertEquals(['forward' => false, 'error' => false], $this->api->pageGatekeeper($role, 'foo/baz')); |
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
175
|
|
View Code Duplication |
public function testDenyHook() { |
|
|
|
|
176
|
|
|
|
177
|
|
|
elgg_register_plugin_hook_handler('foo', 'bar', '\Elgg\Values::getTrue'); |
178
|
|
|
$role = $this->api->getRoleByName('deny'); |
179
|
|
|
$this->api->setupHooks($role); |
|
|
|
|
180
|
|
|
|
181
|
|
|
$expected = false; |
182
|
|
|
$actual = elgg_trigger_plugin_hook('foo', 'bar', null, $expected); |
183
|
|
|
$this->assertEquals($expected, $actual); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
View Code Duplication |
public function testAllowHook() { |
|
|
|
|
187
|
|
|
|
188
|
|
|
elgg_register_plugin_hook_handler('foo', 'bar', '\Elgg\Values::getTrue'); |
189
|
|
|
$role = $this->api->getRoleByName('allow'); |
190
|
|
|
$this->api->setupHooks($role); |
|
|
|
|
191
|
|
|
|
192
|
|
|
$expected = true; |
193
|
|
|
$actual = elgg_trigger_plugin_hook('foo', 'bar', null, null); |
194
|
|
|
$this->assertEquals($expected, $actual); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
View Code Duplication |
public function testExtendHook() { |
|
|
|
|
198
|
|
|
|
199
|
|
|
elgg_register_plugin_hook_handler('foo', 'bar', '\Elgg\Values::getTrue'); |
200
|
|
|
$role = $this->api->getRoleByName('extend'); |
201
|
|
|
$this->api->setupHooks($role); |
|
|
|
|
202
|
|
|
|
203
|
|
|
$expected = false; |
204
|
|
|
$actual = elgg_trigger_plugin_hook('foo', 'bar', null, null); |
205
|
|
|
$this->assertEquals($expected, $actual); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
View Code Duplication |
public function testReplaceHook() { |
|
|
|
|
209
|
|
|
|
210
|
|
|
elgg_register_plugin_hook_handler('foo', 'bar', '\Elgg\Values::getTrue'); |
211
|
|
|
$role = $this->api->getRoleByName('replace'); |
212
|
|
|
$this->api->setupHooks($role); |
|
|
|
|
213
|
|
|
|
214
|
|
|
$expected = false; |
215
|
|
|
$actual = elgg_trigger_plugin_hook('foo', 'bar', null, null); |
216
|
|
|
$this->assertEquals($expected, $actual); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
View Code Duplication |
public function testDenyEvent() { |
|
|
|
|
220
|
|
|
|
221
|
|
|
elgg_register_event_handler('foo', 'bar', '\Elgg\Values::getFalse'); |
222
|
|
|
$role = $this->api->getRoleByName('deny'); |
223
|
|
|
$this->api->setupEvents($role); |
|
|
|
|
224
|
|
|
|
225
|
|
|
$expected = true; |
226
|
|
|
$actual = elgg_trigger_event('foo', 'bar'); |
227
|
|
|
$this->assertEquals($expected, $actual); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
View Code Duplication |
public function testAllowEvent() { |
|
|
|
|
231
|
|
|
|
232
|
|
|
elgg_register_event_handler('foo', 'bar', '\Elgg\Values::getFalse'); |
233
|
|
|
$role = $this->api->getRoleByName('allow'); |
234
|
|
|
$this->api->setupEvents($role); |
|
|
|
|
235
|
|
|
|
236
|
|
|
$expected = false; |
237
|
|
|
$actual = elgg_trigger_event('foo', 'bar'); |
238
|
|
|
$this->assertEquals($expected, $actual); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
View Code Duplication |
public function testExtendEvent() { |
|
|
|
|
242
|
|
|
|
243
|
|
|
elgg_register_event_handler('foo', 'bar', '\Elgg\Values::getTrue'); |
244
|
|
|
$role = $this->api->getRoleByName('extend'); |
245
|
|
|
$this->api->setupEvents($role); |
|
|
|
|
246
|
|
|
|
247
|
|
|
$expected = false; |
248
|
|
|
$actual = elgg_trigger_event('foo', 'bar'); |
249
|
|
|
$this->assertEquals($expected, $actual); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
View Code Duplication |
public function testReplaceEvent() { |
|
|
|
|
253
|
|
|
|
254
|
|
|
elgg_register_event_handler('foo', 'bar', '\Elgg\Values::getTrue'); |
255
|
|
|
$role = $this->api->getRoleByName('replace'); |
256
|
|
|
$this->api->setupEvents($role); |
|
|
|
|
257
|
|
|
|
258
|
|
|
$expected = false; |
259
|
|
|
$actual = elgg_trigger_event('foo', 'bar'); |
260
|
|
|
$this->assertEquals($expected, $actual); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
public function testDenyMenu() { |
264
|
|
|
$role = $this->api->getRoleByName('deny'); |
265
|
|
|
|
266
|
|
|
$menu = $this->getMenu(); |
267
|
|
|
$this->assertEmpty($this->api->setupMenu($role, 'foo', $menu)); |
|
|
|
|
268
|
|
|
|
269
|
|
|
$filtered_menu = $this->api->setupMenu($role, 'bar', $menu); |
|
|
|
|
270
|
|
|
$this->assertNotContains($menu['parent'], $filtered_menu); |
271
|
|
|
$this->assertNotContains($menu['child'], $filtered_menu); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
public function testAllowMenu() { |
275
|
|
|
$role = $this->api->getRoleByName('allow'); |
276
|
|
|
$menu = $this->getMenu(); |
277
|
|
|
$this->assertEquals($menu, $this->api->setupMenu($role, 'foo', $menu)); |
|
|
|
|
278
|
|
|
$this->assertEquals($menu, $this->api->setupMenu($role, 'bar', $menu)); |
|
|
|
|
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
public function testExtendMenu() { |
282
|
|
|
$role = $this->api->getRoleByName('extend'); |
283
|
|
|
$menu = $this->getMenu(); |
284
|
|
|
$this->assertEquals(count($menu) + 1, count($this->api->setupMenu($role, 'foo', $menu))); |
|
|
|
|
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
public function testReplaceMenu() { |
288
|
|
|
$role = $this->api->getRoleByName('replace'); |
289
|
|
|
|
290
|
|
|
$menu = $this->getMenu(); |
291
|
|
|
|
292
|
|
|
$filtered_menu = $this->api->setupMenu($role, 'bar', $menu); |
|
|
|
|
293
|
|
|
$this->assertEquals('baz2', $filtered_menu['parent']->getName()); |
294
|
|
|
$this->assertEquals('baz2', $filtered_menu['child']->getParentName()); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
public function getMenu() { |
298
|
|
|
|
299
|
|
|
$menu = array(); |
300
|
|
|
$menu['parent'] = \ElggMenuItem::factory(array( |
301
|
|
|
'name' => 'baz', |
302
|
|
|
'href' => 'baz', |
303
|
|
|
'text' => 'baz', |
304
|
|
|
)); |
305
|
|
|
$menu['bad'] = 'bad'; |
306
|
|
|
$menu['child'] = \ElggMenuItem::factory(array( |
307
|
|
|
'name' => 'baz:child', |
308
|
|
|
'parent_name' => 'baz', |
309
|
|
|
'href' => 'baz/child', |
310
|
|
|
'text' => 'baz:child' |
311
|
|
|
)); |
312
|
|
|
return $menu; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
public function testCleanMenu() { |
316
|
|
|
|
317
|
|
|
$role = $this->api->getRoleByName('tester1'); |
318
|
|
|
$item = \ElggMenuItem::factory(array( |
319
|
|
|
'name' => 'foo', |
320
|
|
|
'href' => 'action/bar/foo', |
321
|
|
|
'text' => 'foo', |
322
|
|
|
)); |
323
|
|
|
|
324
|
|
|
$this->assertNotContains($item, $this->api->cleanMenu($role, [$item])); |
|
|
|
|
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* @dataProvider providerMatchPath |
329
|
|
|
*/ |
330
|
|
|
public function testMatchPath($expectation, $a, $b) { |
331
|
|
|
if ($expectation === true) { |
332
|
|
|
$this->assertTrue($this->api->matchPath($a, $b)); |
333
|
|
|
} else if ($expectation === false) { |
334
|
|
|
$this->assertFalse($this->api->matchPath($a, $b)); |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
public function providerMatchPath() { |
339
|
|
|
|
340
|
|
|
return array( |
341
|
|
|
array( |
342
|
|
|
true, |
343
|
|
|
'blog/edit', |
344
|
|
|
'/blog/edit', |
345
|
|
|
), |
346
|
|
|
array( |
347
|
|
|
true, |
348
|
|
|
'blog/save', |
349
|
|
|
elgg_normalize_url('blog/save'), |
350
|
|
|
), |
351
|
|
|
array( |
352
|
|
|
false, |
353
|
|
|
'blog/view/123', |
354
|
|
|
'/groups/blog/view/123', |
355
|
|
|
), |
356
|
|
|
array( |
357
|
|
|
true, |
358
|
|
|
'/blog/add/234', |
359
|
|
|
'blog/add/234', |
360
|
|
|
), |
361
|
|
|
array( |
362
|
|
|
false, |
363
|
|
|
'/blog/new/345', |
364
|
|
|
'/blog/new/', |
365
|
|
|
), |
366
|
|
|
array( |
367
|
|
|
true, |
368
|
|
|
'regexp(/^admin\/((?!administer_utilities\/reportedcontent).)*$/)', |
369
|
|
|
'admin/plugins', |
370
|
|
|
), |
371
|
|
|
array( |
372
|
|
|
false, |
373
|
|
|
'regexp(/^admin\/((?!administer_utilities\/reportedcontent).)*$/)', |
374
|
|
|
'/admin/reportedcontent/123', |
375
|
|
|
), |
376
|
|
|
array( |
377
|
|
|
true, |
378
|
|
|
'/blog/owner/\d+/.*', |
379
|
|
|
'/blog/owner/25/some-title', |
380
|
|
|
), |
381
|
|
|
array( |
382
|
|
|
true, |
383
|
|
|
'/blog/(view|edit)/\d+/.*', |
384
|
|
|
'blog/edit/15/some-title', |
385
|
|
|
), |
386
|
|
|
); |
387
|
|
|
} |
388
|
|
|
} |
389
|
|
|
|