File: RecordLintJobTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (462 lines) | stat: -rw-r--r-- 16,521 bytes parent folder | download
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
<?php
/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 */

namespace MediaWiki\Linter\Test;

use MediaWiki\Linter\LintError;
use MediaWiki\Linter\RecordLintJob;
use MediaWiki\Page\PageReference;
use MediaWiki\Title\Title;
use MediaWikiIntegrationTestCase;
use Wikimedia\Rdbms\SelectQueryBuilder;
use Wikimedia\Rdbms\UpdateQueryBuilder;

/**
 * @group Database
 * @covers \MediaWiki\Linter\RecordLintJob
 */
class RecordLintJobTest extends MediaWikiIntegrationTestCase {

	private function getDatabase() {
		return $this->getServiceContainer()->get( 'Linter.Database' );
	}

	private function newRecordLintJob( PageReference $page, array $params ) {
		$services = $this->getServiceContainer();
		return new RecordLintJob(
			$page,
			$params,
			$services->get( 'Linter.TotalsLookup' ),
			$this->getDatabase(),
			$services->get( 'Linter.CategoryManager' )
		);
	}

	/**
	 * @param string $titleText
	 * @param int|null $ns
	 * @return array
	 */
	private function createTitleAndPage( string $titleText, ?int $ns = 0 ) {
		$title = Title::newFromText( $titleText, $ns );
		$page = $this->getExistingTestPage( $title );

		return [
			'title' => $title,
			'pageID' => $page->getRevisionRecord()->getPageId(),
			'revID' => $page->getRevisionRecord()->getID()
		];
	}

	/**
	 * Get just the lint error linter_tag field value for a page
	 *
	 * @param int $pageId
	 * @return mixed
	 */
	private function getTagForPage( int $pageId ) {
		$queryPageTag = new SelectQueryBuilder( $this->db );
		$queryPageTag
			->select( 'linter_tag' )
			->table( 'linter' )
			->where( [ 'linter_page' => $pageId ] )
			->caller( __METHOD__ );
		return $queryPageTag->fetchField();
	}

	/**
	 * Get just the lint error linter_template field value for a page
	 *
	 * @param int $pageId
	 * @return mixed
	 */
	private function getTemplateForPage( int $pageId ) {
		$queryPageTemplate = new SelectQueryBuilder( $this->db );
		$queryPageTemplate
			->select( 'linter_template' )
			->table( 'linter' )
			->where( [ 'linter_page' => $pageId ] )
			->caller( __METHOD__ );
		return $queryPageTemplate->fetchField();
	}

	/**
	 * Get just the linter_namespace field value from the linter table for a page
	 *
	 * @param int $pageId
	 * @return mixed
	 */
	private function getNamespaceForPage( int $pageId ) {
		$queryLinterPageNamespace = new SelectQueryBuilder( $this->db );
		$queryLinterPageNamespace
			->select( 'linter_namespace' )
			->table( 'linter' )
			->where( [ 'linter_page' => $pageId ] )
			->caller( __METHOD__ );
		return $queryLinterPageNamespace->fetchField();
	}

	/**
	 * Set just the linter_namespace field value from the linter table for a page
	 *
	 * @param int $pageId
	 */
	private function setNamespaceForPageToNull( int $pageId ) {
		$queryLinterPageNamespace = new UpdateQueryBuilder( $this->db );
		$queryLinterPageNamespace
			->update( 'linter' )
			->set( [ 'linter_namespace' => null ] )
			->where( [ 'linter_page' => $pageId ] )
			->caller( __METHOD__ )
			->execute();
	}

	public function testRun() {
		$error = [
			'type' => 'fostered',
			'location' => [ 0, 10 ],
			'params' => [],
			'dbid' => null,
		];
		$titleAndPage = $this->createTitleAndPage( 'TestPage' );
		$job = $this->newRecordLintJob( $titleAndPage[ 'title' ], [
			'errors' => [ $error ],
			'revision' => $titleAndPage[ 'revID' ]
		] );
		$this->assertTrue( $job->run() );
		$db = $this->getDatabase();
		$errorsFromDb = array_values( $db->getForPage( $titleAndPage[ 'pageID' ] ) );
		$this->assertCount( 1, $errorsFromDb );
		$this->assertInstanceOf( LintError::class, $errorsFromDb[ 0 ] );
		$this->assertEquals( $error[ 'type' ], $errorsFromDb[ 0 ]->category );
		$this->assertEquals( $error[ 'location' ], $errorsFromDb[ 0 ]->location );
		$this->assertEquals( $error[ 'params' ], $errorsFromDb[ 0 ]->params );
	}

	public function testWriteTagAndTemplate() {
		$error = [
			'type' => 'obsolete-tag',
			'location' => [ 0, 10 ],
			'params' => [
				"name" => "center",
				"templateInfo" => [ "name" => "Template:Echo" ]
			],
			'dbid' => null,
		];
		$titleAndPage = $this->createTitleAndPage( 'TestPage2' );
		$job = $this->newRecordLintJob( $titleAndPage[ 'title' ], [
			'errors' => [ $error ],
			'revision' => $titleAndPage[ 'revID' ]
		] );
		$this->assertTrue( $job->run() );
		$pageId = $titleAndPage[ 'pageID' ];
		$db = $this->getDatabase();
		$errorsFromDb = array_values( $db->getForPage( $pageId ) );
		$this->assertCount( 1, $errorsFromDb );
		$this->assertInstanceOf( LintError::class, $errorsFromDb[0] );
		$this->assertEquals( $error[ 'type' ], $errorsFromDb[0]->category );
		$this->assertEquals( $error[ 'location' ], $errorsFromDb[0]->location );
		$this->assertEquals( $error[ 'params' ], $errorsFromDb[0]->params );
		$tag = $this->getTagForPage( $pageId );
		$this->assertEquals( $error[ 'params' ][ 'name' ], $tag );
		$template = $this->getTemplateForPage( $pageId );
		$this->assertEquals( $error[ 'params' ][ 'templateInfo' ][ 'name' ], $template );
	}

	public function testWriteTagAndTemplateLengthExceeded() {
		// Verify special case test for write code encountering params with tag and template string lengths exceeded
		$tagWithMoreThan30Characters = "center tag exceeding 30 characters";
		$tagTruncated = "center tag exceeding 30 charac";
		$templateWithMoreThan250Characters = str_repeat( "Template:Echo longer than 250 characters ", 8 );
		$templateTruncated = "Template:Echo longer than 250 characters Template:Echo longer than 250 characters " .
			"Template:Echo longer than 250 characters Template:Echo longer than 250 characters " .
			"Template:Echo longer than 250 characters Template:Echo longer than 250 characters Temp";

		$error = [
			'type' => 'obsolete-tag',
			'location' => [ 0, 10 ],
			'params' => [
				"name" => $tagWithMoreThan30Characters,
				"templateInfo" => [ "name" => $templateWithMoreThan250Characters ]
			],
			'dbid' => null,
		];
		$titleAndPage = $this->createTitleAndPage( 'TestPage2' );
		$job = $this->newRecordLintJob( $titleAndPage[ 'title' ], [
			'errors' => [ $error ],
			'revision' => $titleAndPage[ 'revID' ]
		] );
		$this->assertTrue( $job->run() );
		$pageId = $titleAndPage[ 'pageID' ];
		$db = $this->getDatabase();
		$errorsFromDb = array_values( $db->getForPage( $pageId ) );
		$this->assertCount( 1, $errorsFromDb );
		$this->assertInstanceOf( LintError::class, $errorsFromDb[0] );
		$this->assertEquals( $error[ 'type' ], $errorsFromDb[0]->category );
		$this->assertEquals( $error[ 'location' ], $errorsFromDb[0]->location );
		$this->assertEquals( $error[ 'params' ], $errorsFromDb[0]->params );
		$tag = $this->getTagForPage( $pageId );
		$this->assertEquals( $tagTruncated, $tag );
		$template = $this->getTemplateForPage( $pageId );
		$this->assertEquals( $templateTruncated, $template );
	}

	/**
	 * @param string $titleText
	 * @param int $namespace
	 * @return array
	 */
	private function createTitleAndPageAndRunJob( string $titleText, int $namespace ): array {
		$titleAndPage = $this->createTitleAndPage( $titleText, $namespace );
		$error = [
			'type' => 'fostered',
			'location' => [ 0, 10 ],
			'params' => [],
			'dbid' => null,
		];
		$job = $this->newRecordLintJob( $titleAndPage[ 'title' ], [
			'errors' => [ $error ],
			'revision' => $titleAndPage[ 'revID' ]
		] );
		$this->assertTrue( $job->run() );
		return $titleAndPage;
	}

	/**
	 * @param array $namespaceIds
	 * @param array $setToID
	 * @return array
	 */
	private function createPagesWithNamespace( array $namespaceIds, array $setToID ): array {
		$titleAndPages = [];
		foreach ( $namespaceIds as $index => $namespaceId ) {
			$titleAndPage = $this->createTitleAndPageAndRunJob(
				'TestPageNamespace' . $index,
				intval( $namespaceId ) );
			$titleAndPages[] = $titleAndPage;
			// To test the migration code, set some namespaces to null to simulate having a mix of valid and null values
			if ( !$setToID[ $index ] ) {
				$this->setNamespaceForPageToNull( $titleAndPage['pageID'] );
			}
		}
		return $titleAndPages;
	}

	/**
	 * @param array $pages
	 * @param array $namespaceIds
	 * @return void
	 */
	private function checkPagesNamespace( array $pages, array $namespaceIds ) {
		foreach ( $pages as $index => $page ) {
			$pageId = $page[ 'pageID' ];
			$namespace = $this->getNamespaceForPage( $pageId );
			$namespaceId = $namespaceIds[ $index ];
			$this->assertSame( "$namespaceId", $namespace );
		}
	}

	public function testMigrateNamespace() {
		// Create groups of records that do not need migrating to ensure batching works properly
		$namespaceIds = [ '0', '1', '2', '3', '4', '5', '4', '3', '2', '1', '0', '1', '2' ];
		$setToID = [ false, true, true, true, false, false, true, true, false, false, false, true, false ];

		$titleAndPages = $this->createPagesWithNamespace( $namespaceIds, $setToID );

		// Verify the create page function did not populate the linter_namespace field for TestPageNamespace0
		$pageId = $titleAndPages[ 0 ][ 'pageID' ];
		$namespace = $this->getNamespaceForPage( $pageId );
		$this->assertNull( $namespace );

		// migrate unpopulated namespace_id(s) from the page table to linter table
		$database = $this->getDatabase();
		$database->migrateNamespace( 2, 3, 0 );

		// Verify all linter records now have proper namespace IDs in the linter_namespace field
		$this->checkPagesNamespace( $titleAndPages, $namespaceIds );
	}

	/**
	 * @param string $titleText
	 * @param array $error
	 * @return array
	 */
	private function createTitleAndPageForTagsAndRunJob( string $titleText, array $error ): array {
		$titleAndPage = $this->createTitleAndPage( $titleText );
		$job = $this->newRecordLintJob( $titleAndPage[ 'title' ], [
			'errors' => [ $error ],
			'revision' => $titleAndPage[ 'revID' ]
		] );
		$this->assertTrue( $job->run() );
		return $titleAndPage;
	}

	/**
	 * Set the linter_tag and linter_template field values to an empty string for a page
	 * to test that the database migration code handles existing records with fields set
	 * and records where the fields are not yet set, and need migration.
	 * @param int $pageId
	 */
	private function setTagAndTemplateForPageToEmptyString( int $pageId ) {
		$queryLinterPageNamespace = new UpdateQueryBuilder( $this->db );
		$queryLinterPageNamespace
			->update( 'linter' )
			->set( [ 'linter_template' => '', 'linter_tag' => '' ] )
			->where( [ 'linter_page' => $pageId ] )
			->caller( __METHOD__ )
			->execute();
	}

	/**
	 * @param array $writeEnables
	 * @param array $error
	 * @return array
	 */
	private function createPagesWithTagAndTemplate( array $writeEnables, array $error ): array {
		$titleAndPages = [];
		foreach ( $writeEnables as $index => $enable ) {
			$titleAndPage = $this->createTitleAndPageForTagsAndRunJob( 'TestPage' . $index, $error );
			$titleAndPages[] = $titleAndPage;
			// clear the tag and template field data for select test records
			if ( !$enable ) {
				$this->setTagAndTemplateForPageToEmptyString( $titleAndPage[ 'pageID' ] );
			}
		}
		return $titleAndPages;
	}

	/**
	 * @param array $pages
	 * @return void
	 */
	private function checkPagesTagAndTemplate( array $pages ) {
		foreach ( $pages as $page ) {
			$pageId = $page[ 'pageID' ];
			$tag = $this->getTagForPage( $pageId );
			$this->assertEquals( "center", $tag );
			$template = $this->getTemplateForPage( $pageId );
			$this->assertEquals( "Template:Echo", $template );
		}
	}

	public function testMigrateTagAndTemplate() {
		$error = [
			'type' => 'obsolete-tag',
			'location' => [ 0, 10 ],
			'params' => [ "name" => "center",
				"templateInfo" => [ "name" => "Template:Echo" ] ],
			'dbid' => null,
		];

		// Create groups of records that do not need migrating to ensure batching works properly
		$writeEnables = [ false, true, true, true, false, false, true, true, false, false, false, true, false ];
		$titleAndPages = $this->createPagesWithTagAndTemplate( $writeEnables, $error );

		// Create special case test of migrate code encountering brackets - linter_params = '[]'
		$error = [
			'type' => 'wikilink-in-extlink',
			'location' => [ 0, 10 ],
			'params' => [],
			'dbid' => null,
		];
		$titleAndPageBrackets = $this->createTitleAndPageForTagsAndRunJob(
			'TestPageTagAndTemplateBrackets',
			$error );

		// Create special case test for migrate code encountering 'multi-part-template-block'
		$error = [
			'type' => 'obsolete-tag',
			'location' => [ 0, 10 ],
			'params' => '{"name":"center","templateInfo":{"multiPartTemplateBlock":true}}',
			'dbid' => null,
		];
		$titleAndPageMultipart = $this->createTitleAndPageForTagsAndRunJob(
			'TestPageTagAndTemplateMultipart',
			$error );

		// Create special case test for params containing tag and template info strings exceeding the fields lengths
		$tagWithMoreThan30Characters = "center tag exceeding 30 characters";
		$templateWithMoreThan250Characters = str_repeat( "Template:Echo longer than 250 characters ", 8 );
		$error = [
			'type' => 'obsolete-tag',
			'location' => [ 0, 10 ],
			'params' => [ "name" => $tagWithMoreThan30Characters,
				"templateInfo" => $templateWithMoreThan250Characters ],
			'dbid' => null,
		];
		$titleAndPageLengthExceeded = $this->createTitleAndPageForTagsAndRunJob(
			'TestPageTagAndTemplateLengthExceeded',
			$error );

		// Verify the create page function did not populate the linter_tag and linter_template field for TestPage0
		$pageId = $titleAndPages[ 0 ][ 'pageID' ];
		$tag = $this->getTagForPage( $pageId );
		$this->assertSame( "", $tag );
		$template = $this->getTemplateForPage( $pageId );
		$this->assertSame( "", $template );

		// Migrate unpopulated tag and template info from the params field
		$database = $this->getDatabase();
		$database->migrateTemplateAndTagInfo( 3, 0 );

		// Verify all linter records have the proper tag and template field info migrated from the params field
		$this->checkPagesTagAndTemplate( $titleAndPages );

		// Verify special case test of migrate code encountering brackets - linter_params = '[]'
		$tag = $this->getTagForPage( $titleAndPageBrackets[ 'pageID' ] );
		$this->assertSame( "", $tag );
		$template = $this->getTemplateForPage( $titleAndPageBrackets[ 'pageID' ] );
		$this->assertSame( "", $template );

		// Verify special case test for migrate code encountering 'multi-part-template-block'
		$tag = $this->getTagForPage( $titleAndPageMultipart[ 'pageID' ] );
		$this->assertEquals( "center", $tag );
		$template = $this->getTemplateForPage( $titleAndPageMultipart[ 'pageID' ] );
		$this->assertEquals( "multi-part-template-block", $template );

		// Verify special case test for migrate code encountering params with tag and template string length exceeded
		$tagTruncated = "center tag exceeding 30 charac";
		$templateTruncated = "Template:Echo longer than 250 characters Template:Echo longer than 250 characters " .
			"Template:Echo longer than 250 characters Template:Echo longer than 250 characters " .
			"Template:Echo longer than 250 characters Template:Echo longer than 250 characters Temp";

		$tag = $this->getTagForPage( $titleAndPageLengthExceeded[ 'pageID' ] );
		$this->assertEquals( $tagTruncated, $tag );
		$template = $this->getTemplateForPage( $titleAndPageLengthExceeded[ 'pageID' ] );
		$this->assertEquals( $templateTruncated, $template );
	}

	public function testDropInlineMediaCaptionLints() {
		$error = [
			'type' => 'inline-media-caption',
			'location' => [ 0, 10 ],
			'params' => [],
			'dbid' => null,
		];
		$titleAndPage = $this->createTitleAndPage( 'TestPageMediaCaption' );
		$job = $this->newRecordLintJob( $titleAndPage[ 'title' ], [
			'errors' => [ $error ],
			'revision' => $titleAndPage[ 'revID' ]
		] );
		$this->assertTrue( $job->run() );
		$errorsFromDb = array_values( $this->getDatabase()->getForPage( $titleAndPage['pageID'] ) );
		$this->assertCount( 0, $errorsFromDb );
	}
}