|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright Copyright (C) Ibexa AS. All rights reserved. |
| 5 | + * @license For full copyright and license information view LICENSE file distributed with this source code. |
| 6 | + */ |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Ibexa\Tests\Integration\Core\Repository\ContentService; |
| 10 | + |
| 11 | +use Ibexa\Contracts\Core\Repository\Values\Content\Content; |
| 12 | +use Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo; |
| 13 | +use Ibexa\Contracts\Core\Repository\Values\Content\Section; |
| 14 | +use Ibexa\Contracts\Core\Repository\Values\User\Limitation\SectionLimitation; |
| 15 | +use Ibexa\Contracts\Core\Repository\Values\User\User; |
| 16 | +use Ibexa\Tests\Integration\Core\RepositoryTestCase; |
| 17 | + |
| 18 | +/** |
| 19 | + * @covers \Ibexa\Contracts\Core\Repository\ContentService |
| 20 | + */ |
| 21 | +final class UpdateContentTest extends RepositoryTestCase |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\Exception |
| 25 | + */ |
| 26 | + public function testUpdateContentHavingPrivateRelation(): void |
| 27 | + { |
| 28 | + $sectionService = self::getSectionService(); |
| 29 | + $contentService = self::getContentService(); |
| 30 | + $permissionResolver = self::getPermissionResolver(); |
| 31 | + |
| 32 | + $this->addRelationFieldToFolderContentType(); |
| 33 | + |
| 34 | + $privateSection = $this->createPrivateSection(); |
| 35 | + |
| 36 | + $folderPrivate = $this->createFolder(['eng-GB' => 'Private Folder'], 2); |
| 37 | + $sectionService->assignSection($folderPrivate->getContentInfo(), $privateSection); |
| 38 | + |
| 39 | + // Create folder with relation to 'Private Folder' |
| 40 | + $folder = $this->createFolderWithRelations([$folderPrivate->getId()]); |
| 41 | + |
| 42 | + $userWithRoleLimitation = $this->createUserWithNoAccessToPrivateSection(); |
| 43 | + |
| 44 | + // Create & publish new $folder version as $editor |
| 45 | + $permissionResolver->setCurrentUserReference($userWithRoleLimitation); |
| 46 | + $folder = $this->publishVersionWithoutChanges($folder->getContentInfo()); |
| 47 | + |
| 48 | + // Read relations & check if count($relations) is unchanged |
| 49 | + self::setAdministratorUser(); |
| 50 | + $relations = $contentService->loadRelations($folder->getVersionInfo()); |
| 51 | + if ($relations instanceof \Traversable) { |
| 52 | + $relations = iterator_to_array($relations); |
| 53 | + } |
| 54 | + self::assertCount(1, $relations); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\Exception |
| 59 | + */ |
| 60 | + private function addRelationFieldToFolderContentType(): void |
| 61 | + { |
| 62 | + $contentTypeService = self::getContentTypeService(); |
| 63 | + $folderType = $contentTypeService->loadContentTypeByIdentifier('folder'); |
| 64 | + $folderTypeDraft = $contentTypeService->createContentTypeDraft($folderType); |
| 65 | + |
| 66 | + $relationsFieldCreateStruct = $contentTypeService->newFieldDefinitionCreateStruct( |
| 67 | + 'relations', |
| 68 | + 'ezobjectrelationlist' |
| 69 | + ); |
| 70 | + $relationsFieldCreateStruct->names = ['eng-GB' => 'Relations']; |
| 71 | + $contentTypeService->addFieldDefinition($folderTypeDraft, $relationsFieldCreateStruct); |
| 72 | + $contentTypeService->publishContentTypeDraft($folderTypeDraft); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException |
| 77 | + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException |
| 78 | + */ |
| 79 | + private function createPrivateSection(): Section |
| 80 | + { |
| 81 | + $sectionService = self::getSectionService(); |
| 82 | + |
| 83 | + $sectionCreateStruct = $sectionService->newSectionCreateStruct(); |
| 84 | + $sectionCreateStruct->identifier = 'private'; |
| 85 | + $sectionCreateStruct->name = 'Private Section'; |
| 86 | + |
| 87 | + return $sectionService->createSection($sectionCreateStruct); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @param int[] $relationListTarget |
| 92 | + * |
| 93 | + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\Exception |
| 94 | + */ |
| 95 | + private function createFolderWithRelations(array $relationListTarget): Content |
| 96 | + { |
| 97 | + $contentService = self::getContentService(); |
| 98 | + |
| 99 | + $folder = $this->createFolder(['eng-GB' => 'Folder with private relation'], 2); |
| 100 | + $folderDraft = $contentService->createContentDraft($folder->getContentInfo()); |
| 101 | + $folderUpdateStruct = $contentService->newContentUpdateStruct(); |
| 102 | + $folderUpdateStruct->setField('relations', $relationListTarget); |
| 103 | + |
| 104 | + $folder = $contentService->updateContent($folderDraft->getVersionInfo(), $folderUpdateStruct); |
| 105 | + |
| 106 | + return $contentService->publishVersion($folder->getVersionInfo()); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException |
| 111 | + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException |
| 112 | + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException |
| 113 | + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\LimitationValidationException |
| 114 | + */ |
| 115 | + private function assignToUserRoleWithStandardSectionLimitation(User $user): void |
| 116 | + { |
| 117 | + $sectionService = self::getSectionService(); |
| 118 | + $roleService = self::getRoleService(); |
| 119 | + |
| 120 | + $roleCreateStruct = $roleService->newRoleCreateStruct('limited_access'); |
| 121 | + $roleCreateStruct->addPolicy($roleService->newPolicyCreateStruct('*', '*')); |
| 122 | + $role = $roleService->createRole($roleCreateStruct); |
| 123 | + $roleService->publishRoleDraft($role); |
| 124 | + |
| 125 | + // limit access to standard section only on the role assignment level |
| 126 | + $standardSection = $sectionService->loadSectionByIdentifier('standard'); |
| 127 | + $roleService->assignRoleToUser( |
| 128 | + $role, |
| 129 | + $user, |
| 130 | + new SectionLimitation(['limitationValues' => [$standardSection->id]]) |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\Exception |
| 136 | + */ |
| 137 | + private function createUserWithNoAccessToPrivateSection(): User |
| 138 | + { |
| 139 | + $user = $this->createUser('test.editor', 'Editor', 'Test'); |
| 140 | + $this->assignToUserRoleWithStandardSectionLimitation($user); |
| 141 | + |
| 142 | + return $user; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\Exception |
| 147 | + */ |
| 148 | + private function publishVersionWithoutChanges(ContentInfo $contentInfo): Content |
| 149 | + { |
| 150 | + $contentService = self::getContentService(); |
| 151 | + |
| 152 | + $folderDraft = $contentService->createContentDraft($contentInfo); |
| 153 | + $folderUpdateStruct = $contentService->newContentUpdateStruct(); |
| 154 | + $folder = $contentService->updateContent($folderDraft->getVersionInfo(), $folderUpdateStruct); |
| 155 | + |
| 156 | + return $contentService->publishVersion($folder->getVersionInfo()); |
| 157 | + } |
| 158 | +} |
0 commit comments