1 locale.test LocalePluralFormatTest::testGetPluralFormat()

Tests locale_get_plural() functionality.

File

core/modules/locale/tests/locale.test, line 616
Tests for locale.module.

Class

LocalePluralFormatTest
Tests plural index computation functionality.

Code

function testGetPluralFormat() {
  // Import some .po files with formulas to set up the environment.
  // These will also add the languages to the system and enable them.
  $this->importPoFile($this->getPoFileWithSimplePlural(), array(
    'langcode' => 'fr',
  ));
  $this->importPoFile($this->getPoFileWithComplexPlural(), array(
    'langcode' => 'hr',
  ));

  // Attempt to import some broken .po files as well to prove that these
  // will not overwrite the proper plural formula imported above.
  $this->importPoFile($this->getPoFileWithMissingPlural(), array(
    'langcode' => 'fr',
  ));
  $this->importPoFile($this->getPoFileWithBrokenPlural(), array(
    'langcode' => 'hr',
  ));

  // Reset static caches from locale_get_plural() to ensure we get fresh data.
  backdrop_static_reset('locale_get_plural');
  backdrop_static_reset('locale_get_plural:plurals');

  // Test locale_get_plural() for English (no formula present).
  $this->assertIdentical(locale_get_plural(1, 'en'), 0, "Computed plural index for 'en' with count 1 is 0.");
  $this->assertIdentical(locale_get_plural(0, 'en'), 1, "Computed plural index for 'en' with count 0 is 1.");
  $this->assertIdentical(locale_get_plural(5, 'en'), 1, "Computed plural index for 'en' with count 5 is 1.");

  // Test locale_get_plural() for French (simpler formula).
  $this->assertIdentical(locale_get_plural(1, 'fr'), 0, "Computed plural index for 'fr' with count 1 is 0.");
  $this->assertIdentical(locale_get_plural(0, 'fr'), 0, "Computed plural index for 'fr' with count 0 is 0.");
  $this->assertIdentical(locale_get_plural(5, 'fr'), 1, "Computed plural index for 'fr' with count 5 is 1.");

  // Test locale_get_plural() for Croatian (more complex formula).
  $this->assertIdentical(locale_get_plural(1, 'hr'), 0, "Computed plural index for 'hr' with count 1 is 0.");
  $this->assertIdentical(locale_get_plural(21, 'hr'), 0, "Computed plural index for 'hr' with count 21 is 0.");
  $this->assertIdentical(locale_get_plural(0, 'hr'), 2, "Computed plural index for 'hr' with count 0 is 2.");
  $this->assertIdentical(locale_get_plural(2, 'hr'), 1, "Computed plural index for 'hr' with count 2 is 1.");
  $this->assertIdentical(locale_get_plural(8, 'hr'), 2, "Computed plural index for 'hr' with count 8 is 2.");

  // Test locale_get_plural() for Hungarian (nonexistent language).
  $this->assertIdentical(locale_get_plural(1, 'hu'), -1, "Computed plural index for 'hu' with count 1 is -1.");
  $this->assertIdentical(locale_get_plural(21, 'hu'), -1, "Computed plural index for 'hu' with count 21 is -1.");
  $this->assertIdentical(locale_get_plural(0, 'hu'), -1, "Computed plural index for 'hu' with count 0 is -1.");
}