Skip to content

Commit 4bc66ec

Browse files
ENGCOM-6319: Fix translation retrieval #25626
- Merge Pull Request #25626 from brosenberger/magento2:fix-translation-retrieval - Merged commits: 1. 7fff055 2. 00b2ff5 3. 39b2558 4. 9a7dc51 5. 7b36981 6. b40d2b4 7. aaa9e01 8. 50be59c 9. 86a3efa
2 parents a25c107 + 86a3efa commit 4bc66ec

File tree

2 files changed

+96
-71
lines changed

2 files changed

+96
-71
lines changed

app/code/Magento/Translation/Test/Unit/Model/Js/DataProviderTest.php

Lines changed: 95 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -3,89 +3,99 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\Translation\Test\Unit\Model\Js;
89

910
use Magento\Framework\App\State;
1011
use Magento\Framework\App\Utility\Files;
11-
use Magento\Framework\Filesystem;
12+
use Magento\Framework\Component\ComponentRegistrar;
13+
use Magento\Framework\Component\DirSearch;
14+
use Magento\Framework\Filesystem\File\Read;
15+
use Magento\Framework\Filesystem\File\ReadFactory;
1216
use Magento\Framework\Filesystem\File\ReadInterface;
13-
use Magento\Translation\Model\Js\DataProvider;
14-
use Magento\Translation\Model\Js\Config;
1517
use Magento\Framework\Phrase\Renderer\Translate;
18+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
19+
use Magento\Translation\Model\Js\Config;
20+
use Magento\Translation\Model\Js\DataProvider as ModelDataProvider;
21+
use PHPUnit\Framework\TestCase;
22+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
1623

