PortableInfobox/tests/phpunit/MediaWikiParserTest.php

78 lines
2.1 KiB
PHP
Raw Permalink Normal View History

2015-05-07 08:00:18 +00:00
<?php
2021-09-11 23:07:07 +00:00
use MediaWiki\MediaWikiServices;
2018-08-08 09:42:22 +00:00
/**
* @group PortableInfobox
* @covers PortableInfobox\Parser\MediaWikiParserService
2018-08-08 09:42:22 +00:00
*/
class MediaWikiParserTest extends MediaWikiTestCase {
2015-05-07 08:00:18 +00:00
/** @var Parser */
protected $parser;
2021-09-11 23:07:07 +00:00
public function setUp(): void {
$this->parser = MediaWikiServices::getInstance()->getParser();
$title = Title::newFromText( 'test' );
2021-09-11 23:07:07 +00:00
$user = $this->getTestUser()->getUser();
$options = new ParserOptions( $user );
2021-09-12 20:32:41 +00:00
$options->setOption( 'wrapclass', false );
$this->parser->startExternalParse( $title, $options, 'text', true );
parent::setUp();
}
2021-09-11 23:07:07 +00:00
public function tearDown(): void {
2018-08-16 09:25:53 +00:00
unset( $this->parser );
2018-08-08 09:42:22 +00:00
parent::tearDown();
}
protected function parse( $wikitext, $params, $newline = false ) {
2018-10-02 07:41:19 +00:00
$withVars = $this->parser->replaceVariables(
$wikitext, $this->parser->getPreprocessor()->newCustomFrame( $params )
);
$parserOutput = $this->parser->parse(
$withVars, $this->parser->getTitle(), $this->parser->getOptions(), $newline
);
2018-10-02 07:41:19 +00:00
return preg_replace(
'|{{{.*}}}|Us', '', preg_replace( '|[\n\r]|Us', '', $parserOutput->getText() )
);
}
2015-05-07 08:00:18 +00:00
public function testAsideTagPWrappedDuringParsing() {
$aside = "<aside></aside>";
2021-09-11 23:07:07 +00:00
$result = PortableInfobox\Parser\BlockLevelPass::doBlockLevels( $aside, true );
2021-09-12 23:49:00 +00:00
$this->assertEquals( $aside, $result );
}
/**
* @dataProvider mwParserWrapperDataProvider
*
* @param $wikitext
* @param $params
*/
public function testWrapper( $wikitext, $params, $newline ) {
$frame = $this->parser->getPreprocessor()->newCustomFrame( $params );
$wrapper = new PortableInfobox\Parser\MediaWikiParserService( $this->parser, $frame );
2021-09-12 21:14:18 +00:00
$output = $wrapper->parseRecursive( $wikitext );
$this->assertEquals( $this->parse( $wikitext, $params, $newline ), $output );
}
public function mwParserWrapperDataProvider() {
return [
2018-08-16 09:25:53 +00:00
[ "*1\n*2\n*3", [], true ],
[ "''d''", [], false ],
[ "'''dd'''", [], false ],
[ "#1\n#2\n#3 ksajdlk", [], true ],
[ "{{{test}}}", [ 'test' => 1 ], false ],
2018-08-16 09:25:53 +00:00
[ " :asdf", [], false ],
[ "\n:asdf", [], false ],
[ "\n;asdf", [], false ],
[ "[[asdf]]", [], false ]
];
}
2018-08-16 09:25:53 +00:00
}