From e419daf312ac67a9a2a5ad5f4ca6ab516a2a3edc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20E=C3=9Fl?= <indy.essl@gmail.com> Date: Fri, 20 Sep 2019 11:05:02 +0200 Subject: [PATCH] [BUGFIX] Respect fallback languages in slug generation for pages When creating a new subpage of another page, the TCA type "slug" will automatically generate a slug for the new page which uses the title of the parent page. When translating the page, the slug will try to get the correct language for the parent record (if possible). This check did previously not respect possible fallback languages for the language of the new record. (configured in the site configuration) Resolves: #89213 Releases: master, 9.5 Change-Id: Id0e43aba805a7d2596e2468223517241e0151676 Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/61766 Tested-by: TYPO3com <noreply@typo3.com> Tested-by: Markus Klein <markus.klein@typo3.org> Tested-by: Benni Mack <benni@typo3.org> Reviewed-by: Markus Klein <markus.klein@typo3.org> Reviewed-by: Benni Mack <benni@typo3.org> --- .../core/Classes/DataHandling/SlugHelper.php | 22 ++- .../DataHandling/Slug/DataSet/Pages.csv | 9 + .../DataHandling/Slug/SlugHelperTest.php | 168 ++++++++++++++++++ 3 files changed, 195 insertions(+), 4 deletions(-) create mode 100644 typo3/sysext/core/Tests/Functional/DataHandling/Slug/DataSet/Pages.csv create mode 100644 typo3/sysext/core/Tests/Functional/DataHandling/Slug/SlugHelperTest.php diff --git a/typo3/sysext/core/Classes/DataHandling/SlugHelper.php b/typo3/sysext/core/Classes/DataHandling/SlugHelper.php index b029c1519d37..10c9e36a3fba 100644 --- a/typo3/sysext/core/Classes/DataHandling/SlugHelper.php +++ b/typo3/sysext/core/Classes/DataHandling/SlugHelper.php @@ -153,7 +153,7 @@ class SlugHelper * Used when no slug exists for a record * * @param array $recordData - * @param int $pid + * @param int $pid The uid of the page to generate the slug for * @return string */ public function generate(array $recordData, int $pid): string @@ -597,9 +597,23 @@ class SlugHelper // do not use spacers (199), recyclers and folders and everything else } while (!empty($rootLine) && (int)$parentPageRecord['doktype'] >= 199); if ($languageId > 0) { - $localizedParentPageRecord = BackendUtility::getRecordLocalization('pages', $parentPageRecord['uid'], $languageId); - if (!empty($localizedParentPageRecord)) { - $parentPageRecord = reset($localizedParentPageRecord); + $languageIds = [$languageId]; + $siteFinder = GeneralUtility::makeInstance(SiteFinder::class); + + try { + $site = $siteFinder->getSiteByPageId($pid); + $siteLanguage = $site->getLanguageById($languageId); + $languageIds = array_merge($languageIds, $siteLanguage->getFallbackLanguageIds()); + } catch (SiteNotFoundException | \InvalidArgumentException $e) { + // no site or requested language available - move on + } + + foreach ($languageIds as $languageId) { + $localizedParentPageRecord = BackendUtility::getRecordLocalization('pages', $parentPageRecord['uid'], $languageId); + if (!empty($localizedParentPageRecord)) { + $parentPageRecord = reset($localizedParentPageRecord); + break; + } } } return $parentPageRecord; diff --git a/typo3/sysext/core/Tests/Functional/DataHandling/Slug/DataSet/Pages.csv b/typo3/sysext/core/Tests/Functional/DataHandling/Slug/DataSet/Pages.csv new file mode 100644 index 000000000000..65338b85586f --- /dev/null +++ b/typo3/sysext/core/Tests/Functional/DataHandling/Slug/DataSet/Pages.csv @@ -0,0 +1,9 @@ +"pages",,,,,,,,,,,,,,, +,"uid","pid","sorting","deleted","sys_language_uid","l10n_parent","l10n_source","t3_origuid","t3ver_wsid","t3ver_state","t3ver_stage","t3ver_oid","t3ver_move_id","title","slug" +,1,0,0,0,0,0,0,0,0,0,0,0,0,"Root","/" +,13,1,0,0,0,0,0,0,0,0,0,0,0,"Default parent","/default-parent" +,14,1,0,0,2,13,13,0,0,0,0,0,0,"German parent","/german-parent" +,15,13,0,0,0,0,0,0,0,0,0,0,0,"Default Page","/default-parent/default-page" +,16,13,0,0,1,15,15,0,0,0,0,0,0,"Dansk Page","/default-parent/dansk-page" +,17,13,0,0,2,15,15,0,0,0,0,0,0,"German Page","/german-parent/german-page" +,18,13,0,0,3,15,15,0,0,0,0,0,0,"Swiss Page","/german-parent/swiss-page" diff --git a/typo3/sysext/core/Tests/Functional/DataHandling/Slug/SlugHelperTest.php b/typo3/sysext/core/Tests/Functional/DataHandling/Slug/SlugHelperTest.php new file mode 100644 index 000000000000..a81ff78e00e5 --- /dev/null +++ b/typo3/sysext/core/Tests/Functional/DataHandling/Slug/SlugHelperTest.php @@ -0,0 +1,168 @@ +<?php +declare(strict_types = 1); +namespace TYPO3\CMS\Core\Tests\Functional\DataHandling\Slug; + +/* + * 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\Core\DataHandling\SlugHelper; +use TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase; +use TYPO3\CMS\Core\Utility\GeneralUtility; + +/** + * Functional test for the SlugHelper + */ +class SlugHelperTest extends AbstractDataHandlerActionTestCase +{ + /** + * @var string + */ + protected $scenarioDataSetDirectory = 'typo3/sysext/core/Tests/Functional/DataHandling/Slug/DataSet/'; + + /** + * Default Site Configuration + * @var array + */ + protected $siteLanguageConfiguration = [ + 1 => [ + 'title' => 'Dansk', + 'enabled' => true, + 'languageId' => 1, + 'base' => '/dk/', + 'typo3Language' => 'dk', + 'locale' => 'da_DK.UTF-8', + 'iso-639-1' => 'da', + 'flag' => 'dk', + 'fallbackType' => 'fallback', + 'fallbacks' => '0' + ], + 2 => [ + 'title' => 'Deutsch', + 'enabled' => true, + 'languageId' => 2, + 'base' => '/de/', + 'typo3Language' => 'de', + 'locale' => 'de_DE.UTF-8', + 'iso-639-1' => 'de', + 'flag' => 'de', + 'fallbackType' => 'fallback', + 'fallbacks' => '0' + ], + 3 => [ + 'title' => 'Schweizer Deutsch', + 'enabled' => true, + 'languageId' => 3, + 'base' => '/de-ch/', + 'typo3Language' => 'ch', + 'locale' => 'de_CH.UTF-8', + 'iso-639-1' => 'ch', + 'flag' => 'ch', + 'fallbackType' => 'fallback', + 'fallbacks' => '2,0' + ], + ]; + + protected function setUp(): void + { + parent::setUp(); + + $this->importScenarioDataSet('Pages'); + $this->setUpFrontendSite(1, $this->siteLanguageConfiguration); + $this->setUpFrontendRootPage(1, ['typo3/sysext/core/Tests/Functional/Fixtures/Frontend/JsonRenderer.typoscript']); + } + + /** + * DataProvider for testing the language resolving of the parent page. + * - If the language can be resolved, get the slug of the current language + * - If not, consecutively try the fallback languages from the site config + * - As a last resort, fall back to the default language. + * + * Example languages: + * 0 = "Default" + * 1 = "Dansk" - (Fallback to Default) + * 2 = "German" - (Fallback to Default) + * 3 = "Swiss German" - (Fallback to German) + * + * @return array + */ + public function generateRespectsFallbackLanguageOfParentPageSlugDataProvider(): array + { + return [ + 'default page / default parent' => [ + '/default-parent/default-page', + [ + 'uid' => '13', + 'title' => 'Default Page', + 'sys_language_uid' => 0 + ] + ], + 'Dansk page / default parent' => [ + '/default-parent/dansk-page', + [ + 'uid' => '13', + 'title' => 'Dansk Page', + 'sys_language_uid' => 1 + ], + ], + 'german page / german parent' => [ + '/german-parent/german-page', + [ + 'uid' => '13', + 'title' => 'German Page', + 'sys_language_uid' => 2 + ] + ], + 'swiss page / german fallback parent' => [ + '/german-parent/swiss-page', + [ + 'uid' => '13', + 'title' => 'Swiss Page', + 'sys_language_uid' => 3 + ], + ], + ]; + } + + /** + * @dataProvider generateRespectsFallbackLanguageOfParentPageSlugDataProvider + * @param string $expected + * @param array $page + * @test + */ + public function generateRespectsFallbackLanguageOfParentPageSlug(string $expected, array $page) + { + $slugHelper = GeneralUtility::makeInstance( + SlugHelper::class, + 'pages', + 'slug', + [ + 'generatorOptions' => [ + 'fields' => ['title'], + 'prefixParentPageSlug' => true, + ], + ] + ); + + self::assertEquals( + $expected, + $slugHelper->generate( + [ + 'title' => $page['title'], + 'uid' => $page['uid'], + 'sys_language_uid' => $page['sys_language_uid'] + ], + (int)$page['uid'] + ) + ); + } +} -- GitLab