Code Duplication    Length = 22-27 lines in 13 locations

eZ/Publish/API/Repository/Tests/RepositoryTest.php 1 location

@@ 648-672 (lines=25) @@
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);
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);
669
        /* END: Use Case */
670
671
        $this->assertTrue($canUser);
672
    }
673
674
    /**
675
     * Test for the canUser() method.

eZ/Publish/API/Repository/Tests/PermissionResolverTest.php 3 locations

@@ 137-162 (lines=26) @@
134
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService
135
     * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessWithAnonymousUserNo
136
     */
137
    public function testHasAccessForCurrentUserNo()
138
    {
139
        $repository = $this->getRepository();
140
141
        $anonymousUserId = $this->generateId('user', 10);
142
143
        /* BEGIN: Use Case */
144
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
145
        // Publish demo installation.
146
147
        $userService = $repository->getUserService();
148
        $permissionResolver = $repository->getPermissionResolver();
149
150
        // Load anonymous user
151
        $anonymousUser = $userService->loadUser($anonymousUserId);
152
153
        // Set anonymous user as current user reference
154
        $permissionResolver->setCurrentUserReference($anonymousUser);
155
156
        // This call will return false because anonymous user does not have access
157
        // to content removal
158
        $hasAccess = $permissionResolver->hasAccess('content', 'remove');
159
        /* END: Use Case */
160
161
        $this->assertFalse($hasAccess);
162
    }
163
164
    /**
165
     * Test for the hasAccess() method.
@@ 200-223 (lines=24) @@
197
     * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testSetCurrentUserReference
198
     * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessWithAdministratorUser
199
     */
200
    public function testHasAccessForCurrentUserYes()
201
    {
202
        $repository = $this->getRepository();
203
204
        $administratorUserId = $this->generateId('user', 14);
205
206
        /* BEGIN: Use Case */
207
        // $administratorUserId contains the ID of the administrator user
208
209
        $userService = $repository->getUserService();
210
        $permissionResolver = $repository->getPermissionResolver();
211
212
        // Load administrator user
213
        $administratorUser = $userService->loadUser($administratorUserId);
214
215
        // Set administrator user as current user reference
216
        $permissionResolver->setCurrentUserReference($administratorUser);
217
218
        // This call will return true
219
        $hasAccess = $permissionResolver->hasAccess('content', 'read');
220
        /* END: Use Case */
221
222
        $this->assertTrue($hasAccess);
223
    }
224
225
    /**
226
     * Test for the hasAccess() method.
@@ 353-379 (lines=27) @@
350
     * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService
351
     * @depends eZ\Publish\API\Repository\Tests\PermissionResolverTest::testHasAccessLimited
352
     */
353
    public function testCanUserWithLimitationYes()
354
    {
355
        $repository = $this->getRepository();
356
357
        $imagesFolderId = $this->generateId('object', 49);
358
359
        /* BEGIN: Use Case */
360
        // $imagesFolderId contains the ID of the "Images" folder
361
362
        $user = $this->createUserVersion1();
363
364
        $permissionResolver = $repository->getPermissionResolver();
365
366
        // Set created user as current user reference
367
        $permissionResolver->setCurrentUserReference($user);
368
369
        $contentService = $repository->getContentService();
370
371
        // Performing an action having necessary permissions will succeed
372
        $imagesFolder = $contentService->loadContent($imagesFolderId);
373
374
        // This call will return true
375
        $canUser = $permissionResolver->canUser('content', 'read', $imagesFolder);
376
        /* END: Use Case */
377
378
        $this->assertTrue($canUser);
379
    }
380
381
    /**
382
     * Test for the canUser() method.

eZ/Publish/API/Repository/Tests/ContentServiceAuthorizationTest.php 3 locations

@@ 472-497 (lines=26) @@
469
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
470
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentByVersionInfoWithSecondParameter
471
     */
472
    public function testLoadContentByVersionInfoThrowsUnauthorizedExceptionWithSecondParameter()
473
    {
474
        $repository = $this->getRepository();
475
476
        $anonymousUserId = $this->generateId('user', 10);
477
        /* BEGIN: Use Case */
478
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
479
        // demo installation
480
481
        $contentService = $repository->getContentService();
482
483
        // Load the ContentInfo for "Anonymous User"
484
        $contentInfo = $contentService->loadContentInfo($anonymousUserId);
485
486
        // Load the current VersionInfo
487
        $versionInfo = $contentService->loadVersionInfo($contentInfo);
488
489
        $pseudoEditor = $this->createAnonymousWithEditorRole();
490
491
        // Set restricted editor user
492
        $repository->setCurrentUser($pseudoEditor);
493
494
        // This call will fail with a "UnauthorizedException"
495
        $contentService->loadContentByVersionInfo($versionInfo, ['eng-US']);
496
        /* END: Use Case */
497
    }
