Completed
Push — master ( 7c0e42...8c347f )
by André
14:17
created

NameSchemaTest::testResolveNameSchema()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 17

Duplication

Lines 24
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 0
dl 24
loc 24
rs 8.9713
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File contains: eZ\Publish\Core\Repository\Tests\Service\Mock\NameSchemaTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Repository\Tests\Service\Mock;
10
11
use eZ\Publish\Core\Repository\Helper\NameSchemaService;
12
use eZ\Publish\Core\Repository\Tests\Service\Mock\Base as BaseServiceMockTest;
13
use eZ\Publish\Core\Repository\Values\Content\Content;
14
use eZ\Publish\Core\Repository\Values\Content\VersionInfo;
15
use eZ\Publish\Core\Repository\Values\ContentType\ContentType;
16
use eZ\Publish\Core\Repository\Values\ContentType\FieldDefinition;
17
use eZ\Publish\API\Repository\Values\Content\Field;
18
use eZ\Publish\Core\FieldType\TextLine\Value as TextLineValue;
19
20
/**
21
 * Mock Test case for NameSchema service.
22
 */
23
class NameSchemaTest extends BaseServiceMockTest
24
{
25
    /**
26
     * Test eZ\Publish\Core\Repository\Helper\NameSchemaService method.
27
     *
28
     * @covers \eZ\Publish\Core\Repository\Helper\NameSchemaService::resolveUrlAliasSchema
29
     */
30 View Code Duplication
    public function testResolveUrlAliasSchema()
31
    {
32
        $serviceMock = $this->getPartlyMockedNameSchemaService(array('resolve'));
33
34
        $content = $this->buildTestContentObject();
35
        $contentType = $this->buildTestContentType();
36
37
        $serviceMock->expects(
38
            $this->once()
39
        )->method(
40
            'resolve'
41
        )->with(
42
            '<urlalias_schema>',
43
            $this->equalTo($contentType),
44
            $this->equalTo($content->fields),
45
            $this->equalTo($content->versionInfo->languageCodes)
46
        )->will(
47
            $this->returnValue(42)
48
        );
49
50
        $result = $serviceMock->resolveUrlAliasSchema($content, $contentType);
51
52
        self::assertEquals(42, $result);
53
    }
54
55
    /**
56
     * Test eZ\Publish\Core\Repository\Helper\NameSchemaService method.
57
     *
58
     * @covers \eZ\Publish\Core\Repository\Helper\NameSchemaService::resolveUrlAliasSchema
59
     */
60 View Code Duplication
    public function testResolveUrlAliasSchemaFallbackToNameSchema()
61
    {
62
        $serviceMock = $this->getPartlyMockedNameSchemaService(array('resolve'));
63
64
        $content = $this->buildTestContentObject();
65
        $contentType = $this->buildTestContentType('<name_schema>', '');
66
67
        $serviceMock->expects(
68
            $this->once()
69
        )->method(
70
            'resolve'
71
        )->with(
72
            '<name_schema>',
73
            $this->equalTo($contentType),
74
            $this->equalTo($content->fields),
75
            $this->equalTo($content->versionInfo->languageCodes)
76
        )->will(
77
            $this->returnValue(42)
78
        );
79
80
        $result = $serviceMock->resolveUrlAliasSchema($content, $contentType);
81
82
        self::assertEquals(42, $result);
83
    }
84
85
    /**
86
     * Test eZ\Publish\Core\Repository\Helper\NameSchemaService method.
87
     *
88
     * @covers \eZ\Publish\Core\Repository\Helper\NameSchemaService::resolveNameSchema
89
     */
90 View Code Duplication
    public function testResolveNameSchema()
91
    {
92
        $serviceMock = $this->getPartlyMockedNameSchemaService(array('resolve'));
93
94
        $content = $this->buildTestContentObject();
95
        $contentType = $this->buildTestContentType();
96
97
        $serviceMock->expects(
98
            $this->once()
99
        )->method(
100
            'resolve'
101
        )->with(
102
            '<name_schema>',
103
            $this->equalTo($contentType),
104
            $this->equalTo($content->fields),
105
            $this->equalTo($content->versionInfo->languageCodes)
106
        )->will(
107
            $this->returnValue(42)
108
        );
109
110
        $result = $serviceMock->resolveNameSchema($content, array(), array(), $contentType);
111
112
        self::assertEquals(42, $result);
113
    }
114
115
    /**
116
     * Test eZ\Publish\Core\Repository\Helper\NameSchemaService method.
117
     *
118
     * @covers \eZ\Publish\Core\Repository\Helper\NameSchemaService::resolveNameSchema
119
     */
120
    public function testResolveNameSchemaWithFields()
121
    {
122
        $serviceMock = $this->getPartlyMockedNameSchemaService(array('resolve'));
123
124
        $content = $this->buildTestContentObject();
125
        $contentType = $this->buildTestContentType();
126
127
        $fields = array();
128
        $fields['text3']['cro-HR'] = new TextLineValue('tri');
129
        $fields['text1']['ger-DE'] = new TextLineValue('ein');
130
        $fields['text2']['ger-DE'] = new TextLineValue('zwei');
131
        $fields['text3']['ger-DE'] = new TextLineValue('drei');
132
        $mergedFields = $fields;
133
        $mergedFields['text1']['cro-HR'] = new TextLineValue('jedan');
134
        $mergedFields['text2']['cro-HR'] = new TextLineValue('dva');
135
        $mergedFields['text1']['eng-GB'] = new TextLineValue('one');
136
        $mergedFields['text2']['eng-GB'] = new TextLineValue('two');
137
        $mergedFields['text3']['eng-GB'] = new TextLineValue('');
138
        $languages = array('eng-GB', 'cro-HR', 'ger-DE');
139
140
        $serviceMock->expects(
141
            $this->once()
142
        )->method(
143
            'resolve'
144
        )->with(
145
            '<name_schema>',
146
            $this->equalTo($contentType),
147
            $this->equalTo($mergedFields),
148
            $this->equalTo($languages)
149
        )->will(
150
            $this->returnValue(42)
151
        );
152
153
        $result = $serviceMock->resolveNameSchema($content, $fields, $languages, $contentType);
154
155
        self::assertEquals(42, $result);
156
    }
157
158
    /**
159
     * Test eZ\Publish\Core\Repository\Helper\NameSchemaService::resolve method.
160
     *
161
     * @covers \eZ\Publish\Core\Repository\Helper\NameSchemaService::resolve
162
     * @dataProvider \eZ\Publish\Core\Repository\Tests\Service\Mock\NameSchemaTest::resolveDataProvider
163
     * @param string[] $schemaIdentifiers
164
     * @param string $nameSchema
165
     * @param string[] $languageFieldValues field value translations
166
     * @param string[] $fieldTitles [language => [field_identifier => title]]
167
     * @param array $settings NameSchemaService settings
168
     */
169
    public function testResolve(
170
        array $schemaIdentifiers,
171
        $nameSchema,
172
        $languageFieldValues,
173
        $fieldTitles,
174
        $settings = []
175
    ) {
176
        $serviceMock = $this->getPartlyMockedNameSchemaService(['getFieldTitles'], $settings);
177
178
        $content = $this->buildTestContentObject();
179
        $contentType = $this->buildTestContentType();
180
181
        $index = 0;
182
        foreach ($languageFieldValues as $languageCode => $fieldValue) {
183
            $serviceMock->expects(
184
                $this->at($index++)
185
            )->method(
186
                'getFieldTitles'
187
            )->with(
188
                $schemaIdentifiers,
189
                $contentType,
190
                $content->fields,
191
                $languageCode
192
            )->will(
193
                $this->returnValue($fieldTitles[$languageCode])
194
            );
195
        }
196
197
        $result = $serviceMock->resolve($nameSchema, $contentType, $content->fields, $content->versionInfo->languageCodes);
198
199
        self::assertEquals($languageFieldValues, $result);
200
    }
201
202
    /**
203
     * Data provider for the @see testResolve method.
204
     *
205
     * @return array
206
     */
207
    public function resolveDataProvider()
208
    {
209
        return [
210
            [
211
                ['text1'],
212
                '<text1>',
213
                [
214
                    'eng-GB' => 'one',
215
                    'cro-HR' => 'jedan',
216
                ],
217
                [
218
                    'eng-GB' => ['text1' => 'one'],
219
                    'cro-HR' => ['text1' => 'jedan'],
220
                ],
221
            ],
222
            [
223
                ['text2'],
224
                '<text2>',
225
                [
226
                    'eng-GB' => 'two',
227
                    'cro-HR' => 'dva',
228
                ],
229
                [
230
                    'eng-GB' => ['text2' => 'two'],
231
                    'cro-HR' => ['text2' => 'dva'],
232
                ],
233
            ],
234
            [
235
                ['text1', 'text2'],
236
                'Hello, <text1> and <text2> and then goodbye and hello again',
237
                [
238
                    'eng-GB' => 'Hello, one and two and then goodbye...',
239
                    'cro-HR' => 'Hello, jedan and dva and then goodb...',
240
                ],
241
                [
242
                    'eng-GB' => ['text1' => 'one', 'text2' => 'two'],
243
                    'cro-HR' => ['text1' => 'jedan', 'text2' => 'dva'],
244
                ],
245
                [
246
                    'limit' => 38,
247
                    'sequence' => '...',
248
                ],
249
            ],
250
        ];
251
    }
252
253
    /**
254
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
255
     */
256
    protected function getFields()
257
    {
258
        return array(
259
            new Field(
260
                array(
261
                    'languageCode' => 'eng-GB',
262
                    'fieldDefIdentifier' => 'text1',
263
                    'value' => new TextLineValue('one'),
264
                )
265
            ),
266
            new Field(
267
                array(
268
                    'languageCode' => 'eng-GB',
269
                    'fieldDefIdentifier' => 'text2',
270
                    'value' => new TextLineValue('two'),
271
                )
272
            ),
273
            new Field(
274
                array(
275
                    'languageCode' => 'eng-GB',
276
                    'fieldDefIdentifier' => 'text3',
277
                    'value' => new TextLineValue(''),
278
                )
279
            ),
280
            new Field(
281
                array(
282
                    'languageCode' => 'cro-HR',
283
                    'fieldDefIdentifier' => 'text1',
284
                    'value' => new TextLineValue('jedan'),
285
                )
286
            ),
287
            new Field(
288
                array(
289
                    'languageCode' => 'cro-HR',
290
                    'fieldDefIdentifier' => 'text2',
291
                    'value' => new TextLineValue('dva'),
292
                )
293
            ),
294
            new Field(
295
                array(
296
                    'languageCode' => 'cro-HR',
297
                    'fieldDefIdentifier' => 'text3',
298
                    'value' => new TextLineValue(''),
299
                )
300
            ),
301
        );
302
    }
303
304
    /**
305
     * @return \eZ\Publish\Core\Repository\Values\ContentType\FieldDefinition[]
306
     */
307 View Code Duplication
    protected function getFieldDefinitions()
308
    {
309
        return array(
310
            new FieldDefinition(
311
                array(
312
                    'id' => '1',
313
                    'identifier' => 'text1',
314
                    'fieldTypeIdentifier' => 'ezstring',
315
                )
316
            ),
317
            new FieldDefinition(
318
                array(
319
                    'id' => '2',
320
                    'identifier' => 'text2',
321
                    'fieldTypeIdentifier' => 'ezstring',
322
                )
323
            ),
324
            new FieldDefinition(
325
                array(
326
                    'id' => '3',
327
                    'identifier' => 'text3',
328
                    'fieldTypeIdentifier' => 'ezstring',
329
                )
330
            ),
331
        );
332
    }
333
334
    /**
335
     * Build Content Object stub for testing purpose.
336
     *
337
     * @return \eZ\Publish\API\Repository\Values\Content\Content
338
     */
339
    protected function buildTestContentObject()
340
    {
341
        return new Content(
342
            [
343
                'internalFields' => $this->getFields(),
344
                'versionInfo' => new VersionInfo(
345
                    [
346
                        'languageCodes' => ['eng-GB', 'cro-HR'],
347
                    ]
348
                ),
349
            ]
350
        );
351
    }
352
353
    /**
354
     * Build ContentType stub for testing purpose.
355
     *
356
     * @param string $nameSchema
357
     * @param string $urlAliasSchema
358
     *
359
     * @return \eZ\Publish\Core\Repository\Values\ContentType\ContentType
360
     */
361
    protected function buildTestContentType($nameSchema = '<name_schema>', $urlAliasSchema = '<urlalias_schema>')
362
    {
363
        return new ContentType(
364
            [
365
                'nameSchema' => $nameSchema,
366
                'urlAliasSchema' => $urlAliasSchema,
367
                'fieldDefinitions' => $this->getFieldDefinitions(),
368
            ]
369
        );
370
    }
371
372
    /**
373
     * Returns the content service to test with $methods mocked.
374
     *
375
     * Injected Repository comes from {@see getRepositoryMock()}
376
     *
377
     * @param string[] $methods
378
     * @param array $settings
379
     *
380
     * @return \eZ\Publish\Core\Repository\Helper\NameSchemaService|\PHPUnit_Framework_MockObject_MockObject
381
     */
382
    protected function getPartlyMockedNameSchemaService(array $methods = null, array $settings = [])
383
    {
384
        return $this->getMock(
385
            NameSchemaService::class,
386
            $methods,
387
            [
388
                $this->getPersistenceMock()->contentTypeHandler(),
389
                $this->getContentTypeDomainMapperMock(),
390
                $this->getNameableFieldTypeRegistryMock(),
391
                $settings,
392
            ]
393
        );
394
    }
395
396
    protected $contentTypeDomainMapperMock;
397
398
    /**
399
     * @return \PHPUnit_Framework_MockObject_MockObject|\eZ\Publish\Core\Repository\Helper\ContentTypeDomainMapper
400
     */
401
    protected function getContentTypeDomainMapperMock()
402
    {
403
        if (!isset($this->contentTypeDomainMapperMock)) {
404
            $this->contentTypeDomainMapperMock = $this
405
                ->getMockBuilder('eZ\\Publish\\Core\\Repository\\Helper\\ContentTypeDomainMapper')
406
                ->disableOriginalConstructor()
407
                ->getMock();
408
        }
409
410
        return $this->contentTypeDomainMapperMock;
411
    }
412
}
413