Completed
Push — avoid-loading-full-content ( 3e4567 )
by
unknown
14:31
created

TranslationHelperTest::getTranslatedField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 2
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the ContentHelper 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\Helper\Tests;
10
11
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
12
use eZ\Publish\API\Repository\Values\Content\Field;
13
use eZ\Publish\API\Repository\Values\Content\VersionInfo as APIVersionInfo;
14
use eZ\Publish\Core\Repository\Values\Content\Content;
15
use eZ\Publish\Core\Repository\Values\Content\VersionInfo;
16
use PHPUnit_Framework_TestCase;
17
use eZ\Publish\Core\Helper\TranslationHelper;
18
19
class TranslationHelperTest extends PHPUnit_Framework_TestCase
20
{
21
    /**
22
     * @var \PHPUnit_Framework_MockObject_MockObject|\eZ\Publish\Core\MVC\ConfigResolverInterface
23
     */
24
    private $configResolver;
25
26
    /**
27
     * @var \PHPUnit_Framework_MockObject_MockObject|\eZ\Publish\API\Repository\ContentService
28
     */
29
    private $contentService;
30
31
    /**
32
     * @var \PHPUnit_Framework_MockObject_MockObject
33
     */
34
    private $logger;
35
36
    /**
37
     * @var \eZ\Publish\Core\Helper\TranslationHelper
38
     */
39
    private $translationHelper;
40
41
    private $siteAccessByLanguages;
42
43
    /**
44
     * @var Field[]
45
     */
46
    private $translatedFields;
47
48
    /**
49
     * @var string[]
50
     */
51
    private $translatedNames;
52
53
    protected function setUp()
54
    {
55
        parent::setUp();
56
        $this->configResolver = $this->getMock('eZ\\Publish\\Core\\MVC\\ConfigResolverInterface');
57
        $this->contentService = $this->getMock('eZ\\Publish\\API\\Repository\\ContentService');
58
        $this->logger = $this->getMock('Psr\Log\LoggerInterface');
59
        $this->siteAccessByLanguages = array(
60
            'fre-FR' => array('fre'),
61
            'eng-GB' => array('my_siteaccess', 'eng'),
62
            'esl-ES' => array('esl', 'mex'),
63
            'heb-IL' => array('heb'),
64
        );
65
        $this->translationHelper = new TranslationHelper(
66
            $this->configResolver,
67
            $this->contentService,
68
            $this->siteAccessByLanguages,
69
            $this->logger
70
        );
71
        $this->translatedNames = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('eng-GB' => 'My na... of type array<string,string,{"en...ng","heb-IL":"string"}> is incompatible with the declared type array<integer,string> of property $translatedNames.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
72
            'eng-GB' => 'My name in english',
73
            'fre-FR' => 'Mon nom en français',
74
            'esl-ES' => 'Mi nombre en español',
75
            'heb-IL' => 'השם שלי בעברית',
76
        );
77
        $this->translatedFields = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('eng-GB' => new \e...ageCode' => 'heb-IL'))) of type array<string,object<eZ\P...ues\\Content\\Field>"}> is incompatible with the declared type array<integer,object<eZ\...\Values\Content\Field>> of property $translatedFields.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
78
            'eng-GB' => new Field(array('value' => 'Content in english', 'fieldDefIdentifier' => 'test', 'languageCode' => 'eng-GB')),
79
            'fre-FR' => new Field(array('value' => 'Contenu en français', 'fieldDefIdentifier' => 'test', 'languageCode' => 'fre-FR')),
80
            'esl-ES' => new Field(array('value' => 'Contenido en español', 'fieldDefIdentifier' => 'test', 'languageCode' => 'esl-ES')),
81
            'heb-IL' => new Field(array('value' => 'תוכן בספרדית', 'fieldDefIdentifier' => 'test', 'languageCode' => 'heb-IL')),
82
        );
83
    }
84
85
    /**
86
     * @return Content
87
     */
88
    private function generateContent()
89
    {
90
        return new Content(
91
            array(
92
                'versionInfo' => $this->generateVersionInfo(),
93
                'internalFields' => $this->translatedFields,
94
            )
95
        );
96
    }
97
98
    /**
99
     * @return APIVersionInfo
100
     */
101
    private function generateVersionInfo()
102
    {
103
        return new VersionInfo(
104
            [
105
                'names' => $this->translatedNames,
106
                'initialLanguageCode' => 'fre-FR',
107
            ]
108
        );
109
    }
110
111
    /**
112
     * @dataProvider getTranslatedNameProvider
113
     *
114
     * @param array $prioritizedLanguages
115
     * @param string $expectedLocale
116
     */
117
    public function testGetTranslatedName(array $prioritizedLanguages, $expectedLocale)
118
    {
119
        $content = $this->generateContent();
120
        $this->configResolver
121
            ->expects($this->once())
122
            ->method('getParameter')
123
            ->with('languages')
124
            ->will($this->returnValue($prioritizedLanguages));
125
126
        $this->assertSame($this->translatedNames[$expectedLocale], $this->translationHelper->getTranslatedContentName($content));
127
    }
128
129
    /**
130
     * @dataProvider getTranslatedNameProvider
131
     *
132
     * @param array $prioritizedLanguages
133
     * @param string $expectedLocale
134
     */
135
    public function testGetTranslatedNameByContentInfo(array $prioritizedLanguages, $expectedLocale)
136
    {
137
        $versionInfo = $this->generateVersionInfo();
138
        $contentInfo = new ContentInfo(array('id' => 123));
139
        $this->configResolver
140
            ->expects($this->once())
141
            ->method('getParameter')
142
            ->with('languages')
143
            ->will($this->returnValue($prioritizedLanguages));
144
145
        $this->contentService
146
            ->expects($this->once())
147
            ->method('loadVersionInfo')
148
            ->with($contentInfo)
149
            ->will($this->returnValue($versionInfo));
150
151
        $this->assertSame($this->translatedNames[$expectedLocale], $this->translationHelper->getTranslatedContentNameByContentInfo($contentInfo));
152
    }
153
154 View Code Duplication
    public function getTranslatedNameProvider()
155
    {
156
        return array(
157
            array(array('fre-FR', 'eng-GB'), 'fre-FR'),
158
            array(array('esl-ES', 'fre-FR'), 'esl-ES'),
159
            array(array('eng-US', 'heb-IL'), 'heb-IL'),
160
            array(array('eng-US', 'eng-GB'), 'eng-GB'),
161
            array(array('eng-US', 'ger-DE'), 'fre-FR'),
162
        );
163
    }
164
165
    public function testGetTranslatedNameByContentInfoForcedLanguage()
166
    {
167
        $versionInfo = $this->generateVersionInfo();
168
        $contentInfo = new ContentInfo(array('id' => 123));
169
        $this->configResolver
170
            ->expects($this->never())
171
            ->method('getParameter');
172
173
        $this->contentService
174
            ->expects($this->exactly(2))
175
            ->method('loadVersionInfo')
176
            ->with($contentInfo)
177
            ->will($this->returnValue($versionInfo));
178
179
        $this->assertSame('My name in english', $this->translationHelper->getTranslatedContentNameByContentInfo($contentInfo, 'eng-GB'));
180
        $this->assertSame('Mon nom en français', $this->translationHelper->getTranslatedContentNameByContentInfo($contentInfo, 'eng-US'));
181
    }
182
183
    public function testGetTranslatedNameByContentInfoForcedLanguageMainLanguage()
184
    {
185
        $name = 'Name in main language';
186
        $mainLanguage = 'eng-GB';
187
        $contentInfo = new ContentInfo(
188
            array(
189
                'id' => 123,
190
                'mainLanguageCode' => $mainLanguage,
191
                'name' => $name,
192
            )
193
        );
194
        $this->configResolver
195
            ->expects($this->never())
196
            ->method('getParameter');
197
198
        $this->contentService
199
            ->expects($this->never())
200
            ->method('loadContentByContentInfo');
201
202
        $this->assertSame(
203
            $name,
204
            $this->translationHelper->getTranslatedContentNameByContentInfo($contentInfo, $mainLanguage)
205
        );
206
    }
207
208 View Code Duplication
    public function testGetTranslatedNameForcedLanguage()
209
    {
210
        $content = $this->generateContent();
211
        $this->configResolver
212
            ->expects($this->never())
213
            ->method('getParameter');
214
215
        $this->assertSame('My name in english', $this->translationHelper->getTranslatedContentName($content, 'eng-GB'));
216
        $this->assertSame('Mon nom en français', $this->translationHelper->getTranslatedContentName($content, 'eng-US'));
217
    }
218
219
    /**
220
     * @dataProvider getTranslatedFieldProvider
221
     *
222
     * @param array $prioritizedLanguages
223
     * @param string $expectedLocale
224
     */
225 View Code Duplication
    public function getTranslatedField(array $prioritizedLanguages, $expectedLocale)
226
    {
227
        $content = $this->generateContent();
228
        $this->configResolver
229
            ->expects($this->once())
230
            ->method('getParameter')
231
            ->with('languages')
232
            ->will($this->returnValue($prioritizedLanguages));
233
234
        $this->assertSame($this->translatedFields[$expectedLocale], $this->translationHelper->getTranslatedField($content, 'test'));
235
    }
236
237 View Code Duplication
    public function getTranslatedFieldProvider()
238
    {
239
        return array(
240
            array(array('fre-FR', 'eng-GB'), 'fre-FR'),
241
            array(array('esl-ES', 'fre-FR'), 'esl-ES'),
242
            array(array('eng-US', 'heb-IL'), 'heb-IL'),
243
            array(array('eng-US', 'eng-GB'), 'eng-GB'),
244
            array(array('eng-US', 'ger-DE'), 'fre-FR'),
245
        );
246
    }
247
248
    public function testGetTranslationSiteAccessUnkownLanguage()
249
    {
250
        $this->configResolver
251
            ->expects($this->exactly(2))
252
            ->method('getParameter')
253
            ->will(
254
                $this->returnValueMap(
255
                    array(
256
                        array('translation_siteaccesses', null, null, array()),
257
                        array('related_siteaccesses', null, null, array()),
258
                    )
259
                )
260
            );
261
262
        $this->logger
263
            ->expects($this->once())
264
            ->method('error');
265
266
        $this->assertNull($this->translationHelper->getTranslationSiteAccess('eng-DE'));
267
    }
268
269
    /**
270
     * @dataProvider getTranslationSiteAccessProvider
271
     */
272
    public function testGetTranslationSiteAccess($language, array $translationSiteAccesses, array $relatedSiteAccesses, $expectedResult)
273
    {
274
        $this->configResolver
275
            ->expects($this->exactly(2))
276
            ->method('getParameter')
277
            ->will(
278
                $this->returnValueMap(
279
                    array(
280
                        array('translation_siteaccesses', null, null, $translationSiteAccesses),
281
                        array('related_siteaccesses', null, null, $relatedSiteAccesses),
282
                    )
283
                )
284
            );
285
286
        $this->assertSame($expectedResult, $this->translationHelper->getTranslationSiteAccess($language));
287
    }
288
289
    public function getTranslationSiteAccessProvider()
290
    {
291
        return array(
292
            array('eng-GB', array('fre', 'eng', 'heb'), array('esl', 'fre', 'eng', 'heb'), 'eng'),
293
            array('eng-GB', array(), array('esl', 'fre', 'eng', 'heb'), 'eng'),
294
            array('eng-GB', array(), array('esl', 'fre', 'eng', 'heb', 'my_siteaccess'), 'my_siteaccess'),
295
            array('eng-GB', array('esl', 'fre', 'eng', 'heb', 'my_siteaccess'), array(), 'my_siteaccess'),
296
            array('eng-GB', array('esl', 'fre', 'eng', 'heb'), array(), 'eng'),
297
            array('fre-FR', array('esl', 'fre', 'eng', 'heb'), array(), 'fre'),
298
            array('fre-FR', array('esl', 'eng', 'heb'), array(), null),
299
            array('heb-IL', array('esl', 'eng'), array(), null),
300
            array('esl-ES', array(), array('esl', 'mex', 'fre', 'eng', 'heb'), 'esl'),
301
            array('esl-ES', array(), array('mex', 'fre', 'eng', 'heb'), 'mex'),
302
            array('esl-ES', array('esl', 'mex', 'fre', 'eng', 'heb'), array(), 'esl'),
303
            array('esl-ES', array('mex', 'fre', 'eng', 'heb'), array(), 'mex'),
304
        );
305
    }
306
307
    public function testGetAvailableLanguagesWithTranslationSiteAccesses()
308
    {
309
        $this->configResolver
310
            ->expects($this->any())
311
            ->method('getParameter')
312
            ->will(
313
                $this->returnValueMap(
314
                    array(
315
                        array('translation_siteaccesses', null, null, array('fre', 'esl')),
316
                        array('related_siteaccesses', null, null, array('fre', 'esl', 'heb')),
317
                        array('languages', null, null, array('eng-GB')),
318
                        array('languages', null, 'fre', array('fre-FR', 'eng-GB')),
319
                        array('languages', null, 'esl', array('esl-ES', 'fre-FR', 'eng-GB')),
320
                    )
321
                )
322
            );
323
324
        $expectedLanguages = array('eng-GB', 'esl-ES', 'fre-FR');
325
        $this->assertSame($expectedLanguages, $this->translationHelper->getAvailableLanguages());
326
    }
327
328
    public function testGetAvailableLanguagesWithoutTranslationSiteAccesses()
329
    {
330
        $this->configResolver
331
            ->expects($this->any())
332
            ->method('getParameter')
333
            ->will(
334
                $this->returnValueMap(
335
                    array(
336
                        array('translation_siteaccesses', null, null, array()),
337
                        array('related_siteaccesses', null, null, array('fre', 'esl', 'heb')),
338
                        array('languages', null, null, array('eng-GB')),
339
                        array('languages', null, 'fre', array('fre-FR', 'eng-GB')),
340
                        array('languages', null, 'esl', array('esl-ES', 'fre-FR', 'eng-GB')),
341
                        array('languages', null, 'heb', array('heb-IL', 'eng-GB')),
342
                    )
343
                )
344
            );
345
346
        $expectedLanguages = array('eng-GB', 'esl-ES', 'fre-FR', 'heb-IL');
347
        $this->assertSame($expectedLanguages, $this->translationHelper->getAvailableLanguages());
348
    }
349
}
350