Prepended constants with C_ to avoid reserved words as per https://www.php.net/manual...
[htmlpurifier/darkodev.git] / tests / HTMLPurifier / URITest.php
blob91509d69153c4ad9b250bdcff91394180ee4a4f1
1 <?php
3 class HTMLPurifier_URITest extends HTMLPurifier_URIHarness
6 protected function createURI($uri)
8 $parser = new HTMLPurifier_URIParser();
9 return $parser->parse($uri);
12 public function test_construct()
14 $uri1 = new HTMLPurifier_URI('HTTP', 'bob', 'example.com', '23', '/foo', 'bar=2', 'slash');
15 $uri2 = new HTMLPurifier_URI('http', 'bob', 'example.com', 23, '/foo', 'bar=2', 'slash');
16 $this->assertIdentical($uri1, $uri2);
19 protected $oldRegistry;
21 protected function &setUpSchemeRegistryMock() {
22 $this->oldRegistry = HTMLPurifier_URISchemeRegistry::instance();
23 generate_mock_once('HTMLPurifier_URIScheme');
24 generate_mock_once('HTMLPurifier_URISchemeRegistry');
25 $registry = HTMLPurifier_URISchemeRegistry::instance(
26 new HTMLPurifier_URISchemeRegistryMock()
28 return $registry;
31 protected function setUpSchemeMock($name)
33 $registry = $this->setUpSchemeRegistryMock();
34 $scheme_mock = new HTMLPurifier_URISchemeMock();
35 $registry->returns('getScheme', $scheme_mock, array($name, '*', '*'));
36 return $scheme_mock;
39 protected function setUpNoValidSchemes()
41 $registry = $this->setUpSchemeRegistryMock();
42 $registry->returns('getScheme', false, array('*', '*', '*'));
45 protected function tearDownSchemeRegistryMock()
47 HTMLPurifier_URISchemeRegistry::instance($this->oldRegistry);
50 public function test_getSchemeObj()
52 $scheme_mock = $this->setUpSchemeMock('http');
54 $uri = $this->createURI('http:');
55 $scheme_obj = $uri->getSchemeObj($this->config, $this->context);
56 $this->assertIdentical($scheme_obj, $scheme_mock);
58 $this->tearDownSchemeRegistryMock();
61 public function test_getSchemeObj_invalidScheme()
63 $this->setUpNoValidSchemes();
65 $uri = $this->createURI('http:');
66 $result = $uri->getSchemeObj($this->config, $this->context);
67 $this->assertIdentical($result, false);
69 $this->tearDownSchemeRegistryMock();
72 public function test_getSchemaObj_defaultScheme()
74 $scheme = 'foobar';
76 $scheme_mock = $this->setUpSchemeMock($scheme);
77 $this->config->set('URI.DefaultScheme', $scheme);
79 $uri = $this->createURI('hmm');
80 $scheme_obj = $uri->getSchemeObj($this->config, $this->context);
81 $this->assertIdentical($scheme_obj, $scheme_mock);
83 $this->tearDownSchemeRegistryMock();
86 public function test_getSchemaObj_invalidDefaultScheme()
88 $this->setUpNoValidSchemes();
89 $this->config->set('URI.DefaultScheme', 'foobar');
91 $uri = $this->createURI('hmm');
93 $this->expectError('Default scheme object "foobar" was not readable');
94 $result = $uri->getSchemeObj($this->config, $this->context);
95 $this->assertIdentical($result, false);
97 $this->tearDownSchemeRegistryMock();
100 protected function assertToString($expect_uri, $scheme, $userinfo, $host, $port, $path, $query, $fragment)
102 $uri = new HTMLPurifier_URI($scheme, $userinfo, $host, $port, $path, $query, $fragment);
103 $string = $uri->toString();
104 $this->assertIdentical($string, $expect_uri);
107 public function test_toString_full()
109 $this->assertToString(
110 'http://bob@example.com:300/foo?bar=baz#fragment',
111 'http', 'bob', 'example.com', 300, '/foo', 'bar=baz', 'fragment'
115 public function test_toString_scheme()
117 $this->assertToString(
118 'http:',
119 'http', null, null, null, '', null, null
123 public function test_toString_authority()
125 $this->assertToString(
126 '//bob@example.com:8080',
127 null, 'bob', 'example.com', 8080, '', null, null
131 public function test_toString_path()
133 $this->assertToString(
134 '/path/to',
135 null, null, null, null, '/path/to', null, null
139 public function test_toString_query()
141 $this->assertToString(
142 '?q=string',
143 null, null, null, null, '', 'q=string', null
147 public function test_toString_fragment()
149 $this->assertToString(
150 '#fragment',
151 null, null, null, null, '', null, 'fragment'
155 protected function assertValidation($uri, $expect_uri = true)
157 if ($expect_uri === true) $expect_uri = $uri;
158 $uri = $this->createURI($uri);
159 $result = $uri->validate($this->config, $this->context);
160 if ($expect_uri === false) {
161 $this->assertFalse($result);
162 } else {
163 $this->assertTrue($result);
164 $this->assertIdentical($uri->toString(), $expect_uri);
168 public function test_validate_overlongPort()
170 $this->assertValidation('http://example.com:65536', 'http://example.com');
173 public function test_validate_zeroPort()
175 $this->assertValidation('http://example.com:00', 'http://example.com');
178 public function test_validate_invalidHostThatLooksLikeIPv6()
180 $this->assertValidation('http://[2001:0db8:85z3:08d3:1319:8a2e:0370:7334]', '');
183 public function test_validate_removeRedundantScheme()
185 $this->assertValidation('http:foo:/:', 'foo%3A/:');
188 public function test_validate_username()
190 $this->assertValidation("http://user\xE3\x91\x94:@foo.com", 'http://user%E3%91%94:@foo.com');
193 public function test_validate_path_abempty()
195 $this->assertValidation("http://host/\xE3\x91\x94:", 'http://host/%E3%91%94:');
198 public function test_validate_path_absolute()
200 $this->assertValidation("/\xE3\x91\x94:", '/%E3%91%94:');
203 public function test_validate_path_rootless()
205 $this->assertValidation("mailto:\xE3\x91\x94:", 'mailto:%E3%91%94:');
208 public function test_validate_path_noscheme()
210 $this->assertValidation("\xE3\x91\x94", '%E3%91%94');
213 public function test_validate_query()
215 $this->assertValidation("?/\xE3\x91\x94", '?/%E3%91%94');
218 public function test_validate_fragment()
220 $this->assertValidation("#/\xE3\x91\x94", '#/%E3%91%94');
223 public function test_validate_path_empty()
225 $this->assertValidation('http://google.com');
230 // vim: et sw=4 sts=4