[ Index ] |
PHP Cross Reference of GlotPress |
[Summary view] [Print] [Text view]
1 <?php 2 3 class GP_Test_Format_PO extends GP_UnitTestCase { 4 /** 5 * @var GP_Format_PO 6 */ 7 protected $format; 8 9 /** 10 * @var array 11 */ 12 protected $entries; 13 14 /** 15 * @var string 16 */ 17 protected $translation_file; 18 19 /** 20 * @var string 21 */ 22 protected $originals_file; 23 24 /** 25 * @var bool 26 */ 27 protected $has_comments = true; 28 29 public function setUp() { 30 parent::setUp(); 31 32 $this->translation_file = GP_DIR_TESTDATA . '/translation.po'; 33 $this->originals_file = GP_DIR_TESTDATA . '/originals.po'; 34 35 $this->format = new Testable_GP_Format_PO; 36 37 $this->entries = array( 38 array( 'normal_string', 'Normal String', 'Just A Normal String', '' ), 39 array( 'with_a_quote', 'I\'m with a quote', 'I\'m with a quote', '' ), 40 array( 'with_newlines', "new\nlines", "I\nhave\nnew\nlines", '' ), 41 array( 'with_doublequotes', 'double "quotes"', 'I have double "quotes"', '' ), 42 array( 'with_utf8', 'питка', 'баба ми омеси питка', '' ), 43 array( 'with_lt', 'you < me', 'ти < аз', '' ), 44 array( 'with_gt', 'me > you', "аз > ти", '' ), 45 array( 'with_amps', 'me & you are not &', 'аз & ти не сме &', '' ), 46 array( 'with_comment', 'baba', 'баба', 'Me, myself & Irene' ), 47 ); 48 } 49 50 private function get_entries_for_export( $translation_set ) { 51 $entries_for_export = array(); 52 53 foreach ( $this->entries as $sample ) { 54 list( $context, $original, $translation, $comment ) = $sample; 55 56 $translation_entry = new GP_Translation; 57 $translation_entry->context = $context; 58 $translation_entry->singular = $original; 59 $translation_entry->translations = array( $translation ); 60 $translation_entry->status = 'current'; 61 $translation_entry->translation_set_id = $translation_set->id; 62 if ( true === $this->has_comments ) { 63 $translation_entry->extracted_comments = $comment; 64 } 65 66 $entries_for_export[] = new Translation_Entry( (array) $translation_entry ); 67 } 68 69 return $entries_for_export; 70 } 71 72 public function test_export() { 73 $set = $this->factory->translation_set->create_with_project_and_locale(); 74 $project = $set->project; 75 $locale = $this->factory->locale->create(); 76 77 $entries_for_export = $this->get_entries_for_export( $set ); 78 79 $file_contents = file_get_contents( $this->translation_file ); 80 81 $this->assertEquals( $file_contents, $this->format->print_exported_file( $project, $locale, $set, $entries_for_export ) ); 82 } 83 84 /** 85 * @ticket GH-450 86 */ 87 public function test_export_includes_project_id_version_header() { 88 $parent_project_one = $this->factory->project->create(); 89 $parent_project_two = $this->factory->project->create( array( 'parent_project_id' => $parent_project_one->id ) ); 90 $set = $this->factory->translation_set->create_with_project_and_locale( array(), array( 'parent_project_id' => $parent_project_two->id ) ); 91 $project = $set->project; 92 $locale = $this->factory->locale->create(); 93 94 $entries_for_export = $this->get_entries_for_export( $set ); 95 96 $file = $this->format->print_exported_file( $project, $locale, $set, $entries_for_export ); 97 $expected = sprintf( 98 '"Project-Id-Version: %s - %s - %s\n"', 99 $parent_project_one->name, 100 $parent_project_two->name, 101 $project->name 102 ); 103 104 $this->assertContains( $expected, $file ); 105 } 106 107 public function test_read_originals() { 108 $translations = $this->format->read_originals_from_file( $this->originals_file ); 109 110 foreach ( $this->entries as $sample ) { 111 list( $context, $original, $translation, $comment ) = $sample; 112 113 $translatable_entry = new Translation_Entry( array( 'singular' => $original, 'context' => $context ) ); 114 $entry = $translations->translate_entry( $translatable_entry ); 115 116 $this->assertEquals( $original, $entry->singular ); 117 $this->assertEquals( $context, $entry->context ); 118 if ( true === $this->has_comments ) { 119 $this->assertEquals( $comment, $entry->extracted_comments ); 120 } 121 } 122 } 123 124 public function test_read_translations() { 125 $translations = $this->format->read_translations_from_file( $this->translation_file, (object)array( 'id' => 1 ) ); 126 127 foreach ( $this->entries as $sample ) { 128 list( $context, $original, $translation, $comment ) = $sample; 129 130 $this->assertEquals( $translation, $translations->translate( $original, $context ) ); 131 } 132 } 133 134 public function test_get_language_code() { 135 $po_format = new Testable_GP_Format_PO(); 136 137 // Create a locale that has only a 639_1 language code. 138 $as = new GP_Locale(); 139 $as->english_name = 'Assamese'; 140 $as->lang_code_iso_639_1 = 'as'; 141 142 $this->assertEquals( 'as', $po_format->get_language_code( $as ) ); 143 144 // Add the 639_2 language code. 145 $as->lang_code_iso_639_2 = 'asn'; 146 $this->assertEquals( 'as', $po_format->get_language_code( $as ) ); 147 148 // Add the 639_3 language code. 149 $as->lang_code_iso_639_3 = 'asm'; 150 $this->assertEquals( 'as',$po_format->get_language_code( $as ) ); 151 152 // Add the country language code, which is the same as the language code. 153 $as->country_code = 'as'; 154 $this->assertEquals( 'as', $po_format->get_language_code( $as ) ); 155 156 // Change the country code to be different than the language code. 157 $as->country_code = 'in'; 158 $this->assertEquals( 'as_IN', $po_format->get_language_code( $as ) ); 159 160 // Remove the country code for the next tests. 161 $as->country_code = null; 162 163 // Remove the 639_1 language code. 164 $as->lang_code_iso_639_1 = ''; 165 $this->assertEquals( 'asn', $po_format->get_language_code( $as ) ); 166 167 // Remove the 639_2 language code. 168 $as->lang_code_iso_639_2 = ''; 169 $this->assertEquals( 'asm', $po_format->get_language_code( $as ) ); 170 171 // Setup the locale to have the incorrect case in the locale information for country and language. 172 $as->lang_code_iso_639_1 = 'AS'; 173 $as->country_code = 'IN'; 174 $this->assertEquals( 'as_IN', $po_format->get_language_code( $as ) ); 175 } 176 } 177 178 class GP_Test_Format_MO extends GP_Test_Format_PO { 179 180 /** 181 * @var GP_Format_MO 182 */ 183 protected $format; 184 185 public function setUp() { 186 parent::setUp(); 187 188 $this->translation_file = GP_DIR_TESTDATA . '/translation.mo'; 189 $this->originals_file = GP_DIR_TESTDATA . '/originals.mo'; 190 $this->has_comments = false; 191 192 $this->format = new Testable_GP_Format_MO; 193 } 194 195 public function test_export_includes_project_id_version_header() { 196 // MO files do no have header info, so "skip" this test without reporting that it has been skipped by phpunit. 197 $this->assertEquals( 'Testable_GP_Format_MO', get_class( $this->format ) ); 198 } 199 } 200 201 /** 202 * Class used test private/protected and/or override methods. 203 * 204 * @method string get_language_code( GP_Locale $locale ) 205 */ 206 class Testable_GP_Format_PO extends GP_Format_PO { 207 208 /** 209 * List of private/protected methods. 210 * 211 * @var array 212 */ 213 private $non_accessible_methods = array( 214 'get_language_code', 215 ); 216 217 /** 218 * Make private/protected methods readable for tests. 219 * 220 * @param string $name Method to call. 221 * @param array $arguments Arguments to pass when calling. 222 * @return mixed|bool Return value of the callback, false otherwise. 223 */ 224 public function __call( $name, $arguments ) { 225 if ( in_array( $name, $this->non_accessible_methods, true ) ) { 226 return $this->$name( ...$arguments ); 227 } 228 return false; 229 } 230 231 /** 232 * Overrides the value of the 'X-Generator' header field. 233 * 234 * @param GP_Format $format The format. 235 * @param string $header The header field name. 236 * @param string $text The header field value. 237 */ 238 public function set_header( $format, $header, $text ) { 239 if ( 'X-Generator' === $header ) { 240 $text = 'GlotPress/[GP VERSION]'; 241 } 242 parent::set_header( $format, $header, $text ); 243 } 244 } 245 246 /** 247 * Class used test private/protected and/or override methods. 248 */ 249 class Testable_GP_Format_MO extends GP_Format_MO { 250 251 /** 252 * Overrides the value of the 'X-Generator' header field. 253 * 254 * @param GP_Format $format The format. 255 * @param string $header The header field name. 256 * @param string $text The header field value. 257 */ 258 public function set_header( $format, $header, $text ) { 259 if ( 'X-Generator' === $header ) { 260 $text = 'GlotPress/[GP VERSION]'; 261 } 262 parent::set_header( $format, $header, $text ); 263 } 264 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Dec 22 01:01:04 2024 | Cross-referenced by PHPXref 0.7.1 |