1724
/**
1825
* Verify data provider translation
1926
*
2027
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2128
*/
22-
class DataProviderTest extends \PHPUnit\Framework\TestCase
29+
class DataProviderTest extends TestCase
2330
{
2431
/**
25-
* @var DataProvider
32+
* @var ModelDataProvider
2633
*/
27-
protected $model;
34+
private $model;
2835

2936
/**
30-
* @var State|\PHPUnit_Framework_MockObject_MockObject
37+
* @var State|MockObject
3138
*/
32-
protected $appStateMock;
39+
private $appStateMock;
3340

3441
/**
35-
* @var Config|\PHPUnit_Framework_MockObject_MockObject
42+
* @var Config|MockObject
3643
*/
37-
protected $configMock;
44+
private $configMock;
3845

3946
/**
40-
* @var Files|\PHPUnit_Framework_MockObject_MockObject
47+
* @var Files|MockObject
4148
*/
42-
protected $filesUtilityMock;
49+
private $filesUtilityMock;
4350

4451
/**
45-
* @var ReadInterface|\PHPUnit_Framework_MockObject_MockObject
52+
* @var ReadInterface|MockObject
4653
*/
47-
protected $fileReadMock;
54+
private $fileReadMock;
4855

4956
/**
50-
* @var Translate|\PHPUnit_Framework_MockObject_MockObject
57+
* @var Translate|MockObject
5158
*/
52-
protected $translateMock;
59+
private $translateMock;
5360

5461
/**
55-
* @return void
62+
* @inheritDoc
5663
*/
5764
protected function setUp()
5865
{
59-
$this->appStateMock = $this->createMock(\Magento\Framework\App\State::class);
60-
$this->configMock = $this->createMock(\Magento\Translation\Model\Js\Config::class);
61-
$this->filesUtilityMock = $this->createMock(\Magento\Framework\App\Utility\Files::class);
62-
$fileReadFactory = $this->createMock(\Magento\Framework\Filesystem\File\ReadFactory::class);
63-
$this->fileReadMock = $this->createMock(\Magento\Framework\Filesystem\File\Read::class);
64-
$this->translateMock = $this->createMock(\Magento\Framework\Phrase\Renderer\Translate::class);
66+
$this->appStateMock = $this->createMock(State::class);
67+
$this->configMock = $this->createMock(Config::class);
68+
$this->filesUtilityMock = $this->createMock(Files::class);
69+
$fileReadFactory = $this->createMock(ReadFactory::class);
70+
$this->fileReadMock = $this->createMock(Read::class);
71+
$this->translateMock = $this->createMock(Translate::class);
6572
$fileReadFactory->expects($this->atLeastOnce())
6673
->method('create')
6774
->willReturn($this->fileReadMock);
68-
$dirSearch = $this->createMock(\Magento\Framework\Component\DirSearch::class);
69-
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
75+
$dirSearch = $this->createMock(DirSearch::class);
76+
$objectManager = new ObjectManager($this);
7077
$this->model = $objectManager->getObject(
71-
\Magento\Translation\Model\Js\DataProvider::class,
78+
ModelDataProvider::class,
7279
[
7380
'appState' => $this->appStateMock,
7481
'config' => $this->configMock,
7582
'fileReadFactory' => $fileReadFactory,
7683
'translate' => $this->translateMock,
7784
'dirSearch' => $dirSearch,
7885
'filesUtility' => $this->filesUtilityMock,
79-
'componentRegistrar' =>
80-
$this->createMock(\Magento\Framework\Component\ComponentRegistrar::class)
86+
'componentRegistrar' => $this->createMock(ComponentRegistrar::class)
8187
]
8288
);
8389
}
8490

8591
/**
92+
* Verify data translate.
93+
*
94+
* @param array $config
8695
* @return void
96+
* @dataProvider configDataProvider
8797
*/
88-
public function testGetData()
98+
public function testGetData(array $config): void
8999
{
90100
$themePath = 'blank';
91101
$areaCode = 'adminhtml';
@@ -101,36 +111,6 @@ public function testGetData()
101111
[$areaCode, $themePath, '*', '*', [$filePaths[3]]]
102112
];
103113

104-
$expectedResult = [
105-
'hello1' => 'hello1translated',
106-
'hello2' => 'hello2translated',
107-
'hello3' => 'hello3translated',
108-
'hello4' => 'hello4translated',
109-
'ko i18' => 'ko i18 translated',
110-
'underscore i18' => 'underscore i18 translated',
111-
];
112-
113-
$contentsMap = [
114-
'content1$.mage.__("hello1")content1',
115-
'content2$.mage.__("hello2")content2',
116-
'content2$.mage.__("hello4")content4 <!-- ko i18n: "ko i18" --><!-- /ko -->',
117-
'content2$.mage.__("hello3")content3 <% _.i18n("underscore i18") %>',
118-
];
119-
120-
$translateMap = [
121-
[['hello1'], [], 'hello1translated'],
122-
[['hello2'], [], 'hello2translated'],
123-
[['hello3'], [], 'hello3translated'],
124-
[['hello4'], [], 'hello4translated'],
125-
[['ko i18'], [], 'ko i18 translated'],
126-
[['underscore i18'], [], 'underscore i18 translated'],
127-
];
128-
129-
$patterns = [
130-
'~\$\.mage\.__\(([\'"])(.+?)\1\)~',
131-
'~(?:i18n\:|_\.i18n\()\s*(["\'])(.*?)(?<!\\\\)\1~',
132-
];
133-
134114
$this->appStateMock->expects($this->once())
135115
->method('getAreaCode')
136116
->willReturn($areaCode);
@@ -141,42 +121,45 @@ public function testGetData()
141121
->method('getStaticHtmlFiles')
142122
->willReturnMap($staticFilesMap);
143123

144-
foreach ($contentsMap as $index => $content) {
124+
foreach ($config['contentsMap'] as $index => $content) {
145125
$this->fileReadMock->expects($this->at($index))
146126
->method('readAll')
147127
->willReturn($content);
148128
}
149129

150130
$this->configMock->expects($this->any())
151131
->method('getPatterns')
152-
->willReturn($patterns);
132+
->willReturn($config['patterns']);
153133
$this->translateMock->expects($this->any())
154134
->method('render')
155-
->willReturnMap($translateMap);
135+
->willReturnMap($config['translateMap']);
156136

157137
$actualResult = $this->model->getData($themePath);
158-
$this->assertEquals($expectedResult, $actualResult);
138+
$this->assertEquals($config['expectedResult'], $actualResult);
159139
$this->assertEquals(
160-
json_encode($expectedResult),
140+
json_encode($config['expectedResult']),
161141
json_encode($actualResult),
162142
"Translations should be sorted by key"
163143
);
164144
}
165145

166146
/**
147+
* Verify get data throwing exception.
148+
*
149+
* @param array $config
150+
* @return void
151+
* @dataProvider configDataProvider
167152
* @expectedException \Magento\Framework\Exception\LocalizedException
168153
*/
169-
public function testGetDataThrowingException()
154+
public function testGetDataThrowingException(array $config): void
170155
{
171156
$themePath = 'blank';
172157
$areaCode = 'adminhtml';
173-
174-
$patterns = ['~\$\.mage\.__\(([\'"])(.+?)\1\)~'];
158+
$patterns = $config['patterns'];
175159

176160
$this->fileReadMock->expects($this->once())
177161
->method('readAll')
178162
->willReturn('content1$.mage.__("hello1")content1');
179-
180163
$this->appStateMock->expects($this->once())
181164
->method('getAreaCode')
182165
->willReturn($areaCode);
@@ -186,15 +169,57 @@ public function testGetDataThrowingException()
186169
$this->filesUtilityMock->expects($this->any())
187170
->method('getStaticHtmlFiles')
188171
->willReturn(['test']);
189-
190172
$this->configMock->expects($this->any())
191173
->method('getPatterns')
192174
->willReturn($patterns);
193-
194175
$this->translateMock->expects($this->once())
195176
->method('render')
196177
->willThrowException(new \Exception('Test exception'));
197178

198179
$this->model->getData($themePath);
199180
}
181+
182+
/**
183+
* Config data provider.
184+
*
185+
* @return array
186+
*/
187+
public function configDataProvider(): array
188+
{
189+
return [
190+
[
191+
[
192+
'patterns' => [
193+
'~\$\.mage\.__\(([\'"])(.+?)\1\)~',
194+
'~(?:i18n\:|_\.i18n\()\s*(["\'])(.*?)(?<!\\\\)\1~',
195+
'~translate\=("\')([^\'].*?)\'\"~',
196+
'~(?s)\$t\(\s*([\'"])(\?\<translate\>.+?)(?<!\\\)\1\s*(*SKIP)\)(?s)~',
197+
'~translate args\=("|\'|"\'|\\\"\')([^\'].*?)(\'\\\"|\'"|\'|")~',
198+
],
199+
'expectedResult' => [
200+
'hello1' => 'hello1translated',
201+
'hello2' => 'hello2translated',
202+
'hello3' => 'hello3translated',
203+
'hello4' => 'hello4translated',
204+
'ko i18' => 'ko i18 translated',
205+
'underscore i18' => 'underscore i18 translated',
206+
],
207+
'contentsMap' => [
208+
'content1$.mage.__("hello1")content1',
209+
'content2$.mage.__("hello2")content2',
210+
'content2$.mage.__("hello4")content4 <!-- ko i18n: "ko i18" --><!-- /ko -->',
211+
'content2$.mage.__("hello3")content3 <% _.i18n("underscore i18") %>',
212+
],
213+
'translateMap' => [
214+
[['hello1'], [], 'hello1translated'],
215+
[['hello2'], [], 'hello2translated'],
216+
[['hello3'], [], 'hello3translated'],
217+
[['hello4'], [], 'hello4translated'],
218+
[['ko i18'], [], 'ko i18 translated'],
219+
[['underscore i18'], [], 'underscore i18 translated'],
220+
]
221+
],
222+
]
223+
];
224+
}
200225
}

app/code/Magento/Translation/etc/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
<item name="translate_wrapping" xsi:type="string"><![CDATA[~translate\=("')([^\'].*?)\'\"~]]></item>
6969
<item name="mage_translation_widget" xsi:type="string"><![CDATA[~(?s)(?:\$|jQuery)\.mage\.__\(\s*(['"])(?<translate>.+?)(?<!\\)\1\s*(*SKIP)\)\s*(?s)~]]></item>
7070
<item name="mage_translation_static" xsi:type="string"><![CDATA[~(?s)\$t\(\s*(['"])(?<translate>.+?)(?<!\\)\1\s*(*SKIP)\)(?s)~]]></item>
71-
<item name="translate_args" xsi:type="string"><![CDATA[~translate args\=("|'|"')([^\'].*?)('"|'|")~]]></item>
71+
<item name="translate_args" xsi:type="string"><![CDATA[~translate args\=("|'|"'|\\"')([^\'].*?)('\\"|'"|'|")~]]></item>
7272
</argument>
7373
</arguments>
7474
</type>

0 commit comments

Comments
 (0)