Skip to content

Commit

Permalink
[TASK] Add functional tests for hreflang tag generation
Browse files Browse the repository at this point in the history
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
Richard Haeser authored and tmotyl committed May 6, 2019
1 parent 0b03753 commit 17d983f
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 7 deletions.
24 changes: 21 additions & 3 deletions typo3/sysext/seo/Classes/HrefLang/HrefLangGenerator.php
Expand Up @@ -16,6 +16,7 @@
* The TYPO3 project - inspiring people to share!
*/

use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Http\Uri;
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
Expand Down Expand Up @@ -43,23 +44,32 @@ class HrefLangGenerator
*/
protected $typoScriptFrontendController;

/**
* @var ServerRequestInterface
*/
protected $request;

/**
* HreflangGenerator constructor
*
* @param ContentObjectRenderer|null $cObj
* @param TypoScriptFrontendController|null $typoScriptFrontendController
*/
public function __construct(ContentObjectRenderer $cObj = null, TypoScriptFrontendController $typoScriptFrontendController = null)
public function __construct(ContentObjectRenderer $cObj = null, TypoScriptFrontendController $typoScriptFrontendController = null, ServerRequestInterface $request = null)
{
if ($cObj === null) {
$cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
}
if ($typoScriptFrontendController === null) {
$typoScriptFrontendController = $this->getTypoScriptFrontendController();
}
if ($request === null) {
$request = $this->getRequest();
}

$this->cObj = $cObj;
$this->typoScriptFrontendController = $typoScriptFrontendController;
$this->request = $request;
}

public function generate(): string
Expand All @@ -69,7 +79,7 @@ public function generate(): string
return '';
}

if ($GLOBALS['TYPO3_REQUEST']->getAttribute('site') instanceof Site) {
if ($this->request->getAttribute('site') instanceof Site) {
$languageMenu = GeneralUtility::makeInstance(LanguageMenuProcessor::class);
$languages = $languageMenu->process($this->cObj, [], [], []);
foreach ($languages['languagemenu'] as $language) {
Expand Down Expand Up @@ -115,7 +125,7 @@ protected function getAbsoluteUrl(string $url): string
*/
protected function getSiteLanguage(): SiteLanguage
{
return $GLOBALS['TYPO3_REQUEST']->getAttribute('language');
return $this->request->getAttribute('language');
}

/**
Expand All @@ -125,4 +135,12 @@ protected function getTypoScriptFrontendController(): TypoScriptFrontendControll
{
return $GLOBALS['TSFE'];
}

/**
* @return ServerRequestInterface
*/
protected function getRequest(): ServerRequestInterface
{
return $GLOBALS['TYPO3_REQUEST'];
}
}
12 changes: 12 additions & 0 deletions typo3/sysext/seo/Tests/Functional/Fixtures/HrefLang.typoscript
@@ -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 typo3/sysext/seo/Tests/Functional/Fixtures/HrefLangScenario.yml
@@ -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 typo3/sysext/seo/Tests/Functional/HrefLang/HrefLangGeneratorTest.php
@@ -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)
);
}
}
Expand Up @@ -16,6 +16,7 @@
* The TYPO3 project - inspiring people to share!
*/

use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
use TYPO3\CMS\Seo\HrefLang\HrefLangGenerator;
Expand All @@ -41,10 +42,9 @@ public function checkIfGetSiteLanguageIsCalled($url, $shouldBeCalled)
['getSiteLanguage'],
[
$this->prophesize(ContentObjectRenderer::class)->reveal(),
$this->prophesize(TypoScriptFrontendController::class)->reveal()
],
'',
true
$this->prophesize(TypoScriptFrontendController::class)->reveal(),
$this->prophesize(ServerRequestInterface::class)->reveal()
]
);

$check = $shouldBeCalled ? $this->once() : $this->never();
Expand Down

0 comments on commit 17d983f

Please sign in to comment.