WikitextStructureTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * @covers WikiTextStructure
  4. */
  5. class WikitextStructureTest extends MediaWikiLangTestCase {
  6. private function getMockTitle() {
  7. return Title::newFromText( "TestTitle" );
  8. }
  9. /**
  10. * Get parser output for Wiki text
  11. * @param string $text
  12. * @return ParserOutput
  13. */
  14. private function getParserOutput( $text ) {
  15. $content = new WikitextContent( $text );
  16. return $content->getParserOutput( $this->getMockTitle() );
  17. }
  18. /**
  19. * Get WikitextStructure for given text
  20. * @param string $text
  21. * @return WikiTextStructure
  22. */
  23. private function getStructure( $text ) {
  24. return new WikiTextStructure( $this->getParserOutput( $text ) );
  25. }
  26. public function testHeadings() {
  27. $text = <<<END
  28. Some text here
  29. == Heading one ==
  30. Some text
  31. ==== heading two ====
  32. More text
  33. === Applicability of the strict mass-energy equivalence formula, ''E'' = ''mc''<sup>2</sup> ===
  34. and more text
  35. == Wikitext '''in''' [[Heading]] and also <b>html</b> ==
  36. more text
  37. ==== See also ====
  38. * Also things to see!
  39. END;
  40. $struct = $this->getStructure( $text );
  41. $headings = $struct->headings();
  42. $this->assertCount( 4, $headings );
  43. $this->assertContains( "Heading one", $headings );
  44. $this->assertContains( "heading two", $headings );
  45. $this->assertContains( "Applicability of the strict mass-energy equivalence formula, E = mc2",
  46. $headings );
  47. $this->assertContains( "Wikitext in Heading and also html", $headings );
  48. }
  49. public function testDefaultSort() {
  50. $text = <<<END
  51. Louise Michel
  52. == Heading one ==
  53. Some text
  54. ==== See also ====
  55. * Also things to see!
  56. {{DEFAULTSORT:Michel, Louise}}
  57. END;
  58. $struct = $this->getStructure( $text );
  59. $this->assertEquals( "Michel, Louise", $struct->getDefaultSort() );
  60. }
  61. public function testHeadingsFirst() {
  62. $text = <<<END
  63. == Heading one ==
  64. Some text
  65. ==== heading two ====
  66. END;
  67. $struct = $this->getStructure( $text );
  68. $headings = $struct->headings();
  69. $this->assertCount( 2, $headings );
  70. $this->assertContains( "Heading one", $headings );
  71. $this->assertContains( "heading two", $headings );
  72. }
  73. public function testHeadingsNone() {
  74. $text = "This text is completely devoid of headings.";
  75. $struct = $this->getStructure( $text );
  76. $headings = $struct->headings();
  77. $this->assertArrayEquals( [], $headings );
  78. }
  79. public function testTexts() {
  80. $text = <<<END
  81. Opening text is opening.
  82. == Then comes header ==
  83. Then we got more<br>text
  84. === And more headers ===
  85. {| class="wikitable"
  86. |-
  87. ! Header table
  88. |-
  89. | row in table
  90. |-
  91. | another row in table
  92. |}
  93. END;
  94. $struct = $this->getStructure( $text );
  95. $this->assertEquals( "Opening text is opening.", $struct->getOpeningText() );
  96. $this->assertEquals( "Opening text is opening. Then we got more text",
  97. $struct->getMainText() );
  98. $this->assertEquals( [ "Header table row in table another row in table" ],
  99. $struct->getAuxiliaryText() );
  100. }
  101. }