Skip to content

Commit 48cf950

Browse files
author
Bertrand Dunogier
committed
Merge branch '2.0' into 2.1
2 parents a202bac + 2fb712c commit 48cf950

File tree

3 files changed

+133
-7
lines changed

3 files changed

+133
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
namespace spec\EzSystems\EzPlatformQueryFieldType\eZ\ContentView;
8+
9+
use eZ\Publish\API\Repository\ContentTypeService;
10+
use eZ\Publish\API\Repository\Repository;
11+
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
12+
use eZ\Publish\Core\MVC\Symfony\Matcher\ViewMatcherInterface;
13+
use eZ\Publish\Core\MVC\Symfony\View\ContentView;
14+
use eZ\Publish\Core\Repository\Values\Content\Content;
15+
use eZ\Publish\Core\Repository\Values\Content\VersionInfo;
16+
use eZ\Publish\Core\Repository\Values\ContentType\ContentType;
17+
use eZ\Publish\Core\Repository\Values\ContentType\FieldDefinition;
18+
use eZ\Publish\Core\Repository\Values\ContentType\FieldDefinitionCollection;
19+
use EzSystems\EzPlatformQueryFieldType\eZ\ContentView\FieldDefinitionIdentifierMatcher;
20+
use PhpSpec\ObjectBehavior;
21+
22+
class FieldDefinitionIdentifierMatcherSpec extends ObjectBehavior
23+
{
24+
private const CONTENT_TYPE_ID_WITHOUT_FIELD_DEFINITION = 2;
25+
private const CONTENT_TYPE_IDENTIFIER_WITHOUT_FIELD_DEFINITION = 'type_matching_without_field_def';
26+
27+
private const CONTENT_TYPE_ID_WITH_FIELD_DEFINITION = 3;
28+
private const CONTENT_TYPE_IDENTIFIER_WITH_FIELD_DEFINITION = 'type_matching_with_field_def';
29+
30+
const FIELD_DEFINITION_IDENTIFIER = 'field_definition';
31+
32+
function it_is_initializable()
33+
{
34+
$this->shouldHaveType(FieldDefinitionIdentifierMatcher::class);
35+
$this->shouldHaveType(ViewMatcherInterface::class);
36+
}
37+
38+
static function initialize(Repository $repository, array $matchingConfig): FieldDefinitionIdentifierMatcher
39+
{
40+
$matcher = new FieldDefinitionIdentifierMatcher();
41+
$matcher->setRepository($repository);
42+
$matcher->setMatchingConfig($matchingConfig);
43+
44+
return $matcher;
45+
}
46+
47+
function let(Repository $repository, ContentTypeService $contentTypeService)
48+
{
49+
$repository->getContentTypeService()->willReturn($contentTypeService);
50+
$contentTypeService->loadContentType(self::CONTENT_TYPE_ID_WITHOUT_FIELD_DEFINITION)->willReturn($this->createContentTypeWithoutFieldDefinition());
51+
$contentTypeService->loadContentType(self::CONTENT_TYPE_ID_WITH_FIELD_DEFINITION)->willReturn($this->createMatchingContentTypeWithFieldDefinition());
52+
$this->beConstructedThrough([$this, 'initialize'], [$repository, [self::FIELD_DEFINITION_IDENTIFIER]]);
53+
}
54+
55+
function it_does_not_match_if_field_definition_identifier_does_not_exist()
56+
{
57+
$view = $this->buildView(self::CONTENT_TYPE_ID_WITHOUT_FIELD_DEFINITION);
58+
$this->match($view)->shouldBe(false);
59+
}
60+
61+
function it_matches_if_field_definition_identifier_matches()
62+
{
63+
$view = $this->buildView(self::CONTENT_TYPE_ID_WITH_FIELD_DEFINITION);
64+
$this->match($view)->shouldBe(true);
65+
}
66+
67+
private function buildView($contentTypeId): ContentView
68+
{
69+
$view = new ContentView();
70+
$view->setContent(
71+
new Content([
72+
'versionInfo' => new VersionInfo([
73+
'contentInfo' => new ContentInfo(['contentTypeId' => $contentTypeId]),
74+
]),
75+
])
76+
);
77+
$view->addParameters(['fieldIdentifier' => self::FIELD_DEFINITION_IDENTIFIER]);
78+
79+
return $view;
80+
}
81+
82+
private function createContentTypeWithoutFieldDefinition(): ContentType
83+
{
84+
return $this->createContentType(
85+
self::CONTENT_TYPE_ID_WITHOUT_FIELD_DEFINITION,
86+
self::CONTENT_TYPE_IDENTIFIER_WITHOUT_FIELD_DEFINITION,
87+
false
88+
);
89+
}
90+
91+
private function createMatchingContentTypeWithFieldDefinition(): ContentType
92+
{
93+
return $this->createContentType(
94+
self::CONTENT_TYPE_ID_WITH_FIELD_DEFINITION,
95+
self::CONTENT_TYPE_IDENTIFIER_WITH_FIELD_DEFINITION,
96+
true
97+
);
98+
}
99+
100+
private function createContentType(int $contentTypeId, string $contentTypeIdentifier, bool $withFieldDefinition): ContentType
101+
{
102+
$fieldDefinitions = [];
103+
if ($withFieldDefinition === true) {
104+
$fieldDefinitions[] = new FieldDefinition(['identifier' => self::FIELD_DEFINITION_IDENTIFIER]);
105+
}
106+
$fieldDefinitions = new FieldDefinitionCollection($fieldDefinitions);
107+
108+
return new ContentType(
109+
[
110+
'id' => $contentTypeId,
111+
'identifier' => $contentTypeIdentifier,
112+
'fieldDefinitions' => $fieldDefinitions,
113+
]
114+
);
115+
}
116+
}

