summaryrefslogtreecommitdiff
blob: db2058c1283764fffd278d14f73e680c58423d40 (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
<?php
/**
 * Tests for Message Validator and ValidatorResult.
 *
 * @file
 * @author Abijeet Patro
 * @license GPL-2.0-or-later
 */

use MediaWiki\Extension\Translate\Validation\ValidationRunner;

/**
 * @covers \MediaWiki\Extension\Translate\Validation\ValidationRunner
 * @covers \MediaWiki\Extension\Translate\Validation\ValidationResult
 */
class ValidationRunnerTest extends MediaWikiIntegrationTestCase {
	protected function setUp(): void {
		parent::setUp();

		$this->setTemporaryHook(
			'TranslatePostInitGroups',
			[ $this, 'getTestGroups' ]
		);

		$mg = MessageGroups::singleton();
		$mg->setCache( new WANObjectCache( [ 'cache' => wfGetCache( 'hash' ) ] ) );
		$mg->recache();

		MessageIndex::setInstance( new HashMessageIndex() );
		MessageIndex::singleton()->rebuild();

		// Run with empty ignore list by default
		$this->setMwGlobals( 'wgTranslateCheckBlacklist', false );
		ValidationRunner::reloadIgnorePatterns();
	}

	public function getTestGroups( &$list ) {
		$messages = [
			'translated' => 'bunny',
			'untranslated' => 'fanny',
			'testing-key' => 'test this!',
			'regex-key-test' => 'regex test',
			'non-matching-key' => 'non matching key'
		];

		$list['test-group'] = new MockWikiValidationMessageGroup( 'test-group', $messages );
	}

	public function testValidateMessage() {
		$group = MessageGroups::getGroup( 'test-group' );
		$collection = $group->initCollection( 'en-gb' );
		$collection->loadTranslations();

		$msgValidator = $group->getValidator();
		$validationResult = $msgValidator->validateMessage(
			$collection[ 'translated' ],
			'en-gb'
		);

		$this->assertTrue(
			$validationResult->hasErrors(),
			'errors are correctly identified.'
		);
		$this->assertTrue(
			$validationResult->hasWarnings(),
			'warnings are correctly identified.'
		);

		$this->assertCount(
			1,
			$validationResult->getWarnings(),
			'there is 1 warning returned as per the validator.'
		);

		$this->assertCount(
			2,
			$validationResult->getErrors(),
			'there are 2 errors returned as per the validator.'
		);

		$validationResult = $msgValidator->validateMessage(
			$collection[ 'translated' ],
			'en-gb',
			true // ignore warnings
		);

		$this->assertTrue(
			$validationResult->hasErrors(),
			'errors are correctly identified if ignore warnings is set.'
		);
		$this->assertFalse(
			$validationResult->hasWarnings(),
			'warnings are ignored if ignore warnings is set.'
		);
	}

	public function testQuickValidate() {
		$group = MessageGroups::getGroup( 'test-group' );
		$collection = $group->initCollection( 'en-gb' );
		$collection->loadTranslations();

		$msgValidator = $group->getValidator();
		$validationResult = $msgValidator->quickValidate( $collection[ 'translated' ], 'en-gb' );

		$this->assertTrue(
			$validationResult->hasIssues(),
			'either errors or warnings are set.'
		);

		$this->assertFalse(
			$validationResult->hasWarnings() && $validationResult->hasErrors(),
			'either error or warnings are set, but not both.'
		);

		$this->assertCount(
			1,
			$validationResult->getIssues(),
			'there is a single warning or error returned as per the validator.'
		);

		$validationResult = $msgValidator->quickValidate(
			$collection[ 'translated' ],
			'en-gb',
			true // ignore warnings
		);

		$this->assertTrue(
			$validationResult->hasErrors(),
			'errors are identified if ignore warnings is set.'
		);
		$this->assertFalse(
			$validationResult->hasWarnings(),
			'warnings are not identified if ignore warnings is set.'
		);
	}

	public function testDescriptiveMessage() {
		$group = MessageGroups::getGroup( 'test-group' );
		$collection = $group->initCollection( 'en-gb' );
		$collection->loadTranslations();

		$msgValidator = $group->getValidator();
		$validationResult = $msgValidator->validateMessage( $collection[ 'translated' ], 'en-gb' );

		$requestContext = new RequestContext();
		$requestContext->setLanguage( 'en' );

		$this->assertCount(
			count( $validationResult->getErrors() ),
			$validationResult->getDescriptiveErrors( $requestContext ),
			'the number of descriptive errors messages matches the number of errors.'
		);
		$this->assertCount(
			count( $validationResult->getWarnings() ),
			$validationResult->getDescriptiveWarnings( $requestContext ),
			'the number of descriptive warnings messages matches the number of warnings.'
		);

		$this->assertIsString(
			$validationResult->getDescriptiveWarnings( $requestContext )[0],
			'warning messages are of type string.'
		);
		$this->assertIsString(
			$validationResult->getDescriptiveErrors( $requestContext )[0],
			'error messages are of type string'
		);
	}

	public function testIgnoreList() {
		$this->setMwGlobals( [
			'wgTranslateCheckBlacklist' => __DIR__ . '/../data/check-blacklist.php'
		] );

		$group = MessageGroups::getGroup( 'test-group' );
		$collection = $group->initCollection( 'en-gb' );
		$collection->loadTranslations();

		$collectionFr = $group->initCollection( 'fr' );
		$collectionFr->loadTranslations();

		$msgValidator = $group->getValidator();
		$msgValidator::reloadIgnorePatterns();

		$validationResult = $msgValidator->validateMessage( $collection[ 'translated' ], 'en-gb' );
		$this->assertCount(
			1,
			$validationResult->getIssues(),
			'warnings or errors are filtered as per check-blacklist.'
		);

		$validationResult = $msgValidator->validateMessage( $collectionFr[ 'translated' ], 'fr' );
		$this->assertGreaterThan(
			1,
			count( $validationResult->getIssues() ),
			'warnings or errors are filtered as per check-blacklist only for specific language code.'
		);

		$validationResult = $msgValidator->quickValidate( $collection['translated'], 'en-gb' );
		$this->assertCount(
			1,
			$validationResult->getIssues(),
			'warnings or errors are filtered as per check-blacklist.'
		);

		$validationResult = $msgValidator->quickValidate( $collectionFr[ 'translated' ], 'fr' );
		$this->assertCount(
			1,
			$validationResult->getIssues(),
			'warnings or errors are filtered as per check-blacklist only for specific language code.'
		);

		$validationResult = $msgValidator->validateMessage( $collectionFr['regex-key-test'], 'fr' );
		$this->assertCount(
			0,
			$validationResult->getIssues(),
			'warnings or errors are filtered as per check-blacklist for specific message key.'
		);
	}

	public function testKeyMatching() {
		$group = MessageGroups::getGroup( 'test-group' );
		$collection = $group->initCollection( 'en-gb' );
		$collection->loadTranslations();

		$msgValidator = $group->getValidator();
		$validationResult = $msgValidator->validateMessage( $collection['testing-key'], 'en-gb' );
		$this->assertCount(
			0,
			$validationResult->getErrors(),
			'no errors are raised for a non matching key!'
		);

		$validationResult = $msgValidator->validateMessage( $collection['regex-key-test'], 'en-gb' );
		$this->assertCount(
			2,
			$validationResult->getErrors(),
			'errors are raised for a matching key matched via regex!'
		);

		$validationResult = $msgValidator->validateMessage( $collection['translated'], 'en-gb' );
		$this->assertCount(
			2,
			$validationResult->getErrors(),
			'errors are raised for a matching key matched via a direct match!'
		);
	}
}