Skip to content

Commit

Permalink
IBX-6592: Provided integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
barw4 committed Nov 8, 2023
1 parent 6e3f37f commit 2b8fdec
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\Integration\Core\Repository\ObjectStateService;

use eZ\Publish\API\Repository\Exceptions\UnauthorizedException;
use eZ\Publish\API\Repository\Values\User\Limitation\ObjectStateLimitation;
use eZ\Publish\API\Repository\Values\User\Limitation\SubtreeLimitation;
use Ibexa\Tests\Integration\Core\RepositoryTestCase;

/**
* @covers \eZ\Publish\API\Repository\ObjectStateService
*/
final class SetContentStateTest extends RepositoryTestCase
{
/**
* @dataProvider dataProviderForTestSetContentObjectStateWithSubtreeLimitation
*/
public function testSetContentObjectStateWithSubtreeLimitation(
?string $subtreeLimitationValue,
bool $isInsideLimitation
): void {
$permissionResolver = self::getPermissionResolver();
$objectStateService = self::getObjectStateService();

$objectState = $objectStateService->loadObjectState(2);

$subtreeLimitationFolder = $this->createFolder(['eng-GB' => 'Subtree limitation type'], 2);
$contentInfo = $subtreeLimitationFolder->getVersionInfo()->getContentInfo();
$mainLocation = $contentInfo->getMainLocation();

$limitations = [
new SubtreeLimitation(
[
'limitationValues' => [$subtreeLimitationValue ?? $mainLocation->getPathString()],
],
),
new ObjectStateLimitation(
[
'limitationValues' => [1, 2],
],
),
];

$user = $this->createUserWithPolicies(
'object_state_user',
[
['module' => 'content', 'function' => '*'],
['module' => 'state', 'function' => 'assign', 'limitations' => $limitations],
]
);

$permissionResolver->setCurrentUserReference($user);

$childFolder = $this->createFolder(['eng-GB' => 'Child folder'], $mainLocation->id);
$childContentInfo = $childFolder->getVersionInfo()->getContentInfo();

if (!$isInsideLimitation) {
self::expectException(UnauthorizedException::class);
}

$objectStateService->setContentState(
$childContentInfo,
$objectState->getObjectStateGroup(),
$objectState,
$childContentInfo->getMainLocation(),
);

$contentState = $objectStateService->getContentState($childContentInfo, $objectState->getObjectStateGroup());

self::assertSame($objectState->identifier, $contentState->identifier);
}

public function dataProviderForTestSetContentObjectStateWithSubtreeLimitation(): iterable
{
yield 'inside subtree limitation' => [
null,
true,
];

yield 'outside limitation passes' => [
'/1/43',
false,
];
}
}
72 changes: 72 additions & 0 deletions tests/integration/Core/RepositoryTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
namespace Ibexa\Tests\Integration\Core;

use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\User\Role;
use eZ\Publish\API\Repository\Values\User\User;
use Ibexa\Contracts\Core\Test\IbexaKernelTestCase;
use InvalidArgumentException;

Expand Down Expand Up @@ -70,4 +72,74 @@ public function createFolderDraft(array $names, int $parentLocationId = self::CO
]
);
}

/**
* @param array<mixed> $policiesData
*
* @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
public function createRoleWithPolicies(string $roleName, array $policiesData): Role
{
$roleService = self::getRoleService();

$roleCreateStruct = $roleService->newRoleCreateStruct($roleName);
foreach ($policiesData as $policyData) {
$policyCreateStruct = $roleService->newPolicyCreateStruct(
$policyData['module'],
$policyData['function']
);

if (isset($policyData['limitations'])) {
foreach ($policyData['limitations'] as $limitation) {
$policyCreateStruct->addLimitation($limitation);
}
}

$roleCreateStruct->addPolicy($policyCreateStruct);
}

$roleDraft = $roleService->createRole($roleCreateStruct);

$roleService->publishRoleDraft($roleDraft);

return $roleService->loadRole($roleDraft->id);
}

/**
* @param array<array<array-key, array<string|array<\eZ\Publish\API\Repository\Values\User\Limitation>>>> $policiesData
*
* @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
* @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException
* @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException
*/
public function createUserWithPolicies(string $login, array $policiesData): User
{
$roleService = self::getRoleService();
$userService = self::getUserService();

$userCreateStruct = $userService->newUserCreateStruct(
$login,
"{$login}@test.dev",
$login,
'eng-GB'
);

$userCreateStruct->setField('first_name', $login);
$userCreateStruct->setField('last_name', $login);
$user = $userService->createUser($userCreateStruct, []);

$role = $this->createRoleWithPolicies(
uniqid('role_for_' . $login . '_', true),
$policiesData
);
$roleService->assignRoleToUser($role, $user);

return $user;
}
}

0 comments on commit 2b8fdec

Please sign in to comment.