wiki.techinc.nl/tests/phpunit/integration/includes/parser/Parsoid/ParsoidParserTest.php
Isabelle Hurbain-Palatin a3cf629d2f Remove ParserOutput::getText() calls from core (direct pipeline)
This is the second patch of a series of patches to remove
ParserOutput::getText() calls from core. This series of patches should
be functionally equivalent to I2b4bcddb234f10fd8592570cb0496adf3271328e.

This patch replaces the calls to getText where the legacy parser is
called directly by creating a pipeline and invoking it on the generated.
These should probably eventually use the Content framework to generate
output instead of using Parser directly (T371008), which will also allow
them to transparently support Parsoid.

Bug: T293512
Change-Id: I45951a49e57a8031887ee6e4546335141d231c18
2024-08-23 18:15:00 +02:00

100 lines
2.9 KiB
PHP

<?php
namespace MediaWiki\Tests\Parser\Parsoid;
use MediaWiki\MediaWikiServices;
use MediaWiki\Parser\Parsoid\ParsoidParser;
use MediaWiki\Title\Title;
use MediaWikiIntegrationTestCase;
use ParserOptions;
/**
* @covers \MediaWiki\Parser\Parsoid\ParsoidParser::parse
* @group Database
*/
class ParsoidParserTest extends MediaWikiIntegrationTestCase {
/** @dataProvider provideParsoidParserHtml */
public function testParsoidParserHtml( $args, $expected, $getTextOpts = [] ) {
$parsoidParser = $this->getServiceContainer()
->getParsoidParserFactory()->create();
if ( is_string( $args[1] ?? '' ) ) {
// Make a PageReference from a string
$args[1] = Title::newFromText( $args[1] ?? 'Main Page' );
}
if ( ( $args[2] ?? null ) === null ) {
// Make default ParserOptions if none are provided
$args[2] = ParserOptions::newFromAnon();
}
$output = $parsoidParser->parse( ...$args );
$html = $output->getRawText();
$this->assertStringContainsString( $expected, $html );
$this->assertSame(
$args[1]->getPrefixedDBkey(),
$output->getExtensionData( ParsoidParser::PARSOID_TITLE_KEY )
);
$usedOptions = [
'collapsibleSections',
'disableContentConversion',
'interfaceMessage',
'isPreview',
'maxIncludeSize',
'suppressSectionEditLinks',
'wrapclass',
];
$this->assertEqualsCanonicalizing( $usedOptions, $output->getUsedOptions() );
$pipeline = MediaWikiServices::getInstance()->getDefaultOutputPipeline();
$pipeline->run( $output, $args[2], [] );
$this->assertArrayEquals( $usedOptions, $output->getUsedOptions() );
}
public static function provideParsoidParserHtml() {
return [
[ [ 'Hello, World' ], 'Hello, World' ],
[ [ '__NOTOC__' ], '<meta property="mw:PageProp/notoc"' ],
// Once we support $linestart and other parser options we
// can extend these tests.
];
}
public function testParsoidParseRevisions() {
$helloWorld = 'Hello, World';
$page = $this->getNonexistingTestPage( 'Test' );
$this->editPage( $page, $helloWorld );
$pageTitle = $page->getTitle();
$parsoidParser = $this->getServiceContainer()
->getParsoidParserFactory()->create();
$opts = ParserOptions::newFromAnon();
$output = $parsoidParser->parse(
$helloWorld,
$pageTitle,
$opts,
true,
true,
$page->getRevisionRecord()->getId()
);
$html = $output->getRawText();
$this->assertStringContainsString( $helloWorld, $html );
$this->assertSame(
$pageTitle->getPrefixedDBkey(),
$output->getExtensionData( ParsoidParser::PARSOID_TITLE_KEY )
);
$usedOptions = [
'collapsibleSections',
'disableContentConversion',
'interfaceMessage',
'isPreview',
'maxIncludeSize',
'suppressSectionEditLinks',
'wrapclass',
];
$this->assertArrayEquals( $usedOptions, $output->getUsedOptions() );
$pipeline = MediaWikiServices::getInstance()->getDefaultOutputPipeline();
$pipeline->run( $output, $opts, [] );
$this->assertArrayEquals( $usedOptions, $output->getUsedOptions() );
}
}