Completed
Push — modern ( ad95c8...903809 )
by Jeroen De
13:52 queued 03:56
created

SubPageListRendererTest   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 335
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 335
rs 10
c 0
b 0
f 0
wmc 30
lcom 1
cbo 5

27 Methods

Rating   Name   Duplication   Size   Complexity  
A setUpBeforeClass() 0 7 2
A createPage() 0 7 1
A tearDownAfterClass() 0 7 2
A assertCreatesList() 0 6 1
A assertCreatesListWithWrap() 0 6 1
A getListForParams() 0 9 2
A getListForRawParams() 0 19 1
A newParser() 0 9 1
A testListForNonExistingPage() 0 9 1
A testListForExistingPage() 0 9 1
A testListSubPagePageWithParent() 0 10 1
A testListPageWithSub() 0 10 1
A testListForWithHeader() 0 12 1
A testListForWithHeaderAndFooter() 0 14 1
A testListInvalidPageName() 0 8 1
A testDefaultDefaultingBehaviour() 0 8 1
A testSpecifiedDefaultingBehaviour() 0 9 1
A testNullDefaultingBehaviour() 0 9 1
A testListWithMultipleSubPages() 0 11 1
A testLimitIsApplied() 0 13 1
A testLimitAndDescOrderIsApplied() 0 12 1
A testLimitAndAscOrderIsApplied() 0 12 1
A limitProvider() 0 7 1
A testKidsOnly() 0 11 1
A testListWithoutLinks() 0 11 1
A testListWithTemplate() 0 12 1
A testPageDefaulting() 0 13 1
1
<?php
2
3
namespace Tests\System\SubPageList;
4
5
use ParserHooks\FunctionRunner;
6
use PHPUnit\Framework\TestCase;
7
use SubPageList\Extension;
8
use SubPageList\Settings;
9
use Title;
10
11
/**
12
 * @group SubPageList
13
 * @licence GNU GPL v2+
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
class SubPageListRendererTest extends TestCase {
17
18
	private static $pages = [
19
		// A page with no sub pages
20
		'TempSPLTest:AAA',
21
22
		// A page with one sub page
23
		'TempSPLTest:BBB',
24
		'TempSPLTest:BBB/Sub',
25
26
		// A sub page with no parent
27
		'TempSPLTest:CCC/Sub',
28
29
		// A page with several sub pages
30
		'TempSPLTest:DDD',
31
		'TempSPLTest:DDD/Sub0',
32
		'TempSPLTest:DDD/Sub1',
33
		'TempSPLTest:DDD/Sub2',
34
		'TempSPLTest:DDD/Sub2/Sub',
35
36
		// A page with several sub pages unsorted
37
		'TempSPLTest:Releases',
38
		'TempSPLTest:Releases/1.10',
39
		'TempSPLTest:Releases/1.5',
40
		'TempSPLTest:Releases/1.2',
41
		'TempSPLTest:Releases/1.15',
42
		'TempSPLTest:Releases/2.1',
43
	];
44
45
	/**
46
	 * @var Title[]
47
	 */
48
	private static $titles;
49
50
	public static function setUpBeforeClass() {
51
		$GLOBALS['wgNamespacesWithSubpages'][NS_MAIN] = true;
52
53
		foreach ( self::$pages as $pageName ) {
54
			self::createPage( $pageName );
55
		}
56
	}
57
58
	public static function createPage( $titleText ) {
59
		$title = Title::newFromText( $titleText );
60
		self::$titles[] = $title;
61
62
		$pageCreator = new PageCreator();
63
		$pageCreator->createPage( $title );
64
	}
65
66
	public static function tearDownAfterClass() {
67
		$pageDeleter = new PageDeleter();
68
69
		foreach ( self::$titles as $title ) {
70
			$pageDeleter->deletePage( $title );
71
		}
72
	}
73
74
	private function assertCreatesList( array $params, $listText ) {
75
		$this->assertEquals(
76
			$listText,
77
			$this->getListForParams( $params )
78
		);
79
	}
80
81
	private function assertCreatesListWithWrap( array $params, $listText ) {
82
		$this->assertCreatesList(
83
			$params,
84
			'<div class="subpagelist">' . "\n" . $listText . "\n" . '</div>'
85
		);
86
	}
