Completed
Push — ezp26445-rest_draft_http_cache ( 5bd2b6...c1f4a6 )
by
unknown
35:36
created

RepositoryTest   D

Complexity

Total Complexity 41

Size/Duplication

Total Lines 1021
Duplicated Lines 7.74 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 0
Metric Value
dl 79
loc 1021
rs 4.3267
c 0
b 0
f 0
wmc 41
lcom 1
cbo 12

36 Methods

Rating   Name   Duplication   Size   Complexity  
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 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 testCommit() 0 13 2
A testCommitThrowsRuntimeException() 0 5 1
A testRollback() 0 6 1
A testRollbackThrowsRuntimeException() 0 5 1
A testGetCurrentUserReturnsAnonymousUser() 0 22 1
B testSetCurrentUser() 0 34 1
A testHasAccessWithAnonymousUserNo() 0 20 1
A testHasAccessForCurrentUserNo() 0 23 1
A testHasAccessWithAdministratorUser() 0 20 1
A testHasAccessForCurrentUserYes() 0 23 1
A testHasAccessLimited() 0 21 1
B testCanUserForAnonymousUserNo() 0 34 2
B testCanUserWithLimitationNo() 0 31 2
A testCanUserWithTargetYes() 0 49 1
B testCanUserWithTargetNo() 0 46 2
A testCanUserWithMultipleTargetsYes() 0 50 1
A testCanUserWithMultipleTargetsNo() 0 47 2
B testCanUserWithTargetThrowsInvalidArgumentExceptionVariant() 0 33 1
A testCanUserThrowsBadStateException() 0 6 1
B testCanUserForAdministratorUser() 0 33 1
B testCanUserWithLimitationYes() 25 25 1
B testCanUserThrowsInvalidArgumentException() 25 25 1
B testCanUserWithTargetThrowsInvalidArgumentException() 29 29 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
 * @version //autogentag//
10
 */
11
namespace eZ\Publish\API\Repository\Tests;
12
13
use Exception;
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 getContentService() method.
26
     *
27
     * @group content
28
     * @group user
29
     *
30
     * @see \eZ\Publish\API\Repository\Repository::getContentService()
31
     */
32
    public function testGetContentService()
33
    {
34
        $repository = $this->getRepository();
35
        $this->assertInstanceOf(
36
            '\\eZ\\Publish\\API\\Repository\\ContentService',
37
            $repository->getContentService()
38
        );
39
    }
40
41
    /**
42
     * Test for the getContentLanguageService() method.
43
     *
44
     * @group language
45
     *
46
     * @see \eZ\Publish\API\Repository\Repository::getContentLanguageService()
47
     */
48
    public function testGetContentLanguageService()
49
    {
50
        $repository = $this->getRepository();
51
        $this->assertInstanceOf(
52
            '\\eZ\\Publish\\API\\Repository\\LanguageService',
53
            $repository->getContentLanguageService()
54
        );
55
    }
56
57
    /**
58
     * Test for the getContentTypeService() method.
59
     *
60
     * @group content-type
61
     * @group field-type
62
     * @group user
63
     *
64
     * @see \eZ\Publish\API\Repository\Repository::getContentTypeService()
65
     */
66
    public function testGetContentTypeService()
67
    {
68
        $repository = $this->getRepository();
69
        $this->assertInstanceOf(
70
            '\\eZ\\Publish\\API\\Repository\\ContentTypeService',
71
            $repository->getContentTypeService()
72
        );
73
    }
74
75
    /**
76
     * Test for the getLocationService() method.
77
     *
78
     * @group location
79
     *
80
     * @see \eZ\Publish\API\Repository\Repository::getLocationService()
81
     */
82
    public function testGetLocationService()
83
    {
84
        $repository = $this->getRepository();
85
        $this->assertInstanceOf(
86
            '\\eZ\\Publish\\API\\Repository\\LocationService',
87
            $repository->getLocationService()
88
        );
89
    }
90
91
    /**
92
     * Test for the getSectionService() method.
93
     *
94
     * @group section
95
     *
96
     * @see \eZ\Publish\API\Repository\Repository::getSectionService()
97
     */
98
    public function testGetSectionService()
