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