Debian Codegraph @

extensions/Linter/tests/phpunit/SpecialLintErrorsTest.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 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 * @covers \MediaWiki\Linter\SpecialLintErrors
34 *
35 * @group Database
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 // Basic
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 // Verify new tag and template interfaces are present
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 * @param string $titleText
89 * @param int|null $ns
90 * @return array
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 // This test recreates the bug mentioned in T280193 of not
166 // calling the onRevisionFromEditComplete hook with the "mw-contentmodelchange"
167 // tag set when the new content text is literally blank.
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 * @param array $pageData
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 * @return array
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 // namespaces specified: all, Main, Talk and User talk (defined in config as null, int 0, 1 and 3)
250 // Titles exact matched and searched by tests include: empty - "", "L", "Lint Error One", "User Talk:L",
251 // "User talk:LintErrorTwo", "NotANamespace:L", "NotANamespace:LintErrorThree", "NotANamespace:LintErrorFour"
252 //
253 // Tests are grouped into three categories: empty title for all tests, namespace all for half and User talk for
254 // the rest, with exact match booleans cycling.
255 //
256 // The second group is similar, the title being either "NotANamespace:L" or "NotANamespace:LintErrorThree" or
257 // "NotANamespace:LintErrorFour" with half namespace set all or User talk and exact match booleans
258 // cycling.
259 //
260 // The third group is composed of tests against main, talk, User Talk and all namespaces. This test also includes
261 // titles with and without namespace prefixes, some which match the drop-down namespace and some which conflict
262 // depending on the combination of namespace definitions.
263 //
264 // The forth test covers the use of ':title' (main namespace) as the search text to ensure 'all' and 'main'
265 // are handled properly.
266 //
267 // The fifth test covers the user of an editor defined, (non wiki defined namespace with a namespace ID), but
268 // which was created in the User_talk wiki defined namespace ID 3.
269 //
270 // The sixth test covers accessing the search mechanism through the misnested-tag subpage. It verifies that
271 // LintErrorTwo, which has no misnested-tag errors is not in any search results, but other searches are as expected.
272
273 /**
274 * @param string|null $subpage
275 * @return array
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 // check both NS0 and NS3 at the same time
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 * @param array $testConfig
374 * @param string|null $subPage
375 * @param string $titleSearchString
376 * @return void
377 * @throws Exception
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 // simulate the same URL string that the multi namespace widget produces
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 // code to aid in debugging test conditions
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 // code to aid in debugging test conditions
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 * @throws Exception
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 }