‪TYPO3CMS  9.5
HrefLangGeneratorTest.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\DataHandling\Scenario\DataHandlerFactory;
22 use TYPO3\TestingFramework\Core\Functional\Framework\DataHandling\Scenario\DataHandlerWriter;
23 use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequest;
24 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
25 
26 class ‪HrefLangGeneratorTest extends FunctionalTestCase
27 {
29 
33  protected ‪$coreExtensionsToLoad = [
34  'core',
35  'frontend',
36  'seo'
37  ];
38 
42  protected const ‪LANGUAGE_PRESETS = [
43  'EN' => ['id' => 0, 'title' => 'English', 'locale' => 'en_US.UTF8', 'iso' => 'en', 'hrefLang' => 'en-US', 'direction' => ''],
44  'DE' => ['id' => 1, 'title' => 'German', 'locale' => 'de_DE.UTF8', 'iso' => 'de', 'hrefLang' => 'de-DE', 'direction' => ''],
45  ];
46 
47  public static function ‪setUpBeforeClass(): void
48  {
49  parent::setUpBeforeClass();
50  static::initializeDatabaseSnapshot();
51  }
52 
53  public static function ‪tearDownAfterClass(): void
54  {
55  static::destroyDatabaseSnapshot();
56  parent::tearDownAfterClass();
57  }
58 
59  protected function ‪setUp(): void
60  {
61  parent::setUp();
62 
64  'acme-com',
65  $this->‪buildSiteConfiguration(1000, 'https://acme.com/'),
66  [
67  $this->‪buildDefaultLanguageConfiguration('EN', '/'),
68  $this->‪buildLanguageConfiguration('DE', '/de'),
69  ]
70  );
71 
72  $this->‪setUpDatabaseWithYamlPayload(__DIR__ . '/../Fixtures/HrefLangScenario.yml');
73  }
74 
82  public function ‪checkHrefLangOutput($url, $expectedTags, $notExpectedTags): void
83  {
84  $this->setUpFrontendRootPage(
85  1000,
86  ['typo3/sysext/seo/Tests/Functional/Fixtures/HrefLang.typoscript']
87  );
88 
89  $response = $this->executeFrontendRequest(
90  new InternalRequest($url)
91  );
92  $stream = $response->getBody();
93  $stream->rewind();
94  $content = $stream->getContents();
95 
96  foreach ($expectedTags as $expectedTag) {
97  self::assertContains($expectedTag, $content);
98  }
99 
100  foreach ($notExpectedTags as $notExpectedTag) {
101  self::assertNotContains($notExpectedTag, $content);
102  }
103  }
104 
108  public function ‪checkHrefLangOutputDataProvider(): array
109  {
110  return [
111  'No translation available, so no hreflang tags expected' => [
112  'https://acme.com/',
113  [],
114  [
115  '<link rel="alternate" hreflang='
116  ]
117  ],
118  'English page, with German translation' => [
119  'https://acme.com/hello',
120  [
121  '<link rel="alternate" hreflang="en-US" href="https://acme.com/hello"/>',
122  '<link rel="alternate" hreflang="de-DE" href="https://acme.com/de/willkommen"/>',
123  '<link rel="alternate" hreflang="x-default" href="https://acme.com/hello"/>',
124  ],
125  []
126  ],
127  'German page, with English translation and English default' => [
128  'https://acme.com/de/willkommen',
129  [
130  '<link rel="alternate" hreflang="en-US" href="https://acme.com/hello"/>',
131  '<link rel="alternate" hreflang="de-DE" href="https://acme.com/de/willkommen"/>',
132  '<link rel="alternate" hreflang="x-default" href="https://acme.com/hello"/>',
133  ],
134  []
135  ],
136  ];
137  }
138 
143  protected function ‪setUpDatabaseWithYamlPayload(string $pathToYamlFile): void
144  {
145  $this->withDatabaseSnapshot(function () use ($pathToYamlFile) {
146  $backendUser = $this->setUpBackendUserFromFixture(1);
148 
149  $factory = DataHandlerFactory::fromYamlFile($pathToYamlFile);
150  $writer = DataHandlerWriter::withBackendUser($backendUser);
151  $writer->invokeFactory($factory);
152  static::failIfArrayIsNotEmpty(
153  $writer->getErrors()
154  );
155  });
156  }
157 
161  protected static function ‪failIfArrayIsNotEmpty(array $items): void
162  {
163  if (empty($items)) {
164  return;
165  }
166 
167  static::fail(
168  'Array was not empty as expected, but contained these items:' . LF
169  . '* ' . implode(LF . '* ', $items)
170  );
171  }
172 }
‪TYPO3\CMS\Seo\Tests\Functional\HrefLang\HrefLangGeneratorTest\LANGUAGE_PRESETS
‪const LANGUAGE_PRESETS
Definition: HrefLangGeneratorTest.php:40
‪TYPO3\CMS\Seo\Tests\Functional\HrefLang\HrefLangGeneratorTest
Definition: HrefLangGeneratorTest.php:27
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait
Definition: SiteBasedTestTrait.php:34
‪TYPO3\CMS\Seo\Tests\Functional\HrefLang
Definition: HrefLangGeneratorTest.php:4
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildLanguageConfiguration
‪array buildLanguageConfiguration(string $identifier, string $base, array $fallbackIdentifiers=[], string $fallbackType=null)
Definition: SiteBasedTestTrait.php:140
‪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\HrefLang\HrefLangGeneratorTest\failIfArrayIsNotEmpty
‪static failIfArrayIsNotEmpty(array $items)
Definition: HrefLangGeneratorTest.php:159
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildDefaultLanguageConfiguration
‪array buildDefaultLanguageConfiguration(string $identifier, string $base)
Definition: SiteBasedTestTrait.php:122
‪TYPO3\CMS\Seo\Tests\Functional\HrefLang\HrefLangGeneratorTest\setUpBeforeClass
‪static setUpBeforeClass()
Definition: HrefLangGeneratorTest.php:45
‪TYPO3\CMS\Seo\Tests\Functional\HrefLang\HrefLangGeneratorTest\checkHrefLangOutputDataProvider
‪array checkHrefLangOutputDataProvider()
Definition: HrefLangGeneratorTest.php:106
‪TYPO3\CMS\Seo\Tests\Functional\HrefLang\HrefLangGeneratorTest\setUpDatabaseWithYamlPayload
‪setUpDatabaseWithYamlPayload(string $pathToYamlFile)
Definition: HrefLangGeneratorTest.php:141
‪TYPO3\CMS\Seo\Tests\Functional\HrefLang\HrefLangGeneratorTest\tearDownAfterClass
‪static tearDownAfterClass()
Definition: HrefLangGeneratorTest.php:51
‪TYPO3\CMS\Seo\Tests\Functional\HrefLang\HrefLangGeneratorTest\checkHrefLangOutput
‪checkHrefLangOutput($url, $expectedTags, $notExpectedTags)
Definition: HrefLangGeneratorTest.php:80
‪TYPO3\CMS\Seo\Tests\Functional\HrefLang\HrefLangGeneratorTest\setUp
‪setUp()
Definition: HrefLangGeneratorTest.php:57
‪TYPO3\CMS\Core\Core\Bootstrap
Definition: Bootstrap.php:50
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildSiteConfiguration
‪array buildSiteConfiguration(int $rootPageId, string $base='')
Definition: SiteBasedTestTrait.php:107
‪TYPO3\CMS\Seo\Tests\Functional\HrefLang\HrefLangGeneratorTest\$coreExtensionsToLoad
‪string[] $coreExtensionsToLoad
Definition: HrefLangGeneratorTest.php:31
‪TYPO3\CMS\Core\Core\Bootstrap\initializeLanguageObject
‪static Bootstrap null initializeLanguageObject()
Definition: Bootstrap.php:986