aboutsummaryrefslogtreecommitdiff
blob: 49dd40fbec91930c211248dd5c15c58e9323b17a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

class phpbb_path_helper_test extends phpbb_test_case
{
	/** @var \phpbb\path_helper */
	protected $path_helper;
	protected $phpbb_root_path = '';

	public function setUp()
	{
		parent::setUp();

		$filesystem = new \phpbb\filesystem\filesystem();
		$this->set_phpbb_root_path($filesystem);

		$this->path_helper = new \phpbb\path_helper(
			new \phpbb\symfony_request(
				new phpbb_mock_request()
			),
			new \phpbb\filesystem\filesystem(),
			$this->getMock('\phpbb\request\request'),
			$this->phpbb_root_path,
			'php'
		);
	}

	/**
	* Set the phpbb_root_path
	*
	* This is necessary because dataProvider functions are called
	*	before setUp or setUpBeforeClass; so we must set the path
	*	any time we wish to use it in one of these functions (and
	*	also in general for everything else)
	*/
	public function set_phpbb_root_path($filesystem)
	{
		$this->phpbb_root_path = $filesystem->clean_path(dirname(__FILE__) . '/../../phpBB/');
	}

	public function test_get_web_root_path()
	{
		// Symfony Request = null, so always should return phpbb_root_path
		$this->assertEquals($this->phpbb_root_path, $this->path_helper->get_web_root_path());
	}

	public function basic_update_web_root_path_data()
	{
		$filesystem = new \phpbb\filesystem\filesystem();
		$this->set_phpbb_root_path($filesystem);

		return array(
			array(
				'http://www.test.com/test.php',
				'http://www.test.com/test.php',
				'/',
			),
			array(
				$this->phpbb_root_path . 'test.php',
				$this->phpbb_root_path . 'test.php',
			),
			array(
				'test.php',
				'test.php',
			),
			array(
				$this->phpbb_root_path . $this->phpbb_root_path . 'test.php',
				$filesystem->clean_path($this->phpbb_root_path . $this->phpbb_root_path . 'test.php'),
			),
		);
	}

	/**
	* @dataProvider basic_update_web_root_path_data
	*/
	public function test_basic_update_web_root_path($input, $expected)
	{
		$this->assertEquals($expected, $this->path_helper->update_web_root_path($input));
	}

	public function update_web_root_path_data()
	{
		$this->set_phpbb_root_path(new \phpbb\filesystem\filesystem());

		return array(
			array(
				$this->phpbb_root_path . 'test.php',
				'/',
				null,
				null,
				'',
			),
			array(
				$this->phpbb_root_path . 'test.php',
				'//',
				null,
				null,
				'./../',
			),
			array(
				$this->phpbb_root_path . 'test.php',
				'//',
				'foo/bar.php',
				'bar.php',
				'./../',
			),
			array(
				$this->phpbb_root_path . 'test.php',
				'/foo/template',
				'/phpbb3-fork/phpBB/app.php/foo/template',
				'/phpbb3-fork/phpBB/app.php',
				'./../../',
			),
			array(
				$this->phpbb_root_path . 'test.php',
				'/foo/template',
				'/phpbb3-fork/phpBB/foo/template',
				'/phpbb3-fork/phpBB/app.php',
				'./../',
			),
			array(
				$this->phpbb_root_path . 'test.php',
				'/',
				'/phpbb3-fork/phpBB/app.php/',
				'/phpbb3-fork/phpBB/app.php',
				'./../',
			),

			// No correction if the path is already prepend by the web root path
			array(
				'./../' . $this->phpbb_root_path . 'test.php',
				'//',
				null,
				null,
				'',
			),
			array(
				'./../' . $this->phpbb_root_path . 'test.php',
				'//',
				'foo/bar.php',
				'bar.php',
				'',
			),
			array(
				'./../../' . $this->phpbb_root_path . 'test.php',
				'/foo/template',
				'/phpbb3-fork/phpBB/app.php/foo/template',
				'/phpbb3-fork/phpBB/app.php',
				'',
			),
			array(
				'./../' . $this->phpbb_root_path . 'test.php',
				'/foo/template',
				'/phpbb3-fork/phpBB/foo/template',
				'/phpbb3-fork/phpBB/app.php',
				'',
			),
			array(
				'./../'.$this->phpbb_root_path . 'test.php',
				'/',
				'/phpbb3-fork/phpBB/app.php/',
				'/phpbb3-fork/phpBB/app.php',
				'',
			),
		);
	}