99
    {
100
        $repository = $this->getRepository();
101
        $this->assertInstanceOf(
102
            '\\eZ\\Publish\\API\\Repository\\SectionService',
103
            $repository->getSectionService()
104
        );
105
    }
106
107
    /**
108
     * Test for the getUserService() method.
109
     *
110
     * @group user
111
     *
112
     * @see \eZ\Publish\API\Repository\Repository::getUserService()
113
     */
114
    public function testGetUserService()
115
    {
116
        $repository = $this->getRepository();
117
        $this->assertInstanceOf(
118
            '\\eZ\\Publish\\API\\Repository\\UserService',
119
            $repository->getUserService()
120
        );
121
    }
122
123
    /**
124
     * Test for the getTrashService() method.
125
     *
126
     * @group trash
127
     *
128
     * @see \eZ\Publish\API\Repository\Repository::getTrashService()
129
     */
130
    public function testGetTrashService()
131
    {
132
        $repository = $this->getRepository();
133
        $this->assertInstanceOf(
134
            '\\eZ\\Publish\\API\\Repository\\TrashService',
135
            $repository->getTrashService()
136
        );
137
    }
138
139
    /**
140
     * Test for the getRoleService() method.
141
     *
142
     * @group role
143
     *
144
     * @see \eZ\Publish\API\Repository\Repository::getRoleService()
145
     */
146
    public function testGetRoleService()
147
    {
148
        $repository = $this->getRepository();
149
        $this->assertInstanceOf(
150
            '\\eZ\\Publish\\API\\Repository\\RoleService',
151
            $repository->getRoleService()
152
        );
153
    }
154
155
    /**
156
     * Test for the getURLAliasService() method.
157
     *
158
     * @group url-alias
159
     *
160
     * @see \eZ\Publish\API\Repository\Repository::getURLAliasService()
161
     */
162
    public function testGetURLAliasService()
163
    {
164
        $repository = $this->getRepository();
165
        $this->assertInstanceOf(
166
            '\\eZ\\Publish\\API\\Repository\\URLAliasService',
167
            $repository->getURLAliasService()
168
        );
169
    }
170
171
    /**
172
     * Test for the getUrlWildcardService() method.
173
     *
174
     * @group url-wildcard
175
     *
176
     * @see \eZ\Publish\API\Repository\Repository::getUrlWildcardService()
177
     */
178
    public function testGetURLWildcardService()
179
    {
180
        $repository = $this->getRepository();
181
        $this->assertInstanceOf(
182
            '\\eZ\\Publish\\API\\Repository\\URLWildcardService',
183
            $repository->getURLWildcardService()
184
        );
185
    }
186
187
    /**
188
     * Test for the getObjectStateService().
189
     *
190
     * @group object-state
191
     *
192
     * @see \eZ\Publish\API\Repository\Repository::getObjectStateService()
193
     */
194
    public function testGetObjectStateService()
195
    {
196
        $repository = $this->getRepository();
197
        $this->assertInstanceOf(
198
            '\\eZ\\Publish\\API\\Repository\\ObjectStateService',
199
            $repository->getObjectStateService()
200
        );
201
    }
202
203
    /**
204
     * Test for the getFieldTypeService().
205
     *
206
     * @group object-state
207
     *
208
     * @see \eZ\Publish\API\Repository\Repository::getFieldTypeService()
209
     */
210
    public function testGetFieldTypeService()
211
    {
212
        $repository = $this->getRepository();
213
        $this->assertInstanceOf(
214
            '\\eZ\\Publish\\API\\Repository\\FieldTypeService',
215
            $repository->getFieldTypeService()
216
        );
217
    }
218
219
    /**
220
     * Test for the getSearchService() method.
221
     *
222
     * @group search
223
     *
224
     * @see \eZ\Publish\API\Repository\Repository::getSearchService()
225
     */
226
    public function testGetSearchService()
227
    {
228
        $repository = $this->getRepository();
229
230
        $this->assertInstanceOf(
231
            '\\eZ\\Publish\\API\\Repository\\SearchService',
232
            $repository->getSearchService()
233
        );
234
    }
