[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/tests/phpunit/testcases/tests_formats/ -> test_format_ngx.php (source)

   1  <?php
   2  
   3  class GP_Test_Format_NGX extends GP_UnitTestCase {
   4      /**
   5       * @var GP_Translation_Set
   6       */
   7      protected $translation_set;
   8  
   9      /**
  10       * @var GP_Locale
  11       */
  12      protected $locale;
  13  
  14      /**
  15       * @var string
  16       */
  17      protected $format = 'ngx';
  18  
  19  	public function setUp() {
  20          parent::setUp();
  21  
  22          $this->translation_set = $this->factory->translation_set->create_with_project_and_locale( array(), array( 'name' => 'foo_project' ) );
  23  
  24          $this->locale = new GP_Locale( array(
  25              'slug'              => $this->translation_set->locale,
  26              'nplurals'          => 2,
  27              'plural_expression' => 'n != 1',
  28          ) );
  29      }
  30  
  31  	public function test_format_name() {
  32          $this->assertSame( 'NGX-Translate (.json)', GP::$formats[ $this->format ]->name );
  33      }
  34  
  35  	public function test_format_extension() {
  36          $this->assertSame( 'ngx.json', GP::$formats[ $this->format ]->extension );
  37      }
  38  
  39  	public function test_print_exported_file_can_be_decoded() {
  40          $entries = array(
  41              new Translation_Entry( array( 'singular' => 'foo', 'translations' => array( 'foox' ) ) ),
  42          );
  43  
  44          $json = GP::$formats[ $this->format ]->print_exported_file( $this->translation_set->project, $this->locale, $this->translation_set, $entries );
  45  
  46          $this->assertNotNull( json_decode( $json, true ) );
  47      }
  48  
  49  	public function test_print_exported_file_has_valid_format() {
  50          $entries = array(
  51              new Translation_Entry( array( 'singular' => 'foo', 'translations' => array( 'bar' ) ) ),
  52              new Translation_Entry( array( 'singular' => 'bar', 'translations' => array( 'baz' ) ) ),
  53          );
  54  
  55          $json = GP::$formats[ $this->format ]->print_exported_file( $this->translation_set->project, $this->locale, $this->translation_set, $entries );
  56  
  57          $actual = json_decode( $json, true );
  58  
  59          $this->assertEquals( array(
  60              'foo' => 'bar',
  61              'bar' => 'baz',
  62          ), $actual );
  63      }
  64  
  65      public function test_read_originals_from_file_non_existent_file() {
  66          $this->assertFalse( GP::$formats[ $this->format ]->read_originals_from_file( GP_DIR_TESTDATA . '/foo.json' ) );
  67      }
  68  
  69  	public function test_read_originals_from_file_invalid_file() {
  70          $this->assertFalse( GP::$formats[ $this->format ]->read_originals_from_file( GP_DIR_TESTDATA . '/invalid.json' ) );
  71      }
  72  
  73  	public function test_read_originals_from_file() {
  74          $expected = $this->data_example_originals();
  75  
  76          /* @var Translations $actual */
  77          $actual = GP::$formats[ $this->format ]->read_originals_from_file( GP_DIR_TESTDATA . '/originals-ngx.json' );
  78          $this->assertSame( 7, count( $actual->entries ) );
  79          $this->assertEquals( $expected, $actual );
  80      }
  81  
  82      public function test_read_translations_from_file_non_existent_file() {
  83          $this->assertFalse( GP::$formats[ $this->format ]->read_translations_from_file( GP_DIR_TESTDATA . '/foo.json' ) );
  84      }
  85  
  86  	public function test_read_translations_from_file_invalid_file() {
  87          $this->assertFalse( GP::$formats[ $this->format ]->read_translations_from_file( GP_DIR_TESTDATA . '/invalid.json' ) );
  88      }
  89  
  90  	public function test_read_translations_from_file() {
  91          $originals = $this->data_example_originals();
  92          $expected = $this->data_example_translations();
  93          $stubbed_originals = array();
  94          $results = array();
  95          $i = 0;
  96  
  97          // We need a combined set of originals/translations to use when we validate the file read,
  98          // so loop through our entries and create on here.
  99          foreach ( $originals->entries as $orig ) {
 100              $results[ $i ]['original'] = $orig->singular;
 101              $results[ $i ]['context'] = $orig->context;
 102          }
 103  
 104          foreach ( $expected->entries as $exp ) {
 105              $results[ $i ]['translation'] = $exp->singular;
 106          }
 107  
 108          // We need to convert the example originals to an array of GP_Originals before using them.
 109          foreach ( $originals->entries as $entry ) {
 110              $stubbed_originals[] = new GP_Original( array( 'singular' => $entry->singular, 'context' => $entry->context ) );
 111          }
 112  
 113          // Create a mock project to use during the file read.
 114          GP::$original = $this->getMockBuilder( 'GP_Original' )->setMethods( array('by_project_id') )->getMock();
 115          GP::$original->expects( $this->once() )
 116                      ->method( 'by_project_id' )
 117                      ->with( $this->equalTo( 2 ) )
 118                      ->will( $this->returnValue( $stubbed_originals ) );
 119  
 120          // @var Translations $actual.
 121          $actual = GP::$formats[ $this->format ]->read_translations_from_file( GP_DIR_TESTDATA . '/translation-ngx.json', (object)array( 'id' => 2 ) );
 122  
 123          $this->assertSame( 7, count( $actual->entries ) );
 124  
 125          foreach ( $results as $result ) {
 126              $this->assertEquals( $result['translation'], $actual->translate( $result['original'], $result['context'] ) );
 127          }
 128      }
 129  
 130      /**
 131       * Returns the expected data for the parsed example-untranslated.json file.
 132       */
 133  	public function data_example_originals() {
 134          $translations = new Translations();
 135          $translations->add_entry( new Translation_Entry( array(
 136              'singular' => 'This file is too big. Files must be less than %d KB in size.',
 137              'context'  => 'ORIGINAL1',
 138          ) ) );
 139          $translations->add_entry( new Translation_Entry( array(
 140              'singular' => '%d Theme Update',
 141              'context'  => 'ORIGINAL2',
 142          ) ) );
 143          $translations->add_entry( new Translation_Entry( array(
 144              'singular' => 'password strength',
 145              'context'  => 'ORIGINAL3',
 146          ) ) );
 147          $translations->add_entry( new Translation_Entry( array(
 148              'singular' => 'taxonomy singular name',
 149              'context'  => 'ORIGINAL4',
 150          ) ) );
 151          $translations->add_entry( new Translation_Entry( array(
 152              'singular' => 'post type general name',
 153              'context'  => 'ORIGINAL5',
 154          ) ) );
 155          $translations->add_entry( new Translation_Entry( array(
 156              'singular' => 'on',
 157              'context'  => 'ORIGINAL6[1]',
 158          ) ) );
 159          $translations->add_entry( new Translation_Entry( array(
 160              'singular' => 'off',
 161              'context'  => 'ORIGINAL6[2]',
 162          ) ) );
 163          return $translations;
 164      }
 165  
 166      /**
 167       * Returns the expected data for the parsed example-untranslated.json file.
 168       */
 169  	public function data_example_translations() {
 170          $translations = new Translations();
 171          $translations->add_entry( new Translation_Entry( array(
 172              'singular' => 'Ce fichier est trop grang. Les fichiers doivent avoir une taille plus petite que %d KB.',
 173              'context'  => 'ORIGINAL1',
 174          ) ) );
 175          $translations->add_entry( new Translation_Entry( array(
 176              'singular' => '%d Mise \u00e0 jour de th\u00e8me',
 177              'context'  => 'ORIGINAL2',
 178          ) ) );
 179          $translations->add_entry( new Translation_Entry( array(
 180              'singular' => 'Force du mot de passe',
 181              'context'  => 'ORIGINAL3',
 182          ) ) );
 183          $translations->add_entry( new Translation_Entry( array(
 184              'singular' => 'Nom de la taxonomie au singulier',
 185              'context'  => 'ORIGINAL4',
 186          ) ) );
 187          $translations->add_entry( new Translation_Entry( array(
 188              'singular' => 'Nom générique pour les posts',
 189              'context'  => 'ORIGINAL5',
 190          ) ) );
 191          $translations->add_entry( new Translation_Entry( array(
 192              'singular' => 'actif',
 193              'context'  => 'ORIGINAL6[1]',
 194          ) ) );
 195          $translations->add_entry( new Translation_Entry( array(
 196              'singular' => 'inactif',
 197              'context'  => 'ORIGINAL6[2]',
 198          ) ) );
 199          return $translations;
 200      }
 201  }


Generated: Tue Mar 19 01:01:21 2024 Cross-referenced by PHPXref 0.7.1