	/**
	* @dataProvider update_web_root_path_data
	*/
	public function test_update_web_root_path($input, $getPathInfo, $getRequestUri, $getScriptName, $correction)
	{
		$symfony_request = $this->getMock('\phpbb\symfony_request', array(), array(
			new phpbb_mock_request(),
		));
		$symfony_request->expects($this->any())
			->method('getPathInfo')
			->will($this->returnValue($getPathInfo));
		$symfony_request->expects($this->any())
			->method('getRequestUri')
			->will($this->returnValue($getRequestUri));
		$symfony_request->expects($this->any())
			->method('getScriptName')
			->will($this->returnValue($getScriptName));

		$path_helper = new \phpbb\path_helper(
			$symfony_request,
			new \phpbb\filesystem\filesystem(),
			$this->getMock('\phpbb\request\request'),
			$this->phpbb_root_path,
			'php'
		);

		$this->assertEquals($correction . $input, $path_helper->update_web_root_path($input, $symfony_request));
	}

	public function clean_url_data()
	{
		return array(
			array('', ''),
			array('://', '://'),
			array('http://', 'http://'),
			array('http://one/two/three', 'http://one/two/three'),
			array('http://../one/two', 'http://../one/two'),
			array('http://one/../two/three', 'http://two/three'),
			array('http://one/two/../three', 'http://one/three'),
			array('http://one/two/../../three', 'http://three'),
			array('http://one/two/../../../three', 'http://../three'),
		);
	}

	/**
	* @dataProvider clean_url_data
	*/
	public function test_clean_url($input, $expected)
	{
		$this->assertEquals($expected, $this->path_helper->clean_url($input));
	}

	public function glue_url_params_data()
	{
		return array(
			array(
				array(),
				'',
			),
			array(
				array('test' => 'xyz'),
				'test=xyz',
			),
			array(
				array('test' => 'xyz', 'var' => 'value'),
				'test=xyz&amp;var=value',
			),
			array(
				array('test' => null),
				'test',
			),
			array(
				array('test' => null, 'var' => null),
				'test&amp;var',
			),
			array(
				array('test' => 'xyz', 'var' => null, 'bar' => 'value'),
				'test=xyz&amp;var&amp;bar=value',
			),
		);
	}

	/**
	* @dataProvider glue_url_params_data
	*/
	public function test_glue_url_params($params, $expected)
	{
		$this->assertEquals($expected, $this->path_helper->glue_url_params($params));
	}

	public function get_url_parts_data()
	{
		return array(
			array(
				'viewtopic.php',
				true,
				array('base' => 'viewtopic.php', 'params' => array()),
			),
			array(
				'./viewtopic.php?t=5&amp;f=6',
				true,
				array('base' => './viewtopic.php', 'params' => array('t' => '5', 'f' => '6')),
			),
			array(
				'viewtopic.php?t=5&f=6',
				false,
				array('base' => 'viewtopic.php', 'params' => array('t' => '5', 'f' => '6')),
			),
			array(
				'https://phpbb.com/community/viewtopic.php?t=5&amp;f=6',
				true,
				array('base' => 'https://phpbb.com/community/viewtopic.php', 'params' => array('t' => '5', 'f' => '6')),
			),
			array(
				'test.php?topic=post=5&amp;f=3',
				true,
				array('base' => 'test.php', 'params' => array('topic' => 'post=5', 'f' => '3')),
			),
			array(
				'mcp.php?&amp;t=4&amp;f=3',
				true,
				array('base' => 'mcp.php', 'params' => array('t' => '4', 'f' => '3')),
			),
			array(
				'mcp.php?=4&amp;f=3',
				true,
				array('base' => 'mcp.php', 'params' => array('f' => '3')),
			),
			array(
				'index.php?ready',
				false,
				array('base' => 'index.php', 'params' => array('ready' => null)),
			),
			array(
				'index.php?i=1&amp;ready',
				true,
				array('base' => 'index.php', 'params' => array('i' => '1', 'ready' => null)),
			),
			array(
				'index.php?ready&i=1',
				false,
				array('base' => 'index.php', 'params' => array('ready' => null, 'i' => '1')),
			),
		);
	}

	/**
	* @dataProvider get_url_parts_data
	*/
	public function test_get_url_parts($url, $is_amp, $expected)
	{
		$this->assertEquals($expected, $this->path_helper->get_url_parts($url, $is_amp));
	}

