Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/6.13' into HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
Nattfarinn committed Mar 26, 2018
2 parents bcff12b + 38a20d7 commit 9a22957
Show file tree
Hide file tree
Showing 13 changed files with 364 additions and 9 deletions.
Expand Up @@ -77,6 +77,20 @@ services:
tags:
- {name: ezpublish.fieldType.parameterProvider, alias: ezpage}

ezpublish.fieldType.ezobjectrelation.parameterProvider:
class: \eZ\Publish\Core\MVC\Symfony\FieldType\Relation\ParameterProvider
arguments:
- "@ezpublish.api.service.content"
tags:
- {name: ezpublish.fieldType.parameterProvider, alias: ezobjectrelation}

ezpublish.fieldType.ezobjectrelationlist.parameterProvider:
class: \eZ\Publish\Core\MVC\Symfony\FieldType\RelationList\ParameterProvider
arguments:
- "@ezpublish.api.service.content"
tags:
- {name: ezpublish.fieldType.parameterProvider, alias: ezobjectrelationlist}

# Page
ezpublish.fieldType.ezpage.pageService.factory:
class: "%ezpublish.fieldType.ezpage.pageService.factory.class%"
Expand Down
Expand Up @@ -264,7 +264,7 @@
{% spaceless %}
{% if not ez_is_field_empty( content, field ) %}
<ul {{ block( 'field_attributes' ) }}>
{% for contentId in field.value.destinationContentIds %}
{% for contentId in field.value.destinationContentIds if parameters.available[contentId] %}
{{ fos_httpcache_tag('relation-' ~ contentId) }}
<li>
{{ render( controller( "ez_content:viewAction", {'contentId': contentId, 'viewType': 'embed', 'noLayout': 1} ) ) }}
Expand Down Expand Up @@ -435,7 +435,7 @@

{% block ezobjectrelation_field %}
{% spaceless %}
{% if not ez_is_field_empty( content, field ) %}
{% if not ez_is_field_empty( content, field ) and parameters.available %}
{{ fos_httpcache_tag('relation-' ~ field.value.destinationContentId) }}
<div {{ block( 'field_attributes' ) }}>
{{ render( controller( "ez_content:viewAction", {'contentId': field.value.destinationContentId, 'viewType': 'text_linked', 'noLayout': 1} ) ) }}
Expand Down
42 changes: 40 additions & 2 deletions eZ/Publish/API/Repository/Values/Content/ContentInfo.php
Expand Up @@ -25,10 +25,15 @@
* @property-read bool $alwaysAvailable Indicates if the Content object is shown in the mainlanguage if its not present in an other requested language
* @property-read string $remoteId a global unique id of the Content object
* @property-read string $mainLanguageCode The main language code of the Content object. If the available flag is set to true the Content is shown in this language if the requested language does not exist.
* @property-read mixed|null $mainLocationId Identifier of the main location.
* @property-read mixed $mainLocationId Identifier of the main location.
* @property-read int $status status of the Content object
*/
class ContentInfo extends ValueObject
{
const STATUS_DRAFT = 0;
const STATUS_PUBLISHED = 1;
const STATUS_TRASHED = 2;

/**
* The unique id of the Content object.
*
Expand Down Expand Up @@ -122,7 +127,40 @@ class ContentInfo extends ValueObject
* If the Content object has multiple locations,
* $mainLocationId will point to the main one.
*
* @var mixed|null
* @var mixed
*/
protected $mainLocationId;

/**
* Status of the content.
*
* Replaces deprecated API\ContentInfo::$published.
*
* @var int
*/
protected $status;

/**
* @return bool
*/
public function isDraft()
{
return $this->status === self::STATUS_DRAFT;
}

/**
* @return bool
*/
public function isPublished()
{
return $this->status === self::STATUS_PUBLISHED;
}

/**
* @return bool
*/
public function isTrashed()
{
return $this->status === self::STATUS_TRASHED;
}
}
4 changes: 2 additions & 2 deletions eZ/Publish/Core/FieldType/Url/Type.php
Expand Up @@ -82,15 +82,15 @@ protected function createValueFromInput($inputValue)
*/
protected function checkValueStructure(BaseValue $value)
{
if (!is_string($value->link)) {
if (null !== $value->link && !is_string($value->link)) {
throw new InvalidArgumentType(
'$value->link',
'string',
$value->link
);
}

if (isset($value->text) && !is_string($value->text)) {
if (null !== $value->text && !is_string($value->text)) {
throw new InvalidArgumentType(
'$value->text',
'string',
Expand Down
@@ -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];
}
}
}
@@ -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,
];
}
}
@@ -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]);
}
}
@@ -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]]);
}
}
Expand Up @@ -1144,7 +1144,7 @@ public function trashLocation($locationId)
$query->prepare()->execute();

$this->removeLocation($locationRow['node_id']);
$this->setContentStatus($locationRow['contentobject_id'], ContentInfo::STATUS_ARCHIVED);
$this->setContentStatus($locationRow['contentobject_id'], ContentInfo::STATUS_TRASHED);
}

/**
Expand Down

0 comments on commit 9a22957

Please sign in to comment.