forked from ezsystems/ezpublish-kernel
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/6.13' into HEAD
- Loading branch information
Showing
13 changed files
with
364 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
eZ/Publish/Core/MVC/Symfony/FieldType/Relation/ParameterProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace eZ\Publish\Core\MVC\Symfony\FieldType\Relation; | ||
|
||
use eZ\Publish\API\Repository\ContentService; | ||
use eZ\Publish\API\Repository\Exceptions\NotFoundException; | ||
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException; | ||
use eZ\Publish\API\Repository\Values\Content\Field; | ||
use eZ\Publish\Core\MVC\Symfony\FieldType\View\ParameterProviderInterface; | ||
|
||
class ParameterProvider implements ParameterProviderInterface | ||
{ | ||
/** @var \eZ\Publish\API\Repository\ContentService */ | ||
private $contentService; | ||
|
||
/** | ||
* @param \eZ\Publish\API\Repository\ContentService $contentService | ||
*/ | ||
public function __construct(ContentService $contentService) | ||
{ | ||
$this->contentService = $contentService; | ||
} | ||
|
||
/** | ||
* Returns a hash of parameters to inject to the associated fieldtype's view template. | ||
* Returned parameters will only be available for associated field type. | ||
* | ||
* Key is the parameter name (the variable name exposed in the template, in the 'parameters' array). | ||
* Value is the parameter's value. | ||
* | ||
* @param \eZ\Publish\API\Repository\Values\Content\Field $field The field parameters are provided for. | ||
* | ||
* @return array | ||
*/ | ||
public function getViewParameters(Field $field) | ||
{ | ||
try { | ||
$contentInfo = $this->contentService->loadContentInfo($field->value->destinationContentId); | ||
|
||
return [ | ||
'available' => !$contentInfo->isTrashed(), | ||
]; | ||
} catch (NotFoundException $exception) { | ||
return ['available' => false]; | ||
} catch (UnauthorizedException $exception) { | ||
return ['available' => false]; | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
eZ/Publish/Core/MVC/Symfony/FieldType/RelationList/ParameterProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace eZ\Publish\Core\MVC\Symfony\FieldType\RelationList; | ||
|
||
use eZ\Publish\API\Repository\ContentService; | ||
use eZ\Publish\API\Repository\Exceptions\NotFoundException; | ||
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException; | ||
use eZ\Publish\API\Repository\Values\Content\Field; | ||
use eZ\Publish\Core\MVC\Symfony\FieldType\View\ParameterProviderInterface; | ||
|
||
class ParameterProvider implements ParameterProviderInterface | ||
{ | ||
/** @var \eZ\Publish\API\Repository\ContentService */ | ||
private $contentService; | ||
|
||
/** | ||
* @param \eZ\Publish\API\Repository\ContentService $contentService | ||
*/ | ||
public function __construct(ContentService $contentService) | ||
{ | ||
$this->contentService = $contentService; | ||
} | ||
|
||
/** | ||
* Returns a hash of parameters to inject to the associated fieldtype's view template. | ||
* Returned parameters will only be available for associated field type. | ||
* | ||
* Key is the parameter name (the variable name exposed in the template, in the 'parameters' array). | ||
* Value is the parameter's value. | ||
* | ||
* @param \eZ\Publish\API\Repository\Values\Content\Field $field The field parameters are provided for. | ||
* | ||
* @return array | ||
*/ | ||
public function getViewParameters(Field $field) | ||
{ | ||
$available = []; | ||
|
||
foreach ($field->value->destinationContentIds as $contentId) { | ||
try { | ||
$contentInfo = $this->contentService->loadContentInfo($contentId); | ||
|
||
$available[$contentId] = !$contentInfo->isTrashed(); | ||
} catch (NotFoundException $exception) { | ||
$available[$contentId] = false; | ||
} catch (UnauthorizedException $exception) { | ||
$available[$contentId] = false; | ||
} | ||
} | ||
|
||
return [ | ||
'available' => $available, | ||
]; | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
eZ/Publish/Core/MVC/Symfony/FieldType/Tests/Relation/ParameterProviderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace eZ\Publish\Core\MVC\Symfony\FieldType\Tests\RelationList; | ||
|
||
use eZ\Publish\API\Repository\ContentService; | ||
use eZ\Publish\API\Repository\Values\Content\ContentInfo; | ||
use eZ\Publish\API\Repository\Values\Content\Field; | ||
use eZ\Publish\Core\Base\Exceptions\NotFoundException; | ||
use eZ\Publish\Core\Base\Exceptions\UnauthorizedException; | ||
use eZ\Publish\Core\Base\Tests\PHPUnit5CompatTrait; | ||
use eZ\Publish\Core\FieldType\Relation\Value; | ||
use eZ\Publish\Core\MVC\Symfony\FieldType\Relation\ParameterProvider; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ParameterProviderTest extends TestCase | ||
{ | ||
use PHPUnit5CompatTrait; | ||
|
||
public function providerForTestGetViewParameters() | ||
{ | ||
return [ | ||
[ContentInfo::STATUS_DRAFT, ['available' => true]], | ||
[ContentInfo::STATUS_PUBLISHED, ['available' => true]], | ||
[ContentInfo::STATUS_TRASHED, ['available' => false]], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider providerForTestGetViewParameters | ||
*/ | ||
public function testGetViewParameters($status, array $expected) | ||
{ | ||
$contentServiceMock = $this->createMock(ContentService::class); | ||
$contentServiceMock | ||
->method('loadContentInfo') | ||
->will(TestCase::returnValue( | ||
new ContentInfo(['status' => $status]) | ||
)); | ||
|
||
$parameterProvider = new ParameterProvider($contentServiceMock); | ||
$parameters = $parameterProvider->getViewParameters(new Field([ | ||
'value' => new Value(123), | ||
])); | ||
|
||
TestCase::assertSame($parameters, $expected); | ||
} | ||
|
||
public function testNotFoundGetViewParameters() | ||
{ | ||
$contentId = 123; | ||
|
||
$contentServiceMock = $this->createMock(ContentService::class); | ||
$contentServiceMock | ||
->method('loadContentInfo') | ||
->will(TestCase::throwException(new NotFoundException('ContentInfo', $contentId))); | ||
|
||
$parameterProvider = new ParameterProvider($contentServiceMock); | ||
$parameters = $parameterProvider->getViewParameters(new Field([ | ||
'value' => new Value($contentId), | ||
])); | ||
|
||
TestCase::assertSame($parameters, ['available' => false]); | ||
} | ||
|
||
public function testUnauthorizedGetViewParameters() | ||
{ | ||
$contentId = 123; | ||
|
||
$contentServiceMock = $this->createMock(ContentService::class); | ||
$contentServiceMock | ||
->method('loadContentInfo') | ||
->will(TestCase::throwException(new UnauthorizedException('content', 'read'))); | ||
|
||
$parameterProvider = new ParameterProvider($contentServiceMock); | ||
$parameters = $parameterProvider->getViewParameters(new Field([ | ||
'value' => new Value($contentId), | ||
])); | ||
|
||
TestCase::assertSame($parameters, ['available' => false]); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
eZ/Publish/Core/MVC/Symfony/FieldType/Tests/RelationList/ParameterProviderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
namespace eZ\Publish\Core\MVC\Symfony\FieldType\Tests\Relation; | ||
|
||
use eZ\Publish\API\Repository\ContentService; | ||
use eZ\Publish\API\Repository\Values\Content\ContentInfo; | ||
use eZ\Publish\API\Repository\Values\Content\Field; | ||
use eZ\Publish\Core\Base\Exceptions\NotFoundException; | ||
use eZ\Publish\Core\Base\Exceptions\UnauthorizedException; | ||
use eZ\Publish\Core\Base\Tests\PHPUnit5CompatTrait; | ||
use eZ\Publish\Core\FieldType\RelationList\Value; | ||
use eZ\Publish\Core\MVC\Symfony\FieldType\RelationList\ParameterProvider; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ParameterProviderTest extends TestCase | ||
{ | ||
use PHPUnit5CompatTrait; | ||
|
||
public function providerForTestGetViewParameters() | ||
{ | ||
return [ | ||
[[123, 456, 789], ['available' => [123 => true, 456 => true, 789 => false]]], | ||
[[123, 456], ['available' => [123 => true, 456 => true]]], | ||
[[789], ['available' => [789 => false]]], | ||
[[], ['available' => []]], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider providerForTestGetViewParameters | ||
*/ | ||
public function testGetViewParameters(array $desinationContentIds, array $expected) | ||
{ | ||
$contentServiceMock = $this->createMock(ContentService::class); | ||
$contentServiceMock | ||
->method('loadContentInfo') | ||
->will(TestCase::returnValueMap([ | ||
[123, new ContentInfo(['status' => ContentInfo::STATUS_DRAFT])], | ||
[456, new ContentInfo(['status' => ContentInfo::STATUS_PUBLISHED])], | ||
[789, new ContentInfo(['status' => ContentInfo::STATUS_TRASHED])], | ||
])); | ||
|
||
$parameterProvider = new ParameterProvider($contentServiceMock); | ||
$parameters = $parameterProvider->getViewParameters(new Field([ | ||
'value' => new Value($desinationContentIds), | ||
])); | ||
|
||
TestCase::assertSame($parameters, $expected); | ||
} | ||
|
||
public function testNotFoundGetViewParameters() | ||
{ | ||
$contentId = 123; | ||
|
||
$contentServiceMock = $this->createMock(ContentService::class); | ||
$contentServiceMock | ||
->method('loadContentInfo') | ||
->will(TestCase::throwException(new NotFoundException('ContentInfo', $contentId))); | ||
|
||
$parameterProvider = new ParameterProvider($contentServiceMock); | ||
$parameters = $parameterProvider->getViewParameters(new Field([ | ||
'value' => new Value([$contentId]), | ||
])); | ||
|
||
TestCase::assertSame($parameters, ['available' => [$contentId => false]]); | ||
} | ||
|
||
public function testUnauthorizedGetViewParameters() | ||
{ | ||
$contentId = 123; | ||
|
||
$contentServiceMock = $this->createMock(ContentService::class); | ||
$contentServiceMock | ||
->method('loadContentInfo') | ||
->will(TestCase::throwException(new UnauthorizedException('content', 'read'))); | ||
|
||
$parameterProvider = new ParameterProvider($contentServiceMock); | ||
$parameters = $parameterProvider->getViewParameters(new Field([ | ||
'value' => new Value([$contentId]), | ||
])); | ||
|
||
TestCase::assertSame($parameters, ['available' => [$contentId => false]]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.