498
499
    /**
500
     * Test for the loadContent() method.
@@ 590-616 (lines=27) @@
587
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
588
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContent
589
     */
590
    public function testLoadContentThrowsUnauthorizedExceptionOnDrafts()
591
    {
592
        /** @var $repository \eZ\Publish\API\Repository\Repository */
593
        $repository = $this->getRepository();
594
595
        $anonymousUserId = $this->generateId('user', 10);
596
        /* BEGIN: Use Case */
597
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
598
        // demo installation
599
        $user = $this->createUserVersion1();
600
601
        // Set new editor as a content owner
602
        $repository->setCurrentUser($user);
603
604
        // Create draft with this user
605
        $draft = $this->createContentDraftVersion1(2, 'folder');
606
607
        // Load anonymous user
608
        $userService = $repository->getUserService();
609
        $user = $userService->loadUser($anonymousUserId);
610
        $repository->setCurrentUser($user);
611
612
        // Try to load the draft with anonymous user to make sure access won't be allowed by throwing an exception
613
        $contentService = $repository->getContentService();
614
        $contentService->loadContent($draft->id);
615
        /* END: Use Case */
616
    }
617
618
    /**
619
     * Test for the ContentService::loadContent() method on an archive.
@@ 934-960 (lines=27) @@
931
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
932
     * @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testLoadContentDrafts
933
     */
934
    public function testLoadContentDraftsThrowsUnauthorizedExceptionWithFirstParameter()
935
    {
936
        $repository = $this->getRepository();
937
938
        $administratorUserId = $this->generateId('user', 14);
939
        $anonymousUserId = $this->generateId('user', 10);
940
        /* BEGIN: Use Case */
941
        // $anonymousUserId is the ID of the "Anonymous User" in an eZ Publish
942
        // demo installation
943
        // $administratorUserId is  the ID of the "Administrator" user in a eZ
944
        // Publish demo installation.
945
946
        $contentService = $repository->getContentService();
947
948
        // Load the user service
949
        $userService = $repository->getUserService();
950
951
        // Load the "Administrator" user
952
        $administratorUser = $userService->loadUser($administratorUserId);
953
954
        // Set anonymous user
955
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
956
957
        // This call will fail with a "UnauthorizedException"
958
        $contentService->loadContentDrafts($administratorUser);
959
        /* END: Use Case */
960
    }
961
962
    /**
963
     * Test for the updateContent() method.

eZ/Publish/API/Repository/Tests/ContentTypeServiceAuthorizationTest.php 4 locations

@@ 309-331 (lines=23) @@
306
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
307
     * @depends eZ\Publish\API\Repository\Tests\ContentTypeServiceTest::testRemoveFieldDefinition
308
     */
309
    public function testRemoveFieldDefinitionThrowsUnauthorizedException()
310
    {
311
        $repository = $this->getRepository();
312
        $contentTypeService = $repository->getContentTypeService();
313
314
        $anonymousUserId = $this->generateId('user', 10);
315
        /* BEGIN: Use Case */
316
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
317
        // Publish demo installation.
318
        $contentTypeDraft = $this->createContentTypeDraft();
319
320
        // Load the user service
321
        $userService = $repository->getUserService();
322
323
        // Set anonymous user
324
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
325
326
        $bodyField = $contentTypeDraft->getFieldDefinition('body');
327
328
        // This call will fail with a "UnauthorizedException"
329
        $contentTypeService->removeFieldDefinition($contentTypeDraft, $bodyField);
330
        /* END: Use Case */
331
    }
332
333
    /**
334
     * Test for the updateFieldDefinition() method.
@@ 425-446 (lines=22) @@
422
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
423
     * @depends eZ\Publish\API\Repository\Tests\ContentTypeServiceTest::testCreateContentTypeDraft
424
     */
425
    public function testCreateContentTypeDraftThrowsUnauthorizedException()
426
    {
427
        $repository = $this->getRepository();
428
429
        $anonymousUserId = $this->generateId('user', 10);
430
        /* BEGIN: Use Case */
431
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
432
        // Publish demo installation.
433
        $contentTypeService = $repository->getContentTypeService();
434
435
        // Load the user service
436
        $userService = $repository->getUserService();
437
438
        // Set anonymous user
439
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
440
441
        $commentType = $contentTypeService->loadContentTypeByIdentifier('comment');
442
443
        // This call will fail with a "UnauthorizedException"
444
        $contentTypeService->createContentTypeDraft($commentType);
445
        /* END: Use Case */
446
    }
