1 2 3 4 5 6 7 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
<?php
use MediaWiki\Content\FileContentHandler;
use MediaWiki\Content\WikitextContent;
use MediaWiki\Content\WikitextContentHandler;
use MediaWiki\MainConfigNames;
use MediaWiki\Page\PageReferenceValue;
use MediaWiki\Parser\ParserOptions;
use MediaWiki\Parser\ParserOutput;
use MediaWiki\Parser\ParserOutputFlags;
use MediaWiki\Title\Title;
use MediaWiki\Title\TitleFactory;
use MediaWiki\User\UserIdentity;
use Wikimedia\Rdbms\IDBAccessObject;
use Wikimedia\TestingAccessWrapper;
/**
* See also unit tests at \MediaWiki\Tests\Unit\WikitextContentHandlerTest
*
* @group ContentHandler
* @covers \MediaWiki\Content\WikitextContentHandler
* @covers \MediaWiki\Content\TextContentHandler
* @covers \MediaWiki\Content\ContentHandler
*/
class WikitextContentHandlerTest extends MediaWikiLangTestCase {
private WikitextContentHandler $handler;
protected function setUp(): void {
parent::setUp();
$this->handler = $this->getServiceContainer()->getContentHandlerFactory()
->getContentHandler( CONTENT_MODEL_WIKITEXT );
}
public static function dataMerge3() {
return [
[
"first paragraph
second paragraph\n",
"FIRST paragraph
second paragraph\n",
"first paragraph
SECOND paragraph\n",
"FIRST paragraph
SECOND paragraph\n",
],
[ "first paragraph
second paragraph\n",
"Bla bla\n",
"Blubberdibla\n",
false,
],
];
}
/**
* @dataProvider dataMerge3
*/
public function testMerge3( $old, $mine, $yours, $expected ) {
$this->markTestSkippedIfNoDiff3();
// test merge
$oldContent = new WikitextContent( $old );
$myContent = new WikitextContent( $mine );
$yourContent = new WikitextContent( $yours );
$merged = $this->handler->merge3( $oldContent, $myContent, $yourContent );
$this->assertEquals( $expected, $merged ? $merged->getText() : $merged );
}
public static function dataGetAutosummary() {
return [
[
'Hello there, world!',
'#REDIRECT [[Foo]]',
0,
'/^Redirected page .*Foo/'
],
[
null,
'Hello world!',
EDIT_NEW,
'/^Created page .*Hello/'
],
[
null,
'',
EDIT_NEW,
'/^Created blank page$/'
],
[
'Hello there, world!',
'',
0,
'/^Blanked/'
],
[
'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
'Hello world!',
0,
'/^Replaced .*Hello/'
],
[
'foo',
'bar',
0,
'/^$/'
],
];
}
/**
* @dataProvider dataGetAutosummary
*/
public function testGetAutosummary( $old, $new, $flags, $expected ) {
$oldContent = $old === null ? null : new WikitextContent( $old );
$newContent = $new === null ? null : new WikitextContent( $new );
$summary = $this->handler->getAutosummary( $oldContent, $newContent, $flags );
$this->assertTrue(
(bool)preg_match( $expected, $summary ),
"Autosummary didn't match expected pattern $expected: $summary"
);
}
public static function dataGetChangeTag() {
return [
[
null,
'#REDIRECT [[Foo]]',
0,
'mw-new-redirect'
],
[
'Lorem ipsum dolor',
'#REDIRECT [[Foo]]',
0,
'mw-new-redirect'
],
[
'#REDIRECT [[Foo]]',
'Lorem ipsum dolor',
0,
'mw-removed-redirect'
],
[
'#REDIRECT [[Foo]]',
'#REDIRECT [[Bar]]',
0,
'mw-changed-redirect-target'
],
[
null,
'Lorem ipsum dolor',
EDIT_NEW,
null // mw-newpage is not defined as a tag
],
[
null,
'',
EDIT_NEW,
null // mw-newblank is not defined as a tag
],
[
'Lorem ipsum dolor',
'',
0,
'mw-blank'
],
[
'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
'Ipsum',
0,
'mw-replace'
],
[
'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
'Duis purus odio, rhoncus et finibus dapibus, facilisis ac urna. Pellentesque
arcu, tristique nec tempus nec, suscipit vel arcu. Sed non dolor nec ligula
congue tempor. Quisque pellentesque finibus orci a molestie. Nam maximus, purus
euismod finibus mollis, dui ante malesuada felis, dignissim rutrum diam sapien.',
0,
null
],
];
}
/**
* @dataProvider dataGetChangeTag
*/
public function testGetChangeTag( $old, $new, $flags, $expected ) {
$this->overrideConfigValue( MainConfigNames::SoftwareTags, [
'mw-new-redirect' => true,
'mw-removed-redirect' => true,
'mw-changed-redirect-target' => true,
'mw-newpage' => true,
'mw-newblank' => true,
'mw-blank' => true,
'mw-replace' => true,
] );
$oldContent = $old === null ? null : new WikitextContent( $old );
$newContent = $new === null ? null : new WikitextContent( $new );
$tag = $this->handler->getChangeTag( $oldContent, $newContent, $flags );
$this->assertSame( $expected, $tag );
}
public function testGetFieldsForSearchIndex() {
$searchEngine = $this->createMock( SearchEngine::class );
$searchEngine->method( 'makeSearchFieldMapping' )
->willReturnCallback( static function ( $name, $type ) {
return new DummySearchIndexFieldDefinition( $name, $type );
} );
$this->hideDeprecated( 'MediaWiki\\Content\\ContentHandler::getForModelID' );
$fields = $this->handler->getFieldsForSearchIndex( $searchEngine );
$this->assertArrayHasKey( 'category', $fields );
$this->assertArrayHasKey( 'external_link', $fields );
$this->assertArrayHasKey( 'outgoing_link', $fields );
$this->assertArrayHasKey( 'template', $fields );
$this->assertArrayHasKey( 'content_model', $fields );
}
public function testDataIndexFieldsFile() {
$mockEngine = $this->createMock( SearchEngine::class );
$title = Title::makeTitle( NS_FILE, 'Somefile.jpg' );
$page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
// Mark the page as not existent to avoid DB queries.
$pageWrapper = TestingAccessWrapper::newFromObject( $page );
$pageWrapper->mLatest = null;
$pageWrapper->mId = 0;
$pageWrapper->mDataLoadedFrom = IDBAccessObject::READ_NORMAL;
$fileHandler = $this->getMockBuilder( FileContentHandler::class )
->disableOriginalConstructor()
->onlyMethods( [ 'getDataForSearchIndex' ] )
->getMock();
$handler = $this->getMockBuilder( WikitextContentHandler::class )
->disableOriginalConstructor()
->onlyMethods( [ 'getFileHandler' ] )
->getMock();
$handler->method( 'getFileHandler' )->willReturn( $fileHandler );
$fileHandler->expects( $this->once() )
->method( 'getDataForSearchIndex' )
->willReturn( [ 'file_text' => 'This is file content' ] );
$data = $handler->getDataForSearchIndex( $page, new ParserOutput( '' ), $mockEngine );
$this->assertArrayHasKey( 'file_text', $data );
$this->assertEquals( 'This is file content', $data['file_text'] );
}
public function testHadSignature() {
$services = $this->getServiceContainer();
$pageObj = PageReferenceValue::localReference( NS_MAIN, __CLASS__ );
// Force a content model in the converted Title to avoid DB queries.
$title = $services->getTitleFactory()->newFromPageReference( $pageObj );
$title->setContentModel( CONTENT_MODEL_WIKITEXT );
$titleFactory = $this->createMock( TitleFactory::class );
$titleFactory->method( 'newFromPageReference' )
->with( $pageObj )
->willReturn( $title );
$this->setService( 'TitleFactory', $titleFactory );
$contentTransformer = $services->getContentTransformer();
$contentRenderer = $services->getContentRenderer();
$this->hideDeprecated( 'AbstractContent::preSaveTransform' );
$content = new WikitextContent( '~~~~' );
$pstContent = $contentTransformer->preSaveTransform(
$content,
$pageObj,
$this->createMock( UserIdentity::class ),
ParserOptions::newFromAnon()
);
$this->assertTrue( $contentRenderer->getParserOutput( $pstContent, $pageObj )->getOutputFlag(
ParserOutputFlags::USER_SIGNATURE
) );
}
}
|