Skip to content

Commit

Permalink
[Tests] Added REST integration coverage for NodeExtendedInfo endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
alongosz committed Apr 9, 2024
1 parent 166c702 commit 3952e72
Show file tree
Hide file tree
Showing 9 changed files with 326 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"twig/string-extra": "^3.0"
},
"require-dev": {
"dama/doctrine-test-bundle": "^v6.7",
"ibexa/ci-scripts": "^0.2@dev",
"ibexa/behat": "~4.5.0@dev",
"friendsofphp/php-cs-fixer": "^3.0",
Expand Down
3 changes: 3 additions & 0 deletions phpunit.integration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@
<directory>tests/integration</directory>
</testsuite>
</testsuites>
<extensions>
<extension class="DAMA\DoctrineTestBundle\PHPUnit\PHPUnitExtension" />
</extensions>
</phpunit>
4 changes: 4 additions & 0 deletions tests/integration/AdminUiIbexaTestKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Ibexa\Tests\Integration\AdminUi;

use DAMA\DoctrineTestBundle\DAMADoctrineTestBundle;
use Hautelook\TemplatedUriBundle\HautelookTemplatedUriBundle;
use Ibexa\Bundle\AdminUi\IbexaAdminUiBundle;
use Ibexa\Bundle\ContentForms\IbexaContentFormsBundle;
Expand All @@ -20,6 +21,7 @@
use Ibexa\Contracts\Test\Core\IbexaTestKernel;
use Ibexa\Rest\Server\Controller\JWT;
use Knp\Bundle\MenuBundle\KnpMenuBundle;
use Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle;
use Swift_Mailer;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\Resource\FileResource;
Expand All @@ -39,6 +41,8 @@ public function registerBundles(): iterable
yield new HautelookTemplatedUriBundle();
yield new KnpMenuBundle();
yield new WebpackEncoreBundle();
yield new SensioFrameworkExtraBundle();
yield new DAMADoctrineTestBundle();

yield new IbexaContentFormsBundle();
yield new IbexaDesignEngineBundle();
Expand Down
81 changes: 81 additions & 0 deletions tests/integration/REST/BaseAdminUiRestWebTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@

namespace Ibexa\Tests\Integration\AdminUi\REST;

use Ibexa\Contracts\Core\Repository\Values\User\Limitation\RoleLimitation;
use Ibexa\Contracts\Core\Repository\Values\User\Role;
use Ibexa\Contracts\Core\Repository\Values\User\User;
use Ibexa\Contracts\Test\Rest\BaseRestWebTestCase;
use Ibexa\Core\MVC\Symfony\Security\UserWrapped;
use Symfony\Component\Security\Core\User\UserInterface;

/**
* Requires \Ibexa\Tests\Integration\AdminUi\AdminUiIbexaTestKernel kernel.
*
* @phpstan-type TPoliciesData array<string, \Ibexa\Contracts\Core\Repository\Values\User\Limitation[]>
*
* @see \Ibexa\Tests\Integration\AdminUi\AdminUiIbexaTestKernel
*/
abstract class BaseAdminUiRestWebTestCase extends BaseRestWebTestCase
Expand All @@ -26,4 +33,78 @@ protected static function getSnapshotDirectory(): ?string
{
return dirname(__DIR__) . '/Resources/REST/Snapshots';
}

/**
* @phpstan-param TPoliciesData $policiesData
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\ForbiddenException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
*/
protected function createUserWithPolicies(
string $login,
array $policiesData,
?RoleLimitation $roleLimitation = null
): User {
$ibexaTestCore = $this->getIbexaTestCore();
$userService = $ibexaTestCore->getUserService();
$roleService = $ibexaTestCore->getRoleService();

$userCreateStruct = $userService->newUserCreateStruct(
$login,
"$login@test.local",
$login,
'eng-GB'
);
$userCreateStruct->setField('first_name', $login);
$userCreateStruct->setField('last_name', $login);
$user = $userService->createUser($userCreateStruct, [$userService->loadUserGroup(4)]);

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

return $user;
}

/**
* @phpstan-param TPoliciesData $policiesData
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\ForbiddenException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
*/
protected function createRoleWithPolicies(string $roleName, array $policiesData): Role
{
$roleService = $this->getIbexaTestCore()->getRoleService();

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

foreach ($limitations as $limitation) {
$policyCreateStruct->addLimitation($limitation);
}
$roleCreateStruct->addPolicy($policyCreateStruct);
}

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

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

protected function loginAsUser(User $ibexaUser): void
{
$this->client->loginUser($this->mockSymfonyUser($ibexaUser));
}

private function mockSymfonyUser(User $ibexaUser): UserInterface
{
$symfonyUser = $this->createMock(UserInterface::class);
$symfonyUser->method('getRoles')->willReturn(['ROLE_USER']);
$symfonyUser->method('getUsername')->willReturn($ibexaUser->login);

return new UserWrapped($symfonyUser, $ibexaUser);
}
}
71 changes: 71 additions & 0 deletions tests/integration/REST/GetContentTreeExtendedInfoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?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\AdminUi\REST;

