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
|
<?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 Exception;
use MediaWiki\Content\ContentHandler;
use MediaWiki\Linter\RecordLintJob;
use MediaWiki\Linter\SpecialLintErrors;
use MediaWiki\Page\PageReference;
use MediaWiki\Request\FauxRequest;
use MediaWiki\Title\Title;
use SpecialPageTestBase;
/**
* @covers \MediaWiki\Linter\SpecialLintErrors
*
* @group Database
*/
class SpecialLintErrorsTest extends SpecialPageTestBase {
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' )
);
}
protected function newSpecialPage() {
$services = $this->getServiceContainer();
return new SpecialLintErrors(
$services->getNamespaceInfo(),
$services->getTitleParser(),
$services->getLinkCache(),
$services->getPermissionManager(),
$services->get( 'Linter.CategoryManager' ),
$services->get( 'Linter.TotalsLookup' )
);
}
public function testExecute() {
$categoryManager =
$this->getServiceContainer()->get( 'Linter.CategoryManager' );
$category = $categoryManager->getVisibleCategories()[0];
// Basic
$html = $this->executeSpecialPage( '', null, 'qqx' )[0];
$this->assertStringContainsString( '(linterrors-summary)', $html );
$this->assertStringContainsString( "(linter-category-$category)", $html );
$this->assertStringContainsString(
"(linter-category-$category-desc)",
$this->executeSpecialPage( $category, null, 'qqx' )[0]
);
// Verify new tag and template interfaces are present
$html = $this->executeSpecialPage( 'misnested-tag', null, 'qqx' )[0];
$this->assertStringContainsString( 'linter-form-template', $html );
$this->assertStringContainsString( 'linter-form-tag', $html );
}
/**
* @param string $titleText
* @param int|null $ns
* @return array
*/
private function createTitleAndPage(
string $titleText = 'SpecialLintErrorsTest test page',
?int $ns = null
): array {
$ns ??= $this->getDefaultWikitextNS();
$title = Title::newFromText( $titleText, $ns );
$page = $this->getExistingTestPage( $title );
return [
'title' => $title,
'pageID' => $page->getRevisionRecord()->getPageId(),
'revID' => $page->getRevisionRecord()->getID(),
'page' => $page,
];
}
public function testContentModelChange() {
$error = [
'type' => 'obsolete-tag',
'location' => [ 0, 10 ],
'params' => [],
'dbid' => null,
];
$titleAndPage = $this->createTitleAndPage();
$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 );
$cssText = 'css content model change test page content';
$content = ContentHandler::makeContent(
$cssText,
$titleAndPage['title'],
'css'
);
$page = $titleAndPage['page'];
$this->editPage(
$page,
$content,
"update with css content model to trigger onRevisionFromEditComplete hook"
);
$errorsFromDb = array_values( $db->getForPage( $pageId ) );
$this->assertCount( 0, $errorsFromDb );
}
public function testContentModelChangeWithBlankPage() {
$error = [
'type' => 'obsolete-tag',
'location' => [ 0, 10 ],
'params' => [],
'dbid' => null,
];
$titleAndPage = $this->createTitleAndPage();
$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 test recreates the bug mentioned in T280193 of not
// calling the onRevisionFromEditComplete hook with the "mw-contentmodelchange"
// tag set when the new content text is literally blank.
$blankText = '';
$content = ContentHandler::makeContent(
$blankText,
$titleAndPage['title'],
'text'
);
$page = $titleAndPage['page'];
$this->editPage(
$page,
$content,
"update with blank text content model to trigger onRevisionFromEditComplete hook"
);
$errorsFromDb = array_values( $db->getForPage( $pageId ) );
$this->assertCount( 0, $errorsFromDb );
}
/**
* @param array $pageData
*/
private function createPagesWithLintErrorsFromData( array $pageData ) {
foreach ( $pageData as $data ) {
$titleAndPage = $this->createTitleAndPage( $data[ 'name' ], $data[ 'ns' ] );
$errors = [];
foreach ( $data[ 'lintErrors' ] as $lintError ) {
$errors[] = [
'type' => $lintError[ 'type' ],
'location' => $lintError[ 'location' ],
'params' => [],
'dbid' => null
];
}
$job = $this->newRecordLintJob( $titleAndPage[ 'title' ], [
'errors' => $errors,
'revision' => $titleAndPage[ 'revID' ]
] );
$job->run();
}
}
/**
* @return array
*/
private function createTitleAndPageAndLintErrorData(): array {
$pageData = [];
$pageData[] = [ 'name' => 'Lint Error One', 'ns' => 0,
'lintErrors' => [
[ 'type' => 'obsolete-tag', 'location' => [ 0, 10 ] ],
[ 'type' => 'misnested-tag', 'location' => [ 20, 30 ] ]
]
];
$pageData[] = [ 'name' => 'LintErrorTwo', 'ns' => 3,
'lintErrors' => [ [ 'type' => 'obsolete-tag', 'location' => [ 0, 10 ] ] ]
];
$pageData[] = [ 'name' => 'NotANamespace:LintErrorThree', 'ns' => 0,
'lintErrors' => [
[ 'type' => 'obsolete-tag', 'location' => [ 0, 10 ] ],
[ 'type' => 'misnested-tag', 'location' => [ 20, 30 ] ]
]
];
$pageData[] = [ 'name' => 'NotANamespace:LintErrorFour', 'ns' => 0,
'lintErrors' => [
[ 'type' => 'obsolete-tag', 'location' => [ 0, 10 ] ],
[ 'type' => 'misnested-tag', 'location' => [ 20, 30 ] ]
]
];
$pageData[] = [ 'name' => 'Some other page', 'ns' => 0,
'lintErrors' => [ [ 'type' => 'bogus-image-options', 'location' => [ 30, 40 ] ] ]
];
$pageData[] = [ 'name' => 'FooBar:ErrorFive', 'ns' => 3,
'lintErrors' => [ [ 'type' => 'obsolete-tag', 'location' => [ 0, 10 ] ] ]
];
$pageData[] = [ 'name' => 'ErrorSix', 'ns' => 3,
'lintErrors' => [
[ 'type' => 'obsolete-tag', 'location' => [ 0, 10 ] ],
[ 'type' => 'misnested-tag', 'location' => [ 20, 30 ] ]
]
];
return $pageData;
}
// namespaces specified: all, Main, Talk and User talk (defined in config as null, int 0, 1 and 3)
// Titles exact matched and searched by tests include: empty - "", "L", "Lint Error One", "User Talk:L",
// "User talk:LintErrorTwo", "NotANamespace:L", "NotANamespace:LintErrorThree", "NotANamespace:LintErrorFour"
//
// Tests are grouped into three categories: empty title for all tests, namespace all for half and User talk for
// the rest, with exact match booleans cycling.
//
// The second group is similar, the title being either "NotANamespace:L" or "NotANamespace:LintErrorThree" or
// "NotANamespace:LintErrorFour" with half namespace set all or User talk and exact match booleans
// cycling.
//
// The third group is composed of tests against main, talk, User Talk and all namespaces. This test also includes
// titles with and without namespace prefixes, some which match the drop-down namespace and some which conflict
// depending on the combination of namespace definitions.
//
// The forth test covers the use of ':title' (main namespace) as the search text to ensure 'all' and 'main'
// are handled properly.
//
// The fifth test covers the user of an editor defined, (non wiki defined namespace with a namespace ID), but
// which was created in the User_talk wiki defined namespace ID 3.
//
// The sixth test covers accessing the search mechanism through the misnested-tag subpage. It verifies that
// LintErrorTwo, which has no misnested-tag errors is not in any search results, but other searches are as expected.
/**
* @param string|null $subpage
* @return array
*/
private function createLinterSearchTestConfigurations( ?string $subpage ): array {
$testConfigurations = [];
if ( $subpage !== 'misnested-tag' ) {
$testConfigurations[ 1 ] = [
'namespaces' => [ 0, 3 ],
'titles' => [ '' ],
'cases' => [ [ 'iterations' => [ 0, 1, 2, 3 ], 'message' => 'linter-invalid-title' ]
]
];
$testConfigurations[ 2 ] = [
'namespaces' => [ 0, 3 ],
'titles' => [ 'NotANamespace:L', 'NotANamespace:LintErrorFour' ],
'cases' => [
[ 'iterations' => [ 1 ], 'message' => 'NotANamespace:LintErrorThree' ],
[ 'iterations' => [ 1, 2, 3 ], 'message' => 'NotANamespace:LintErrorFour' ],
[ 'iterations' => [ 0, 4, 5, 6, 7 ], 'message' => 'table_pager_empty' ]
]
];
$testConfigurations[ 3 ] = [
'namespaces' => [ 0, 1, 3 ],
'titles' => [ 'L', 'Lint Error One', 'LintErrorTwo', 'User talk:L', 'User talk:LintErrorTwo',
'Talk:L' ],
'cases' => [
[ 'iterations' => [ 1, 2, 3 ],
'message' => 'Lint Error One' ],
[ 'iterations' => [ 25, 28, 29, 31, 32, 33 ],
'message' => 'LintErrorTwo' ],
[ 'iterations' => [ 0, 4, 5, 12, 13, 14, 15, 16, 17, 22, 23, 24, 26, 27, 30 ],
'message' => 'table_pager_empty' ],
[ 'iterations' => [ 6, 7, 8, 9, 10, 11, 18, 19, 20, 21, 34, 35 ],
'message' => 'linter-namespace-mismatch' ]
]
];
$testConfigurations[ 4 ] = [
'namespaces' => [ 0, 3 ],
'titles' => [ ':Lint Error One' ],
'cases' => [
[ 'iterations' => [ 0, 1 ],
'message' => 'Lint Error One' ],
[ 'iterations' => [ 2, 3 ],
'message' => 'linter-namespace-mismatch' ]
]
];
$testConfigurations[ 5 ] = [
'namespaces' => [ 0, 3 ],
'titles' => [ 'FooBar:ErrorFive' ],
'cases' => [
[ 'iterations' => [ 2, 3 ],
'message' => 'FooBar:ErrorFive' ],
[ 'iterations' => [ 0, 1 ],
'message' => 'table_pager_empty' ],
]
];
// check both NS0 and NS3 at the same time
$testConfigurations[ 6 ] = [
'namespaces' => [ [ 0, 3 ] ],
'titles' => [ 'L', 'Lint Error One', 'LintErrorTwo' ],
'cases' => [
[ 'iterations' => [ 1, 2, 3 ],
'message' => 'Lint Error One' ],
[ 'iterations' => [ 1, 4, 5 ],
'message' => 'LintErrorTwo' ],
[ 'iterations' => [ 0 ],
'message' => 'table_pager_empty' ],
]
];
} else {
$testConfigurations[ 7 ] = [
'namespaces' => [ 0, 3 ],
'titles' => [ 'L', 'Lint Error One', 'NotANamespace:L' ],
'cases' => [
[ 'iterations' => [ 1, 2, 3 ], 'message' => 'title="Lint Error One">' ],
[ 'iterations' => [], 'message' => 'title="LintErrorTwo">' ],
[ 'iterations' => [ 5 ], 'message' => 'title="NotANamespace:LintErrorThree">' ],
[ 'iterations' => [ 5 ], 'message' => 'title="NotANamespace:LintErrorFour">' ],
[ 'iterations' => [ 0, 4, 6, 7, 8, 9, 10, 11 ], 'message' => '(table_pager_empty)' ]
]
];
$testConfigurations[ 8 ] = [
'namespaces' => [ [ 0, 3 ] ],
'titles' => [ 'L', 'Lint Error One', 'NotANamespace:L', 'ErrorSix' ],
'cases' => [
[ 'iterations' => [ 1, 2, 3 ], 'message' => 'title="Lint Error One">' ],
[ 'iterations' => [], 'message' => 'title="LintErrorTwo">' ],
[ 'iterations' => [ 5 ], 'message' => 'title="NotANamespace:LintErrorThree">' ],
[ 'iterations' => [ 5 ], 'message' => 'title="NotANamespace:LintErrorFour">' ],
[ 'iterations' => [ 6, 7 ], 'message' => ':ErrorSix">' ],
[ 'iterations' => [ 0, 4 ], 'message' => '(table_pager_empty)' ]
]
];
}
return $testConfigurations;
}
/**
* @param array $testConfig
* @param string|null $subPage
* @param string $titleSearchString
* @return void
* @throws Exception
*/
private function performLinterSearchTests( array $testConfig, ?string $subPage, string $titleSearchString ): void {
foreach ( $testConfig as $groupIndex => $group ) {
$testIndex = 0;
foreach ( $group[ 'namespaces' ] as $namespace ) {
foreach ( $group[ 'titles' ] as $title ) {
$exact = true;
do {
if ( $namespace === null ) {
$params = [ $titleSearchString => $title, 'exactmatch' => $exact ];
} else {
if ( is_array( $namespace ) ) {
// simulate the same URL string that the multi namespace widget produces
$namespaces = implode( "\r\n", $namespace );
$params = array_merge( [ 'wpNamespaceRestrictions' => $namespaces ],
[ $titleSearchString => $title, 'exactmatch' => $exact ] );
} else {
$params = [ 'wpNamespaceRestrictions' => $namespace, $titleSearchString => $title,
'exactmatch' => $exact ];
}
}
$webRequest = new FauxRequest( $params );
$html = $this->executeSpecialPage( $subPage, $webRequest, 'qqx' )[ 0 ];
foreach ( $group[ 'cases' ] as $caseIndex => $case ) {
$exactString = [ 'prefix', 'exact' ][ $exact ];
$message = $case[ 'message' ];
$descriptionNamespace = implode( ',', (array)$namespace );
$description = "On group [$groupIndex], namespace [$descriptionNamespace], " .
"case [$caseIndex], iteration [$testIndex] " .
"for a [$exactString] match with search title [$title] and test text [$message] ";
if ( in_array( $testIndex, $case[ 'iterations' ] ) ) {
if ( empty( $debugTests ) ) {
$this->assertStringContainsString( $message, $html, $description .
"was not found." );
} else {
// code to aid in debugging test conditions
if ( !str_contains( $html, $message ) ) {
echo $description . "was not found.\n";
}
}
} else {
if ( empty( $debugTests ) ) {
$this->assertStringNotContainsString( $message, $html, $description .
"was not supposed to be found." );
} else {
// code to aid in debugging test conditions
if ( str_contains( $html, $message ) ) {
echo $description . "was not supposed to be found.\n";
}
}
}
}
$testIndex++;
$exact = !$exact;
} while ( !$exact );
}
}
}
}
/**
* @throws Exception
*/
public function testLinterSearchVariations(): void {
$this->createTitleAndPage();
$pageData = $this->createTitleAndPageAndLintErrorData();
$this->createPagesWithLintErrorsFromData( $pageData );
$testConfigurations = $this->createLinterSearchTestConfigurations( null );
$this->performLinterSearchTests( $testConfigurations, null, 'titlesearch' );
$testConfigurations = $this->createLinterSearchTestConfigurations( 'misnested-tag' );
$this->performLinterSearchTests( $testConfigurations, 'misnested-tag', 'titlecategorysearch' );
}
}
|