[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

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

   1  <?php
   2  
   3  class GP_Test_Format_Android extends GP_UnitTestCase {
   4      function setUp() {
   5          parent::setUp();
   6          $this->android = new GP_Format_Android;
   7          $this->entries = array(
   8              array( 'normal_string', 'Normal String', 'Just A Normal String', '' ),
   9              array( 'with_a_quote', 'I\'m with a quote', 'I\'m with a quote', '' ),
  10              array( 'with_newlines', "new\nlines", "I\nhave\nnew\nlines", '' ),
  11              array( 'with_doublequotes', 'double "quotes"', 'I have double "quotes"', '' ),
  12              array( 'with_utf8', 'питка', 'баба ми омеси питка', '' ),
  13              array( 'with_lt', 'you < me', 'ти < аз', '' ),
  14              array( 'with_gt', 'me > you', "аз > ти", '' ),
  15              array( 'with_amps', 'me & you are not &amp;', 'аз & ти не сме &amp;', '' ),
  16              array( 'with_comment', 'baba', 'баба', 'Me, myself & Irene' ),
  17              array( 'with_escaped_unicode', 'No posts saved \u2014 yet!', 'Keine Beiträge gespeichert \u2014 noch!', '' ),
  18          );
  19      }
  20  
  21  	function test_export() {
  22          $set = $this->factory->translation_set->create_with_project_and_locale();
  23          $project = $set->project;
  24          $locale = $this->factory->locale->create();
  25          $entries_for_export = array();
  26  
  27          foreach( $this->entries as $sample ) {
  28              list( $context, $original, $translation ) = $sample;
  29              $entries_for_export[] = (object)array(
  30                  'context' => $context,
  31                  'singular' => $original,
  32                  'translations' => array($translation),
  33              );
  34          }
  35  
  36          $file_contents = file_get_contents( GP_DIR_TESTDATA . '/translation.android.xml' );
  37          $file_contents = str_replace( '[GP VERSION]', GP_VERSION, $file_contents );
  38  
  39          $this->assertEquals( $file_contents, $this->android->print_exported_file( $project, $locale, $set, $entries_for_export ) );
  40      }
  41  
  42  
  43  	function test_read_originals() {
  44          $translations = $this->android->read_originals_from_file( GP_DIR_TESTDATA . '/originals.android.xml' );
  45  
  46          foreach( $this->entries as $sample ) {
  47              list( $context, $original, $translation, $comment ) = $sample;
  48              $translatable_entry = new Translation_Entry( array( 'singular' => $original, 'context' => $context) );
  49              $entry = $translations->translate_entry( $translatable_entry );
  50              $this->assertEquals( $original, $entry->singular );
  51              $this->assertEquals( $context, $entry->context );
  52              $this->assertEquals( $comment, $entry->extracted_comments );
  53          }
  54      }
  55  
  56  	function test_read_original_with_xliff() {
  57          $translations = $this->android->read_originals_from_file( GP_DIR_TESTDATA . '/originals.android-with-xliff.xml' );
  58  
  59          $data = array(
  60              array(
  61                      'context'  => 'with_xliff',
  62                      'original' => 'Please don\'t translate this text',
  63                      'comment'  => 'This string has content that should not be translated, the "this" component of the original, which is identified as the "excluded" attribute by the developer and is not intended to be translated.',
  64              ),
  65              array(
  66                      'context'  => 'with_two_xliff',
  67                      'original' => 'Please don\'t translate this text or even this other text',
  68                      'comment'  => 'This string has content that should not be translated, the "this" component of the original, which is identified as the "first" attribute by the developer and is not intended to be translated. This string has content that should not be translated, the "other" component of the original, which is identified as the "second" attribute by the developer and is not intended to be translated.',
  69              ),
  70              array(
  71                      'context'  => 'with_xliff_with_example',
  72                      'original' => 'Please don\'t translate %s text',
  73                      'comment'  => 'This string has content that should not be translated, the "%s" component of the original, which is identified as the "excluded" attribute by the developer may be replaced at run time with text like this: this',
  74              ),
  75              array(
  76                      'context'  => 'with_xliff_with_example_and_no_id',
  77                      'original' => 'Please don\'t translate %s text',
  78                      'comment'  => 'This string has content that should not be translated, the "%s" component of the original may be replaced at run time with text like this: this',
  79              ),
  80              array(
  81                      'context'  => 'with_xliff_with_no_example_and_no_id',
  82                      'original' => 'Please don\'t translate %s text',
  83                      'comment'  => 'This string has content that should not be translated, the "%s" component is not intended to be translated.',
  84              ),
  85              array(
  86                      'context'  => 'with_xliff_with_single_quote_attributes',
  87                      'original' => 'Please don\'t translate %s text',
  88                      'comment'  => 'This string has content that should not be translated, the "%s" component of the original, which is identified as the "excluded" attribute by the developer may be replaced at run time with text like this: this',
  89              ),
  90              array(
  91                      'context'  => 'with_xliff_with_single_and_double_quote_attributes',
  92                      'original' => 'Please don\'t translate %s text',
  93                      'comment'  => 'This string has content that should not be translated, the "%s" component of the original, which is identified as the "excluded" attribute by the developer may be replaced at run time with text like this: this',
  94              ),
  95              array(
  96                      'context'  => 'with_xliff_with_quotes_inside_attributes',
  97                      'original' => 'Please don\'t translate %s text',
  98                      'comment'  => 'This string has content that should not be translated, the "%s" component of the original, which is identified as the "exclud\'d" attribute by the developer may be replaced at run time with text like this: "this"',
  99              ),
 100          );
 101  
 102          foreach( $data as $set ) {
 103              $translatable_entry = new Translation_Entry( array( 'singular' => $set['original'], 'context' => $set['context'] ) );
 104              $entry = $translations->translate_entry( $translatable_entry );
 105              $this->assertEquals( $set['original'], $entry->singular );
 106              $this->assertEquals( $set['context'], $entry->context );
 107              $this->assertEquals( $set['comment'], $entry->extracted_comments );
 108          }
 109      }
 110  
 111  	function test_read_translations() {
 112          $stubbed_originals = array();
 113          foreach( $this->entries as $sample ) {
 114              list( $context, $original, $translation ) = $sample;
 115              $stubbed_originals[] = new GP_Original( array( 'singular' => $original, 'context' => $context ) );
 116          }
 117          GP::$original = $this->getMockBuilder( 'GP_Original' )->setMethods( array('by_project_id') )->getMock();
 118          GP::$original->expects( $this->once() )
 119                      ->method( 'by_project_id' )
 120                      ->with( $this->equalTo(2) )
 121                      ->will( $this->returnValue($stubbed_originals) );
 122          $translations = $this->android->read_translations_from_file( GP_DIR_TESTDATA . '/translation.android.xml', (object)array( 'id' => 2 ) );
 123          foreach( $this->entries as $sample ) {
 124              list( $context, $original, $translation ) = $sample;
 125              $this->assertEquals( $translation, $translations->translate( $original, $context ) );
 126          }
 127      }
 128  
 129  	function test_escape() {
 130          $test_class = new Testable_GP_Format_Strings_escape;
 131  
 132          $this->assertEquals( "test \'string\'", $test_class->testable_escape( "test 'string'" ) );
 133          $this->assertEquals( "test\\nstring", $test_class->testable_escape( "test\nstring" ) );
 134          $this->assertEquals( '\@test string', $test_class->testable_escape( '@test string' ) );
 135          $this->assertEquals( 'test @string', $test_class->testable_escape( 'test @string' ) );
 136      }
 137  
 138  }
 139  
 140  /**
 141   * Class that makes it possible to test protected functions.
 142   */
 143  class Testable_GP_Format_Strings_escape extends GP_Format_Android {
 144      /**
 145       * Wraps the protected escape function
 146       *
 147       * @param string $string The string to escape.
 148       *
 149       * @return string Returns escaped string.
 150       */
 151  	public function testable_escape( $string ) {
 152          return $this->escape( $string );
 153      }
 154  }


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