wiki.techinc.nl/tests/phpunit/includes/content/WikitextStructureTest.php
James D. Forrester ad06527fb4 Reorg: Namespace the Title class
This is moderately messy.

Process was principally:

* xargs rg --files-with-matches '^use Title;' | grep 'php$' | \
  xargs -P 1 -n 1 sed -i -z 's/use Title;/use MediaWiki\\Title\\Title;/1'
* rg --files-without-match 'MediaWiki\\Title\\Title;' . | grep 'php$' | \
  xargs rg --files-with-matches 'Title\b' | \
  xargs -P 1 -n 1 sed -i -z 's/\nuse /\nuse MediaWiki\\Title\\Title;\nuse /1'
* composer fix

Then manual fix-ups for a few files that don't have any use statements.

Bug: T166010
Follows-Up: Ia5d8cb759dc3bc9e9bbe217d0fb109e2f8c4101a
Change-Id: If8fc9d0d95fc1a114021e282a706fc3e7da3524b
2023-03-02 08:46:53 -05:00

107 lines
2.9 KiB
PHP

<?php
use MediaWiki\Title\Title;
/**
* @covers WikiTextStructure
*/
class WikitextStructureTest extends MediaWikiLangTestCase {
/**
* Get WikitextStructure for given text
* @param string $text
* @return WikiTextStructure
*/
private function getStructure( $text ) {
$content = new WikitextContent( $text );
$contentRenderer = $this->getServiceContainer()->getContentRenderer();
$parserOutput = $contentRenderer->getParserOutput( $content, Title::newFromText( 'TestTitle' ) );
return new WikiTextStructure( $parserOutput );
}
public function testHeadings() {
$text = <<<END
Some text here
== Heading one ==
Some text
==== heading two ====
More text
=== Applicability of the strict mass-energy equivalence formula, ''E'' = ''mc''<sup>2</sup> ===
and more text
== Wikitext '''in''' [[Heading]] and also <b>html</b> ==
more text
==== See also ====
* Also things to see!
END;
$struct = $this->getStructure( $text );
$headings = $struct->headings();
$this->assertCount( 4, $headings );
$this->assertContains( "Heading one", $headings );
$this->assertContains( "heading two", $headings );
$this->assertContains( "Applicability of the strict mass-energy equivalence formula, E = mc2",
$headings );
$this->assertContains( "Wikitext in Heading and also html", $headings );
}
public function testDefaultSort() {
$text = <<<END
Louise Michel
== Heading one ==
Some text
==== See also ====
* Also things to see!
{{DEFAULTSORT:Michel, Louise}}
END;
$struct = $this->getStructure( $text );
$this->assertEquals( "Michel, Louise", $struct->getDefaultSort() );
}
public function testHeadingsFirst() {
$text = <<<END
== Heading one ==
Some text
==== heading two ====
END;
$struct = $this->getStructure( $text );
$headings = $struct->headings();
$this->assertCount( 2, $headings );
$this->assertContains( "Heading one", $headings );
$this->assertContains( "heading two", $headings );
}
public function testHeadingsNone() {
$text = "This text is completely devoid of headings.";
$struct = $this->getStructure( $text );
$headings = $struct->headings();
$this->assertArrayEquals( [], $headings );
}
public function testTexts() {
$text = <<<END
Opening text is opening.
== Then comes header ==
Then we got more<br>text
=== And more headers ===
{| class="wikitable"
|-
! Header table
|-
| row in table
|-
| another row in table
|}
END;
$struct = $this->getStructure( $text );
$this->assertEquals( "Opening text is opening.", $struct->getOpeningText() );
$this->assertEquals( "Opening text is opening. Then we got more text",
$struct->getMainText() );
$this->assertEquals( [ "Header table row in table another row in table" ],
$struct->getAuxiliaryText() );
}
public function testPreservesWordSpacing() {
$text = "<dd><dl>foo</dl><dl>bar</dl></dd><p>baz</p>";
$struct = $this->getStructure( $text );
$this->assertEquals( "foo bar baz", $struct->getMainText() );
}
}