use Ibexa\Contracts\Core\Repository\Values\User\Limitation\ContentTypeLimitation;
use Ibexa\Contracts\Core\Repository\Values\User\Limitation\LanguageLimitation;
use Ibexa\Contracts\Test\Rest\Request\Value\EndpointRequestDefinition;

/**
* Coverage for /location/tree/{locationId}/extended-info REST endpoint.
*/
final class GetContentTreeExtendedInfoTest extends BaseAdminUiRestWebTestCase
{
/**
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\ForbiddenException
*/
protected function setUp(): void
{
parent::setUp();

// to create a new user before logging-in via REST
$this->getIbexaTestCore()->setAdministratorUser();

$this->loginAsUser(
$this->createUserWithPolicies(
'editor',
[
'user/login' => [],
'content/read' => [],
'content/create' => [
new ContentTypeLimitation(
['limitationValues' => ['1', '16']]
),
new LanguageLimitation(
['limitationValues' => ['eng-GB', 'eng-US']]
),
],
'content/edit' => [
new LanguageLimitation(
['limitationValues' => ['eng-GB']]
),
],
]
)
);
}

protected static function getEndpointsToTest(): iterable
{
foreach (self::REQUIRED_FORMATS as $format) {
yield new EndpointRequestDefinition(
'GET',
'/api/ibexa/v2/location/tree/2/extended-info',
'ContentTreeNodeExtendedInfo',
"application/vnd.ibexa.api.ContentTreeNodeExtendedInfo+$format",
['HTTP_X-SiteAccess' => 'admin'],
null,
null,
'ContentTreeNodeExtendedInfo'
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"ContentTreeNodeExtendedInfo": {
"type": "object",
"properties": {
"_media-type": {
"type": "string"
},
"permissions": {
"type": "array",
"items": {
"type": "object",
"properties": {
"_name": {
"type": "string"
},
"hasAccess": {
"type": "boolean"
},
"restrictedContentTypeIdsList": {
"type": "object",
"properties": {
"restrictedContentTypeIds": {
"type": "array",
"items": {
"type": "string"
}
}
},
"required": [
"restrictedContentTypeIds"
]
},
"restrictedLanguageCodesList": {
"type": "object",
"properties": {
"restrictedLanguageCodes": {
"type": "array",
"items": {
"type": "string"
}
}
},
"required": [
"restrictedLanguageCodes"
]
}
},
"required": [
"_name",
"hasAccess"
]
}
}
},
"required": [
"_media-type",
"permissions"
]
}
},
"required": [
"ContentTreeNodeExtendedInfo"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Technologies Online Tools 1.0 (https://www.liquid-technologies.com) -->
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="ContentTreeNodeExtendedInfo">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="function">
<xs:complexType>
<xs:sequence>
<xs:element name="hasAccess" type="xs:boolean" />
<xs:element minOccurs="0" name="restrictedContentTypeIdsList">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="value" type="xs:unsignedByte" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element minOccurs="0" name="restrictedLanguageCodesList">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="value" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="media-type" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:schema>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"ContentTreeNodeExtendedInfo": {
"_media-type": "application\/vnd.ibexa.api.ContentTreeNodeExtendedInfo+json",
"permissions": [
{
"_name": "create",
"hasAccess": true,
"restrictedContentTypeIdsList": {
"restrictedContentTypeIds": [
"1",
"16"
]
},
"restrictedLanguageCodesList": {
"restrictedLanguageCodes": [
"eng-GB",
"eng-US"
]
}
},
{
"_name": "edit",
"hasAccess": true,
"restrictedLanguageCodesList": {
"restrictedLanguageCodes": [
"eng-GB"
]
}
},
{
"_name": "delete",
"hasAccess": false
},
{
"_name": "hide",
"hasAccess": false
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<ContentTreeNodeExtendedInfo media-type="application/vnd.ibexa.api.ContentTreeNodeExtendedInfo+xml">
<function name="create">
<hasAccess>true</hasAccess>
<restrictedContentTypeIdsList>
<value>1</value>
<value>16</value>
</restrictedContentTypeIdsList>
<restrictedLanguageCodesList>
<value>eng-GB</value>
<value>eng-US</value>
</restrictedLanguageCodesList>
</function>
<function name="edit">
<hasAccess>true</hasAccess>
<restrictedLanguageCodesList>
<value>eng-GB</value>
</restrictedLanguageCodesList>
</function>
<function name="delete">
<hasAccess>false</hasAccess>
</function>
<function name="hide">
<hasAccess>false</hasAccess>
</function>
</ContentTreeNodeExtendedInfo>

0 comments on commit 3952e72

Please sign in to comment.