[BUGFIX] Ensure "0" is treated as proper fallback in site language 79/60879/2
authorBenni Mack <benni@typo3.org>
Wed, 5 Jun 2019 07:32:51 +0000 (09:32 +0200)
committerAndreas Fernandez <a.fernandez@scripting-base.de>
Fri, 7 Jun 2019 09:00:26 +0000 (11:00 +0200)
Due to incomplete handling of "0" and casting, the fallback
is always "0" which is wrong.

The patch fixes the behavior and adds tests to all use-cases
mentioned in the ticket.

Resolves: #88490
Releases: master, 9.5
Change-Id: Ie685b96a94213104729261daa0e3be6e8ef29c35
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/60879
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Daniel Goerz <daniel.goerz@posteo.de>
Tested-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Reviewed-by: Daniel Goerz <daniel.goerz@posteo.de>
Reviewed-by: Tobi Kretschmann <tobi@tobishome.de>
Reviewed-by: Andreas Fernandez <a.fernandez@scripting-base.de>
typo3/sysext/core/Classes/Site/Entity/SiteLanguage.php
typo3/sysext/core/Tests/Unit/Site/Entity/SiteLanguageTest.php

index 033596a..0064027 100644 (file)
@@ -154,10 +154,16 @@ class SiteLanguage
         if (isset($configuration['fallbacks'])) {
             $fallbackLanguageIds = $configuration['fallbacks'];
 
+            // It is important to distinct between "0" and "" so, empty() should not be used here
             if (is_string($fallbackLanguageIds)) {
-                $fallbackLanguageIds = explode(',', $fallbackLanguageIds);
+                if ($fallbackLanguageIds !== '') {
+                    $fallbackLanguageIds = explode(',', $fallbackLanguageIds);
+                } else {
+                    $fallbackLanguageIds = [];
+                }
+            } elseif (is_scalar($fallbackLanguageIds)) {
+                $fallbackLanguageIds = [$fallbackLanguageIds];
             }
-
             $this->fallbackLanguageIds = array_map('intval', $fallbackLanguageIds);
         }
         if (isset($configuration['enabled'])) {
index 2935405..3f3d490 100644 (file)
@@ -22,6 +22,56 @@ use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
 
 class SiteLanguageTest extends UnitTestCase
 {
+    public function languageFallbackIdConversionDataProvider()
+    {
+        return [
+            'no fallback set' => [
+                null,
+                []
+            ],
+            'fallback given as empty string returns no fallback' => [
+                '',
+                []
+            ],
+            'fallback to default language as string returns proper fallback' => [
+                '0',
+                [0]
+            ],
+            'fallback to multiple languages as string returns proper fallback' => [
+                '3,0',
+                [3, 0]
+            ],
+            'fallback to default language as array returns proper fallback' => [
+                ['0'],
+                [0]
+            ],
+            'fallback to multiple languages as array returns proper fallback' => [
+                ['3', '0'],
+                [3, 0]
+            ],
+            'fallback to multiple languages as array with integers returns proper fallback' => [
+                [3, 0],
+                [3, 0]
+            ],
+
+        ];
+    }
+
+    /**
+     * @dataProvider languageFallbackIdConversionDataProvider
+     * @test
+     * @param string|array|null $input
+     * @param array $expected
+     */
+    public function languageFallbackIdConversion($input, array $expected)
+    {
+        $configuration = [
+            'fallbacks' => $input
+        ];
+        $subject = new SiteLanguage(1, 'fr', new Uri('/'), $configuration);
+        $this->assertSame($expected, $subject->getFallbackLanguageIds());
+    }
+
     /**
      * @test
      */