1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Helper\CacheCleaner;
use PHPUnit_Framework_MockObject_MockObject as MockObject;
/**
* @magentoAppIsolation enabled
* @magentoCache all disabled
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class TranslateTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Framework\Translate
*/
private $translate;
/**
* @inheritdoc
*/
protected function setUp()
{
/** @var \Magento\Framework\View\FileSystem|MockObject $viewFileSystem */
$viewFileSystem = $this->createPartialMock(
\Magento\Framework\View\FileSystem::class,
['getLocaleFileName']
);
$viewFileSystem->expects($this->any())
->method('getLocaleFileName')
->will(
$this->returnValue(
dirname(__DIR__) . '/Translation/Model/_files/Magento/design/Magento/theme/i18n/en_US.csv'
)
);
/** @var \Magento\Framework\View\Design\ThemeInterface|MockObject $theme */
$theme = $this->createMock(\Magento\Framework\View\Design\ThemeInterface::class);
$theme->expects($this->any())->method('getThemePath')->will($this->returnValue('Magento/luma'));
/** @var \Magento\TestFramework\ObjectManager $objectManager */
$objectManager = Bootstrap::getObjectManager();
$objectManager->addSharedInstance($viewFileSystem, \Magento\Framework\View\FileSystem::class);
/** @var $moduleReader \Magento\Framework\Module\Dir\Reader */
$moduleReader = $objectManager->get(\Magento\Framework\Module\Dir\Reader::class);
$moduleReader->setModuleDir(
'Magento_Store',
'i18n',
dirname(__DIR__) . '/Translation/Model/_files/Magento/Store/i18n'
);
$moduleReader->setModuleDir(
'Magento_Catalog',
'i18n',
dirname(__DIR__) . '/Translation/Model/_files/Magento/Catalog/i18n'
);
/** @var \Magento\Theme\Model\View\Design|MockObject $designModel */
$designModel = $this->getMockBuilder(\Magento\Theme\Model\View\Design::class)
->setMethods(['getDesignTheme'])
->setConstructorArgs(
[
$objectManager->get(\Magento\Store\Model\StoreManagerInterface::class),
$objectManager->get(\Magento\Framework\View\Design\Theme\FlyweightFactory::class),
$objectManager->get(\Magento\Framework\App\Config\ScopeConfigInterface::class),
$objectManager->get(\Magento\Theme\Model\ThemeFactory::class),
$objectManager->get(\Magento\Framework\ObjectManagerInterface::class),
$objectManager->get(\Magento\Framework\App\State::class),
['frontend' => 'Test/default']
]
)
->getMock();
$designModel->expects($this->any())->method('getDesignTheme')->willReturn($theme);
$objectManager->addSharedInstance($designModel, \Magento\Theme\Model\View\Design\Proxy::class);
$this->translate = $objectManager->create(\Magento\Framework\Translate::class);
$objectManager->addSharedInstance($this->translate, \Magento\Framework\Translate::class);
$objectManager->removeSharedInstance(\Magento\Framework\Phrase\Renderer\Composite::class);
$objectManager->removeSharedInstance(\Magento\Framework\Phrase\Renderer\Translate::class);
\Magento\Framework\Phrase::setRenderer(
$objectManager->get(\Magento\Framework\Phrase\RendererInterface::class)
);
}
public function testLoadData()
{
$data = $this->translate->loadData(null, true)->getData();
CacheCleaner::cleanAll();
$this->translate->loadData()->getData();
$dataCached = $this->translate->loadData()->getData();
$this->assertEquals($data, $dataCached);
}
/**
* @magentoCache all disabled
* @dataProvider translateDataProvider
*
* @param string $inputText
* @param string $expectedTranslation
* @return void
* @throws Exception\LocalizedException
*/
public function testTranslate($inputText, $expectedTranslation)
{
$this->translate->loadData(\Magento\Framework\App\Area::AREA_FRONTEND);
$actualTranslation = new \Magento\Framework\Phrase($inputText);
$this->assertEquals($expectedTranslation, $actualTranslation);
}
/**
* @return array
*/
public function translateDataProvider()
{
return [
['', ''],
[
'Theme phrase will be translated',
'Theme phrase is translated',
],
[
'Phrase in Magento_Store module that doesn\'t need translation',
'Phrase in Magento_Store module that doesn\'t need translation',
],
[
'Phrase in Magento_Catalog module that doesn\'t need translation',
'Phrase in Magento_Catalog module that doesn\'t need translation',
],
[
'Magento_Store module phrase will be overridden by theme translation',
'Magento_Store module phrase is overridden by theme translation',
],
[
'Magento_Catalog module phrase will be overridden by theme translation',
'Magento_Catalog module phrase is overridden by theme translation',
],
];
}
}