[BUGFIX] Respect no_index while creating canonical and hreflang tags 88/58588/3
authorRichard Haeser <richard@maxserv.com>
Sat, 6 Oct 2018 19:05:13 +0000 (21:05 +0200)
committerBenni Mack <benni@typo3.org>
Sat, 27 Oct 2018 14:09:33 +0000 (16:09 +0200)
When the current page is set to no_index, no canonical tag
and hreflang tags will be generated.

Resolves: #86569
Releases: master
Change-Id: I7e5360bc9159e31a4ffaa352a05ba0d8296693ed
Reviewed-on: https://review.typo3.org/58588
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Jörg Bösche <typo3@joergboesche.de>
Reviewed-by: Marc Hirdes <hirdes@clickstorm.de>
Reviewed-by: Willi Wehmeier <wwwehmeier@gmail.com>
Reviewed-by: Guido Schmechel <guido.schmechel@brandung.de>
Tested-by: Guido Schmechel <guido.schmechel@brandung.de>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
typo3/sysext/seo/Classes/Canonical/CanonicalGenerator.php
typo3/sysext/seo/Classes/HrefLang/HrefLangGenerator.php
typo3/sysext/seo/Tests/Functional/Canonical/CanonicalGeneratorTest.php
typo3/sysext/seo/Tests/Functional/Fixtures/pages-canonical.xml
typo3/sysext/seo/Tests/Unit/HrefLang/HrefLangGeneratorTest.php

index 22a276c..43b6c4e 100644 (file)
@@ -72,6 +72,10 @@ class CanonicalGenerator
         $href = '';
         $this->signalSlotDispatcher->dispatch(self::class, 'beforeGeneratingCanonical', [&$href]);
 
+        if (empty($href) && (int)$this->typoScriptFrontendController->page['no_index'] === 1) {
+            return '';
+        }
+
         if (empty($href)) {
             // 1) Check if page show content from other page
             $href = $this->checkContentFromPid();
index 4eb9576..a72c720 100644 (file)
@@ -21,6 +21,7 @@ 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\Controller\TypoScriptFrontendController;
 use TYPO3\CMS\Frontend\DataProcessing\LanguageMenuProcessor;
 
 /**
@@ -38,21 +39,36 @@ class HrefLangGenerator
     public $cObj;
 
     /**
+     * @var TypoScriptFrontendController
+     */
+    protected $typoScriptFrontendController;
+
+    /**
      * HreflangGenerator constructor
      *
-     * @param ContentObjectRenderer $cObj
+     * @param ContentObjectRenderer|null $cObj
+     * @param TypoScriptFrontendController|null $typoScriptFrontendController
      */
-    public function __construct(ContentObjectRenderer $cObj = null)
+    public function __construct(ContentObjectRenderer $cObj = null, TypoScriptFrontendController $typoScriptFrontendController = null)
     {
         if ($cObj === null) {
             $cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
         }
+        if ($typoScriptFrontendController === null) {
+            $typoScriptFrontendController = $this->getTypoScriptFrontendController();
+        }
+
         $this->cObj = $cObj;
+        $this->typoScriptFrontendController = $typoScriptFrontendController;
     }
 
     public function generate(): string
     {
         $hreflangs = '';
+        if ((int)$this->typoScriptFrontendController->page['no_index'] === 1) {
+            return '';
+        }
+
         if ($GLOBALS['TYPO3_REQUEST']->getAttribute('site') instanceof Site) {
             $languageMenu = GeneralUtility::makeInstance(LanguageMenuProcessor::class);
             $languages = $languageMenu->process($this->cObj, [], [], []);
@@ -94,4 +110,12 @@ class HrefLangGenerator
     {
         return $GLOBALS['TYPO3_REQUEST']->getAttribute('language');
     }
+
+    /**
+     * @return TypoScriptFrontendController
+     */
+    protected function getTypoScriptFrontendController(): TypoScriptFrontendController
+    {
+        return $GLOBALS['TSFE'];
+    }
 }
index b8be4e3..490f435 100644 (file)
@@ -69,6 +69,7 @@ class CanonicalGeneratorTest extends AbstractTestCase
             'uid: 5 without canonical_link AND without content_from_pid set' => [5, '<link rel="canonical" href="http://localhost/dummy-1-2-5"/>' . chr(10)],
             'uid: 6 without canonical_link AND content_from_pid = 7 (but target page is deleted)' => [6, '<link rel="canonical" href="http://localhost/dummy-1-2-6"/>' . chr(10)],
             'uid: 8 without canonical_link AND content_from_pid = 9 (but target page is hidden)' => [8, '<link rel="canonical" href="http://localhost/dummy-1-2-8"/>' . chr(10)],
+            'uid: 10 no index' => [10, ''],
         ];
     }
 
index 7499c01..5359b88 100644 (file)
         <deleted>0</deleted>
         <hidden>1</hidden>
     </pages>
+    <pages>
+        <uid>10</uid>
+        <pid>2</pid>
+        <title>Dummy</title>
+        <canonical_link></canonical_link>
+        <slug>/dummy-1-2-10</slug>
+        <content_from_pid>0</content_from_pid>
+        <deleted>0</deleted>
+        <hidden>0</hidden>
+        <no_index>1</no_index>
+    </pages>
 </dataset>
index 2fa2d11..6a5dee2 100644 (file)
@@ -17,6 +17,7 @@ namespace TYPO3\CMS\Seo\Tests\Unit\HrefLang;
  */
 
 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
+use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
 use TYPO3\CMS\Seo\HrefLang\HrefLangGenerator;
 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
 
@@ -38,7 +39,10 @@ class HrefLangGeneratorTest extends UnitTestCase
         $subject = $this->getAccessibleMock(
             HrefLangGenerator::class,
             ['getSiteLanguage'],
-            [$this->prophesize(ContentObjectRenderer::class)->reveal()],
+            [
+                $this->prophesize(ContentObjectRenderer::class)->reveal(),
+                $this->prophesize(TypoScriptFrontendController::class)->reveal()
+            ],
             '',
             true
         );