Completed
Push — missing_siteaccess_aware_tests ( c030ea...f3bf75 )
by
unknown
29:08 queued 08:31
created

RepositoryTest   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 1062
Duplicated Lines 30.7 %

Coupling/Cohesion

Components 1
Dependencies 13

Importance

Changes 0
Metric Value
dl 326
loc 1062
rs 8.232
c 0
b 0
f 0
wmc 44
lcom 1
cbo 13

39 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetRepository() 0 4 1
A testGetContentService() 0 8 1
A testGetContentLanguageService() 0 8 1
A testGetContentTypeService() 0 8 1
A testGetLocationService() 0 8 1
A testGetSectionService() 0 8 1
A testGetUserService() 0 8 1
A testGetNotificationService() 0 8 1
A testGetTrashService() 0 8 1
A testGetRoleService() 0 8 1
A testGetURLAliasService() 0 8 1
A testGetURLWildcardService() 0 8 1
A testGetObjectStateService() 0 8 1
A testGetFieldTypeService() 0 8 1
A testGetSearchService() 0 9 1
A testGetPermissionResolver() 0 9 1
A testCommit() 0 13 2
A testCommitThrowsRuntimeException() 0 5 1
A testRollback() 0 6 1
A testRollbackThrowsRuntimeException() 0 5 1
A testGetCurrentUserReturnsAnonymousUser() 0 22 1
A testSetCurrentUser() 34 34 1
A testHasAccessWithAnonymousUserNo() 0 20 1
A testHasAccessWithAdministratorUser() 0 20 1
A testHasAccessLimited() 21 21 1
A testCanUserForAnonymousUserNo() 0 34 2
A testCanUserWithLimitationYes() 0 25 1
A testCanUserWithLimitationNo() 0 31 2
A testCanUserThrowsInvalidArgumentException() 0 25 1
A testCanUserWithTargetYes() 49 49 1
A testCanUserWithTargetNo() 46 46 2
A testCanUserWithMultipleTargetsYes() 50 50 1
A testCanUserWithMultipleTargetsNo() 47 47 2
A testCanUserWithTargetThrowsInvalidArgumentException() 0 29 1
A testCanUserWithTargetThrowsInvalidArgumentExceptionVariant() 33 33 1
A testCanUserThrowsBadStateException() 0 6 1
A testCanUserForAdministratorUser() 0 33 1
A testHasAccessForCurrentUserNo() 23 23 1
A testHasAccessForCurrentUserYes() 23 23 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like RepositoryTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use RepositoryTest, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * File containing the RepositoryTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\API\Repository\Tests;
10
11
use Exception;
12
use eZ\Publish\API\Repository\NotificationService;
13
use eZ\Publish\API\Repository\Repository;
14
use eZ\Publish\Core\Repository\Values\User\UserReference;
15
16
/**
17
 * Test case for operations in the Repository using in memory storage.
18
 *
19
 * @see eZ\Publish\API\Repository\Repository
20
 * @group integration
21
 */
