aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/integration/includes/parser/Parsoid/ParsoidParserTest.php
blob: dc79736218cca04108932c509f0d5c513e808a15 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php

namespace MediaWiki\Tests\Parser\Parsoid;

use MediaWiki\MainConfigNames;
use MediaWiki\MediaWikiServices;
use MediaWiki\Parser\ParserOptions;
use MediaWiki\Parser\Parsoid\ParsoidParser;
use MediaWiki\Title\Title;
use MediaWikiIntegrationTestCase;

/**
 * @covers \MediaWiki\Parser\Parsoid\ParsoidParser::parse
 * @group Database
 */
class ParsoidParserTest extends MediaWikiIntegrationTestCase {

	protected function setUp(): void {
		parent::setUp();
		// Limit reporting affects the options used
		$this->overrideConfigValue(
			MainConfigNames::EnableParserLimitReporting,
			false
		);
	}

	/** @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',
			'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',
			'suppressSectionEditLinks',
			'wrapclass',
		];
		$this->assertArrayEquals( $usedOptions, $output->getUsedOptions() );

		$pipeline = MediaWikiServices::getInstance()->getDefaultOutputPipeline();
		$pipeline->run( $output, $opts, [] );
		$this->assertArrayEquals( $usedOptions, $output->getUsedOptions() );
	}
}