Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TASK] Add functional tests for hreflang tag generation
To make sure rendering of hreflang tags will not change unexpectedly, some functional tests are added. Resolves: #88251 Related: #88140 Releases: master, 9.5 Change-Id: Idd8bc846e4bd57eb5335fe4795dcb233849eb4e8 Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/60635 Tested-by: Benni Mack <benni@typo3.org> Tested-by: TYPO3com <noreply@typo3.com> Tested-by: Jonas Eberle <flightvision@googlemail.com> Tested-by: Tymoteusz Motylewski <t.motylewski@gmail.com> Reviewed-by: Benni Mack <benni@typo3.org> Reviewed-by: Tymoteusz Motylewski <t.motylewski@gmail.com>
- Loading branch information
Showing
5 changed files
with
253 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
typo3/sysext/seo/Tests/Functional/Fixtures/HrefLang.typoscript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
config { | ||
no_cache = 1 | ||
debug = 0 | ||
admPanel = 0 | ||
sendCacheHeaders = 0 | ||
} | ||
|
||
page = PAGE | ||
page { | ||
10 = TEXT | ||
10.value = Test | ||
} |
44 changes: 44 additions & 0 deletions
44
typo3/sysext/seo/Tests/Functional/Fixtures/HrefLangScenario.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
__variables: | ||
- &pageStandard 0 | ||
- &contentText 'text' | ||
- &idAcmeRootPage 1000 | ||
|
||
entitySettings: | ||
'*': | ||
nodeColumnName: 'pid' | ||
columnNames: {id: 'uid', language: 'sys_language_uid'} | ||
defaultValues: {pid: 0} | ||
page: | ||
isNode: true | ||
tableName: 'pages' | ||
parentColumnName: 'pid' | ||
languageColumnNames: ['l10n_parent', 'l10n_source'] | ||
columnNames: {type: 'doktype', root: 'is_siteroot', mount: 'mount_pid', visitorGroups: 'fe_group'} | ||
defaultValues: {hidden: 0, doktype: *pageStandard} | ||
valueInstructions: | ||
shortcut: | ||
first: {shortcut: 0, shortcut_mode: 1} | ||
content: | ||
tableName: 'tt_content' | ||
languageColumnNames: ['l18n_parent', 'l10n_source'] | ||
columnNames: {title: 'header', type: 'CType', column: 'colPos'} | ||
defaultValues: {hidden: 0, type: *contentText, column: 0} | ||
language: | ||
tableName: 'sys_language' | ||
columnNames: {code: 'language_isocode'} | ||
typoscript: | ||
tableName: 'sys_template' | ||
valueInstructions: | ||
type: | ||
site: {root: 1, clear: 1} | ||
|
||
entities: | ||
language: | ||
- self: {id: 1, title: 'German', code: 'de'} | ||
- self: {id: 2, title: 'Swiss German', code: 'de'} | ||
page: | ||
- self: {id: *idAcmeRootPage, title: 'EN: Root', root: true, slug: '/'} | ||
children: | ||
- self: {id: 1100, title: 'EN: Welcome', slug: '/hello'} | ||
languageVariants: | ||
- self: {id: 1101, title: 'DE: Willkommen', language: 1, slug: '/willkommen'} |
172 changes: 172 additions & 0 deletions
172
typo3/sysext/seo/Tests/Functional/HrefLang/HrefLangGeneratorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace TYPO3\CMS\Seo\Tests\Functional\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\Core\Bootstrap; | ||
use TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait; | ||
use TYPO3\TestingFramework\Core\Functional\Framework\DataHandling\Scenario\DataHandlerFactory; | ||
use TYPO3\TestingFramework\Core\Functional\Framework\DataHandling\Scenario\DataHandlerWriter; | ||
use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequest; | ||
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase; | ||
|
||
class HrefLangGeneratorTest extends FunctionalTestCase | ||
{ | ||
use SiteBasedTestTrait; | ||
|
||
/** | ||
* @var string[] | ||
*/ | ||
protected $coreExtensionsToLoad = [ | ||
'core', | ||
'frontend', | ||
'seo' | ||
]; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected const LANGUAGE_PRESETS = [ | ||
'EN' => ['id' => 0, 'title' => 'English', 'locale' => 'en_US.UTF8', 'iso' => 'en', 'hrefLang' => 'en-US', 'direction' => ''], | ||
'DE' => ['id' => 1, 'title' => 'German', 'locale' => 'de_DE.UTF8', 'iso' => 'de', 'hrefLang' => 'de-DE', 'direction' => ''], | ||
]; | ||
|
||
public static function setUpBeforeClass(): void | ||
{ | ||
parent::setUpBeforeClass(); | ||
static::initializeDatabaseSnapshot(); | ||
} | ||
|
||
public static function tearDownAfterClass(): void | ||
{ | ||
static::destroyDatabaseSnapshot(); | ||
parent::tearDownAfterClass(); | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->writeSiteConfiguration( | ||
'acme-com', | ||
$this->buildSiteConfiguration(1000, 'https://acme.com/'), | ||
[ | ||
$this->buildDefaultLanguageConfiguration('EN', '/'), | ||
$this->buildLanguageConfiguration('DE', '/de'), | ||
] | ||
); | ||
|
||
$this->setUpDatabaseWithYamlPayload(__DIR__ . '/../Fixtures/HrefLangScenario.yml'); | ||
} | ||
|
||
/** | ||
* @param string $url | ||
* @param array $expected | ||
* | ||
* @test | ||
* @dataProvider checkHrefLangOutputDataProvider | ||
*/ | ||
public function checkHrefLangOutput($url, $expectedTags, $notExpectedTags): void | ||
{ | ||
$this->setUpFrontendRootPage( | ||
1000, | ||
['typo3/sysext/seo/Tests/Functional/Fixtures/HrefLang.typoscript'] | ||
); | ||
|
||
$response = $this->executeFrontendRequest( | ||
new InternalRequest($url) | ||
); | ||
$stream = $response->getBody(); | ||
$stream->rewind(); | ||
$content = $stream->getContents(); | ||
|
||
foreach ($expectedTags as $expectedTag) { | ||
self::assertContains($expectedTag, $content); | ||
} | ||
|
||
foreach ($notExpectedTags as $notExpectedTag) { | ||
self::assertNotContains($notExpectedTag, $content); | ||
} | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function checkHrefLangOutputDataProvider(): array | ||
{ | ||
return [ | ||
'No translation available, so no hreflang tags expected' => [ | ||
'https://acme.com/', | ||
[], | ||
[ | ||
'<link rel="alternate" hreflang=' | ||
] | ||
], | ||
'English page, with German translation' => [ | ||
'https://acme.com/hello', | ||
[ | ||
'<link rel="alternate" hreflang="en-US" href="https://acme.com/hello"/>', | ||
'<link rel="alternate" hreflang="de-DE" href="https://acme.com/de/willkommen"/>', | ||
'<link rel="alternate" hreflang="x-default" href="https://acme.com/hello"/>', | ||
], | ||
[] | ||
], | ||
'German page, with English translation and English default' => [ | ||
'https://acme.com/de/willkommen', | ||
[ | ||
'<link rel="alternate" hreflang="en-US" href="https://acme.com/hello"/>', | ||
'<link rel="alternate" hreflang="de-DE" href="https://acme.com/de/willkommen"/>', | ||
'<link rel="alternate" hreflang="x-default" href="https://acme.com/hello"/>', | ||
], | ||
[] | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @param string $pathToYamlFile | ||
* @throws \Doctrine\DBAL\DBALException | ||
*/ | ||
protected function setUpDatabaseWithYamlPayload(string $pathToYamlFile): void | ||
{ | ||
$this->withDatabaseSnapshot(function () use ($pathToYamlFile) { | ||
$backendUser = $this->setUpBackendUserFromFixture(1); | ||
Bootstrap::initializeLanguageObject(); | ||
|
||
$factory = DataHandlerFactory::fromYamlFile($pathToYamlFile); | ||
$writer = DataHandlerWriter::withBackendUser($backendUser); | ||
$writer->invokeFactory($factory); | ||
static::failIfArrayIsNotEmpty( | ||
$writer->getErrors() | ||
); | ||
}); | ||
} | ||
|
||
/** | ||
* @param array $items | ||
*/ | ||
protected static function failIfArrayIsNotEmpty(array $items): void | ||
{ | ||
if (empty($items)) { | ||
return; | ||
} | ||
|
||
static::fail( | ||
'Array was not empty as expected, but contained these items:' . LF | ||
. '* ' . implode(LF . '* ', $items) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters