wiki.techinc.nl/tests/phpunit/includes/content/TextContentHandlerIntegrationTest.php
C. Scott Ananian 82da9cf14b Use Remex for DeduplicateStyles transform
The previous implementation was using an ad-hoc regular expression which
was matching inside the data-mw attribute of Parsoid output, eg:

 <sup about="#mwt42" [...] typeof="mw:Extension/ref mw:Error" data-mw="{&quot;name&quot;:&quot;ref&quot;,&quot;attrs&quot;:{&quot;name&quot;:&quot;infobox_stats_ref_rail&quot;},&quot;body&quot;:{&quot;html&quot;:&quot;<style data-mw-deduplicate=\&quot;TemplateStyles:r1133582631\&quot; typeof=\&quot;...">

After substitution, the <link> element inserted contained " instead of
&quot; and so broke out of the attribute.

Instead use a proper HTML tokenizer (via wikimedia/remex-html) so that
we don't allow bogus matches inside attribute values.

To fix up tests:
* Don't deduplicate styles when parsing UX messages (also helps performance)
* Don't deduplicate styles in ContentHandler integration tests
* Don't deduplicate styles by default in parser tests
  (unless explicit option is set)

Depends-On: Id9801a9ff540bd818a32bc6fa35c48a9cff12d3a
Depends-On: I5111f1fdb7140948b82113adbc774af286174ab3
Followup-To: Ic0b17e361bf6eb0e71c498abc17f5f67f82318f8
Change-Id: I32d3d1772243c3819e1e1486351d16871b6e21c4
2023-12-15 17:49:21 +01:00

63 lines
1.8 KiB
PHP

<?php
use MediaWiki\Title\Title;
/**
* @group ContentHandler
* @group Database
* ^--- needed, because we do need the database to test link updates
*/
class TextContentHandlerIntegrationTest extends MediaWikiLangTestCase {
public static function provideGetParserOutput() {
yield 'Basic render' => [
'title' => 'TextContentTest_testGetParserOutput',
'model' => CONTENT_MODEL_TEXT,
'text' => "hello ''world'' & [[stuff]]\n",
'expectedHtml' => "<pre>hello ''world'' &amp; [[stuff]]\n</pre>",
'expectedFields' => [ 'Links' => [] ]
];
yield 'Multi line render' => [
'title' => 'TextContentTest_testGetParserOutput',
'model' => CONTENT_MODEL_TEXT,
'text' => "Test 1\nTest 2\n\nTest 3\n",
'expectedHtml' => "<pre>Test 1\nTest 2\n\nTest 3\n</pre>",
'expectedFields' => [ 'Links' => [] ]
];
}
/**
* @dataProvider provideGetParserOutput
* @covers TextContentHandler::fillParserOutput
*/
public function testGetParserOutput( $title, $model, $text, $expectedHtml,
$expectedFields = null, $parserOptions = null
) {
$title = Title::newFromText( $title );
$content = ContentHandler::makeContent( $text, $title, $model );
$contentRenderer = $this->getServiceContainer()->getContentRenderer();
$po = $contentRenderer->getParserOutput( $content, $title, null, $parserOptions );
$html = $po->getText( [
'deduplicateStyles' => false,
] );
$html = preg_replace( '#<!--.*?-->#sm', '', $html ); // strip comments
if ( $expectedHtml !== null ) {
$this->assertEquals( $expectedHtml, trim( $html ) );
}
if ( $expectedFields ) {
foreach ( $expectedFields as $field => $exp ) {
$getter = 'get' . ucfirst( $field );
$v = $po->$getter();
if ( is_array( $exp ) ) {
$this->assertArrayEquals( $exp, $v );
} else {
$this->assertEquals( $exp, $v );
}
}
}
}
}