diff --git a/dev/tests/integration/testsuite/Magento/Setup/Console/Command/I18nCollectPhrasesCommandTest.php b/dev/tests/integration/testsuite/Magento/Setup/Console/Command/I18nCollectPhrasesCommandTest.php index 924853df6bc0c75cb219ec88afb42317c33d4f1d..2d5e9026ceefe6e4853e08e3e3859b681e6ab6ea 100644 --- a/dev/tests/integration/testsuite/Magento/Setup/Console/Command/I18nCollectPhrasesCommandTest.php +++ b/dev/tests/integration/testsuite/Magento/Setup/Console/Command/I18nCollectPhrasesCommandTest.php @@ -46,17 +46,19 @@ class I18nCollectPhrasesCommandTest extends \PHPUnit_Framework_TestCase public function testExecuteCsvOutput() { - $outputPath = BP . '/dev/tests/integration/testsuite/Magento/Setup/Console/Command/_files/output/output.csv'; + $outputPath = BP . '/dev/tests/integration/testsuite/Magento/Setup/Console/Command/_files/output/phrases.csv'; $this->tester->execute( [ - 'directory' => BP . '/dev/tests/integration/testsuite/Magento/Setup/Console/Command/_files/', + 'directory' => BP . '/dev/tests/integration/testsuite/Magento/Setup/Console/Command/_files/phrases/', '--output' => $outputPath, ] ); $handle = fopen($outputPath, 'r'); $output = fread($handle, filesize($outputPath)); - $expected = '"Hello world","Hello world"' . PHP_EOL . '"Foo bar","Foo bar"' . PHP_EOL; + $expected = file_get_contents( + BP . '/dev/tests/integration/testsuite/Magento/Setup/Console/Command/_files/expectedPhrases.csv' + ); $this->assertEquals($expected, $output); unlink($outputPath); } diff --git a/dev/tests/integration/testsuite/Magento/Setup/Console/Command/_files/Magento/TestModule/Phrases.php b/dev/tests/integration/testsuite/Magento/Setup/Console/Command/_files/Magento/TestModule/Phrases.php deleted file mode 100644 index d55885ff96f2b8da7acce4f97c505fa2a6b8aa76..0000000000000000000000000000000000000000 --- a/dev/tests/integration/testsuite/Magento/Setup/Console/Command/_files/Magento/TestModule/Phrases.php +++ /dev/null @@ -1,7 +0,0 @@ -<?php -/** - * Copyright © 2015 Magento. All rights reserved. - * See COPYING.txt for license details. - */ -$a = __("Hello world"); -$b = __("Foo bar"); diff --git a/dev/tests/integration/testsuite/Magento/Setup/Console/Command/_files/expectedPhrases.csv b/dev/tests/integration/testsuite/Magento/Setup/Console/Command/_files/expectedPhrases.csv new file mode 100644 index 0000000000000000000000000000000000000000..43c331ecb48d6faa3e12d04eccb53d992779e907 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Setup/Console/Command/_files/expectedPhrases.csv @@ -0,0 +1,19 @@ +"simple text","simple text" +"simple text with 1 string literal placeholder %1","simple text with 1 string literal placeholder %1" +"simple text with 1 variable placeholder %1","simple text with 1 variable placeholder %1" +"simple text with multiple placeholders %1 %2","simple text with multiple placeholders %1 %2" +"first part second part","first part second part" +"first part second part third part","first part second part third part" +"first part second part with one string literal placeholder %1","first part second part with one string literal placeholder %1" +"first part of concat second part with one variable placeholder %1","first part of concat second part with one variable placeholder %1" +"first part of concat second part with two placeholders %1, %2","first part of concat second part with two placeholders %1, %2" +"first part of concat second part third part with one placeholder %1","first part of concat second part third part with one placeholder %1" +"first part of concat second part third part with two placeholders %1, %2","first part of concat second part third part with two placeholders %1, %2" +"string with escaped 'single quotes'","string with escaped 'single quotes'" +"string with placeholder in escaped single quotes '%1'","string with placeholder in escaped single quotes '%1'" +"string with ""double quotes""","string with ""double quotes""" +"string with placeholder in double quotes ""%1""","string with placeholder in double quotes ""%1""" +"string with 'single quotes'","string with 'single quotes'" +"string with placeholder in single quotes '%1'","string with placeholder in single quotes '%1'" +"string with escaped ""double quotes""","string with escaped ""double quotes""" +"string with placeholder in escaped double quotes ""%1""","string with placeholder in escaped double quotes ""%1""" diff --git a/dev/tests/integration/testsuite/Magento/Setup/Console/Command/_files/phrases/TestPhrases.php b/dev/tests/integration/testsuite/Magento/Setup/Console/Command/_files/phrases/TestPhrases.php new file mode 100644 index 0000000000000000000000000000000000000000..f3306a521add5a671e626d3cd49c19c9a6640c5f --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Setup/Console/Command/_files/phrases/TestPhrases.php @@ -0,0 +1,87 @@ +<?php +/*** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +// @codingStandardsIgnoreFile + +use Magento\Framework\Phrase; + +/** + * @SuppressWarnings(PHPMD) + */ +class TestPhrases +{ + public function awesomeFunction() + { + $str1 = 'str1'; + $str2 = 'str2'; + + // Simple + $simpleCases = [ + new Phrase('simple text'), + new Phrase('simple text with 1 string literal placeholder %1', 'arg'), + new Phrase('simple text with 1 variable placeholder %1', $str1), + new Phrase('simple text with multiple placeholders %1 %2', $str1, $str2), + ]; + + // Phrase objects + $phraseObjects = [ + // Single concatenation + new Phrase('first part' . ' second part'), + new Phrase('first part' . ' second part' . ' third part'), + + // Multiple concatenation + new Phrase('first part' . ' second part with one string literal placeholder %1', 'arg'), + new Phrase('first part of concat' . ' second part with one variable placeholder %1', $str1), + new Phrase('first part of concat' . ' second part with two placeholders %1, %2', $str1, $str2), + new Phrase('first part of concat' . ' second part' . ' third part with one placeholder %1', 'arg'), + new Phrase('first part of concat' . ' second part' . ' third part with two placeholders %1, %2', $str1, $str2), + + // Escaped quotes + new Phrase('string with escaped \'single quotes\''), + new Phrase('string with placeholder in escaped single quotes \'%1\'', 'arg'), + new Phrase('string with "double quotes"'), + new Phrase('string with placeholder in double quotes "%1"', 'arg'), + ]; + + $singleQuoteTranslateFunctions = [ + // Single concatenation + __('first part' . ' second part'), + __('first part' . ' second part' . ' third part'), + + // Multiple concatenation + __('first part' . ' second part with one string literal placeholder %1', 'arg'), + __('first part of concat' . ' second part with one variable placeholder %1', $str1), + __('first part of concat' . ' second part with two placeholders %1, %2', $str1, $str2), + __('first part of concat' . ' second part' . ' third part with one placeholder %1', 'arg'), + __('first part of concat' . ' second part' . ' third part with two placeholders %1, %2', $str1, $str2), + + // Escaped quotes + __('string with escaped \'single quotes\''), + __('string with placeholder in escaped single quotes \'%1\'', 'arg'), + __('string with "double quotes"'), + __('string with placeholder in double quotes "%1"', 'arg'), + ]; + + $doubleQuoteTranslateFunctions = [ + // Single concatenation + __("first part" . " second part"), + __("first part" . " second part" . " third part"), + + // Multiple concatenation + __("first part" . " second part with one string literal placeholder %1", "arg"), + __("first part of concat" . " second part with one variable placeholder %1", $str1), + __("first part of concat" . " second part with two placeholders %1, %2", $str1, $str2), + __("first part of concat" . " second part" . " third part with one placeholder %1", "arg"), + __("first part of concat" . " second part" . " third part with two placeholders %1, %2", $str1, $str2), + + // Escaped quotes + __("string with 'single quotes'"), + __("string with placeholder in single quotes '%1'", "arg"), + __("string with escaped \"double quotes\""), + __("string with placeholder in escaped double quotes \"%1\"", "arg"), + ]; + } +} diff --git a/setup/src/Magento/Setup/Module/I18n/Parser/Adapter/Php/Tokenizer/PhraseCollector.php b/setup/src/Magento/Setup/Module/I18n/Parser/Adapter/Php/Tokenizer/PhraseCollector.php index ee7ecd15e0c5fdc8ed9f24c2eb4a3092dffe0371..96f7c84fd7e7a611579f13e153d37f0cd1b87af9 100644 --- a/setup/src/Magento/Setup/Module/I18n/Parser/Adapter/Php/Tokenizer/PhraseCollector.php +++ b/setup/src/Magento/Setup/Module/I18n/Parser/Adapter/Php/Tokenizer/PhraseCollector.php @@ -139,7 +139,7 @@ class PhraseCollector if ($phraseTokens) { /** @var \Magento\Setup\Module\I18n\Parser\Adapter\Php\Tokenizer\Token $phraseToken */ foreach ($phraseTokens as $phraseToken) { - if ($phraseToken->isConstantEncapsedString()) { + if ($phraseToken->isConstantEncapsedString() || $phraseToken->isConcatenateOperator()) { $phrase[] = $phraseToken->getValue(); } } diff --git a/setup/src/Magento/Setup/Module/I18n/Parser/Adapter/Php/Tokenizer/Token.php b/setup/src/Magento/Setup/Module/I18n/Parser/Adapter/Php/Tokenizer/Token.php index a36b8b1e885307150ba4ef9d95ccd8381bfd2137..0477da1bcd3ce5f490c19c7e058e35d89b92f0f2 100644 --- a/setup/src/Magento/Setup/Module/I18n/Parser/Adapter/Php/Tokenizer/Token.php +++ b/setup/src/Magento/Setup/Module/I18n/Parser/Adapter/Php/Tokenizer/Token.php @@ -166,6 +166,14 @@ class Token return $this->getValue() == ';'; } + /** + * @return bool + */ + public function isConcatenateOperator() + { + return $this->getValue() == '.'; + } + /** * Is namespace separator * diff --git a/setup/src/Magento/Setup/Test/Unit/Module/I18n/Parser/Adapter/Php/Tokenizer/PhraseCollectorTest.php b/setup/src/Magento/Setup/Test/Unit/Module/I18n/Parser/Adapter/Php/Tokenizer/PhraseCollectorTest.php index 954bcc013ade66bfdd3c10c10597fe205f6f5e58..b4f7d60a1ca113b43c289ed4cc57e0bfc471a2b7 100644 --- a/setup/src/Magento/Setup/Test/Unit/Module/I18n/Parser/Adapter/Php/Tokenizer/PhraseCollectorTest.php +++ b/setup/src/Magento/Setup/Test/Unit/Module/I18n/Parser/Adapter/Php/Tokenizer/PhraseCollectorTest.php @@ -204,4 +204,23 @@ class PhraseCollectorTest extends \PHPUnit_Framework_TestCase ->willReturn($line); return $token; } + + public function testCollectPhrases() + { + $firstPart = "'first part'"; + $firstPartToken = new Token(\T_CONSTANT_ENCAPSED_STRING, $firstPart); + $concatenationToken = new Token('.', '.'); + $secondPart = "' second part'"; + $secondPartToken = new Token(\T_CONSTANT_ENCAPSED_STRING, $secondPart); + $phraseTokens = [$firstPartToken, $concatenationToken, $secondPartToken]; + $phraseString = "'first part' . ' second part'"; + + $reflectionMethod = new \ReflectionMethod( + '\Magento\Setup\Module\I18n\Parser\Adapter\Php\Tokenizer\PhraseCollector', + '_collectPhrase' + ); + + $reflectionMethod->setAccessible(true); + $this->assertSame($phraseString, $reflectionMethod->invoke($this->phraseCollector, $phraseTokens)); + } } diff --git a/setup/src/Magento/Setup/Test/Unit/Module/I18n/Parser/Adapter/Php/Tokenizer/TokenTest.php b/setup/src/Magento/Setup/Test/Unit/Module/I18n/Parser/Adapter/Php/Tokenizer/TokenTest.php index 26bcea7516dd2495526e0b75202c3082591a77c6..c29edab1c442d5d96398424fd00be82251c223af 100644 --- a/setup/src/Magento/Setup/Test/Unit/Module/I18n/Parser/Adapter/Php/Tokenizer/TokenTest.php +++ b/setup/src/Magento/Setup/Test/Unit/Module/I18n/Parser/Adapter/Php/Tokenizer/TokenTest.php @@ -118,4 +118,16 @@ class TokenTest extends \PHPUnit_Framework_TestCase ] ); } + + public function testIsConcatenateOperatorTrue() + { + $token = new \Magento\Setup\Module\I18n\Parser\Adapter\Php\Tokenizer\Token('.', '.'); + $this->assertTrue($token->isConcatenateOperator()); + } + + public function testIsConcatenateOperatorFalse() + { + $token = new \Magento\Setup\Module\I18n\Parser\Adapter\Php\Tokenizer\Token(',', ','); + $this->assertFalse($token->isConcatenateOperator()); + } }