21fff68403d3748a2a490f55d7468598d742cc7f
[lhc/web/wiklou.git] / tests / phpunit / includes / GlobalFunctions / wfAssembleUrlTest.php
1 <?php
2 /**
3 * Unit tests for wfAssembleUrl()
4 */
5
6 class wfAssembleUrl extends MediaWikiTestCase {
7 /** @dataProvider provideURLParts */
8 public function testWfAssembleUrl( $parts, $output ) {
9 $partsDump = print_r( $parts, true );
10 $this->assertEquals(
11 $output,
12 wfAssembleUrl( $parts ),
13 "Testing $partsDump assembles to $output"
14 );
15 }
16
17 /**
18 * Provider of URL parts for testing wfAssembleUrl()
19 *
20 * @return array
21 */
22 public static function provideURLParts() {
23 $schemes = array(
24 '' => array(),
25 '//' => array(
26 'delimiter' => '//',
27 ),
28 'http://' => array(
29 'scheme' => 'http',
30 'delimiter' => '://',
31 ),
32 );
33
34 $hosts = array(
35 '' => array(),
36 'example.com' => array(
37 'host' => 'example.com',
38 ),
39 'example.com:123' => array(
40 'host' => 'example.com',
41 'port' => 123,
42 ),
43 'id@example.com' => array(
44 'user' => 'id',
45 'host' => 'example.com',
46 ),
47 'id@example.com:123' => array(
48 'user' => 'id',
49 'host' => 'example.com',
50 'port' => 123,
51 ),
52 'id:key@example.com' => array(
53 'user' => 'id',
54 'pass' => 'key',
55 'host' => 'example.com',
56 ),
57 'id:key@example.com:123' => array(
58 'user' => 'id',
59 'pass' => 'key',
60 'host' => 'example.com',
61 'port' => 123,
62 ),
63 );
64
65 $cases = array();
66 foreach ( $schemes as $scheme => $schemeParts ) {
67 foreach ( $hosts as $host => $hostParts ) {
68 foreach ( array( '', '/path' ) as $path ) {
69 foreach ( array( '', 'query' ) as $query ) {
70 foreach ( array( '', 'fragment' ) as $fragment ) {
71 $parts = array_merge(
72 $schemeParts,
73 $hostParts
74 );
75 $url = $scheme .
76 $host .
77 $path;
78
79 if ( $path ) {
80 $parts['path'] = $path;
81 }
82 if ( $query ) {
83 $parts['query'] = $query;
84 $url .= '?' . $query;
85 }
86 if ( $fragment ) {
87 $parts['fragment'] = $fragment;
88 $url .= '#' . $fragment;
89 }
90
91
92 $cases[] = array(
93 $parts,
94 $url,
95 );
96 }
97 }
98 }
99 }
100 }
101
102 $complexURL = 'http://id:key@example.org:321' .
103 '/over/there?name=ferret&foo=bar#nose';
104 $cases[] = array(
105 wfParseUrl( $complexURL ),
106 $complexURL,
107 );
108
109 return $cases;
110 }
111 }