	public function strip_url_params_data()
	{
		return array(
			array(
				'viewtopic.php',
				'sid',
				false,
				'viewtopic.php',
			),
			array(
				'./viewtopic.php?t=5&amp;f=6',
				'f',
				true,
				'./viewtopic.php?t=5',
			),
			array(
				'viewtopic.php?t=5&f=6&sid=19adc288814103cbb4625e74e77455aa',
				array('t'),
				false,
				'viewtopic.php?f=6&amp;sid=19adc288814103cbb4625e74e77455aa',
			),
			array(
				'https://phpbb.com/community/viewtopic.php?t=5&amp;f=6',
				array('t', 'f'),
				true,
				'https://phpbb.com/community/viewtopic.php',
			),
		);
	}

	/**
	* @dataProvider strip_url_params_data
	*/
	public function test_strip_url_params($url, $strip, $is_amp, $expected)
	{
		$this->assertEquals($expected, $this->path_helper->strip_url_params($url, $strip, $is_amp));
	}

	public function append_url_params_data()
	{
		return array(
			array(
				'viewtopic.php',
				array(),
				false,
				'viewtopic.php',
			),
			array(
				'./viewtopic.php?t=5&amp;f=6',
				array('t' => '7'),
				true,
				'./viewtopic.php?t=7&amp;f=6',
			),
			array(
				'viewtopic.php?t=5&f=6&sid=19adc288814103cbb4625e74e77455aa',
				array('p' => '5'),
				false,
				'viewtopic.php?t=5&amp;f=6&amp;p=5&amp;sid=19adc288814103cbb4625e74e77455aa',
			),
			array(
				'https://phpbb.com/community/viewtopic.php',
				array('t' => '7', 'f' => '8'),
				true,
				'https://phpbb.com/community/viewtopic.php?t=7&amp;f=8',
			),
		);
	}

	/**
	* @dataProvider append_url_params_data
	*/
	public function test_append_url_params($url, $params, $is_amp, $expected)
	{
		$this->assertEquals($expected, $this->path_helper->append_url_params($url, $params, $is_amp));
	}

	public function get_web_root_path_from_ajax_referer_data()
	{
		return array(
			array(
				'http://www.phpbb.com/community/route1/route2/',
				'http://www.phpbb.com/community',
				'../../',
			),
			array(
				'http://www.phpbb.com/community/route1/route2',
				'http://www.phpbb.com/community',
				'../',
			),
			array(
				'http://www.phpbb.com/community/route1',
				'http://www.phpbb.com/community',
				'',
			),
			array(
				'http://www.phpbb.com/community/',
				'http://www.phpbb.com/community',
				'',
			),
			array(
				'http://www.phpbb.com/notcommunity/route1/route2/',
				'http://www.phpbb.com/community',
				'../../../community/',
			),
			array(
				'http://www.phpbb.com/notcommunity/route1/route2',
				'http://www.phpbb.com/community',
				'../../community/',
			),
			array(
				'http://www.phpbb.com/notcommunity/route1',
				'http://www.phpbb.com/community',
				'../community/',
			),
			array(
				'http://www.phpbb.com/notcommunity/',
				'http://www.phpbb.com/community',
				'../community/',
			),
			array(
				'http://www.phpbb.com/foobar',
				'http://www.phpbb.com',
				'',
			),
			array(
				'http://www.foobar.com',
				'http://www.phpbb.com',
				'/www.phpbb.com/',
			),
			array(
				'foobar',
				'http://www.phpbb.com/community',
				'',
			)
		);
	}

	/**
	* @dataProvider get_web_root_path_from_ajax_referer_data
	*/
	public function test_get_web_root_path_from_ajax_referer($referer_url, $board_url, $expected)
	{
		$this->assertEquals($this->phpbb_root_path . $expected, $this->path_helper->get_web_root_path_from_ajax_referer($referer_url, $board_url));
	}

	public function data_get_valid_page()
	{
		return array(
			// array( current page , mod_rewrite setting , expected output )
			array('index', true, 'index'),
			array('index', false, 'index'),
			array('foo/index', true, 'foo/index'),
			array('foo/index', false, 'foo/index'),
			array('app.php/foo', true, 'foo'),
			array('app.php/foo', false, 'app.php/foo'),
			array('/../app.php/foo', true, '../foo'),
			array('/../app.php/foo', false, '../app.php/foo'),
			array('/../example/app.php/foo/bar', true, '../example/foo/bar'),
			array('/../example/app.php/foo/bar', false, '../example/app.php/foo/bar'),
		);
	}

	/**
	 * @dataProvider data_get_valid_page
	 */
	public function test_get_valid_page($page, $mod_rewrite, $expected)
	{
		$this->assertEquals($this->phpbb_root_path . $expected, $this->path_helper->get_valid_page($page, $mod_rewrite));
	}
}