From 06475d2724b831e980cd2c84cdc9de7b2178d325 Mon Sep 17 00:00:00 2001 From: Helmut Hummel Date: Wed, 13 Oct 2021 21:25:47 +0200 Subject: [PATCH] [WIP][TASK] Add frontend functional tests --- .gitignore | 1 + .../Fixtures/TypoScript/setup.typoscript | 55 +++++++++ .../Functional/Fixtures/redirect-scenario.xml | 32 +++++ Tests/Functional/FrontendTest.php | 114 ++++++++++++++++++ composer.json | 5 +- 5 files changed, 203 insertions(+), 4 deletions(-) create mode 100644 Tests/Functional/Fixtures/TypoScript/setup.typoscript create mode 100644 Tests/Functional/Fixtures/redirect-scenario.xml create mode 100644 Tests/Functional/FrontendTest.php diff --git a/.gitignore b/.gitignore index 0e618dc..658e10a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /.Build .phpunit.result.cache /composer.lock +/var diff --git a/Tests/Functional/Fixtures/TypoScript/setup.typoscript b/Tests/Functional/Fixtures/TypoScript/setup.typoscript new file mode 100644 index 0000000..965fb00 --- /dev/null +++ b/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 + } +} diff --git a/Tests/Functional/Fixtures/redirect-scenario.xml b/Tests/Functional/Fixtures/redirect-scenario.xml new file mode 100644 index 0000000..b2ff6e4 --- /dev/null +++ b/Tests/Functional/Fixtures/redirect-scenario.xml @@ -0,0 +1,32 @@ + + + + 1 + 0 + Root 1 + 1 + 0 + / + + + 2 + 1 + GB + 0 + /gb + + + 3 + 1 + DE + 0 + /de + + + 4 + 1 + ZN + 0 + /zn + + diff --git a/Tests/Functional/FrontendTest.php b/Tests/Functional/FrontendTest.php new file mode 100644 index 0000000..d726e60 --- /dev/null +++ b/Tests/Functional/FrontendTest.php @@ -0,0 +1,114 @@ +, 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'); + } + } +} diff --git a/composer.json b/composer.json index 5861b30..ec5912d 100644 --- a/composer.json +++ b/composer.json @@ -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" }