[TASK] Make hreflang generator use PSR7 interface 74/58274/7
authorRichard Haeser <richard@maxserv.com>
Fri, 14 Sep 2018 09:28:09 +0000 (11:28 +0200)
committerBenni Mack <benni@typo3.org>
Sun, 16 Sep 2018 19:49:40 +0000 (21:49 +0200)
Use the Uri class to check if a host is given in the url
to determine if an absolute url should be generated.

Resolves: #86253
Releases: master
Change-Id: Ib84d391b0564f29352eda93a814f5d9b46696f37
Reviewed-on: https://review.typo3.org/58274
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Richard Haeser <richard@maxserv.com>
Tested-by: Richard Haeser <richard@maxserv.com>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
typo3/sysext/seo/Classes/HrefLang/HrefLangGenerator.php [new file with mode: 0644]
typo3/sysext/seo/Classes/HrefLang/HreflangGenerator.php [deleted file]
typo3/sysext/seo/Tests/Unit/HrefLang/HrefLangGeneratorTest.php [new file with mode: 0644]
typo3/sysext/seo/ext_localconf.php

diff --git a/typo3/sysext/seo/Classes/HrefLang/HrefLangGenerator.php b/typo3/sysext/seo/Classes/HrefLang/HrefLangGenerator.php
new file mode 100644 (file)
index 0000000..4eb9576
--- /dev/null
@@ -0,0 +1,97 @@
+<?php
+declare(strict_types = 1);
+
+namespace TYPO3\CMS\Seo\HrefLang;
+
+/*
+ * 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\Uri;
+use TYPO3\CMS\Core\Site\Entity\Site;
+use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
+use TYPO3\CMS\Frontend\DataProcessing\LanguageMenuProcessor;
+
+/**
+ * Class to add the hreflang tags to the page
+ *
+ * @internal
+ */
+class HrefLangGenerator
+{
+    /**
+     * The content object renderer
+     *
+     * @var ContentObjectRenderer
+     */
+    public $cObj;
+
+    /**
+     * HreflangGenerator constructor
+     *
+     * @param ContentObjectRenderer $cObj
+     */
+    public function __construct(ContentObjectRenderer $cObj = null)
+    {
+        if ($cObj === null) {
+            $cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
+        }
+        $this->cObj = $cObj;
+    }
+
+    public function generate(): string
+    {
+        $hreflangs = '';
+        if ($GLOBALS['TYPO3_REQUEST']->getAttribute('site') instanceof Site) {
+            $languageMenu = GeneralUtility::makeInstance(LanguageMenuProcessor::class);
+            $languages = $languageMenu->process($this->cObj, [], [], []);
+            $hreflangs = '';
+            foreach ($languages['languagemenu'] as $language) {
+                if ($language['available'] === 1) {
+                    $href = $this->getAbsoluteUrl($language['link']);
+                    $hreflangs .= '<link rel="alternate" hreflang="' . htmlspecialchars($language['hreflang']) . '" href="' . htmlspecialchars($href) . '"/>' . LF;
+                }
+            }
+
+            $href = $this->getAbsoluteUrl($languages['languagemenu'][0]['link']);
+            $hreflangs .= '<link rel="alternate" hreflang="x-default" href="' . htmlspecialchars($href) . '"/>' . LF;
+
+            $GLOBALS['TSFE']->additionalHeaderData[] = $hreflangs;
+        }
+
+        return $hreflangs;
+    }
+
+    /**
+     * @param string $url
+     * @return string
+     */
+    protected function getAbsoluteUrl(string $url): string
+    {
+        $uri = new Uri($url);
+        if (empty($uri->getHost())) {
+            $url = (string)$this->getSiteLanguage()->getBase()->withPath($uri->getPath());
+        }
+
+        return $url;
+    }
+
+    /**
+     * @return SiteLanguage
+     */
+    protected function getSiteLanguage(): SiteLanguage
+    {
+        return $GLOBALS['TYPO3_REQUEST']->getAttribute('language');
+    }
+}
diff --git a/typo3/sysext/seo/Classes/HrefLang/HreflangGenerator.php b/typo3/sysext/seo/Classes/HrefLang/HreflangGenerator.php
deleted file mode 100644 (file)
index 1339ec9..0000000
+++ /dev/null
@@ -1,90 +0,0 @@
-<?php
-declare(strict_types = 1);
-
-namespace TYPO3\CMS\Seo\HrefLang;
-
-/*
- * 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\Site\Entity\Site;
-use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
-use TYPO3\CMS\Core\Utility\GeneralUtility;
-use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
-use TYPO3\CMS\Frontend\DataProcessing\LanguageMenuProcessor;
-
-/**
- * Class to add the hreflang tags to the page
- *
- * @internal
- */
-class HreflangGenerator
-{
-    /**
-     * The content object renderer
-     *
-     * @var ContentObjectRenderer
-     */
-    public $cObj;
-
-    /**
-     * HreflangGenerator constructor
-     *
-     * @param ContentObjectRenderer $cObj
-     */
-    public function __construct(ContentObjectRenderer $cObj = null)
-    {
-        if ($cObj === null) {
-            $cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
-        }
-        $this->cObj = $cObj;
-    }
-
-    public function generate(): string
-    {
-        $hreflangs = '';
-        if ($GLOBALS['TYPO3_REQUEST']->getAttribute('site') instanceof Site) {
-            $languageMenu = GeneralUtility::makeInstance(LanguageMenuProcessor::class);
-            $languages = $languageMenu->process($this->cObj, [], [], []);
-            $hreflangs = '';
-            foreach ($languages['languagemenu'] as $language) {
-                if ($language['available'] === 1) {
-                    $href = $this->getAbsoluteUrl($language['link']);
-                    $hreflangs .= '<link rel="alternate" hreflang="' . $language['hreflang'] . '" href="' . $href . '"/>' . LF;
-                }
-            }
-
-            $href = $this->getAbsoluteUrl($languages['languagemenu'][0]['link']);
-            $hreflangs .= '<link rel="alternate" hreflang="x-default" href="' . $href . '"/>' . LF;
-
-            $GLOBALS['TSFE']->additionalHeaderData[] = $hreflangs;
-        }
-
-        return $hreflangs;
-    }
-
-    /**
-     * @param string $url
-     * @return string
-     */
-    protected function getAbsoluteUrl(string $url): string
-    {
-        /** @var SiteLanguage $siteLanguage */
-        $siteLanguage = $GLOBALS['TYPO3_REQUEST']->getAttribute('language');
-
-        if (strpos($url, 'http') !== 0) {
-            $url = (string)$siteLanguage->getBase()->withPath($url);
-        }
-
-        return $url;
-    }
-}
diff --git a/typo3/sysext/seo/Tests/Unit/HrefLang/HrefLangGeneratorTest.php b/typo3/sysext/seo/Tests/Unit/HrefLang/HrefLangGeneratorTest.php
new file mode 100644 (file)
index 0000000..a95f7e1
--- /dev/null
@@ -0,0 +1,94 @@
+<?php
+declare(strict_types = 1);
+
+namespace TYPO3\CMS\Seo\Tests\Unit\HrefLang;
+
+/*
+ * 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\Frontend\ContentObject\ContentObjectRenderer;
+use TYPO3\CMS\Seo\HrefLang\HrefLangGenerator;
+
+/**
+ * Test case
+ */
+class HrefLangGeneratorTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
+{
+    /**
+     * @test
+     *
+     * @param string $url
+     * @param bool $shouldBeCalled
+     *
+     * @dataProvider urlPathDataProvider
+     */
+    public function checkIfGetSiteLanguageIsCalled($url, $shouldBeCalled)
+    {
+        $subject = $this->getAccessibleMock(
+            HrefLangGenerator::class,
+            ['getSiteLanguage'],
+            [$this->prophesize(ContentObjectRenderer::class)->reveal()],
+            '',
+            true
+        );
+
+        $check = $shouldBeCalled ? $this->once() : $this->never();
+        $subject->expects($check)->method('getSiteLanguage');
+        $subject->_call('getAbsoluteUrl', $url);
+    }
+
+    /**
+     * @return array
+     */
+    public function urlPathDataProvider(): array
+    {
+        return [
+            [
+                '/',
+                true
+            ],
+            [
+                'example.com',
+                true    //This can't be defined as a domain because it can also be a filename
+            ],
+            [
+                'filename.pdf',
+                true
+            ],
+            [
+                'example.com/filename.pdf',
+                true
+            ],
+            [
+                '//example.com/filename.pdf',
+                false
+            ],
+            [
+                '//example.com',
+                false
+            ],
+            [
+                'https://example.com',
+                false
+            ],
+            [
+                '/page-1/subpage-1',
+                true
+            ],
+            [
+                'https://example.com/page-1/subpage-1',
+                false
+            ],
+        ];
+    }
+}
index 79f1b71..0310075 100644 (file)
@@ -4,7 +4,7 @@ defined('TYPO3_MODE') or die();
 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['TYPO3\CMS\Frontend\Page\PageGenerator']['generateMetaTags'][] =
     \TYPO3\CMS\Seo\MetaTag\MetaTagGenerator::class . '->generate';
 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['TYPO3\CMS\Frontend\Page\PageGenerator']['generateMetaTags'][] =
-    \TYPO3\CMS\Seo\HrefLang\HreflangGenerator::class . '->generate';
+    \TYPO3\CMS\Seo\HrefLang\HrefLangGenerator::class . '->generate';
 
 $metaTagManagerRegistry = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\MetaTag\MetaTagManagerRegistry::class);
 $metaTagManagerRegistry->registerManager(