235
236
    /**
237
     * Test for the commit() method.
238
     *
239
     * @see \eZ\Publish\API\Repository\Repository::commit()
240
     */
241
    public function testCommit()
242
    {
243
        $repository = $this->getRepository();
244
245
        try {
246
            $repository->beginTransaction();
247
            $repository->commit();
248
        } catch (Exception $e) {
249
            // Cleanup hanging transaction on error
250
            $repository->rollback();
251
            throw $e;
252
        }
253
    }
254
255
    /**
256
     * Test for the commit() method.
257
     *
258
     * @see \eZ\Publish\API\Repository\Repository::commit()
259
     * @expectedException \RuntimeException
260
     */
261
    public function testCommitThrowsRuntimeException()
262
    {
263
        $repository = $this->getRepository();
264
        $repository->commit();
265
    }
266
267
    /**
268
     * Test for the rollback() method.
269
     *
270
     * @see \eZ\Publish\API\Repository\Repository::rollback()
271
     */
272
    public function testRollback()
273
    {
274
        $repository = $this->getRepository();
275
        $repository->beginTransaction();
276
        $repository->rollback();
277
    }
278
279
    /**
280
     * Test for the rollback() method.
281
     *
282
     * @see \eZ\Publish\API\Repository\Repository::rollback()
283
     * @expectedException \RuntimeException
284
     */
285
    public function testRollbackThrowsRuntimeException()
286
    {
287
        $repository = $this->getRepository();
288
        $repository->rollback();
289
    }
290
291
    /**
292
     * Test for the getCurrentUser() method.
293
     *
294
     * @group content
295
     * @group user
296
     *
297
     * @see \eZ\Publish\API\Repository\Repository::getCurrentUser()
298
     */
299
    public function testGetCurrentUserReturnsAnonymousUser()
300
    {
301
        $repository = $this->getRepository();
302
        $anonymousUserId = $this->generateId('user', 10);
303
        $repository->setCurrentUser(new UserReference($anonymousUserId));
304
305
        /* BEGIN: Use Case */
306
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
307
        // Publish demo installation.
308
        // Only a UserReference has previously been set to the $repository
309
        $anonymousUser = $repository->getCurrentUser();
310
        /* END: Use Case */
311
312
        $this->assertInstanceOf(
313
            '\\eZ\\Publish\\API\\Repository\\Values\\User\\User',
314
            $anonymousUser
315
        );
316
        $this->assertEquals(
317
            $anonymousUser->id,
318
            $repository->getUserService()->loadUser($anonymousUserId)->id
319
        );
320
    }
321
322
    /**
323
     * Test for the setCurrentUser() method.
324
     *
325
     * @group content
326
     * @group user
327
     *
328
     * @see \eZ\Publish\API\Repository\Repository::setCurrentUser()
329
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
330
     */
331
    public function testSetCurrentUser()
332
    {
333
        $repository = $this->getRepository();
334
        $repository->setCurrentUser(new UserReference($this->generateId('user', 10)));
335
336
        $administratorUserId = $this->generateId('user', 14);
337
338
        /* BEGIN: Use Case */
339
        // $administratorUserId contains the ID of the administrator user
340
341
        $userService = $repository->getUserService();
342
343
        // Load administrator user
344
        $administratorUser = $userService->loadUser($administratorUserId);
345
346
        // Set administrator user as current user
347
        $repository->setCurrentUser($administratorUser);
348
        /* END: Use Case */
349
350
        $this->assertEquals(
351
            $administratorUserId,
352
            $repository->getCurrentUserReference()->getUserId()
353
        );
354
355
        $this->assertEquals(
356
            $administratorUser->getUserId(),
357
            $repository->getCurrentUser()->getUserId()
358
        );
359
360
        $this->assertSame(
361
            $administratorUser,
362
            $repository->getCurrentUser()
363
        );
364
    }
365
366
    /**
367
     * Test for the hasAccess() method.
368
     *
369
     * @see \eZ\Publish\API\Repository\Repository::hasAccess()
370
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
371
     */
372
    public function testHasAccessWithAnonymousUserNo()
