[ 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->assertSame( 5, count( $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' => 'Diese Datei ist zu gross. Dateien müssen kleiner als %d KB sein.',
 135          ) ) );
 136          $translations->add_entry( new Translation_Entry( array(
 137              'singular'     => '%d Theme Update',
 138              'translations' => '%d Theme-Aktualisierung',
 139              'plural'       => '%d Theme-Aktualisierungen',
 140          ) ) );
 141          $translations->add_entry( new Translation_Entry( array(
 142              'singular'     => 'Medium',
 143              'context'      => 'password strength',
 144              'translations' => 'Medium',
 145          ) ) );
 146          $translations->add_entry( new Translation_Entry( array(
 147              'singular'     => 'Category',
 148              'context'      => 'taxonomy singular name',
 149              'translations' => 'Kategorie',
 150          ) ) );
 151          $translations->add_entry( new Translation_Entry( array(
 152              'singular'     => 'Pages',
 153              'context'      => 'post type general name',
 154              'translations' => 'Seiten',
 155          ) ) );
 156  
 157          return $translations;
 158      }
 159  }


Generated: Mon Nov 18 01:01:56 2019 Cross-referenced by PHPXref 0.7.1