summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/GlobalFunctions/wfAssembleUrlTest.php
blob: 13f49f793d82ed33b94fd197baf0709320473f0a (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
<?php
/**
 * @group GlobalFunctions
 * @covers ::wfAssembleUrl
 */
class WfAssembleUrlTest extends MediaWikiTestCase {
	/**
	 * @dataProvider provideURLParts
	 */
	public function testWfAssembleUrl( $parts, $output ) {
		$partsDump = print_r( $parts, true );
		$this->assertEquals(
			$output,
			wfAssembleUrl( $parts ),
			"Testing $partsDump assembles to $output"
		);
	}

	/**
	 * Provider of URL parts for testing wfAssembleUrl()
	 *
	 * @return array
	 */
	public static function provideURLParts() {
		$schemes = array(
			'' => array(),
			'//' => array(
				'delimiter' => '//',
			),
			'http://' => array(
				'scheme' => 'http',
				'delimiter' => '://',
			),
		);

		$hosts = array(
			'' => array(),
			'example.com' => array(
				'host' => 'example.com',
			),
			'example.com:123' => array(
				'host' => 'example.com',
				'port' => 123,
			),
			'id@example.com' => array(
				'user' => 'id',
				'host' => 'example.com',
			),
			'id@example.com:123' => array(
				'user' => 'id',
				'host' => 'example.com',
				'port' => 123,
			),
			'id:key@example.com' => array(
				'user' => 'id',
				'pass' => 'key',
				'host' => 'example.com',
			),
			'id:key@example.com:123' => array(
				'user' => 'id',
				'pass' => 'key',
				'host' => 'example.com',
				'port' => 123,
			),
		);

		$cases = array();
		foreach ( $schemes as $scheme => $schemeParts ) {
			foreach ( $hosts as $host => $hostParts ) {
				foreach ( array( '', '/path' ) as $path ) {
					foreach ( array( '', 'query' ) as $query ) {
						foreach ( array( '', 'fragment' ) as $fragment ) {
							$parts = array_merge(
								$schemeParts,
								$hostParts
							);
							$url = $scheme .
								$host .
								$path;

							if ( $path ) {
								$parts['path'] = $path;
							}
							if ( $query ) {
								$parts['query'] = $query;
								$url .= '?' . $query;
							}
							if ( $fragment ) {
								$parts['fragment'] = $fragment;
								$url .= '#' . $fragment;
							}

							$cases[] = array(
								$parts,
								$url,
							);
						}
					}
				}
			}
		}

		$complexURL = 'http://id:key@example.org:321' .
			'/over/there?name=ferret&foo=bar#nose';
		$cases[] = array(
			wfParseUrl( $complexURL ),
			$complexURL,
		);

		return $cases;
	}
}