373
    {
374
        $repository = $this->getRepository();
375
376
        $anonymousUserId = $this->generateId('user', 10);
377
        /* BEGIN: Use Case */
378
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
379
        // Publish demo installation.
380
        $userService = $repository->getUserService();
381
382
        // Load anonymous user
383
        $anonymousUser = $userService->loadUser($anonymousUserId);
384
385
        // This call will return false because anonymous user does not have access
386
        // to content removal
387
        $hasAccess = $repository->hasAccess('content', 'remove', $anonymousUser);
388
        /* END: Use Case */
389
390
        $this->assertFalse($hasAccess);
391
    }
392
393
    /**
394
     * Test for the hasAccess() method.
395
     *
396
     * @see \eZ\Publish\API\Repository\Repository::hasAccess()
397
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
398
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessWithAnonymousUserNo
399
     */
400
    public function testHasAccessForCurrentUserNo()
401
    {
402
        $repository = $this->getRepository();
403
404
        $anonymousUserId = $this->generateId('user', 10);
405
        /* BEGIN: Use Case */
406
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
407
        // Publish demo installation.
408
        $userService = $repository->getUserService();
409
410
        // Load anonymous user
411
        $anonymousUser = $userService->loadUser($anonymousUserId);
412
413
        // Set anonymous user as current user
414
        $repository->setCurrentUser($anonymousUser);
415
416
        // This call will return false because anonymous user does not have access
417
        // to content removal
418
        $hasAccess = $repository->hasAccess('content', 'remove');
419
        /* END: Use Case */
420
421
        $this->assertFalse($hasAccess);
422
    }
423
424
    /**
425
     * Test for the hasAccess() method.
426
     *
427
     * @see \eZ\Publish\API\Repository\Repository::hasAccess()
428
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
429
     */
430
    public function testHasAccessWithAdministratorUser()
431
    {
432
        $repository = $this->getRepository();
433
434
        $administratorUserId = $this->generateId('user', 14);
435
436
        /* BEGIN: Use Case */
437
        // $administratorUserId contains the ID of the administrator user
438
439
        $userService = $repository->getUserService();
440
441
        // Load administrator user
442
        $administratorUser = $userService->loadUser($administratorUserId);
443
444
        // This call will return true
445
        $hasAccess = $repository->hasAccess('content', 'read', $administratorUser);
446
        /* END: Use Case */
447
448
        $this->assertTrue($hasAccess);
449
    }
450
451
    /**
452
     * Test for the hasAccess() method.
453
     *
454
     * @see \eZ\Publish\API\Repository\Repository::hasAccess()
455
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
456
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testSetCurrentUser
457
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessWithAdministratorUser
458
     */
459
    public function testHasAccessForCurrentUserYes()
460
    {
461
        $repository = $this->getRepository();
462
463
        $administratorUserId = $this->generateId('user', 14);
464
465
        /* BEGIN: Use Case */
466
        // $administratorUserId contains the ID of the administrator user
467
468
        $userService = $repository->getUserService();
469
470
        // Load administrator user
471
        $administratorUser = $userService->loadUser($administratorUserId);
472
473
        // Set administrator user as current user
474
        $repository->setCurrentUser($administratorUser);
475
476
        // This call will return true
477
        $hasAccess = $repository->hasAccess('content', 'read');
478
        /* END: Use Case */
479
480
        $this->assertTrue($hasAccess);
481
    }
482
483
    /**
484
     * Test for the hasAccess() method.
485
     *
486
     * @see \eZ\Publish\API\Repository\Repository::hasAccess()
487
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
488
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testSetCurrentUser
489
     */
490
    public function testHasAccessLimited()
491
    {
492
        $repository = $this->getRepository();
493
494
        /* BEGIN: Use Case */
495
        $user = $this->createUserVersion1();
496
497
        // Set created user as current user
498
        $repository->setCurrentUser($user);
499
500
        // This call will return an array of permission sets describing user's access
501
        // to reading content
502
        $permissionSets = $repository->hasAccess('content', 'read');
503
        /* END: Use Case */
504
505
        $this->assertInternalType(
506
            'array',
507
            $permissionSets
508
        );
509
        $this->assertNotEmpty($permissionSets);
510
    }
