| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 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 |
|
| 33 |
|
| 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 |
|
| 54 |
|
| 55 |
|
| 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 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 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 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 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 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 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 |
|
| 118 |
|
| 119 |
|
| 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 |
|
| 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 |
|
| 223 |
|
| 224 |
|
| 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 |
|
| 244 |
|
| 245 |
|
| 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 |
|
| 255 |
if ( !$setToID[ $index ] ) { |
| 256 |
$this->setNamespaceForPageToNull( $titleAndPage['pageID'] ); |
| 257 |
} |
| 258 |
} |
| 259 |
return $titleAndPages; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 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 |
|
| 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 |
|
| 284 |
$pageId = $titleAndPages[ 0 ][ 'pageID' ]; |
| 285 |
$namespace = $this->getNamespaceForPage( $pageId ); |
| 286 |
$this->assertNull( $namespace ); |
| 287 |
|
| 288 |
|
| 289 |
$database = $this->getDatabase(); |
| 290 |
$database->migrateNamespace( 2, 3, 0 ); |
| 291 |
|
| 292 |
|
| 293 |
$this->checkPagesNamespace( $titleAndPages, $namespaceIds ); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 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 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 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 |
|
| 329 |
|
| 330 |
|
| 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 |
|
| 338 |
if ( !$enable ) { |
| 339 |
$this->setTagAndTemplateForPageToEmptyString( $titleAndPage[ 'pageID' ] ); |
| 340 |
} |
| 341 |
} |
| 342 |
return $titleAndPages; |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 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 |
|
| 369 |
$writeEnables = [ false, true, true, true, false, false, true, true, false, false, false, true, false ]; |
| 370 |
$titleAndPages = $this->createPagesWithTagAndTemplate( $writeEnables, $error ); |
| 371 |
|
| 372 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 416 |
$database = $this->getDatabase(); |
| 417 |
$database->migrateTemplateAndTagInfo( 3, 0 ); |
| 418 |
|
| 419 |
|
| 420 |
$this->checkPagesTagAndTemplate( $titleAndPages ); |
| 421 |
|
| 422 |
|
| 423 |
$tag = $this->getTagForPage( $titleAndPageBrackets[ 'pageID' ] ); |
| 424 |
$this->assertSame( "", $tag ); |
| 425 |
$template = $this->getTemplateForPage( $titleAndPageBrackets[ 'pageID' ] ); |
| 426 |
$this->assertSame( "", $template ); |
| 427 |
|
| 428 |
|
| 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 |
|
| 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 |
} |