[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/tests/phpunit/testcases/ -> test_locales.php (source)

   1  <?php
   2  
   3  /**
   4   * @group locales
   5   */
   6  class GP_Test_Locales extends GP_UnitTestCase {
   7  
   8      const SKIP_LOCALES = [
   9          // Actual: n != 1
  10          // Expected: n > 1
  11          'ak', 'am', 'as', 'bho', 'bn', 'fuc', 'ful', 'gu', 'hi', 'hy', 'kn', 'mg',
  12          'nso', 'pt-ao', 'si', 'wa', 'zul',
  13  
  14          // Actual: n > 1
  15          // Expected: n != 1
  16          'orm', 'tr', 'tuk',
  17  
  18          // Actual: n != 1
  19          // Expected: 0
  20          'bm', 'jv', 'ka', 'mya', 'nqo', 'sah', 'yor',
  21  
  22          // Actual: n > 1
  23          // Expected: (n % 10 == 1 && n % 100 != 11 && n % 100 != 71 && n % 100 != 91) ? 0 : ((n % 10 == 2 && n % 100 != 12 && n % 100 != 72 && n % 100 != 92) ? 1 : ((((n % 10 == 3 || n % 10 == 4) || n % 10 == 9) && (n % 100 < 10 || n % 100 > 19) && (n % 100 < 70 || n % 100 > 79) && (n % 100 < 90 || n % 100 > 99)) ? 2 : ((n != 0 && n % 1000000 == 0) ? 3 : 4)))
  24          'br',
  25  
  26          // Actual: n != 1
  27          // Expected: n != 1 && n != 2 && n != 3 && (n % 10 == 4 || n % 10 == 6 || n % 10 == 9)
  28          'ceb', 'tl',
  29  
  30          // Actual: (n==1) ? 0 : (n==2) ? 1 : (n != 8 && n != 11) ? 2 : 3
  31          // Expected: (n == 0) ? 0 : ((n == 1) ? 1 : ((n == 2) ? 2 : ((n == 3) ? 3 : ((n == 6) ? 4 : 5))))
  32          'cy',
  33  
  34          // Actual: n != 1
  35          // Expected: (n == 1) ? 0 : ((n == 2) ? 1 : ((n > 10 && n % 10 == 0) ? 2 : 3))
  36          'he',
  37  
  38          // Actual: n > 1
  39          // Expected: 0
  40          'id',
  41  
  42          // Actual: 0
  43          // Expected: n != 1
  44          'kir', 'uz',
  45  
  46          // Actual: n != 1
  47          // Expected: 0
  48          'tir',
  49  
  50          // Actual: n > 1
  51          // Expected: n >= 2 && (n < 11 || n > 99)
  52          'tzm',
  53      ];
  54  
  55  	public function test_class_exists() {
  56          $this->assertTrue( class_exists( 'GP_Locales' ) );
  57      }
  58  
  59      /**
  60       * Tests syntax of ISO codes for language and country for a locale.
  61       *
  62       * @dataProvider data_provider_locales
  63       *
  64       * @param GP_locale $locale The locale to test.
  65       */
  66  	public function test_locale_iso_codes( $locale ) {
  67          if ( isset( $locale->lang_code_iso_639_1 ) ) {
  68              $this->assertSame( 2, strlen( $locale->lang_code_iso_639_1 ), "ISO 639-1 length check for $locale" );
  69              $this->assertSame( 0, preg_match( '/[^a-z]/', $locale->lang_code_iso_639_1 ), "ISO 639-1 format check for $locale" );
  70          }
  71  
  72          if ( isset( $locale->lang_code_iso_639_2 ) ) {
  73              $this->assertSame( 3, strlen( $locale->lang_code_iso_639_2 ), "ISO 639-2 length check for $locale" );
  74              $this->assertSame( 0, preg_match( '/[^a-z]/', $locale->lang_code_iso_639_2 ), "ISO 639-2 format check for $locale" );
  75          }
  76  
  77          if ( isset( $locale->lang_code_iso_639_3 ) ) {
  78              $this->assertSame( 3, strlen( $locale->lang_code_iso_639_3 ), "ISO 639-3 length check for $locale" );
  79              $this->assertSame( 0, preg_match( '/[^a-z]/', $locale->lang_code_iso_639_3 ), "ISO 639-3 format check for $locale" );
  80          }
  81  
  82          if ( isset( $locale->country_code ) ) {
  83              $this->assertSame( 2, strlen( $locale->country_code ), "ISO 3166 length check for $locale" );
  84              $this->assertSame( 0, preg_match( '/[^a-z]/', $locale->country_code ), "ISO 3166 format check for $locale" );
  85          }
  86      }
  87  
  88      /**
  89       * Tests plural form of a locale.
  90       *
  91       * @dataProvider data_provider_locales
  92       *
  93       * @param GP_locale $locale The locale to test.
  94       */
  95  	public function test_locale_plural( $locale ) {
  96          if ( in_array( $locale->slug, self::SKIP_LOCALES, true ) ) {
  97              $this->markTestSkipped( "{$locale->slug} skipped, requires research" );
  98          }
  99  
 100          $id = '';
 101          if ( isset( $locale->lang_code_iso_639_1, $locale->country_code ) ) {
 102              $id = $locale->lang_code_iso_639_1 . '_' . strtoupper( $locale->country_code );
 103          }
 104  
 105          if ( ! $id && isset( $locale->lang_code_iso_639_1 ) ) {
 106              $id = $locale->lang_code_iso_639_1;
 107          }
 108  
 109          if ( ! $id && isset( $locale->lang_code_iso_639_2 ) ) {
 110              $id = $locale->lang_code_iso_639_2;
 111          }
 112  
 113          if ( ! $id && isset( $locale->lang_code_iso_639_3 ) ) {
 114              $id = $locale->lang_code_iso_639_3;
 115          }
 116  
 117          $language = Gettext\Languages\Language::getById( $id );
 118          if ( $language ) {
 119              $this->assertSame( $language->formula, $locale->plural_expression, $locale->slug );
 120              $this->assertSame( count( $language->categories ), $locale->nplurals, $locale->slug );
 121              return;
 122          }
 123  
 124          // Try again with different ID.
 125          $id = '';
 126          if ( isset( $locale->lang_code_iso_639_1 ) ) {
 127              $id = $locale->lang_code_iso_639_1;
 128          }
 129  
 130          if ( ! $id && isset( $locale->lang_code_iso_639_2 ) ) {
 131              $id = $locale->lang_code_iso_639_2;
 132          }
 133  
 134          if ( ! $id && isset( $locale->lang_code_iso_639_3 ) ) {
 135              $id = $locale->lang_code_iso_639_3;
 136          }
 137  
 138          $language = Gettext\Languages\Language::getById( $id );
 139          if ( $language ) {
 140              $this->assertSame( $language->formula, $locale->plural_expression, $locale->slug );
 141              $this->assertSame( count( $language->categories ), $locale->nplurals, $locale->slug );
 142              return;
 143          }
 144  
 145          // Try again with different ID.
 146          $id = '';
 147          if ( isset( $locale->lang_code_iso_639_2 ) ) {
 148              $id = $locale->lang_code_iso_639_2;
 149          }
 150  
 151          if ( ! $id && isset( $locale->lang_code_iso_639_3 ) ) {
 152              $id = $locale->lang_code_iso_639_3;
 153          }
 154  
 155          $language = Gettext\Languages\Language::getById( $id );
 156          if ( ! $language ) {
 157              $this->markTestSkipped( "{$locale->slug} not found in CLDR" );
 158          }
 159  
 160          $this->assertSame( $language->formula, $locale->plural_expression, $locale->slug );
 161          $this->assertSame( count( $language->categories ), $locale->nplurals, $locale->slug );
 162      }
 163  
 164  	public function data_provider_locales() {
 165          unset( $GLOBALS['gp_locales'] );
 166          $locales = GP_Locales::locales();
 167  
 168          $data = [];
 169  
 170          foreach ( $locales as $locale ) {
 171              $data[] = [ $locale ];
 172          }
 173  
 174          return $data;
 175      }
 176  }


Generated: Tue Dec 1 01:01:56 2020 Cross-referenced by PHPXref 0.7.1