diff --git a/app/code/Magento/Backend/Block/Widget/Grid/Column/Renderer/Text.php b/app/code/Magento/Backend/Block/Widget/Grid/Column/Renderer/Text.php index 1427cec7604e88935f53d6c108d77bb58357bdfa..11aa6bf86257cad79ea7698e6c9fdf893ba7082b 100644 --- a/app/code/Magento/Backend/Block/Widget/Grid/Column/Renderer/Text.php +++ b/app/code/Magento/Backend/Block/Widget/Grid/Column/Renderer/Text.php @@ -3,14 +3,13 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ +namespace Magento\Backend\Block\Widget\Grid\Column\Renderer; + +use Magento\Framework\DataObject; /** * Backend grid item renderer - * - * @author Magento Core Team <core@magentocommerce.com> */ -namespace Magento\Backend\Block\Widget\Grid\Column\Renderer; - class Text extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer { /** @@ -21,30 +20,53 @@ class Text extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRe protected $_variablePattern = '/\\$([a-z0-9_]+)/i'; /** - * Renders grid column + * Get value for the cel * - * @param \Magento\Framework\DataObject $row - * @return mixed + * @param DataObject $row + * @return string */ - public function _getValue(\Magento\Framework\DataObject $row) + public function _getValue(DataObject $row) { - $format = $this->getColumn()->getFormat() ? $this->getColumn()->getFormat() : null; - $defaultValue = $this->getColumn()->getDefault(); - if ($format === null) { - // If no format and it column not filtered specified return data as is. - $data = parent::_getValue($row); - $string = $data === null ? $defaultValue : $data; - return $this->escapeHtml($string); - } elseif (preg_match_all($this->_variablePattern, $format, $matches)) { - // Parsing of format string - $formattedString = $format; - foreach ($matches[0] as $matchIndex => $match) { - $value = $row->getData($matches[1][$matchIndex]); - $formattedString = str_replace($match, $value, $formattedString); + if (null === $this->getColumn()->getFormat()) { + return $this->getSimpleValue($row); + } + return $this->getFormattedValue($row); + } + + /** + * Get simple value + * + * @param DataObject $row + * @return string + */ + private function getSimpleValue(DataObject $row) + { + $data = parent::_getValue($row); + $value = null === $data ? $this->getColumn()->getDefault() : $data; + if (true === $this->getColumn()->getTranslate()) { + $value = __($value); + } + return $this->escapeHtml($value); + } + + /** + * Replace placeholders in the string with values + * + * @param DataObject $row + * @return string + */ + private function getFormattedValue(DataObject $row) + { + $value = $this->getColumn()->getFormat() ?: null; + if (true === $this->getColumn()->getTranslate()) { + $value = __($value); + } + if (preg_match_all($this->_variablePattern, $value, $matches)) { + foreach ($matches[0] as $index => $match) { + $replacement = $row->getData($matches[1][$index]); + $value = str_replace($match, $replacement, $value); } - return $formattedString; - } else { - return $this->escapeHtml($format); } + return $this->escapeHtml($value); } } diff --git a/app/code/Magento/Backend/view/adminhtml/layout/adminhtml_cache_block.xml b/app/code/Magento/Backend/view/adminhtml/layout/adminhtml_cache_block.xml index 3e61fec077c6e3fcfccfb62b5ecad98248e9b41d..decc26f331c8298d16b4a76d4a74d82c5808c651 100644 --- a/app/code/Magento/Backend/view/adminhtml/layout/adminhtml_cache_block.xml +++ b/app/code/Magento/Backend/view/adminhtml/layout/adminhtml_cache_block.xml @@ -48,6 +48,7 @@ <argument name="width" xsi:type="string">180</argument> <argument name="align" xsi:type="string">left</argument> <argument name="sortable" xsi:type="string">0</argument> + <argument name="translate" xsi:type="boolean">true</argument> </arguments> </block> <block class="Magento\Backend\Block\Widget\Grid\Column" as="description"> @@ -57,6 +58,7 @@ <argument name="type" xsi:type="string">text</argument> <argument name="align" xsi:type="string">left</argument> <argument name="sortable" xsi:type="string">0</argument> + <argument name="translate" xsi:type="boolean">true</argument> </arguments> </block> <block class="Magento\Backend\Block\Widget\Grid\Column" as="tags"> diff --git a/app/code/Magento/Indexer/view/adminhtml/layout/indexer_indexer_list_grid.xml b/app/code/Magento/Indexer/view/adminhtml/layout/indexer_indexer_list_grid.xml index 6def568e9cbbd83fec74955430834f7538418ab9..63ef028238393bbbbcebaeff4c50caa1e94b44db 100644 --- a/app/code/Magento/Indexer/view/adminhtml/layout/indexer_indexer_list_grid.xml +++ b/app/code/Magento/Indexer/view/adminhtml/layout/indexer_indexer_list_grid.xml @@ -46,6 +46,7 @@ <argument name="index" xsi:type="string">title</argument> <argument name="sortable" xsi:type="string">0</argument> <argument name="column_css_class" xsi:type="string">indexer-title</argument> + <argument name="translate" xsi:type="boolean">true</argument> </arguments> </block> <block class="Magento\Backend\Block\Widget\Grid\Column" as="indexer_description"> @@ -54,6 +55,7 @@ <argument name="index" xsi:type="string">description</argument> <argument name="sortable" xsi:type="string">0</argument> <argument name="column_css_class" xsi:type="string">indexer-description</argument> + <argument name="translate" xsi:type="boolean">true</argument> </arguments> </block> <block class="Magento\Backend\Block\Widget\Grid\Column" as="indexer_mode"> diff --git a/dev/tests/integration/testsuite/Magento/Backend/Block/Widget/Grid/Column/Renderer/TextTest.php b/dev/tests/integration/testsuite/Magento/Backend/Block/Widget/Grid/Column/Renderer/TextTest.php new file mode 100644 index 0000000000000000000000000000000000000000..9d2875716156e83b48502be84a4ef1a86ef46772 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Backend/Block/Widget/Grid/Column/Renderer/TextTest.php @@ -0,0 +1,142 @@ +<?php +/** + * Copyright © 2016 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Backend\Block\Widget\Grid\Column\Renderer; + +use Magento\TestFramework\Helper\Bootstrap; +use Magento\TestFramework\ObjectManager; +use Magento\Backend\Block\Widget\Grid\Column; +use Magento\Framework\DataObject; +use Magento\Framework\Phrase; +use Magento\Framework\Phrase\RendererInterface; + +class TextTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var ObjectManager + */ + private $objectManager; + + /** + * @var RendererInterface + */ + private $origRenderer; + + protected function setUp() + { + $this->objectManager = Bootstrap::getObjectManager(); + $this->origRenderer = Phrase::getRenderer(); + /** @var RendererInterface|PHPUnit_Framework_MockObject_MockObject $rendererMock */ + $rendererMock = $this->getMock(RendererInterface::class); + $rendererMock->expects($this->any()) + ->method('render') + ->willReturnCallback( + function ($input) { + return end($input) . ' translated'; + } + ); + Phrase::setRenderer($rendererMock); + } + + protected function tearDown() + { + Phrase::setRenderer($this->origRenderer); + } + + /** + * @param array $columnData + * @param array $rowData + * @param string $expected + * @dataProvider renderDataProvider + */ + public function testRender($columnData, $rowData, $expected) + { + /** @var Text $renderer */ + $renderer = $this->objectManager->create(Text::class); + /** @var Column $column */ + $column = $this->objectManager->create( + Column::class, + [ + 'data' => $columnData + ] + ); + /** @var DataObject $row */ + $row = $this->objectManager->create( + DataObject::class, + [ + 'data' => $rowData + ] + ); + $this->assertEquals( + $expected, + $renderer->setColumn($column)->render($row) + ); + } + + /** + * @return array + */ + public function renderDataProvider() + { + return [ + [ + [ + 'index' => 'title', + 'translate' => true + ], + [ + 'title' => 'String' + ], + 'String translated' + ], + [ + [ + 'index' => 'title' + ], + [ + 'title' => 'Doesn\'t need to be translated' + ], + 'Doesn't need to be translated' + ], + [ + [ + 'format' => '#$subscriber_id $customer_name ($subscriber_email)' + ], + [ + 'subscriber_id' => '10', + 'customer_name' => 'John Doe', + 'subscriber_email' => 'john@doe.com' + ], + '#10 John Doe (john@doe.com)' + ], + [ + [ + 'format' => '$customer_name, email: $subscriber_email', + 'translate' => true + ], + [ + 'customer_name' => 'John Doe', + 'subscriber_email' => 'john@doe.com' + ], + 'John Doe, email: john@doe.com translated' + ], + [ + [ + 'format' => 'String', + 'translate' => true + ], + [], + 'String translated' + ], + [ + [ + 'format' => 'Doesn\'t need to be translated' + ], + [], + 'Doesn't need to be translated' + ] + ]; + } +} diff --git a/dev/tests/static/framework/Magento/TestFramework/Utility/ChangedFiles.php b/dev/tests/static/framework/Magento/TestFramework/Utility/ChangedFiles.php index d71c03eb0f85d01634f6f3a28c185c37e84bc1b2..571b663067151db4eb05d1484f38e1e10cd9527e 100644 --- a/dev/tests/static/framework/Magento/TestFramework/Utility/ChangedFiles.php +++ b/dev/tests/static/framework/Magento/TestFramework/Utility/ChangedFiles.php @@ -3,10 +3,10 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - namespace Magento\TestFramework\Utility; use Magento\Framework\App\Utility\Files; +use Magento\TestFramework\Utility\File\RegexIteratorFactory; /** * A helper to gather various changed files @@ -23,7 +23,7 @@ class ChangedFiles */ public static function getPhpFiles($changedFilesList) { - $fileHelper = \Magento\Framework\App\Utility\Files::init(); + $fileUtilities = new File(Files::init(), new RegexIteratorFactory()); if (isset($_ENV['INCREMENTAL_BUILD'])) { $phpFiles = []; foreach (glob($changedFilesList) as $listFile) { @@ -36,27 +36,11 @@ class ChangedFiles } ); if (!empty($phpFiles)) { - $phpFiles = \Magento\Framework\App\Utility\Files::composeDataSets($phpFiles); - $phpFiles = array_intersect_key($phpFiles, $fileHelper->getPhpFiles( - Files::INCLUDE_APP_CODE - | Files::INCLUDE_PUB_CODE - | Files::INCLUDE_LIBS - | Files::INCLUDE_TEMPLATES - | Files::INCLUDE_TESTS - | Files::AS_DATA_SET - | Files::INCLUDE_NON_CLASSES - )); + $phpFiles = Files::composeDataSets($phpFiles); + $phpFiles = array_intersect_key($phpFiles, $fileUtilities->getPhpFiles()); } } else { - $phpFiles = $fileHelper->getPhpFiles( - Files::INCLUDE_APP_CODE - | Files::INCLUDE_PUB_CODE - | Files::INCLUDE_LIBS - | Files::INCLUDE_TEMPLATES - | Files::INCLUDE_TESTS - | Files::AS_DATA_SET - | Files::INCLUDE_NON_CLASSES - ); + $phpFiles = $fileUtilities->getPhpFiles(); } return $phpFiles; diff --git a/dev/tests/static/framework/Magento/TestFramework/Utility/File.php b/dev/tests/static/framework/Magento/TestFramework/Utility/File.php new file mode 100644 index 0000000000000000000000000000000000000000..a46b293f625a0daf78ef4b5b5ab76e82674efbd0 --- /dev/null +++ b/dev/tests/static/framework/Magento/TestFramework/Utility/File.php @@ -0,0 +1,101 @@ +<?php +/** + * Copyright © 2016 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\TestFramework\Utility; + +use Magento\Framework\App\Utility\Files; +use Magento\TestFramework\Utility\File\RegexIteratorFactory; + +/** + * Get list of PHP files including files in setup application + */ +class File +{ + /**@#+ + * File types offset flags + */ + const INCLUDE_APP_CODE = Files::INCLUDE_APP_CODE; + const INCLUDE_PUB_CODE = Files::INCLUDE_PUB_CODE; + const INCLUDE_LIBS = Files::INCLUDE_LIBS; + const INCLUDE_TEMPLATES = Files::INCLUDE_TEMPLATES; + const INCLUDE_TESTS = Files::INCLUDE_TESTS; + const INCLUDE_SETUP = 128; + const INCLUDE_NON_CLASSES = Files::INCLUDE_NON_CLASSES; + const AS_DATA_SET = Files::AS_DATA_SET; + /**#@-*/ + + /** + * @var RegexIteratorFactory + */ + private $regexIteratorFactory; + + /** + * @var Files + */ + private $fileUtilities; + + /** + * Constructor + * + * @param Files $fileUtilities + * @param RegexIteratorFactory $regexIteratorFactory + */ + public function __construct( + Files $fileUtilities, + RegexIteratorFactory $regexIteratorFactory + ) { + $this->fileUtilities = $fileUtilities; + $this->regexIteratorFactory = $regexIteratorFactory; + } + + /** + * Get list of PHP files + * + * @param int $flags + * @return array + * @throws \Exception + */ + public function getPhpFiles( + $flags = self::INCLUDE_APP_CODE + | self::INCLUDE_PUB_CODE + | self::INCLUDE_LIBS + | self::INCLUDE_TEMPLATES + | self::INCLUDE_TESTS + | self::INCLUDE_SETUP + | self::INCLUDE_NON_CLASSES + | self::AS_DATA_SET + ) { + $files = array_merge( + $this->fileUtilities->getPhpFiles((2147483647 - self::AS_DATA_SET) & $flags), + $this->getSetupPhpFiles($flags) + ); + + if ($flags & self::AS_DATA_SET) { + return Files::composeDataSets($files); + } + return $files; + } + + /** + * Get list of PHP files in setup application + * + * @param int $flags + * @return array + */ + private function getSetupPhpFiles($flags) + { + $files = []; + if ($flags & self::INCLUDE_SETUP) { + $regexIterator = $this->regexIteratorFactory->create( + BP . '/setup', + '/.*php^/' + ); + foreach ($regexIterator as $file) { + $files = array_merge($files, [$file]); + } + } + return $files; + } +} diff --git a/dev/tests/static/framework/Magento/TestFramework/Utility/File/RegexIteratorFactory.php b/dev/tests/static/framework/Magento/TestFramework/Utility/File/RegexIteratorFactory.php new file mode 100644 index 0000000000000000000000000000000000000000..d2e5768414f06f13138ba83ebcf34ce7535783e6 --- /dev/null +++ b/dev/tests/static/framework/Magento/TestFramework/Utility/File/RegexIteratorFactory.php @@ -0,0 +1,26 @@ +<?php +/** + * Copyright © 2016 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\TestFramework\Utility\File; + +/** + * Factory for \RegexIterator + */ +class RegexIteratorFactory +{ + /** + * Create instance of \RegexIterator + * + * @param string $directoryPath + * @param string $regexp + * @return \RegexIterator + */ + public function create($directoryPath, $regexp) + { + $directory = new \RecursiveDirectoryIterator($directoryPath); + $recursiveIterator = new \RecursiveIteratorIterator($directory); + return new \RegexIterator($recursiveIterator, $regexp, \RegexIterator::GET_MATCH); + } +} diff --git a/dev/tests/static/framework/Magento/TestFramework/Utility/FunctionDetector.php b/dev/tests/static/framework/Magento/TestFramework/Utility/FunctionDetector.php new file mode 100644 index 0000000000000000000000000000000000000000..aa48ca3344bb00555f0cd1241398f8103480e17f --- /dev/null +++ b/dev/tests/static/framework/Magento/TestFramework/Utility/FunctionDetector.php @@ -0,0 +1,67 @@ +<?php +/** + * Copyright © 2016 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\TestFramework\Utility; + +/** + * A helper detects functions + */ +class FunctionDetector +{ + /** + * Detect functions in given file + * + * return result in this format: + * [ + * line_number => [ + * function_name_1, + * function_name_2, + * function_name_3, + * ], + * line_number => [ + * function_name_1, + * function_name_2, + * function_name_3, + * ], + * ] + * + * @param string $fileFullPath + * @param string[] $functions + * @return array + */ + public function detectFunctions($fileFullPath, $functions) + { + $result = []; + $regexp = $this->composeRegexp($functions); + if ($regexp) { + $file = file($fileFullPath); + array_unshift($file, ''); + $lines = preg_grep( + $regexp, + $file + ); + foreach ($lines as $lineNumber => $line) { + if (preg_match_all($regexp, $line, $matches)) { + $result[$lineNumber] = $matches[1]; + } + } + } + return $result; + } + + /** + * Compose regular expression + * + * @param array $functions + * @return string + */ + private function composeRegexp(array $functions) + { + if (empty($functions)) { + return ''; + } + return '/(?<!function |->|::)\b(' . join('|', $functions) . ')\s*\(/i'; + } +} diff --git a/dev/tests/static/framework/tests/unit/testsuite/Magento/TestFramework/Utility/FileTest.php b/dev/tests/static/framework/tests/unit/testsuite/Magento/TestFramework/Utility/FileTest.php new file mode 100644 index 0000000000000000000000000000000000000000..e802228c282f0a7f6af45ee181a5938a9b2aec7b --- /dev/null +++ b/dev/tests/static/framework/tests/unit/testsuite/Magento/TestFramework/Utility/FileTest.php @@ -0,0 +1,164 @@ +<?php +/** + * Copyright © 2016 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\TestFramework\Utility; + +use Magento\Framework\App\Utility\Files; +use Magento\TestFramework\Utility\File\RegexIteratorFactory; +use Magento\TestFramework\Utility\File; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; + +class FileTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var Files|PHPUnit_Framework_MockObject_MockObject + */ + private $fileUtilitiesMock; + + /** + * @var RegexIteratorFactory|PHPUnit_Framework_MockObject_MockObject + */ + private $regexIteratorFactoryMock; + + /** + * @var ObjectManager + */ + private $objectManager; + + /** + * @var File + */ + private $file; + + protected function setUp() + { + $this->objectManager = new ObjectManager($this); + $this->fileUtilitiesMock = $this->getMock(Files::class, [], [], '', false); + $this->regexIteratorFactoryMock = $this->getMock(RegexIteratorFactory::class, [], [], '', false); + $this->file = $this->objectManager->getObject( + File::class, + [ + 'fileUtilities' => $this->fileUtilitiesMock, + 'regexIteratorFactory' => $this->regexIteratorFactoryMock + ] + ); + } + + public function testGetPhpFilesWithoutSetup() + { + $appFiles = [ + 'file1', + 'file2' + ]; + $expected = [ + 'file1' => ['file1'], + 'file2' => ['file2'] + ]; + $this->regexIteratorFactoryMock->expects($this->never()) + ->method('create'); + $this->fileUtilitiesMock->expects($this->once()) + ->method('getPhpFiles') + ->with( + File::INCLUDE_APP_CODE + | File::INCLUDE_PUB_CODE + | File::INCLUDE_LIBS + | File::INCLUDE_TEMPLATES + | File::INCLUDE_TESTS + | File::INCLUDE_NON_CLASSES + ) + ->willReturn($appFiles); + $actual = $this->file->getPhpFiles( + File::INCLUDE_APP_CODE + | File::INCLUDE_PUB_CODE + | File::INCLUDE_LIBS + | File::INCLUDE_TEMPLATES + | File::INCLUDE_TESTS + | File::INCLUDE_NON_CLASSES + | File::AS_DATA_SET + ); + $this->assertEquals($expected, $actual); + } + + /** + * @param array $appFiles + * @param array$setupFiles + * @param int $flags + * @param array $expected + * @dataProvider getPhpFilesWithSetupDataProvider + */ + public function testGetPhpFilesWithSetup( + $appFiles, + $setupFiles, + $flags, + $expected + ) { + $iteratorMock = $this->getMock(\IteratorAggregate::class, [], [], '', false); + $iteratorMock->expects($this->any()) + ->method('getIterator') + ->willReturn(new \ArrayIterator($setupFiles)); + $this->regexIteratorFactoryMock->expects($this->once()) + ->method('create') + ->willReturn($iteratorMock); + $this->fileUtilitiesMock->expects($this->once()) + ->method('getPhpFiles') + ->with( + File::INCLUDE_APP_CODE + | File::INCLUDE_PUB_CODE + | File::INCLUDE_LIBS + | File::INCLUDE_TEMPLATES + | File::INCLUDE_TESTS + | File::INCLUDE_SETUP + | File::INCLUDE_NON_CLASSES + ) + ->willReturn($appFiles); + $this->assertEquals($expected, $this->file->getPhpFiles($flags)); + } + + /** + * @return array + */ + public function getPhpFilesWithSetupDataProvider() + { + $flags = File::INCLUDE_APP_CODE + | File::INCLUDE_PUB_CODE + | File::INCLUDE_LIBS + | File::INCLUDE_TEMPLATES + | File::INCLUDE_TESTS + | File::INCLUDE_SETUP + | File::INCLUDE_NON_CLASSES; + return [ + [ + [ + 'file1', + 'file2' + ], + [ + 'file3' + ], + $flags | File::AS_DATA_SET, + [ + 'file1' => ['file1'], + 'file2' => ['file2'], + 'file3' => ['file3'] + ] + ], + [ + [ + 'file1', + 'file2' + ], + [ + 'file3' + ], + $flags, + [ + 'file1', + 'file2', + 'file3' + ] + ] + ]; + } +} diff --git a/dev/tests/static/framework/tests/unit/testsuite/Magento/TestFramework/Utility/FunctionDetectorTest.php b/dev/tests/static/framework/tests/unit/testsuite/Magento/TestFramework/Utility/FunctionDetectorTest.php new file mode 100644 index 0000000000000000000000000000000000000000..e2dc7cdd4fae97af39a30f626c5ed3687086a1c4 --- /dev/null +++ b/dev/tests/static/framework/tests/unit/testsuite/Magento/TestFramework/Utility/FunctionDetectorTest.php @@ -0,0 +1,22 @@ +<?php +/** + * Copyright © 2016 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\TestFramework\Utility; + +class FunctionDetectorTest extends \PHPUnit_Framework_TestCase +{ + public function testDetectFunctions() + { + $fixturePath = __DIR__ . '/_files/test.txt'; + $expectedResults = [ + 24 => ['strtoupper', 'md5'], + 36 => ['security'], + 37 => ['security'], + ]; + $functionDetector = new FunctionDetector(); + $lines = $functionDetector->detectFunctions($fixturePath, ['security', 'md5', 'test', 'strtoupper']); + $this->assertEquals($expectedResults, $lines); + } +} diff --git a/dev/tests/static/framework/tests/unit/testsuite/Magento/TestFramework/Utility/_files/test.txt b/dev/tests/static/framework/tests/unit/testsuite/Magento/TestFramework/Utility/_files/test.txt new file mode 100644 index 0000000000000000000000000000000000000000..a7f1bced711892131125585d8f6a5ea31d5157a1 --- /dev/null +++ b/dev/tests/static/framework/tests/unit/testsuite/Magento/TestFramework/Utility/_files/test.txt @@ -0,0 +1,46 @@ +<?php +/** + * Copyright © 2016 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Authorizenet\Model\Security; + +/** + * security + */ +class Test extends AuthorizenetResponse +{ + /** + * Generates an Md5 hash to compare against AuthNet's. + * + * @param string $merchantMd5 + * @param string $merchantApiLogin + * @param string $amount + * @param string $transactionId + * @return string + */ + public function checkSecurity($merchantMd5, $merchantApiLogin, $amount, $transactionId) + { + return strtoupper(md5($merchantMd5 . $merchantApiLogin . $transactionId . $amount)); + } + + /** + * Return if is valid order id. + * + * @param string $merchantMd5 + * @param string $merchantApiLogin + * @return bool + */ + public function security($merchantMd5, $merchantApiLogin) + { + $hash = $this->generateHash(security($merchantMd5), $merchantApiLogin, $this->getXAmount()); + security( + 'something' + ); + return Security::compareStrings($hash, $this->getData('x_MD5_Hash')); + + $this->security('check'); + Security::security(); + return $this->getXResponseCode() == \Magento\Authorizenet\Model\Security::RESPONSE_CODE_APPROVED; + } +} diff --git a/dev/tests/static/testsuite/Magento/Test/Legacy/RestrictedCodeTest.php b/dev/tests/static/testsuite/Magento/Test/Legacy/RestrictedCodeTest.php index 0c4c6eba49efa33c997b2dc62c70bd49434540e8..68c2d166448ab6fb4c39354b92ddb9f803d500f1 100644 --- a/dev/tests/static/testsuite/Magento/Test/Legacy/RestrictedCodeTest.php +++ b/dev/tests/static/testsuite/Magento/Test/Legacy/RestrictedCodeTest.php @@ -3,14 +3,13 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - -/** - * Tests to find usage of restricted code - */ namespace Magento\Test\Legacy; use Magento\Framework\Component\ComponentRegistrar; +/** + * Tests to find usage of restricted code + */ class RestrictedCodeTest extends \PHPUnit_Framework_TestCase { /**@#+ @@ -18,17 +17,29 @@ class RestrictedCodeTest extends \PHPUnit_Framework_TestCase * * @var array */ - protected static $_classes = []; + private static $_classes = []; /**#@-*/ /** * List of fixtures that contain restricted classes and should not be tested + * * @var array */ - protected static $_fixtureFiles = []; + private static $_fixtureFiles = []; + + /** + * @var ComponentRegistrar + */ + private $componentRegistrar; + + protected function setUp() + { + $this->componentRegistrar = new ComponentRegistrar(); + } /** * Read fixtures into memory as arrays + * * @return void */ public static function setUpBeforeClass() @@ -69,6 +80,7 @@ class RestrictedCodeTest extends \PHPUnit_Framework_TestCase /** * Test that restricted entities are not used in PHP files + * * @return void */ public function testPhpFiles() @@ -97,15 +109,13 @@ class RestrictedCodeTest extends \PHPUnit_Framework_TestCase protected function _testRestrictedClasses($file) { $content = file_get_contents($file); - $componentRegistrar = new ComponentRegistrar(); foreach (self::$_classes as $restrictedClass => $classRules) { foreach ($classRules['exclude'] as $skippedPathInfo) { - $skippedPath = $componentRegistrar->getPath($skippedPathInfo['type'], $skippedPathInfo['name']) - . '/' . $skippedPathInfo['path']; - if (strpos($file, $skippedPath) === 0) { + if (strpos($file, $this->getExcludedFilePath($skippedPathInfo)) === 0) { continue 2; } } + $this->assertFalse( \Magento\TestFramework\Utility\CodeCheck::isClassUsed($restrictedClass, $content), sprintf( @@ -117,4 +127,18 @@ class RestrictedCodeTest extends \PHPUnit_Framework_TestCase ); } } + + /** + * Get full path for excluded file + * + * @param array $pathInfo + * @return string + */ + private function getExcludedFilePath($pathInfo) + { + if ($pathInfo['type'] != 'setup') { + return $this->componentRegistrar->getPath($pathInfo['type'], $pathInfo['name']) . '/' . $pathInfo['path']; + } + return BP . '/setup/' . $pathInfo['path']; + } } diff --git a/dev/tests/static/testsuite/Magento/Test/Legacy/UnsecureFunctionsUsageTest.php b/dev/tests/static/testsuite/Magento/Test/Legacy/UnsecureFunctionsUsageTest.php index e5263713d71d7af374ea1f32c2d765d3639e56ce..23d216c5fbcf6a49f2560d61486db35e0fce513c 100644 --- a/dev/tests/static/testsuite/Magento/Test/Legacy/UnsecureFunctionsUsageTest.php +++ b/dev/tests/static/testsuite/Magento/Test/Legacy/UnsecureFunctionsUsageTest.php @@ -6,12 +6,28 @@ namespace Magento\Test\Legacy; use Magento\Framework\App\Utility\Files; +use Magento\Framework\Component\ComponentRegistrar; +use Magento\TestFramework\Utility\FunctionDetector; /** * Tests to detect unsecure functions usage */ class UnsecureFunctionsUsageTest extends \PHPUnit_Framework_TestCase { + /** + * Php unsecure functions + * + * @var array + */ + private static $phpUnsecureFunctions = []; + + /** + * JS unsecure functions + * + * @var array + */ + private static $jsUnsecureFunctions = []; + /** * File extensions pattern to search for * @@ -20,25 +36,54 @@ class UnsecureFunctionsUsageTest extends \PHPUnit_Framework_TestCase private $fileExtensions = '/\.(php|phtml|js)$/'; /** - * Php unsecure functions to detect + * Read fixtures into memory as arrays * - * @var array + * @return void */ - private $phpUnsecureFunctions = [ - 'unserialize', - 'serialize', - 'eval', - 'md5', - 'srand', - 'mt_srand' - ]; + public static function setUpBeforeClass() + { + self::loadData(self::$phpUnsecureFunctions, 'unsecure_php_functions*.php'); + self::loadData(self::$jsUnsecureFunctions, 'unsecure_js_functions*.php'); + } /** - * JS unsecure functions to detect + * Loads and merges data from fixtures * - * @var array + * @param array $data + * @param string $filePattern + * @return void */ - private $jsUnsecureFunctions = []; + private static function loadData(array &$data, $filePattern) + { + foreach (glob(__DIR__ . '/_files/security/' . $filePattern) as $file) { + $data = array_merge_recursive($data, self::readList($file)); + } + $componentRegistrar = new ComponentRegistrar(); + foreach ($data as $key => $value) { + $excludes = $value['exclude']; + $excludePaths = []; + foreach ($excludes as $exclude) { + if ('setup' == $exclude['type']) { + $excludePaths[] = BP . '/setup/' . $exclude['path']; + } else { + $excludePaths[] = $componentRegistrar->getPath($exclude['type'], $exclude['name']) + . '/' . $exclude['path']; + } + } + $data[$key]['exclude'] = $excludePaths; + } + } + + /** + * Isolate including a file into a method to reduce scope + * + * @param string $file + * @return array + */ + private static function readList($file) + { + return include $file; + } /** * Detect unsecure functions usage for changed files in whitelist with the exception of blacklist @@ -48,46 +93,69 @@ class UnsecureFunctionsUsageTest extends \PHPUnit_Framework_TestCase public function testUnsecureFunctionsUsage() { $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this); + $functionDetector = new FunctionDetector(); $invoker( - function ($fileName) { - $result = ''; - $errorMessage = 'The following functions are non secure and should be avoided: ' - . implode(', ', $this->phpUnsecureFunctions) - . ' for PHP'; - if (!empty($this->jsUnsecureFunctions)) { - $errorMessage .= ', and ' - . implode(', ', $this->jsUnsecureFunctions) - . ' for JavaScript'; - } - $errorMessage .= ".\n"; - $regexp = $this->getRegexpByFileExtension(pathinfo($fileName, PATHINFO_EXTENSION)); - if ($regexp) { - $matches = preg_grep( - $regexp, - file($fileName) - ); - if (!empty($matches)) { - foreach (array_keys($matches) as $line) { - $result .= $fileName . ':' . ($line + 1) . "\n"; - } - } - $this->assertEmpty($result, $errorMessage . $result); + function ($fileFullPath) use ($functionDetector) { + $functions = $this->getFunctions($fileFullPath); + $lines = $functionDetector->detectFunctions($fileFullPath, array_keys($functions)); + + $message = ''; + if (!empty($lines)) { + $message = $this->composeMessage($fileFullPath, $lines, $functions); } + $this->assertEmpty( + $lines, + $message + ); }, - $this->unsecureFunctionsUsageDataProvider() + $this->getFilesToVerify() ); } /** - * Data provider for test + * Compose message + * + * @param string $fileFullPath + * @param array $lines + * @param array $functionRules + * @return string + */ + private function composeMessage($fileFullPath, $lines, $functionRules) + { + $result = ''; + foreach ($lines as $lineNumber => $detectedFunctions) { + $detectedFunctionRules = array_intersect_key($functionRules, array_flip($detectedFunctions)); + $replacementString = ''; + foreach ($detectedFunctionRules as $function => $functionRule) { + $replacement = $functionRule['replacement']; + if (is_array($replacement)) { + $replacement = array_unique($replacement); + $replacement = count($replacement) > 1 ? + "[\n\t\t\t" . implode("\n\t\t\t", $replacement) . "\n\t\t]" : + $replacement[0]; + } + $replacement = empty($replacement) ? 'No suggested replacement at this time' : $replacement; + $replacementString .= "\t\t'$function' => '$replacement'\n"; + } + $result .= sprintf( + "Functions '%s' are not secure in %s. \n\tSuggested replacement:\n%s", + implode(', ', $detectedFunctions), + $fileFullPath . ':' . $lineNumber, + $replacementString + ); + } + return $result; + } + + /** + * Get files to be verified * * @return array */ - public function unsecureFunctionsUsageDataProvider() + private function getFilesToVerify() { $fileExtensions = $this->fileExtensions; $directoriesToScan = Files::init()->readLists(__DIR__ . '/_files/security/whitelist.txt'); - $blackListFiles = include __DIR__ . '/_files/security/blacklist.php'; $filesToVerify = []; foreach (glob(__DIR__ . '/../_files/changed_files*') as $listFile) { @@ -104,7 +172,7 @@ class UnsecureFunctionsUsageTest extends \PHPUnit_Framework_TestCase ); $filesToVerify = array_filter( $filesToVerify, - function ($path) use ($directoriesToScan, $fileExtensions, $blackListFiles) { + function ($path) use ($directoriesToScan, $fileExtensions) { if (!file_exists($path[0])) { return false; } @@ -113,11 +181,9 @@ class UnsecureFunctionsUsageTest extends \PHPUnit_Framework_TestCase $directory = realpath($directory); if (strpos($path, $directory) === 0) { if (preg_match($fileExtensions, $path)) { - foreach ($blackListFiles as $blackListFile) { - $blackListFile = preg_quote($blackListFile, '#'); - if (preg_match('#' . $blackListFile . '#', $path)) { - return false; - } + // skip unit tests + if (preg_match('#' . preg_quote('Test/Unit', '#') . '#', $path)) { + return false; } return true; } @@ -130,35 +196,27 @@ class UnsecureFunctionsUsageTest extends \PHPUnit_Framework_TestCase } /** - * Get regular expression by file extension + * Get functions for the given file * - * @param string $fileExtension - * @return string|bool + * @param string $fileFullPath + * @return array */ - private function getRegexpByFileExtension($fileExtension) + private function getFunctions($fileFullPath) { - $regexp = false; + $fileExtension = pathinfo($fileFullPath, PATHINFO_EXTENSION); + $functions = []; if ($fileExtension == 'php') { - $regexp = $this->prepareRegexp($this->phpUnsecureFunctions); + $functions = self::$phpUnsecureFunctions; } elseif ($fileExtension == 'js') { - $regexp = $this->prepareRegexp($this->jsUnsecureFunctions); + $functions = self::$jsUnsecureFunctions; } elseif ($fileExtension == 'phtml') { - $regexp = $this->prepareRegexp($this->phpUnsecureFunctions + $this->jsUnsecureFunctions); + $functions = array_merge_recursive(self::$phpUnsecureFunctions, self::$jsUnsecureFunctions); } - return $regexp; - } - - /** - * Prepare regular expression for unsecure function names - * - * @param array $functions - * @return string - */ - private function prepareRegexp(array $functions) - { - if (empty($functions)) { - return ''; + foreach ($functions as $function => $functionRules) { + if (in_array($fileFullPath, $functionRules['exclude'])) { + unset($functions[$function]); + } } - return '/(?<!function |[^\s])\b(' . join('|', $functions) . ')\s*\(/i'; + return $functions; } } diff --git a/dev/tests/static/testsuite/Magento/Test/Legacy/_files/restricted_classes.php b/dev/tests/static/testsuite/Magento/Test/Legacy/_files/restricted_classes.php index a950b2d7e9ed243356da176f8eef1095ef4fb210..683449d4e5e34b919ef476d93c26fe01d122b0a8 100644 --- a/dev/tests/static/testsuite/Magento/Test/Legacy/_files/restricted_classes.php +++ b/dev/tests/static/testsuite/Magento/Test/Legacy/_files/restricted_classes.php @@ -1,27 +1,103 @@ <?php +/** + * Copyright © 2016 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + /** * Classes that are restricted to use directly. * A <replacement> will be suggested to be used instead. * Use <whitelist> to specify files and directories that are allowed to use restricted classes. * * Format: array(<class_name>, <replacement>[, array(<whitelist>)]]) - * - * Copyright © 2016 Magento. All rights reserved. - * See COPYING.txt for license details. */ return [ 'Zend_Db_Select' => [ 'replacement' => '\Magento\Framework\DB\Select', 'exclude' => [ - ['type' => 'library', 'name' => 'magento/framework', 'path' => 'DB/Select.php'], - ['type' => 'library', 'name' => 'magento/framework', 'path' => 'DB/Adapter/Pdo/Mysql.php'], - ['type' => 'library', 'name' => 'magento/framework', 'path' => 'Model/ResourceModel/Iterator.php'], + [ + 'type' => 'library', + 'name' => 'magento/framework', + 'path' => 'DB/Select.php' + ], + [ + 'type' => 'library', + 'name' => 'magento/framework', + 'path' => 'DB/Adapter/Pdo/Mysql.php' + ], + [ + 'type' => 'library', + 'name' => 'magento/framework', + 'path' => 'Model/ResourceModel/Iterator.php' + ], ] ], 'Zend_Db_Adapter_Pdo_Mysql' => [ 'replacement' => '\Magento\Framework\DB\Adapter\Pdo\Mysql', 'exclude' => [ - ['type' => 'library', 'name' => 'magento/framework', 'path' => 'DB/Adapter/Pdo/Mysql.php'], + [ + 'type' => 'library', + 'name' => 'magento/framework', + 'path' => 'DB/Adapter/Pdo/Mysql.php' + ], ] ], + 'Magento\Framework\Serialize\Serializer\Serialize' => [ + 'replacement' => 'Magento\Framework\Serialize\SerializerInterface', + 'exclude' => [ + [ + 'type' => 'library', + 'name' => 'magento/framework', + 'path' => 'DB/Adapter/Pdo/Mysql.php' + ], + [ + 'type' => 'library', + 'name' => 'magento/framework', + 'path' => 'App/ObjectManager/ConfigLoader/Compiled.php' + ], + [ + 'type' => 'library', + 'name' => 'magento/framework', + 'path' => 'App/Config/ScopePool.php'], + [ + 'type' => 'library', + 'name' => 'magento/framework', + 'path' => 'App/ObjectManager/ConfigCache.php' + ], + [ + 'type' => 'library', + 'name' => 'magento/framework', + 'path' => 'App/ObjectManager/ConfigLoader.php' + ], + [ + 'type' => 'library', + 'name' => 'magento/framework', + 'path' => 'ObjectManager/Config/Compiled.php' + ], + [ + 'type' => 'library', + 'name' => 'magento/framework', + 'path' => 'Interception/Config/Config.php' + ], + [ + 'type' => 'library', + 'name' => 'magento/framework', + 'path' => 'Interception/PluginList/PluginList.php' + ], + [ + 'type' => 'library', + 'name' => 'magento/framework', + 'path' => 'App/Router/ActionList.php' + ], + [ + 'type' => 'library', + 'name' => 'magento/framework', + 'path' => 'Serialize/Test/Unit/Serializer/SerializeTest.php' + ], + [ + 'type' => 'setup', + 'path' => 'src/Magento/Setup/Module/Di/Compiler/Config/Writer/Filesystem.php' + ], + ] + ] ]; diff --git a/dev/tests/static/testsuite/Magento/Test/Legacy/_files/security/blacklist.php b/dev/tests/static/testsuite/Magento/Test/Legacy/_files/security/blacklist.php deleted file mode 100644 index 42b8e68e784111b96d43049ae27dc5ebc963ad40..0000000000000000000000000000000000000000 --- a/dev/tests/static/testsuite/Magento/Test/Legacy/_files/security/blacklist.php +++ /dev/null @@ -1,10 +0,0 @@ -<?php -/** - * Copyright © 2016 Magento. All rights reserved. - * See COPYING.txt for license details. - */ -return [ - 'Test/Unit', - 'lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php', - 'lib/internal/Magento/Framework/Serialize/Serializer/Serialize.php', -]; diff --git a/dev/tests/static/testsuite/Magento/Test/Legacy/_files/security/unsecure_php_functions.php b/dev/tests/static/testsuite/Magento/Test/Legacy/_files/security/unsecure_php_functions.php new file mode 100644 index 0000000000000000000000000000000000000000..eb51d1bbba3bd98ec7ef2d5fd26754aea8fc221a --- /dev/null +++ b/dev/tests/static/testsuite/Magento/Test/Legacy/_files/security/unsecure_php_functions.php @@ -0,0 +1,52 @@ +<?php +/** + * Copyright © 2016 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +/** + * Functions that are not secure to use. + * A <replacement> will be suggested to be used instead. + * Use <exclude> to specify files and directories that are allowed to use function. + * + * Format: [ + * <class_name> => [ + * 'replacement' => <replacement>, + * 'exclude' => [ + * <exclude>, + * <exclude>, + * ] + * ] + */ +return [ + 'unserialize' => [ + 'replacement' => '\Magento\Framework\Serialize\SerializerInterface::unserialize', + 'exclude' => [ + ['type' => 'library', 'name' => 'magento/framework', 'path' => 'DB/Adapter/Pdo/Mysql.php'], + ['type' => 'library', 'name' => 'magento/framework', 'path' => 'Serialize/Serializer/Serialize.php'], + ] + ], + 'serialize' => [ + 'replacement' => '\Magento\Framework\Serialize\SerializerInterface::serialize', + 'exclude' => [ + ['type' => 'library', 'name' => 'magento/framework', 'path' => 'DB/Adapter/Pdo/Mysql.php'], + ['type' => 'library', 'name' => 'magento/framework', 'path' => 'Serialize/Serializer/Serialize.php'], + ] + ], + 'eval' => [ + 'replacement' => '', + 'exclude' => [] + ], + 'md5' => [ + 'replacement' => '', + 'exclude' => [] + ], + 'srand' => [ + 'replacement' => '', + 'exclude' => [] + ], + 'mt_srand' => [ + 'replacement' => '', + 'exclude' => [] + ], +]; diff --git a/lib/internal/Magento/Framework/Indexer/Config/Converter.php b/lib/internal/Magento/Framework/Indexer/Config/Converter.php index b8b17b185a1f2630842d6fe6e01b6a9de2a52107..0112b4d9a4de301a312e3e66bd146d8f477cd349 100644 --- a/lib/internal/Magento/Framework/Indexer/Config/Converter.php +++ b/lib/internal/Magento/Framework/Indexer/Config/Converter.php @@ -72,10 +72,10 @@ class Converter implements ConverterInterface $data['fieldsets'] = isset($data['fieldsets']) ? $data['fieldsets'] : []; switch ($childNode->nodeName) { case 'title': - $data['title'] = $this->getTranslatedNodeValue($childNode); + $data['title'] = $childNode->nodeValue; break; case 'description': - $data['description'] = $this->getTranslatedNodeValue($childNode); + $data['description'] = $childNode->nodeValue; break; case 'saveHandler': $data['saveHandler'] = $this->getAttributeValue($childNode, 'class'); @@ -207,6 +207,7 @@ class Converter implements ConverterInterface * * @param \DOMNode $node * @return string + * @deprecated */ protected function getTranslatedNodeValue(\DOMNode $node) {