[BUGFIX] Add support for language -1 to TCA field type slug 29/59029/5
authorAndré Schließer <a.schliesser@zeroseven.de>
Tue, 4 Dec 2018 16:50:24 +0000 (17:50 +0100)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Fri, 1 Feb 2019 19:42:24 +0000 (20:42 +0100)
Use default language as fallback for records with sys_language_uid = -1

Resolves: #87067
Releases: master, 9.5
Change-Id: If57d5566b83f4bba79451e8595337ca6c0eb3fc9
Reviewed-on: https://review.typo3.org/59029
Tested-by: TYPO3com <noreply@typo3.com>
Reviewed-by: Susanne Moog <susanne.moog@typo3.org>
Reviewed-by: Mona Muzaffar <mona.muzaffar@gmx.de>
Tested-by: Mona Muzaffar <mona.muzaffar@gmx.de>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
typo3/sysext/backend/Classes/Form/Element/InputSlugElement.php
typo3/sysext/backend/Tests/Unit/Form/Element/InputSlugElementTest.php [new file with mode: 0644]

index cd67afd..6151dce 100644 (file)
@@ -215,7 +215,7 @@ class InputSlugElement extends AbstractFormElement
      */
     protected function getPrefix(SiteInterface $site, int $requestLanguageId = 0): string
     {
-        $language = $site->getLanguageById($requestLanguageId);
+        $language = ($requestLanguageId < 0) ? $site->getDefaultLanguage() : $site->getLanguageById($requestLanguageId);
         $base = $language->getBase();
         $baseUrl = (string)$base;
         $baseUrl = rtrim($baseUrl, '/');
diff --git a/typo3/sysext/backend/Tests/Unit/Form/Element/InputSlugElementTest.php b/typo3/sysext/backend/Tests/Unit/Form/Element/InputSlugElementTest.php
new file mode 100644 (file)
index 0000000..08b7d59
--- /dev/null
@@ -0,0 +1,82 @@
+<?php
+declare(strict_types = 1);
+namespace TYPO3\CMS\Backend\Tests\Unit\Form\Element;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Backend\Form\Element\InputSlugElement;
+use TYPO3\CMS\Core\Site\Entity\Site;
+use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
+
+/**
+ * Tests for InputSlugElement Form
+ */
+class InputSlugElementTest extends UnitTestCase
+{
+    /**
+     * @test
+     */
+    public function getPrefixReturnsDefaultBaseUrlForAllDefinedLanguagesAndMinusOne(): void
+    {
+        $languages = [
+            [
+                'languageId' => 0,
+                'locale' => 'en_US.UTF-8',
+                'base' => '/en/'
+            ],
+            [
+                'languageId' => 1,
+                'locale' => 'de_DE.UTF-8',
+                'base' => '/de/'
+            ]
+        ];
+
+        $site = new Site('www.foo.de', 0, [
+            'languages' => $languages
+        ]);
+
+        $subject = $this->getAccessibleMock(
+            InputSlugElement::class,
+            ['dummy'],
+            [],
+            '',
+            false
+        );
+
+        static::assertSame('/en', $subject->_call('getPrefix', $site, -1));
+        static::assertSame('/en', $subject->_call('getPrefix', $site, 0));
+        static::assertSame('/de', $subject->_call('getPrefix', $site, 1));
+    }
+
+    /**
+     * @test
+     */
+    public function getPrefixThrowsInvalidArgumentExceptionForUndefinedLanguages(): void
+    {
+        $this->expectException(\InvalidArgumentException::class);
+        $this->expectExceptionCode(1522960188);
+
+        $site = new Site('www.foo.de', 0, []);
+
+        $subject = $this->getAccessibleMock(
+            InputSlugElement::class,
+            ['dummy'],
+            [],
+            '',
+            false
+        );
+
+        $subject->_call('getPrefix', $site, 99);
+    }
+}