Skip to content

Commit

Permalink
Merge pull request #1223 from magento-tsg/MAGETWO-67048
Browse files Browse the repository at this point in the history
[TSGAB] MAGETWO-67048: Cannot add translate attribute into the di.xml
  • Loading branch information
kandy authored Jun 26, 2017
2 parents 2992ee2 + 5dbe193 commit d03bc98
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
*/
namespace Magento\Framework\ObjectManager\Config\Reader;

/**
* Class DomTest @covers \Magento\Framework\ObjectManager\Config\Reader\Dom
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class DomTest extends \PHPUnit_Framework_TestCase
{
/**
Expand Down Expand Up @@ -58,8 +63,9 @@ protected function setUp()
false
);
$this->_fileResolverMock->expects($this->once())->method('get')->will($this->returnValue($this->_fileList));
$this->_mapper = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
\Magento\Framework\ObjectManager\Config\Mapper\Dom::class
$this->_mapper = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Framework\ObjectManager\Config\Mapper\Dom::class,
['argumentInterpreter' => $this->getArgumentInterpreterWithMockedStringUtils()]
);
$this->_validationState = new \Magento\Framework\App\Arguments\ValidationState(
\Magento\Framework\App\State::MODE_DEFAULT
Expand All @@ -80,4 +86,47 @@ public function testRead()
);
$this->assertEquals($this->_mapper->convert($this->_mergedConfig), $model->read('scope'));
}

/**
* Replace Magento\Framework\Data\Argument\Interpreter\StringUtils with mock to check arguments wasn't translated.
*
* Check argument $data has not key $data['translate'], therefore
* Magento\Framework\Data\Argument\Interpreter\StringUtils::evaluate($data) won't translate $data['value'].
*
* @return \Magento\Framework\Data\Argument\Interpreter\Composite
*/
private function getArgumentInterpreterWithMockedStringUtils()
{
$booleanUtils = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
\Magento\Framework\Stdlib\BooleanUtils::class
);
$stringUtilsMock = $this->getMockBuilder(\Magento\Framework\Data\Argument\Interpreter\StringUtils::class)
->setConstructorArgs(['booleanUtils' => $booleanUtils])
->setMethods(['evaluate'])
->getMock();
$stringUtilsMock->expects($this->any())
->method('evaluate')
->with(self::callback(function ($data) {
return !isset($data['translate']);
}))
->will(self::returnCallback(function ($data) {
return isset($data['value']) ? $data['value'] : '';
}));
$constInterpreter = new \Magento\Framework\Data\Argument\Interpreter\Constant();
$composite = new \Magento\Framework\Data\Argument\Interpreter\Composite(
[
'boolean' => new \Magento\Framework\Data\Argument\Interpreter\Boolean($booleanUtils),
'string' => $stringUtilsMock,
'number' => new \Magento\Framework\Data\Argument\Interpreter\Number(),
'null' => new \Magento\Framework\Data\Argument\Interpreter\NullType(),
'object' => new \Magento\Framework\Data\Argument\Interpreter\DataObject($booleanUtils),
'const' => $constInterpreter,
'init_parameter' => new \Magento\Framework\App\Arguments\ArgumentInterpreter($constInterpreter),
],
\Magento\Framework\ObjectManager\Config\Reader\Dom::TYPE_ATTRIBUTE
);
$composite->addInterpreter('array', new \Magento\Framework\Data\Argument\Interpreter\ArrayType($composite));

