Debian Codegraph @

vendor/wikimedia/parsoid/tests/phpunit/Parsoid/Wt2Html/DOM/Handlers/HeadingsTest.php

raw
1 <?php
2
3 namespace Test\Parsoid\Wt2Html\DOM\Handlers;
4
5 use PHPUnit\Framework\TestCase;
6 use Wikimedia\Parsoid\DOM\Element;
7 use Wikimedia\Parsoid\Mocks\MockDataAccess;
8 use Wikimedia\Parsoid\Mocks\MockPageConfig;
9 use Wikimedia\Parsoid\Mocks\MockPageContent;
10 use Wikimedia\Parsoid\Mocks\MockSiteConfig;
11 use Wikimedia\Parsoid\Parsoid;
12 use Wikimedia\Parsoid\Utils\DOMCompat;
13 use Wikimedia\Parsoid\Utils\DOMUtils;
14
15 /**
16 * based on tests/mocha/heading.ids.js
17 * @coversDefaultClass \Wikimedia\Parsoid\Wt2Html\DOM\Handlers\Headings
18 */
19 class HeadingsTest extends TestCase {
20
21 public function validateId( string $name, string $heading, string $description, array $expectedIds,
22 Element $doc ): void {
23 $elts = DOMCompat::querySelectorAll( $doc, 'body > h1' );
24 $this->assertSameSize( $expectedIds, $elts );
25
26 foreach ( $expectedIds as $key => $id ) {
27 $h = $elts[$key];
28 $fallback = DOMCompat::querySelectorAll( $h, 'span[typeof="mw:FallbackId"]:empty' );
29 if ( is_String( $id ) ) {
30 $attrib = DOMCompat::getAttribute( $h, 'id' );
31 $this->assertEquals( $id, $attrib, $name . $heading . $description . $id );
32 $this->assertCount( 0, $fallback, $name . $description .
33 ' fallback should not be set.' );
34 } else {
35 $attrib = DOMCompat::getAttribute( $h, 'id' );
36 $this->assertEquals( $attrib, $id[0], $name . $heading . $description . $id[0] );
37 $this->assertCount( 1, $fallback, $name . $description .
38 ' fallback should be set to 1.' );
39 $fallbackAttribute = DOMCompat::getAttribute( $fallback[0], 'id' );
40 $this->assertEquals( $id[1], $fallbackAttribute, $name . $heading . $description . $id[1] );
41 }
42 }
43 }
44
45 public function runHeadingTest( string $name, array $test, string $description ): void {
46 $heading = current( $test );
47 array_shift( $test );
48 $expectedIds = $test;
49
50 $siteConfig = new MockSiteConfig( [] );
51 $dataAccess = new MockDataAccess( $siteConfig, [] );
52 $parsoid = new Parsoid( $siteConfig, $dataAccess );
53
54 $content = new MockPageContent( [ 'main' => $heading ] );
55 $pageConfig = new MockPageConfig( $siteConfig, [], $content );
56 $html = $parsoid->wikitext2html( $pageConfig, [ "wrapSections" => false ] );
57
58 $doc = DOMUtils::parseHTML( $html );
59 $body = DOMCompat::getBody( $doc );
60
61 $this->validateId( $name, $heading, $description, $expectedIds, $body );
62 }
63
64 /**
65 * @covers ::genAnchors
66 * @dataProvider provideSimpleHeadings
67 * @param array $simpleHeadings
68 */
69 public function testSimpleHeadings( array $simpleHeadings ): void {
70 $this->runHeadingTest( 'Simple Headings ', $simpleHeadings, ' should be valid for ' );
71 }
72
73 public function provideSimpleHeadings(): array {
74 return [
75 [ [ '=Test=', 'Test' ] ],
76 [ [ '=Test 1 2 3=', 'Test_1_2_3' ] ],
77 [ [ '= Test 1 _2 3 =', 'Test_1_2_3' ] ]
78 ];
79 }
80
81 /**
82 * @covers ::genAnchors
83 * @dataProvider provideHeadingsWithWtChars
84 * @param array $headingsWithWtChars
85 */
86 public function testHeadingsWithWtChars( array $headingsWithWtChars ): void {
87 $this->runHeadingTest( 'Headings with ', $headingsWithWtChars,
88 ' wikitext chars should be ignored in ' );
89 }
90
91 public function provideHeadingsWithWtChars(): array {
92 return [
93 [ [ '=This is a [[Link]]=', 'This_is_a_Link' ] ],
94 [ [ "=Some '''bold''' and '''italic''' text=", 'Some_bold_and_italic_text' ] ],
95 [ [ "=Some {{1x|transclusion}} here=", 'Some_transclusion_here' ] ]
96 /* PORT-FIXME this is a bug in the template handler for nested templates
97 [ [ "={{1x|a and ''b'' and [[c]] and {{1x|d}} and e}}=", "a_and_b_and_c_and_d_and_e" ] ],
98 */
99 /* PORT-FIXME this test depends on missing mock environment code
100 [ [ "=Some {{convert|1|km}} here=", [ "Some_1_kilometre_(0.62_mi)_here",
101 "Some_1_kilometre_.280.62_mi.29_here" ] ] ]
102 */
103 ];
104 }
105
106 /**
107 * @covers ::genAnchors
108 * @dataProvider provideHeadingsWithHTML
109 * @param array $headingsWithHTML
110 */
111 public function testHeadingsWithHTML( array $headingsWithHTML ): void {
112 $this->runHeadingTest( 'Headings with ', $headingsWithHTML,
113 ' HTML tags should be stripped in ' );
114 }
115
116 public function provideHeadingsWithHTML(): array {
117 return [
118 [ [ "=Some <span>html</span> <b>tags</b> here=", 'Some_html_tags_here' ] ],
119 /* PHP parser output is a bit weird on a heading with these contents */
120 [ [ "=a <div>b <span>c</span>d</div> e=", 'a_b_cd_e' ] ]
121 ];
122 }
123
124 /**
125 * @covers ::genAnchors
126 * @dataProvider provideHeadingsWithEntities
127 * @param array $headingsWithEntities
128 */
129 public function testHeadingsWithEntities( array $headingsWithEntities ): void {
130 $this->runHeadingTest( 'Headings with ', $headingsWithEntities,
131 ' Entities should be encoded ' );
132 }
133
134 public function provideHeadingsWithEntities(): array {
135 return [
136 [ [ '=Red, Blue, Yellow=', [ 'Red,_Blue,_Yellow', 'Red.2C_Blue.2C_Yellow' ] ] ],
137 [ [ '=!@#$%^&*()=', [ '!@#$%^&*()', ".21.40.23.24.25.5E.26.2A.28.29" ] ] ],
138 [ [ '=:=', ":" ] ]
139 ];
140 }
141
142 /**
143 * @covers ::genAnchors
144 * @dataProvider provideNonEnglishHeadings
145 * @param array $nonEnglishHeadings
146 */
147 public function testNonEnglishHeadings( array $nonEnglishHeadings ): void {
148 $this->runHeadingTest( 'Headings with non English characters ', $nonEnglishHeadings,
149 ' Ids should be valid ' );
150 }
151
152 public function provideNonEnglishHeadings(): array {
153 return [
154 [ [ "=Références=", [ "Références", "R.C3.A9f.C3.A9rences" ] ] ],
155 // phpcs:ignore Generic.Files.LineLength.TooLong
156 [ [ "=बादलों का वगीर्करण=", [ "बादलों_का_वगीर्करण",
157 ".E0.A4.AC.E0.A4.BE.E0.A4.A6.E0.A4.B2.E0.A5.8B.E0.A4.82_.E0.A4.95.E0.A4.BE_" .
158 ".E0.A4.B5.E0.A4.97.E0.A5.80.E0.A4.B0.E0.A5.8D.E0.A4.95.E0.A4.B0.E0.A4.A3" ] ] ]
159 ];
160 }
161
162 /**
163 * @covers ::genAnchors
164 * @dataProvider provideEdgeCases
165 * @param array $edgeCases
166 */
167 public function testEdgeCases( array $edgeCases ): void {
168 $this->runHeadingTest( 'Edge Case Tests ', $edgeCases, ' Ids should be valid ' );
169 }
170
171 public function provideEdgeCases(): array {
172 return [
173 [ [ "=a=\n=a=", 'a', 'a_2' ] ],
174 [ [ "=a/b=\n=a.2Fb=", [ "a/b", "a.2Fb" ], "a.2Fb_2" ] ],
175 [ [ "<h1 id='bar'>foo</h1>", 'bar' ] ],
176 [ [ "<h1>foo</h1>\n=foo=\n<h1 id='foo'>bar</h1>", 'foo', 'foo_2', 'foo_3' ] ]
177 ];
178 }
179
180 }