Debian Codegraph @

tests/phpunit/includes/preferences/SignatureValidatorTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\MainConfigNames;
4 use MediaWiki\Parser\ParserOptions;
5 use MediaWiki\Preferences\SignatureValidator;
6 use MediaWiki\Registration\ExtensionRegistry;
7 use Wikimedia\TestingAccessWrapper;
8
9 /**
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 *
25 * @file
26 */
27
28 /**
29 * @group Preferences
30 * @group Database
31 */
32 class SignatureValidatorTest extends MediaWikiIntegrationTestCase {
33
34 /** @var SignatureValidator */
35 private $validator;
36
37 protected function setUp(): void {
38 parent::setUp();
39 $this->overrideConfigValue( MainConfigNames::ParsoidSettings, [
40 'linting' => true
41 ] );
42 // For testing SignatureAllowedLintErrors in ::testValidateSignature
43 $this->overrideConfigValue( MainConfigNames::SignatureAllowedLintErrors, [
44 // No allowed lint errors in default set up
45 ] );
46 // For testing hidden category support in ::testValidateSignature
47 $this->overrideConfigValue( 'LinterCategories', [
48 // No hidden categories in default set up
49 ] );
50 $extReg = $this->createMock( ExtensionRegistry::class );
51 $extReg->method( 'isLoaded' )->willReturnCallback( static function ( string $which ) {
52 return $which == 'Linter';
53 } );
54 $this->setService( 'ExtensionRegistry', $extReg );
55 $this->validator = $this->getSignatureValidator();
56 }
57
58 /**
59 * Get a basic SignatureValidator for testing with.
60 * @return SignatureValidator
61 */
62 protected function getSignatureValidator() {
63 $services = $this->getServiceContainer();
64 $lang = $services->getLanguageFactory()->getLanguage( 'en' );
65 $user = $services->getUserFactory()->newFromName( 'SignatureValidatorTest' );
66 $validator = $services->getSignatureValidatorFactory()->newSignatureValidator(
67 $user,
68 null,
69 ParserOptions::newFromUserAndLang( $user, $lang )
70 );
71
72 return TestingAccessWrapper::newFromObject( $validator );
73 }
74
75 /**
76 * @covers \MediaWiki\Preferences\SignatureValidator::applyPreSaveTransform()
77 * @dataProvider provideApplyPreSaveTransform
78 */
79 public function testApplyPreSaveTransform( $signature, $expected ) {
80 $pstSig = $this->validator->applyPreSaveTransform( $signature );
81 $this->assertSame( $expected, $pstSig );
82 }
83
84 public static function provideApplyPreSaveTransform() {
85 return [
86 'Pipe trick' =>
87 [ '[[test|]]', '[[test|test]]' ],
88 'One level substitution' =>
89 [ '{{subst:uc:whatever}}', 'WHATEVER' ],
90 'Hidden nested substitution' =>
91 [ '{{subst:uc:{}}{{subst:uc:{subst:uc:}}}{{subst:uc:}}}', false ],
92 'Hidden nested signature' =>
93 [ '{{subst:uc:~~}}{{subst:uc:~~}}', false ],
94 ];
95 }
96
97 /**
98 * @covers \MediaWiki\Preferences\SignatureValidator::checkUserLinks()
99 * @dataProvider provideCheckUserLinks
100 */
101 public function testCheckUserLinks( $signature, $expected ) {
102 $isValid = $this->validator->checkUserLinks( $signature );
103 $this->assertSame( $expected, $isValid );
104 }
105
106 public static function provideCheckUserLinks() {
107 return [
108 'Perfect' =>
109 [ '[[User:SignatureValidatorTest|Signature]] ([[User talk:SignatureValidatorTest|talk]])', true ],
110 'User link' =>
111 [ '[[User:SignatureValidatorTest|Signature]]', true ],
112 'User talk link' =>
113 [ '[[User talk:SignatureValidatorTest]]', true ],
114 'Contributions link' =>
115 [ '[[Special:Contributions/SignatureValidatorTest]]', true ],
116 'Silly formatting permitted' =>
117 [ '[[_uSeR :_signatureValidatorTest_]]', true ],
118 'Contributions of wrong user' =>
119 [ '[[Special:Contributions/SignatureValidatorTestNot]]', false ],
120 'Link to subpage only' =>
121 [ '[[User:SignatureValidatorTest/blah|Signature]]', false ],
122 ];
123 }
124
125 /**
126 * @covers \MediaWiki\Preferences\SignatureValidator::checkLintErrors()
127 * @dataProvider provideCheckLintErrors
128 */
129 public function testCheckLintErrors( $signature, $expected ) {
130 $errors = $this->validator->checkLintErrors( $signature );
131 $this->assertSame( $expected, $errors );
132 }
133
134 public static function provideCheckLintErrors() {
135 yield 'Perfect' => [ '<strong>Foo</strong>', [] ];
136 yield 'Unclosed tag' => [
137 '<strong>Foo',
138 [
139 [
140 'type' => 'missing-end-tag',
141 'dsr' => [ 0, 11, 8, 0 ],
142 'templateInfo' => null,
143 'params' => [
144 'name' => 'strong',
145 'inTable' => false,
146 ]
147 ]
148 ]
149 ];
150 }
151
152 /**
153 * @covers \MediaWiki\Preferences\SignatureValidator::validateSignature()
154 * @dataProvider provideValidateSignature
155 */
156 public function testValidateSignature( string $signature, $expected ) {
157 $result = $this->validator->validateSignature( $signature );
158 if ( is_string( $expected ) ) {
159 // All special cases should report errors here.
160 $expected = true;
161 }
162 $this->assertSame( $expected, $result );
163 }
164
165 /**
166 * @covers \MediaWiki\Preferences\SignatureValidator::validateSignature()
167 * @dataProvider provideValidateSignature
168 */
169 public function testValidateSignatureAllowed( string $signature, $expected ) {
170 $this->overrideConfigValue( MainConfigNames::SignatureAllowedLintErrors, [
171 'obsolete-tag'
172 ] );
173 $this->validator = $this->getSignatureValidator();
174 $result = $this->validator->validateSignature( $signature );
175 if ( $expected === 'allowed' ) {
176 $expected = false;
177 } elseif ( is_string( $expected ) ) {
178 $expected = true;
179 }
180 $this->assertSame( $expected, $result );
181 }
182
183 public function provideValidateSignature() {
184 yield 'Perfect' => [
185 '[[User:SignatureValidatorTest|Signature]] ([[User talk:SignatureValidatorTest|talk]])',
186 // no complaints from lint
187 false
188 ];
189 yield 'Missing end tag' => [
190 '<span>[[User:SignatureValidatorTest|Signature]] ([[User talk:SignatureValidatorTest|talk]])',
191 // missing-end-tag is never allowed
192 true
193 ];
194 yield 'Obsolete tag' => [
195 '<font color="red">RED</font> [[User:SignatureValidatorTest|Signature]] ([[User talk:SignatureValidatorTest|talk]])',
196 // This is allowed by SignatureAllowedLintErrors
197 'allowed'
198 ];
199 }
200
201 /**
202 * @covers \MediaWiki\Preferences\SignatureValidator::checkLineBreaks()
203 * @dataProvider provideCheckLineBreaks
204 */
205 public function testCheckLineBreaks( $signature, $expected ) {
206 $isValid = $this->validator->checkLineBreaks( $signature );
207 $this->assertSame( $expected, $isValid );
208 }
209
210 public static function provideCheckLineBreaks() {
211 return [
212 'Perfect' =>
213 [ '[[User:SignatureValidatorTest|Signature]] ([[User talk:SignatureValidatorTest|talk]])', true ],
214 'Line break' =>
215 [ "[[User:SignatureValidatorTest|Signature]] ([[User talk:SignatureValidatorTest|talk\n]])", false ],
216 ];
217 }
218
219 }