wiki.techinc.nl/tests/phpunit/integration/includes/parser/Parsoid/ParsoidParserTest.php
C. Scott Ananian 8d031bcf87 Add ParserOptions::setCollapsibleSections()
This is a non-default option that will add a <div> wrapper around
section contents to allow client-side collapsing.  This is intended
for use by MobileFrontEnd, but could eventually be enabled for
desktop read views as well.

Since this parser option is in the "cache-varying options" set, any
caller who sets this option will fork the cache for that page, which
is reasonable as the parser options sets a ParserOutput property.
In the future our caching strategy will get smarter and we'll add
code which avoids the cache split and just transfers the appropriate
values from ParserOptions to ParserOutput flags after the cached
output is retrieved.

Bug: T359001
Change-Id: Ie93959a056ed15a728404eb293e4bb6eeaeb15c0
2024-04-29 12:11:09 -04:00

88 lines
2.4 KiB
PHP

<?php
namespace MediaWiki\Tests\Parser\Parsoid;
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->getText( $getTextOpts );
$this->assertStringContainsString( $expected, $html );
$this->assertSame(
$args[1]->getPrefixedDBkey(),
$output->getExtensionData( ParsoidParser::PARSOID_TITLE_KEY )
);
$this->assertArrayEquals( [
'disableContentConversion',
'interfaceMessage',
'wrapclass',
'suppressSectionEditLinks',
'isPreview',
'maxIncludeSize',
'collapsibleSections',
], $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();
$output = $parsoidParser->parse(
$helloWorld,
$pageTitle,
ParserOptions::newFromAnon(),
true,
true,
$page->getRevisionRecord()->getId()
);
$html = $output->getText();
$this->assertStringContainsString( $helloWorld, $html );
$this->assertSame(
$pageTitle->getPrefixedDBkey(),
$output->getExtensionData( ParsoidParser::PARSOID_TITLE_KEY )
);
$this->assertArrayEquals( [
'collapsibleSections',
'disableContentConversion',
'interfaceMessage',
'isPreview',
'maxIncludeSize',
'suppressSectionEditLinks',
'wrapclass',
], $output->getUsedOptions() );
}
}