|
| 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\Limitation; |
| 10 | + |
| 11 | +use Ibexa\Tests\Integration\Core\Repository\Limitation\PermissionResolver\BaseLimitationIntegrationTest; |
| 12 | +use Ibexa\Contracts\Core\Repository\Values\Content\LocationQuery; |
| 13 | +use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion; |
| 14 | +use Ibexa\Contracts\Core\Repository\Values\Content\Search\SearchHit; |
| 15 | +use Ibexa\Contracts\Core\Repository\Values\User\Limitation\ContentTypeLimitation; |
| 16 | +use Ibexa\Contracts\Core\Repository\Values\User\Limitation\LocationLimitation; |
| 17 | +use Ibexa\Contracts\Core\Repository\Values\User\Limitation\UserGroupLimitation; |
| 18 | + |
| 19 | +final class UserGroupLimitationTest extends BaseLimitationIntegrationTest |
| 20 | +{ |
| 21 | + private const FOLDER_CONTENT_TYPE_ID = 1; |
| 22 | + |
| 23 | + public function testHasUserWithUserGroupLimitationAccessToCreatedLocations(): void |
| 24 | + { |
| 25 | + $repository = $this->getRepository(); |
| 26 | + |
| 27 | + $user = $this->createUserWithPolicies('test_user', $this->getPermissions()); |
| 28 | + $userGroups = $repository->getUserService()->loadUserGroupsOfUser($user); |
| 29 | + $userGroupIds = array_column($userGroups, 'id'); |
| 30 | + |
| 31 | + $repository->getPermissionResolver()->setCurrentUserReference($user); |
| 32 | + |
| 33 | + $parentFolder = $this->createFolder( |
| 34 | + ['eng-US' => 'Parent folder'], |
| 35 | + 2 |
| 36 | + ); |
| 37 | + $childFolder = $this->createFolder( |
| 38 | + ['eng-US' => 'Child folder'], |
| 39 | + $parentFolder->contentInfo->getMainLocationId() |
| 40 | + ); |
| 41 | + |
| 42 | + $this->refreshSearch($repository); |
| 43 | + |
| 44 | + $query = new LocationQuery(); |
| 45 | + $query->filter = new Criterion\LogicalAnd([ |
| 46 | + new Criterion\ContentTypeId(self::FOLDER_CONTENT_TYPE_ID), |
| 47 | + new Criterion\UserMetadata('group', 'in', $userGroupIds), |
| 48 | + ]); |
| 49 | + |
| 50 | + $results = $repository->getSearchService()->findLocations($query)->searchHits; |
| 51 | + $resultLocationIds = array_map(static function (SearchHit $hit): int { |
| 52 | + /** @var \Ibexa\Contracts\Core\Repository\Values\Content\Location $location */ |
| 53 | + $location = $hit->valueObject; |
| 54 | + |
| 55 | + return $location->id; |
| 56 | + }, $results); |
| 57 | + |
| 58 | + self::assertContains($parentFolder->contentInfo->getMainLocationId(), $resultLocationIds); |
| 59 | + self::assertContains($childFolder->contentInfo->getMainLocationId(), $resultLocationIds); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @return array<array<string, mixed>> |
| 64 | + */ |
| 65 | + private function getPermissions(): array |
| 66 | + { |
| 67 | + return [ |
| 68 | + [ |
| 69 | + 'module' => 'content', |
| 70 | + 'function' => 'create', |
| 71 | + ], |
| 72 | + [ |
| 73 | + 'module' => 'content', |
| 74 | + 'function' => 'publish', |
| 75 | + ], |
| 76 | + [ |
| 77 | + 'module' => 'content', |
| 78 | + 'function' => 'read', |
| 79 | + 'limitations' => [ |
| 80 | + new LocationLimitation(['limitationValues' => [2]]), |
| 81 | + ], |
| 82 | + ], |
| 83 | + [ |
| 84 | + 'module' => 'content', |
| 85 | + 'function' => 'read', |
| 86 | + 'limitations' => [ |
| 87 | + new ContentTypeLimitation(['limitationValues' => [self::FOLDER_CONTENT_TYPE_ID]]), |
| 88 | + new UserGroupLimitation(['limitationValues' => [1]]), |
| 89 | + ], |
| 90 | + ], |
| 91 | + ]; |
| 92 | + } |
| 93 | +} |
0 commit comments