Make all of the tests work on all PHP versions.
[htmlpurifier.git] / tests / HTMLPurifier / URITest.php
blob02b95013382a767afb84732b501f07ff215a5e3d
1 <?php
3 class HTMLPurifier_URITest extends HTMLPurifier_URIHarness
6 protected function createURI($uri) {
7 $parser = new HTMLPurifier_URIParser();
8 return $parser->parse($uri);
11 function test_construct() {
12 $uri1 = new HTMLPurifier_URI('HTTP', 'bob', 'example.com', '23', '/foo', 'bar=2', 'slash');
13 $uri2 = new HTMLPurifier_URI('http', 'bob', 'example.com', 23, '/foo', 'bar=2', 'slash');
14 $this->assertIdentical($uri1, $uri2);
17 protected $oldRegistry;
19 protected function &setUpSchemeRegistryMock() {
20 $this->oldRegistry = HTMLPurifier_URISchemeRegistry::instance();
21 generate_mock_once('HTMLPurifier_URIScheme');
22 generate_mock_once('HTMLPurifier_URISchemeRegistry');
23 $registry = HTMLPurifier_URISchemeRegistry::instance(
24 new HTMLPurifier_URISchemeRegistryMock()
26 return $registry;
29 protected function setUpSchemeMock($name) {
30 $registry = $this->setUpSchemeRegistryMock();
31 $scheme_mock = new HTMLPurifier_URISchemeMock();
32 $registry->setReturnValue('getScheme', $scheme_mock, array($name, '*', '*'));
33 return $scheme_mock;
36 protected function setUpNoValidSchemes() {
37 $registry = $this->setUpSchemeRegistryMock();
38 $registry->setReturnValue('getScheme', false, array('*', '*', '*'));
41 protected function tearDownSchemeRegistryMock() {
42 HTMLPurifier_URISchemeRegistry::instance($this->oldRegistry);
45 function test_getSchemeObj() {
46 $scheme_mock = $this->setUpSchemeMock('http');
48 $uri = $this->createURI('http:');
49 $scheme_obj = $uri->getSchemeObj($this->config, $this->context);
50 $this->assertIdentical($scheme_obj, $scheme_mock);
52 $this->tearDownSchemeRegistryMock();
55 function test_getSchemeObj_invalidScheme() {
56 $this->setUpNoValidSchemes();
58 $uri = $this->createURI('http:');
59 $result = $uri->getSchemeObj($this->config, $this->context);
60 $this->assertIdentical($result, false);
62 $this->tearDownSchemeRegistryMock();
65 function test_getSchemaObj_defaultScheme() {
66 $scheme = 'foobar';
68 $scheme_mock = $this->setUpSchemeMock($scheme);
69 $this->config->set('URI.DefaultScheme', $scheme);
71 $uri = $this->createURI('hmm');
72 $scheme_obj = $uri->getSchemeObj($this->config, $this->context);
73 $this->assertIdentical($scheme_obj, $scheme_mock);
75 $this->tearDownSchemeRegistryMock();
78 function test_getSchemaObj_invalidDefaultScheme() {
79 $this->setUpNoValidSchemes();
80 $this->config->set('URI.DefaultScheme', 'foobar');
82 $uri = $this->createURI('hmm');
84 $this->expectError('Default scheme object "foobar" was not readable');
85 $result = $uri->getSchemeObj($this->config, $this->context);
86 $this->assertIdentical($result, false);
88 $this->tearDownSchemeRegistryMock();
91 protected function assertToString($expect_uri, $scheme, $userinfo, $host, $port, $path, $query, $fragment) {
92 $uri = new HTMLPurifier_URI($scheme, $userinfo, $host, $port, $path, $query, $fragment);
93 $string = $uri->toString();
94 $this->assertIdentical($string, $expect_uri);
97 function test_toString_full() {
98 $this->assertToString(
99 'http://bob@example.com:300/foo?bar=baz#fragment',
100 'http', 'bob', 'example.com', 300, '/foo', 'bar=baz', 'fragment'
104 function test_toString_scheme() {
105 $this->assertToString(
106 'http:',
107 'http', null, null, null, '', null, null
111 function test_toString_authority() {
112 $this->assertToString(
113 '//bob@example.com:8080',
114 null, 'bob', 'example.com', 8080, '', null, null
118 function test_toString_path() {
119 $this->assertToString(
120 '/path/to',
121 null, null, null, null, '/path/to', null, null
125 function test_toString_query() {
126 $this->assertToString(
127 '?q=string',
128 null, null, null, null, '', 'q=string', null
132 function test_toString_fragment() {
133 $this->assertToString(
134 '#fragment',
135 null, null, null, null, '', null, 'fragment'
139 protected function assertValidation($uri, $expect_uri = true) {
140 if ($expect_uri === true) $expect_uri = $uri;
141 $uri = $this->createURI($uri);
142 $result = $uri->validate($this->config, $this->context);
143 if ($expect_uri === false) {
144 $this->assertFalse($result);
145 } else {
146 $this->assertTrue($result);
147 $this->assertIdentical($uri->toString(), $expect_uri);
151 function test_validate_overlongPort() {
152 $this->assertValidation('http://example.com:65536', 'http://example.com');
155 function test_validate_zeroPort() {
156 $this->assertValidation('http://example.com:00', 'http://example.com');
159 function test_validate_invalidHostThatLooksLikeIPv6() {
160 $this->assertValidation('http://[2001:0db8:85z3:08d3:1319:8a2e:0370:7334]', '');
163 function test_validate_removeRedundantScheme() {
164 $this->assertValidation('http:foo:/:', 'foo%3A/:');
167 function test_validate_username() {
168 $this->assertValidation("http://user\xE3\x91\x94:@foo.com", 'http://user%E3%91%94:@foo.com');
171 function test_validate_path_abempty() {
172 $this->assertValidation("http://host/\xE3\x91\x94:", 'http://host/%E3%91%94:');
175 function test_validate_path_absolute() {
176 $this->assertValidation("/\xE3\x91\x94:", '/%E3%91%94:');
179 function test_validate_path_rootless() {
180 $this->assertValidation("mailto:\xE3\x91\x94:", 'mailto:%E3%91%94:');
183 function test_validate_path_noscheme() {
184 $this->assertValidation("\xE3\x91\x94", '%E3%91%94');
187 function test_validate_query() {
188 $this->assertValidation("?/\xE3\x91\x94", '?/%E3%91%94');
191 function test_validate_fragment() {
192 $this->assertValidation("#/\xE3\x91\x94", '#/%E3%91%94');
195 function test_validate_path_empty() {
196 $this->assertValidation('http://google.com');
201 // vim: et sw=4 sts=4