511
512
    /**
513
     * Test for the canUser() method.
514
     *
515
     * @see \eZ\Publish\API\Repository\Repository::canUser()
516
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
517
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService
518
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessForCurrentUserNo
519
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
520
     */
521
    public function testCanUserForAnonymousUserNo()
522
    {
523
        $repository = $this->getRepository();
524
525
        $homeId = $this->generateId('object', 57);
526
527
        $anonymousUserId = $this->generateId('user', 10);
528
        /* BEGIN: Use Case */
529
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
530
        // Publish demo installation.
531
        // $homeId contains the ID of the "Home" frontpage
532
533
        $contentService = $repository->getContentService();
534
        $userService = $repository->getUserService();
535
536
        // Load anonymous user
537
        $anonymousUser = $userService->loadUser($anonymousUserId);
538
539
        // Set anonymous user as current user
540
        $repository->setCurrentUser($anonymousUser);
541
542
        // Load the ContentInfo for "Home" frontpage
543
        $contentInfo = $contentService->loadContentInfo($homeId);
544
545
        // This call will return false because anonymous user does not have access
546
        // to content removal and hence no permission to remove given content
547
        $canUser = $repository->canUser('content', 'remove', $contentInfo);
548
549
        // Performing an action without necessary permissions will fail with "UnauthorizedException"
550
        if (!$canUser) {
551
            $contentService->deleteContent($contentInfo);
552
        }
553
        /* END: Use Case */
554
    }
555
556
    /**
557
     * Test for the canUser() method.
558
     *
559
     * @see \eZ\Publish\API\Repository\Repository::canUser()
560
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
561
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService
562
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessForCurrentUserYes
563
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
564
     */
565
    public function testCanUserForAdministratorUser()
566
    {
567
        $repository = $this->getRepository();
568
569
        $administratorUserId = $this->generateId('user', 14);
570
        $homeId = $this->generateId('object', 57);
571
572
        /* BEGIN: Use Case */
573
        // $administratorUserId contains the ID of the administrator user
574
        // $homeId contains the ID of the "Home" frontpage
575
576
        $contentService = $repository->getContentService();
577
        $userService = $repository->getUserService();
578
579
        // Load administrator user
580
        $administratorUser = $userService->loadUser($administratorUserId);
581
582
        // Set administrator user as current user
583
        $repository->setCurrentUser($administratorUser);
584
585
        // Load the ContentInfo for "Home" frontpage
586
        $contentInfo = $contentService->loadContentInfo($homeId);
587
588
        // This call will return true
589
        $canUser = $repository->canUser('content', 'remove', $contentInfo);
590
591
        // Performing an action having necessary permissions will succeed
592
        $contentService->deleteContent($contentInfo);
593
        /* END: Use Case */
594
595
        $this->assertTrue($canUser);
596
        $contentService->loadContent($homeId);
597
    }
598
599
    /**
600
     * Test for the canUser() method.
601
     *
602
     * @see \eZ\Publish\API\Repository\Repository::canUser()
603
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
604
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService
605
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited
606
     */
607 View Code Duplication
    public function testCanUserWithLimitationYes()
608
    {
609
        $repository = $this->getRepository();
610
611
        $imagesFolderId = $this->generateId('object', 49);
612
613
        /* BEGIN: Use Case */
614
        // $imagesFolderId contains the ID of the "Images" folder
615
616
        $user = $this->createUserVersion1();
617
618
        // Set created user as current user
619
        $repository->setCurrentUser($user);
620
621
        $contentService = $repository->getContentService();
622
623
        // Performing an action having necessary permissions will succeed
624
        $imagesFolder = $contentService->loadContent($imagesFolderId);
625
626
        // This call will return true
627
        $canUser = $repository->canUser('content', 'read', $imagesFolder);
628
        /* END: Use Case */
629
630
        $this->assertTrue($canUser);
631
    }
632
633
    /**
634
     * Test for the canUser() method.
635
     *
636
     * @see \eZ\Publish\API\Repository\Repository::canUser()
637
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
638
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService
639
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited
640
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
641
     */
642
    public function testCanUserWithLimitationNo()
