[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

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

   1  <?php
   2  
   3  class GP_Test_Format_JSON 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 = 'json';
  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( 'JSON (.json)', GP::$formats[ $this->format ]->name );
  33      }
  34  
  35  	public function test_format_extension() {
  36          $this->assertSame( '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' => array( 'bar' ),
  61              'bar' => array( '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.json' );
  78          $this->assertSame( 5, 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          $expected = $this->data_example_translations();
  92  
  93          /* @var Translations $actual */
  94          $actual = GP::$formats[ $this->format ]->read_translations_from_file( GP_DIR_TESTDATA . '/translation.json' );
  95  
  96          $this->assertCount( 5, $actual->entries );
  97          $this->assertEquals( $expected, $actual );
  98      }
  99  
 100      /**
 101       * Returns the expected data for the parsed example-untranslated.json file.
 102       */
 103  	public function data_example_originals() {
 104          $translations = new Translations();
 105          $translations->add_entry( new Translation_Entry( array(
 106              'singular' => 'This file is too big. Files must be less than %d KB in size.',
 107          ) ) );
 108          $translations->add_entry( new Translation_Entry( array(
 109              'singular' => '%d Theme Update',
 110          ) ) );
 111          $translations->add_entry( new Translation_Entry( array(
 112              'singular' => 'Medium',
 113              'context'  => 'password strength',
 114          ) ) );
 115          $translations->add_entry( new Translation_Entry( array(
 116              'singular' => 'Category',
 117              'context'  => 'taxonomy singular name',
 118          ) ) );
 119          $translations->add_entry( new Translation_Entry( array(
 120              'singular' => 'Pages',
 121              'context'  => 'post type general name',
 122          ) ) );
 123  
 124          return $translations;
 125      }
 126  
 127      /**
 128       * Returns the expected data for the parsed example-untranslated.json file.
 129       */
 130  	public function data_example_translations() {
 131          $translations = new Translations();
 132          $translations->add_entry( new Translation_Entry( array(
 133              'singular'     => 'This file is too big. Files must be less than %d KB in size.',
 134              'translations' => array(
 135                  'Diese Datei ist zu gross. Dateien müssen kleiner als %d KB sein.',
 136              ),
 137          ) ) );
 138          $translations->add_entry( new Translation_Entry( array(
 139              'singular'     => '%d Theme Update',
 140              'translations' => array(
 141                  '%d Theme-Aktualisierung',
 142                  '%d Theme-Aktualisierungen',
 143              )
 144          ) ) );
 145          $translations->add_entry( new Translation_Entry( array(
 146              'singular'     => 'Medium',
 147              'context'      => 'password strength',
 148              'translations' => array(
 149                  'Medium',
 150              )
 151          ) ) );
 152          $translations->add_entry( new Translation_Entry( array(
 153              'singular'     => 'Category',
 154              'context'      => 'taxonomy singular name',
 155              'translations' => array(
 156                  'Kategorie',
 157              )
 158          ) ) );
 159          $translations->add_entry( new Translation_Entry( array(
 160              'singular'     => 'Pages',
 161              'context'      => 'post type general name',
 162              'translations' => array(
 163                  'Seiten',
 164              )
 165          ) ) );
 166  
 167          return $translations;
 168      }
 169  }


Generated: Sun Dec 22 01:01:04 2024 Cross-referenced by PHPXref 0.7.1