class LocaleBrowserDetectionTest
Test browser language detection.
Hierarchy
- class \DrupalTestCase
- class \DrupalUnitTestCase
- class \LocaleBrowserDetectionTest
- class \DrupalUnitTestCase
Expanded class hierarchy of LocaleBrowserDetectionTest
File
- drupal-7.x/
modules/ locale/ locale.test, line 1640 - Tests for locale.module.
View source
class LocaleBrowserDetectionTest extends DrupalUnitTestCase {
public static function getInfo() {
return array(
'name' => 'Browser language detection',
'description' => 'Tests for the browser language detection.',
'group' => 'Locale',
);
}
/**
* Unit tests for the locale_language_from_browser() function.
*/
function testLanguageFromBrowser() {
// Load the required functions.
require_once DRUPAL_ROOT . '/includes/locale.inc';
$languages = array(
// In our test case, 'en' has priority over 'en-US'.
'en' => (object) array(
'language' => 'en',
),
'en-US' => (object) array(
'language' => 'en-US',
),
// But 'fr-CA' has priority over 'fr'.
'fr-CA' => (object) array(
'language' => 'fr-CA',
),
'fr' => (object) array(
'language' => 'fr',
),
// 'es-MX' is alone.
'es-MX' => (object) array(
'language' => 'es-MX',
),
// 'pt' is alone.
'pt' => (object) array(
'language' => 'pt',
),
// Language codes with more then one dash are actually valid.
// eh-oh-laa-laa is the official language code of the Teletubbies.
'eh-oh-laa-laa' => (object) array(
'language' => 'eh-oh-laa-laa',
),
);
$test_cases = array(
// Equal qvalue for each language, choose the site preferred one.
'en,en-US,fr-CA,fr,es-MX' => 'en',
'en-US,en,fr-CA,fr,es-MX' => 'en',
'fr,en' => 'en',
'en,fr' => 'en',
'en-US,fr' => 'en',
'fr,en-US' => 'en',
'fr,fr-CA' => 'fr-CA',
'fr-CA,fr' => 'fr-CA',
'fr' => 'fr-CA',
'fr;q=1' => 'fr-CA',
'fr,es-MX' => 'fr-CA',
'fr,es' => 'fr-CA',
'es,fr' => 'fr-CA',
'es-MX,de' => 'es-MX',
'de,es-MX' => 'es-MX',
// Different cases and whitespace.
'en' => 'en',
'En' => 'en',
'EN' => 'en',
' en' => 'en',
'en ' => 'en',
'en, fr' => 'en',
// A less specific language from the browser matches a more specific one
// from the website, and the other way around for compatibility with
// some versions of Internet Explorer.
'es' => 'es-MX',
'es-MX' => 'es-MX',
'pt' => 'pt',
'pt-PT' => 'pt',
'pt-PT;q=0.5,pt-BR;q=1,en;q=0.7' => 'en',
'pt-PT;q=1,pt-BR;q=0.5,en;q=0.7' => 'en',
'pt-PT;q=0.4,pt-BR;q=0.1,en;q=0.7' => 'en',
'pt-PT;q=0.1,pt-BR;q=0.4,en;q=0.7' => 'en',
// Language code with several dashes are valid. The less specific language
// from the browser matches the more specific one from the website.
'eh-oh-laa-laa' => 'eh-oh-laa-laa',
'eh-oh-laa' => 'eh-oh-laa-laa',
'eh-oh' => 'eh-oh-laa-laa',
'eh' => 'eh-oh-laa-laa',
// Different qvalues.
'fr,en;q=0.5' => 'fr-CA',
'fr,en;q=0.5,fr-CA;q=0.25' => 'fr',
// Silly wildcards are also valid.
'*,fr-CA;q=0.5' => 'en',
'*,en;q=0.25' => 'fr-CA',
'en,en-US;q=0.5,fr;q=0.25' => 'en',
'en-US,en;q=0.5,fr;q=0.25' => 'en-US',
// Unresolvable cases.
'' => FALSE,
'de,pl' => FALSE,
'iecRswK4eh' => FALSE,
$this->randomName(10) => FALSE,
);
foreach ($test_cases as $accept_language => $expected_result) {
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = $accept_language;
$result = locale_language_from_browser($languages);
$this->assertIdentical($result, $expected_result, format_string("Language selection '@accept-language' selects '@result', result = '@actual'", array('@accept-language' => $accept_language, '@result' => $expected_result, '@actual' => isset($result) ? $result : 'none')));
}
}
}
Members
Name | Modifiers | Type | Description |
---|---|---|---|
DrupalTestCase:: |
protected | property | Assertions thrown in that test case. |
DrupalTestCase:: |
protected | property | The database prefix of this test run. |
DrupalTestCase:: |
protected | property | The original file directory, before it was changed for testing purposes. |
DrupalTestCase:: |
public | property | Current results of this test case. |
DrupalTestCase:: |
protected | property | Flag to indicate whether the test has been set up. |
DrupalTestCase:: |
protected | property | |
DrupalTestCase:: |
protected | property | |
DrupalTestCase:: |
protected | property | This class is skipped when looking for the source of an assertion. |
DrupalTestCase:: |
protected | property | The test run ID. |
DrupalTestCase:: |
protected | property | Time limit for the test. |
DrupalTestCase:: |
protected | function | Internal helper: stores the assert. |
DrupalTestCase:: |
protected | function | Check to see if two values are equal. |
DrupalTestCase:: |
protected | function | Check to see if a value is false (an empty string, 0, NULL, or FALSE). |
DrupalTestCase:: |
protected | function | Check to see if two values are identical. |
DrupalTestCase:: |
protected | function | Check to see if two values are not equal. |
DrupalTestCase:: |
protected | function | Check to see if two values are not identical. |
DrupalTestCase:: |
protected | function | Check to see if a value is not NULL. |
DrupalTestCase:: |
protected | function | Check to see if a value is NULL. |
DrupalTestCase:: |
protected | function | Check to see if a value is not false (not an empty string, 0, NULL, or FALSE). |
DrupalTestCase:: |
public static | function | Delete an assertion record by message ID. |
DrupalTestCase:: |
protected | function | Fire an error assertion. |
DrupalTestCase:: |
public | function | Handle errors during test runs. |
DrupalTestCase:: |
protected | function | Handle exceptions. |
DrupalTestCase:: |
protected | function | Fire an assertion that is always negative. |
DrupalTestCase:: |
public static | function | Converts a list of possible parameters into a stack of permutations. |
DrupalTestCase:: |
protected | function | Cycles through backtrace until the first non-assertion method is found. |
DrupalTestCase:: |
public static | function | Store an assertion from outside the testing context. |
DrupalTestCase:: |
protected | function | Fire an assertion that is always positive. |
DrupalTestCase:: |
public static | function | Generates a random string containing letters and numbers. |
DrupalTestCase:: |
public static | function | Generates a random string of ASCII characters of codes 32 to 126. |
DrupalTestCase:: |
public | function | Run all tests in this class. |
DrupalTestCase:: |
protected | function | Logs verbose message in a text file. |
DrupalUnitTestCase:: |
protected | function | Sets up unit test environment. |
DrupalUnitTestCase:: |
protected | function | |
DrupalUnitTestCase:: |
function |
Constructor for DrupalUnitTestCase. Overrides DrupalTestCase:: |
|
LocaleBrowserDetectionTest:: |
public static | function | |
LocaleBrowserDetectionTest:: |
function | Unit tests for the locale_language_from_browser() function. |