Skip to content

Commit

Permalink
Split up test cases into more classes
Browse files Browse the repository at this point in the history
  • Loading branch information
mikehaertl committed Sep 14, 2015
1 parent 85cd638 commit 7784a52
Show file tree
Hide file tree
Showing 9 changed files with 434 additions and 395 deletions.
244 changes: 244 additions & 0 deletions tests/RedirectTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
<?php

use yii\helpers\Url;

class RedirectTest extends TestCase
{
public function testRedirectsIfDefaultLanguageInUrlAndDefaultLanguageUsesNoSuffix()
{
$this->expectRedirect('/site/page');
$this->mockUrlManager([
'languages' => ['en-US', 'en', 'de'],
]);
$this->mockRequest('/en/site/page');
}

public function testRedirectsToRootIfOnlyDefaultLanguageInUrlAndDefaultLanguageUsesNoSuffix()
{
$this->expectRedirect('/');
$this->mockUrlManager([
'languages' => ['en-US', 'en', 'de'],
]);
$this->mockRequest('/en');
}

public function testRedirectsRootToDefaultLanguageIfDefaultLanguageUsesSuffix()
{
$this->expectRedirect('/en');
$this->mockUrlManager([
'languages' => ['en-US', 'en', 'de'],
'enableDefaultLanguageUrlCode' => true,
]);
$this->mockRequest('/');
}

public function testRedirectsIfNoLanguageInUrlAndDefaultLanguageUsesSuffix()
{
$this->expectRedirect('/en/site/page');
$this->mockUrlManager([
'languages' => ['en-US', 'en', 'de'],
'enableDefaultLanguageUrlCode' => true,
]);
$this->mockRequest('/site/page');
}

public function testRedirectsIfDefaultLanguageInUrl()
{
$this->expectRedirect('/');
$this->mockUrlManager([
'languages' => ['en'],
]);
$this->mockRequest('/en');
}

public function testRedirectsIfLanguageWithUpperCaseCountryInUrl()
{
$this->expectRedirect('/es-bo/site/page');
$this->mockUrlManager([
'languages' => ['en-US', 'deutsch' => 'de', 'es-BO'],
]);
$this->mockRequest('/es-BO/site/page');
}

public function testRedirectsIfLanguageWithUpperCaseWildcardCountryInUrl()
{
$this->expectRedirect('/es-bo/site/page');
$this->mockUrlManager([
'languages' => ['en-US', 'deutsch' => 'de', 'es-*'],
]);
$this->mockRequest('/es-BO/site/page');
}

public function testRedirectsIfNoLanguageInUrlAndAcceptedLanguageMatches()
{
$this->expectRedirect('/de/site/page');
$this->mockUrlManager([
'languages' => ['en-US', 'en', 'de'],
]);
$this->mockRequest('/site/page',[
'acceptableLanguages' => ['de'],
]);
}

public function testRedirectsIfNoLanguageInUrlAndAcceptedLanguageMatchesWildcard()
{
$this->expectRedirect('/de/site/page');
$this->mockUrlManager([
'languages' => ['en-US', 'en', 'de-*'],
]);
$this->mockRequest('/site/page',[
'acceptableLanguages' => ['de'],
]);
}

public function testRedirectsIfNoLanguageInUrlAndAcceptedLanguageWithCountryMatches()
{
$this->expectRedirect('/de-at/site/page');
$this->mockUrlManager([
'languages' => ['en-US', 'en', 'de', 'de-AT'],
]);
$this->mockRequest('/site/page',[
'acceptableLanguages' => ['de-AT', 'de', 'en'],
]);
}

public function testRedirectsIfNoLanguageInUrlAndAcceptedLanguageWithCountryMatchesWildcard()
{
$this->expectRedirect('/de-at/site/page');
$this->mockUrlManager([
'languages' => ['en-US', 'en', 'de-*'],
]);
$this->mockRequest('/site/page',[
'acceptableLanguages' => ['de-AT', 'de', 'en'],
]);
}

public function testRedirectsIfNoLanguageInUrlAndAcceptedLanguageWithCountryMatchesCountryAlias()
{
$this->expectRedirect('/at/site/page');
$this->mockUrlManager([
'languages' => ['de', 'at'=>'de-AT'],
]);
$this->mockRequest('/site/page',[
'acceptableLanguages' => ['de-at', 'de'],
]);
}

public function testRedirectsIfNoLanguageInUrlAndAcceptedLanguageMatchesLanguageAndCountryAlias()
{
$this->expectRedirect('/de/site/page');
$this->mockUrlManager([
'languages' => ['de', 'at'=>'de-AT'],
]);
$this->mockRequest('/site/page',[
'acceptableLanguages' => ['en-US', 'en', 'de'],
]);
}

public function testRedirectsIfNoLanguageInUrlAndAcceptedLanguageWithLowercaseCountryMatches()
{
$this->expectRedirect('/de-at/site/page');
$this->mockUrlManager([
'languages' => ['en-US', 'en', 'de', 'de-AT'],
]);
$this->mockRequest('/site/page',[
'acceptableLanguages' => ['de-at', 'de', 'en'],
]);
}

public function testRedirectsIfNoLanguageInUrlAndAcceptedLanguageWithCountryMatchesLanguage()
{
$this->expectRedirect('/de/site/page');
$this->mockUrlManager([
'languages' => ['en-US', 'en', 'de'],
]);
$this->mockRequest('/site/page',[
'acceptableLanguages' => ['de-at'],
]);
}

public function testRedirectsIfNoLanguageInUrlAndLanguageInSession()
{
$this->expectRedirect('/de/site/page');
@session_start();
$_SESSION['_language'] = 'de';
$this->mockUrlManager( [
'languages' => ['en-US', 'en', 'de'],
]);
$this->mockRequest('/site/page');
}

public function testRedirectsIfNoLanguageInUrlAndLanguageInSessionMatchesWildcard()
{
$this->expectRedirect('/de/site/page');
@session_start();
$_SESSION['_language'] = 'de';
$this->mockUrlManager( [
'languages' => ['en-US', 'en', 'de-*'],
]);
$this->mockRequest('/site/page');
}

public function testRedirectsIfNoLanguageInUrlAndLanguageInCookie()
{
$this->expectRedirect('/de/site/page');
$_COOKIE['_language'] = 'de';
$this->mockUrlManager( [
'languages' => ['en-US', 'en', 'de'],
]);
$this->mockRequest('/site/page');
}

public function testRedirectsIfNoLanguageInUrlAndLanguageInCookieMatchesWildcard()
{
$this->expectRedirect('/de/site/page');
$_COOKIE['_language'] = 'de';
$this->mockUrlManager( [
'languages' => ['en-US', 'en', 'de-*'],
]);
$this->mockRequest('/site/page');
}

public function testRedirectsIfUrlDoesNotMatchIgnoresUrls()
{
$this->expectRedirect('/site/page');
$this->mockUrlManager([
'languages' => ['en-US', 'en', 'de'],
'ignoreLanguageUrlPatterns' => [
'#not/used#' => '#^site/other#'
],
]);
$this->mockRequest('/en/site/page');
}

public function testRedirectsIfDefaultLanguageInUrlAndDefaultLanguageUsesNoSuffixAndTrailingSlashEnabled()
{
$this->expectRedirect('/site/page/');
$this->mockUrlManager([
'languages' => ['en-US', 'en', 'de'],
'suffix' => '/'
]);
$this->mockRequest('/en/site/page/');
}

public function testRedirectsToRootIfOnlyDefaultLanguageInUrlAndDefaultLanguageUsesNoSuffixAndTrailingSlashEnabled()
{
$this->expectRedirect('/');
$this->mockUrlManager([
'languages' => ['en-US', 'en', 'de'],
'suffix' => '/',
]);
$this->mockRequest('/en');
}

public function testRedirectsRootToDefaultLanguageIfDefaultLanguageUsesSuffixAndTrailingSlashEnabled()
{
$this->expectRedirect('/en/');
$this->mockUrlManager([
'languages' => ['en-US', 'en', 'de'],
'enableDefaultLanguageUrlCode' => true,
'suffix' => '/',
]);
$this->mockRequest('/');
}
}
9 changes: 9 additions & 0 deletions tests/RedirectWithBaseUrlAndScriptNameTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use yii\helpers\Url;

class RedirectWithBaseUrlAndScriptName extends RedirectTest
{
protected $baseUrl = '/base';
protected $showScriptName = true;
}
8 changes: 8 additions & 0 deletions tests/RedirectWithBaseUrlTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

use yii\helpers\Url;

class RedirectWithBaseUrlTest extends RedirectTest
{
protected $baseUrl = '/base';
}
8 changes: 8 additions & 0 deletions tests/RedirectWithScriptNameTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

use yii\helpers\Url;

class RedirectWithScriptNameTest extends RedirectTest
{
protected $showScriptName = true;
}
Loading

0 comments on commit 7784a52

Please sign in to comment.