Completed
Push — master ( 3df45e...ce7128 )
by Jeroen De
10s
created

testLimitAndAscOrderIsApplied()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Tests\System\SubPageList;
4
5
use ParserHooks\FunctionRunner;
6
use SubPageList\Extension;
7
use SubPageList\Settings;
8
use Title;
9
10
/**
11
 * @group SubPageList
12
 * @licence GNU GPL v2+
13
 * @author Jeroen De Dauw < [email protected] >
14
 */
15
class SubPageListRendererTest extends \PHPUnit_Framework_TestCase {
16
17
	private static $pages = [
18
		// A page with no sub pages
19
		'TempSPLTest:AAA',
20
21
		// A page with one sub page
22
		'TempSPLTest:BBB',
23
		'TempSPLTest:BBB/Sub',
24
25
		// A sub page with no parent
26
		'TempSPLTest:CCC/Sub',
27
28
		// A page with several sub pages
29
		'TempSPLTest:DDD',
30
		'TempSPLTest:DDD/Sub0',
31
		'TempSPLTest:DDD/Sub1',
32
		'TempSPLTest:DDD/Sub2',
33
		'TempSPLTest:DDD/Sub2/Sub',
34
35
		// A page with several sub pages unsorted
36
		'TempSPLTest:Releases',
37
		'TempSPLTest:Releases/1.10',
38
		'TempSPLTest:Releases/1.5',
39
		'TempSPLTest:Releases/1.2',
40
		'TempSPLTest:Releases/1.15',
41
		'TempSPLTest:Releases/2.1',
42
	];
43
44
	/**
45
	 * @var Title[]
46
	 */
47
	private static $titles;
48
49
	public static function setUpBeforeClass() {
0 ignored issues
show
Coding Style introduced by
setUpBeforeClass uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
50
		$GLOBALS['wgNamespacesWithSubpages'][NS_MAIN] = true;
51
52
		foreach ( self::$pages as $pageName ) {
53
			self::createPage( $pageName );
54
		}
55
	}
56
57
	public static function createPage( $titleText ) {
58
		$title = Title::newFromText( $titleText );
59
		self::$titles[] = $title;
60
61
		$pageCreator = new PageCreator();
62
		$pageCreator->createPage( $title );
63
	}
64
65
	public static function tearDownAfterClass() {
66
		$pageDeleter = new PageDeleter();
67
68
		foreach ( self::$titles as $title ) {
69
			$pageDeleter->deletePage( $title );
70
		}
71
	}
72
73
	private function assertCreatesList( array $params, $listText ) {
74
		$this->assertEquals(
75
			$listText,
76
			$this->getListForParams( $params )
77
		);
78
	}
79
80
	private function assertCreatesListWithWrap( array $params, $listText ) {
81
		$this->assertCreatesList(
82
			$params,
83
			'<div class="subpagelist">' . "\n" . $listText . "\n" . '</div>'
84
		);
85
	}
86
87
	private function getListForParams( array $params ) {
88
		$functionParams = [];
89
90
		foreach ( $params as $name => $value ) {
91
			$functionParams[] = $name . '=' . $value;
92
		}
93
94
		return $this->getListForRawParams( $functionParams );
95
	}
96
97
	private function getListForRawParams( array $params ) {
0 ignored issues
show
Coding Style introduced by
getListForRawParams uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
98
		$extension = new Extension( Settings::newFromGlobals( $GLOBALS ) );
99
100
		$functionRunner = new FunctionRunner(
101
			$extension->getListHookDefinition(),
102
			$extension->getListHookHandler()
103
		);
104
105
		$frame = $this->getMock( 'PPFrame' );
106
107
		$frame->expects( $this->exactly( count( $params ) ) )
108
			->method( 'expand' )
109
			->will( $this->returnArgument( 0 ) );
110
111
		$result = $functionRunner->run( $GLOBALS['wgParser'], $params, $frame );
112
113
		return reset( $result );
114
	}
115
116
	public function testListForNonExistingPage() {
117
		$this->assertCreatesListWithWrap(
118
			[
119
				'page' => 'TempSPLTest:ZZZ',
120
				'showpage' => 'yes',
121
			],
122
			"[[TempSPLTest:ZZZ|TempSPLTest:ZZZ]]"
123
		);
124
	}
125
126
	public function testListForExistingPage() {
127
		$this->assertCreatesListWithWrap(
128
			[
129
				'page' => 'TempSPLTest:AAA',
130
				'showpage' => 'yes',
131
			],
132
			"[[TempSPLTest:AAA|TempSPLTest:AAA]]"
133
		);
134
	}
135
136
	public function testListSubPagePageWithParent() {
137
		$this->assertCreatesListWithWrap(
138
			[
139
				'page' => 'TempSPLTest:CCC/Sub',
140
				'showpage' => 'yes',
141
			],
142
			'[[TempSPLTest:CCC|TempSPLTest:CCC]]
143
* [[TempSPLTest:CCC/Sub|Sub]]'
144
		);
145
	}
146
147
	public function testListPageWithSub() {
148
		$this->assertCreatesListWithWrap(
149
			[
150
				'page' => 'TempSPLTest:CCC',
151
				'showpage' => 'yes',
152
			],
153
			'[[TempSPLTest:CCC|TempSPLTest:CCC]]
154
* [[TempSPLTest:CCC/Sub|Sub]]'
155
		);
156
	}
157
158
	public function testListForWithHeader() {
159
		$introText = '~=[,,_,,]:3';
160
161
		$this->assertCreatesListWithWrap(
162
			[
163
				'page' => 'TempSPLTest:AAA',
164
				'intro' => $introText,
165
				'showpage' => 'yes',
166
			],
167
			$introText . "\n[[TempSPLTest:AAA|TempSPLTest:AAA]]"
168
		);
169
	}
170
171
	public function testListForWithHeaderAndFooter() {
172
		$introText = '~=[,,_,,]:3';
173
		$outroText = 'in your test';
174
175
		$this->assertCreatesListWithWrap(
176
			[
177
				'page' => 'TempSPLTest:AAA',
178
				'intro' => $introText,
179
				'outro' => $outroText,
180
				'showpage' => 'yes',
181
			],
182
			$introText . "\n[[TempSPLTest:AAA|TempSPLTest:AAA]]\n" . $outroText
183
		);
184
	}
185
186
	public function testListInvalidPageName() {
187
		$this->assertCreatesList(
188
			[
189
				'page' => 'TempSPLTest:Invalid|Title',
190
			],
191
			'Error: invalid title provided'
192
		);
193
	}
194
195
	public function testDefaultDefaultingBehaviour() {
196
		$this->assertCreatesList(
197
			[
198
				'page' => 'TempSPLTest:DoesNotExist',
199
			],
200
			'"TempSPLTest:DoesNotExist" has no sub pages.'
201
		);
202
	}
203
204
	public function testSpecifiedDefaultingBehaviour() {
205
		$this->assertCreatesList(
206
			[
207
				'page' => 'TempSPLTest:DoesNotExist',
208
				'default' => '~=[,,_,,]:3'
209
			],
210
			'~=[,,_,,]:3'
211
		);
212
	}
213
214
	public function testNullDefaultingBehaviour() {
215
		$this->assertCreatesList(
216
			[
217
				'page' => 'TempSPLTest:DoesNotExist',
218
				'default' => '-'
219
			],
220
			''
221
		);
222
	}
223
224
	public function testListWithMultipleSubPages() {
225
		$this->assertCreatesListWithWrap(
226
			[
227
				'page' => 'TempSPLTest:DDD',
228
			],
229
			'* [[TempSPLTest:DDD/Sub0|Sub0]]
230
* [[TempSPLTest:DDD/Sub1|Sub1]]
231
* [[TempSPLTest:DDD/Sub2|Sub2]]
232
** [[TempSPLTest:DDD/Sub2/Sub|Sub]]'
233
		);
234
	}
235
236
	/**
237
	 * @dataProvider limitProvider
238
	 */
239
	public function testLimitIsApplied( $limit ) {
240
		$list = $this->getListForParams(
241
			[
242
				'page' => 'TempSPLTest:DDD',
243
				'limit' => (string)$limit,
244
			]
245
		);
246
247
		$this->assertEquals(
248
			$limit,
249
			substr_count( $list, '*' )
250
		);
251
	}
252
253
	public function testLimitAndDescOrderIsApplied() {
254
        $this->assertCreatesListWithWrap(
255
            [
256
                'page' => 'TempSPLTest:Releases',
257
                'sort' => 'desc',
258
                'limit' => '3',
259
            ],
260
            '* [[TempSPLTest:Releases/2.1|2.1]]
261
* [[TempSPLTest:Releases/1.5|1.5]]
262
* [[TempSPLTest:Releases/1.2|1.2]]'
263
        );
264
	}
265
266
	public function testLimitAndAscOrderIsApplied() {
267
        $this->assertCreatesListWithWrap(
268
            [
269
                'page' => 'TempSPLTest:Releases',
270
                'sort' => 'asc',
271
                'limit' => '3',
272
            ],
273
            '* [[TempSPLTest:Releases/1.10|1.10]]
274
* [[TempSPLTest:Releases/1.15|1.15]]
275
* [[TempSPLTest:Releases/1.2|1.2]]'
276
        );
277
	}
278
279
	public function limitProvider() {
280
		return [
281
			[ 1 ],
282
			[ 2 ],
283
			[ 3 ],
284
		];
285
	}
286
287
	public function testKidsOnly() {
288
		$this->assertCreatesListWithWrap(
289
			[
290
				'page' => 'TempSPLTest:DDD',
291
				'kidsonly' => 'yes',
292
			],
293
			'* [[TempSPLTest:DDD/Sub0|Sub0]]
294
* [[TempSPLTest:DDD/Sub1|Sub1]]
295
* [[TempSPLTest:DDD/Sub2|Sub2]]'
296
		);
297
	}
298
299
	public function testListWithoutLinks() {
300
		$this->assertCreatesListWithWrap(
301
			[
302
				'page' => 'TempSPLTest:CCC',
303
				'showpage' => 'yes',
304
				'links' => 'no',
305
			],
306
			'TempSPLTest:CCC
307
* Sub'
308
		);
309
	}
310
311
	public function testListWithTemplate() {
312
		$this->assertCreatesListWithWrap(
313
			[
314
				'page' => 'TempSPLTest:CCC',
315
				'pathstyle' => 'full',
316
				'showpage' => 'yes',
317
				'template' => 'MyTemplate',
318
			],
319
			'{{MyTemplate|TempSPLTest:CCC}}
320
* {{MyTemplate|TempSPLTest:CCC/Sub}}'
321
		);
322
	}
323
324
	public function testPageDefaulting() {
325
		self::createPage( 'TempSPLTest:ZZZ/ABC' );
326
		self::createPage( 'TempSPLTest:ZZZ' );
327
328
		$this->assertCreatesListWithWrap(
329
			[
330
				'showpage' => 'yes',
331
				'pathstyle' => 'full',
332
			],
333
			'[[TempSPLTest:ZZZ|TempSPLTest:ZZZ]]
334
* [[TempSPLTest:ZZZ/ABC|TempSPLTest:ZZZ/ABC]]'
335
		);
336
	}
337
338
}
339