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