447
448
    /**
449
     * Test for the deleteContentType() method.
@@ 455-476 (lines=22) @@
452
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
453
     * @depends eZ\Publish\API\Repository\Tests\ContentTypeServiceTest::testDeleteContentType
454
     */
455
    public function testDeleteContentTypeThrowsUnauthorizedException()
456
    {
457
        $repository = $this->getRepository();
458
459
        $anonymousUserId = $this->generateId('user', 10);
460
        /* BEGIN: Use Case */
461
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
462
        // Publish demo installation.
463
        $contentTypeService = $repository->getContentTypeService();
464
465
        // Load the user service
466
        $userService = $repository->getUserService();
467
468
        // Set anonymous user
469
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
470
471
        $commentType = $contentTypeService->loadContentTypeByIdentifier('comment');
472
473
        // This call will fail with a "UnauthorizedException"
474
        $contentTypeService->deleteContentType($commentType);
475
        /* END: Use Case */
476
    }
477
478
    /**
479
     * Test for the copyContentType() method.
@@ 485-506 (lines=22) @@
482
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
483
     * @depends eZ\Publish\API\Repository\Tests\ContentTypeServiceTest::testCopyContentType
484
     */
485
    public function testCopyContentTypeThrowsUnauthorizedException()
486
    {
487
        $repository = $this->getRepository();
488
489
        $anonymousUserId = $this->generateId('user', 10);
490
        /* BEGIN: Use Case */
491
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
492
        // Publish demo installation.
493
        $contentTypeService = $repository->getContentTypeService();
494
495
        // Load the user service
496
        $userService = $repository->getUserService();
497
498
        // Set anonymous user
499
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
500
501
        $commentType = $contentTypeService->loadContentTypeByIdentifier('comment');
502
503
        // This call will fail with a "UnauthorizedException"
504
        $contentTypeService->copyContentType($commentType);
505
        /* END: Use Case */
506
    }
507
508
    /**
509
     * Test for the assignContentTypeGroup() method.

eZ/Publish/API/Repository/Tests/ObjectStateServiceAuthorizationTest.php 1 location

@@ 116-141 (lines=26) @@
113
     * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
114
     * @depends eZ\Publish\API\Repository\Tests\ObjectStateServiceTest::testDeleteObjectStateGroup
115
     */
116
    public function testDeleteObjectStateGroupThrowsUnauthorizedException()
117
    {
118
        $repository = $this->getRepository();
119
120
        $objectStateGroupId = $this->generateId('objectstategroup', 2);
121
        $anonymousUserId = $this->generateId('user', 10);
122
        /* BEGIN: Use Case */
123
        // $anonymousUserId is the ID of the "Anonymous" user in a eZ
124
        // Publish demo installation.
125
        // Set anonymous user
126
        $userService = $repository->getUserService();
127
        $repository->setCurrentUser($userService->loadUser($anonymousUserId));
128
129
        // $objectStateGroupId contains the ID of the standard object state
130
        // group ez_lock.
131
        $objectStateService = $repository->getObjectStateService();
132
133
        $loadedObjectStateGroup = $objectStateService->loadObjectStateGroup(
134
            $objectStateGroupId
135
        );
136
137
        // Throws unauthorized exception, since the anonymous user must not
138
        // delete object state groups
139
        $objectStateService->deleteObjectStateGroup($loadedObjectStateGroup);
140
        /* END: Use Case */
141
    }
142
143
    /**
144
     * Test for the createObjectState() method.

eZ/Publish/API/Repository/Tests/RoleServiceAuthorizationTest.php 1 location

@@ 580-602 (lines=23) @@
577
     * @depends eZ\Publish\API\Repository\Tests\RoleServiceTest::testGetRoleAssignmentsForUserGroup
578
     * @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser
579
     */
580
    public function testGetRoleAssignmentsForUserGroupThrowsUnauthorizedException()
581
    {
582
        $repository = $this->getRepository();
583
        $roleService = $repository->getRoleService();
584
        $userService = $repository->getUserService();
585
586
        $editorsGroupId = $this->generateId('group', 13);
587
588
        /* BEGIN: Use Case */
589
        $user = $this->createUserVersion1();
590
591
        $this->createRole();
592
593
        // Load the "Editors" user group
594
        $userGroup = $userService->loadUserGroup($editorsGroupId);
595
596
        // Set "Editor" user as current user.
597
        $repository->setCurrentUser($user);
598
599
        // This call will fail with an "UnauthorizedException"
600
        $roleService->getRoleAssignmentsForUserGroup($userGroup);
601
        /* END: Use Case */
602
    }
603
604
    /**
605
     * Create a role fixture in a variable named <b>$role</b>,.