Allow %URI.DefaultScheme to be null.
[htmlpurifier.git] / tests / HTMLPurifier / AttrDef / URITest.php
blobf400217325bf6ffb808ce443c06da2137aa1cbe2
1 <?php
3 /**
4 * @todo Aim for complete code coverage with mocks
5 */
6 class HTMLPurifier_AttrDef_URITest extends HTMLPurifier_AttrDefHarness
9 public function setUp()
11 $this->def = new HTMLPurifier_AttrDef_URI();
12 parent::setUp();
15 public function testIntegration()
17 $this->assertDef('http://www.google.com/');
18 $this->assertDef('http:', '');
19 $this->assertDef('http:/foo', '/foo');
20 $this->assertDef('javascript:bad_stuff();', false);
21 $this->assertDef('ftp://www.example.com/');
22 $this->assertDef('news:rec.alt');
23 $this->assertDef('nntp://news.example.com/324234');
24 $this->assertDef('mailto:bob@example.com');
25 $this->assertDef('tel:+15555555555');
28 public function testIntegrationWithPercentEncoder()
30 $this->assertDef(
31 'http://www.example.com/%56%fc%GJ%5%FC',
32 'http://www.example.com/V%FC%25GJ%255%FC'
36 public function testPercentEncoding()
38 $this->assertDef(
39 'http:colon:mercenary',
40 'colon%3Amercenary'
44 public function testPercentEncodingPreserve()
46 $this->assertDef(
47 'http://www.example.com/abcABC123-_.!~*()\''
51 public function testEmbeds()
53 $this->def = new HTMLPurifier_AttrDef_URI(true);
54 $this->assertDef('http://sub.example.com/alas?foo=asd');
55 $this->assertDef('mailto:foo@example.com', false);
58 public function testConfigMunge()
60 $this->config->set('URI.Munge', 'http://www.google.com/url?q=%s');
61 $this->assertDef(
62 'http://www.example.com/',
63 'http://www.google.com/url?q=http%3A%2F%2Fwww.example.com%2F'
65 $this->assertDef('index.html');
66 $this->assertDef('javascript:foobar();', false);
69 public function testDefaultSchemeRemovedInBlank()
71 $this->assertDef('http:', '');
74 public function testDefaultSchemeRemovedInRelativeURI()
76 $this->assertDef('http:/foo/bar', '/foo/bar');
79 public function testDefaultSchemeNotRemovedInAbsoluteURI()
81 $this->assertDef('http://example.com/foo/bar');
84 public function testDefaultSchemeNull()
86 $this->config->set('URI.DefaultScheme', null);
87 $this->assertDef('foo', false);
90 public function testAltSchemeNotRemoved()
92 $this->assertDef('mailto:this-looks-like-a-path@example.com');
95 public function testResolveNullSchemeAmbiguity()
97 $this->assertDef('///foo', '/foo');
100 public function testResolveNullSchemeDoubleAmbiguity()
102 $this->config->set('URI.Host', 'example.com');
103 $this->assertDef('////foo', '//example.com//foo');
106 public function testURIDefinitionValidation()
108 $parser = new HTMLPurifier_URIParser();
109 $uri = $parser->parse('http://example.com');
110 $this->config->set('URI.DefinitionID', 'HTMLPurifier_AttrDef_URITest->testURIDefinitionValidation');
112 generate_mock_once('HTMLPurifier_URIDefinition');
113 $uri_def = new HTMLPurifier_URIDefinitionMock();
114 $uri_def->expectOnce('filter', array($uri, '*', '*'));
115 $uri_def->returns('filter', true, array($uri, '*', '*'));
116 $uri_def->expectOnce('postFilter', array($uri, '*', '*'));
117 $uri_def->returns('postFilter', true, array($uri, '*', '*'));
118 $uri_def->setup = true;
120 // Since definitions are no longer passed by reference, we need
121 // to muck around with the cache to insert our mock. This is
122 // technically a little bad, since the cache shouldn't change
123 // behavior, but I don't feel too good about letting users
124 // overload entire definitions.
125 generate_mock_once('HTMLPurifier_DefinitionCache');
126 $cache_mock = new HTMLPurifier_DefinitionCacheMock();
127 $cache_mock->returns('get', $uri_def);
129 generate_mock_once('HTMLPurifier_DefinitionCacheFactory');
130 $factory_mock = new HTMLPurifier_DefinitionCacheFactoryMock();
131 $old = HTMLPurifier_DefinitionCacheFactory::instance();
132 HTMLPurifier_DefinitionCacheFactory::instance($factory_mock);
133 $factory_mock->returns('create', $cache_mock);
135 $this->assertDef('http://example.com');
137 HTMLPurifier_DefinitionCacheFactory::instance($old);
140 public function test_make()
142 $factory = new HTMLPurifier_AttrDef_URI();
143 $def = $factory->make('');
144 $def2 = new HTMLPurifier_AttrDef_URI();
145 $this->assertIdentical($def, $def2);
147 $def = $factory->make('embedded');
148 $def2 = new HTMLPurifier_AttrDef_URI(true);
149 $this->assertIdentical($def, $def2);
153 public function test_validate_configWhitelist()
155 $this->config->set('URI.HostPolicy', 'DenyAll');
156 $this->config->set('URI.HostWhitelist', array(null, 'google.com'));
158 $this->assertDef('http://example.com/fo/google.com', false);
159 $this->assertDef('server.txt');
160 $this->assertDef('ftp://www.google.com/?t=a');
161 $this->assertDef('http://google.com.tricky.spamsite.net', false);
168 // vim: et sw=4 sts=4