87
88
	private function getListForParams( array $params ) {
89
		$functionParams = [];
90
91
		foreach ( $params as $name => $value ) {
92
			$functionParams[] = $name . '=' . $value;
93
		}
94
95
		return $this->getListForRawParams( $functionParams );
96
	}
97
98
	private function getListForRawParams( array $params ) {
99
		$extension = new Extension( Settings::newFromGlobals( $GLOBALS ) );
100
101
		$functionRunner = new FunctionRunner(
102
			$extension->getListHookDefinition(),
103
			$extension->getListHookHandler()
104
		);
105
106
		$frame = $this->createMock( 'PPFrame' );
107
108
		$frame->expects( $this->exactly( count( $params ) ) )
109
			->method( 'expand' )
110
			->will( $this->returnArgument( 0 ) );
111
112
		$parser = $this->newParser();
113
		$result = $functionRunner->run( $parser, $params, $frame );
114
115
		return reset( $result );
116
	}
117
118
	private function newParser() {
119
		$parser = new \Parser();
120
121
		$parser->mOptions = new \ParserOptions();
122
		$parser->clearState();
123
		$parser->setTitle( \Title::newMainPage() );
124
125
		return $parser;
126
	}
127
128
	public function testListForNonExistingPage() {
129
		$this->assertCreatesListWithWrap(
130
			[
131
				'page' => 'TempSPLTest:ZZZ',
132
				'showpage' => 'yes',
133
			],
134
			"[[TempSPLTest:ZZZ|TempSPLTest:ZZZ]]"
135
		);
136
	}
137
138
	public function testListForExistingPage() {
139
		$this->assertCreatesListWithWrap(
140
			[
141
				'page' => 'TempSPLTest:AAA',
142
				'showpage' => 'yes',
143
			],
144
			"[[TempSPLTest:AAA|TempSPLTest:AAA]]"
145
		);
146
	}
147
148
	public function testListSubPagePageWithParent() {
149
		$this->assertCreatesListWithWrap(
150
			[
151
				'page' => 'TempSPLTest:CCC/Sub',
152
				'showpage' => 'yes',
153
			],
154
			'[[TempSPLTest:CCC|TempSPLTest:CCC]]
155
* [[TempSPLTest:CCC/Sub|Sub]]'
156
		);
157
	}
158
159
	public function testListPageWithSub() {
160
		$this->assertCreatesListWithWrap(
161
			[
162
				'page' => 'TempSPLTest:CCC',
163
				'showpage' => 'yes',
164
			],
165
			'[[TempSPLTest:CCC|TempSPLTest:CCC]]
166
* [[TempSPLTest:CCC/Sub|Sub]]'
167
		);
168
	}
169
170
	public function testListForWithHeader() {
171
		$introText = '~=[,,_,,]:3';
172
173
		$this->assertCreatesListWithWrap(
174
			[
175
				'page' => 'TempSPLTest:AAA',
176
				'intro' => $introText,
177
				'showpage' => 'yes',
178
			],
179
			$introText . "\n[[TempSPLTest:AAA|TempSPLTest:AAA]]"
180
		);
181
	}
182
183
	public function testListForWithHeaderAndFooter() {
184
		$introText = '~=[,,_,,]:3';
185
		$outroText = 'in your test';
186
187
		$this->assertCreatesListWithWrap(
188
			[
189
				'page' => 'TempSPLTest:AAA',
190
				'intro' => $introText,
191
				'outro' => $outroText,
192
				'showpage' => 'yes',
193
			],
194
			$introText . "\n[[TempSPLTest:AAA|TempSPLTest:AAA]]\n" . $outroText
195
		);
196
	}
197
198
	public function testListInvalidPageName() {
199
		$this->assertCreatesList(
200
			[
201
				'page' => 'TempSPLTest:Invalid|Title',
202
			],
203
			'Error: invalid title provided'
204
		);
205
	}
206
207
	public function testDefaultDefaultingBehaviour() {
208
		$this->assertCreatesList(
209
			[
210
				'page' => 'TempSPLTest:DoesNotExist',
211
			],
212
			'"TempSPLTest:DoesNotExist" has no sub pages.'
213
		);
214
	}
215
216
	public function testSpecifiedDefaultingBehaviour() {
217
		$this->assertCreatesList(
218
			[
219
				'page' => 'TempSPLTest:DoesNotExist',
220
				'default' => '~=[,,_,,]:3'
221
			],
222
			'~=[,,_,,]:3'
223
		);
224
	}
