| 1 |
<?php |
| 2 |
declare( strict_types = 1 ); |
| 3 |
|
| 4 |
namespace MediaWiki\Tests\Content; |
| 5 |
|
| 6 |
use MediaWiki\Content\Content; |
| 7 |
use MediaWiki\Content\JavaScriptContent; |
| 8 |
use MediaWiki\Content\TextContent; |
| 9 |
use MediaWiki\Content\WikitextContent; |
| 10 |
use MediaWiki\Context\RequestContext; |
| 11 |
use MediaWiki\MainConfigNames; |
| 12 |
use MediaWiki\Title\Title; |
| 13 |
use MediaWiki\User\User; |
| 14 |
use MediaWikiLangTestCase; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
class TextContentTest extends MediaWikiLangTestCase { |
| 25 |
|
| 26 |
protected $context; |
| 27 |
|
| 28 |
protected function setUp(): void { |
| 29 |
parent::setUp(); |
| 30 |
|
| 31 |
|
| 32 |
$user = new User(); |
| 33 |
$user->setName( '127.0.0.1' ); |
| 34 |
|
| 35 |
$this->context = new RequestContext(); |
| 36 |
$this->context->setTitle( Title::makeTitle( NS_MAIN, 'Test' ) ); |
| 37 |
$this->context->setUser( $user ); |
| 38 |
|
| 39 |
RequestContext::getMain()->setTitle( $this->context->getTitle() ); |
| 40 |
|
| 41 |
$this->overrideConfigValues( [ |
| 42 |
MainConfigNames::TextModelsToParse => [ |
| 43 |
CONTENT_MODEL_WIKITEXT, |
| 44 |
CONTENT_MODEL_CSS, |
| 45 |
CONTENT_MODEL_JAVASCRIPT, |
| 46 |
], |
| 47 |
MainConfigNames::CapitalLinks => true, |
| 48 |
] ); |
| 49 |
$this->clearHook( 'ContentGetParserOutput' ); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public function newContent( $text ) { |
| 59 |
return new TextContent( $text ); |
| 60 |
} |
| 61 |
|
| 62 |
public static function dataGetRedirectTarget() { |
| 63 |
return [ |
| 64 |
[ '#REDIRECT [[Test]]', |
| 65 |
null, |
| 66 |
], |
| 67 |
]; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public function testGetRedirectTarget( $text, $expected ) { |
| 74 |
$content = $this->newContent( $text ); |
| 75 |
$t = $content->getRedirectTarget(); |
| 76 |
|
| 77 |
if ( $expected === null ) { |
| 78 |
$this->assertNull( $t, "text should not have generated a redirect target: $text" ); |
| 79 |
} else { |
| 80 |
$this->assertEquals( $expected, $t->getPrefixedText() ); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public function testIsRedirect( $text, $expected ) { |
| 88 |
$content = $this->newContent( $text ); |
| 89 |
|
| 90 |
$this->assertEquals( $expected !== null, $content->isRedirect() ); |
| 91 |
} |
| 92 |
|
| 93 |
public static function dataIsCountable() { |
| 94 |
return [ |
| 95 |
[ '', |
| 96 |
null, |
| 97 |
'any', |
| 98 |
true |
| 99 |
], |
| 100 |
[ 'Foo', |
| 101 |
null, |
| 102 |
'any', |
| 103 |
true |
| 104 |
], |
| 105 |
]; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public function testIsCountable( $text, $hasLinks, $mode, $expected ) { |
| 112 |
$this->overrideConfigValue( MainConfigNames::ArticleCountMethod, $mode ); |
| 113 |
|
| 114 |
$content = $this->newContent( $text ); |
| 115 |
|
| 116 |
$v = $content->isCountable( $hasLinks ); |
| 117 |
|
| 118 |
$this->assertEquals( |
| 119 |
$expected, |
| 120 |
$v, |
| 121 |
'isCountable() returned unexpected value ' . var_export( $v, true ) |
| 122 |
. ' instead of ' . var_export( $expected, true ) |
| 123 |
. " in mode `$mode` for text \"$text\"" |
| 124 |
); |
| 125 |
} |
| 126 |
|
| 127 |
public static function dataGetTextForSummary() { |
| 128 |
return [ |
| 129 |
[ "hello\nworld.", |
| 130 |
16, |
| 131 |
'hello world.', |
| 132 |
], |
| 133 |
[ 'hello world.', |
| 134 |
8, |
| 135 |
'hello...', |
| 136 |
], |
| 137 |
[ '[[hello world]].', |
| 138 |
8, |
| 139 |
'[[hel...', |
| 140 |
], |
| 141 |
]; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public function testGetTextForSummary( $text, $maxlength, $expected ) { |
| 148 |
$content = $this->newContent( $text ); |
| 149 |
|
| 150 |
$this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) ); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public function testCopy() { |
| 156 |
$content = $this->newContent( 'hello world.' ); |
| 157 |
$copy = $content->copy(); |
| 158 |
|
| 159 |
$this->assertTrue( $content->equals( $copy ), 'copy must be equal to original' ); |
| 160 |
$this->assertEquals( 'hello world.', $copy->getText() ); |
| 161 |
} |
| 162 |
|
| 163 |
public function testGetTextMethods() { |
| 164 |
$content = $this->newContent( 'hello world.' ); |
| 165 |
|
| 166 |
$this->assertEquals( 12, $content->getSize() ); |
| 167 |
$this->assertEquals( 'hello world.', $content->getText() ); |
| 168 |
$this->assertEquals( 'hello world.', $content->getTextForSearchIndex() ); |
| 169 |
$this->assertEquals( 'hello world.', $content->getNativeData() ); |
| 170 |
$this->assertEquals( 'hello world.', $content->getWikitextForTransclusion() ); |
| 171 |
} |
| 172 |
|
| 173 |
public function testGetModel() { |
| 174 |
$content = $this->newContent( "hello world." ); |
| 175 |
|
| 176 |
$this->assertEquals( CONTENT_MODEL_TEXT, $content->getModel() ); |
| 177 |
} |
| 178 |
|
| 179 |
public function testGetContentHandler() { |
| 180 |
$content = $this->newContent( "hello world." ); |
| 181 |
|
| 182 |
$this->assertEquals( CONTENT_MODEL_TEXT, $content->getContentHandler()->getModelID() ); |
| 183 |
} |
| 184 |
|
| 185 |
public static function dataIsEmpty() { |
| 186 |
return [ |
| 187 |
[ '', true ], |
| 188 |
[ ' ', false ], |
| 189 |
[ '0', false ], |
| 190 |
[ 'hallo welt.', false ], |
| 191 |
]; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public function testIsEmpty( $text, $empty ) { |
| 198 |
$content = $this->newContent( $text ); |
| 199 |
|
| 200 |
$this->assertEquals( $empty, $content->isEmpty() ); |
| 201 |
} |
| 202 |
|
| 203 |
public static function dataEquals() { |
| 204 |
return [ |
| 205 |
[ new TextContent( "hallo" ), null, false ], |
| 206 |
[ new TextContent( "hallo" ), new TextContent( "hallo" ), true ], |
| 207 |
[ new TextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ], |
| 208 |
[ new TextContent( "hallo" ), new WikitextContent( "hallo" ), false ], |
| 209 |
[ new TextContent( "hallo" ), new TextContent( "HALLO" ), false ], |
| 210 |
]; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public function testEquals( Content $a, ?Content $b = null, $equal = false ) { |
| 217 |
$this->assertEquals( $equal, $a->equals( $b ) ); |
| 218 |
} |
| 219 |
|
| 220 |
public static function provideConvert() { |
| 221 |
return [ |
| 222 |
[ |
| 223 |
'Hallo Welt', |
| 224 |
CONTENT_MODEL_WIKITEXT, |
| 225 |
'lossless', |
| 226 |
'Hallo Welt' |
| 227 |
], |
| 228 |
[ |
| 229 |
'Hallo Welt', |
| 230 |
CONTENT_MODEL_WIKITEXT, |
| 231 |
'lossless', |
| 232 |
'Hallo Welt' |
| 233 |
], |
| 234 |
[ |
| 235 |
'Hallo Welt', |
| 236 |
CONTENT_MODEL_CSS, |
| 237 |
'lossless', |
| 238 |
'Hallo Welt' |
| 239 |
], |
| 240 |
[ |
| 241 |
'Hallo Welt', |
| 242 |
CONTENT_MODEL_JAVASCRIPT, |
| 243 |
'lossless', |
| 244 |
'Hallo Welt' |
| 245 |
], |
| 246 |
]; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
public function testConvert( $text, $model, $lossy, $expectedNative ) { |
| 253 |
$content = $this->newContent( $text ); |
| 254 |
|
| 255 |
|
| 256 |
$converted = $content->convert( $model, $lossy ); |
| 257 |
|
| 258 |
if ( $expectedNative === false ) { |
| 259 |
$this->assertFalse( $converted, "conversion to $model was expected to fail!" ); |
| 260 |
} else { |
| 261 |
$this->assertInstanceOf( Content::class, $converted ); |
| 262 |
$this->assertEquals( $expectedNative, $converted->getText() ); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
public function testNormalizeLineEndings( $input, $expected ) { |
| 270 |
$this->assertEquals( $expected, TextContent::normalizeLineEndings( $input ) ); |
| 271 |
} |
| 272 |
|
| 273 |
public static function provideNormalizeLineEndings() { |
| 274 |
return [ |
| 275 |
[ |
| 276 |
"Foo\r\nbar", |
| 277 |
"Foo\nbar" |
| 278 |
], |
| 279 |
[ |
| 280 |
"Foo\rbar", |
| 281 |
"Foo\nbar" |
| 282 |
], |
| 283 |
[ |
| 284 |
"Foobar\n ", |
| 285 |
"Foobar" |
| 286 |
] |
| 287 |
]; |
| 288 |
} |
| 289 |
|
| 290 |
public function testSerialize() { |
| 291 |
$cnt = $this->newContent( 'testing text' ); |
| 292 |
|
| 293 |
$this->assertSame( 'testing text', $cnt->serialize() ); |
| 294 |
} |
| 295 |
|
| 296 |
} |
| 297 |
class_alias( TextContentTest::class, 'TextContentTest' ); |