| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426 |
- <?php
- declare(strict_types=1);
- /**
- * CakePHP : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- * @link https://cakephp.org CakePHP Project
- * @since 1.2.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Command;
- use Cake\Console\TestSuite\ConsoleIntegrationTestTrait;
- use Cake\Core\Configure;
- use Cake\TestSuite\TestCase;
- use Cake\Utility\Filesystem;
- /**
- * I18nExtractCommandTest
- */
- class I18nExtractCommandTest extends TestCase
- {
- use ConsoleIntegrationTestTrait;
- /**
- * @var string
- */
- protected $path;
- /**
- * setUp method
- */
- public function setUp(): void
- {
- parent::setUp();
- $this->setAppNamespace();
- $this->path = TMP . 'tests/extract_task_test';
- $fs = new Filesystem();
- $fs->deleteDir($this->path);
- $fs->mkdir($this->path . DS . 'locale');
- }
- /**
- * tearDown method
- */
- public function tearDown(): void
- {
- parent::tearDown();
- $fs = new Filesystem();
- $fs->deleteDir($this->path);
- $this->clearPlugins();
- }
- /**
- * testExecute method
- */
- public function testExecute(): void
- {
- $this->exec(
- 'i18n extract ' .
- '--merge=no ' .
- '--extract-core=no ' .
- '--paths=' . TEST_APP . 'templates' . DS . 'Pages ' .
- '--output=' . $this->path . DS
- );
- $this->assertExitSuccess();
- $this->assertFileExists($this->path . DS . 'default.pot');
- $result = file_get_contents($this->path . DS . 'default.pot');
- $this->assertFileDoesNotExist($this->path . DS . 'cake.pot');
- // The additional "./tests/test_app" is just due to the wonky folder structure of the test app.
- // In a regular app the path would start with "./templates".
- $pattern = '@\#: \./tests/test_app/templates/Pages/extract\.php:\d+\n';
- $pattern .= '\#: \./tests/test_app/templates/Pages/extract\.php:\d+\n';
- $pattern .= 'msgid "You have %d new message."\nmsgid_plural "You have %d new messages."@';
- $this->assertMatchesRegularExpression($pattern, $result);
- $pattern = '/msgid "You have %d new message."\nmsgstr ""/';
- $this->assertDoesNotMatchRegularExpression($pattern, $result, 'No duplicate msgid');
- $pattern = '@\#: \./tests/test_app/templates/Pages/extract\.php:\d+\n';
- $pattern .= 'msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."@';
- $this->assertMatchesRegularExpression($pattern, $result);
- $pattern = '@\#: \./tests/test_app/templates/Pages/extract\.php:\d+\nmsgid "';
- $pattern .= 'Hot features!';
- $pattern .= '\\\n - No Configuration: Set-up the database and let the magic begin';
- $pattern .= '\\\n - Extremely Simple: Just look at the name...It\'s Cake';
- $pattern .= '\\\n - Active, Friendly Community: Join us #cakephp on IRC. We\'d love to help you get started';
- $pattern .= '"\nmsgstr ""@';
- $this->assertMatchesRegularExpression($pattern, $result);
- $this->assertStringContainsString('msgid "double \\"quoted\\""', $result, 'Strings with quotes not handled correctly');
- $this->assertStringContainsString("msgid \"single 'quoted'\"", $result, 'Strings with quotes not handled correctly');
- $pattern = '@\#: \./tests/test_app/templates/Pages/extract\.php:\d+\n';
- $pattern .= 'msgctxt "mail"\n';
- $pattern .= 'msgid "letter"@';
- $this->assertMatchesRegularExpression($pattern, $result);
- $pattern = '@\#: \./tests/test_app/templates/Pages/extract\.php:\d+\n';
- $pattern .= 'msgctxt "alphabet"\n';
- $pattern .= 'msgid "letter"@';
- $this->assertMatchesRegularExpression($pattern, $result);
- // extract.php - reading the domain.pot
- $result = file_get_contents($this->path . DS . 'domain.pot');
- $pattern = '/msgid "You have %d new message."\nmsgid_plural "You have %d new messages."/';
- $this->assertDoesNotMatchRegularExpression($pattern, $result);
- $pattern = '/msgid "You deleted %d message."\nmsgid_plural "You deleted %d messages."/';
- $this->assertDoesNotMatchRegularExpression($pattern, $result);
- $pattern = '/msgid "You have %d new message \(domain\)."\nmsgid_plural "You have %d new messages \(domain\)."/';
- $this->assertMatchesRegularExpression($pattern, $result);
- $pattern = '/msgid "You deleted %d message \(domain\)."\nmsgid_plural "You deleted %d messages \(domain\)."/';
- $this->assertMatchesRegularExpression($pattern, $result);
- }
- /**
- * testExecute with no paths
- */
- public function testExecuteNoPathOption(): void
- {
- $this->exec(
- 'i18n extract ' .
- '--merge=no ' .
- '--extract-core=no ' .
- '--output=' . $this->path . DS,
- [
- TEST_APP . 'templates' . DS,
- 'D',
- ]
- );
- $this->assertExitSuccess();
- $this->assertFileExists($this->path . DS . 'default.pot');
- }
- /**
- * testExecute with merging on method
- */
- public function testExecuteMerge(): void
- {
- $this->exec(
- 'i18n extract ' .
- '--merge=yes ' .
- '--extract-core=no ' .
- '--paths=' . TEST_APP . 'templates' . DS . 'Pages ' .
- '--output=' . $this->path . DS
- );
- $this->assertExitSuccess();
- $this->assertFileExists($this->path . DS . 'default.pot');
- $this->assertFileDoesNotExist($this->path . DS . 'cake.pot');
- $this->assertFileDoesNotExist($this->path . DS . 'domain.pot');
- }
- /**
- * test exclusions
- */
- public function testExtractWithExclude(): void
- {
- $this->exec(
- 'i18n extract ' .
- '--extract-core=no ' .
- '--exclude=Pages,Layout ' .
- '--paths=' . TEST_APP . 'templates' . DS . ' ' .
- '--output=' . $this->path . DS
- );
- $this->assertExitSuccess();
- $this->assertFileExists($this->path . DS . 'default.pot');
- $result = file_get_contents($this->path . DS . 'default.pot');
- $pattern = '/\#: .*extract\.php:\d+\n/';
- $this->assertDoesNotMatchRegularExpression($pattern, $result);
- $pattern = '/\#: .*default\.php:\d+\n/';
- $this->assertDoesNotMatchRegularExpression($pattern, $result);
- }
- /**
- * testExtractWithoutLocations method
- */
- public function testExtractWithoutLocations(): void
- {
- $this->exec(
- 'i18n extract ' .
- '--extract-core=no ' .
- '--no-location=true ' .
- '--exclude=Pages,Layout ' .
- '--paths=' . TEST_APP . 'templates' . DS . ' ' .
- '--output=' . $this->path . DS
- );
- $this->assertExitSuccess();
- $this->assertFileExists($this->path . DS . 'default.pot');
- $result = file_get_contents($this->path . DS . 'default.pot');
- $pattern = '/\n\#: .*\n/';
- $this->assertDoesNotMatchRegularExpression($pattern, $result);
- }
- /**
- * test extract can read more than one path.
- */
- public function testExtractMultiplePaths(): void
- {
- $this->exec(
- 'i18n extract ' .
- '--extract-core=no ' .
- '--exclude=Pages,Layout ' .
- '--paths=' . TEST_APP . 'templates/Pages,' .
- TEST_APP . 'templates/Posts ' .
- '--output=' . $this->path . DS
- );
- $this->assertExitSuccess();
- $result = file_get_contents($this->path . DS . 'default.pot');
- $pattern = '/msgid "Add User"/';
- $this->assertMatchesRegularExpression($pattern, $result);
- }
- /**
- * Tests that it is possible to exclude plugin paths by enabling the param option for the ExtractTask
- */
- public function testExtractExcludePlugins(): void
- {
- static::setAppNamespace();
- $this->exec(
- 'i18n extract ' .
- '--extract-core=no ' .
- '--exclude-plugins=true ' .
- '--paths=' . TEST_APP . 'TestApp/ ' .
- '--output=' . $this->path . DS
- );
- $this->assertExitSuccess();
- $result = file_get_contents($this->path . DS . 'default.pot');
- $this->assertDoesNotMatchRegularExpression('#TestPlugin#', $result);
- }
- /**
- * Test that is possible to extract messages from a single plugin
- */
- public function testExtractPlugin(): void
- {
- Configure::write('Plugins.autoload', ['TestPlugin']);
- $this->exec(
- 'i18n extract ' .
- '--extract-core=no ' .
- '--plugin=TestPlugin ' .
- '--output=' . $this->path . DS
- );
- $this->assertExitSuccess();
- $result = file_get_contents($this->path . DS . 'default.pot');
- $this->assertDoesNotMatchRegularExpression('#Pages#', $result);
- $this->assertMatchesRegularExpression('/translate\.php:\d+/', $result);
- $this->assertStringContainsString('This is a translatable string', $result);
- }
- /**
- * Test that is possible to extract messages from a vendor prefixed plugin.
- */
- public function testExtractVendorPrefixedPlugin(): void
- {
- $this->pluginsToLoad(['Company/TestPluginThree']);
- $this->exec(
- 'i18n extract ' .
- '--extract-core=no ' .
- '--plugin=Company/TestPluginThree ' .
- '--output=' . $this->path . DS
- );
- $this->assertExitSuccess();
- $result = file_get_contents($this->path . DS . 'company_test_plugin_three.pot');
- $this->assertDoesNotMatchRegularExpression('#Pages#', $result);
- $this->assertMatchesRegularExpression('/default\.php:\d+/', $result);
- $this->assertStringContainsString('A vendor message', $result);
- }
- /**
- * Test that the extract shell overwrites existing files with the overwrite parameter
- */
- public function testExtractOverwrite(): void
- {
- file_put_contents($this->path . DS . 'default.pot', 'will be overwritten');
- $this->assertFileExists($this->path . DS . 'default.pot');
- $original = file_get_contents($this->path . DS . 'default.pot');
- $this->exec(
- 'i18n extract ' .
- '--extract-core=no ' .
- '--overwrite ' .
- '--paths=' . TEST_APP . 'TestApp/ ' .
- '--output=' . $this->path . DS
- );
- $this->assertExitSuccess();
- $result = file_get_contents($this->path . DS . 'default.pot');
- $this->assertNotEquals($original, $result);
- }
- /**
- * Test that the extract shell scans the core libs
- */
- public function testExtractCore(): void
- {
- $this->exec(
- 'i18n extract ' .
- '--extract-core=yes ' .
- '--paths=' . TEST_APP . 'TestApp/ ' .
- '--output=' . $this->path . DS
- );
- $this->assertExitSuccess();
- $this->assertFileExists($this->path . DS . 'cake.pot');
- $result = file_get_contents($this->path . DS . 'cake.pot');
- $pattern = '/#: Console\/Templates\//';
- $this->assertDoesNotMatchRegularExpression($pattern, $result);
- $pattern = '/#: Test\//';
- $this->assertDoesNotMatchRegularExpression($pattern, $result);
- }
- /**
- * Test when marker-error option is set
- * When marker-error is unset, it's already test
- * with other functions like testExecute that not detects error because err never called
- */
- public function testMarkerErrorSets(): void
- {
- $this->exec(
- 'i18n extract ' .
- '--marker-error ' .
- '--merge=no ' .
- '--extract-core=no ' .
- '--paths=' . TEST_APP . 'templates/Pages ' .
- '--output=' . $this->path . DS
- );
- $this->assertExitSuccess();
- $this->assertErrorContains('Invalid marker content in');
- $this->assertErrorContains('extract.php');
- }
- /**
- * test relative-paths option
- */
- public function testExtractWithRelativePaths(): void
- {
- $this->exec(
- 'i18n extract ' .
- '--extract-core=no ' .
- '--paths=' . TEST_APP . 'templates ' .
- '--output=' . $this->path . DS
- );
- $this->assertExitSuccess();
- $this->assertFileExists($this->path . DS . 'default.pot');
- $result = file_get_contents($this->path . DS . 'default.pot');
- $expected = '#: ./tests/test_app/templates/Pages/extract.php:';
- $this->assertStringContainsString($expected, $result);
- }
- /**
- * test invalid path options
- */
- public function testExtractWithInvalidPaths(): void
- {
- $this->exec(
- 'i18n extract ' .
- '--extract-core=no ' .
- '--paths=' . TEST_APP . 'templates,' . TEST_APP . 'unknown ' .
- '--output=' . $this->path . DS
- );
- $this->assertExitSuccess();
- $this->assertFileExists($this->path . DS . 'default.pot');
- $result = file_get_contents($this->path . DS . 'default.pot');
- $expected = '#: ./tests/test_app/templates/Pages/extract.php:';
- $this->assertStringContainsString($expected, $result);
- }
- /**
- * Test with associative arrays in App.path.locales and App.path.templates.
- */
- public function testExtractWithAssociativePaths(): void
- {
- Configure::write('App.paths', [
- 'plugins' => ['customKey' => TEST_APP . 'Plugin' . DS],
- 'templates' => ['customKey' => TEST_APP . 'templates' . DS],
- 'locales' => ['customKey' => TEST_APP . 'resources' . DS . 'locales' . DS],
- ]);
- $this->exec(
- 'i18n extract ' .
- '--merge=no ' .
- '--extract-core=no ',
- [
- // Sending two empty inputs so \Cake\Command\I18nExtractCommand::_getPaths()
- // loops through all paths
- '',
- '',
- 'D',
- $this->path . DS,
- ]
- );
- $this->assertExitSuccess();
- $this->assertFileExists($this->path . DS . 'default.pot');
- $result = file_get_contents($this->path . DS . 'default.pot');
- $expected = '#: ./tests/test_app/templates/Pages/extract.php:';
- $this->assertStringContainsString($expected, $result);
- }
- }
|