|
| 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 | +} |
0 commit comments