Completed
Push — master ( f6659b...84ef1f )
by Jeroen De
14s queued 10s
created

SubPageListRendererTest::newParser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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