643
    {
644
        $repository = $this->getRepository();
645
646
        $administratorUserId = $this->generateId('user', 14);
647
648
        /* BEGIN: Use Case */
649
        // $administratorUserId contains the ID of the administrator user
650
651
        $user = $this->createUserVersion1();
652
653
        // Set created user as current user
654
        $repository->setCurrentUser($user);
655
656
        $userService = $repository->getUserService();
657
658
        // Load administrator user using UserService, this does not check for permissions
659
        $administratorUser = $userService->loadUser($administratorUserId);
660
661
        // This call will return false as user with Editor role does not have
662
        // permission to read "Users" subtree
663
        $canUser = $repository->canUser('content', 'read', $administratorUser);
664
665
        $contentService = $repository->getContentService();
666
667
        // Performing an action without necessary permissions will fail with "UnauthorizedException"
668
        if (!$canUser) {
669
            $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...
670
        }
671
        /* END: Use Case */
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::testGetContentTypeService
680
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testSetCurrentUser
681
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited
682
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
683
     */
684 View Code Duplication
    public function testCanUserThrowsInvalidArgumentException()
685
    {
686
        $repository = $this->getRepository();
687
688
        $userGroupContentTypeId = $this->generateId('type', 3);
689
690
        /* BEGIN: Use Case */
691
        // $userGroupContentTypeId contains the ID of the "UserGroup" ContentType
692
693
        $user = $this->createUserVersion1();
694
695
        // Set created user as current user
696
        $repository->setCurrentUser($user);
697
698
        $contentTypeService = $repository->getContentTypeService();
699
700
        // Load the "UserGroup" ContentType
701
        $userGroupContentType = $contentTypeService->loadContentType($userGroupContentTypeId);
702
703
        // This call will throw "InvalidArgumentException" because $userGroupContentType
704
        // is an instance of \eZ\Publish\API\Repository\Values\ContentType\ContentType,
705
        // which can not be checked for user access
706
        $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...
707
        /* END: Use Case */
708
    }
709
710
    /**
711
     * Test for the canUser() method.
712
     *
713
     * @see \eZ\Publish\API\Repository\Repository::canUser()
714
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
715
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService
716
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentTypeService
717
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited
718
     */
719
    public function testCanUserWithTargetYes()
720
    {
721
        $repository = $this->getRepository();
722
723
        $homeLocationId = $this->generateId('location', 2);
724
725
        /* BEGIN: Use Case */
726
        // $homeLocationId contains the ID of the "Home" location
727
728
        $user = $this->createUserVersion1();
729
730
        // Set created user as current user
731
        $repository->setCurrentUser($user);
732
733
        $contentTypeService = $repository->getContentTypeService();
734
735
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forums');
736
737
        $contentService = $repository->getContentService();
738
739
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
740
        $contentCreateStruct->setField('title', 'My awesome forums');
741
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
742
        $contentCreateStruct->alwaysAvailable = true;
743
744
        $locationService = $repository->getLocationService();
745
        $locationCreateStruct = $locationService->newLocationCreateStruct($homeLocationId);
746
747
        // This call will return true
748
        $canUser = $repository->canUser(
749
            'content',
750
            'create',
751
            $contentCreateStruct,
752
            $locationCreateStruct
753
        );
754
755
        // Performing an action having necessary permissions will succeed
756
        $contentDraft = $contentService->createContent(
757
            $contentCreateStruct,
758
            array($locationCreateStruct)
759
        );
760
        /* END: Use Case */
761
762
        $this->assertTrue($canUser);
763
        $this->assertEquals(
764
            'My awesome forums',
765
            $contentDraft->getFieldValue('title')->text
766
        );
767
    }
768
769
    /**
770
     * Test for the canUser() method.
771
     *
772
     * @see \eZ\Publish\API\Repository\Repository::canUser()
773
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
774
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService
775
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentTypeService
776
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited
777
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
778
     */
779
    public function testCanUserWithTargetNo()
780
    {
781
        $repository = $this->getRepository();
782
783
        $homeLocationId = $this->generateId('location', 2);
784
785
        /* BEGIN: Use Case */
786
        // $homeLocationId contains the ID of the "Home" frontpage location
787
788
        $user = $this->createUserVersion1();
789
790
        // Set created user as current user
791
        $repository->setCurrentUser($user);
792
793
        $contentTypeService = $repository->getContentTypeService();
794
795
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
796
797
        $contentService = $repository->getContentService();
798
799
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
800
        $contentCreateStruct->setField('name', 'My awesome forum');
801
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
802
        $contentCreateStruct->alwaysAvailable = true;
803
804
        $locationService = $repository->getLocationService();
805
        $locationCreateStruct = $locationService->newLocationCreateStruct($homeLocationId);
806
807
        // This call will return false because user with Editor role has permission to
808
        // create "forum" type content only under "folder" type content.
809
        $canUser = $repository->canUser(
810
            'content',
811
            'create',
812
            $contentCreateStruct,
813
            $locationCreateStruct
814
        );
815
816
        // Performing an action without necessary permissions will fail with "UnauthorizedException"
817
        if (!$canUser) {
818
            $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...
819
                $contentCreateStruct,
820
                array($locationCreateStruct)
821
            );
822
        }
823
        /* END: Use Case */
824
    }
