Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/bundle/Resources/config/input_parsers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -874,3 +874,11 @@ services:
$validator: '@validator'
tags:
- { name: ibexa.rest.input.parser, mediaType: application/vnd.ibexa.api.SwapLocationInput }

Ibexa\Rest\Server\Input\Parser\RestoreTrashItemInput:
parent: Ibexa\Rest\Server\Common\Parser
arguments:
$locationService: '@ibexa.api.service.location'
$validator: '@validator'
tags:
- { name: ibexa.rest.input.parser, mediaType: application/vnd.ibexa.api.RestoreTrashItemInput }
10 changes: 10 additions & 0 deletions src/bundle/Resources/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,16 @@ ibexa.rest.unlink_content_type_from_group:
# Trash


ibexa.rest.trash.restore_trash_item:
path: /content/trash/{trashItemId}
controller: Ibexa\Rest\Server\Controller\Trash::restoreItem
condition: 'ibexa_get_media_type(request) === "RestoreTrashItemInput"'
methods: [POST]
options:
options_route_suffix: 'RestoreTrashItemInput'
requirements:
trashItemId: \d+

ibexa.rest.load_trash_items:
path: /content/trash
defaults:
Expand Down
32 changes: 32 additions & 0 deletions src/lib/Server/Controller/Trash.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Ibexa\Contracts\Core\Repository\LocationService;
use Ibexa\Contracts\Core\Repository\TrashService;
use Ibexa\Contracts\Core\Repository\Values\Content\Query;
use Ibexa\Rest\Message;
use Ibexa\Rest\Server\Controller as RestController;
use Ibexa\Rest\Server\Exceptions\ForbiddenException;
use Ibexa\Rest\Server\Values;
Expand Down Expand Up @@ -176,4 +177,35 @@ public function restoreTrashItem($trashItemId, Request $request)
)
);
}

/**
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
*/
public function restoreItem(int $trashItemId, Request $request): Values\ResourceCreated
{
$locationDestination = $this->inputDispatcher->parse(
new Message(
['Content-Type' => $request->headers->get('Content-Type')],
$request->getContent(),
),
);

$trashItem = $this->trashService->loadTrashItem($trashItemId);

if ($locationDestination === null) {
$locationDestination = $this->locationService->loadLocation($trashItem->parentLocationId);
}

$location = $this->trashService->recover($trashItem, $locationDestination);

return new Values\ResourceCreated(
$this->router->generate(
'ibexa.rest.load_location',
[
'locationPath' => trim($location->getPathString(), '/'),
],
)
);
}
}
92 changes: 92 additions & 0 deletions src/lib/Server/Input/Parser/RestoreTrashItemInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?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\Rest\Server\Input\Parser;

use Ibexa\Contracts\Core\Repository\LocationService;
use Ibexa\Contracts\Core\Repository\Values\Content\Location;
use Ibexa\Contracts\Rest\Input\ParsingDispatcher;
use Ibexa\Rest\Input\BaseParser;
use Ibexa\Rest\Server\Exceptions\ValidationFailedException;
use Ibexa\Rest\Server\Validation\Builder\Input\Parser\RestoreTrashItemInputValidatorBuilder;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Webmozart\Assert\Assert;

