Source of file MultilingualControllerTest.php
Size: 7,747 Bytes - Last Modified: 2021-12-23T10:58:00+00:00
/var/www/docs.ssmods.com/process/src/tests/MultilingualControllerTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 | <?php class MultilingualControllerTest extends FunctionalTest { private $origLocale; private $origCurrentLocale; private $origAllowedLocales; private $origi18nLocale; private $origCookieLocale; private $origAcceptLanguage; private $origLocaleRoutingEnabled; private $origDashLocaleEnabled; private $origCountryOnly; protected $autoFollowRedirection=false; public function setUp() { parent::setUp(); $this->origCountryOnly=MultilingualRootURLController::config()->use_country_only; MultilingualRootURLController::config()->use_country_only=false; $this->origLocaleRoutingEnabled=MultilingualRootURLController::config()->UseLocaleURL; MultilingualRootURLController::config()->UseLocaleURL=false; $this->origDashLocaleEnabled=MultilingualRootURLController::config()->UseDashLocale; MultilingualRootURLController::config()->UseDashLocale=false; $this->origAcceptLanguage=$_SERVER['HTTP_ACCEPT_LANGUAGE']; $_SERVER['HTTP_ACCEPT_LANGUAGE']='en-US,en;q=0.5'; $this->origCookieLocale=Cookie::get('language'); Cookie::force_expiry('language'); Cookie::set('language', ''); $this->origCurrentLocale=Translatable::get_current_locale(); Translatable::set_current_locale('en_US'); $this->origLocale=Translatable::default_locale(); Translatable::set_default_locale('en_US'); $this->origi18nLocale=i18n::get_locale(); i18n::set_locale('en_US'); $this->origAllowedLocales=Translatable::get_allowed_locales(); Translatable::set_allowed_locales(array('en_US', 'fr_FR')); MultilingualTestController::add_extension('MultilingualControllerExtension'); Config::inst()->update('Director', 'rules', array( '$Language/multilingual-test-controller//$Action/$ID/$OtherID'=>'MultilingualTestController' )); MultilingualRootURLController::reset(); } public function tearDown() { MultilingualRootURLController::config()->use_country_only=$this->origCountryOnly; MultilingualRootURLController::config()->UseLocaleURL=$this->origLocaleRoutingEnabled; MultilingualRootURLController::config()->UseDashLocale=$this->origDashLocaleEnabled; Translatable::set_current_locale($this->origCurrentLocale); Translatable::set_default_locale($this->origLocale); Translatable::set_allowed_locales($this->origAllowedLocales); i18n::set_locale($this->origi18nLocale); Cookie::force_expiry('language'); if($this->origCookieLocale) { Cookie::set('language', $this->origCookieLocale); } $_SERVER['HTTP_ACCEPT_LANGUAGE']=$this->origAcceptLanguage; MultilingualRootURLController::reset(); parent::tearDown(); } /** * Tests to see if the controller responds correctly if the language is in the url */ public function testLanguageInURL() { //Perform Request $response=$this->get('fr/multilingual-test-controller/'); //Ensure a 200 response $this->assertEquals(200, $response->getStatusCode()); //Verify the response matches what is expected $this->assertEquals('i18n: fr_FR|Translatable: fr_FR', $response->getBody()); } /** * Tests to see if the controller responds correctly if the locale is in the url */ public function testLocaleInURL() { //Enable locale urls MultilingualRootURLController::config()->UseLocaleURL=true; //Set accept language to french $_SERVER['HTTP_ACCEPT_LANGUAGE']='fr-FR,fr;q=0.5'; //Get the root url $response=$this->get('fr_FR/multilingual-test-controller/'); //Ensure a 200 response $this->assertEquals(200, $response->getStatusCode()); //Verify the response matches what is expected $this->assertEquals('i18n: fr_FR|Translatable: fr_FR', $response->getBody()); } /** * Tests to see if the controller responds correctly if the dashed locale is in the url */ public function testDashLocaleInURL() { //Enable locale urls MultilingualRootURLController::config()->UseLocaleURL=true; MultilingualRootURLController::config()->UseDashLocale=true; //Set accept language to french $_SERVER['HTTP_ACCEPT_LANGUAGE']='fr-FR,fr;q=0.5'; //Get the root url $response=$this->get('fr-fr/multilingual-test-controller/'); //Ensure a 200 response $this->assertEquals(200, $response->getStatusCode()); //Verify the response matches what is expected $this->assertEquals('i18n: fr_FR|Translatable: fr_FR', $response->getBody()); } /** * Tests to see if the controller responds correctly if the county is in the url */ public function testCountryInURL() { //Enable locale urls MultilingualRootURLController::config()->use_country_only=true; //Set accept language to french $_SERVER['HTTP_ACCEPT_LANGUAGE']='fr-FR,fr;q=0.5'; //Change the allowed locales to have fr_CA Translatable::set_allowed_locales(array('en_US', 'fr_CA')); //Get the root url $response=$this->get('ca/multilingual-test-controller/'); //Ensure a 200 response $this->assertEquals(200, $response->getStatusCode()); //Verify the response matches what is expected $this->assertEquals('i18n: fr_CA|Translatable: fr_CA', $response->getBody()); } /** * Tests to see if the controller responds correctly if the language is in the url */ public function testAutoDetectLanguage() { //Set accept language to french $_SERVER['HTTP_ACCEPT_LANGUAGE']='fr-FR,fr;q=0.5'; Translatable::set_default_locale('fr_FR'); Translatable::set_current_locale('fr_FR'); i18n::set_locale('fr_FR'); //Perform Request $response=$this->get('MultilingualTestController'); //Ensure a 200 response $this->assertEquals(200, $response->getStatusCode()); //Verify the response matches what is expected $this->assertEquals('i18n: fr_FR|Translatable: fr_FR', $response->getBody()); } /** * Tests to see if the controller responds correctly if the language is in the url */ public function testInvalidLanguageURL() { //Perform Request $response=$this->get('es/multilingual-test-controller/'); //Ensure a 301 response $this->assertEquals(301, $response->getStatusCode()); //Verify the redirect url matches the default $this->assertEquals(Director::baseURL().'en/multilingual-test-controller', $response->getHeader('Location')); } } class MultilingualTestController extends Controller implements TestOnly { private static $extensions=array( 'MultilingualControllerExtension' ); public function index() { return 'i18n: '.i18n::get_locale().'|Translatable: '.Translatable::get_current_locale(); } } ?> |