Commit 6e511a9e authored by Fabrice Gangler's avatar Fabrice Gangler 🎨 Committed by Matthieu FAURE
Browse files

TEST: improve URLs webapp tests cases

Refs: #910
parent d258793b
......@@ -49,18 +49,23 @@ class AppUrlTest extends ApiIntegrationTestCase
// Mapping method
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Check that the redirections are working
* when the language prefix is not present in the URL.
* Providing the list of URLs to be tested
* with the expected redirection URLs (with english prefix at begining of URL)
*
* @group public
* @group url
* @return void
* @param bool $extractOnly
* @return string[] example: Array('/<tested_URL>' => '/en/<expected_URL>')
*/
public function testRedirectWhenLangPrefixIsMissing()
private function getEnglishUrls($extractOnly = false)
{
// Format: 'Tested URL' => 'Expected URL'
$englishUrls = [
$urlsExtract = [
'/' => '/en/',
'/pages/contact' => '/en/pages/contact',
'/users/2' => '/en/users/2',
]; // Format: ''/<tested_URL>' => '/en/<expected_URL>'
$urls = [
'/' => '/en/',
'/pages/index' => '/en/',
'/pages/contact' => '/en/pages/contact',
......@@ -76,19 +81,113 @@ class AppUrlTest extends ApiIntegrationTestCase
'/softwares/usersSoftware/3' => '/en/softwares/usersSoftware/3',
'/softwares/1/reviews' => '/en/softwares/1/reviews',
'/softwares/1/reviews/2' => '/en/softwares/1/reviews/2',
];
$frenchUrls = [];
foreach ($englishUrls as $testedUrl => $expectedUrl) {
$frenchUrls[$testedUrl] = str_replace('/en/', '/fr/', $expectedUrl);
]; // Format: '/<tested_URL>' => '/en/<expected_URL>'
if ($extractOnly === true) {
return $urlsExtract;
}
return $urls;
}
/**
* Providing the list of URLs to be tested
* with the expected redirection URLs (with french prefix at begining of URL)
*
* @param bool $extractOnly
* @return string[] example: Array('/<tested_URL>' => '/fr/<expected_URL>')
*/
private function getFrenchUrls($extractOnly = false)
{
$urls = [];
foreach ($this->getEnglishUrls($extractOnly) as $testedUrl => $expectedUrl) {
$urls[$testedUrl] = str_replace('/en/', '/fr/', $expectedUrl);
}
return $urls;
}
/**
* Check that the redirections are working
* when the language prefix is not present in the URL.
*
* @group public
* @group url
* @return void
*/
public function testRedirectWhenLangPrefixIsMissing()
{
// Load data
$englishUrls = $this->getEnglishUrls(); // Format: '/<tested_URL>' => '/en/<expected_URL>'
$frenchUrls = $this->getFrenchUrls(); // Format: '/<tested_URL>' => '/fr/<expected_URL>'
// Checki if the URLs redirects correctly
$this->get("/en/"); // force user language to english
$this->commonCheck($englishUrls);
$this->get("/fr/"); // force user language to french
$this->commonCheck($frenchUrls);
$acceptLanguages = ['en']; // force user language to english using HTTP header "Accept-Language"
$this->commonCheck($englishUrls, $acceptLanguages);
$acceptLanguages = ['fr']; // force user language to french using HTTP header "Accept-Language"
$this->commonCheck($frenchUrls, $acceptLanguages);
}
/**
* Check if the URLs redirects correctly for English variations of HTTP 'Accept-Language' header
* when the language prefix is not present in the URL.
*
* @group public
* @group url
* @return void
*/
public function testRedirectForEnglishVariationsOfHttpAcceptLanguage()
{
$urls = $this->getEnglishUrls(true); // Format: '/<tested_URL>' => '/en/<expected_URL>'
$acceptLanguages = [
'en-US',
'en-gb',
'en-GB,en;q=0.5',
];
$this->commonCheck($urls, $acceptLanguages);
}
/**
* Check if the URLs redirects correctly for French variations of HTTP 'Accept-Language' header
* when the language prefix is not present in the URL.
*
* @group public
* @group url
* @return void
*/
public function testRedirectForFrenchVariationsOfHttpAcceptLanguage()
{
$urls = $this->getFrenchUrls(true); // Format: '/<tested_URL>' => '/fr/<expected_URL>'
$acceptLanguages = [
'fr-CA',
'fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5',
];
$this->commonCheck($urls, $acceptLanguages);
}
/**
* Check if the URLs redirects correctly for not allowed HTTP 'Accept-Language' header
* when the language prefix is not present in the URL.
*
* @group public
* @group url
* @return void
*/
public function testRedirectWhenHttpAcceptLanguageIsNotAllowed()
{
$urls = $this->getEnglishUrls(true); // Format: '/<tested_URL>' => '/en/<expected_URL>'
$acceptLanguages = [
'de',
'de-CH',
'da, en-GB;q=0.8, en;q=0.7',
'zh, en-us; q=0.8, en; q=0.6',
];
$this->commonCheck($urls, $acceptLanguages);
}
/**
* Check that redirections are working
* for old URLs
......@@ -110,20 +209,25 @@ class AppUrlTest extends ApiIntegrationTestCase
}
// Checki if the URLs redirects correctly
$this->get("/en/"); // force user language to english
$this->commonCheck($urls);
$this->commonCheck($englishUrls);
$acceptLanguages = ['en']; // force user language to english using HTTP header "Accept-Language"
$this->commonCheck($urls, $acceptLanguages);
$this->commonCheck($englishUrls, $acceptLanguages);
}
/**
* Checking if a list of URLs redirects correctly
* and force user language using HTTP header "Accept-Language"
*
* @param array $urls example: [ 'Tested URL' => 'Expected URL', ... ]
* @param array $urls
* @param array|string[] $acceptLanguages default: ['en']
*/
private function commonCheck(array $urls)
private function commonCheck(array $urls, array $acceptLanguages = ['en'])
{
foreach ($urls as $testedUrl => $expectedUrl) {
$this->checkUrlRedirectToAnotherUrl($testedUrl, $expectedUrl, ['html'], 301);
foreach ($acceptLanguages as $acceptLanguage) {
$this->_request['headers']['Accept-Language'] = "$acceptLanguage";
foreach ($urls as $testedUrl => $expectedUrl) {
$this->checkUrlRedirectToAnotherUrl($testedUrl, $expectedUrl, ['html'], 301);
}
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment