Debian Codegraph @

extensions/Linter/tests/phpunit/RecordLintJobTest.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Linter\Test;
22
23 use MediaWiki\Linter\LintError;
24 use MediaWiki\Linter\RecordLintJob;
25 use MediaWiki\Page\PageReference;
26 use MediaWiki\Title\Title;
27 use MediaWikiIntegrationTestCase;
28 use Wikimedia\Rdbms\SelectQueryBuilder;
29 use Wikimedia\Rdbms\UpdateQueryBuilder;
30
31 /**
32 * @group Database
33 * @covers \MediaWiki\Linter\RecordLintJob
34 */
35 class RecordLintJobTest extends MediaWikiIntegrationTestCase {
36
37 private function getDatabase() {
38 return $this->getServiceContainer()->get( 'Linter.Database' );
39 }
40
41 private function newRecordLintJob( PageReference $page, array $params ) {
42 $services = $this->getServiceContainer();
43 return new RecordLintJob(
44 $page,
45 $params,
46 $services->get( 'Linter.TotalsLookup' ),
47 $this->getDatabase(),
48 $services->get( 'Linter.CategoryManager' )
49 );
50 }
51
52 /**
53 * @param string $titleText
54 * @param int|null $ns
55 * @return array
56 */
57 private function createTitleAndPage( string $titleText, ?int $ns = 0 ) {
58 $title = Title::newFromText( $titleText, $ns );
59 $page = $this->getExistingTestPage( $title );
60
61 return [
62 'title' => $title,
63 'pageID' => $page->getRevisionRecord()->getPageId(),
64 'revID' => $page->getRevisionRecord()->getID()
65 ];
66 }
67
68 /**
69 * Get just the lint error linter_tag field value for a page
70 *
71 * @param int $pageId
72 * @return mixed
73 */
74 private function getTagForPage( int $pageId ) {
75 $queryPageTag = new SelectQueryBuilder( $this->db );
76 $queryPageTag
77 ->select( 'linter_tag' )
78 ->table( 'linter' )
79 ->where( [ 'linter_page' => $pageId ] )
80 ->caller( __METHOD__ );
81 return $queryPageTag->fetchField();
82 }
83
84 /**
85 * Get just the lint error linter_template field value for a page
86 *
87 * @param int $pageId
88 * @return mixed
89 */
90 private function getTemplateForPage( int $pageId ) {
91 $queryPageTemplate = new SelectQueryBuilder( $this->db );
92 $queryPageTemplate
93 ->select( 'linter_template' )
94 ->table( 'linter' )
95 ->where( [ 'linter_page' => $pageId ] )
96 ->caller( __METHOD__ );
97 return $queryPageTemplate->fetchField();
98 }
99
100 /**
101 * Get just the linter_namespace field value from the linter table for a page
102 *
103 * @param int $pageId
104 * @return mixed
105 */
106 private function getNamespaceForPage( int $pageId ) {
107 $queryLinterPageNamespace = new SelectQueryBuilder( $this->db );
108 $queryLinterPageNamespace
109 ->select( 'linter_namespace' )
110 ->table( 'linter' )
111 ->where( [ 'linter_page' => $pageId ] )
112 ->caller( __METHOD__ );
113 return $queryLinterPageNamespace->fetchField();
114 }
115
116 /**
117 * Set just the linter_namespace field value from the linter table for a page
118 *
119 * @param int $pageId
120 */
121 private function setNamespaceForPageToNull( int $pageId ) {
122 $queryLinterPageNamespace = new UpdateQueryBuilder( $this->db );
123 $queryLinterPageNamespace
124 ->update( 'linter' )
125 ->set( [ 'linter_namespace' => null ] )
126 ->where( [ 'linter_page' => $pageId ] )
127 ->caller( __METHOD__ )
128 ->execute();
129 }
130
131 public function testRun() {
132 $error = [
133 'type' => 'fostered',
134 'location' => [ 0, 10 ],
135 'params' => [],
136 'dbid' => null,
137 ];
138 $titleAndPage = $this->createTitleAndPage( 'TestPage' );
139 $job = $this->newRecordLintJob( $titleAndPage[ 'title' ], [
140 'errors' => [ $error ],
141 'revision' => $titleAndPage[ 'revID' ]
142 ] );
143 $this->assertTrue( $job->run() );
144 $db = $this->getDatabase();
145 $errorsFromDb = array_values( $db->getForPage( $titleAndPage[ 'pageID' ] ) );
146 $this->assertCount( 1, $errorsFromDb );
147 $this->assertInstanceOf( LintError::class, $errorsFromDb[ 0 ] );
148 $this->assertEquals( $error[ 'type' ], $errorsFromDb[ 0 ]->category );
149 $this->assertEquals( $error[ 'location' ], $errorsFromDb[ 0 ]->location );
150 $this->assertEquals( $error[ 'params' ], $errorsFromDb[ 0 ]->params );
151 }
152
153 public function testWriteTagAndTemplate() {
154 $error = [
155 'type' => 'obsolete-tag',
156 'location' => [ 0, 10 ],
157 'params' => [
158 "name" => "center",
159 "templateInfo" => [ "name" => "Template:Echo" ]
160 ],
161 'dbid' => null,
162 ];
163 $titleAndPage = $this->createTitleAndPage( 'TestPage2' );
164 $job = $this->newRecordLintJob( $titleAndPage[ 'title' ], [
165 'errors' => [ $error ],
166 'revision' => $titleAndPage[ 'revID' ]
167 ] );
168 $this->assertTrue( $job->run() );
169 $pageId = $titleAndPage[ 'pageID' ];
170 $db = $this->getDatabase();
171 $errorsFromDb = array_values( $db->getForPage( $pageId ) );
172 $this->assertCount( 1, $errorsFromDb );
173 $this->assertInstanceOf( LintError::class, $errorsFromDb[0] );
174 $this->assertEquals( $error[ 'type' ], $errorsFromDb[0]->category );
175 $this->assertEquals( $error[ 'location' ], $errorsFromDb[0]->location );
176 $this->assertEquals( $error[ 'params' ], $errorsFromDb[0]->params );
177 $tag = $this->getTagForPage( $pageId );
178 $this->assertEquals( $error[ 'params' ][ 'name' ], $tag );
179 $template = $this->getTemplateForPage( $pageId );
180 $this->assertEquals( $error[ 'params' ][ 'templateInfo' ][ 'name' ], $template );
181 }
182
183 public function testWriteTagAndTemplateLengthExceeded() {
184 // Verify special case test for write code encountering params with tag and template string lengths exceeded
185 $tagWithMoreThan30Characters = "center tag exceeding 30 characters";
186 $tagTruncated = "center tag exceeding 30 charac";
187 $templateWithMoreThan250Characters = str_repeat( "Template:Echo longer than 250 characters ", 8 );
188 $templateTruncated = "Template:Echo longer than 250 characters Template:Echo longer than 250 characters " .
189 "Template:Echo longer than 250 characters Template:Echo longer than 250 characters " .
190 "Template:Echo longer than 250 characters Template:Echo longer than 250 characters Temp";
191
192 $error = [
193 'type' => 'obsolete-tag',
194 'location' => [ 0, 10 ],
195 'params' => [
196 "name" => $tagWithMoreThan30Characters,
197 "templateInfo" => [ "name" => $templateWithMoreThan250Characters ]
198 ],
199 'dbid' => null,
200 ];
201 $titleAndPage = $this->createTitleAndPage( 'TestPage2' );
202 $job = $this->newRecordLintJob( $titleAndPage[ 'title' ], [
203 'errors' => [ $error ],
204 'revision' => $titleAndPage[ 'revID' ]
205 ] );
206 $this->assertTrue( $job->run() );
207 $pageId = $titleAndPage[ 'pageID' ];
208 $db = $this->getDatabase();
209 $errorsFromDb = array_values( $db->getForPage( $pageId ) );
210 $this->assertCount( 1, $errorsFromDb );
211 $this->assertInstanceOf( LintError::class, $errorsFromDb[0] );
212 $this->assertEquals( $error[ 'type' ], $errorsFromDb[0]->category );
213 $this->assertEquals( $error[ 'location' ], $errorsFromDb[0]->location );
214 $this->assertEquals( $error[ 'params' ], $errorsFromDb[0]->params );
215 $tag = $this->getTagForPage( $pageId );
216 $this->assertEquals( $tagTruncated, $tag );
217 $template = $this->getTemplateForPage( $pageId );
218 $this->assertEquals( $templateTruncated, $template );
219 }
220
221 /**
222 * @param string $titleText
223 * @param int $namespace
224 * @return array
225 */
226 private function createTitleAndPageAndRunJob( string $titleText, int $namespace ): array {
227 $titleAndPage = $this->createTitleAndPage( $titleText, $namespace );
228 $error = [
229 'type' => 'fostered',
230 'location' => [ 0, 10 ],
231 'params' => [],
232 'dbid' => null,
233 ];
234 $job = $this->newRecordLintJob( $titleAndPage[ 'title' ], [
235 'errors' => [ $error ],
236 'revision' => $titleAndPage[ 'revID' ]
237 ] );
238 $this->assertTrue( $job->run() );
239 return $titleAndPage;
240 }
241
242 /**
243 * @param array $namespaceIds
244 * @param array $setToID
245 * @return array
246 */
247 private function createPagesWithNamespace( array $namespaceIds, array $setToID ): array {
248 $titleAndPages = [];
249 foreach ( $namespaceIds as $index => $namespaceId ) {
250 $titleAndPage = $this->createTitleAndPageAndRunJob(
251 'TestPageNamespace' . $index,
252 intval( $namespaceId ) );
253 $titleAndPages[] = $titleAndPage;
254 // To test the migration code, set some namespaces to null to simulate having a mix of valid and null values
255 if ( !$setToID[ $index ] ) {
256 $this->setNamespaceForPageToNull( $titleAndPage['pageID'] );
257 }
258 }
259 return $titleAndPages;
260 }
261
262 /**
263 * @param array $pages
264 * @param array $namespaceIds
265 * @return void
266 */
267 private function checkPagesNamespace( array $pages, array $namespaceIds ) {
268 foreach ( $pages as $index => $page ) {
269 $pageId = $page[ 'pageID' ];
270 $namespace = $this->getNamespaceForPage( $pageId );
271 $namespaceId = $namespaceIds[ $index ];
272 $this->assertSame( "$namespaceId", $namespace );
273 }
274 }
275
276 public function testMigrateNamespace() {
277 // Create groups of records that do not need migrating to ensure batching works properly
278 $namespaceIds = [ '0', '1', '2', '3', '4', '5', '4', '3', '2', '1', '0', '1', '2' ];
279 $setToID = [ false, true, true, true, false, false, true, true, false, false, false, true, false ];
280
281 $titleAndPages = $this->createPagesWithNamespace( $namespaceIds, $setToID );
282
283 // Verify the create page function did not populate the linter_namespace field for TestPageNamespace0
284 $pageId = $titleAndPages[ 0 ][ 'pageID' ];
285 $namespace = $this->getNamespaceForPage( $pageId );
286 $this->assertNull( $namespace );
287
288 // migrate unpopulated namespace_id(s) from the page table to linter table
289 $database = $this->getDatabase();
290 $database->migrateNamespace( 2, 3, 0 );
291
292 // Verify all linter records now have proper namespace IDs in the linter_namespace field
293 $this->checkPagesNamespace( $titleAndPages, $namespaceIds );
294 }
295
296 /**
297 * @param string $titleText
298 * @param array $error
299 * @return array
300 */
301 private function createTitleAndPageForTagsAndRunJob( string $titleText, array $error ): array {
302 $titleAndPage = $this->createTitleAndPage( $titleText );
303 $job = $this->newRecordLintJob( $titleAndPage[ 'title' ], [
304 'errors' => [ $error ],
305 'revision' => $titleAndPage[ 'revID' ]
306 ] );
307 $this->assertTrue( $job->run() );
308 return $titleAndPage;
309 }
310
311 /**
312 * Set the linter_tag and linter_template field values to an empty string for a page
313 * to test that the database migration code handles existing records with fields set
314 * and records where the fields are not yet set, and need migration.
315 * @param int $pageId
316 */
317 private function setTagAndTemplateForPageToEmptyString( int $pageId ) {
318 $queryLinterPageNamespace = new UpdateQueryBuilder( $this->db );
319 $queryLinterPageNamespace
320 ->update( 'linter' )
321 ->set( [ 'linter_template' => '', 'linter_tag' => '' ] )
322 ->where( [ 'linter_page' => $pageId ] )
323 ->caller( __METHOD__ )
324 ->execute();
325 }
326
327 /**
328 * @param array $writeEnables
329 * @param array $error
330 * @return array
331 */
332 private function createPagesWithTagAndTemplate( array $writeEnables, array $error ): array {
333 $titleAndPages = [];
334 foreach ( $writeEnables as $index => $enable ) {
335 $titleAndPage = $this->createTitleAndPageForTagsAndRunJob( 'TestPage' . $index, $error );
336 $titleAndPages[] = $titleAndPage;
337 // clear the tag and template field data for select test records
338 if ( !$enable ) {
339 $this->setTagAndTemplateForPageToEmptyString( $titleAndPage[ 'pageID' ] );
340 }
341 }
342 return $titleAndPages;
343 }
344
345 /**
346 * @param array $pages
347 * @return void
348 */
349 private function checkPagesTagAndTemplate( array $pages ) {
350 foreach ( $pages as $page ) {
351 $pageId = $page[ 'pageID' ];
352 $tag = $this->getTagForPage( $pageId );
353 $this->assertEquals( "center", $tag );
354 $template = $this->getTemplateForPage( $pageId );
355 $this->assertEquals( "Template:Echo", $template );
356 }
357 }
358
359 public function testMigrateTagAndTemplate() {
360 $error = [
361 'type' => 'obsolete-tag',
362 'location' => [ 0, 10 ],
363 'params' => [ "name" => "center",
364 "templateInfo" => [ "name" => "Template:Echo" ] ],
365 'dbid' => null,
366 ];
367
368 // Create groups of records that do not need migrating to ensure batching works properly
369 $writeEnables = [ false, true, true, true, false, false, true, true, false, false, false, true, false ];
370 $titleAndPages = $this->createPagesWithTagAndTemplate( $writeEnables, $error );
371
372 // Create special case test of migrate code encountering brackets - linter_params = '[]'
373 $error = [
374 'type' => 'wikilink-in-extlink',
375 'location' => [ 0, 10 ],
376 'params' => [],
377 'dbid' => null,
378 ];
379 $titleAndPageBrackets = $this->createTitleAndPageForTagsAndRunJob(
380 'TestPageTagAndTemplateBrackets',
381 $error );
382
383 // Create special case test for migrate code encountering 'multi-part-template-block'
384 $error = [
385 'type' => 'obsolete-tag',
386 'location' => [ 0, 10 ],
387 'params' => '{"name":"center","templateInfo":{"multiPartTemplateBlock":true}}',
388 'dbid' => null,
389 ];
390 $titleAndPageMultipart = $this->createTitleAndPageForTagsAndRunJob(
391 'TestPageTagAndTemplateMultipart',
392 $error );
393
394 // Create special case test for params containing tag and template info strings exceeding the fields lengths
395 $tagWithMoreThan30Characters = "center tag exceeding 30 characters";
396 $templateWithMoreThan250Characters = str_repeat( "Template:Echo longer than 250 characters ", 8 );
397 $error = [
398 'type' => 'obsolete-tag',
399 'location' => [ 0, 10 ],
400 'params' => [ "name" => $tagWithMoreThan30Characters,
401 "templateInfo" => $templateWithMoreThan250Characters ],
402 'dbid' => null,
403 ];
404 $titleAndPageLengthExceeded = $this->createTitleAndPageForTagsAndRunJob(
405 'TestPageTagAndTemplateLengthExceeded',
406 $error );
407
408 // Verify the create page function did not populate the linter_tag and linter_template field for TestPage0
409 $pageId = $titleAndPages[ 0 ][ 'pageID' ];
410 $tag = $this->getTagForPage( $pageId );
411 $this->assertSame( "", $tag );
412 $template = $this->getTemplateForPage( $pageId );
413 $this->assertSame( "", $template );
414
415 // Migrate unpopulated tag and template info from the params field
416 $database = $this->getDatabase();
417 $database->migrateTemplateAndTagInfo( 3, 0 );
418
419 // Verify all linter records have the proper tag and template field info migrated from the params field
420 $this->checkPagesTagAndTemplate( $titleAndPages );
421
422 // Verify special case test of migrate code encountering brackets - linter_params = '[]'
423 $tag = $this->getTagForPage( $titleAndPageBrackets[ 'pageID' ] );
424 $this->assertSame( "", $tag );
425 $template = $this->getTemplateForPage( $titleAndPageBrackets[ 'pageID' ] );
426 $this->assertSame( "", $template );
427
428 // Verify special case test for migrate code encountering 'multi-part-template-block'
429 $tag = $this->getTagForPage( $titleAndPageMultipart[ 'pageID' ] );
430 $this->assertEquals( "center", $tag );
431 $template = $this->getTemplateForPage( $titleAndPageMultipart[ 'pageID' ] );
432 $this->assertEquals( "multi-part-template-block", $template );
433
434 // Verify special case test for migrate code encountering params with tag and template string length exceeded
435 $tagTruncated = "center tag exceeding 30 charac";
436 $templateTruncated = "Template:Echo longer than 250 characters Template:Echo longer than 250 characters " .
437 "Template:Echo longer than 250 characters Template:Echo longer than 250 characters " .
438 "Template:Echo longer than 250 characters Template:Echo longer than 250 characters Temp";
439
440 $tag = $this->getTagForPage( $titleAndPageLengthExceeded[ 'pageID' ] );
441 $this->assertEquals( $tagTruncated, $tag );
442 $template = $this->getTemplateForPage( $titleAndPageLengthExceeded[ 'pageID' ] );
443 $this->assertEquals( $templateTruncated, $template );
444 }
445
446 public function testDropInlineMediaCaptionLints() {
447 $error = [
448 'type' => 'inline-media-caption',
449 'location' => [ 0, 10 ],
450 'params' => [],
451 'dbid' => null,
452 ];
453 $titleAndPage = $this->createTitleAndPage( 'TestPageMediaCaption' );
454 $job = $this->newRecordLintJob( $titleAndPage[ 'title' ], [
455 'errors' => [ $error ],
456 'revision' => $titleAndPage[ 'revID' ]
457 ] );
458 $this->assertTrue( $job->run() );
459 $errorsFromDb = array_values( $this->getDatabase()->getForPage( $titleAndPage['pageID'] ) );
460 $this->assertCount( 0, $errorsFromDb );
461 }
462 }