2021-03-15 22:53:32 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Tests\Unit;
|
|
|
|
|
|
2022-05-27 16:38:32 +00:00
|
|
|
use MediaWiki\Content\Renderer\ContentParseParams;
|
2022-08-24 12:55:03 +00:00
|
|
|
use MediaWiki\Languages\LanguageNameUtils;
|
2022-12-09 12:28:41 +00:00
|
|
|
use MediaWiki\Parser\MagicWordFactory;
|
2022-05-27 16:38:32 +00:00
|
|
|
use MediaWiki\Parser\Parsoid\ParsoidParser;
|
|
|
|
|
use MediaWiki\Parser\Parsoid\ParsoidParserFactory;
|
2021-03-15 22:53:32 +00:00
|
|
|
use MediaWiki\Revision\SlotRecord;
|
|
|
|
|
use MediaWiki\Revision\SlotRenderingProvider;
|
2023-03-01 20:33:26 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2022-08-24 12:55:03 +00:00
|
|
|
use MediaWiki\Title\TitleFactory;
|
2021-03-15 22:53:32 +00:00
|
|
|
use MediaWikiUnitTestCase;
|
|
|
|
|
use MWException;
|
2022-05-27 16:38:32 +00:00
|
|
|
use Parser;
|
2022-08-24 12:55:03 +00:00
|
|
|
use ParserFactory;
|
2022-05-27 16:38:32 +00:00
|
|
|
use ParserOptions;
|
|
|
|
|
use ParserOutput;
|
|
|
|
|
use ReflectionClass;
|
2022-08-24 12:55:03 +00:00
|
|
|
use Wikimedia\UUID\GlobalIdGenerator;
|
2021-03-15 22:53:32 +00:00
|
|
|
use WikitextContent;
|
|
|
|
|
use WikitextContentHandler;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Split from \WikitextContentHandlerTest integration tests
|
|
|
|
|
*
|
|
|
|
|
* @group ContentHandler
|
|
|
|
|
* @coversDefaultClass \WikitextContentHandler
|
|
|
|
|
*/
|
|
|
|
|
class WikitextContentHandlerTest extends MediaWikiUnitTestCase {
|
|
|
|
|
|
2022-05-27 16:38:32 +00:00
|
|
|
private function newWikitextContentHandler( $overrides = [] ): WikitextContentHandler {
|
2022-08-24 12:55:03 +00:00
|
|
|
return new WikitextContentHandler(
|
|
|
|
|
CONTENT_MODEL_WIKITEXT,
|
2022-05-27 16:38:32 +00:00
|
|
|
$overrides[TitleFactory::class] ?? $this->createMock( TitleFactory::class ),
|
|
|
|
|
$overrides[ParserFactory::class] ?? $this->createMock( ParserFactory::class ),
|
|
|
|
|
$overrides[GlobalIdGenerator::class] ?? $this->createMock( GlobalIdGenerator::class ),
|
|
|
|
|
$overrides[LanguageNameUtils::class] ?? $this->createMock( LanguageNameUtils::class ),
|
|
|
|
|
$overrides[MagicWordFactory::class] ?? $this->createMock( MagicWordFactory::class ),
|
|
|
|
|
$overrides[ParsoidParserFactory::class] ?? $this->createMock( ParsoidParserFactory::class )
|
2022-08-24 12:55:03 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-15 22:53:32 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ::serializeContent
|
|
|
|
|
*/
|
|
|
|
|
public function testSerializeContent() {
|
|
|
|
|
$content = new WikitextContent( 'hello world' );
|
2022-08-24 12:55:03 +00:00
|
|
|
$handler = $this->newWikitextContentHandler();
|
2021-03-15 22:53:32 +00:00
|
|
|
|
|
|
|
|
$this->assertEquals( 'hello world', $handler->serializeContent( $content ) );
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'hello world',
|
|
|
|
|
$handler->serializeContent( $content, CONTENT_FORMAT_WIKITEXT )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->expectException( MWException::class );
|
|
|
|
|
$handler->serializeContent( $content, 'dummy/foo' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ::unserializeContent
|
|
|
|
|
*/
|
|
|
|
|
public function testUnserializeContent() {
|
2022-08-24 12:55:03 +00:00
|
|
|
$handler = $this->newWikitextContentHandler();
|
2021-03-15 22:53:32 +00:00
|
|
|
|
|
|
|
|
$content = $handler->unserializeContent( 'hello world' );
|
|
|
|
|
$this->assertEquals( 'hello world', $content->getText() );
|
|
|
|
|
|
|
|
|
|
$content = $handler->unserializeContent( 'hello world', CONTENT_FORMAT_WIKITEXT );
|
|
|
|
|
$this->assertEquals( 'hello world', $content->getText() );
|
|
|
|
|
|
|
|
|
|
$this->expectException( MWException::class );
|
|
|
|
|
$handler->unserializeContent( 'hello world', 'dummy/foo' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers WikitextContentHandler::makeEmptyContent
|
|
|
|
|
*/
|
|
|
|
|
public function testMakeEmptyContent() {
|
2022-08-24 12:55:03 +00:00
|
|
|
$handler = $this->newWikitextContentHandler();
|
2021-03-15 22:53:32 +00:00
|
|
|
$content = $handler->makeEmptyContent();
|
|
|
|
|
|
|
|
|
|
$this->assertTrue( $content->isEmpty() );
|
|
|
|
|
$this->assertSame( '', $content->getText() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function dataIsSupportedFormat() {
|
|
|
|
|
return [
|
|
|
|
|
[ null, true ],
|
|
|
|
|
[ CONTENT_FORMAT_WIKITEXT, true ],
|
|
|
|
|
[ 99887766, false ],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider dataIsSupportedFormat
|
|
|
|
|
* @covers ::isSupportedFormat
|
|
|
|
|
*/
|
|
|
|
|
public function testIsSupportedFormat( $format, $supported ) {
|
2022-08-24 12:55:03 +00:00
|
|
|
$handler = $this->newWikitextContentHandler();
|
2021-03-15 22:53:32 +00:00
|
|
|
$this->assertEquals( $supported, $handler->isSupportedFormat( $format ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ::supportsDirectEditing
|
|
|
|
|
*/
|
|
|
|
|
public function testSupportsDirectEditing() {
|
2022-08-24 12:55:03 +00:00
|
|
|
$handler = $this->newWikiTextContentHandler();
|
2021-03-15 22:53:32 +00:00
|
|
|
$this->assertTrue( $handler->supportsDirectEditing(), 'direct editing is supported' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ::getSecondaryDataUpdates
|
|
|
|
|
*/
|
|
|
|
|
public function testGetSecondaryDataUpdates() {
|
|
|
|
|
$title = $this->createMock( Title::class );
|
|
|
|
|
$content = new WikitextContent( '' );
|
|
|
|
|
$srp = $this->createMock( SlotRenderingProvider::class );
|
2022-08-24 12:55:03 +00:00
|
|
|
$handler = $this->newWikitextContentHandler();
|
2021-03-15 22:53:32 +00:00
|
|
|
|
|
|
|
|
$updates = $handler->getSecondaryDataUpdates( $title, $content, SlotRecord::MAIN, $srp );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( [], $updates );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ::getDeletionUpdates
|
|
|
|
|
*/
|
|
|
|
|
public function testGetDeletionUpdates() {
|
|
|
|
|
$title = $this->createMock( Title::class );
|
2022-08-24 12:55:03 +00:00
|
|
|
$handler = $this->newWikitextContentHandler();
|
2021-03-15 22:53:32 +00:00
|
|
|
|
|
|
|
|
$updates = $handler->getDeletionUpdates( $title, SlotRecord::MAIN );
|
|
|
|
|
$this->assertEquals( [], $updates );
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-27 16:38:32 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ::fillParserOutput
|
|
|
|
|
* @dataProvider provideFillParserOutput
|
|
|
|
|
*/
|
|
|
|
|
public function testFillParserOutput( $useParsoid = true ) {
|
|
|
|
|
$parserOptions = $this->createMock( ParserOptions::class );
|
|
|
|
|
$parserOptions
|
|
|
|
|
->method( 'getUseParsoid' )
|
|
|
|
|
->willReturn( $useParsoid );
|
|
|
|
|
|
|
|
|
|
// This is the core of the test: if the useParsoid option is NOT
|
|
|
|
|
// present, we expect ParserFactory->getInstance()->parse()
|
|
|
|
|
// to be called exactly once, otherwise never.
|
|
|
|
|
$parser = $this->createMock( Parser::class );
|
|
|
|
|
$parser
|
|
|
|
|
->expects( $useParsoid ? $this->never() : $this->once() )
|
|
|
|
|
->method( 'parse' );
|
|
|
|
|
$parserFactory = $this->createMock( ParserFactory::class );
|
|
|
|
|
$parserFactory
|
|
|
|
|
->expects( $useParsoid ? $this->never() : $this->once() )
|
|
|
|
|
->method( 'getInstance' )
|
|
|
|
|
->willReturn( $parser );
|
|
|
|
|
|
|
|
|
|
// If the useParsoid option is present, we expect
|
|
|
|
|
// ParsoidParserFactory()->create()->parse() to be called
|
|
|
|
|
// exactly once, otherwise never.
|
|
|
|
|
$parsoidParser = $this->createMock( ParsoidParser::class );
|
|
|
|
|
$parsoidParser
|
|
|
|
|
->expects( $useParsoid ? $this->once() : $this->never() )
|
|
|
|
|
->method( 'parse' );
|
|
|
|
|
$parsoidParserFactory = $this->createMock( ParsoidParserFactory::class );
|
|
|
|
|
$parsoidParserFactory
|
|
|
|
|
->expects( $useParsoid ? $this->once() : $this->never() )
|
|
|
|
|
->method( 'create' )
|
|
|
|
|
->willReturn( $parsoidParser );
|
|
|
|
|
|
|
|
|
|
// Set up the rest of the mocks
|
|
|
|
|
$content = $this->createMock( WikitextContent::class );
|
|
|
|
|
$content
|
|
|
|
|
->method( 'getRedirectTargetAndText' )
|
|
|
|
|
->willReturn( [ false, '* Hello, world!' ] );
|
|
|
|
|
$content
|
|
|
|
|
->method( 'getPreSaveTransformFlags' )
|
|
|
|
|
->willReturn( [] );
|
|
|
|
|
|
|
|
|
|
$title = $this->createMock( Title::class );
|
|
|
|
|
$titleFactory = $this->createMock( TitleFactory::class );
|
|
|
|
|
$titleFactory
|
2023-04-22 13:57:00 +00:00
|
|
|
->method( 'newFromPageReference' )
|
2022-05-27 16:38:32 +00:00
|
|
|
->willReturn( $title );
|
|
|
|
|
|
|
|
|
|
$cpoParams = new ContentParseParams( $title, 42, $parserOptions );
|
|
|
|
|
|
|
|
|
|
$parserOutput = $this->createMock( ParserOutput::class );
|
|
|
|
|
|
|
|
|
|
// The method we'd like to test, fillParserOutput, is protected;
|
|
|
|
|
// make it public
|
|
|
|
|
$class = new ReflectionClass( WikitextContentHandler::class );
|
|
|
|
|
$method = $class->getMethod( 'fillParserOutput' );
|
|
|
|
|
$method->setAccessible( true );
|
|
|
|
|
|
|
|
|
|
$handler = $this->newWikitextContentHandler( [
|
|
|
|
|
TitleFactory::class => $titleFactory,
|
|
|
|
|
ParserFactory::class => $parserFactory,
|
|
|
|
|
ParsoidParserFactory::class => $parsoidParserFactory,
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
// Okay, invoke fillParserOutput() and verify that the assertions
|
|
|
|
|
// above about the parse() invocations are correct.
|
|
|
|
|
$method->invokeArgs( $handler, [ $content, $cpoParams, &$parserOutput ] );
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-19 22:32:30 +00:00
|
|
|
public static function provideFillParserOutput() {
|
2022-05-27 16:38:32 +00:00
|
|
|
return [ [ false ], [ true ] ];
|
|
|
|
|
}
|
2021-03-15 22:53:32 +00:00
|
|
|
}
|