final class RestoreTrashItemInput extends BaseParser
{
public const string DESTINATION_KEY = 'destination';

public function __construct(
private readonly LocationService $locationService,
private readonly ValidatorInterface $validator,
) {
}

/**
* @phpstan-param array{
* 'destination': string,
* } $data
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
* @throws \Ibexa\Contracts\Rest\Exceptions\Parser
*/
public function parse(array $data, ParsingDispatcher $parsingDispatcher): ?Location
{
$this->validateInputData($data);

$location = $data[self::DESTINATION_KEY] ?? null;

return $location === null ? null : $this->getLocationByPath($location);
}

/**
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
*/
private function getLocationByPath(string $path): Location
{
return $this->locationService->loadLocation(
$this->extractLocationIdFromPath($path)
);
}

private function extractLocationIdFromPath(string $path): int
{
$pathParts = explode('/', $path);

$lastPart = array_pop($pathParts);

Assert::integerish($lastPart);

return (int)$lastPart;
}

/**
* @phpstan-assert array{
* destination?: string,
* } $data
*
* @param array<mixed> $data
*
* @throws \Ibexa\Rest\Server\Exceptions\ValidationFailedException
*/
private function validateInputData(array $data): void
{
$builder = new RestoreTrashItemInputValidatorBuilder($this->validator);
$builder->validateInputArray($data);
$violations = $builder->build()->getViolations();

if ($violations->count() > 0) {
throw new ValidationFailedException(
'RestoreTrashItemInput',
$violations,
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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\Rest\Server\Validation\Builder\Input\Parser;

use Ibexa\Rest\Server\Input\Parser\RestoreTrashItemInput;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints as Assert;

final class RestoreTrashItemInputValidatorBuilder extends BaseInputParserValidatorBuilder
{
protected function buildConstraint(): Constraint
{
return new Assert\AtLeastOneOf(
[
new Assert\Count(0),
new Assert\Collection(
[
RestoreTrashItemInput::DESTINATION_KEY => [
new Assert\NotBlank(),
new Assert\Type('string'),
new Assert\Regex('/^(\/\d+)+$/'),
],
],
),
],
);
}
}
1 change: 1 addition & 0 deletions tests/bundle/Functional/HttpOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public function providerForTestHttpOptions(): array
['/content/types/1/groups/1', ['DELETE']],
['/content/trash', ['GET', 'DELETE']],
['/content/trash/1', ['GET', 'DELETE', 'MOVE']],
['/content/trash/1', ['POST'], 'RestoreTrashItemInput+json'],
['/content/urlwildcards', ['GET', 'POST']],
['/content/urlwildcards/1', ['GET', 'DELETE']],
['/user/policies', ['GET']],
Expand Down
34 changes: 34 additions & 0 deletions tests/bundle/Functional/TrashTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,38 @@ private function sendLocationToTrash(string $contentHref): string

return $trashHref;
}

public function testRestoreItemWithDestination(): void
{
$trashItemHref = $this->createTrashItem('testItemToRestore');

$request = $this->createHttpRequest(
'POST',
$trashItemHref,
'RestoreTrashItemInput+json',
'',
json_encode(['RestoreTrashItemInput' => ['destination' => '/1/2']], JSON_THROW_ON_ERROR),
);
$response = $this->sendHttpRequest($request);

self::assertHttpResponseCodeEquals($response, 201);
self::assertHttpResponseHasHeader($response, 'Location');
}

public function testRestoreTrashItemWithoutDestination(): void
{
$trashItemHref = $this->createTrashItem('testItemToRestore');

$request = $this->createHttpRequest(
'POST',
$trashItemHref,
'RestoreTrashItemInput+json',
'',
json_encode(['RestoreTrashItemInput' => []], JSON_THROW_ON_ERROR),
);
$response = $this->sendHttpRequest($request);

self::assertHttpResponseCodeEquals($response, 201);
self::assertHttpResponseHasHeader($response, 'Location');
}
}
102 changes: 102 additions & 0 deletions tests/lib/Server/Input/Parser/RestoreTrashItemInputTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?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\Rest\Server\Input\Parser;

use Ibexa\Contracts\Core\Repository\LocationService;
use Ibexa\Core\Repository\Values\Content\Location;
use Ibexa\Rest\Server\Exceptions\ValidationFailedException;
use Ibexa\Rest\Server\Input\Parser\RestoreTrashItemInput;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Validator\ValidatorInterface;

final class RestoreTrashItemInputTest extends BaseTest
{
private const int TESTED_LOCATION_ID = 22;

private MockObject&LocationService $locationService;

private ValidatorInterface $validator;

public function testParse(): void
{
$destinationPath = sprintf('/1/2/%d', self::TESTED_LOCATION_ID);

$inputArray = [
'destination' => $destinationPath,
];

$moveLocationParser = $this->getParser();

$this->locationService
->expects(self::once())
->method('loadLocation')
->with(self::TESTED_LOCATION_ID)
->willReturn($this->getMockedLocation());

$result = $moveLocationParser->parse($inputArray, $this->getParsingDispatcherMock());

self::assertEquals(
$this->getMockedLocation()->getId(),
$result->id,
);

self::assertEquals(
$this->getMockedLocation()->getPathString(),
$result->getPathString(),
);
}

public function testParseWithMissingDestinationElement(): void
{
$inputArray = [];

$sessionInput = $this->getParser();

$result = $sessionInput->parse($inputArray, $this->getParsingDispatcherMock());

self::assertEmpty($result);
}

public function testParseExceptionOnInvalidDestinationElement(): void
{
$inputArray = [
'destination' => 'test_destination',
];

$sessionInput = $this->getParser();

$this->expectException(ValidationFailedException::class);
$this->expectExceptionMessage('Input data validation failed for RestoreTrashItemInput');

$sessionInput->parse($inputArray, $this->getParsingDispatcherMock());
}

protected function internalGetParser(): RestoreTrashItemInput
{
$locationService = $this->createMock(LocationService::class);
$this->locationService = $locationService;
$this->validator = Validation::createValidator();

return new RestoreTrashItemInput(
$this->locationService,
$this->validator,
);
}

private function getMockedLocation(): Location
{
return new Location(
[
'id' => self::TESTED_LOCATION_ID,
'pathString' => sprintf('/1/2/%d', self::TESTED_LOCATION_ID),
],
);
}
}