22
class RepositoryTest extends BaseTest
23
{
24
    /**
25
     * Test for the getRepository() method.
26
     */
27
    public function testGetRepository()
28
    {
29
        $this->assertInstanceOf(Repository::class, $this->getSetupFactory()->getRepository(true));
30
    }
31
32
    /**
33
     * Test for the getContentService() method.
34
     *
35
     * @group content
36
     * @group user
37
     *
38
     * @see \eZ\Publish\API\Repository\Repository::getContentService()
39
     */
40
    public function testGetContentService()
41
    {
42
        $repository = $this->getRepository();
43
        $this->assertInstanceOf(
44
            '\\eZ\\Publish\\API\\Repository\\ContentService',
45
            $repository->getContentService()
46
        );
47
    }
48
49
    /**
50
     * Test for the getContentLanguageService() method.
51
     *
52
     * @group language
53
     *
54
     * @see \eZ\Publish\API\Repository\Repository::getContentLanguageService()
55
     */
56
    public function testGetContentLanguageService()
57
    {
58
        $repository = $this->getRepository();
59
        $this->assertInstanceOf(
60
            '\\eZ\\Publish\\API\\Repository\\LanguageService',
61
            $repository->getContentLanguageService()
62
        );
63
    }
64
65
    /**
66
     * Test for the getContentTypeService() method.
67
     *
68
     * @group content-type
69
     * @group field-type
70
     * @group user
71
     *
72
     * @see \eZ\Publish\API\Repository\Repository::getContentTypeService()
73
     */
74
    public function testGetContentTypeService()
75
    {
76
        $repository = $this->getRepository();
77
        $this->assertInstanceOf(
78
            '\\eZ\\Publish\\API\\Repository\\ContentTypeService',
79
            $repository->getContentTypeService()
80
        );
81
    }
82
83
    /**
84
     * Test for the getLocationService() method.
85
     *
86
     * @group location
87
     *
88
     * @see \eZ\Publish\API\Repository\Repository::getLocationService()
89
     */
90
    public function testGetLocationService()
91
    {
92
        $repository = $this->getRepository();
93
        $this->assertInstanceOf(
94
            '\\eZ\\Publish\\API\\Repository\\LocationService',
95
            $repository->getLocationService()
96
        );
97
    }
98
99
    /**
100
     * Test for the getSectionService() method.
101
     *
102
     * @group section
103
     *
104
     * @see \eZ\Publish\API\Repository\Repository::getSectionService()
105
     */
106
    public function testGetSectionService()
107
    {
108
        $repository = $this->getRepository();
109
        $this->assertInstanceOf(
110
            '\\eZ\\Publish\\API\\Repository\\SectionService',
111
            $repository->getSectionService()
112
        );
113
    }
114
115
    /**
116
     * Test for the getUserService() method.
117
     *
118
     * @group user
119
     *
120
     * @see \eZ\Publish\API\Repository\Repository::getUserService()
121
     */
122
    public function testGetUserService()
123
    {
124
        $repository = $this->getRepository();
125
        $this->assertInstanceOf(
126
            '\\eZ\\Publish\\API\\Repository\\UserService',
127
            $repository->getUserService()
128
        );
129
    }
130
131
    /**
132
     * Test for the getNotificationService() method.
133
     *
134
     * @group user
135
     *
136
     * @see \eZ\Publish\API\Repository\Repository::getNotificationService()
137
     */
138
    public function testGetNotificationService()
139
    {
140
        $repository = $this->getRepository();
141
        $this->assertInstanceOf(
142
            NotificationService::class,
143
            $repository->getNotificationService()
144
        );
145
    }
146
147
    /**
148
     * Test for the getTrashService() method.
149
     *
150
     * @group trash
151
     *
152
     * @see \eZ\Publish\API\Repository\Repository::getTrashService()
153
     */
154
    public function testGetTrashService()
155
    {
156
        $repository = $this->getRepository();
157
        $this->assertInstanceOf(
158
            '\\eZ\\Publish\\API\\Repository\\TrashService',
159
            $repository->getTrashService()
160
        );
161
    }
162
163
    /**
164
     * Test for the getRoleService() method.
165
     *
166
     * @group role
167
     *
168
     * @see \eZ\Publish\API\Repository\Repository::getRoleService()
169
     */
170
    public function testGetRoleService()
171
    {
172
        $repository = $this->getRepository();
173
        $this->assertInstanceOf(
174
            '\\eZ\\Publish\\API\\Repository\\RoleService',
175
            $repository->getRoleService()
176
        );
177
    }
178
179
    /**
180
     * Test for the getURLAliasService() method.
181
     *
182
     * @group url-alias
183
     *
184
     * @see \eZ\Publish\API\Repository\Repository::getURLAliasService()
185
     */
186
    public function testGetURLAliasService()
187
    {
188
        $repository = $this->getRepository();
189
        $this->assertInstanceOf(
190
            '\\eZ\\Publish\\API\\Repository\\URLAliasService',
191
            $repository->getURLAliasService()
192
        );
193
    }
194
195
    /**
196
     * Test for the getUrlWildcardService() method.
197
     *
198
     * @group url-wildcard
199
     *
200
     * @see \eZ\Publish\API\Repository\Repository::getUrlWildcardService()
201
     */
202
    public function testGetURLWildcardService()
203
    {
204
        $repository = $this->getRepository();
205
        $this->assertInstanceOf(
206
            '\\eZ\\Publish\\API\\Repository\\URLWildcardService',
207
            $repository->getURLWildcardService()
208
        );
209
    }
210
211
    /**
212
     * Test for the getObjectStateService().
213
     *
214
     * @group object-state
215
     *
216
     * @see \eZ\Publish\API\Repository\Repository::getObjectStateService()
217
     */
218
    public function testGetObjectStateService()
219
    {
220
        $repository = $this->getRepository();
221
        $this->assertInstanceOf(
222
            '\\eZ\\Publish\\API\\Repository\\ObjectStateService',
223
            $repository->getObjectStateService()
224
        );
225
    }
226
227
    /**
228
     * Test for the getFieldTypeService().
229
     *
230
     * @group object-state
231
     *
232
     * @see \eZ\Publish\API\Repository\Repository::getFieldTypeService()
233
     */
234
    public function testGetFieldTypeService()
235
    {
236
        $repository = $this->getRepository();
237
        $this->assertInstanceOf(
238
            '\\eZ\\Publish\\API\\Repository\\FieldTypeService',
239
            $repository->getFieldTypeService()
240
        );
241
    }
242
243
    /**
244
     * Test for the getSearchService() method.
245
     *
246
     * @group search
247
     *
248
     * @see \eZ\Publish\API\Repository\Repository::getSearchService()
249
     */
250
    public function testGetSearchService()
251
    {
252
        $repository = $this->getRepository();
253
254
        $this->assertInstanceOf(
255
            '\\eZ\\Publish\\API\\Repository\\SearchService',
256
            $repository->getSearchService()
257
        );
258
    }
259
260
    /**
261
     * Test for the getSearchService() method.
262
     *
263
     * @group permission
264
     *
265
     * @see \eZ\Publish\API\Repository\Repository::getPermissionResolver()
266
     */
267
    public function testGetPermissionResolver()
268
    {
269
        $repository = $this->getRepository();
270
271
        $this->assertInstanceOf(
272
            '\\eZ\\Publish\\API\\Repository\\PermissionResolver',
273
            $repository->getPermissionResolver()
274
        );
275
    }
276
277
    /**
278
     * Test for the commit() method.
279
     *
280
     * @see \eZ\Publish\API\Repository\Repository::commit()
281
     */
282
    public function testCommit()
283
    {
284
        $repository = $this->getRepository();
285
286
        try {
287
            $repository->beginTransaction();
288
            $repository->commit();
289
        } catch (Exception $e) {
290
            // Cleanup hanging transaction on error
291
            $repository->rollback();
292
            throw $e;
293
        }
294
    }
295
296
    /**
297
     * Test for the commit() method.
298
     *
299
     * @see \eZ\Publish\API\Repository\Repository::commit()
300
     * @expectedException \RuntimeException
301
     */
302
    public function testCommitThrowsRuntimeException()
303
    {
304
        $repository = $this->getRepository();
305
        $repository->commit();
306
    }
307
308
    /**
309
     * Test for the rollback() method.
310
     *
311
     * @see \eZ\Publish\API\Repository\Repository::rollback()
312
     */
313
    public function testRollback()
314
    {
315
        $repository = $this->getRepository();
316
        $repository->beginTransaction();
317
        $repository->rollback();
318
    }
319
320
    /**
321
     * Test for the rollback() method.
322
     *
323
     * @see \eZ\Publish\API\Repository\Repository::rollback()
324
     * @expectedException \RuntimeException
325
     */
326
    public function testRollbackThrowsRuntimeException()
327
    {
328
        $repository = $this->getRepository();
329
        $repository->rollback();
330
    }
331
332
    /**
333
     * Test for the getCurrentUser() method.
334
     *
335
     * @group content
336
     * @group user
337
     *
338
     * @see \eZ\Publish\API\Repository\Repository::getCurrentUser()
339
     */
340
    public function testGetCurrentUserReturnsAnonymousUser()
341
    {
342
        $repository = $this->getRepository();
343
        $anonymousUserId = $this->generateId('user', 10);
344
        $repository->setCurrentUser(new UserReference($anonymousUserId));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
345
346
        /* BEGIN: Use Case */
347
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
348
        // Publish demo installation.
349
        // Only a UserReference has previously been set to the $repository
350
        $anonymousUser = $repository->getCurrentUser();
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
351
        /* END: Use Case */
352
353
        $this->assertInstanceOf(
354
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\User',
355
            $anonymousUser
356
        );
357
        $this->assertEquals(
358
            $anonymousUser->id,
359
            $repository->getUserService()->loadUser($anonymousUserId)->id
360
        );
361
    }
362
363
    /**
364
     * Test for the setCurrentUser() method.
365
     *
366
     * @group content
367
     * @group user
368
     *
369
     * @see \eZ\Publish\API\Repository\Repository::setCurrentUser()
370
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
371
     */
372 View Code Duplication
    public function testSetCurrentUser()
373
    {
374
        $repository = $this->getRepository();
375
        $repository->setCurrentUser(new UserReference($this->generateId('user', 10)));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
376
377
        $administratorUserId = $this->generateId('user', 14);
378
379
        /* BEGIN: Use Case */
380
        // $administratorUserId contains the ID of the administrator user
381
382
        $userService = $repository->getUserService();
383
384
        // Load administrator user
385
        $administratorUser = $userService->loadUser($administratorUserId);
386
387
        // Set administrator user as current user
388
        $repository->setCurrentUser($administratorUser);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
389
        /* END: Use Case */
390
391
        $this->assertEquals(
392
            $administratorUserId,
393
            $repository->getCurrentUserReference()->getUserId()
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...tCurrentUserReference() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user reference.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
394
        );
395
396
        $this->assertEquals(
397
            $administratorUser->getUserId(),
398
            $repository->getCurrentUser()->getUserId()
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
399
        );
400
401
        $this->assertSame(
402
            $administratorUser,
403
            $repository->getCurrentUser()
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
404
        );
405
    }
406
407
    /**
408
     * Test for the hasAccess() method.
409
     *
410
     * @see \eZ\Publish\API\Repository\Repository::hasAccess()
411
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
412
     */
413
    public function testHasAccessWithAnonymousUserNo()
414
    {
415
        $repository = $this->getRepository();
416
417
        $anonymousUserId = $this->generateId('user', 10);
418
        /* BEGIN: Use Case */
419
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
420
        // Publish demo installation.
421
        $userService = $repository->getUserService();
422
423
        // Load anonymous user
424
        $anonymousUser = $userService->loadUser($anonymousUserId);
425
426
        // This call will return false because anonymous user does not have access
427
        // to content removal
428
        $hasAccess = $repository->hasAccess('content', 'remove', $anonymousUser);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\Repository::hasAccess() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::hasAccess() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
429
        /* END: Use Case */
430
431
        $this->assertFalse($hasAccess);
432
    }
433
434
    /**
435
     * Test for the hasAccess() method.
436
     *
437
     * @see \eZ\Publish\API\Repository\Repository::hasAccess()
438
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
439
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessWithAnonymousUserNo
440
     */
441 View Code Duplication
    public function testHasAccessForCurrentUserNo()
442
    {
443
        $repository = $this->getRepository();
444
445
        $anonymousUserId = $this->generateId('user', 10);
446
        /* BEGIN: Use Case */
447
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
448
        // Publish demo installation.
449
        $userService = $repository->getUserService();
450
451
        // Load anonymous user
452
        $anonymousUser = $userService->loadUser($anonymousUserId);
453
454
        // Set anonymous user as current user
455
        $repository->setCurrentUser($anonymousUser);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
456
457
        // This call will return false because anonymous user does not have access
458
        // to content removal
459
        $hasAccess = $repository->hasAccess('content', 'remove');
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\Repository::hasAccess() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::hasAccess() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
460
        /* END: Use Case */
461
462
        $this->assertFalse($hasAccess);
463
    }
464
465
    /**
466
     * Test for the hasAccess() method.
467
     *
468
     * @see \eZ\Publish\API\Repository\Repository::hasAccess()
469
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
470
     */
471
    public function testHasAccessWithAdministratorUser()
472
    {
473
        $repository = $this->getRepository();
474
475
        $administratorUserId = $this->generateId('user', 14);
476
477
        /* BEGIN: Use Case */
478
        // $administratorUserId contains the ID of the administrator user
479
480
        $userService = $repository->getUserService();
481
482
        // Load administrator user
483
        $administratorUser = $userService->loadUser($administratorUserId);
484
485
        // This call will return true
486
        $hasAccess = $repository->hasAccess('content', 'read', $administratorUser);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\Repository::hasAccess() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::hasAccess() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
487
        /* END: Use Case */
488
489
        $this->assertTrue($hasAccess);
490
    }
491
492
    /**
493
     * Test for the hasAccess() method.
494
     *
495
     * @see \eZ\Publish\API\Repository\Repository::hasAccess()
496
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
497
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testSetCurrentUser
498
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessWithAdministratorUser
499
     */
500 View Code Duplication
    public function testHasAccessForCurrentUserYes()
501
    {
502
        $repository = $this->getRepository();
503
504
        $administratorUserId = $this->generateId('user', 14);
505
506
        /* BEGIN: Use Case */
507
        // $administratorUserId contains the ID of the administrator user
508
509
        $userService = $repository->getUserService();
510
511
        // Load administrator user
512
        $administratorUser = $userService->loadUser($administratorUserId);
513
514
        // Set administrator user as current user
515
        $repository->setCurrentUser($administratorUser);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
516
517
        // This call will return true
518
        $hasAccess = $repository->hasAccess('content', 'read');
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\Repository::hasAccess() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::hasAccess() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
519
        /* END: Use Case */
520
521
        $this->assertTrue($hasAccess);
522
    }
523
524
    /**
525
     * Test for the hasAccess() method.
526
     *
527
     * @see \eZ\Publish\API\Repository\Repository::hasAccess()
528
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
529
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testSetCurrentUser
530
     */
531 View Code Duplication
    public function testHasAccessLimited()
532
    {
533
        $repository = $this->getRepository();
534
535
        /* BEGIN: Use Case */
536
        $user = $this->createUserVersion1();
537
538
        // Set created user as current user
539
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
540
541
        // This call will return an array of permission sets describing user's access
542
        // to reading content
543
        $permissionSets = $repository->hasAccess('content', 'read');
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\Repository::hasAccess() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::hasAccess() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
544
        /* END: Use Case */
545
546
        $this->assertInternalType(
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit\Framework\Assert::assertInternalType() has been deprecated with message: https://github.com/sebastianbergmann/phpunit/issues/3369

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
547
            'array',
548
            $permissionSets
549
        );
550
        $this->assertNotEmpty($permissionSets);
551
    }
552
553
    /**
554
     * Test for the canUser() method.
555
     *
556
     * @see \eZ\Publish\API\Repository\Repository::canUser()
557
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
558
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService
559
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessForCurrentUserNo
560
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
561
     */
562
    public function testCanUserForAnonymousUserNo()
563
    {
564
        $repository = $this->getRepository();
565
566
        $homeId = $this->generateId('object', 57);
567
568
        $anonymousUserId = $this->generateId('user', 10);
569
        /* BEGIN: Use Case */
570
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
571
        // Publish demo installation.
572
        // $homeId contains the ID of the "Home" frontpage
573
574
        $contentService = $repository->getContentService();
575
        $userService = $repository->getUserService();
576
577
        // Load anonymous user
578
        $anonymousUser = $userService->loadUser($anonymousUserId);
579
580
        // Set anonymous user as current user
581
        $repository->setCurrentUser($anonymousUser);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
582
583
        // Load the ContentInfo for "Home" frontpage
584
        $contentInfo = $contentService->loadContentInfo($homeId);
585
586
        // This call will return false because anonymous user does not have access
587
        // to content removal and hence no permission to remove given content
588
        $canUser = $repository->canUser('content', 'remove', $contentInfo);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Indicates if the current user is allowed to perform an action given by the function on the given
objects. Example: canUser( 'content', 'edit', $content, $location ); This will check edit permission on content given the specific location, if skipped if will check on all locations. Example2: canUser( 'section', 'assign', $content, $section ); Check if user has access to assign $content to $section.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
589
590
        // Performing an action without necessary permissions will fail with "UnauthorizedException"
591
        if (!$canUser) {
592
            $contentService->deleteContent($contentInfo);
593
        }
594
        /* END: Use Case */
595
    }
596
597
    /**
598
     * Test for the canUser() method.
599
     *
600
     * @see \eZ\Publish\API\Repository\Repository::canUser()
601
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
602
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService
603
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessForCurrentUserYes
604
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
605
     */
606
    public function testCanUserForAdministratorUser()
607
    {
608
        $repository = $this->getRepository();
609
610
        $administratorUserId = $this->generateId('user', 14);
611
        $homeId = $this->generateId('object', 57);
612
613
        /* BEGIN: Use Case */
614
        // $administratorUserId contains the ID of the administrator user
615
        // $homeId contains the ID of the "Home" frontpage
616
617
        $contentService = $repository->getContentService();
618
        $userService = $repository->getUserService();
619
620
        // Load administrator user
621
        $administratorUser = $userService->loadUser($administratorUserId);
622
623
        // Set administrator user as current user
624
        $repository->setCurrentUser($administratorUser);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
625
626
        // Load the ContentInfo for "Home" frontpage
627
        $contentInfo = $contentService->loadContentInfo($homeId);
628
629
        // This call will return true
630
        $canUser = $repository->canUser('content', 'remove', $contentInfo);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Indicates if the current user is allowed to perform an action given by the function on the given
objects. Example: canUser( 'content', 'edit', $content, $location ); This will check edit permission on content given the specific location, if skipped if will check on all locations. Example2: canUser( 'section', 'assign', $content, $section ); Check if user has access to assign $content to $section.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
631
632
        // Performing an action having necessary permissions will succeed
633
        $contentService->deleteContent($contentInfo);
634
        /* END: Use Case */
635
636
        $this->assertTrue($canUser);
637
        $contentService->loadContent($homeId);
638
    }
639
640
    /**
641
     * Test for the canUser() method.
642
     *
643
     * @see \eZ\Publish\API\Repository\Repository::canUser()
644
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
645
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService
646
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited
647
     */
648
    public function testCanUserWithLimitationYes()
649
    {
650
        $repository = $this->getRepository();
651
652
        $imagesFolderId = $this->generateId('object', 49);
653
654
        /* BEGIN: Use Case */
655
        // $imagesFolderId contains the ID of the "Images" folder
656
657
        $user = $this->createUserVersion1();
658
659
        // Set created user as current user
660
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
661
662
        $contentService = $repository->getContentService();
663
664
        // Performing an action having necessary permissions will succeed
665
        $imagesFolder = $contentService->loadContent($imagesFolderId);
666
667
        // This call will return true
668
        $canUser = $repository->canUser('content', 'read', $imagesFolder);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Indicates if the current user is allowed to perform an action given by the function on the given
objects. Example: canUser( 'content', 'edit', $content, $location ); This will check edit permission on content given the specific location, if skipped if will check on all locations. Example2: canUser( 'section', 'assign', $content, $section ); Check if user has access to assign $content to $section.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
669
        /* END: Use Case */
670
671
        $this->assertTrue($canUser);
672
    }
673
674
    /**
675
     * Test for the canUser() method.
676
     *
677
     * @see \eZ\Publish\API\Repository\Repository::canUser()
678
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
679
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService
680
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited
681
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
682
     */
683
    public function testCanUserWithLimitationNo()
684
    {
685
        $repository = $this->getRepository();
686
687
        $administratorUserId = $this->generateId('user', 14);
688
689
        /* BEGIN: Use Case */
690
        // $administratorUserId contains the ID of the administrator user
691
692
        $user = $this->createUserVersion1();
693
694
        // Set created user as current user
695
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
696
697
        $userService = $repository->getUserService();
698
699
        // Load administrator user using UserService, this does not check for permissions
700
        $administratorUser = $userService->loadUser($administratorUserId);
701
702
        // This call will return false as user with Editor role does not have
703
        // permission to read "Users" subtree
704
        $canUser = $repository->canUser('content', 'read', $administratorUser);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Indicates if the current user is allowed to perform an action given by the function on the given
objects. Example: canUser( 'content', 'edit', $content, $location ); This will check edit permission on content given the specific location, if skipped if will check on all locations. Example2: canUser( 'section', 'assign', $content, $section ); Check if user has access to assign $content to $section.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
705
706
        $contentService = $repository->getContentService();
707
708
        // Performing an action without necessary permissions will fail with "UnauthorizedException"
709
        if (!$canUser) {
710
            $content = $contentService->loadContent($administratorUserId);
0 ignored issues
show
Unused Code introduced by
$content is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
711
        }
712
        /* END: Use Case */
713
    }
714
715
    /**
716
     * Test for the canUser() method.
717
     *
718
     * @see \eZ\Publish\API\Repository\Repository::canUser()
719
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
720
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentTypeService
721
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testSetCurrentUser
722
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited
723
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
724
     */
725
    public function testCanUserThrowsInvalidArgumentException()
726
    {
727
        $repository = $this->getRepository();
728
729
        $userGroupContentTypeId = $this->generateId('type', 3);
730
731
        /* BEGIN: Use Case */
732
        // $userGroupContentTypeId contains the ID of the "UserGroup" ContentType
733
734
        $user = $this->createUserVersion1();
735
736
        // Set created user as current user
737
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
738
739
        $contentTypeService = $repository->getContentTypeService();
740
741
        // Load the "UserGroup" ContentType
742
        $userGroupContentType = $contentTypeService->loadContentType($userGroupContentTypeId);
743
744
        // This call will throw "InvalidArgumentException" because $userGroupContentType
745
        // is an instance of \eZ\Publish\API\Repository\Values\ContentType\ContentType,
746
        // which can not be checked for user access
747
        $canUser = $repository->canUser('content', 'create', $userGroupContentType);
0 ignored issues
show
Unused Code introduced by
$canUser is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Deprecated Code introduced by
The method eZ\Publish\API\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Indicates if the current user is allowed to perform an action given by the function on the given
objects. Example: canUser( 'content', 'edit', $content, $location ); This will check edit permission on content given the specific location, if skipped if will check on all locations. Example2: canUser( 'section', 'assign', $content, $section ); Check if user has access to assign $content to $section.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
748
        /* END: Use Case */
749
    }
750
751
    /**
752
     * Test for the canUser() method.
753
     *
754
     * @see \eZ\Publish\API\Repository\Repository::canUser()
755
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
756
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService
757
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentTypeService
758
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited
759
     */
760 View Code Duplication
    public function testCanUserWithTargetYes()
761
    {
762
        $repository = $this->getRepository();
763
764
        $homeLocationId = $this->generateId('location', 2);
765
766
        /* BEGIN: Use Case */
767
        // $homeLocationId contains the ID of the "Home" location
768
769
        $user = $this->createUserVersion1();
770
771
        // Set created user as current user
772
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
773
774
        $contentTypeService = $repository->getContentTypeService();
775
776
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forums');
777
778
        $contentService = $repository->getContentService();
779
780
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
781
        $contentCreateStruct->setField('title', 'My awesome forums');
782
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
783
        $contentCreateStruct->alwaysAvailable = true;
784
785
        $locationService = $repository->getLocationService();
786
        $locationCreateStruct = $locationService->newLocationCreateStruct($homeLocationId);
787
788
        // This call will return true
789
        $canUser = $repository->canUser(
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Indicates if the current user is allowed to perform an action given by the function on the given
objects. Example: canUser( 'content', 'edit', $content, $location ); This will check edit permission on content given the specific location, if skipped if will check on all locations. Example2: canUser( 'section', 'assign', $content, $section ); Check if user has access to assign $content to $section.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
790
            'content',
791
            'create',
792
            $contentCreateStruct,
793
            $locationCreateStruct
794
        );
795
796
        // Performing an action having necessary permissions will succeed
797
        $contentDraft = $contentService->createContent(
798
            $contentCreateStruct,
799
            [$locationCreateStruct]
800
        );
801
        /* END: Use Case */
802
803
        $this->assertTrue($canUser);
804
        $this->assertEquals(
805
            'My awesome forums',
806
            $contentDraft->getFieldValue('title')->text
807
        );
808
    }
809
810
    /**
811
     * Test for the canUser() method.
812
     *
813
     * @see \eZ\Publish\API\Repository\Repository::canUser()
814
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
815
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService
816
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentTypeService
817
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited
818
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
819
     */
820 View Code Duplication
    public function testCanUserWithTargetNo()
821
    {
822
        $repository = $this->getRepository();
823
824
        $homeLocationId = $this->generateId('location', 2);
825
826
        /* BEGIN: Use Case */
827
        // $homeLocationId contains the ID of the "Home" frontpage location
828
829
        $user = $this->createUserVersion1();
830
831
        // Set created user as current user
832
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
833
834
        $contentTypeService = $repository->getContentTypeService();
835
836
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
837
838
        $contentService = $repository->getContentService();
839
840
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
841
        $contentCreateStruct->setField('name', 'My awesome forum');
842
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
843
        $contentCreateStruct->alwaysAvailable = true;
844
845
        $locationService = $repository->getLocationService();
846
        $locationCreateStruct = $locationService->newLocationCreateStruct($homeLocationId);
847
848
        // This call will return false because user with Editor role has permission to
849
        // create "forum" type content only under "folder" type content.
850
        $canUser = $repository->canUser(
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Indicates if the current user is allowed to perform an action given by the function on the given
objects. Example: canUser( 'content', 'edit', $content, $location ); This will check edit permission on content given the specific location, if skipped if will check on all locations. Example2: canUser( 'section', 'assign', $content, $section ); Check if user has access to assign $content to $section.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
851
            'content',
852
            'create',
853
            $contentCreateStruct,
854
            $locationCreateStruct
855
        );
856
857
        // Performing an action without necessary permissions will fail with "UnauthorizedException"
858
        if (!$canUser) {
859
            $contentDraft = $contentService->createContent(
0 ignored issues
show
Unused Code introduced by
$contentDraft is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
860
                $contentCreateStruct,
861
                [$locationCreateStruct]
862
            );
863
        }
864
        /* END: Use Case */
865
    }
866
867
    /**
868
     * Test for the canUser() method.
869
     *
870
     * @see \eZ\Publish\API\Repository\Repository::canUser()
871
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
872
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService
873
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentTypeService
874
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited
875
     */
876 View Code Duplication
    public function testCanUserWithMultipleTargetsYes()
877
    {
878
        $repository = $this->getRepository();
879
880
        $imagesLocationId = $this->generateId('location', 51);
881
        $filesLocationId = $this->generateId('location', 52);
882
883
        /* BEGIN: Use Case */
884
        // $imagesLocationId contains the ID of the "Images" location
885
        // $filesLocationId contains the ID of the "Files" location
886
887
        $user = $this->createUserVersion1();
888
889
        // Set created user as current user
890
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
891
892
        $contentTypeService = $repository->getContentTypeService();
893
894
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
895
896
        $contentService = $repository->getContentService();
897
898
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
899
        $contentCreateStruct->setField('name', 'My multipurpose folder');
900
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
901
        $contentCreateStruct->alwaysAvailable = true;
902
903
        $locationService = $repository->getLocationService();
904
        $locationCreateStruct1 = $locationService->newLocationCreateStruct($imagesLocationId);
905
        $locationCreateStruct2 = $locationService->newLocationCreateStruct($filesLocationId);
906
        $locationCreateStructs = [$locationCreateStruct1, $locationCreateStruct2];
907
908
        // This call will return true
909
        $canUser = $repository->canUser(
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Indicates if the current user is allowed to perform an action given by the function on the given
objects. Example: canUser( 'content', 'edit', $content, $location ); This will check edit permission on content given the specific location, if skipped if will check on all locations. Example2: canUser( 'section', 'assign', $content, $section ); Check if user has access to assign $content to $section.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
910
            'content',
911
            'create',
912
            $contentCreateStruct,
913
            $locationCreateStructs
914
        );
915
916
        // Performing an action having necessary permissions will succeed
917
        $contentDraft = $contentService->createContent($contentCreateStruct, $locationCreateStructs);
918
        /* END: Use Case */
919
920
        $this->assertTrue($canUser);
921
        $this->assertEquals(
922
            'My multipurpose folder',
923
            $contentDraft->getFieldValue('name')->text
924
        );
925
    }
926
927
    /**
928
     * Test for the canUser() method.
929
     *
930
     * @see \eZ\Publish\API\Repository\Repository::canUser()
931
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
932
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService
933
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentTypeService
934
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited
935
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
936
     */
937 View Code Duplication
    public function testCanUserWithMultipleTargetsNo()
938
    {
939
        $repository = $this->getRepository();
940
941
        $homeLocationId = $this->generateId('location', 2);
942
        $administratorUsersLocationId = $this->generateId('location', 13);
943
944
        /* BEGIN: Use Case */
945
        // $homeLocationId contains the ID of the "Home" location
946
        // $administratorUsersLocationId contains the ID of the "Administrator users" location
947
948
        $user = $this->createUserVersion1();
949
950
        // Set created user as current user
951
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
952
953
        $contentTypeService = $repository->getContentTypeService();
954
955
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forums');
956
957
        $contentService = $repository->getContentService();
958
959
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
960
        $contentCreateStruct->setField('name', 'My awesome forums');
961
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
962
        $contentCreateStruct->alwaysAvailable = true;
963
964
        $locationService = $repository->getLocationService();
965
        $locationCreateStruct1 = $locationService->newLocationCreateStruct($homeLocationId);
966
        $locationCreateStruct2 = $locationService->newLocationCreateStruct($administratorUsersLocationId);
967
        $locationCreateStructs = [$locationCreateStruct1, $locationCreateStruct2];
968
969
        // This call will return false because user with Editor role does not have permission to
970
        // create content in the "Administrator users" location subtree
971
        $canUser = $repository->canUser(
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Indicates if the current user is allowed to perform an action given by the function on the given
objects. Example: canUser( 'content', 'edit', $content, $location ); This will check edit permission on content given the specific location, if skipped if will check on all locations. Example2: canUser( 'section', 'assign', $content, $section ); Check if user has access to assign $content to $section.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
972
            'content',
973
            'create',
974
            $contentCreateStruct,
975
            $locationCreateStructs
976
        );
977
978
        // Performing an action without necessary permissions will fail with "UnauthorizedException"
979
        if (!$canUser) {
980
            $contentDraft = $contentService->createContent($contentCreateStruct, $locationCreateStructs);
0 ignored issues
show
Unused Code introduced by
$contentDraft is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
981
        }
982
        /* END: Use Case */
983
    }
984
985
    /**
986
     * Test for the canUser() method.
987
     *
988
     * @see \eZ\Publish\API\Repository\Repository::canUser()
989
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
990
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService
991
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testSetCurrentUser
992
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited
993
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
994
     */
995
    public function testCanUserWithTargetThrowsInvalidArgumentException()
996
    {
997
        $repository = $this->getRepository();
998
999
        $homeId = $this->generateId('object', 57);
1000
1001
        /* BEGIN: Use Case */
1002
        // $homeId contains the ID of the "Home" frontpage
1003
1004
        $user = $this->createUserVersion1();
1005
1006
        // Set created user as current user
1007
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1008
1009
        $contentService = $repository->getContentService();
1010
1011
        // Load the ContentInfo for "Home" frontpage
1012
        $contentInfo = $contentService->loadContentInfo($homeId);
1013
1014
        // This call will throw "InvalidArgumentException" because $targets argument must be an
1015
        // instance of \eZ\Publish\API\Repository\Values\ValueObject class or an array of the same
1016
        $canUser = $repository->canUser(
0 ignored issues
show
Unused Code introduced by
$canUser is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Deprecated Code introduced by
The method eZ\Publish\API\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Indicates if the current user is allowed to perform an action given by the function on the given
objects. Example: canUser( 'content', 'edit', $content, $location ); This will check edit permission on content given the specific location, if skipped if will check on all locations. Example2: canUser( 'section', 'assign', $content, $section ); Check if user has access to assign $content to $section.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1017
            'content',
1018
            'remove',
1019
            $contentInfo,
1020
            new \stdClass()
1021
        );
1022
        /* END: Use Case */
1023
    }
1024
1025
    /**
1026
     * Test for the canUser() method.
1027
     *
1028
     * @see \eZ\Publish\API\Repository\Repository::canUser()
1029
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
1030
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService
1031
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentTypeService
1032
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetURLAliasService
1033
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testSetCurrentUser
1034
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited
1035
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
1036
     */
1037 View Code Duplication
    public function testCanUserWithTargetThrowsInvalidArgumentExceptionVariant()
1038
    {
1039
        $repository = $this->getRepository();
1040
1041
        /* BEGIN: Use Case */
1042
        $user = $this->createUserVersion1();
1043
1044
        // Set created user as current user
1045
        $repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1046
1047
        $contentTypeService = $repository->getContentTypeService();
1048
1049
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
1050
1051
        $contentService = $repository->getContentService();
1052
1053
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
1054
        $contentCreateStruct->setField('name', 'My awesome forum');
1055
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
1056
        $contentCreateStruct->alwaysAvailable = true;
1057
1058
        $urlAliasService = $repository->getURLAliasService();
1059
        $rootUrlAlias = $urlAliasService->lookUp('/');
1060
1061
        // This call will throw "InvalidArgumentException" because $rootAlias is not a valid target object
1062
        $canUser = $repository->canUser(
0 ignored issues
show
Unused Code introduced by
$canUser is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Deprecated Code introduced by
The method eZ\Publish\API\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Indicates if the current user is allowed to perform an action given by the function on the given
objects. Example: canUser( 'content', 'edit', $content, $location ); This will check edit permission on content given the specific location, if skipped if will check on all locations. Example2: canUser( 'section', 'assign', $content, $section ); Check if user has access to assign $content to $section.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
1063
            'content',
1064
            'create',
1065
            $contentCreateStruct,
1066
            $rootUrlAlias
1067
        );
1068
        /* END: Use Case */
1069
    }
1070
1071
    /**
1072
     * Test for the canUser() method.
1073
     *
1074
     * @see \eZ\Publish\API\Repository\Repository::canUser()
1075
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
1076
     */
1077
    public function testCanUserThrowsBadStateException()
1078
    {
1079
        $this->markTestIncomplete(
1080
            'Cannot be tested on current fixture since policy with unsupported limitation value is not available.'
1081
        );
1082
    }
1083
}
1084