| 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 Exception; |
| 24 |
use MediaWiki\Content\ContentHandler; |
| 25 |
use MediaWiki\Linter\RecordLintJob; |
| 26 |
use MediaWiki\Linter\SpecialLintErrors; |
| 27 |
use MediaWiki\Page\PageReference; |
| 28 |
use MediaWiki\Request\FauxRequest; |
| 29 |
use MediaWiki\Title\Title; |
| 30 |
use SpecialPageTestBase; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
class SpecialLintErrorsTest extends SpecialPageTestBase { |
| 38 |
|
| 39 |
private function getDatabase() { |
| 40 |
return $this->getServiceContainer()->get( 'Linter.Database' ); |
| 41 |
} |
| 42 |
|
| 43 |
private function newRecordLintJob( PageReference $page, array $params ) { |
| 44 |
$services = $this->getServiceContainer(); |
| 45 |
return new RecordLintJob( |
| 46 |
$page, |
| 47 |
$params, |
| 48 |
$services->get( 'Linter.TotalsLookup' ), |
| 49 |
$this->getDatabase(), |
| 50 |
$services->get( 'Linter.CategoryManager' ) |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
protected function newSpecialPage() { |
| 55 |
$services = $this->getServiceContainer(); |
| 56 |
return new SpecialLintErrors( |
| 57 |
$services->getNamespaceInfo(), |
| 58 |
$services->getTitleParser(), |
| 59 |
$services->getLinkCache(), |
| 60 |
$services->getPermissionManager(), |
| 61 |
$services->get( 'Linter.CategoryManager' ), |
| 62 |
$services->get( 'Linter.TotalsLookup' ) |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
public function testExecute() { |
| 67 |
$categoryManager = |
| 68 |
$this->getServiceContainer()->get( 'Linter.CategoryManager' ); |
| 69 |
$category = $categoryManager->getVisibleCategories()[0]; |
| 70 |
|
| 71 |
|
| 72 |
$html = $this->executeSpecialPage( '', null, 'qqx' )[0]; |
| 73 |
$this->assertStringContainsString( '(linterrors-summary)', $html ); |
| 74 |
$this->assertStringContainsString( "(linter-category-$category)", $html ); |
| 75 |
|
| 76 |
$this->assertStringContainsString( |
| 77 |
"(linter-category-$category-desc)", |
| 78 |
$this->executeSpecialPage( $category, null, 'qqx' )[0] |
| 79 |
); |
| 80 |
|
| 81 |
|
| 82 |
$html = $this->executeSpecialPage( 'misnested-tag', null, 'qqx' )[0]; |
| 83 |
$this->assertStringContainsString( 'linter-form-template', $html ); |
| 84 |
$this->assertStringContainsString( 'linter-form-tag', $html ); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
private function createTitleAndPage( |
| 93 |
string $titleText = 'SpecialLintErrorsTest test page', |
| 94 |
?int $ns = null |
| 95 |
): array { |
| 96 |
$ns ??= $this->getDefaultWikitextNS(); |
| 97 |
$title = Title::newFromText( $titleText, $ns ); |
| 98 |
$page = $this->getExistingTestPage( $title ); |
| 99 |
|
| 100 |
return [ |
| 101 |
'title' => $title, |
| 102 |
'pageID' => $page->getRevisionRecord()->getPageId(), |
| 103 |
'revID' => $page->getRevisionRecord()->getID(), |
| 104 |
'page' => $page, |
| 105 |
]; |
| 106 |
} |
| 107 |
|
| 108 |
public function testContentModelChange() { |
| 109 |
$error = [ |
| 110 |
'type' => 'obsolete-tag', |
| 111 |
'location' => [ 0, 10 ], |
| 112 |
'params' => [], |
| 113 |
'dbid' => null, |
| 114 |
]; |
| 115 |
$titleAndPage = $this->createTitleAndPage(); |
| 116 |
$job = $this->newRecordLintJob( $titleAndPage['title'], [ |
| 117 |
'errors' => [ $error ], |
| 118 |
'revision' => $titleAndPage['revID'] |
| 119 |
] ); |
| 120 |
$this->assertTrue( $job->run() ); |
| 121 |
|
| 122 |
$pageId = $titleAndPage['pageID']; |
| 123 |
$db = $this->getDatabase(); |
| 124 |
|
| 125 |
$errorsFromDb = array_values( $db->getForPage( $pageId ) ); |
| 126 |
$this->assertCount( 1, $errorsFromDb ); |
| 127 |
|
| 128 |
$cssText = 'css content model change test page content'; |
| 129 |
$content = ContentHandler::makeContent( |
| 130 |
$cssText, |
| 131 |
$titleAndPage['title'], |
| 132 |
'css' |
| 133 |
); |
| 134 |
$page = $titleAndPage['page']; |
| 135 |
$this->editPage( |
| 136 |
$page, |
| 137 |
$content, |
| 138 |
"update with css content model to trigger onRevisionFromEditComplete hook" |
| 139 |
); |
| 140 |
|
| 141 |
$errorsFromDb = array_values( $db->getForPage( $pageId ) ); |
| 142 |
$this->assertCount( 0, $errorsFromDb ); |
| 143 |
} |
| 144 |
|
| 145 |
public function testContentModelChangeWithBlankPage() { |
| 146 |
$error = [ |
| 147 |
'type' => 'obsolete-tag', |
| 148 |
'location' => [ 0, 10 ], |
| 149 |
'params' => [], |
| 150 |
'dbid' => null, |
| 151 |
]; |
| 152 |
$titleAndPage = $this->createTitleAndPage(); |
| 153 |
$job = $this->newRecordLintJob( $titleAndPage['title'], [ |
| 154 |
'errors' => [ $error ], |
| 155 |
'revision' => $titleAndPage['revID'] |
| 156 |
] ); |
| 157 |
$this->assertTrue( $job->run() ); |
| 158 |
|
| 159 |
$pageId = $titleAndPage['pageID']; |
| 160 |
$db = $this->getDatabase(); |
| 161 |
|
| 162 |
$errorsFromDb = array_values( $db->getForPage( $pageId ) ); |
| 163 |
$this->assertCount( 1, $errorsFromDb ); |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
$blankText = ''; |
| 169 |
$content = ContentHandler::makeContent( |
| 170 |
$blankText, |
| 171 |
$titleAndPage['title'], |
| 172 |
'text' |
| 173 |
); |
| 174 |
$page = $titleAndPage['page']; |
| 175 |
$this->editPage( |
| 176 |
$page, |
| 177 |
$content, |
| 178 |
"update with blank text content model to trigger onRevisionFromEditComplete hook" |
| 179 |
); |
| 180 |
|
| 181 |
$errorsFromDb = array_values( $db->getForPage( $pageId ) ); |
| 182 |
$this->assertCount( 0, $errorsFromDb ); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
private function createPagesWithLintErrorsFromData( array $pageData ) { |
| 189 |
foreach ( $pageData as $data ) { |
| 190 |
$titleAndPage = $this->createTitleAndPage( $data[ 'name' ], $data[ 'ns' ] ); |
| 191 |
$errors = []; |
| 192 |
foreach ( $data[ 'lintErrors' ] as $lintError ) { |
| 193 |
$errors[] = [ |
| 194 |
'type' => $lintError[ 'type' ], |
| 195 |
'location' => $lintError[ 'location' ], |
| 196 |
'params' => [], |
| 197 |
'dbid' => null |
| 198 |
]; |
| 199 |
} |
| 200 |
$job = $this->newRecordLintJob( $titleAndPage[ 'title' ], [ |
| 201 |
'errors' => $errors, |
| 202 |
'revision' => $titleAndPage[ 'revID' ] |
| 203 |
] ); |
| 204 |
$job->run(); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
private function createTitleAndPageAndLintErrorData(): array { |
| 212 |
$pageData = []; |
| 213 |
$pageData[] = [ 'name' => 'Lint Error One', 'ns' => 0, |
| 214 |
'lintErrors' => [ |
| 215 |
[ 'type' => 'obsolete-tag', 'location' => [ 0, 10 ] ], |
| 216 |
[ 'type' => 'misnested-tag', 'location' => [ 20, 30 ] ] |
| 217 |
] |
| 218 |
]; |
| 219 |
$pageData[] = [ 'name' => 'LintErrorTwo', 'ns' => 3, |
| 220 |
'lintErrors' => [ [ 'type' => 'obsolete-tag', 'location' => [ 0, 10 ] ] ] |
| 221 |
]; |
| 222 |
$pageData[] = [ 'name' => 'NotANamespace:LintErrorThree', 'ns' => 0, |
| 223 |
'lintErrors' => [ |
| 224 |
[ 'type' => 'obsolete-tag', 'location' => [ 0, 10 ] ], |
| 225 |
[ 'type' => 'misnested-tag', 'location' => [ 20, 30 ] ] |
| 226 |
] |
| 227 |
]; |
| 228 |
$pageData[] = [ 'name' => 'NotANamespace:LintErrorFour', 'ns' => 0, |
| 229 |
'lintErrors' => [ |
| 230 |
[ 'type' => 'obsolete-tag', 'location' => [ 0, 10 ] ], |
| 231 |
[ 'type' => 'misnested-tag', 'location' => [ 20, 30 ] ] |
| 232 |
] |
| 233 |
]; |
| 234 |
$pageData[] = [ 'name' => 'Some other page', 'ns' => 0, |
| 235 |
'lintErrors' => [ [ 'type' => 'bogus-image-options', 'location' => [ 30, 40 ] ] ] |
| 236 |
]; |
| 237 |
$pageData[] = [ 'name' => 'FooBar:ErrorFive', 'ns' => 3, |
| 238 |
'lintErrors' => [ [ 'type' => 'obsolete-tag', 'location' => [ 0, 10 ] ] ] |
| 239 |
]; |
| 240 |
$pageData[] = [ 'name' => 'ErrorSix', 'ns' => 3, |
| 241 |
'lintErrors' => [ |
| 242 |
[ 'type' => 'obsolete-tag', 'location' => [ 0, 10 ] ], |
| 243 |
[ 'type' => 'misnested-tag', 'location' => [ 20, 30 ] ] |
| 244 |
] |
| 245 |
]; |
| 246 |
return $pageData; |
| 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 |
private function createLinterSearchTestConfigurations( ?string $subpage ): array { |
| 278 |
$testConfigurations = []; |
| 279 |
if ( $subpage !== 'misnested-tag' ) { |
| 280 |
$testConfigurations[ 1 ] = [ |
| 281 |
'namespaces' => [ 0, 3 ], |
| 282 |
'titles' => [ '' ], |
| 283 |
'cases' => [ [ 'iterations' => [ 0, 1, 2, 3 ], 'message' => 'linter-invalid-title' ] |
| 284 |
] |
| 285 |
]; |
| 286 |
$testConfigurations[ 2 ] = [ |
| 287 |
'namespaces' => [ 0, 3 ], |
| 288 |
'titles' => [ 'NotANamespace:L', 'NotANamespace:LintErrorFour' ], |
| 289 |
'cases' => [ |
| 290 |
[ 'iterations' => [ 1 ], 'message' => 'NotANamespace:LintErrorThree' ], |
| 291 |
[ 'iterations' => [ 1, 2, 3 ], 'message' => 'NotANamespace:LintErrorFour' ], |
| 292 |
[ 'iterations' => [ 0, 4, 5, 6, 7 ], 'message' => 'table_pager_empty' ] |
| 293 |
] |
| 294 |
]; |
| 295 |
$testConfigurations[ 3 ] = [ |
| 296 |
'namespaces' => [ 0, 1, 3 ], |
| 297 |
'titles' => [ 'L', 'Lint Error One', 'LintErrorTwo', 'User talk:L', 'User talk:LintErrorTwo', |
| 298 |
'Talk:L' ], |
| 299 |
'cases' => [ |
| 300 |
[ 'iterations' => [ 1, 2, 3 ], |
| 301 |
'message' => 'Lint Error One' ], |
| 302 |
[ 'iterations' => [ 25, 28, 29, 31, 32, 33 ], |
| 303 |
'message' => 'LintErrorTwo' ], |
| 304 |
[ 'iterations' => [ 0, 4, 5, 12, 13, 14, 15, 16, 17, 22, 23, 24, 26, 27, 30 ], |
| 305 |
'message' => 'table_pager_empty' ], |
| 306 |
[ 'iterations' => [ 6, 7, 8, 9, 10, 11, 18, 19, 20, 21, 34, 35 ], |
| 307 |
'message' => 'linter-namespace-mismatch' ] |
| 308 |
] |
| 309 |
]; |
| 310 |
$testConfigurations[ 4 ] = [ |
| 311 |
'namespaces' => [ 0, 3 ], |
| 312 |
'titles' => [ ':Lint Error One' ], |
| 313 |
'cases' => [ |
| 314 |
[ 'iterations' => [ 0, 1 ], |
| 315 |
'message' => 'Lint Error One' ], |
| 316 |
[ 'iterations' => [ 2, 3 ], |
| 317 |
'message' => 'linter-namespace-mismatch' ] |
| 318 |
] |
| 319 |
]; |
| 320 |
$testConfigurations[ 5 ] = [ |
| 321 |
'namespaces' => [ 0, 3 ], |
| 322 |
'titles' => [ 'FooBar:ErrorFive' ], |
| 323 |
'cases' => [ |
| 324 |
[ 'iterations' => [ 2, 3 ], |
| 325 |
'message' => 'FooBar:ErrorFive' ], |
| 326 |
[ 'iterations' => [ 0, 1 ], |
| 327 |
'message' => 'table_pager_empty' ], |
| 328 |
] |
| 329 |
]; |
| 330 |
|
| 331 |
$testConfigurations[ 6 ] = [ |
| 332 |
'namespaces' => [ [ 0, 3 ] ], |
| 333 |
'titles' => [ 'L', 'Lint Error One', 'LintErrorTwo' ], |
| 334 |
'cases' => [ |
| 335 |
[ 'iterations' => [ 1, 2, 3 ], |
| 336 |
'message' => 'Lint Error One' ], |
| 337 |
[ 'iterations' => [ 1, 4, 5 ], |
| 338 |
'message' => 'LintErrorTwo' ], |
| 339 |
[ 'iterations' => [ 0 ], |
| 340 |
'message' => 'table_pager_empty' ], |
| 341 |
] |
| 342 |
]; |
| 343 |
} else { |
| 344 |
$testConfigurations[ 7 ] = [ |
| 345 |
'namespaces' => [ 0, 3 ], |
| 346 |
'titles' => [ 'L', 'Lint Error One', 'NotANamespace:L' ], |
| 347 |
'cases' => [ |
| 348 |
[ 'iterations' => [ 1, 2, 3 ], 'message' => 'title="Lint Error One">' ], |
| 349 |
[ 'iterations' => [], 'message' => 'title="LintErrorTwo">' ], |
| 350 |
[ 'iterations' => [ 5 ], 'message' => 'title="NotANamespace:LintErrorThree">' ], |
| 351 |
[ 'iterations' => [ 5 ], 'message' => 'title="NotANamespace:LintErrorFour">' ], |
| 352 |
[ 'iterations' => [ 0, 4, 6, 7, 8, 9, 10, 11 ], 'message' => '(table_pager_empty)' ] |
| 353 |
] |
| 354 |
]; |
| 355 |
$testConfigurations[ 8 ] = [ |
| 356 |
'namespaces' => [ [ 0, 3 ] ], |
| 357 |
'titles' => [ 'L', 'Lint Error One', 'NotANamespace:L', 'ErrorSix' ], |
| 358 |
'cases' => [ |
| 359 |
[ 'iterations' => [ 1, 2, 3 ], 'message' => 'title="Lint Error One">' ], |
| 360 |
[ 'iterations' => [], 'message' => 'title="LintErrorTwo">' ], |
| 361 |
[ 'iterations' => [ 5 ], 'message' => 'title="NotANamespace:LintErrorThree">' ], |
| 362 |
[ 'iterations' => [ 5 ], 'message' => 'title="NotANamespace:LintErrorFour">' ], |
| 363 |
[ 'iterations' => [ 6, 7 ], 'message' => ':ErrorSix">' ], |
| 364 |
[ 'iterations' => [ 0, 4 ], 'message' => '(table_pager_empty)' ] |
| 365 |
] |
| 366 |
]; |
| 367 |
|
| 368 |
} |
| 369 |
return $testConfigurations; |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
private function performLinterSearchTests( array $testConfig, ?string $subPage, string $titleSearchString ): void { |
| 380 |
foreach ( $testConfig as $groupIndex => $group ) { |
| 381 |
$testIndex = 0; |
| 382 |
foreach ( $group[ 'namespaces' ] as $namespace ) { |
| 383 |
foreach ( $group[ 'titles' ] as $title ) { |
| 384 |
$exact = true; |
| 385 |
do { |
| 386 |
if ( $namespace === null ) { |
| 387 |
$params = [ $titleSearchString => $title, 'exactmatch' => $exact ]; |
| 388 |
} else { |
| 389 |
if ( is_array( $namespace ) ) { |
| 390 |
|
| 391 |
$namespaces = implode( "\r\n", $namespace ); |
| 392 |
$params = array_merge( [ 'wpNamespaceRestrictions' => $namespaces ], |
| 393 |
[ $titleSearchString => $title, 'exactmatch' => $exact ] ); |
| 394 |
} else { |
| 395 |
$params = [ 'wpNamespaceRestrictions' => $namespace, $titleSearchString => $title, |
| 396 |
'exactmatch' => $exact ]; |
| 397 |
} |
| 398 |
} |
| 399 |
$webRequest = new FauxRequest( $params ); |
| 400 |
$html = $this->executeSpecialPage( $subPage, $webRequest, 'qqx' )[ 0 ]; |
| 401 |
|
| 402 |
foreach ( $group[ 'cases' ] as $caseIndex => $case ) { |
| 403 |
$exactString = [ 'prefix', 'exact' ][ $exact ]; |
| 404 |
$message = $case[ 'message' ]; |
| 405 |
$descriptionNamespace = implode( ',', (array)$namespace ); |
| 406 |
$description = "On group [$groupIndex], namespace [$descriptionNamespace], " . |
| 407 |
"case [$caseIndex], iteration [$testIndex] " . |
| 408 |
"for a [$exactString] match with search title [$title] and test text [$message] "; |
| 409 |
|
| 410 |
if ( in_array( $testIndex, $case[ 'iterations' ] ) ) { |
| 411 |
if ( empty( $debugTests ) ) { |
| 412 |
$this->assertStringContainsString( $message, $html, $description . |
| 413 |
"was not found." ); |
| 414 |
} else { |
| 415 |
|
| 416 |
if ( !str_contains( $html, $message ) ) { |
| 417 |
echo $description . "was not found.\n"; |
| 418 |
} |
| 419 |
} |
| 420 |
} else { |
| 421 |
if ( empty( $debugTests ) ) { |
| 422 |
$this->assertStringNotContainsString( $message, $html, $description . |
| 423 |
"was not supposed to be found." ); |
| 424 |
} else { |
| 425 |
|
| 426 |
if ( str_contains( $html, $message ) ) { |
| 427 |
echo $description . "was not supposed to be found.\n"; |
| 428 |
} |
| 429 |
} |
| 430 |
} |
| 431 |
} |
| 432 |
$testIndex++; |
| 433 |
$exact = !$exact; |
| 434 |
} while ( !$exact ); |
| 435 |
} |
| 436 |
} |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
public function testLinterSearchVariations(): void { |
| 444 |
$this->createTitleAndPage(); |
| 445 |
$pageData = $this->createTitleAndPageAndLintErrorData(); |
| 446 |
$this->createPagesWithLintErrorsFromData( $pageData ); |
| 447 |
|
| 448 |
$testConfigurations = $this->createLinterSearchTestConfigurations( null ); |
| 449 |
$this->performLinterSearchTests( $testConfigurations, null, 'titlesearch' ); |
| 450 |
|
| 451 |
$testConfigurations = $this->createLinterSearchTestConfigurations( 'misnested-tag' ); |
| 452 |
$this->performLinterSearchTests( $testConfigurations, 'misnested-tag', 'titlecategorysearch' ); |
| 453 |
} |
| 454 |
} |