Completed
Push — ezp-31420-merge-up ( ec14fb...141a64 )
by
unknown
40:13 queued 27:42
created

FieldRenderingExtensionIntegrationTest::getResourceProviderMock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 42
rs 9.248
c 0
b 0
f 0
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 eZ\Publish\Core\MVC\Symfony\Templating\Tests\Twig\Extension;
8
9
use eZ\Publish\API\Repository\ContentService;
10
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
11
use eZ\Publish\API\Repository\Values\Content\Field;
12
use eZ\Publish\Core\Helper\TranslationHelper;
13
use eZ\Publish\Core\MVC\ConfigResolverInterface;
14
use eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\FieldRenderingExtension;
15
use eZ\Publish\Core\MVC\Symfony\Templating\Twig\FieldBlockRenderer;
16
use eZ\Publish\Core\MVC\Symfony\FieldType\View\ParameterProviderRegistryInterface;
17
use eZ\Publish\Core\Repository\Values\Content\Content;
18
use eZ\Publish\Core\Repository\Values\Content\VersionInfo;
19
use eZ\Publish\Core\Repository\Values\ContentType\ContentType;
20
use eZ\Publish\Core\Repository\Values\ContentType\FieldDefinition;
21
use eZ\Publish\Core\Repository\Values\ContentType\FieldDefinitionCollection;
22
use Psr\Log\LoggerInterface;
23
24
class FieldRenderingExtensionIntegrationTest extends FileSystemTwigIntegrationTestCase
25
{
26
    private $fieldDefinitions = [];
27
28
    public function getExtensions()
29
    {
30
        $configResolver = $this->getConfigResolverMock();
31
32
        $fieldBlockRenderer = new FieldBlockRenderer();
33
        $fieldBlockRenderer->setBaseTemplate($this->getTemplatePath('base.html.twig'));
34
        $fieldBlockRenderer->setFieldViewResources(
35
            [
36
                [
37
                    'template' => $this->getTemplatePath('fields_override1.html.twig'),
38
                    'priority' => 10,
39
                ],
40
                [
41
                    'template' => $this->getTemplatePath('fields_default.html.twig'),
42
                    'priority' => 0,
43
                ],
44
                [
45
                    'template' => $this->getTemplatePath('fields_override2.html.twig'),
46
                    'priority' => 20,
47
                ],
48
            ]
49
        );
50
        $fieldBlockRenderer->setFieldDefinitionViewResources(
51
            [
52
                [
53
                    'template' => $this->getTemplatePath('settings_override1.html.twig'),
54
                    'priority' => 10,
55
                ],
56
                [
57
                    'template' => $this->getTemplatePath('settings_default.html.twig'),
58
                    'priority' => 0,
59
                ],
60
                [
61
                    'template' => $this->getTemplatePath('settings_override2.html.twig'),
62
                    'priority' => 20,
63
                ],
64
            ]
65
        );
66
67
        return [
68
            new FieldRenderingExtension(
69
                $fieldBlockRenderer,
70
                $this->createMock(ParameterProviderRegistryInterface::class),
71
                new TranslationHelper(
72
                    $configResolver,
73
                    $this->createMock(ContentService::class),
74
                    [],
75
                    $this->createMock(LoggerInterface::class)
76
                )
77
            ),
78
        ];
79
    }
80
81
    public function getFixturesDir()
82
    {
83
        return __DIR__ . '/_fixtures/field_rendering_functions/';
84
    }
85
86
    public function getFieldDefinition($typeIdentifier, $id = null, $settings = [])
87
    {
88
        return new FieldDefinition(
89
            [
90
                'id' => $id,
91
                'fieldSettings' => $settings,
92
                'fieldTypeIdentifier' => $typeIdentifier,
93
            ]
94
        );
95
    }
96
97
    /**
98
     * Creates content with initial/main language being fre-FR.
99
     *
100
     * @param string $contentTypeIdentifier
101
     * @param array $fieldsData
102
     * @param array $namesData
103
     *
104
     * @return Content
105
     */
106
    protected function getContent($contentTypeIdentifier, array $fieldsData, array $namesData = [])
107
    {
108
        $fields = [];
109
        foreach ($fieldsData as $fieldTypeIdentifier => $fieldsArray) {
110
            $fieldsArray = isset($fieldsArray['id']) ? [$fieldsArray] : $fieldsArray;
111
            foreach ($fieldsArray as $fieldInfo) {
112
                // Save field definitions in property for mocking purposes
113
                $this->fieldDefinitions[$contentTypeIdentifier][$fieldInfo['fieldDefIdentifier']] = new FieldDefinition(
114
                    [
115
                        'identifier' => $fieldInfo['fieldDefIdentifier'],
116
                        'id' => $fieldInfo['id'],
117
                        'fieldTypeIdentifier' => $fieldTypeIdentifier,
118
                        'names' => isset($fieldInfo['fieldDefNames']) ? $fieldInfo['fieldDefNames'] : [],
119
                        'descriptions' => isset($fieldInfo['fieldDefDescriptions']) ? $fieldInfo['fieldDefDescriptions'] : [],
120
                    ]
121
                );
122
                unset($fieldInfo['fieldDefNames'], $fieldInfo['fieldDefDescriptions']);
123
                $fields[] = new Field($fieldInfo);
124
            }
125
        }
126
        $content = new Content(
127
            [
128
                'internalFields' => $fields,
129
                'contentType' => new ContentType([
130
                    'id' => $contentTypeIdentifier,
131
                    'identifier' => $contentTypeIdentifier,
132
                    'mainLanguageCode' => 'fre-FR',
133
                    'fieldDefinitions' => new FieldDefinitionCollection(
134
                        $this->fieldDefinitions[$contentTypeIdentifier
135
                    ]),
136
                ]),
137
                'versionInfo' => new VersionInfo(
138
                    [
139
                        'versionNo' => 64,
140
                        'names' => $namesData,
141
                        'initialLanguageCode' => 'fre-FR',
142
                        'contentInfo' => new ContentInfo(
143
                            [
144
                                'id' => 42,
145
                                'mainLanguageCode' => 'fre-FR',
146
                                // Using as id as we don't really care to test the service here
147
                                'contentTypeId' => $contentTypeIdentifier,
148
                            ]
149
                        ),
150
                    ]
151
                ),
152
            ]
153
        );
154
155
        return $content;
156
    }
157
158
    private function getTemplatePath($tpl)
159
    {
160
        return 'templates/' . $tpl;
161
    }
162
163 View Code Duplication
    private function getConfigResolverMock()
164
    {
165
        $mock = $this->createMock(ConfigResolverInterface::class);
166
        // Signature: ConfigResolverInterface->getParameter( $paramName, $namespace = null, $scope = null )
167
        $mock->expects($this->any())
168
            ->method('getParameter')
169
            ->will(
170
                $this->returnValueMap(
171
                    [
172
                        [
173
                            'languages',
174
                            null,
175
                            null,
176
                            ['fre-FR', 'eng-US'],
177
                        ],
178
                    ]
179
                )
180
            );
181
182
        return $mock;
183
    }
184
}
185