WikitextSubPageListRendererTest   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 210
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 210
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 6

16 Methods

Rating   Name   Duplication   Size   Complexity  
A setUpBeforeClass() 0 20 1
A assertCreatesList() 0 6 1
A getListForParams() 0 12 1
A getProcessedParams() 0 16 2
A testListForOnePage() 0 9 1
A testListForOnePageWithOneSub() 0 12 1
A testListWithHeader() 0 10 1
A textProvider() 0 7 1
A testListWithFooter() 0 10 1
A testListWithoutLinks() 0 9 1
A testListWithOlFormat() 0 13 1
A testListWithTemplate() 0 14 1
A testCannotUseScriptElement() 0 10 1
A testOneLevelOfAdditionalIndent() 0 13 1
A testTwoLevelsOfAdditionalIndent() 0 11 1
A testElementNone() 0 9 1
1
<?php
2
3
namespace Tests\Component\SubPageList;
4
5
use ParamProcessor\Processor;
6
use ParserHooks\HookDefinition;
7
use PHPUnit\Framework\TestCase;
8
use SubPageList\Extension;
9
use SubPageList\Lister\Page;
10
use SubPageList\Settings;
11
use Title;
12
13
/**
14
 * @covers \SubPageList\Lister\UI\WikitextSubPageListRenderer
15
 *
16
 * @group SubPageList
17
 *
18
 * @licence GNU GPL v2+
19
 * @author Jeroen De Dauw < [email protected] >
20
 */
21
class WikitextSubPageListRendererTest extends TestCase {
22
23
	/**
24
	 * @var Page[]
25
	 */
26
	private static $pages;
27
28
	public static function setUpBeforeClass(): void {
29
		$GLOBALS['wgNamespacesWithSubpages'][NS_MAIN] = true;
30
31
		self::$pages = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array('AAA' => new \SubP...FromText('BBB/Sub'))))) of type array<string,object<SubP...eList\\Lister\\Page>"}> is incompatible with the declared type array<integer,object<SubPageList\Lister\Page>> of property $pages.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
32
			// A page with no sub pages
33
			'AAA' => new Page(
34
				Title::newFromText( 'AAA' )
35
			),
36
37
			// A page with one sub page
38
			'BBB' => new Page(
39
				Title::newFromText( 'BBB' ),
40
				[
41
					new Page(
42
						Title::newFromText( 'BBB/Sub' )
43
					)
44
				]
45
			),
46
		];
47
	}
48
49
	private function assertCreatesList( array $params, $listText ) {
50
		$this->assertEquals(
51
			$listText,
52
			$this->getListForParams( $params )
53
		);
54
	}
55
56
	private function getListForParams( array $rawParams ) {
57
		$extension = new Extension( Settings::newFromGlobals( $GLOBALS ) );
58
59
		$definition = $extension->getListHookDefinition();
60
61
		$params = $this->getProcessedParams( $definition, $rawParams );
62
63
		return $extension->newSubPageListRenderer()->render(
64
			self::$pages[$params['page']],
65
			$params
66
		);
67
	}
68
69
	private function getProcessedParams( HookDefinition $definition, array $rawParams ) {
70
		$processor = Processor::newDefault();
71
72
		$processor->setParameters(
73
			$rawParams,
74
			$definition->getParameters()
75
		);
76
77
		$params = [];
78
79
		foreach ( $processor->processParameters()->getParameters() as $param ) {
80
			$params[$param->getName()] = $param->getValue();
81
		}
82
83
		return $params;
84
	}
85
86
	public function testListForOnePage() {
87
		$this->assertCreatesList(
88
			[
89
				'page' => 'AAA',
90
				'showpage' => 'yes',
91
			],
92
			'<div class="subpagelist">' . "\n[[AAA|AAA]]\n</div>"
93
		);
94
	}
95
96
	public function testListForOnePageWithOneSub() {
97
		$this->assertCreatesList(
98
			[
99
				'page' => 'BBB',
100
				'showpage' => 'yes',
101
			],
102
			'<div class="subpagelist">
103
[[BBB|BBB]]
104
* [[BBB/Sub|Sub]]
105
</div>'
106
		);
107
	}
108
109
	/**
110
	 * @dataProvider textProvider
111
	 */
112
	public function testListWithHeader( $introText ) {
113
		$this->assertCreatesList(
114
			[
115
				'page' => 'AAA',
116
				'intro' => $introText,
117
				'showpage' => 'yes',
118
			],
119
			'<div class="subpagelist">' . "\n" .$introText . "\n[[AAA|AAA]]\n</div>"
120
		);
121
	}
122
123
	public function textProvider() {
124
		return [
125
			[ 'a' ],
126
			[ '0' ],
127
			[ '~=[,,_,,]:3' ],
128
		];
129
	}
130
131
	/**
132
	 * @dataProvider textProvider
133
	 */
134
	public function testListWithFooter( $outroText ) {
135
		$this->assertCreatesList(
136
			[
137
				'page' => 'AAA',
138
				'outro' => $outroText,
139
				'showpage' => 'yes',
140
			],
141
			'<div class="subpagelist">' ."\n[[AAA|AAA]]\n" . $outroText . "\n</div>"
142
		);
143
	}
144
145
	public function testListWithoutLinks() {
146
		$this->assertCreatesList(
147
			[
148
				'page' => 'BBB',
149
				'links' => 'no',
150
			],
151
			'<div class="subpagelist">' ."\n* Sub\n</div>"
152
		);
153
	}
154
155
	public function testListWithOlFormat() {
156
		$this->assertCreatesList(
157
			[
158
				'page' => 'BBB',
159
				'showpage' => 'yes',
160
				'format' => 'ol',
161
			],
162
			'<div class="subpagelist">
163
[[BBB|BBB]]
164
# [[BBB/Sub|Sub]]
165
</div>'
166
		);
167
	}
168
169
	public function testListWithTemplate() {
170
		$this->assertCreatesList(
171
			[
172
				'page' => 'BBB',
173
				'showpage' => 'yes',
174
				'format' => 'ol',
175
				'template' => 'foo',
176
			],
177
			'<div class="subpagelist">
178
{{foo|BBB}}
179
# {{foo|Sub}}
180
</div>'
181
		);
182
	}
183
184
	public function testCannotUseScriptElement() {
185
		$badElement = 'script';
186
187
		$this->expectException( 'RuntimeException' );
188
189
		$this->getListForParams( [
190
			'page' => 'BBB',
191
			'element'=> $badElement,
192
		] );
193
	}
194
195
	public function testOneLevelOfAdditionalIndent() {
196
		$this->assertCreatesList(
197
			[
198
				'page' => 'BBB',
199
				'showpage' => 'yes',
200
				'addlevel' => '1',
201
			],
202
			'<div class="subpagelist">
203
* [[BBB|BBB]]
204
** [[BBB/Sub|Sub]]
205
</div>'
206
		);
207
	}
208
209
	public function testTwoLevelsOfAdditionalIndent() {
210
		$this->assertCreatesList(
211
			[
212
				'page' => 'BBB',
213
				'addlevel' => '2',
214
			],
215
			'<div class="subpagelist">
216
*** [[BBB/Sub|Sub]]
217
</div>'
218
		);
219
	}
220
221
	public function testElementNone() {
222
		$this->assertCreatesList(
223
			[
224
				'page' => 'BBB',
225
				'element' => 'none',
226
			],
227
			'* [[BBB/Sub|Sub]]'
228
		);
229
	}
230
}
231