class LocaleBrowserDetectionTest

Test browser language detection.

Hierarchy

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

Contains filters are case sensitive
Namesort descending Modifiers Type Description
DrupalTestCase::$assertions protected property Assertions thrown in that test case.
DrupalTestCase::$databasePrefix protected property The database prefix of this test run.
DrupalTestCase::$originalFileDirectory protected property The original file directory, before it was changed for testing purposes.
DrupalTestCase::$results public property Current results of this test case.
DrupalTestCase::$setup protected property Flag to indicate whether the test has been set up.
DrupalTestCase::$setupDatabasePrefix protected property
DrupalTestCase::$setupEnvironment protected property
DrupalTestCase::$skipClasses protected property This class is skipped when looking for the source of an assertion.
DrupalTestCase::$testId protected property The test run ID.
DrupalTestCase::$timeLimit protected property Time limit for the test.
DrupalTestCase::assert protected function Internal helper: stores the assert.
DrupalTestCase::assertEqual protected function Check to see if two values are equal.
DrupalTestCase::assertFalse protected function Check to see if a value is false (an empty string, 0, NULL, or FALSE).
DrupalTestCase::assertIdentical protected function Check to see if two values are identical.
DrupalTestCase::assertNotEqual protected function Check to see if two values are not equal.
DrupalTestCase::assertNotIdentical protected function Check to see if two values are not identical.
DrupalTestCase::assertNotNull protected function Check to see if a value is not NULL.
DrupalTestCase::assertNull protected function Check to see if a value is NULL.
DrupalTestCase::assertTrue protected function Check to see if a value is not false (not an empty string, 0, NULL, or FALSE).
DrupalTestCase::deleteAssert public static function Delete an assertion record by message ID.
DrupalTestCase::error protected function Fire an error assertion.
DrupalTestCase::errorHandler public function Handle errors during test runs.
DrupalTestCase::exceptionHandler protected function Handle exceptions.
DrupalTestCase::fail protected function Fire an assertion that is always negative.
DrupalTestCase::generatePermutations public static function Converts a list of possible parameters into a stack of permutations.
DrupalTestCase::getAssertionCall protected function Cycles through backtrace until the first non-assertion method is found.
DrupalTestCase::insertAssert public static function Store an assertion from outside the testing context.
DrupalTestCase::pass protected function Fire an assertion that is always positive.
DrupalTestCase::randomName public static function Generates a random string containing letters and numbers.
DrupalTestCase::randomString public static function Generates a random string of ASCII characters of codes 32 to 126.
DrupalTestCase::run public function Run all tests in this class.
DrupalTestCase::verbose protected function Logs verbose message in a text file.
DrupalUnitTestCase::setUp protected function Sets up unit test environment.
DrupalUnitTestCase::tearDown protected function
DrupalUnitTestCase::__construct function Constructor for DrupalUnitTestCase. Overrides DrupalTestCase::__construct
LocaleBrowserDetectionTest::getInfo public static function
LocaleBrowserDetectionTest::testLanguageFromBrowser function Unit tests for the locale_language_from_browser() function.