Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-5387: Handled not accessible Location in RelationProcessor #60

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 10 additions & 7 deletions src/lib/FieldTypeProcessor/BaseRelationProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
namespace Ibexa\Rest\FieldTypeProcessor;

use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
use Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException;
use Ibexa\Contracts\Core\Repository\LocationService;
use Ibexa\Contracts\Rest\FieldTypeProcessor;
use Ibexa\Core\FieldType\Relation\Type;
Expand Down Expand Up @@ -57,15 +59,16 @@ public function mapToContentHref($contentId)
return $this->router->generate('ibexa.rest.load_content', ['contentId' => $contentId]);
}

/**
* @param int $locationId
*
* @return string
*/
public function mapToLocationHref($locationId)
public function mapToLocationHref(int $locationId): ?string
{
try {
$location = $this->locationService->loadLocation($locationId);
} catch (UnauthorizedException | NotFoundException $e) {
return null;
barw4 marked this conversation as resolved.
Show resolved Hide resolved
}

return $this->router->generate('ibexa.rest.load_location', [
'locationPath' => implode('/', $this->locationService->loadLocation($locationId)->path),
'locationPath' => implode('/', $location->path),
]);
}

Expand Down
28 changes: 28 additions & 0 deletions tests/lib/FieldTypeProcessor/RelationProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Ibexa\Tests\Rest\FieldTypeProcessor;

use Ibexa\Contracts\Core\Repository\LocationService;
use Ibexa\Core\Base\Exceptions\NotFoundException;
use Ibexa\Core\Repository\Values\Content\Location;
use Ibexa\Rest\FieldTypeProcessor\RelationProcessor;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -127,6 +128,33 @@ public function testPostProcessFieldValueHashNullValue()
$this->assertArrayNotHasKey('destinationContentHref', $hash);
}

public function testPostProcessFieldValueHashNotAccessibleLocation(): void
{
$processor = $this->getProcessor();

$serviceLocationMock = $this->createMock(LocationService::class);
$processor->setLocationService($serviceLocationMock);

$serviceLocationMock
->method('loadLocation')
->with('-1')
->willThrowException(new NotFoundException('', ''));

$routerMock = $this->createMock(RouterInterface::class);
$processor->setRouter($routerMock);

$routerMock
->expects(self::never())
->method('generate');

$hash = $processor->postProcessFieldSettingsHash(['selectionRoot' => -1]);

self::assertEquals([
'selectionRoot' => -1,
'selectionRootHref' => null,
], $hash);
}

/**
* @return \Ibexa\Rest\FieldTypeProcessor\RelationProcessor
*/
Expand Down