825
826
    /**
827
     * Test for the canUser() method.
828
     *
829
     * @see \eZ\Publish\API\Repository\Repository::canUser()
830
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
831
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService
832
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentTypeService
833
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited
834
     */
835
    public function testCanUserWithMultipleTargetsYes()
836
    {
837
        $repository = $this->getRepository();
838
839
        $imagesLocationId = $this->generateId('location', 51);
840
        $filesLocationId = $this->generateId('location', 52);
841
842
        /* BEGIN: Use Case */
843
        // $imagesLocationId contains the ID of the "Images" location
844
        // $filesLocationId contains the ID of the "Files" location
845
846
        $user = $this->createUserVersion1();
847
848
        // Set created user as current user
849
        $repository->setCurrentUser($user);
850
851
        $contentTypeService = $repository->getContentTypeService();
852
853
        $contentType = $contentTypeService->loadContentTypeByIdentifier('folder');
854
855
        $contentService = $repository->getContentService();
856
857
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
858
        $contentCreateStruct->setField('name', 'My multipurpose folder');
859
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
860
        $contentCreateStruct->alwaysAvailable = true;
861
862
        $locationService = $repository->getLocationService();
863
        $locationCreateStruct1 = $locationService->newLocationCreateStruct($imagesLocationId);
864
        $locationCreateStruct2 = $locationService->newLocationCreateStruct($filesLocationId);
865
        $locationCreateStructs = array($locationCreateStruct1, $locationCreateStruct2);
866
867
        // This call will return true
868
        $canUser = $repository->canUser(
869
            'content',
870
            'create',
871
            $contentCreateStruct,
872
            $locationCreateStructs
873
        );
874
875
        // Performing an action having necessary permissions will succeed
876
        $contentDraft = $contentService->createContent($contentCreateStruct, $locationCreateStructs);
877
        /* END: Use Case */
878
879
        $this->assertTrue($canUser);
880
        $this->assertEquals(
881
            'My multipurpose folder',
882
            $contentDraft->getFieldValue('name')->text
883
        );
884
    }
885
886
    /**
887
     * Test for the canUser() method.
888
     *
889
     * @see \eZ\Publish\API\Repository\Repository::canUser()
890
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
891
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService
892
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentTypeService
893
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited
894
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
895
     */
896
    public function testCanUserWithMultipleTargetsNo()