src/eZ/ContentView/FieldDefinitionIdentifierMatcher.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ public function match(View $view)
7171
->getContentTypeService()
7272
->loadContentType($view->getContent()->contentInfo->contentTypeId);
7373

74-
return $this->hasFieldDefinition($contentType);
74+
if (!$this->hasFieldDefinition($contentType)) {
75+
return false;
76+
}
77+
78+
if (!$view->hasParameter('fieldIdentifier')) {
79+
return false;
80+
}
81+
82+
return in_array($view->getParameter('fieldIdentifier'), $this->getValues(), true);
7583
}
7684
}

src/eZ/ContentView/QueryResultsInjector.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
namespace EzSystems\EzPlatformQueryFieldType\eZ\ContentView;
88

9+
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
910
use eZ\Publish\Core\MVC\Symfony\View\Event\FilterViewParametersEvent;
1011
use eZ\Publish\Core\MVC\Symfony\View\ViewEvents;
1112
use EzSystems\EzPlatformQueryFieldType\API\QueryFieldServiceInterface;
@@ -45,17 +46,19 @@ public static function getSubscribedEvents()
4546
*/
4647
public function injectQueryResults(FilterViewParametersEvent $event)
4748
{
48-
$viewType = $event->getView()->getViewType();
49-
50-
if ($viewType === $this->views['field']) {
49+
if ($event->getView()->getViewType() === $this->views['field']) {
50+
$builderParameters = $event->getBuilderParameters();
51+
if (!isset($builderParameters['queryFieldDefinitionIdentifier'])) {
52+
throw new InvalidArgumentException('queryFieldDefinitionIdentifier', 'missing');
53+
}
5154
$parameters = [
5255
'itemViewType' => $event->getBuilderParameters()['itemViewType'] ?? $this->views['item'],
5356
'items' => $this->buildResults($event),
57+
'fieldIdentifier' => $builderParameters['queryFieldDefinitionIdentifier'],
5458
];
5559
$parameters['isPaginationEnabled'] = ($parameters['items'] instanceof Pagerfanta);
5660
if ($parameters['isPaginationEnabled']) {
57-
$fieldDefinitionIdentifier = $event->getBuilderParameters()['queryFieldDefinitionIdentifier'];
58-
$parameters['pageParameter'] = sprintf('[%s_page]', $fieldDefinitionIdentifier);
61+
$parameters['pageParameter'] = sprintf('[%s_page]', $parameters['fieldIdentifier']);
5962
}
6063
$event->getParameterBag()->add($parameters);
6164
}
@@ -116,7 +119,6 @@ private function buildResults(FilterViewParametersEvent $event): iterable
116119

117120
return $pager;
118121
} else {
119-
// @todo error handling if parameter not set
120122
return $this->queryFieldService->loadContentItems(
121123
$content,
122124
$fieldDefinitionIdentifier

0 commit comments

Comments
 (0)