Skip to content

Fix translation retrieval #25626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 95 additions & 70 deletions app/code/Magento/Translation/Test/Unit/Model/Js/DataProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,89 +3,99 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

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

use Magento\Framework\App\State;
use Magento\Framework\App\Utility\Files;
use Magento\Framework\Filesystem;
use Magento\Framework\Component\ComponentRegistrar;
use Magento\Framework\Component\DirSearch;
use Magento\Framework\Filesystem\File\Read;
use Magento\Framework\Filesystem\File\ReadFactory;
use Magento\Framework\Filesystem\File\ReadInterface;
use Magento\Translation\Model\Js\DataProvider;
use Magento\Translation\Model\Js\Config;
use Magento\Framework\Phrase\Renderer\Translate;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Translation\Model\Js\Config;
use Magento\Translation\Model\Js\DataProvider as ModelDataProvider;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject as MockObject;

/**
* Verify data provider translation
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class DataProviderTest extends \PHPUnit\Framework\TestCase
class DataProviderTest extends TestCase
{
/**
* @var DataProvider
* @var ModelDataProvider
*/
protected $model;
private $model;

/**
* @var State|\PHPUnit_Framework_MockObject_MockObject
* @var State|MockObject
*/
protected $appStateMock;
private $appStateMock;

/**
* @var Config|\PHPUnit_Framework_MockObject_MockObject
* @var Config|MockObject
*/
protected $configMock;
private $configMock;

/**
* @var Files|\PHPUnit_Framework_MockObject_MockObject
* @var Files|MockObject
*/
protected $filesUtilityMock;
private $filesUtilityMock;

/**
* @var ReadInterface|\PHPUnit_Framework_MockObject_MockObject
* @var ReadInterface|MockObject
*/
protected $fileReadMock;
private $fileReadMock;

/**
* @var Translate|\PHPUnit_Framework_MockObject_MockObject
* @var Translate|MockObject
*/
protected $translateMock;
private $translateMock;

/**
* @return void
* @inheritDoc
*/
protected function setUp()
{
$this->appStateMock = $this->createMock(\Magento\Framework\App\State::class);
$this->configMock = $this->createMock(\Magento\Translation\Model\Js\Config::class);
$this->filesUtilityMock = $this->createMock(\Magento\Framework\App\Utility\Files::class);
$fileReadFactory = $this->createMock(\Magento\Framework\Filesystem\File\ReadFactory::class);
$this->fileReadMock = $this->createMock(\Magento\Framework\Filesystem\File\Read::class);
$this->translateMock = $this->createMock(\Magento\Framework\Phrase\Renderer\Translate::class);
$this->appStateMock = $this->createMock(State::class);
$this->configMock = $this->createMock(Config::class);
$this->filesUtilityMock = $this->createMock(Files::class);
$fileReadFactory = $this->createMock(ReadFactory::class);
$this->fileReadMock = $this->createMock(Read::class);
$this->translateMock = $this->createMock(Translate::class);
$fileReadFactory->expects($this->atLeastOnce())
->method('create')
->willReturn($this->fileReadMock);
$dirSearch = $this->createMock(\Magento\Framework\Component\DirSearch::class);
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$dirSearch = $this->createMock(DirSearch::class);
$objectManager = new ObjectManager($this);
$this->model = $objectManager->getObject(
\Magento\Translation\Model\Js\DataProvider::class,
ModelDataProvider::class,
[
'appState' => $this->appStateMock,
'config' => $this->configMock,
'fileReadFactory' => $fileReadFactory,
'translate' => $this->translateMock,
'dirSearch' => $dirSearch,
'filesUtility' => $this->filesUtilityMock,
'componentRegistrar' =>
$this->createMock(\Magento\Framework\Component\ComponentRegistrar::class)
'componentRegistrar' => $this->createMock(ComponentRegistrar::class)
]
);
}

/**
* Verify data translate.
*
* @param array $config
* @return void
* @dataProvider configDataProvider
*/
public function testGetData()
public function testGetData(array $config): void
{
$themePath = 'blank';
$areaCode = 'adminhtml';
Expand All @@ -101,36 +111,6 @@ public function testGetData()
[$areaCode, $themePath, '*', '*', [$filePaths[3]]]
];

$expectedResult = [
'hello1' => 'hello1translated',
'hello2' => 'hello2translated',
'hello3' => 'hello3translated',
'hello4' => 'hello4translated',
'ko i18' => 'ko i18 translated',
'underscore i18' => 'underscore i18 translated',
];

$contentsMap = [
'content1$.mage.__("hello1")content1',
'content2$.mage.__("hello2")content2',
'content2$.mage.__("hello4")content4 <!-- ko i18n: "ko i18" --><!-- /ko -->',
'content2$.mage.__("hello3")content3 <% _.i18n("underscore i18") %>',
];

$translateMap = [
[['hello1'], [], 'hello1translated'],
[['hello2'], [], 'hello2translated'],
[['hello3'], [], 'hello3translated'],
[['hello4'], [], 'hello4translated'],
[['ko i18'], [], 'ko i18 translated'],
[['underscore i18'], [], 'underscore i18 translated'],
];

