Skip to content

Commit

Permalink
[WIP][TASK] Add frontend functional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
helhum committed Oct 13, 2021
1 parent 008e459 commit 06475d2
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
/.Build
.phpunit.result.cache
/composer.lock
/var
55 changes: 55 additions & 0 deletions Tests/Functional/Fixtures/TypoScript/setup.typoscript
@@ -0,0 +1,55 @@
page = PAGE
page.10 = TEXT
page.10.value = Hello World
page.config.disableAllHeaderCode = 1


# Enable language redirect
config.tx_locate = 1
config.tx_locate {
# Set this param if you do not want to execute the redirect
dryRun = 0

# Whether bots should be excluded from the behavior of the extension.
excludeBots = 0

# If this option is enabled, the verdict will be saved in a session and will not be evaluated again.
sessionHandling = 0

# If this option is enabled, it is possible to overwrite the verdict stored in the session.
overrideSessionValue = 0

# URL Parameter which has to be true when overrideCookie is allowed within action and cookieHandling is enabled
overrideQueryParameter = setLang

verdicts {
redirectToMainlandChina = Leuchtfeuer\Locate\Verdict\Redirect
redirectToMainlandChina {
page = 4
}
redirectToEN = Leuchtfeuer\Locate\Verdict\Redirect
redirectToEN {
page = 2
}

}

facts {
# browserAcceptLanguage = Leuchtfeuer\Locate\FactProvider\BrowserAcceptedLanguage
countryByIP = Leuchtfeuer\Locate\FactProvider\IP2Country
}

judges {
# Users from mainland China should be redirected to a specific URL.
100 = Leuchtfeuer\Locate\Judge\Condition
100 {
verdict = redirectToMainlandChina
fact = countryByIP
prosecution = cn
}

# All other users should be redirected to the English language version of the current page.
999999 = Leuchtfeuer\Locate\Judge\Fixed
999999.verdict = redirectToEN
}
}
32 changes: 32 additions & 0 deletions Tests/Functional/Fixtures/redirect-scenario.xml
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<dataset>
<pages>
<uid>1</uid>
<pid>0</pid>
<title>Root 1</title>
<is_siteroot>1</is_siteroot>
<deleted>0</deleted>
<slug>/</slug>
</pages>
<pages>
<uid>2</uid>
<pid>1</pid>
<title>GB</title>
<deleted>0</deleted>
<slug>/gb</slug>
</pages>
<pages>
<uid>3</uid>
<pid>1</pid>
<title>DE</title>
<deleted>0</deleted>
<slug>/de</slug>
</pages>
<pages>
<uid>4</uid>
<pid>1</pid>
<title>ZN</title>
<deleted>0</deleted>
<slug>/zn</slug>
</pages>
</dataset>
114 changes: 114 additions & 0 deletions Tests/Functional/FrontendTest.php
@@ -0,0 +1,114 @@
<?php

/*
* This file is part of the "Locate" extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* Florian Wessels <f.wessels@Leuchtfeuer.com>, Leuchtfeuer Digital Marketing
*/

namespace Leuchtfeuer\Locate\Tests\Functional;

use Symfony\Component\Yaml\Yaml;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequest;
use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequestContext;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;

class FrontendTest extends FunctionalTestCase
{
protected $testExtensionsToLoad = [
'typo3conf/ext/locate'
];

protected function setUp(): void
{
parent::setUp();
$this->importDataSet(__DIR__ . '/Fixtures/redirect-scenario.xml');
}

/**
* @test
*/
public function redirectToMainlandChinaFromIpAddress(): void
{
$this->setUpFrontendRootPage(
1,
['setup' => ['EXT:locate/Tests/Functional/Fixtures/TypoScript/setup.typoscript']]
);
$this->setUpFrontendSite(1);
$response = $this->executeFrontendRequest(
(new InternalRequest())
->withPageId(1)
->withHeader('Accept-Language', 'en-GB,en-US;q=0.9,en;q=0.8'),
(new InternalRequestContext())->withGlobalSettings([
'_SERVER' => [
'REMOTE_ADDR' => '1.207.255.250',
],
])
);
self::assertSame('Hello World', (string)$response->getBody());
}

/**
* Copied from \TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase::setUpFrontendSite
* in typo3/cms-core
*
* Create a simple site config for the tests that
* call a frontend page.
*
* @param int $pageId
* @param array $additionalLanguages
*/
protected function setUpFrontendSite(int $pageId, array $additionalLanguages = []): void
{
$languages = [
0 => [
'title' => 'English',
'enabled' => true,
'languageId' => 0,
'base' => '/',
'typo3Language' => 'default',
'locale' => 'en_US.UTF-8',
'iso-639-1' => 'en',
'navigationTitle' => '',
'hreflang' => '',
'direction' => '',
'flag' => 'us',
],
1 => [
'title' => 'ZN',
'enabled' => true,
'languageId' => 1,
'base' => '/zn',
'typo3Language' => 'default',
'locale' => 'zn_ZN.UTF-8',
'iso-639-1' => 'zn',
'navigationTitle' => '',
'hreflang' => '',
'direction' => '',
'flag' => 'zn',
],
];
$languages = array_merge($languages, $additionalLanguages);
$configuration = [
'rootPageId' => $pageId,
'base' => '/',
'languages' => $languages,
'errorHandling' => [],
'routes' => [],
];
GeneralUtility::mkdir_deep($this->instancePath . '/typo3conf/sites/testing/');
$yamlFileContents = Yaml::dump($configuration, 99, 2);
$fileName = $this->instancePath . '/typo3conf/sites/testing/config.yaml';
GeneralUtility::writeFile($fileName, $yamlFileContents);
// Ensure that no other site configuration was cached before
$cache = GeneralUtility::makeInstance(CacheManager::class)->getCache('core');
if ($cache->has('sites-configuration')) {
$cache->remove('sites-configuration');
}
}
}
5 changes: 1 addition & 4 deletions composer.json
Expand Up @@ -62,14 +62,11 @@
},
"scripts": {
"post-autoload-dump": [
"TYPO3\\TestingFramework\\Composer\\ExtensionTestEnvironment::prepare",
"mkdir -p .Build/web/typo3conf/ext/",
"[ -L .Build/web/typo3conf/ext/locate ] || ln -snvf ../../../../. .Build/web/typo3conf/ext/locate"
"TYPO3\\TestingFramework\\Composer\\ExtensionTestEnvironment::prepare"
]
},
"extra": {
"typo3/cms": {
"cms-package-dir": "{$vendor-dir}/typo3/cms",
"web-dir": ".Build/web",
"extension-key": "locate"
}
Expand Down

0 comments on commit 06475d2

Please sign in to comment.