return $composite;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<item name="boolZero" xsi:type="boolean">false</item>
<item name="intValue" xsi:type="number">100500</item>
<item name="nullValue" xsi:type="null"/>
<item name="stringPattern" xsi:type="string">az-value</item>
<item name="stringPattern" xsi:type="string" translate="true">az-value</item>
</argument>
<argument name="constParam" xsi:type="const">Magento\Store\Model\Website::CACHE_TAG</argument>
<argument name="boolFalseParam" xsi:type="boolean">false</argument>
Expand All @@ -28,7 +28,7 @@
<argument name="boolZeroParam" xsi:type="boolean">false</argument>
<argument name="intValueParam" xsi:type="number">100500</argument>
<argument name="nullValueParam" xsi:type="null"/>
<argument name="stringPatternParam" xsi:type="string">az-value</argument>
<argument name="stringPatternParam" xsi:type="string" translate="true">az-value</argument>
</arguments>
</type>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<item name="boolTrue" xsi:type="number">10</item>
<item name="boolOne" xsi:type="string">1</item>
<item name="boolZero" xsi:type="boolean">false</item>
<item name="stringPattern" xsi:type="string">Az-Value</item>
<item name="stringPattern" xsi:type="string" translate="true">Az-Value</item>
</argument>
<argument name="constParam" xsi:type="const">Magento\Store\Model\Website::CACHE_TAG</argument>
<argument name="boolFalseParam" xsi:type="number">100</argument>
Expand Down
27 changes: 27 additions & 0 deletions lib/internal/Magento/Framework/Config/Converter/Dom/DiFlat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Framework\Config\Converter\Dom;

/**
* Converter of XML data to an array representation with no data loss excluding argument translation.
*/
class DiFlat extends Flat
{
/**
* Retrieve key-value pairs of node attributes excluding translate attribute.
*
* @param \DOMNode $node
* @return array
*/
protected function getNodeAttributes(\DOMNode $node)
{
$result = parent::getNodeAttributes($node);
unset($result['translate']);

return $result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Framework\Config\Test\Unit\Converter\Dom;

/**
* Class DiFlatTest @covers \Magento\Framework\Config\Converter\Dom\DiFlat
*/
class DiFlatTest extends \PHPUnit_Framework_TestCase
{
/**
* Test subject.
*
* @var \Magento\Framework\Config\Converter\Dom\DiFlat
*/
private $model;

/**
* @inheritdoc
*/
protected function setUp()
{
$arrayNodeConfig = new \Magento\Framework\Config\Dom\ArrayNodeConfig(
new \Magento\Framework\Config\Dom\NodePathMatcher(),
[
'/root/multipleNode' => 'id',
'/root/wrongArray' => 'id',
],
[
'/root/node_one/subnode',
]
);
$this->model = new \Magento\Framework\Config\Converter\Dom\DiFlat($arrayNodeConfig);
}

/**
* Test \Magento\Framework\Config\Converter\Dom\DiFlat::convert() exclude attribute 'translate'.
*
* @covers \Magento\Framework\Config\Converter\Dom\DiFlat::convert()
*/
public function testConvert()
{
$fixturePath = __DIR__ . '/../../_files/converter/dom/flat/';
$expected = require $fixturePath . 'result.php';

$dom = new \DOMDocument();
$dom->load($fixturePath . 'di_source.xml');

$actual = $this->model->convert($dom);
$this->assertEquals($expected, $actual);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<root>
<node_one attributeOne = '10' attributeTwo = '20' translate="true" >
<subnode attributeThree = '30' translate="true" ></subnode>
<subnode attributeThree = '40' attributeFour = '40' translate="true" >Value1</subnode>
<books attributeFive = '50' translate="true" />
</node_one>
<multipleNode id="one" name="name1" translate="true" >
<value>1</value>
</multipleNode>
<multipleNode id="two" name="name2" translate="true" >
<value>2</value>
</multipleNode>
<someOtherVal translate="true" ></someOtherVal>
<someDataVal translate="true" ><![CDATA[]]></someDataVal>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
namespace Magento\Framework\ObjectManager\Config\Mapper;

use Magento\Framework\Config\Converter\Dom\Flat as FlatConverter;
use Magento\Framework\Config\Converter\Dom\DiFlat as FlatConverter;
use Magento\Framework\Config\Dom\ArrayNodeConfig;
use Magento\Framework\Config\Dom\NodePathMatcher;

Expand Down

0 comments on commit d03bc98

Please sign in to comment.