‪TYPO3CMS  10.4
CanonicalGeneratorTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
22 use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\Internal\TypoScriptInstruction;
23 use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequest;
24 use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequestContext;
25 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
26 
30 class ‪CanonicalGeneratorTest extends FunctionalTestCase
31 {
33 
34  private const ‪ENCRYPTION_KEY = '4408d27a916d51e624b69af3554f516dbab61037a9f7b9fd6f81b4d3bedeccb6';
35  private const ‪TYPO3_CONF_VARS = [
36  'SYS' => [
37  'encryptionKey' => ‪self::ENCRYPTION_KEY,
38  ],
39  'FE' => [
40  'cacheHash' => [
41  'requireCacheHashPresenceParameters' => []
42  ],
43  ]
44  ];
45  private const ‪LANGUAGE_PRESETS = [
46  'EN' => ['id' => 0, 'title' => 'English', 'locale' => 'en_US.UTF8', 'iso' => 'en', 'hrefLang' => 'en-US', 'direction' => ''],
47  ];
48 
52  protected ‪$coreExtensionsToLoad = [
53  'core', 'frontend', 'seo'
54  ];
55 
61  protected ‪$pathsToLinkInTestInstance = [
62  'typo3/sysext/core/Tests/Functional/Fixtures/Frontend/AdditionalConfiguration.php' => 'typo3conf/AdditionalConfiguration.php',
63  ];
64 
69 
70  protected function ‪setUp(): void
71  {
72  parent::setUp();
73 
74  // these settings are forwarded to the frontend sub-request as well
75  $this->internalRequestContext = (new InternalRequestContext())
76  ->withGlobalSettings(['TYPO3_CONF_VARS' => static::TYPO3_CONF_VARS]);
77 
79  'website-local',
80  $this->‪buildSiteConfiguration(1, 'http://localhost/'),
81  [
82  $this->‪buildDefaultLanguageConfiguration('EN', '/'),
83  ]
84  );
85 
87  'website-example',
88  $this->‪buildSiteConfiguration(100, 'http://example.com/'),
89  [
90  $this->‪buildDefaultLanguageConfiguration('EN', '/'),
91  ]
92  );
93 
95  'dummy',
96  $this->‪buildSiteConfiguration(200, '/'),
97  [
98  $this->‪buildDefaultLanguageConfiguration('EN', '/'),
99  ]
100  );
101 
102  $this->importDataSet('EXT:seo/Tests/Functional/Fixtures/pages-canonical.xml');
103  $this->setUpFrontendRootPage(1);
104  }
105 
106  protected function ‪tearDown(): void
107  {
108  unset($this->internalRequestContext);
109  parent::tearDown();
110  }
111 
112  public function ‪generateDataProvider(): array
113  {
114  return [
115  'uid: 1 with canonical_link' => [
116  'http://localhost/',
117  '<link rel="canonical" href="http://localhost/"/>' . chr(10),
118  ],
119  'uid: 2 with canonical_link' => [
120  'http://localhost/dummy-1-2',
121  '<link rel="canonical" href="http://localhost/dummy-1-2"/>' . chr(10),
122  ],
123  'uid: 3 with canonical_link AND content_from_pid = 2' => [
124  'http://localhost/dummy-1-3',
125  '<link rel="canonical" href="http://localhost/dummy-1-2"/>' . chr(10),
126  ],
127  'uid: 4 without canonical_link AND content_from_pid = 2' => [
128  'http://localhost/dummy-1-4',
129  '<link rel="canonical" href="http://localhost/dummy-1-2"/>' . chr(10),
130  ],
131  'uid: 5 without canonical_link AND without content_from_pid set' => [
132  'http://localhost/dummy-1-2-5',
133  '<link rel="canonical" href="http://localhost/dummy-1-2-5"/>' . chr(10),
134  ],
135  'uid: 8 without canonical_link AND content_from_pid = 9 (but target page is hidden) results in no canonical' => [
136  'http://localhost/dummy-1-2-8',
137  '',
138  ],
139  'uid: 10 no index' => [
140  'http://localhost/dummy-1-2-10',
141  ''
142  ],
143  'uid: 11 with mount_pid_ol = 0' => [
144  'http://localhost/dummy-1-2-11',
145  '<link rel="canonical" href="http://localhost/dummy-1-2-11"/>' . chr(10),
146  ],
147  'uid: 12 with mount_pid_ol = 1' => [
148  'http://localhost/dummy-1-2-12',
149  '<link rel="canonical" href="http://example.com/"/>' . chr(10),
150  ],
151  'subpage of page with mount_pid_ol = 0' => [
152  'http://localhost/dummy-1-2-11/subpage-of-new-root',
153  '<link rel="canonical" href="http://example.com/subpage-of-new-root"/>' . chr(10),
154  ],
155  'subpage of page with mount_pid_ol = 1' => [
156  'http://localhost/dummy-1-2-12/subpage-of-new-root',
157  '<link rel="canonical" href="http://example.com/subpage-of-new-root"/>' . chr(10),
158  ],
159  'mountpoint to page without siteroot' => [
160  'http://localhost/dummy-1-2-13',
161  '',
162  ],
163  'subpage of mountpoint without siteroot' => [
164  'http://localhost/dummy-1-2-13/imprint',
165  '',
166  ],
167  'uid: 14 typoscript setting config.disableCanonical' => [
168  'http://localhost/no-canonical',
169  ''
170  ]
171  ];
172  }
173 
181  public function ‪generate(string $targetUri, string $expectedCanonicalUrl): void
182  {
183  $response = $this->executeFrontendRequest(
184  (new InternalRequest($targetUri))
185  ->withInstructions([$this->‪buildPageTypoScript()]),
186  $this->internalRequestContext,
187  true
188  );
189 
190  self::assertStringContainsString($expectedCanonicalUrl, (string)$response->getBody());
191  }
192 
193  private function ‪buildPageTypoScript(): TypoScriptInstruction
194  {
195  return (new TypoScriptInstruction(TemplateService::class))
196  ->withTypoScript([
197  'page' => 'PAGE',
198  'page.' => [
199  'typeNum' => 0,
200  ],
201  ]);
202  }
203 }
‪TYPO3\CMS\Seo\Tests\Functional\Canonical\CanonicalGeneratorTest\$internalRequestContext
‪InternalRequestContext $internalRequestContext
Definition: CanonicalGeneratorTest.php:64
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait
Definition: SiteBasedTestTrait.php:36
‪TYPO3\CMS\Seo\Tests\Functional\Canonical
Definition: CanonicalGeneratorTest.php:18
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\writeSiteConfiguration
‪writeSiteConfiguration(string $identifier, array $site=[], array $languages=[], array $errorHandling=[])
Definition: SiteBasedTestTrait.php:58
‪TYPO3\CMS\Seo\Tests\Functional\Canonical\CanonicalGeneratorTest\buildPageTypoScript
‪buildPageTypoScript()
Definition: CanonicalGeneratorTest.php:189
‪TYPO3\CMS\Seo\Tests\Functional\Canonical\CanonicalGeneratorTest\TYPO3_CONF_VARS
‪const TYPO3_CONF_VARS
Definition: CanonicalGeneratorTest.php:34
‪TYPO3\CMS\Seo\Tests\Functional\Canonical\CanonicalGeneratorTest\ENCRYPTION_KEY
‪const ENCRYPTION_KEY
Definition: CanonicalGeneratorTest.php:33
‪TYPO3\CMS\Seo\Tests\Functional\Canonical\CanonicalGeneratorTest\$pathsToLinkInTestInstance
‪string[] $pathsToLinkInTestInstance
Definition: CanonicalGeneratorTest.php:58
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildDefaultLanguageConfiguration
‪array buildDefaultLanguageConfiguration(string $identifier, string $base)
Definition: SiteBasedTestTrait.php:124
‪TYPO3\CMS\Seo\Tests\Functional\Canonical\CanonicalGeneratorTest\generate
‪generate(string $targetUri, string $expectedCanonicalUrl)
Definition: CanonicalGeneratorTest.php:177
‪TYPO3\CMS\Seo\Tests\Functional\Canonical\CanonicalGeneratorTest
Definition: CanonicalGeneratorTest.php:31
‪TYPO3\CMS\Seo\Tests\Functional\Canonical\CanonicalGeneratorTest\$coreExtensionsToLoad
‪string[] $coreExtensionsToLoad
Definition: CanonicalGeneratorTest.php:50
‪TYPO3\CMS\Seo\Tests\Functional\Canonical\CanonicalGeneratorTest\LANGUAGE_PRESETS
‪const LANGUAGE_PRESETS
Definition: CanonicalGeneratorTest.php:44
‪TYPO3\CMS\Seo\Tests\Functional\Canonical\CanonicalGeneratorTest\setUp
‪setUp()
Definition: CanonicalGeneratorTest.php:66
‪TYPO3\CMS\Core\TypoScript\TemplateService
Definition: TemplateService.php:46
‪TYPO3\CMS\Seo\Tests\Functional\Canonical\CanonicalGeneratorTest\tearDown
‪tearDown()
Definition: CanonicalGeneratorTest.php:102
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildSiteConfiguration
‪array buildSiteConfiguration(int $rootPageId, string $base='')
Definition: SiteBasedTestTrait.php:109
‪TYPO3\CMS\Seo\Tests\Functional\Canonical\CanonicalGeneratorTest\generateDataProvider
‪generateDataProvider()
Definition: CanonicalGeneratorTest.php:108