[BUGFIX] Allow to resolve sites without trailing slash as base 92/58092/2
authorBenni Mack <benni@typo3.org>
Thu, 30 Aug 2018 16:38:44 +0000 (18:38 +0200)
committerBenni Mack <benni@typo3.org>
Thu, 30 Aug 2018 17:02:43 +0000 (19:02 +0200)
When a site (language) has "/en" as base, then the page router
does not kick in, as the PageRouter receives a "/" at the very beginning
of the $routePathTail, which needs to be removed.

Resolves: #86044
Releases: master
Change-Id: I770911bc1157cfb8a217b5e16dc303859f63cedd
Reviewed-on: https://review.typo3.org/58092
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Susanne Moog <susanne.moog@typo3.org>
Tested-by: Susanne Moog <susanne.moog@typo3.org>
Reviewed-by: Daniel Sattler <sattler@b13.de>
Tested-by: Daniel Sattler <sattler@b13.de>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
typo3/sysext/core/Classes/Routing/PageRouter.php
typo3/sysext/core/Tests/Unit/Routing/PageRouterTest.php [new file with mode: 0644]

index 63a8294..b43e99a 100644 (file)
@@ -91,7 +91,8 @@ class PageRouter
         $context = new RequestContext('/', $request->getMethod(), $request->getUri()->getHost());
         $matcher = new UrlMatcher($collection, $context);
         try {
-            $result = $matcher->match('/' . $routePathTail);
+            $result = $matcher->match('/' . ltrim($routePathTail, '/'));
+            unset($result['_route']);
             return new RouteResult($request->getUri(), $site, $language, $result['tail'], $result);
         } catch (ResourceNotFoundException $e) {
             // do nothing
diff --git a/typo3/sysext/core/Tests/Unit/Routing/PageRouterTest.php b/typo3/sysext/core/Tests/Unit/Routing/PageRouterTest.php
new file mode 100644 (file)
index 0000000..9b8bc22
--- /dev/null
@@ -0,0 +1,83 @@
+<?php
+declare(strict_types = 1);
+
+namespace TYPO3\CMS\Core\Tests\Unit\Routing;
+
+/*
+ * 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\Http\ServerRequest;
+use TYPO3\CMS\Core\Routing\PageRouter;
+use TYPO3\CMS\Core\Routing\RouteResult;
+use TYPO3\CMS\Core\Site\Entity\Site;
+use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
+
+class PageRouterTest extends UnitTestCase
+{
+    /**
+     * @test
+     */
+    public function properSiteConfigurationFindsRoute()
+    {
+        $incomingUrl = 'https://king.com/lotus-flower/en/mr-magpie/bloom';
+        $pageRecord = ['uid' => 13, 'l10n_parent' => 0, 'slug' => '/mr-magpie/bloom/'];
+        $site = new Site('lotus-flower', 13, [
+            'base' => '/lotus-flower/',
+            'languages' => [
+                0 => [
+                    'languageId' => 0,
+                    'locale' => 'en_US.UTF-8',
+                    'base' => '/en/'
+                ],
+            ]
+        ]);
+        $language = $site->getDefaultLanguage();
+
+        $request = new ServerRequest($incomingUrl, 'GET');
+        $subject = $this->getAccessibleMock(PageRouter::class, ['getPagesFromDatabaseForCandidates']);
+        $subject->expects($this->once())->method('getPagesFromDatabaseForCandidates')->willReturn([$pageRecord]);
+        $routeResult = $subject->matchRoute($request, '/mr-magpie/bloom', $site, $language);
+
+        $expectedRouteResult = new RouteResult($request->getUri(), $site, $language, '', ['page' => $pageRecord, 'tail' => '']);
+        $this->assertEquals($expectedRouteResult, $routeResult);
+    }
+
+    /**
+     * Let's see if the slug is "/blabla" and the base does not have a trailing slash ("/en")
+     * @test
+     */
+    public function properSiteConfigurationWithoutTrailingSlashFindsRoute()
+    {
+        $incomingUrl = 'https://king.com/lotus-flower/en/mr-magpie/bloom/unknown-code/';
+        $pageRecord = ['uid' => 13, 'l10n_parent' => 0, 'slug' => '/mr-magpie/bloom/'];
+        $site = new Site('lotus-flower', 13, [
+            'base' => '/lotus-flower/',
+            'languages' => [
+                0 => [
+                    'languageId' => 0,
+                    'locale' => 'en_US.UTF-8',
+                    'base' => '/en'
+                ],
+            ]
+        ]);
+        $language = $site->getDefaultLanguage();
+
+        $request = new ServerRequest($incomingUrl, 'GET');
+        $subject = $this->getAccessibleMock(PageRouter::class, ['getPagesFromDatabaseForCandidates']);
+        $subject->expects($this->once())->method('getPagesFromDatabaseForCandidates')->willReturn([$pageRecord]);
+        $routeResult = $subject->matchRoute($request, '/mr-magpie/bloom/unknown-code/', $site, $language);
+
+        $expectedRouteResult = new RouteResult($request->getUri(), $site, $language, 'unknown-code/', ['page' => $pageRecord, 'tail' => 'unknown-code/']);
+        $this->assertEquals($expectedRouteResult, $routeResult);
+    }
+}