| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Parser; |
| 4 |
|
| 5 |
use MediaWiki\Language\Language; |
| 6 |
use MediaWiki\Parser\MagicWord; |
| 7 |
use MediaWiki\Parser\MagicWordArray; |
| 8 |
use MediaWiki\Parser\MagicWordFactory; |
| 9 |
use MediaWikiUnitTestCase; |
| 10 |
use Wikimedia\TestingAccessWrapper; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class MagicWordArrayTest extends MediaWikiUnitTestCase { |
| 17 |
|
| 18 |
private const MAGIC_WORDS = [ |
| 19 |
'notitleconvert' => [ 0, '__NOTITLECONVERT__', '__NOTC__' ], |
| 20 |
'notoc' => [ 0, '__NOTOC__' ], |
| 21 |
'img_thumbnail' => [ 1, 'thumb', 'thumbnail' ], |
| 22 |
'img_upright' => [ 1, 'upright', 'upright=$1', 'upright $1' ], |
| 23 |
'img_width' => [ 1, '$1px' ], |
| 24 |
]; |
| 25 |
|
| 26 |
public function testConstructor() { |
| 27 |
$array = new MagicWordArray( [ 'ID' ], $this->getFactory() ); |
| 28 |
$this->assertSame( [ 'ID' ], $array->getNames() ); |
| 29 |
$this->assertSame( [ '(?i:(?P<a_ID>SYNONYM)|(?P<b_ID>alt\=\$1))', '(?!)' ], |
| 30 |
$array->getBaseRegex() ); |
| 31 |
$this->assertSame( [ '(?i:SYNONYM|alt\=\$1)', '(?!)' ], |
| 32 |
$array->getBaseRegex( false ) ); |
| 33 |
$this->assertSame( 'ID', $array->matchStartToEnd( 'SyNoNyM' ) ); |
| 34 |
} |
| 35 |
|
| 36 |
public function testAdd() { |
| 37 |
$array = new MagicWordArray( [], $this->getFactory() ); |
| 38 |
$array->add( 'ADD' ); |
| 39 |
$this->assertSame( [ 'ADD' ], $array->getNames() ); |
| 40 |
$this->assertSame( [ '(?i:(?P<a_ADD>SYNONYM)|(?P<b_ADD>alt\=\$1))', '(?!)' ], |
| 41 |
$array->getBaseRegex() ); |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public function testMatchStartToEndCaseSensitive( string $input, $expected ) { |
| 48 |
$array = new MagicWordArray( [ 'ID' ], $this->getFactory( true ) ); |
| 49 |
$this->assertSame( $expected, $array->matchStartToEnd( $input ) ); |
| 50 |
} |
| 51 |
|
| 52 |
public function provideMatchStartToEndCaseSensitive() { |
| 53 |
return [ |
| 54 |
'identifier is not automatically valid syntax' => [ 'ID', false ], |
| 55 |
'mismatch' => [ 'unknown', false ], |
| 56 |
'incorrect capitalization' => [ 'synonym', false ], |
| 57 |
'exact match' => [ 'SYNONYM', 'ID' ], |
| 58 |
]; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public function testMatchVariableStartToEnd( |
| 65 |
string $input, |
| 66 |
array $expected = [ false, false ] |
| 67 |
) { |
| 68 |
$array = new MagicWordArray( [ 'ID' ], $this->getFactory() ); |
| 69 |
$this->assertSame( $expected, $array->matchVariableStartToEnd( $input ) ); |
| 70 |
} |
| 71 |
|
| 72 |
public function provideMatchVariableStartToEnd() { |
| 73 |
return [ |
| 74 |
'identifier is not automatically valid syntax' => [ 'ID' ], |
| 75 |
'match' => [ 'SyNoNyM', [ 'ID', false ] ], |
| 76 |
'end does not match' => [ 'SyNoNyMx' ], |
| 77 |
'with empty parameter' => [ 'alt=', [ 'ID', '' ] ], |
| 78 |
'with parameter' => [ 'alt=Description', [ 'ID', 'Description' ] ], |
| 79 |
'whitespace is not ignored' => [ 'alt =' ], |
| 80 |
]; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public function testMatchVariableStartToEndMultiple( |
| 87 |
string $input, |
| 88 |
array $expected = [ false, false ] |
| 89 |
) { |
| 90 |
$array = new MagicWordArray( array_keys( self::MAGIC_WORDS ), $this->getFactory() ); |
| 91 |
$this->assertSame( $expected, $array->matchVariableStartToEnd( $input ) ); |
| 92 |
|
| 93 |
|
| 94 |
$spy = TestingAccessWrapper::newFromObject( $array ); |
| 95 |
$this->assertSame( [ |
| 96 |
'/^(?:(?i:(?P<a_notitleconvert>__NOTITLECONVERT__)|(?P<b_notitleconvert>__NOTC__)|' . |
| 97 |
'(?P<a_notoc>__NOTOC__)))$/JSu', |
| 98 |
'/^(?:(?P<a_img_thumbnail>thumb)|(?P<b_img_thumbnail>thumbnail)|' . |
| 99 |
'(?P<a_img_upright>upright)|(?P<b_img_upright>upright\=(.*?))|(?P<c_img_upright>upright (.*?))|' . |
| 100 |
'(?P<a_img_width>(.*?)px))$/JS', |
| 101 |
], $spy->getVariableStartToEndRegex() ); |
| 102 |
} |
| 103 |
|
| 104 |
public function provideMatchVariableStartToEndMultiple() { |
| 105 |
return [ |
| 106 |
[ 'thumb', [ 'img_thumbnail', false ] ], |
| 107 |
[ 'upright=1.2', [ 'img_upright', '1.2' ] ], |
| 108 |
[ 'upright=', [ 'img_upright', '' ] ], |
| 109 |
[ 'upright', [ 'img_upright', false ] ], |
| 110 |
[ '100px', [ 'img_width', '100' ] ], |
| 111 |
[ 'px', [ 'img_width', '' ] ], |
| 112 |
[ 'PX' ], |
| 113 |
]; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public function testMatchStartAndRemove( |
| 120 |
string $input, |
| 121 |
$expectedMatches, |
| 122 |
?string $expectedText = null |
| 123 |
) { |
| 124 |
$array = new MagicWordArray( [ 'ID' ], $this->getFactory() ); |
| 125 |
$text = $input; |
| 126 |
$this->assertSame( $expectedMatches, $array->matchStartAndRemove( $text ) ); |
| 127 |
$this->assertSame( $expectedText ?? $input, $text ); |
| 128 |
} |
| 129 |
|
| 130 |
public function provideMatchStartAndRemove() { |
| 131 |
return [ |
| 132 |
'identifier is not automatically valid syntax' => [ 'ID', false ], |
| 133 |
'match' => [ 'SyNoNyMx', 'ID', 'x' ], |
| 134 |
'not at the start' => [ 'xSyNoNyMx', false ], |
| 135 |
'this method does not support parameters' => [ 'alt=x', false ], |
| 136 |
'unexpected behavior when used with parameters' => [ 'alt=$1x', 'ID', 'x' ], |
| 137 |
]; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public function testMatchAndRemove( |
| 144 |
string $input, |
| 145 |
array $expectedMatches = [], |
| 146 |
?string $expectedText = null |
| 147 |
) { |
| 148 |
$array = new MagicWordArray( [ 'ID' ], $this->getFactory() ); |
| 149 |
$text = $input; |
| 150 |
$this->assertSame( $expectedMatches, $array->matchAndRemove( $text ) ); |
| 151 |
$this->assertSame( $expectedText ?? $input, $text ); |
| 152 |
} |
| 153 |
|
| 154 |
public function provideMatchAndRemove() { |
| 155 |
return [ |
| 156 |
'identifier is not automatically valid syntax' => [ 'ID' ], |
| 157 |
'two matches' => [ 'xSyNoNyMxSyNoNyMx', [ 'ID' => false ], 'xxx' ], |
| 158 |
'this method does not support parameters' => [ 'xalt=x' ], |
| 159 |
'unexpected behavior when used with parameters' => [ 'xalt=$1x', [ 'ID' => false ], 'xx' ], |
| 160 |
'T321234' => [ "\x83", [] ], |
| 161 |
]; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public function testMatchAndRemoveMultiple( |
| 168 |
string $input, |
| 169 |
array $expectedMatches = [], |
| 170 |
?string $expectedText = null |
| 171 |
) { |
| 172 |
$array = new MagicWordArray( array_keys( self::MAGIC_WORDS ), $this->getFactory() ); |
| 173 |
$text = $input; |
| 174 |
$this->assertSame( $expectedMatches, $array->matchAndRemove( $text ) ); |
| 175 |
$this->assertSame( $expectedText ?? $input, $text ); |
| 176 |
} |
| 177 |
|
| 178 |
public function provideMatchAndRemoveMultiple() { |
| 179 |
return [ |
| 180 |
[ |
| 181 |
'x__NOTC__x__NOTOC__x__NOTITLECONVERT__x', |
| 182 |
[ 'notitleconvert' => false, 'notoc' => false ], |
| 183 |
'xxxx', |
| 184 |
], |
| 185 |
[ |
| 186 |
'__NOTOC__NOTITLECONVERT__NOTOC____NOTOC__', |
| 187 |
[ 'notoc' => false ], |
| 188 |
'NOTITLECONVERT', |
| 189 |
], |
| 190 |
[ |
| 191 |
'[[File:X.png|thumb|Alt]]', |
| 192 |
[ 'img_thumbnail' => false ], |
| 193 |
'[[File:X.png||Alt]]', |
| 194 |
], |
| 195 |
[ |
| 196 |
'[[File:X.png|thumb|100px]]', |
| 197 |
[ 'img_thumbnail' => false ], |
| 198 |
'[[File:X.png||100px]]', |
| 199 |
], |
| 200 |
[ |
| 201 |
'[[File:X.png|upright=1.2|thumbnail]]', |
| 202 |
[ 'img_upright' => false, 'img_thumbnail' => false ], |
| 203 |
|
| 204 |
|
| 205 |
'[[File:X.png|=1.2|nail]]', |
| 206 |
], |
| 207 |
]; |
| 208 |
} |
| 209 |
|
| 210 |
public function testRegexWithDuplicateGroupNames() { |
| 211 |
$array = new MagicWordArray( [ 'ID', 'ID' ], $this->getFactory() ); |
| 212 |
$text = 'SYNONYM'; |
| 213 |
$this->assertSame( [ 'ID', false ], $array->matchVariableStartToEnd( $text ) ); |
| 214 |
$this->assertSame( 'ID', $array->matchStartAndRemove( $text ) ); |
| 215 |
$text = 'SYNONYM'; |
| 216 |
$this->assertSame( [ 'ID' => false ], $array->matchAndRemove( $text ) ); |
| 217 |
} |
| 218 |
|
| 219 |
private function getFactory( ?bool $caseSensitive = null ): MagicWordFactory { |
| 220 |
$language = $this->createNoOpMock( Language::class, [ 'lc' ] ); |
| 221 |
$language->method( 'lc' )->willReturnCallback( static fn ( $s ) => strtolower( $s ) ); |
| 222 |
|
| 223 |
$factory = $this->createNoOpMock( MagicWordFactory::class, [ 'getContentLanguage', 'get' ] ); |
| 224 |
$factory->method( 'getContentLanguage' )->willReturn( $language ); |
| 225 |
$factory->method( 'get' )->willReturnCallback( |
| 226 |
static function ( $id ) use ( $caseSensitive, $language ) { |
| 227 |
$data = self::MAGIC_WORDS[$id] ?? [ 0, 'SYNONYM', 'alt=$1' ]; |
| 228 |
$caseSensitive ??= (bool)$data[0]; |
| 229 |
return new MagicWord( $id, array_slice( $data, 1 ), $caseSensitive, $language ); |
| 230 |
} |
| 231 |
); |
| 232 |
return $factory; |
| 233 |
} |
| 234 |
|
| 235 |
} |