Debian Codegraph @

tests/phpunit/includes/parser/Parsoid/LintErrorCheckerTest.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 use MediaWiki\MainConfigNames;
22 use MediaWiki\Parser\Parsoid\LintErrorChecker;
23 use MediaWiki\Registration\ExtensionRegistry;
24
25 /**
26 * @group Parser
27 * @group Database
28 * @covers \MediaWiki\Parser\Parsoid\LintErrorChecker
29 */
30 class LintErrorCheckerTest extends MediaWikiIntegrationTestCase {
31
32 protected function setUp(): void {
33 parent::setUp();
34 $this->overrideConfigValue( MainConfigNames::ParsoidSettings, [
35 'linting' => true
36 ] );
37 $this->overrideConfigValue( 'LinterCategories', [
38 // No hidden categories in default set up
39 ] );
40 }
41
42 /**
43 * Get a basic LintErrorChecker for testing with.
44 * @return LintErrorChecker
45 */
46 protected function getLintErrorChecker() {
47 $services = $this->getServiceContainer();
48 $extReg = $this->createMock( ExtensionRegistry::class );
49 $extReg->method( 'isLoaded' )->willReturnCallback( static function ( string $which ) {
50 return $which == 'Linter';
51 } );
52
53 return new LintErrorChecker(
54 $services->get( '_Parsoid' ),
55 $services->getParsoidPageConfigFactory(),
56 $services->getTitleFactory(),
57 $extReg,
58 $services->getMainConfig(),
59 );
60 }
61
62 /**
63 * @dataProvider provideCheck
64 */
65 public function testCheck( $wikitext, $expected ) {
66 $errors = $this->getLintErrorChecker()->check( $wikitext );
67 $this->assertSame( $expected, $errors );
68 }
69
70 public static function provideCheck() {
71 yield 'Perfect' => [ '<strong>Foo</strong>', [] ];
72 yield 'Unclosed tag' => [
73 '<strong>Foo',
74 [
75 [
76 'type' => 'missing-end-tag',
77 'dsr' => [ 0, 11, 8, 0 ],
78 'templateInfo' => null,
79 'params' => [
80 'name' => 'strong',
81 'inTable' => false,
82 ]
83 ]
84 ]
85 ];
86 }
87
88 public function testCheckSome() {
89 // Take the same "Unclosed tag" test from above but disable the category
90 $errors = $this->getLintErrorChecker()->checkSome( '<strong>Foo', [ 'missing-end-tag' ] );
91 $this->assertSame( [], $errors );
92 }
93
94 /** Test when categories are diabled in $wgLinterCategories */
95 public function testLinterCategory() {
96 $input = '<font color="red">RED</font>';
97 $errors = $this->getLintErrorChecker()->check( $input );
98 $this->assertEquals( 'obsolete-tag', $errors[0]['type'] );
99
100 // Now disable the category
101 $this->overrideConfigValue( 'LinterCategories', [
102 'obsolete-tag' => [ 'priority' => 'none' ],
103 ] );
104
105 $errors = $this->getLintErrorChecker()->check( $input );
106 $this->assertSame( [], $errors );
107 }
108 }