[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

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

   1  <?php
   2  
   3  class GP_Test_Format_Strings extends GP_UnitTestCase {
   4  
   5      function setUp() {
   6          parent::setUp();
   7          $this->strings = new GP_Format_Strings;
   8          $this->entries = array(
   9              array('Normal String', 'Normal String', 'Just A Normal String', ''),
  10              array('I\'m with a quote', 'I\'m with a quote', 'I\'m with a quote', ''),
  11              array('double "quotes"', 'double "quotes"', 'I have double "quotes"', ''),
  12              array('питка', 'питка', 'баба ми омеси питка', ''),
  13              array('you < me', 'you < me', 'ти < аз', ''),
  14              array('me > you', 'me > you', "аз > ти", ''),
  15              array('me & you are not &amp;', 'me & you are not &amp;', 'аз & ти не сме &amp;', ''),
  16              array('baba', 'baba', 'баба', 'Me, myself & Irene'),
  17              array('multiline', 'multiline', 'multiline', 'This is
  18     a multiline
  19     comment which is
  20     also supposed to work.'),
  21          );
  22      }
  23  
  24  	function test_export() {
  25          $set = $this->factory->translation_set->create_with_project_and_locale();
  26          $project = $set->project;
  27          $locale = $this->factory->locale->create();
  28          $entries_for_export = array();
  29  
  30          foreach( $this->entries as $sample ) {
  31              list( $context, $original, $translation, $comment ) = $sample;
  32  
  33              $entries_for_export[] = (object)array(
  34                  'context' => $context,
  35                  'singular' => $original,
  36                  'translations' => array($translation),
  37                  'extracted_comments' => $comment,
  38              );
  39          }
  40  
  41          $file_contents = file_get_contents( GP_DIR_TESTDATA . '/translation.strings' );
  42          $file_contents = str_replace( '[GP VERSION]', GP_VERSION, $file_contents );
  43          // Remove the UTF-8 Byte Order Mark which is necessary to ensure a UTF-8 detection of the file.
  44          $file_contents = substr( $file_contents, 3 );
  45  
  46          $exported = $this->strings->print_exported_file( $project, $locale, $set, $entries_for_export );
  47  
  48          $this->assertEquals( $file_contents, $exported );
  49      }
  50  
  51  	function test_read_originals() {
  52          $translations = $this->strings->read_originals_from_file( GP_DIR_TESTDATA . '/originals.strings' );
  53          $this->assertEquals( count( $this->entries ), count( $translations->entries ), 'number of read originals is different from the expected' );
  54  
  55          foreach( $this->entries as $sample ) {
  56              list( $context, $original, $translation, $comment ) = $sample;
  57              $translatable_entry = new Translation_Entry( array('singular' => $original, 'context' => $context, 'extracted_comments' => $comment ) );
  58              $entry = $translations->translate_entry( $translatable_entry );
  59              $this->assertEquals( $original, $entry->singular );
  60              $this->assertEquals( $context, $entry->context );
  61              $this->assertEquals( $comment, $entry->extracted_comments );
  62          }
  63      }
  64  
  65  	function test_read_originals_from_UTF16LE() {
  66          $translations = $this->strings->read_originals_from_file( GP_DIR_TESTDATA . '/originals.16le.strings' );
  67          $this->assertEquals( count( $this->entries ), count( $translations->entries ), 'number of read originals is different from the expected' );
  68  
  69          foreach( $this->entries as $sample ) {
  70              list( $context, $original, $translation, $comment ) = $sample;
  71              $translatable_entry = new Translation_Entry( array('singular' => $original, 'context' => $context, 'extracted_comments' => $comment ) );
  72              $entry = $translations->translate_entry( $translatable_entry );
  73              $this->assertEquals( $original, $entry->singular );
  74              $this->assertEquals( $context, $entry->context );
  75              $this->assertEquals( $comment, $entry->extracted_comments );
  76          }
  77      }
  78  
  79  	function test_read_translations() {
  80          $stubbed_originals = array();
  81  
  82          foreach( $this->entries as $sample ) {
  83              list( $context, $original, $translation ) = $sample;
  84              $stubbed_originals[] = new GP_Original( array( 'singular' => $original, 'context' => $context ) );
  85          }
  86  
  87          GP::$original = $this->getMockBuilder( 'GP_Original' )->setMethods( array('by_project_id') )->getMock();
  88          GP::$original->expects( $this->once() )
  89                      ->method( 'by_project_id' )
  90                      ->with( $this->equalTo(2) )
  91                      ->will( $this->returnValue($stubbed_originals) );
  92  
  93          $translations = $this->strings->read_translations_from_file( GP_DIR_TESTDATA . '/translation.strings', (object)array( 'id' => 2 ) );
  94  
  95          foreach( $this->entries as $sample ) {
  96              list( $context, $original, $translation ) = $sample;
  97              $this->assertEquals( $translation, $translations->translate( $original, $context ) );
  98          }
  99      }
 100  
 101  }


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