$patterns = [
'~\$\.mage\.__\(([\'"])(.+?)\1\)~',
'~(?:i18n\:|_\.i18n\()\s*(["\'])(.*?)(?<!\\\\)\1~',
];

$this->appStateMock->expects($this->once())
->method('getAreaCode')
->willReturn($areaCode);
Expand All @@ -141,42 +121,45 @@ public function testGetData()
->method('getStaticHtmlFiles')
->willReturnMap($staticFilesMap);

foreach ($contentsMap as $index => $content) {
foreach ($config['contentsMap'] as $index => $content) {
$this->fileReadMock->expects($this->at($index))
->method('readAll')
->willReturn($content);
}

$this->configMock->expects($this->any())
->method('getPatterns')
->willReturn($patterns);
->willReturn($config['patterns']);
$this->translateMock->expects($this->any())
->method('render')
->willReturnMap($translateMap);
->willReturnMap($config['translateMap']);

$actualResult = $this->model->getData($themePath);
$this->assertEquals($expectedResult, $actualResult);
$this->assertEquals($config['expectedResult'], $actualResult);
$this->assertEquals(
json_encode($expectedResult),
json_encode($config['expectedResult']),
json_encode($actualResult),
"Translations should be sorted by key"
);
}

/**
* Verify get data throwing exception.
*
* @param array $config
* @return void
* @dataProvider configDataProvider
* @expectedException \Magento\Framework\Exception\LocalizedException
*/
public function testGetDataThrowingException()
public function testGetDataThrowingException(array $config): void
{
$themePath = 'blank';
$areaCode = 'adminhtml';

$patterns = ['~\$\.mage\.__\(([\'"])(.+?)\1\)~'];
$patterns = $config['patterns'];

$this->fileReadMock->expects($this->once())
->method('readAll')
->willReturn('content1$.mage.__("hello1")content1');

$this->appStateMock->expects($this->once())
->method('getAreaCode')
->willReturn($areaCode);
Expand All @@ -186,15 +169,57 @@ public function testGetDataThrowingException()
$this->filesUtilityMock->expects($this->any())
->method('getStaticHtmlFiles')
->willReturn(['test']);

$this->configMock->expects($this->any())
->method('getPatterns')
->willReturn($patterns);

$this->translateMock->expects($this->once())
->method('render')
->willThrowException(new \Exception('Test exception'));

$this->model->getData($themePath);
}

/**
* Config data provider.
*
* @return array
*/
public function configDataProvider(): array
{
return [
[
[
'patterns' => [
'~\$\.mage\.__\(([\'"])(.+?)\1\)~',
'~(?:i18n\:|_\.i18n\()\s*(["\'])(.*?)(?<!\\\\)\1~',
'~translate\=("\')([^\'].*?)\'\"~',
'~(?s)\$t\(\s*([\'"])(\?\<translate\>.+?)(?<!\\\)\1\s*(*SKIP)\)(?s)~',
'~translate args\=("|\'|"\'|\\\"\')([^\'].*?)(\'\\\"|\'"|\'|")~',
],
'expectedResult' => [
'hello1' => 'hello1translated',
'hello2' => 'hello2translated',
'hello3' => 'hello3translated',
'hello4' => 'hello4translated',
'ko i18' => 'ko i18 translated',
'underscore i18' => 'underscore i18 translated',
],
'contentsMap' => [
'content1$.mage.__("hello1")content1',
'content2$.mage.__("hello2")content2',
'content2$.mage.__("hello4")content4 <!-- ko i18n: "ko i18" --><!-- /ko -->',
'content2$.mage.__("hello3")content3 <% _.i18n("underscore i18") %>',
],
'translateMap' => [
[['hello1'], [], 'hello1translated'],
[['hello2'], [], 'hello2translated'],
[['hello3'], [], 'hello3translated'],
[['hello4'], [], 'hello4translated'],
[['ko i18'], [], 'ko i18 translated'],
[['underscore i18'], [], 'underscore i18 translated'],
]
],
]
];
}
}
2 changes: 1 addition & 1 deletion app/code/Magento/Translation/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
<item name="translate_wrapping" xsi:type="string"><![CDATA[~translate\=("')([^\'].*?)\'\"~]]></item>
<item name="mage_translation_widget" xsi:type="string"><![CDATA[~(?s)(?:\$|jQuery)\.mage\.__\(\s*(['"])(?<translate>.+?)(?<!\\)\1\s*(*SKIP)\)\s*(?s)~]]></item>
<item name="mage_translation_static" xsi:type="string"><![CDATA[~(?s)\$t\(\s*(['"])(?<translate>.+?)(?<!\\)\1\s*(*SKIP)\)(?s)~]]></item>
<item name="translate_args" xsi:type="string"><![CDATA[~translate args\=("|'|"')([^\'].*?)('"|'|")~]]></item>
<item name="translate_args" xsi:type="string"><![CDATA[~translate args\=("|'|"'|\\"')([^\'].*?)('\\"|'"|'|")~]]></item>
</argument>
</arguments>
</type>
Expand Down