| 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 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
class SignatureValidatorTest extends MediaWikiIntegrationTestCase { |
| 33 |
|
| 34 |
|
| 35 |
private $validator; |
| 36 |
|
| 37 |
protected function setUp(): void { |
| 38 |
parent::setUp(); |
| 39 |
$this->overrideConfigValue( MainConfigNames::ParsoidSettings, [ |
| 40 |
'linting' => true |
| 41 |
] ); |
| 42 |
|
| 43 |
$this->overrideConfigValue( MainConfigNames::SignatureAllowedLintErrors, [ |
| 44 |
|
| 45 |
] ); |
| 46 |
|
| 47 |
$this->overrideConfigValue( 'LinterCategories', [ |
| 48 |
|
| 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 |
|
| 60 |
|
| 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 |
|
| 77 |
|
| 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 |
|
| 99 |
|
| 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 |
|
| 127 |
|
| 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 |
|
| 154 |
|
| 155 |
|
| 156 |
public function testValidateSignature( string $signature, $expected ) { |
| 157 |
$result = $this->validator->validateSignature( $signature ); |
| 158 |
if ( is_string( $expected ) ) { |
| 159 |
|
| 160 |
$expected = true; |
| 161 |
} |
| 162 |
$this->assertSame( $expected, $result ); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 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 |
|
| 187 |
false |
| 188 |
]; |
| 189 |
yield 'Missing end tag' => [ |
| 190 |
'<span>[[User:SignatureValidatorTest|Signature]] ([[User talk:SignatureValidatorTest|talk]])', |
| 191 |
|
| 192 |
true |
| 193 |
]; |
| 194 |
yield 'Obsolete tag' => [ |
| 195 |
'<font color="red">RED</font> [[User:SignatureValidatorTest|Signature]] ([[User talk:SignatureValidatorTest|talk]])', |
| 196 |
|
| 197 |
'allowed' |
| 198 |
]; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 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 |
} |