Skip to content

Commit

Permalink
locales mapping test (#601)
Browse files Browse the repository at this point in the history
* added script to composer running tests

* added localesMapping test on language negotiation
  • Loading branch information
addorange authored and Marc Cámara committed Oct 16, 2018
1 parent 9807eab commit f79896f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
"suggest": {
"ext-intl": "*"
},
"scripts": {
"test": "vendor/bin/phpunit"
},
"autoload": {
"classmap": [
],
Expand Down
38 changes: 37 additions & 1 deletion tests/LocalizerTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,7 @@ public function testCreateUrlFromUri()
* @dataProvider accept_language_variations_data
*/
public function testLanguageNegotiation($accept_string, $must_resolve_to, $asd = null) {

$full_config = include __DIR__ . '/full-config/config.php';

$request = $this->createMock(\Illuminate\Http\Request::class);
Expand All @@ -717,9 +718,11 @@ public function testLanguageNegotiation($accept_string, $must_resolve_to, $asd =

$language = $negotiator->negotiateLanguage();


$this->assertEquals($must_resolve_to, $language);
}


public function accept_language_variations_data() {
$variations = [
['en-GB', 'en-GB'],
Expand All @@ -730,7 +733,7 @@ public function accept_language_variations_data() {
['fr-CA,fr;q=0.8', 'fr-CA'],
['fr-150', 'fr'],
['zh-Hant-cn', 'zh-Hant'],
['zh-cn', 'zh']
['zh-cn', 'zh'],
];

$dataset = [];
Expand All @@ -740,4 +743,37 @@ public function accept_language_variations_data() {

return $dataset;
}

public function testLanguageNegotiationWithMapping() {

$accept_string = 'en-GB';
$must_resolve_to = 'uk';

$mapping = [
$accept_string => $must_resolve_to
];

$full_config = include __DIR__ . '/full-config/config.php';

$full_config['supportedLocales']['uk'] = $full_config['supportedLocales']['en-GB'];
unset($full_config['supportedLocales']['en-GB']);

app('config')->set('laravellocalization.localesMapping', $mapping);

$request = $this->createMock(\Illuminate\Http\Request::class);
$request->expects($this->any())->method('header')->with('Accept-Language')->willReturn($accept_string);

$negotiator = app(\Mcamara\LaravelLocalization\LanguageNegotiator::class,
[
'defaultLocale' => 'wrong',
'supportedLanguages' => $full_config['supportedLocales'],
'request' => $request
]
);

$language = $negotiator->negotiateLanguage();

$this->assertEquals($must_resolve_to, $language);
}

}
5 changes: 4 additions & 1 deletion tests/full-config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,14 @@
//
'hideDefaultLocaleInURL' => false,

// If you want to display the locales in particular order in the language selector you should write the order here.
// If you want to display the locales in particular order in the language selector you should write the order here.
//CAUTION: Please consider using the appropriate locale code otherwise it will not work
//Example: 'localesOrder' => ['es','en'],
'localesOrder' => [],

// If you want to use custom lang url segments like 'at' instead of 'de-AT', you can use the mapping to tallow the LanguageNegotiator to assign the descired locales based on HTTP Accept Language Header. For example you want ot use 'at', so map HTTP Accept Language Header 'de-AT' to 'at' (['de-AT' => 'at']).
'localesMapping' => [],

// URLs which should not be processed, e.g. '/nova', '/nova/*', '/nova-api/*' or specific application URLs
// Defaults to []
'urlsIgnored' => [],
Expand Down

0 comments on commit f79896f

Please sign in to comment.