897
    {
898
        $repository = $this->getRepository();
899
900
        $homeLocationId = $this->generateId('location', 2);
901
        $administratorUsersLocationId = $this->generateId('location', 13);
902
903
        /* BEGIN: Use Case */
904
        // $homeLocationId contains the ID of the "Home" location
905
        // $administratorUsersLocationId contains the ID of the "Administrator users" location
906
907
        $user = $this->createUserVersion1();
908
909
        // Set created user as current user
910
        $repository->setCurrentUser($user);
911
912
        $contentTypeService = $repository->getContentTypeService();
913
914
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forums');
915
916
        $contentService = $repository->getContentService();
917
918
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
919
        $contentCreateStruct->setField('name', 'My awesome forums');
920
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
921
        $contentCreateStruct->alwaysAvailable = true;
922
923
        $locationService = $repository->getLocationService();
924
        $locationCreateStruct1 = $locationService->newLocationCreateStruct($homeLocationId);
925
        $locationCreateStruct2 = $locationService->newLocationCreateStruct($administratorUsersLocationId);
926
        $locationCreateStructs = array($locationCreateStruct1, $locationCreateStruct2);
927
928
        // This call will return false because user with Editor role does not have permission to
929
        // create content in the "Administrator users" location subtree
930
        $canUser = $repository->canUser(
931
            'content',
932
            'create',
933
            $contentCreateStruct,
934
            $locationCreateStructs
935
        );
936
937
        // Performing an action without necessary permissions will fail with "UnauthorizedException"
938
        if (!$canUser) {
939
            $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...
940
        }
941
        /* END: Use Case */
942
    }
943
944
    /**
945
     * Test for the canUser() method.
946
     *
947
     * @see \eZ\Publish\API\Repository\Repository::canUser()
948
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
949
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService
950
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testSetCurrentUser
951
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited
952
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
953
     */
954 View Code Duplication
    public function testCanUserWithTargetThrowsInvalidArgumentException()
955
    {
956
        $repository = $this->getRepository();
957
958
        $homeId = $this->generateId('object', 57);
959
960
        /* BEGIN: Use Case */
961
        // $homeId contains the ID of the "Home" frontpage
962
963
        $user = $this->createUserVersion1();
964
965
        // Set created user as current user
966
        $repository->setCurrentUser($user);
967
968
        $contentService = $repository->getContentService();
969
970
        // Load the ContentInfo for "Home" frontpage
971
        $contentInfo = $contentService->loadContentInfo($homeId);
972
973
        // This call will throw "InvalidArgumentException" because $targets argument must be an
974
        // instance of \eZ\Publish\API\Repository\Values\ValueObject class or an array of the same
975
        $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...
976
            'content',
977
            'remove',
978
            $contentInfo,
979
            new \stdClass()
980
        );
981
        /* END: Use Case */
982
    }
983
984
    /**
985
     * Test for the canUser() method.
986
     *
987
     * @see \eZ\Publish\API\Repository\Repository::canUser()
988
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
989
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService
990
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentTypeService
991
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetURLAliasService
992
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testSetCurrentUser
993
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited
994
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
995
     */
996
    public function testCanUserWithTargetThrowsInvalidArgumentExceptionVariant()
997
    {
998
        $repository = $this->getRepository();
999
1000
        /* BEGIN: Use Case */
1001
        $user = $this->createUserVersion1();
1002
1003
        // Set created user as current user
1004
        $repository->setCurrentUser($user);
1005
1006
        $contentTypeService = $repository->getContentTypeService();
1007
1008
        $contentType = $contentTypeService->loadContentTypeByIdentifier('forum');
1009
1010
        $contentService = $repository->getContentService();
1011
1012
        $contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-US');
1013
        $contentCreateStruct->setField('name', 'My awesome forum');
1014
        $contentCreateStruct->remoteId = 'abcdef0123456789abcdef0123456789';
1015
        $contentCreateStruct->alwaysAvailable = true;
1016
1017
        $urlAliasService = $repository->getURLAliasService();
1018
        $rootUrlAlias = $urlAliasService->lookUp('/');
1019
1020
        // This call will throw "InvalidArgumentException" because $rootAlias is not a valid target object
1021
        $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...
1022
            'content',
1023
            'create',
1024
            $contentCreateStruct,
1025
            $rootUrlAlias
1026
        );
1027
        /* END: Use Case */
1028
    }
1029
1030
    /**
1031
     * Test for the canUser() method.
1032
     *
1033
     * @see \eZ\Publish\API\Repository\Repository::canUser()
1034
     * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException
1035
     */
1036
    public function testCanUserThrowsBadStateException()
1037
    {
1038
        $this->markTestIncomplete(
1039
            'Cannot be tested on current fixture since policy with unsupported limitation value is not available.'
1040
        );
1041
    }
1042
}
1043