Skip to content

Commit 71ce98b

Browse files
MAGETWO-70599: Product Edit Page Can't Load
- Fix CR comments; - Add integration test.
1 parent 02bca98 commit 71ce98b

File tree

4 files changed

+170
-5
lines changed

4 files changed

+170
-5
lines changed

app/code/Magento/Translation/Model/Js/PreProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,6 @@ public function translate($content)
9292
*/
9393
protected function replaceCallback($matches)
9494
{
95-
return '"' . __($matches[1]) . '"';
95+
return '\'' . __($matches['translate']) . '\'';
9696
}
9797
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public function testGetData()
4646
$chain = $this->createMock(\Magento\Framework\View\Asset\PreProcessor\Chain::class);
4747
$context = $this->createMock(\Magento\Framework\View\Asset\File\FallbackContext::class);
4848
$originalContent = 'content$.mage.__("hello1")content';
49-
$translatedContent = 'content"hello1"content';
50-
$patterns = ['~\$\.mage\.__\([\'|\"](.+?)[\'|\"]\)~'];
49+
$translatedContent = 'content\'hello1\'content';
50+
$patterns = ["~(?:\\$|jQuery)\\.mage\\.__\\((?s)[^'\\\")]*?(['\\\"])(?P<translate>.+?)(?<!\\\\)\\1(?s).*?\\)~"];
5151
$areaCode = 'adminhtml';
5252
$area = $this->createMock(\Magento\Framework\App\Area::class);
5353

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,25 @@
6565
<argument name="patterns" xsi:type="array">
6666
<item name="i18n_translation" xsi:type="string"><![CDATA[~i18n\:\s*(["'])(.*?)(?<!\\)\1~]]></item>
6767
<item name="translate_wrapping" xsi:type="string"><![CDATA[~translate\=("')([^\'].*?)\'\"~]]></item>
68-
<item name="mage_translation_widget" xsi:type="string"><![CDATA[~(?:\$|jQuery)\.mage\.__\((?s)[^'"]*?(['"])(.+?)(?<!\\)\1(?s).*?\)~]]></item>
69-
<item name="mage_translation_static" xsi:type="string"><![CDATA[~\$t\((?s)[^'"]*?(["'])(.+?)\1(?s).*?\)~]]></item>
68+
<item name="mage_translation_widget" xsi:type="string"><![CDATA[~(?:\$|jQuery)\.mage\.__\((?s)[^'"]*?(['"])(?P<translate>.+?)(?<!\\)\1(?s).*?\)~]]></item>
69+
<item name="mage_translation_static" xsi:type="string"><![CDATA[~\$t\((?s)[^'")]*?(["'])(?P<translate>.+?)\1(?s).*?\)~]]></item>
7070
<item name="translate_args" xsi:type="string"><![CDATA[~translate args\=("|'|"')([^\'].*?)('"|'|")~]]></item>
7171
</argument>
7272
</arguments>
7373
</type>
74+
<virtualType name="embeddedJsConfig" type="Magento\Translation\Model\Js\Config">
75+
<arguments>
76+
<argument name="patterns" xsi:type="array">
77+
<item name="mage_translation_widget" xsi:type="string"><![CDATA[~(?:\$|jQuery)\.mage\.__\((?s)[^'")]*?(['"])(?P<translate>.+?)(?<!\\)\1(?s).*?\)~]]></item>
78+
<item name="mage_translation_static" xsi:type="string"><![CDATA[~\$t\((?s)[^'")]*?(["'])(?P<translate>.+?)\1(?s).*?\)~]]></item>
79+
</argument>
80+
</arguments>
81+
</virtualType>
82+
<type name="Magento\Translation\Model\Js\PreProcessor">
83+
<arguments>
84+
<argument name="config" xsi:type="object">embeddedJsConfig</argument>
85+
</arguments>
86+
</type>
7487
<virtualType name="AssetPreProcessorPool">
7588
<arguments>
7689
<argument name="preprocessors" xsi:type="array">
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Translation\Model\Js;
9+
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
use Magento\Framework\View\Asset\PreProcessor\Chain;
12+
use Magento\Framework\View\Asset\LocalInterface;
13+
use Magento\Framework\View\Asset\File\FallbackContext;
14+
use Magento\Framework\View\FileSystem;
15+
use Magento\TestFramework\Helper\CacheCleaner;
16+
use Magento\Framework\Translate;
17+
18+
/**
19+
* Class for testing translation.
20+
*/
21+
class PreProcessorTest extends \PHPUnit\Framework\TestCase
22+
{
23+
/**
24+
* @var PreProcessor
25+
*/
26+
private $model;
27+
28+
/**
29+
* Set up.
30+
*/
31+
protected function setUp()
32+
{
33+
$viewFileSystem = $this->createPartialMock(FileSystem::class, ['getLocaleFileName']);
34+
$viewFileSystem->expects($this->any())->method('getLocaleFileName')
35+
->willReturn(dirname(__DIR__) . '/_files/Magento/Store/i18n/en_AU.csv');
36+
37+
$objectManager = Bootstrap::getObjectManager();
38+
$objectManager->addSharedInstance($viewFileSystem, FileSystem::class);
39+
$translator = $objectManager->create(Translate::class);
40+
$objectManager->addSharedInstance($translator, Translate::class);
41+
42+
$config = $this->createPartialMock(Config::class, ['isEmbeddedStrategy', 'getPatterns']);
43+
$config->expects($this->atLeastOnce())->method('isEmbeddedStrategy')->willReturn(true);
44+
$config->expects($this->atLeastOnce())->method('getPatterns')->willReturn(
45+
[
46+
"~(?:\\$|jQuery)\\.mage\\.__\\((?s)[^'\\\")]*?(['\\\"])(?P<translate>.+?)(?<!\\\\)\\1(?s).*?\\)~",
47+
"~\\\$t\\((?s)[^'\\\")]*?([\\\"'])(?P<translate>.+?)\\1(?s).*?\\)~"
48+
]
49+
);
50+
$this->model = $objectManager->create(
51+
PreProcessor::class,
52+
[
53+
'config' => $config
54+
]
55+
);
56+
}
57+
58+
/**
59+
* Test for backend translation strategy.
60+
*
61+
* @param string $content
62+
* @param string $translation
63+
* @return void
64+
* @dataProvider contentForTranslateDataProvider
65+
*/
66+
public function testProcess(string $content, string $translation)
67+
{
68+
CacheCleaner::cleanAll();
69+
$locale = $this->getMockBuilder(
70+
LocalInterface::class
71+
)->getMockForAbstractClass();
72+
$context = $this->createPartialMock(
73+
FallbackContext::class,
74+
['getAreaCode', 'getLocale']
75+
);
76+
77+
$context->expects($this->atLeastOnce())->method('getAreaCode')->willReturn('base');
78+
$context->expects($this->atLeastOnce())->method('getLocale')->willReturn('en_AU');
79+
$locale->expects($this->atLeastOnce())->method('getContext')->willReturn($context);
80+
81+
$chain = Bootstrap::getObjectManager()->create(
82+
Chain::class,
83+
['asset' => $locale, 'origContent' => '', 'origContentType' => '', 'origAssetPath' => '']
84+
);
85+
$chain->setContent($content);
86+
$this->model->process($chain);
87+
$this->assertEquals($translation, $chain->getContent());
88+
}
89+
90+
/**
91+
* Data provider for translation.
92+
*
93+
* @return array
94+
*/
95+
public function contentForTranslateDataProvider()
96+
{
97+
return [
98+
[
99+
'setTranslateProp = function (el, original) {
100+
var location = $(el).prop(\'tagName\').toLowerCase(),
101+
translated = $.mage.__(original),
102+
translationData = {
103+
shown: translated,
104+
translated: translated,
105+
original: original
106+
},
107+
translateAttr = composeTranslateAttr(translationData, location);
108+
109+
$(el).attr(\'data-translate\', translateAttr);
110+
111+
setText(el, translationData.shown);
112+
},',
113+
'setTranslateProp = function (el, original) {
114+
var location = $(el).prop(\'tagName\').toLowerCase(),
115+
translated = $.mage.__(original),
116+
translationData = {
117+
shown: translated,
118+
translated: translated,
119+
original: original
120+
},
121+
translateAttr = composeTranslateAttr(translationData, location);
122+
123+
$(el).attr(\'data-translate\', translateAttr);
124+
125+
setText(el, translationData.shown);
126+
},'
127+
],
128+
[
129+
<<<EOT
130+
title: $.mage.__(
131+
'Original value for Magento_Store module'
132+
)
133+
EOT
134+
,
135+
<<<EOT
136+
title: 'Translated value for Magento_Store module in en_AU'
137+
EOT
138+
],
139+
[
140+
<<<EOT
141+
title: \$t(
142+
'Original value for Magento_Store module'
143+
)
144+
EOT
145+
,
146+
<<<EOT
147+
title: 'Translated value for Magento_Store module in en_AU'
148+
EOT
149+
],
150+
];
151+
}
152+
}

0 commit comments

Comments
 (0)