Browse Source

Merge pull request #17154 from fabian-mcfly/4.x

[4.4] Use array values of app paths returned by App::path() for I18n commands
Mark Story 2 years ago
parent
commit
e85d5861d3

+ 2 - 2
src/Command/I18nExtractCommand.php

@@ -133,7 +133,7 @@ class I18nExtractCommand extends Command
         /** @psalm-suppress UndefinedConstant */
         $defaultPaths = array_merge(
             [APP],
-            App::path('templates'),
+            array_values(App::path('templates')),
             ['D'] // This is required to break the loop below
         );
         $defaultPathIndex = 0;
@@ -217,7 +217,7 @@ class I18nExtractCommand extends Command
                 . 'locales' . DIRECTORY_SEPARATOR;
         } else {
             $message = "What is the path you would like to output?\n[Q]uit";
-            $localePaths = App::path('locales');
+            $localePaths = array_values(App::path('locales'));
             if (!$localePaths) {
                 $localePaths[] = ROOT . 'resources' . DIRECTORY_SEPARATOR . 'locales';
             }

+ 1 - 1
src/Command/I18nInitCommand.php

@@ -56,7 +56,7 @@ class I18nInitCommand extends Command
             return static::CODE_ERROR;
         }
 
-        $paths = App::path('locales');
+        $paths = array_values(App::path('locales'));
         if ($args->hasOption('plugin')) {
             $plugin = Inflector::camelize((string)$args->getOption('plugin'));
             $paths = [Plugin::path($plugin) . 'resources' . DIRECTORY_SEPARATOR . 'locales' . DIRECTORY_SEPARATOR];

+ 32 - 0
tests/TestCase/Command/I18nCommandTest.php

@@ -17,6 +17,7 @@ declare(strict_types=1);
 namespace Cake\Test\TestCase\Command;
 
 use Cake\Console\TestSuite\ConsoleIntegrationTestTrait;
+use Cake\Core\Configure;
 use Cake\TestSuite\TestCase;
 
 /**
@@ -76,10 +77,41 @@ class I18nCommandTest extends TestCase
         if (file_exists($deDir . 'default.po')) {
             unlink($deDir . 'default.po');
         }
+        if (file_exists($deDir . 'cake.po')) {
+            unlink($deDir . 'cake.po');
+        }
+
+        $this->exec('i18n init --verbose', [
+            'de_DE',
+            $this->localeDir,
+        ]);
+
+        $this->assertExitSuccess();
+        $this->assertOutputContains('Generated 2 PO files');
+        $this->assertFileExists($deDir . 'default.po');
+        $this->assertFileExists($deDir . 'cake.po');
+    }
+
+    /**
+     * Tests that init() creates the PO files from POT files when App.path.locales contains an associative array
+     */
+    public function testInitWithAssociativePaths(): void
+    {
+        $deDir = $this->localeDir . 'de_DE' . DS;
+        if (!is_dir($deDir)) {
+            mkdir($deDir, 0770, true);
+        }
+        file_put_contents($this->localeDir . 'default.pot', 'Testing POT file.');
+        file_put_contents($this->localeDir . 'cake.pot', 'Testing POT file.');
         if (file_exists($deDir . 'default.po')) {
+            unlink($deDir . 'default.po');
+        }
+        if (file_exists($deDir . 'cake.po')) {
             unlink($deDir . 'cake.po');
         }
 
+        Configure::write('App.paths.locales', ['customKey' => TEST_APP . 'resources' . DS . 'locales' . DS]);
+
         $this->exec('i18n init --verbose', [
             'de_DE',
             $this->localeDir,

+ 32 - 0
tests/TestCase/Command/I18nExtractCommandTest.php

@@ -392,4 +392,36 @@ class I18nExtractCommandTest extends TestCase
         $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);
+    }
 }

+ 0 - 23
tests/TestCase/I18n/MessagesFileLoaderTest.php

@@ -26,29 +26,6 @@ use Cake\TestSuite\TestCase;
 class MessagesFileLoaderTest extends TestCase
 {
     /**
-     * @var string[]
-     */
-    protected $localePaths;
-
-    /**
-     * Set Up
-     */
-    public function setUp(): void
-    {
-        parent::setUp();
-        $this->localePaths = Configure::read('App.paths.locales');
-    }
-
-    /**
-     * Tear down method
-     */
-    public function tearDown(): void
-    {
-        parent::tearDown();
-        Configure::write('App.paths.locales', $this->localePaths);
-    }
-
-    /**
      * test reading file from custom locale folder
      */
     public function testCustomLocalePath(): void

+ 0 - 15
tests/TestCase/Utility/XmlTest.php

@@ -35,29 +35,14 @@ use TypeError;
 class XmlTest extends TestCase
 {
     /**
-     * @var string
-     */
-    protected $appEncoding;
-
-    /**
      * setUp method
      */
     public function setUp(): void
     {
         parent::setUp();
-        $this->appEncoding = Configure::read('App.encoding');
         Configure::write('App.encoding', 'UTF-8');
     }
 
-    /**
-     * tearDown method
-     */
-    public function tearDown(): void
-    {
-        parent::tearDown();
-        Configure::write('App.encoding', $this->appEncoding);
-    }
-
     public function testExceptionChainingForInvalidInput(): void
     {
         try {

+ 0 - 15
tests/TestCase/Validation/ValidationTest.php

@@ -37,26 +37,11 @@ require_once __DIR__ . '/stubs.php';
 class ValidationTest extends TestCase
 {
     /**
-     * @var string
-     */
-    protected $_appEncoding;
-
-    /**
-     * setUp method
-     */
-    public function setUp(): void
-    {
-        parent::setUp();
-        $this->_appEncoding = Configure::read('App.encoding');
-    }
-
-    /**
      * tearDown method
      */
     public function tearDown(): void
     {
         parent::tearDown();
-        Configure::write('App.encoding', $this->_appEncoding);
         I18n::setLocale(I18n::getDefaultLocale());
     }
 

+ 0 - 9
tests/TestCase/View/Helper/NumberHelperTest.php

@@ -18,7 +18,6 @@ declare(strict_types=1);
  */
 namespace Cake\Test\TestCase\View\Helper;
 
-use Cake\Core\Configure;
 use Cake\I18n\Number;
 use Cake\TestSuite\TestCase;
 use Cake\View\Helper\NumberHelper;
@@ -41,19 +40,12 @@ class NumberHelperTest extends TestCase
     protected $View;
 
     /**
-     * @var string
-     */
-    protected $appNamespace;
-
-    /**
      * setUp method
      */
     public function setUp(): void
     {
         parent::setUp();
         $this->View = new View();
-
-        $this->appNamespace = Configure::read('App.namespace');
         static::setAppNamespace();
     }
 
@@ -64,7 +56,6 @@ class NumberHelperTest extends TestCase
     {
         parent::tearDown();
         $this->clearPlugins();
-        static::setAppNamespace($this->appNamespace);
         unset($this->View);
     }
 

+ 0 - 9
tests/TestCase/View/Helper/TextHelperTest.php

@@ -16,7 +16,6 @@ declare(strict_types=1);
  */
 namespace Cake\Test\TestCase\View\Helper;
 
-use Cake\Core\Configure;
 use Cake\TestSuite\TestCase;
 use Cake\View\Helper\TextHelper;
 use Cake\View\View;
@@ -41,11 +40,6 @@ class TextHelperTest extends TestCase
     protected $View;
 
     /**
-     * @var string
-     */
-    protected $appNamespace;
-
-    /**
      * setUp method
      */
     public function setUp(): void
@@ -53,8 +47,6 @@ class TextHelperTest extends TestCase
         parent::setUp();
         $this->View = new View();
         $this->Text = new TextHelper($this->View);
-
-        $this->appNamespace = Configure::read('App.namespace');
         static::setAppNamespace();
     }
 
@@ -64,7 +56,6 @@ class TextHelperTest extends TestCase
     public function tearDown(): void
     {
         unset($this->Text, $this->View);
-        static::setAppNamespace($this->appNamespace);
         parent::tearDown();
     }