| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Content; |
| 4 |
|
| 5 |
use MediaWiki\Content\JavaScriptContent; |
| 6 |
use MediaWiki\Content\TextContent; |
| 7 |
use MediaWiki\Content\WikitextContent; |
| 8 |
use MediaWiki\Deferred\LinksUpdate\LinksDeletionUpdate; |
| 9 |
use MediaWiki\Parser\Parser; |
| 10 |
use MediaWiki\Parser\ParserOptions; |
| 11 |
use MediaWiki\Parser\ParserOutputFlags; |
| 12 |
use MediaWiki\Title\Title; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
class WikitextContentTest extends TextContentTest { |
| 22 |
use ContentSerializationTestTrait; |
| 23 |
|
| 24 |
public const SECTIONS = "Intro |
| 25 |
|
| 26 |
== stuff == |
| 27 |
hello world |
| 28 |
|
| 29 |
== test == |
| 30 |
just a test |
| 31 |
|
| 32 |
== foo == |
| 33 |
more stuff |
| 34 |
"; |
| 35 |
|
| 36 |
public function newContent( $text ) { |
| 37 |
return new WikitextContent( $text ); |
| 38 |
} |
| 39 |
|
| 40 |
public static function dataGetSection() { |
| 41 |
return [ |
| 42 |
[ self::SECTIONS, |
| 43 |
"0", |
| 44 |
"Intro" |
| 45 |
], |
| 46 |
[ self::SECTIONS, |
| 47 |
"2", |
| 48 |
"== test == |
| 49 |
just a test" |
| 50 |
], |
| 51 |
[ self::SECTIONS, |
| 52 |
"8", |
| 53 |
false |
| 54 |
], |
| 55 |
]; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public function testGetSection( $text, $sectionId, $expectedText ) { |
| 62 |
$content = $this->newContent( $text ); |
| 63 |
|
| 64 |
$sectionContent = $content->getSection( $sectionId ); |
| 65 |
if ( is_object( $sectionContent ) ) { |
| 66 |
$sectionText = $sectionContent->getText(); |
| 67 |
} else { |
| 68 |
$sectionText = $sectionContent; |
| 69 |
} |
| 70 |
|
| 71 |
$this->assertEquals( $expectedText, $sectionText ); |
| 72 |
} |
| 73 |
|
| 74 |
public static function dataReplaceSection() { |
| 75 |
return [ |
| 76 |
[ self::SECTIONS, |
| 77 |
"0", |
| 78 |
"No more", |
| 79 |
null, |
| 80 |
trim( preg_replace( '/^Intro/m', 'No more', self::SECTIONS ) ) |
| 81 |
], |
| 82 |
[ self::SECTIONS, |
| 83 |
"", |
| 84 |
"No more", |
| 85 |
null, |
| 86 |
"No more" |
| 87 |
], |
| 88 |
[ self::SECTIONS, |
| 89 |
"2", |
| 90 |
"== TEST ==\nmore fun", |
| 91 |
null, |
| 92 |
trim( preg_replace( |
| 93 |
'/^== test ==.*== foo ==/sm', "== TEST ==\nmore fun\n\n== foo ==", |
| 94 |
self::SECTIONS |
| 95 |
) ) |
| 96 |
], |
| 97 |
[ self::SECTIONS, |
| 98 |
"8", |
| 99 |
"No more", |
| 100 |
null, |
| 101 |
self::SECTIONS |
| 102 |
], |
| 103 |
[ self::SECTIONS, |
| 104 |
"new", |
| 105 |
"No more", |
| 106 |
"New", |
| 107 |
trim( self::SECTIONS ) . "\n\n\n== New ==\n\nNo more" |
| 108 |
], |
| 109 |
]; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public function testReplaceSection( $text, $section, $with, $sectionTitle, $expected ) { |
| 116 |
$content = $this->newContent( $text ); |
| 117 |
|
| 118 |
$c = $content->replaceSection( $section, $this->newContent( $with ), $sectionTitle ); |
| 119 |
|
| 120 |
$this->assertEquals( $expected, $c ? $c->getText() : null ); |
| 121 |
} |
| 122 |
|
| 123 |
public function testAddSectionHeader() { |
| 124 |
$content = $this->newContent( 'hello world' ); |
| 125 |
$content = $content->addSectionHeader( 'test' ); |
| 126 |
$this->assertEquals( "== test ==\n\nhello world", $content->getText() ); |
| 127 |
|
| 128 |
$content = $this->newContent( 'hello world' ); |
| 129 |
$content = $content->addSectionHeader( '' ); |
| 130 |
$this->assertEquals( "hello world", $content->getText() ); |
| 131 |
} |
| 132 |
|
| 133 |
public static function dataPreSaveTransform() { |
| 134 |
return [ |
| 135 |
[ 'hello this is ~~~', |
| 136 |
"hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]", |
| 137 |
], |
| 138 |
[ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>', |
| 139 |
'hello \'\'this\'\' is <nowiki>~~~</nowiki>', |
| 140 |
], |
| 141 |
[ |
| 142 |
" Foo \n ", |
| 143 |
" Foo", |
| 144 |
], |
| 145 |
]; |
| 146 |
} |
| 147 |
|
| 148 |
public static function dataGetRedirectTarget() { |
| 149 |
return [ |
| 150 |
[ '#REDIRECT [[Test]]', |
| 151 |
'Test', |
| 152 |
], |
| 153 |
[ '#REDIRECT Test', |
| 154 |
null, |
| 155 |
], |
| 156 |
[ '* #REDIRECT [[Test]]', |
| 157 |
null, |
| 158 |
], |
| 159 |
]; |
| 160 |
} |
| 161 |
|
| 162 |
public static function dataGetTextForSummary() { |
| 163 |
return [ |
| 164 |
[ "hello\nworld.", |
| 165 |
16, |
| 166 |
'hello world.', |
| 167 |
], |
| 168 |
[ 'hello world.', |
| 169 |
8, |
| 170 |
'hello...', |
| 171 |
], |
| 172 |
[ '[[hello world]].', |
| 173 |
8, |
| 174 |
'hel...', |
| 175 |
], |
| 176 |
]; |
| 177 |
} |
| 178 |
|
| 179 |
public static function dataIsCountable() { |
| 180 |
return [ |
| 181 |
[ '', |
| 182 |
null, |
| 183 |
'any', |
| 184 |
true |
| 185 |
], |
| 186 |
[ 'Foo', |
| 187 |
null, |
| 188 |
'any', |
| 189 |
true |
| 190 |
], |
| 191 |
[ 'Foo', |
| 192 |
null, |
| 193 |
'link', |
| 194 |
false |
| 195 |
], |
| 196 |
[ 'Foo [[bar]]', |
| 197 |
null, |
| 198 |
'link', |
| 199 |
true |
| 200 |
], |
| 201 |
[ 'Foo', |
| 202 |
true, |
| 203 |
'link', |
| 204 |
true |
| 205 |
], |
| 206 |
[ 'Foo [[bar]]', |
| 207 |
false, |
| 208 |
'link', |
| 209 |
false |
| 210 |
], |
| 211 |
[ '#REDIRECT [[bar]]', |
| 212 |
true, |
| 213 |
'any', |
| 214 |
false |
| 215 |
], |
| 216 |
[ '#REDIRECT [[bar]]', |
| 217 |
true, |
| 218 |
'link', |
| 219 |
false |
| 220 |
], |
| 221 |
]; |
| 222 |
} |
| 223 |
|
| 224 |
public function testMatchMagicWord() { |
| 225 |
$mw = $this->getServiceContainer()->getMagicWordFactory()->get( "staticredirect" ); |
| 226 |
|
| 227 |
$content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" ); |
| 228 |
$this->assertTrue( $content->matchMagicWord( $mw ), "should have matched magic word" ); |
| 229 |
|
| 230 |
$content = $this->newContent( "#REDIRECT [[FOO]]" ); |
| 231 |
$this->assertFalse( |
| 232 |
$content->matchMagicWord( $mw ), |
| 233 |
"should not have matched magic word" |
| 234 |
); |
| 235 |
} |
| 236 |
|
| 237 |
public function testUpdateRedirect() { |
| 238 |
$target = Title::makeTitle( NS_MAIN, 'TestUpdateRedirect_target' ); |
| 239 |
|
| 240 |
|
| 241 |
$content = $this->newContent( "hello world." ); |
| 242 |
$newContent = $content->updateRedirect( $target ); |
| 243 |
|
| 244 |
$this->assertTrue( $content->equals( $newContent ), "content should be unchanged" ); |
| 245 |
|
| 246 |
|
| 247 |
$content = $this->newContent( "#REDIRECT [[Someplace]]" ); |
| 248 |
$newContent = $content->updateRedirect( $target ); |
| 249 |
|
| 250 |
$this->assertFalse( $content->equals( $newContent ), "content should have changed" ); |
| 251 |
$this->assertTrue( $newContent->isRedirect(), "new content should be a redirect" ); |
| 252 |
|
| 253 |
$this->assertEquals( |
| 254 |
$target->getFullText(), |
| 255 |
$newContent->getRedirectTarget()->getFullText() |
| 256 |
); |
| 257 |
} |
| 258 |
|
| 259 |
public function testGetModel() { |
| 260 |
$content = $this->newContent( "hello world." ); |
| 261 |
|
| 262 |
$this->assertEquals( CONTENT_MODEL_WIKITEXT, $content->getModel() ); |
| 263 |
} |
| 264 |
|
| 265 |
public function testGetContentHandler() { |
| 266 |
$content = $this->newContent( "hello world." ); |
| 267 |
|
| 268 |
$this->assertEquals( CONTENT_MODEL_WIKITEXT, $content->getContentHandler()->getModelID() ); |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
public function testRedirectParserOption() { |
| 275 |
$title = Title::makeTitle( NS_MAIN, 'TestRedirectParserOption' ); |
| 276 |
$contentRenderer = $this->getServiceContainer()->getContentRenderer(); |
| 277 |
|
| 278 |
|
| 279 |
$wikitext = null; |
| 280 |
$redirectTarget = null; |
| 281 |
$this->mergeMwGlobalArrayValue( 'wgHooks', [ |
| 282 |
'InternalParseBeforeLinks' => [ |
| 283 |
static function ( Parser $parser, $text, $stripState ) use ( &$wikitext, &$redirectTarget ) { |
| 284 |
$wikitext = $text; |
| 285 |
$redirectTarget = $parser->getOptions()->getRedirectTarget(); |
| 286 |
} |
| 287 |
] |
| 288 |
] ); |
| 289 |
|
| 290 |
|
| 291 |
$wikitext = false; |
| 292 |
$redirectTarget = false; |
| 293 |
$content = $this->newContent( 'hello world.' ); |
| 294 |
$options = ParserOptions::newFromAnon(); |
| 295 |
$options->setRedirectTarget( $title ); |
| 296 |
$contentRenderer->getParserOutput( $content, $title, null, $options ); |
| 297 |
$this->assertEquals( 'hello world.', $wikitext, |
| 298 |
'Wikitext passed to hook was not as expected' |
| 299 |
); |
| 300 |
$this->assertNull( $redirectTarget, 'Redirect seen in hook was not null' ); |
| 301 |
$this->assertEquals( $title, $options->getRedirectTarget(), |
| 302 |
'ParserOptions\' redirectTarget was changed' |
| 303 |
); |
| 304 |
|
| 305 |
|
| 306 |
$wikitext = false; |
| 307 |
$redirectTarget = false; |
| 308 |
$content = $this->newContent( |
| 309 |
"#REDIRECT [[TestRedirectParserOption/redir]]\nhello redirect." |
| 310 |
); |
| 311 |
$options = ParserOptions::newFromAnon(); |
| 312 |
$contentRenderer->getParserOutput( $content, $title, null, $options ); |
| 313 |
$this->assertEquals( |
| 314 |
'hello redirect.', |
| 315 |
$wikitext, |
| 316 |
'Wikitext passed to hook was not as expected' |
| 317 |
); |
| 318 |
$this->assertNotEquals( |
| 319 |
null, |
| 320 |
$redirectTarget, |
| 321 |
'Redirect seen in hook was null' ); |
| 322 |
$this->assertEquals( |
| 323 |
'TestRedirectParserOption/redir', |
| 324 |
$redirectTarget->getFullText(), |
| 325 |
'Redirect seen in hook was not the expected title' |
| 326 |
); |
| 327 |
$this->assertNull( |
| 328 |
$options->getRedirectTarget(), |
| 329 |
'ParserOptions\' redirectTarget was changed' |
| 330 |
); |
| 331 |
} |
| 332 |
|
| 333 |
public static function dataEquals() { |
| 334 |
return [ |
| 335 |
[ new WikitextContent( "hallo" ), null, false ], |
| 336 |
[ new WikitextContent( "hallo" ), new WikitextContent( "hallo" ), true ], |
| 337 |
[ new WikitextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ], |
| 338 |
[ new WikitextContent( "hallo" ), new TextContent( "hallo" ), false ], |
| 339 |
[ new WikitextContent( "hallo" ), new WikitextContent( "HALLO" ), false ], |
| 340 |
]; |
| 341 |
} |
| 342 |
|
| 343 |
public static function dataGetDeletionUpdates() { |
| 344 |
return [ |
| 345 |
[ |
| 346 |
CONTENT_MODEL_WIKITEXT, "hello ''world''\n", |
| 347 |
[ LinksDeletionUpdate::class => [] ] |
| 348 |
], |
| 349 |
[ |
| 350 |
CONTENT_MODEL_WIKITEXT, "hello [[world test 21344]]\n", |
| 351 |
[ LinksDeletionUpdate::class => [] ] |
| 352 |
], |
| 353 |
|
| 354 |
]; |
| 355 |
} |
| 356 |
|
| 357 |
public static function getClassToTest(): string { |
| 358 |
return WikitextContent::class; |
| 359 |
} |
| 360 |
|
| 361 |
public static function getTestInstancesAndAssertions(): array { |
| 362 |
|
| 363 |
|
| 364 |
$pstFlags = [ |
| 365 |
ParserOutputFlags::SHOW_TOC, |
| 366 |
ParserOutputFlags::VARY_REVISION, |
| 367 |
]; |
| 368 |
$withPstFlags = new WikitextContent( 'with PST flags' ); |
| 369 |
$withPstFlags->setPreSaveTransformFlags( $pstFlags ); |
| 370 |
|
| 371 |
return [ |
| 372 |
'basic' => [ |
| 373 |
'instance' => new WikitextContent( 'hello' ), |
| 374 |
'assertions' => static function ( $testCase, $obj ) { |
| 375 |
$testCase->assertInstanceof( WikitextContent::class, $obj ); |
| 376 |
$testCase->assertSame( 'hello', $obj->getText() ); |
| 377 |
$testCase->assertArrayEquals( [], $obj->getPreSaveTransformFlags() ); |
| 378 |
}, |
| 379 |
], |
| 380 |
'withPstFlags' => [ |
| 381 |
'instance' => $withPstFlags, |
| 382 |
'assertions' => static function ( $testCase, $obj ) use ( $pstFlags ) { |
| 383 |
$testCase->assertInstanceof( WikitextContent::class, $obj ); |
| 384 |
$testCase->assertSame( 'with PST flags', $obj->getText() ); |
| 385 |
$testCase->assertArrayEquals( $pstFlags, $obj->getPreSaveTransformFlags() ); |
| 386 |
}, |
| 387 |
], |
| 388 |
]; |
| 389 |
} |
| 390 |
} |