225
226
	public function testNullDefaultingBehaviour() {
227
		$this->assertCreatesList(
228
			[
229
				'page' => 'TempSPLTest:DoesNotExist',
230
				'default' => '-'
231
			],
232
			''
233
		);
234
	}
235
236
	public function testListWithMultipleSubPages() {
237
		$this->assertCreatesListWithWrap(
238
			[
239
				'page' => 'TempSPLTest:DDD',
240
			],
241
			'* [[TempSPLTest:DDD/Sub0|Sub0]]
242
* [[TempSPLTest:DDD/Sub1|Sub1]]
243
* [[TempSPLTest:DDD/Sub2|Sub2]]
244
** [[TempSPLTest:DDD/Sub2/Sub|Sub]]'
245
		);
246
	}
247
248
	/**
249
	 * @dataProvider limitProvider
250
	 */
251
	public function testLimitIsApplied( $limit ) {
252
		$list = $this->getListForParams(
253
			[
254
				'page' => 'TempSPLTest:DDD',
255
				'limit' => (string)$limit,
256
			]
257
		);
258
259
		$this->assertEquals(
260
			$limit,
261
			substr_count( $list, '*' )
262
		);
263
	}
264
265
	public function testLimitAndDescOrderIsApplied() {
266
        $this->assertCreatesListWithWrap(
267
            [
268
                'page' => 'TempSPLTest:Releases',
269
                'sort' => 'desc',
270
                'limit' => '3',
271
            ],
272
            '* [[TempSPLTest:Releases/2.1|2.1]]
273
* [[TempSPLTest:Releases/1.5|1.5]]
274
* [[TempSPLTest:Releases/1.2|1.2]]'
275
        );
276
	}
277
278
	public function testLimitAndAscOrderIsApplied() {
279
        $this->assertCreatesListWithWrap(
280
            [
281
                'page' => 'TempSPLTest:Releases',
282
                'sort' => 'asc',
283
                'limit' => '3',
284
            ],
285
            '* [[TempSPLTest:Releases/1.10|1.10]]
286
* [[TempSPLTest:Releases/1.15|1.15]]
287
* [[TempSPLTest:Releases/1.2|1.2]]'
288
        );
289
	}
290
291
	public function limitProvider() {
292
		return [
293
			[ 1 ],
294
			[ 2 ],
295
			[ 3 ],
296
		];
297
	}
298
299
	public function testKidsOnly() {
300
		$this->assertCreatesListWithWrap(
301
			[
302
				'page' => 'TempSPLTest:DDD',
303
				'kidsonly' => 'yes',
304
			],
305
			'* [[TempSPLTest:DDD/Sub0|Sub0]]
306
* [[TempSPLTest:DDD/Sub1|Sub1]]
307
* [[TempSPLTest:DDD/Sub2|Sub2]]'
308
		);
309
	}
310
311
	public function testListWithoutLinks() {
312
		$this->assertCreatesListWithWrap(
313
			[
314
				'page' => 'TempSPLTest:CCC',
315
				'showpage' => 'yes',
316
				'links' => 'no',
317
			],
318
			'TempSPLTest:CCC
319
* Sub'
320
		);
321
	}
322
323
	public function testListWithTemplate() {
324
		$this->assertCreatesListWithWrap(
325
			[
326
				'page' => 'TempSPLTest:CCC',
327
				'pathstyle' => 'full',
328
				'showpage' => 'yes',
329
				'template' => 'MyTemplate',
330
			],
331
			'{{MyTemplate|TempSPLTest:CCC}}
332
* {{MyTemplate|TempSPLTest:CCC/Sub}}'
333
		);
334
	}
335
336
	public function testPageDefaulting() {
337
		self::createPage( 'TempSPLTest:ZZZ/ABC' );
338
		self::createPage( 'TempSPLTest:ZZZ' );
339
340
		$this->assertCreatesListWithWrap(
341
			[
342
				'showpage' => 'yes',
343
				'pathstyle' => 'full',
344
			],
345
			'[[TempSPLTest:ZZZ|TempSPLTest:ZZZ]]
346
* [[TempSPLTest:ZZZ/ABC|TempSPLTest:ZZZ/ABC]]'
347
		);
348
	}
349
350
}
351