summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/tests/phpunit/ffs/GettextFFSTest.php
blob: e471ceb9af2f7f088493917cb16201a5a37193ca (plain)
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
<?php
/**
 * Tests for Gettext message file format.
 *
 * @file
 * @author Niklas Laxström
 * @copyright Copyright © 2012-2013, Niklas Laxström
 * @license GPL-2.0-or-later
 */

/**
 * @see GettextFFS
 */
class GettextFFSTest extends MediaWikiTestCase {
	protected $groupConfiguration;

	public function setUp() {
		parent::setUp();
		$this->groupConfiguration = [
			'BASIC' => [
				'class' => 'FileBasedMessageGroup',
				'id' => 'test-id',
				'label' => 'Test Label',
				'namespace' => 'NS_MEDIAWIKI',
				'description' => 'Test description',
			],
			'FILES' => [
				'class' => 'GettextFFS',
				'sourcePattern' => __DIR__ . '/../data/gettext.po',
			],
		];
	}

	/**
	 * @dataProvider provideMangling
	 */
	public function testMangling( $expected, $item, $algo ) {
		$this->assertEquals( $expected, GettextFFS::generateKeyFromItem( $item, $algo ) );
	}

	public static function provideMangling() {
		return [
			[
				'3f9999051ce0bc6e98f43224fe6ee1c220e34e49-Hello!_world_loooooooooooooooo',
				[ 'id' => 'Hello! world loooooooooooooooooooooooooooooooooooooooooong', 'ctxt' => 'baa' ],
				'legacy'
			],
			[
				'3f9999-Hello!_world_loooooooooooooooo',
				[ 'id' => 'Hello! world loooooooooooooooooooooooooooooooooooooooooong', 'ctxt' => 'baa' ],
				'simple'
			],

			[
				'1437e478b59e220640bf530f7e3bac93950eb8ae-"¤_=FJQ"_¤r_£_ab',
				[ 'id' => '"¤#=FJQ"<>¤r £}[]}%ab', 'ctxt' => false ],
				'legacy'
			],
			[
				'1437e4-"¤#=FJQ"<>¤r_£}[]}%ab',
				[ 'id' => '"¤#=FJQ"<>¤r £}[]}%ab', 'ctxt' => false ],
				'simple'
			],

		];
	}

	public function testHashing() {
		$item1 = [
			'id' => 'a',
			'str' => 'b',
			'ctxt' => false,
		];

		$item2 = [
			'id' => 'a',
			'str' => 'b',
			'ctxt' => '',
		];

		$this->assertNotEquals(
			GettextFFS::generateKeyFromItem( $item1, 'legacy' ),
			GettextFFS::generateKeyFromItem( $item2, 'legacy' ),
			'Empty msgctxt is different from no msgctxt'
		);

		$this->assertNotEquals(
			GettextFFS::generateKeyFromItem( $item1, 'simple' ),
			GettextFFS::generateKeyFromItem( $item2, 'simple' ),
			'Empty msgctxt is different from no msgctxt'
		);

		$this->assertEquals(
			sha1( $item1['id'] ) . '-' . $item1['id'],
			GettextFFS::generateKeyFromItem( $item1, 'legacy' )
		);

		$this->assertEquals(
			substr( sha1( $item1['id'] ), 0, 6 ) . '-' . $item1['id'],
			GettextFFS::generateKeyFromItem( $item1, 'simple' )
		);
	}

	public function testMsgctxtExport() {
		/** @var FileBasedMessageGroup $group */
		$group = MessageGroupBase::factory( $this->groupConfiguration );
		$ffs = new GettextFFS( $group );

		$object = new ReflectionObject( $ffs );
		$method = $object->getMethod( 'formatMessageBlock' );
		$method->setAccessible( true );

		$key = 'key';
		$m = new FatMessage( 'key', 'definition' );
		$m->setTranslation( 'translation' );
		$trans = [];
		$pot = [];
		$pluralCount = 0;

		$results = <<<GETTEXT
#
msgid "definition"
msgstr "translation"

#
msgctxt ""
msgid "definition"
msgstr "translation"

#
msgctxt "context"
msgid "definition"
msgstr "translation"
GETTEXT;

		$results = preg_split( '/\n\n/', $results );

		// Case 1: no context
		$this->assertEquals(
			$results[0],
			trim( $method->invoke( $ffs, $key, $m, $trans, $pot, $pluralCount ) )
		);

		// Case 2: empty context
		$pot['ctxt'] = '';
		$this->assertEquals(
			$results[1],
			trim( $method->invoke( $ffs, $key, $m, $trans, $pot, $pluralCount ) )
		);

		// Case 3: context
		$pot['ctxt'] = 'context';
		$this->assertEquals(
			$results[2],
			trim( $method->invoke( $ffs, $key, $m, $trans, $pot, $pluralCount ) )
		);
	}

	/**
	 * @dataProvider provideShouldOverwrite
	 */
	public function testShouldOverwrite( $a, $b, $expected, $comment ) {
		$group = MessageGroupBase::factory( $this->groupConfiguration );
		$ffs = new GettextFFS( $group );
		$actual = $ffs->shouldOverwrite( $a, $b );
		$this->assertEquals( $expected, $actual, $comment );
	}

	public function provideShouldOverwrite() {
		$cases = [];

		$cases[] = [
<<<GETTEXT
#
msgid ""
msgstr ""
""
"PO-Revision-Date: 2017-02-09 07:24:07+0000\\n"
"X-POT-Import-Date: 2016-08-11 04:53:15+0000\\n"
"Content-Type: text/plain; charset=UTF-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
"Language: azb\\n"
"X-Generator: MediaWiki 1.29.0-alpha; Translate 2017-01-24\\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\\n"

#: frontend/templates/index.html:38
msgid "About the map"
msgstr ""
GETTEXT
			,
<<<GETTEXT
#
msgid ""
msgstr ""
""
"PO-Revision-Date: 2017-02-06 07:07:03+0000\\n"
"X-POT-Import-Date: 2016-08-11 04:53:15+0000\\n"
"Content-Type: text/plain; charset=UTF-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
"Language: azb\\n"
"X-Generator: MediaWiki 1.29.0-alpha; Translate 2017-01-24\\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\\n"

#: frontend/templates/index.html:38
msgid "About the map"
msgstr ""
GETTEXT
			,
			false,
			"Only date has changed"
		];

		$cases[] = [
<<<GETTEXT
#
msgid ""
msgstr ""
""
"PO-Revision-Date: 2017-02-09 07:24:07+0000\\n"
"X-POT-Import-Date: 2016-08-11 04:53:15+0000\\n"
"Content-Type: text/plain; charset=UTF-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
"Language: azb\\n"
"X-Generator: MediaWiki 1.29.0-alpha; Translate 2017-01-24\\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\\n"

#: frontend/templates/index.html:38
msgid "About the map"
msgstr ""
GETTEXT
			,
<<<GETTEXT
#
msgid ""
msgstr ""
""
"PO-Revision-Date: 2017-02-06 07:07:03+0000\\n"
"X-POT-Import-Date: 2016-08-11 04:53:15+0000\\n"
"Content-Type: text/plain; charset=UTF-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
"Language: fi\\n"
"X-Generator: MediaWiki 1.29.0-alpha; Translate 2017-01-24\\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\\n"

#: frontend/templates/index.html:38
msgid "About the map"
msgstr "Tietoja kartasta"
GETTEXT
			,
			true,
			"Content has changed"
		];

		return $cases;
	}
}