2012-06-23 18:28:51 +00:00
|
|
|
<?php
|
2019-05-28 14:04:23 +00:00
|
|
|
|
2022-12-12 02:10:13 +00:00
|
|
|
use MediaWiki\Language\RawMessage;
|
2022-08-01 19:14:41 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2019-04-11 13:36:15 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2018-09-20 17:29:04 +00:00
|
|
|
use MediaWiki\Revision\MutableRevisionRecord;
|
|
|
|
|
use MediaWiki\Revision\RevisionStore;
|
|
|
|
|
use MediaWiki\Revision\SlotRecord;
|
2023-03-01 20:33:26 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-09-19 12:13:45 +00:00
|
|
|
use MediaWiki\User\User;
|
2018-09-05 18:03:15 +00:00
|
|
|
use MediaWiki\User\UserIdentityValue;
|
2012-06-23 18:28:51 +00:00
|
|
|
|
2015-04-01 07:35:32 +00:00
|
|
|
/**
|
|
|
|
|
* @group Database
|
2017-02-27 04:44:31 +00:00
|
|
|
* @covers Parser
|
2018-02-06 05:21:37 +00:00
|
|
|
* @covers BlockLevelPass
|
2015-04-01 07:35:32 +00:00
|
|
|
*/
|
2012-06-23 21:04:57 +00:00
|
|
|
class ParserMethodsTest extends MediaWikiLangTestCase {
|
2021-01-11 22:36:33 +00:00
|
|
|
use MockTitleTrait;
|
2012-06-23 18:28:51 +00:00
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
public static function providePreSaveTransform() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ 'hello this is ~~~',
|
2013-02-15 10:24:31 +00:00
|
|
|
"hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
|
2013-02-15 10:24:31 +00:00
|
|
|
'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
];
|
2012-06-23 18:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-10-08 10:56:20 +00:00
|
|
|
* @dataProvider providePreSaveTransform
|
2012-06-23 18:28:51 +00:00
|
|
|
*/
|
|
|
|
|
public function testPreSaveTransform( $text, $expected ) {
|
2022-07-05 22:21:30 +00:00
|
|
|
$title = Title::makeTitle( NS_MAIN, 'TestPreSaveTransform' );
|
2012-06-23 18:28:51 +00:00
|
|
|
$user = new User();
|
|
|
|
|
$user->setName( "127.0.0.1" );
|
|
|
|
|
$popts = ParserOptions::newFromUser( $user );
|
2022-01-12 20:13:39 +00:00
|
|
|
$text = $this->getServiceContainer()->getParser()
|
2019-04-11 13:36:15 +00:00
|
|
|
->preSaveTransform( $text, $title, $user, $popts );
|
2012-06-23 18:28:51 +00:00
|
|
|
|
|
|
|
|
$this->assertEquals( $expected, $text );
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-22 15:21:36 +00:00
|
|
|
public static function provideStripOuterParagraph() {
|
|
|
|
|
// This mimics the most common use case (stripping paragraphs generated by the parser).
|
|
|
|
|
$message = new RawMessage( "Message text." );
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[
|
2014-02-22 15:21:36 +00:00
|
|
|
"<p>Text.</p>",
|
|
|
|
|
"Text.",
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2014-02-22 15:21:36 +00:00
|
|
|
"<p class='foo'>Text.</p>",
|
|
|
|
|
"<p class='foo'>Text.</p>",
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2014-02-22 15:21:36 +00:00
|
|
|
"<p>Text.\n</p>\n",
|
|
|
|
|
"Text.",
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2014-02-22 15:21:36 +00:00
|
|
|
"<p>Text.</p><p>More text.</p>",
|
|
|
|
|
"<p>Text.</p><p>More text.</p>",
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2014-02-22 15:21:36 +00:00
|
|
|
$message->parse(),
|
|
|
|
|
"Message text.",
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
];
|
2014-02-22 15:21:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideStripOuterParagraph
|
|
|
|
|
*/
|
|
|
|
|
public function testStripOuterParagraph( $text, $expected ) {
|
|
|
|
|
$this->assertEquals( $expected, Parser::stripOuterParagraph( $text ) );
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-09 02:52:53 +00:00
|
|
|
public static function provideFormatPageTitle() {
|
|
|
|
|
return [
|
|
|
|
|
"Non-main namespace" => [
|
|
|
|
|
[ 'Talk', ':', 'Hello' ],
|
|
|
|
|
'<span class="mw-page-title-namespace">Talk</span><span class="mw-page-title-separator">:</span><span class="mw-page-title-main">Hello</span>',
|
|
|
|
|
],
|
|
|
|
|
"Main namespace (ignores the separator)" => [
|
|
|
|
|
[ '', ':', 'Hello' ],
|
|
|
|
|
'<span class="mw-page-title-main">Hello</span>',
|
|
|
|
|
],
|
|
|
|
|
"Pieces are HTML-escaped" => [
|
|
|
|
|
[ 'Ta&lk', ':', 'He&llo' ],
|
|
|
|
|
'<span class="mw-page-title-namespace">Ta&lk</span><span class="mw-page-title-separator">:</span><span class="mw-page-title-main">He&llo</span>',
|
|
|
|
|
],
|
|
|
|
|
"In the future, the colon separator could be localized" => [
|
|
|
|
|
[ 'Talk', ' : ', 'Hello' ],
|
|
|
|
|
'<span class="mw-page-title-namespace">Talk</span><span class="mw-page-title-separator"> : </span><span class="mw-page-title-main">Hello</span>',
|
|
|
|
|
],
|
|
|
|
|
"In the future, displaytitle could be customized separately from the namespace" => [
|
|
|
|
|
[ 'Talk', ':', new HtmlArmor( '<span class="whatever">Hello</span>' ) ],
|
|
|
|
|
'<span class="mw-page-title-namespace">Talk</span><span class="mw-page-title-separator">:</span><span class="mw-page-title-main"><span class="whatever">Hello</span></span>',
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideFormatPageTitle
|
|
|
|
|
*/
|
|
|
|
|
public function testFormatPageTitle( $args, $expected ) {
|
|
|
|
|
$this->assertEquals( $expected, Parser::formatPageTitle( ...$args ) );
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-27 20:18:06 +00:00
|
|
|
public function testRecursiveParse() {
|
2022-07-05 22:21:30 +00:00
|
|
|
$title = Title::makeTitle( NS_MAIN, 'Foo' );
|
2022-01-12 20:13:39 +00:00
|
|
|
$parser = $this->getServiceContainer()->getParser();
|
2020-09-18 15:07:18 +00:00
|
|
|
$po = ParserOptions::newFromAnon();
|
2019-04-11 13:36:15 +00:00
|
|
|
$parser->setHook( 'recursivecallparser', [ $this, 'helperParserFunc' ] );
|
2019-10-11 22:22:26 +00:00
|
|
|
$this->expectException( MWException::class );
|
|
|
|
|
$this->expectExceptionMessage(
|
|
|
|
|
"Parser state cleared while parsing. Did you call Parser::parse recursively?"
|
|
|
|
|
);
|
2019-04-11 13:36:15 +00:00
|
|
|
$parser->parse( '<recursivecallparser>baz</recursivecallparser>', $title, $po );
|
2013-10-27 20:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
2014-07-19 21:12:10 +00:00
|
|
|
public function helperParserFunc( $input, $args, $parser ) {
|
2022-07-05 22:21:30 +00:00
|
|
|
$title = Title::makeTitle( NS_MAIN, 'Foo' );
|
2020-09-18 15:07:18 +00:00
|
|
|
$po = ParserOptions::newFromAnon();
|
2013-10-27 20:18:06 +00:00
|
|
|
$parser->parse( $input, $title, $po );
|
|
|
|
|
return 'bar';
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-04 03:35:05 +00:00
|
|
|
public function testCallParserFunction() {
|
|
|
|
|
// Normal parses test passing PPNodes. Test passing an array.
|
2022-07-05 22:21:30 +00:00
|
|
|
$title = Title::makeTitle( NS_MAIN, 'TestCallParserFunction' );
|
2022-01-12 20:13:39 +00:00
|
|
|
$parser = $this->getServiceContainer()->getParser();
|
2020-09-18 15:07:18 +00:00
|
|
|
$parser->startExternalParse(
|
|
|
|
|
$title,
|
|
|
|
|
ParserOptions::newFromAnon(),
|
|
|
|
|
Parser::OT_HTML
|
|
|
|
|
);
|
2019-04-11 13:36:15 +00:00
|
|
|
$frame = $parser->getPreprocessor()->newFrame();
|
|
|
|
|
$ret = $parser->callParserFunction( $frame, '#tag',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'pre', 'foo', 'style' => 'margin-left: 1.6em' ]
|
2013-03-04 03:35:05 +00:00
|
|
|
);
|
2021-02-19 22:01:19 +00:00
|
|
|
$ret['text'] = $parser->getStripState()->unstripBoth( $ret['text'] );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [
|
2013-03-04 03:35:05 +00:00
|
|
|
'found' => true,
|
|
|
|
|
'text' => '<pre style="margin-left: 1.6em">foo</pre>',
|
2016-02-17 09:09:32 +00:00
|
|
|
], $ret, 'callParserFunction works for {{#tag:pre|foo|style=margin-left: 1.6em}}' );
|
2013-03-04 03:35:05 +00:00
|
|
|
}
|
2013-10-09 15:03:40 +00:00
|
|
|
|
2013-10-22 23:50:38 +00:00
|
|
|
/**
|
2017-02-27 04:44:31 +00:00
|
|
|
* @covers Parser
|
2013-10-22 23:50:38 +00:00
|
|
|
* @covers ParserOutput::getSections
|
|
|
|
|
*/
|
2013-10-09 15:03:40 +00:00
|
|
|
public function testGetSections() {
|
2022-09-09 20:18:22 +00:00
|
|
|
$this->overrideConfigValue( MainConfigNames::FragmentMode, [ 'html5' ] );
|
2022-07-05 22:21:30 +00:00
|
|
|
$title = Title::makeTitle( NS_MAIN, 'TestGetSections' );
|
2022-01-12 20:13:39 +00:00
|
|
|
$out = $this->getServiceContainer()->getParser()->parse(
|
2022-09-09 20:18:22 +00:00
|
|
|
"==foo==\n<h2>bar</h2>\n==baz==\n== Romeo+Juliet %A Ó %20 ==\ntest",
|
2020-09-18 15:07:18 +00:00
|
|
|
$title,
|
|
|
|
|
ParserOptions::newFromAnon()
|
|
|
|
|
);
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertSame( [
|
|
|
|
|
[
|
2013-10-09 15:03:40 +00:00
|
|
|
'toclevel' => 1,
|
2022-03-18 16:54:51 +00:00
|
|
|
'level' => '2',
|
2013-10-09 15:03:40 +00:00
|
|
|
'line' => 'foo',
|
|
|
|
|
'number' => '1',
|
|
|
|
|
'index' => '1',
|
|
|
|
|
'fromtitle' => $title->getPrefixedDBkey(),
|
|
|
|
|
'byteoffset' => 0,
|
|
|
|
|
'anchor' => 'foo',
|
2022-09-09 20:18:22 +00:00
|
|
|
'linkAnchor' => 'foo',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2013-10-09 15:03:40 +00:00
|
|
|
'toclevel' => 1,
|
2022-03-18 16:54:51 +00:00
|
|
|
'level' => '2',
|
2013-10-09 15:03:40 +00:00
|
|
|
'line' => 'bar',
|
|
|
|
|
'number' => '2',
|
|
|
|
|
'index' => '',
|
|
|
|
|
'fromtitle' => false,
|
|
|
|
|
'byteoffset' => null,
|
|
|
|
|
'anchor' => 'bar',
|
2022-09-09 20:18:22 +00:00
|
|
|
'linkAnchor' => 'bar',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2013-10-09 15:03:40 +00:00
|
|
|
'toclevel' => 1,
|
2022-03-18 16:54:51 +00:00
|
|
|
'level' => '2',
|
2013-10-09 15:03:40 +00:00
|
|
|
'line' => 'baz',
|
|
|
|
|
'number' => '3',
|
|
|
|
|
'index' => '2',
|
|
|
|
|
'fromtitle' => $title->getPrefixedDBkey(),
|
|
|
|
|
'byteoffset' => 21,
|
|
|
|
|
'anchor' => 'baz',
|
2022-09-09 20:18:22 +00:00
|
|
|
'linkAnchor' => 'baz',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2022-09-09 20:18:22 +00:00
|
|
|
[
|
|
|
|
|
'toclevel' => 1,
|
|
|
|
|
'level' => '2',
|
|
|
|
|
'line' => 'Romeo+Juliet %A Ó %20',
|
|
|
|
|
'number' => '4',
|
|
|
|
|
'index' => '3',
|
|
|
|
|
'fromtitle' => $title->getPrefixedDBkey(),
|
|
|
|
|
'byteoffset' => 29,
|
|
|
|
|
'anchor' => 'Romeo+Juliet_%A_Ó_%20',
|
|
|
|
|
'linkAnchor' => 'Romeo+Juliet_%A_Ó_%2520',
|
|
|
|
|
]
|
2016-02-17 09:09:32 +00:00
|
|
|
], $out->getSections(), 'getSections() with proper value when <h2> is used' );
|
2013-10-09 15:03:40 +00:00
|
|
|
}
|
2013-12-21 02:14:48 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideNormalizeLinkUrl
|
|
|
|
|
*/
|
|
|
|
|
public function testNormalizeLinkUrl( $explanation, $url, $expected ) {
|
|
|
|
|
$this->assertEquals( $expected, Parser::normalizeLinkUrl( $url ), $explanation );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideNormalizeLinkUrl() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[
|
2013-12-21 02:14:48 +00:00
|
|
|
'Escaping of unsafe characters',
|
|
|
|
|
'http://example.org/foo bar?param[]="value"¶m[]=valüe',
|
|
|
|
|
'http://example.org/foo%20bar?param%5B%5D=%22value%22¶m%5B%5D=val%C3%BCe',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2013-12-21 02:14:48 +00:00
|
|
|
'Case normalization of percent-encoded characters',
|
|
|
|
|
'http://example.org/%ab%cD%Ef%FF',
|
|
|
|
|
'http://example.org/%AB%CD%EF%FF',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2013-12-21 02:14:48 +00:00
|
|
|
'Unescaping of safe characters',
|
|
|
|
|
'http://example.org/%3C%66%6f%6F%3E?%3C%66%6f%6F%3E#%3C%66%6f%6F%3E',
|
|
|
|
|
'http://example.org/%3Cfoo%3E?%3Cfoo%3E#%3Cfoo%3E',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2013-12-21 02:14:48 +00:00
|
|
|
'Context-sensitive replacement of sometimes-safe characters',
|
|
|
|
|
'http://example.org/%23%2F%3F%26%3D%2B%3B?%23%2F%3F%26%3D%2B%3B#%23%2F%3F%26%3D%2B%3B',
|
|
|
|
|
'http://example.org/%23%2F%3F&=+;?%23/?%26%3D%2B%3B#%23/?&=+;',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2016-11-19 00:50:43 +00:00
|
|
|
[
|
|
|
|
|
'IPv6 links aren\'t escaped',
|
|
|
|
|
'http://[::1]/foobar',
|
|
|
|
|
'http://[::1]/foobar',
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'non-IPv6 links aren\'t unescaped',
|
|
|
|
|
'http://%5B::1%5D/foobar',
|
|
|
|
|
'http://%5B::1%5D/foobar',
|
|
|
|
|
],
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2013-12-21 02:14:48 +00:00
|
|
|
}
|
|
|
|
|
|
2018-08-28 16:48:10 +00:00
|
|
|
public function testWrapOutput() {
|
2022-07-05 22:21:30 +00:00
|
|
|
$title = Title::makeTitle( NS_MAIN, 'Foo' );
|
2020-09-18 15:07:18 +00:00
|
|
|
$po = ParserOptions::newFromAnon();
|
2022-01-12 20:13:39 +00:00
|
|
|
$parser = $this->getServiceContainer()->getParser();
|
2019-04-11 13:36:15 +00:00
|
|
|
$parser->parse( 'Hello World', $title, $po );
|
|
|
|
|
$text = $parser->getOutput()->getText();
|
2018-08-28 16:48:10 +00:00
|
|
|
|
2019-12-14 10:27:56 +00:00
|
|
|
$this->assertStringContainsString( 'Hello World', $text );
|
|
|
|
|
$this->assertStringContainsString( '<div', $text );
|
parser: Move lang/dir and mw-content-ltr to ParserOutput::getText
== Skin::wrapHTML ==
Skin::wrapHTML no longer has to perform any guessing of the
ParserOutput language. Nor does it have to special wiki pages vs
special pages in this regard. Yay, code removal.
== ImagePage ==
On URLs like /wiki/File:Example.jpg, the main output handler is
ImagePage::view. This calls the parent Article::view to handle most of
its output. Article::view obtains the ParserOptions, and then fetches
ParserOutput, and then adds `<div class=mw-parser-output>` and its
metadata to OutputPage.
Before this change, ImagePage::view was creating a wrapper based
on "predicting" what language the ParserOutput will contain. It
couldn't call the new OutputPage::getContentLanguage or some
equivalent as Article::view wouldn't have populated that yet.
This leaky abstraction is fixed by this change as now the `<div>`
from ParserOutput no longer comes with a "please wrap it properly"
contract that Article subclasses couldn't possibly implement correctly
(it coudln't wrap it after the fact because Article::view writes to
OutputPage directly).
RECENT (T310445):
A special case was recently added for file pages about translated SVGs.
For those, we decide which language to use for the "fullMedia" thumb
atop the page. This was recently changed as part of T310445 from a
hardcoded $wgLanguageCode (site content lang) to new problematic
Title::getPageViewLanguage, which tries to guestimate the page
language of the rendered ParserOutput and then gets the preferred
variant for the current user. The motivation for this was to support
language variants but used Title::getPageViewLanguage as a kitchen
sink to achieve that minor side-effect. The only part of this
now-deprecated method that we actually need is
LanguageConverter::getPreferredVariant().
Test plan: Covered by ImagePageTest.
== Skin mainpage-title ==
RECENT (T331095, T298715):
A special case was added to Skin::getTemplateData that powers the
mainpage-title interface message feature. This is empty by default,
but when created via MediaWiki:mainpage-title allows interface admins
to replace the H1 with a custom and localised page heading.
A few months ago, in Ifc9f0a7174, Title::getPageViewLanguage was
applied here to support language variants. Replace with the same
fix as for ImagePage. Revert back to Message::inContentLanguage()
but refactor to inLanguage() via MediaWikiServices::getContentLanguage
so that LanguageConverter::getPreferredVariant can be applied.
== EditPage ==
This was doing similar "predicting" of the ParserOutput language to
create an empty preview placeholder for use by preview.js. Now that
ApiParse (via ParserOutput::getText) returns a usable element without
any secret "you magically know the right class, lang, and dir" contract,
this placeholder is no longer needed.
Test Plan:
* EditPage: Default preview
1. index.php?title=Main_Page&action=edit
2. Show preview
3. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr>
* EditPage: JS preview
1. Preferences > Editing > Show preview without reload
2. index.php?title=Main_Page&action=edit
3. Show preview
4. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr>
5. Type something and 'Show preview' again
6. Assert old element gone, new text is shown, and new element
attributes are the same as the above.
== McrUndoAction ==
Same as EditPage basically, but without the JS preview use case.
== DifferenceEngine ==
Test:
1. Open /w/index.php?title=Main_Page&diff=0
(this shows the latest diff, can do manually by viewing
/wiki/Main_Page, click "View history", click "Compare selected revisions")
2. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr>
3. Open /w/index.php?title=Main_Page&diff=0&action=render
4. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr>
== Special:ExpandTemplates ==
Test:
1. /wiki/Special:ExpandTemplates
2. Write "Hello".
3. "OK"
4. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr>
Bug: T341244
Depends-On: Icd9c079f5896ee83d86b9c2699636dc81d25a14c
Depends-On: I4e7484b3b94f1cb6062e7cef9f20626b650bb4b1
Depends-On: I90b88f3b3a3bbeba4f48d118f92f54864997e105
Change-Id: Ib130a055e46764544af0f1a46d2bc2b3a7ee85b7
2023-10-04 04:45:07 +00:00
|
|
|
$this->assertStringContainsString( 'class="mw-content-ltr mw-parser-output"', $text );
|
2018-08-28 16:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
2018-09-05 18:03:15 +00:00
|
|
|
public function provideRevisionAccess() {
|
2021-01-11 22:36:33 +00:00
|
|
|
$title = $this->makeMockTitle( 'ParserRevisionAccessTest', [
|
|
|
|
|
'language' => MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( 'en' )
|
|
|
|
|
] );
|
2018-09-05 18:03:15 +00:00
|
|
|
|
2021-05-03 15:39:33 +00:00
|
|
|
$frank = new UserIdentityValue( 5, 'Frank' );
|
2018-09-05 18:03:15 +00:00
|
|
|
|
|
|
|
|
$text = '* user:{{REVISIONUSER}};id:{{REVISIONID}};time:{{REVISIONTIMESTAMP}};';
|
|
|
|
|
$po = new ParserOptions( $frank );
|
|
|
|
|
|
|
|
|
|
yield 'current' => [ $text, $po, 0, 'user:CurrentAuthor;id:200;time:20160606000000;' ];
|
2019-11-11 08:04:38 +00:00
|
|
|
yield 'anonymous' => [ $text, $po, null, 'user:;id:;time:' ];
|
2018-09-05 18:03:15 +00:00
|
|
|
yield 'current with ID' => [ $text, $po, 200, 'user:CurrentAuthor;id:200;time:20160606000000;' ];
|
|
|
|
|
|
|
|
|
|
$text = '* user:{{REVISIONUSER}};id:{{REVISIONID}};time:{{REVISIONTIMESTAMP}};';
|
|
|
|
|
$po = new ParserOptions( $frank );
|
|
|
|
|
|
|
|
|
|
yield 'old' => [ $text, $po, 100, 'user:OldAuthor;id:100;time:20140404000000;' ];
|
|
|
|
|
|
|
|
|
|
$oldRevision = new MutableRevisionRecord( $title );
|
|
|
|
|
$oldRevision->setId( 100 );
|
2021-02-15 18:58:09 +00:00
|
|
|
$oldRevision->setUser( new UserIdentityValue( 7, 'FauxAuthor' ) );
|
2018-09-05 18:03:15 +00:00
|
|
|
$oldRevision->setTimestamp( '20141111111111' );
|
2018-09-24 21:10:08 +00:00
|
|
|
$oldRevision->setContent( SlotRecord::MAIN, new WikitextContent( 'FAUX' ) );
|
2018-09-05 18:03:15 +00:00
|
|
|
|
|
|
|
|
$po = new ParserOptions( $frank );
|
2021-02-07 13:10:36 +00:00
|
|
|
$po->setCurrentRevisionRecordCallback( static function () use ( $oldRevision ) {
|
2020-06-03 03:48:42 +00:00
|
|
|
return $oldRevision;
|
2018-09-05 18:03:15 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
yield 'old with override' => [ $text, $po, 100, 'user:FauxAuthor;id:100;time:20141111111111;' ];
|
|
|
|
|
|
|
|
|
|
$text = '* user:{{REVISIONUSER}};user-subst:{{subst:REVISIONUSER}};';
|
|
|
|
|
|
|
|
|
|
$po = new ParserOptions( $frank );
|
|
|
|
|
$po->setIsPreview( true );
|
|
|
|
|
|
|
|
|
|
yield 'preview without override, using context' => [
|
|
|
|
|
$text,
|
|
|
|
|
$po,
|
|
|
|
|
null,
|
|
|
|
|
'user:Frank;',
|
|
|
|
|
'user-subst:Frank;',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$text = '* user:{{REVISIONUSER}};time:{{REVISIONTIMESTAMP}};'
|
|
|
|
|
. 'user-subst:{{subst:REVISIONUSER}};time-subst:{{subst:REVISIONTIMESTAMP}};';
|
|
|
|
|
|
|
|
|
|
$newRevision = new MutableRevisionRecord( $title );
|
2021-02-15 18:58:09 +00:00
|
|
|
$newRevision->setUser( new UserIdentityValue( 9, 'NewAuthor' ) );
|
2018-09-05 18:03:15 +00:00
|
|
|
$newRevision->setTimestamp( '20180808000000' );
|
2018-09-24 21:10:08 +00:00
|
|
|
$newRevision->setContent( SlotRecord::MAIN, new WikitextContent( 'NEW' ) );
|
2018-09-05 18:03:15 +00:00
|
|
|
|
|
|
|
|
$po = new ParserOptions( $frank );
|
|
|
|
|
$po->setIsPreview( true );
|
2021-02-07 13:10:36 +00:00
|
|
|
$po->setCurrentRevisionRecordCallback( static function () use ( $newRevision ) {
|
2020-06-03 03:48:42 +00:00
|
|
|
return $newRevision;
|
2018-09-05 18:03:15 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
yield 'preview' => [
|
|
|
|
|
$text,
|
|
|
|
|
$po,
|
|
|
|
|
null,
|
|
|
|
|
'user:NewAuthor;time:20180808000000;',
|
|
|
|
|
'user-subst:NewAuthor;time-subst:20180808000000;',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$po = new ParserOptions( $frank );
|
2021-02-07 13:10:36 +00:00
|
|
|
$po->setCurrentRevisionRecordCallback( static function () use ( $newRevision ) {
|
2020-06-03 03:48:42 +00:00
|
|
|
return $newRevision;
|
2018-09-05 18:03:15 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
yield 'pre-save' => [
|
|
|
|
|
$text,
|
|
|
|
|
$po,
|
|
|
|
|
null,
|
|
|
|
|
'user:NewAuthor;time:20180808000000;',
|
|
|
|
|
'user-subst:NewAuthor;time-subst:20180808000000;',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$text = "(ONE)<includeonly>(TWO)</includeonly>"
|
|
|
|
|
. "<noinclude>#{{:ParserRevisionAccessTest}}#</noinclude>";
|
|
|
|
|
|
|
|
|
|
$newRevision = new MutableRevisionRecord( $title );
|
2021-02-15 18:58:09 +00:00
|
|
|
$newRevision->setUser( new UserIdentityValue( 9, 'NewAuthor' ) );
|
2018-09-05 18:03:15 +00:00
|
|
|
$newRevision->setTimestamp( '20180808000000' );
|
2018-09-24 21:10:08 +00:00
|
|
|
$newRevision->setContent( SlotRecord::MAIN, new WikitextContent( $text ) );
|
2018-09-05 18:03:15 +00:00
|
|
|
|
|
|
|
|
$po = new ParserOptions( $frank );
|
|
|
|
|
$po->setIsPreview( true );
|
2021-02-07 13:10:36 +00:00
|
|
|
$po->setCurrentRevisionRecordCallback( static function () use ( $newRevision ) {
|
2020-06-03 03:48:42 +00:00
|
|
|
return $newRevision;
|
2018-09-05 18:03:15 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
yield 'preview with self-transclude' => [ $text, $po, null, '(ONE)#(ONE)(TWO)#' ];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideRevisionAccess
|
|
|
|
|
*/
|
|
|
|
|
public function testRevisionAccess(
|
|
|
|
|
$text,
|
|
|
|
|
ParserOptions $po,
|
|
|
|
|
$revId,
|
|
|
|
|
$expectedInHtml,
|
|
|
|
|
$expectedInPst = null
|
|
|
|
|
) {
|
2021-01-11 22:36:33 +00:00
|
|
|
$title = $this->makeMockTitle( 'ParserRevisionAccessTest', [
|
2022-01-12 20:13:39 +00:00
|
|
|
'language' => $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'en' )
|
2021-01-11 22:36:33 +00:00
|
|
|
] );
|
2018-09-05 18:03:15 +00:00
|
|
|
|
|
|
|
|
$oldRevision = new MutableRevisionRecord( $title );
|
|
|
|
|
$oldRevision->setId( 100 );
|
2021-02-15 18:58:09 +00:00
|
|
|
$oldRevision->setUser( new UserIdentityValue( 7, 'OldAuthor' ) );
|
2018-09-05 18:03:15 +00:00
|
|
|
$oldRevision->setTimestamp( '20140404000000' );
|
2018-09-24 21:10:08 +00:00
|
|
|
$oldRevision->setContent( SlotRecord::MAIN, new WikitextContent( 'OLD' ) );
|
2018-09-05 18:03:15 +00:00
|
|
|
|
|
|
|
|
$currentRevision = new MutableRevisionRecord( $title );
|
|
|
|
|
$currentRevision->setId( 200 );
|
2021-02-15 18:58:09 +00:00
|
|
|
$currentRevision->setUser( new UserIdentityValue( 9, 'CurrentAuthor' ) );
|
2018-09-05 18:03:15 +00:00
|
|
|
$currentRevision->setTimestamp( '20160606000000' );
|
2018-09-24 21:10:08 +00:00
|
|
|
$currentRevision->setContent( SlotRecord::MAIN, new WikitextContent( 'CURRENT' ) );
|
2018-09-05 18:03:15 +00:00
|
|
|
|
2022-07-14 12:42:07 +00:00
|
|
|
$revisionStore = $this->createMock( RevisionStore::class );
|
2018-09-05 18:03:15 +00:00
|
|
|
|
|
|
|
|
$revisionStore
|
|
|
|
|
->method( 'getKnownCurrentRevision' )
|
|
|
|
|
->willReturnMap( [
|
|
|
|
|
[ $title, 100, $oldRevision ],
|
|
|
|
|
[ $title, 200, $currentRevision ],
|
|
|
|
|
[ $title, 0, $currentRevision ],
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$revisionStore
|
|
|
|
|
->method( 'getRevisionById' )
|
|
|
|
|
->willReturnMap( [
|
2021-03-15 16:51:47 +00:00
|
|
|
[ 100, 0, null, $oldRevision ],
|
|
|
|
|
[ 200, 0, null, $currentRevision ],
|
2018-09-05 18:03:15 +00:00
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->setService( 'RevisionStore', $revisionStore );
|
|
|
|
|
|
2022-01-12 20:13:39 +00:00
|
|
|
$parser = $this->getServiceContainer()->getParser();
|
2019-04-11 13:36:15 +00:00
|
|
|
$parser->parse( $text, $title, $po, true, true, $revId );
|
|
|
|
|
$html = $parser->getOutput()->getText();
|
2018-09-05 18:03:15 +00:00
|
|
|
|
2019-12-14 12:45:35 +00:00
|
|
|
$this->assertStringContainsString( $expectedInHtml, $html, 'In HTML' );
|
2018-09-05 18:03:15 +00:00
|
|
|
|
|
|
|
|
if ( $expectedInPst !== null ) {
|
2021-07-28 14:08:59 +00:00
|
|
|
$pst = $parser->preSaveTransform( $text, $title, $po->getUserIdentity(), $po );
|
2019-12-14 12:45:35 +00:00
|
|
|
$this->assertStringContainsString( $expectedInPst, $pst, 'After Pre-Safe Transform' );
|
2018-09-05 18:03:15 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-25 23:14:15 +00:00
|
|
|
public static function provideGuessSectionNameFromWikiText() {
|
|
|
|
|
return [
|
|
|
|
|
[ '1/2', 'html5', '#1/2' ],
|
|
|
|
|
[ '1/2', 'legacy', '#1.2F2' ],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @dataProvider provideGuessSectionNameFromWikiText */
|
|
|
|
|
public function testGuessSectionNameFromWikiText( $input, $mode, $expected ) {
|
2022-08-01 19:14:41 +00:00
|
|
|
$this->overrideConfigValue( MainConfigNames::FragmentMode, [ $mode ] );
|
2022-01-12 20:13:39 +00:00
|
|
|
$result = $this->getServiceContainer()->getParser()
|
2019-04-11 13:36:15 +00:00
|
|
|
->guessSectionNameFromWikiText( $input );
|
2022-02-08 05:12:43 +00:00
|
|
|
$this->assertEquals( $expected, $result );
|
2018-10-25 23:14:15 +00:00
|
|
|
}
|
|
|
|
|
|
2014-04-24 17:52:09 +00:00
|
|
|
// @todo Add tests for cleanSig() / cleanSigInSig(), getSection(),
|
|
|
|
|
// replaceSection(), getPreloadText()
|
2012-06-23 18:28:51 +00:00
|
|
|
}
|