Merge "Added some docs, fixed some docs and made implicit checks explicit"
[lhc/web/wiklou.git] / tests / phpunit / includes / UriTest.php
1 <?php
2
3 class UriTest extends MediaWikiTestCase {
4
5 function setUp() {
6 AutoLoader::loadClass( 'Uri' );
7 }
8
9 function dataUris() {
10 return array(
11 array(
12 'http://example.com/',
13 array(
14 'scheme' => 'http',
15 'delimiter' => '://',
16 'user' => null,
17 'pass' => null,
18 'host' => 'example.com',
19 'port' => null,
20 'path' => '/',
21 'query' => null,
22 'fragment' => null,
23 ),
24 ),
25 array(
26 '//mediawiki.org/wiki/Main_Page',
27 array(
28 'scheme' => null,
29 'delimiter' => '//',
30 'user' => null,
31 'pass' => null,
32 'host' => 'mediawiki.org',
33 'port' => null,
34 'path' => '/wiki/Main_Page',
35 'query' => null,
36 'fragment' => null,
37 ),
38 ),
39 array(
40 'http://user:pass@example.com/',
41 array(
42 'scheme' => 'http',
43 'delimiter' => '://',
44 'user' => 'user',
45 'pass' => 'pass',
46 'host' => 'example.com',
47 'port' => null,
48 'path' => '/',
49 'query' => null,
50 'fragment' => null,
51 ),
52 ),
53 array(
54 '/?asdf=asdf',
55 array(
56 'scheme' => null,
57 'delimiter' => null,
58 'user' => null,
59 'pass' => null,
60 'host' => null,
61 'port' => null,
62 'path' => '/',
63 'query' => 'asdf=asdf',
64 'fragment' => null,
65 ),
66 ),
67 array(
68 '?asdf=asdf#asdf',
69 array(
70 'scheme' => null,
71 'delimiter' => null,
72 'user' => null,
73 'pass' => null,
74 'host' => null,
75 'port' => null,
76 'path' => null,
77 'query' => 'asdf=asdf',
78 'fragment' => 'asdf',
79 ),
80 )
81 );
82 }
83
84 /**
85 * Ensure that get* methods properly match the appropriate getComponent( key ) value
86 * @dataProvider dataUris
87 */
88 function testGetters( $uri ) {
89 $uri = new Uri( $uri );
90 $getterMap = array(
91 'getProtocol' => 'scheme',
92 'getUser' => 'user',
93 'getPassword' => 'pass',
94 'getHost' => 'host',
95 'getPort' => 'port',
96 'getPath' => 'path',
97 'getQueryString' => 'query',
98 'getFragment' => 'fragment',
99 );
100 foreach ( $getterMap as $fn => $c ) {
101 $this->assertSame( $uri->{$fn}(), $uri->getComponent( $c ), "\$uri->{$fn}(); matches \$uri->getComponent( '$c' );" );
102 }
103 }
104
105 /**
106 * Ensure that Uri has the proper components for our example uris
107 * @dataProvider dataUris
108 */
109 function testComponents( $uri, $components ) {
110 $uri = new Uri( $uri );
111
112 $this->assertSame( $components['scheme'], $uri->getProtocol(), 'Correct scheme' );
113 $this->assertSame( $components['delimiter'], $uri->getDelimiter(), 'Correct delimiter' );
114 $this->assertSame( $components['user'], $uri->getUser(), 'Correct user' );
115 $this->assertSame( $components['pass'], $uri->getPassword(), 'Correct pass' );
116 $this->assertSame( $components['host'], $uri->getHost(), 'Correct host' );
117 $this->assertSame( $components['port'], $uri->getPort(), 'Correct port' );
118 $this->assertSame( $components['path'], $uri->getPath(), 'Correct path' );
119 $this->assertSame( $components['query'], $uri->getQueryString(), 'Correct query' );
120 $this->assertSame( $components['fragment'], $uri->getFragment(), 'Correct fragment' );
121 }
122
123 /**
124 * Ensure that the aliases work for various components.
125 */
126 function testAliases() {
127 $url = "//myuser@test.com";
128 $uri = new Uri( $url );
129
130 // Set the aliases.
131 $uri->setComponent( 'protocol', 'https' );
132 $uri->setComponent( 'password', 'mypass' );
133
134 // Now try getting them.
135 $this->assertSame( 'https', $uri->getComponent( 'protocol' ), 'Correct protocol (alias for scheme)' );
136 $this->assertSame( 'mypass', $uri->getComponent( 'password' ), 'Correct password (alias for pass)' );
137
138 // Finally check their actual names.
139 $this->assertSame( 'https', $uri->getProtocol(), 'Alias for scheme works' );
140 $this->assertSame( 'mypass', $uri->getPassword(), 'Alias for pass works' );
141 }
142
143 /**
144 * Ensure that Uri's helper methods return the correct data
145 */
146 function testHelpers() {
147 $uri = new Uri( 'http://a:b@example.com:8080/path?query=value' );
148
149 $this->assertSame( 'a:b', $uri->getUserInfo(), 'Correct getUserInfo' );
150 $this->assertSame( 'example.com:8080', $uri->getHostPort(), 'Correct getHostPort' );
151 $this->assertSame( 'a:b@example.com:8080', $uri->getAuthority(), 'Correct getAuthority' );
152 $this->assertSame( '/path?query=value', $uri->getRelativePath(), 'Correct getRelativePath' );
153 $this->assertSame( 'http://a:b@example.com:8080/path?query=value', $uri->toString(), 'Correct toString' );
154 }
155
156 /**
157 * Ensure that Uri's extend method properly overrides keys
158 */
159 function testExtend() {
160 $uri = new Uri( 'http://example.org/?a=b&hello=world' );
161 $uri->extendQuery( 'a=c&foo=bar' );
162 $this->assertSame( 'a=c&hello=world&foo=bar', $uri->getQueryString() );
163 }
164 }