Debian Codegraph @

tests/phpunit/unit/includes/skins/components/SkinComponentTableOfContentsTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Output\OutputPage;
4 use MediaWiki\Skin\SkinComponentTableOfContents;
5 use Wikimedia\Parsoid\Core\SectionMetadata;
6 use Wikimedia\Parsoid\Core\TOCData;
7
8 /**
9 * @covers \MediaWiki\Skin\SkinComponentTableOfContents
10 * @group Skin
11 */
12 class SkinComponentTableOfContentsTest extends MediaWikiUnitTestCase {
13
14 private function addDefaults( array $sectionData ): array {
15 return SectionMetadata::fromLegacy( $sectionData )->toLegacy();
16 }
17
18 public function provideGetSectionsData(): array {
19 // byteoffset and fromtitle are redacted from this test.
20 $SECTION_1 = [
21 'toclevel' => 1,
22 'line' => 'Section 1',
23 'anchor' => 'section_1',
24 ];
25 $SECTION_1_1 = [
26 'toclevel' => 2,
27 'line' => 'Section 1.1',
28 'anchor' => 'section_1_1',
29 ];
30 $SECTION_1_2 = [
31 'toclevel' => 2,
32 'line' => 'Section 1.2',
33 'anchor' => 'section_1_2',
34 ];
35 $SECTION_1_2_1 = [
36 'toclevel' => 3,
37 'line' => 'Section 1.2.1',
38 'anchor' => 'section_1_2_1',
39 ];
40 $SECTION_1_3 = [
41 'toclevel' => 2,
42 'line' => 'Section 1.3',
43 'anchor' => 'section_1_3',
44 ];
45 $SECTION_2 = [
46 'toclevel' => 1,
47 'line' => 'Section 2',
48 'anchor' => 'section_2',
49 ];
50
51 return [
52 [
53 // isTocEnabled
54 false,
55 // sections data
56 [],
57 // expected
58 []
59 ],
60 [
61 // isTocEnabled
62 true,
63 // sections data
64 [
65 $SECTION_1,
66 $SECTION_2
67 ],
68 // expected
69 [
70 'number-section-count' => 2,
71 'array-sections' => [
72 $this->addDefaults( $SECTION_1 ) + [
73 'array-sections' => [],
74 'is-top-level-section' => true,
75 'is-parent-section' => false,
76 ],
77 $this->addDefaults( $SECTION_2 ) + [
78 'array-sections' => [],
79 'is-top-level-section' => true,
80 'is-parent-section' => false,
81 ]
82 ]
83 ]
84 ],
85 [
86 // isTocEnabled
87 true,
88 // sections data
89 [
90 $SECTION_1,
91 $SECTION_1_1,
92 $SECTION_2,
93 ],
94 // expected
95 [
96 'number-section-count' => 3,
97 'array-sections' => [
98 $this->addDefaults( $SECTION_1 ) + [
99 'array-sections' => [
100 $this->addDefaults( $SECTION_1_1 ) + [
101 'array-sections' => [],
102 'is-top-level-section' => false,
103 'is-parent-section' => false,
104 ]
105 ],
106 'is-top-level-section' => true,
107 'is-parent-section' => true,
108 ],
109 $this->addDefaults( $SECTION_2 ) + [
110 'array-sections' => [],
111 'is-top-level-section' => true,
112 'is-parent-section' => false,
113 ]
114 ]
115 ]
116 ],
117 [
118 // isTocEnabled
119 true,
120 // sections data
121 [
122 $SECTION_1,
123 $SECTION_1_1,
124 $SECTION_1_2,
125 $SECTION_1_2_1,
126 $SECTION_1_3,
127 $SECTION_2,
128 ],
129 // expected
130 [
131 'number-section-count' => 6,
132 'array-sections' => [
133 $this->addDefaults( $SECTION_1 ) + [
134 'array-sections' => [
135 $this->addDefaults( $SECTION_1_1 ) + [
136 'array-sections' => [],
137 'is-top-level-section' => false,
138 'is-parent-section' => false,
139 ],
140 $this->addDefaults( $SECTION_1_2 ) + [
141 'array-sections' => [
142 $this->addDefaults( $SECTION_1_2_1 ) + [
143 'array-sections' => [],
144 'is-top-level-section' => false,
145 'is-parent-section' => false,
146 ],
147 ],
148 'is-top-level-section' => false,
149 'is-parent-section' => true,
150 ],
151 $this->addDefaults( $SECTION_1_3 ) + [
152 'array-sections' => [],
153 'is-top-level-section' => false,
154 'is-parent-section' => false,
155 ]
156 ],
157 'is-top-level-section' => true,
158 'is-parent-section' => true,
159 ],
160 $this->addDefaults( $SECTION_2 ) + [
161 'array-sections' => [],
162 'is-top-level-section' => true,
163 'is-parent-section' => false,
164 ]
165 ]
166 ]
167 ]
168 ];
169 }
170
171 /**
172 * @dataProvider provideGetSectionsData
173 * @param bool $isTocEnabled
174 * @param array $sectionsData
175 * @param array $expected
176 */
177 public function testGetTemplateData( $isTocEnabled, $sectionsData, $expected ) {
178 // Convert to first class objects
179 $tocData = new TOCData;
180 array_map(
181 static function ( $s ) use ( $tocData ) {
182 $tocData->addSection( SectionMetadata::fromLegacy( $s ) );
183 },
184 $sectionsData
185 );
186 $mockOutput = $this->createMock( OutputPage::class );
187 $mockOutput->method( 'isTOCEnabled' )->willReturn( $isTocEnabled );
188 $mockOutput->method( 'getTOCData' )->willReturn( $tocData );
189 $skinComponent = new SkinComponentTableOfContents( $mockOutput );
190
191 $data = $skinComponent->getTemplateData();
192
193 $this->assertEquals( $expected, $data );
194 }
195 }