Update to work with Git version of SimpleTest.
[htmlpurifier.git] / tests / HTMLPurifier / AttrDef / URITest.php
blob01274ca2bb879d2f3770cfbdff63ae1da1a4c2db
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');
27 public function testIntegrationWithPercentEncoder()
29 $this->assertDef(
30 'http://www.example.com/%56%fc%GJ%5%FC',
31 'http://www.example.com/V%FC%25GJ%255%FC'
35 public function testPercentEncoding()
37 $this->assertDef(
38 'http:colon:mercenary',
39 'colon%3Amercenary'
43 public function testPercentEncodingPreserve()
45 $this->assertDef(
46 'http://www.example.com/abcABC123-_.!~*()\''
50 public function testEmbeds()
52 $this->def = new HTMLPurifier_AttrDef_URI(true);
53 $this->assertDef('http://sub.example.com/alas?foo=asd');
54 $this->assertDef('mailto:foo@example.com', false);
57 public function testConfigMunge()
59 $this->config->set('URI.Munge', 'http://www.google.com/url?q=%s');
60 $this->assertDef(
61 'http://www.example.com/',
62 'http://www.google.com/url?q=http%3A%2F%2Fwww.example.com%2F'
64 $this->assertDef('index.html');
65 $this->assertDef('javascript:foobar();', false);
68 public function testDefaultSchemeRemovedInBlank()
70 $this->assertDef('http:', '');
73 public function testDefaultSchemeRemovedInRelativeURI()
75 $this->assertDef('http:/foo/bar', '/foo/bar');
78 public function testDefaultSchemeNotRemovedInAbsoluteURI()
80 $this->assertDef('http://example.com/foo/bar');
83 public function testAltSchemeNotRemoved()
85 $this->assertDef('mailto:this-looks-like-a-path@example.com');
88 public function testResolveNullSchemeAmbiguity()
90 $this->assertDef('///foo', '/foo');
93 public function testResolveNullSchemeDoubleAmbiguity()
95 $this->config->set('URI.Host', 'example.com');
96 $this->assertDef('////foo', '//example.com//foo');
99 public function testURIDefinitionValidation()
101 $parser = new HTMLPurifier_URIParser();
102 $uri = $parser->parse('http://example.com');
103 $this->config->set('URI.DefinitionID', 'HTMLPurifier_AttrDef_URITest->testURIDefinitionValidation');
105 generate_mock_once('HTMLPurifier_URIDefinition');
106 $uri_def = new HTMLPurifier_URIDefinitionMock();
107 $uri_def->expectOnce('filter', array($uri, '*', '*'));
108 $uri_def->returns('filter', true, array($uri, '*', '*'));
109 $uri_def->expectOnce('postFilter', array($uri, '*', '*'));
110 $uri_def->returns('postFilter', true, array($uri, '*', '*'));
111 $uri_def->setup = true;
113 // Since definitions are no longer passed by reference, we need
114 // to muck around with the cache to insert our mock. This is
115 // technically a little bad, since the cache shouldn't change
116 // behavior, but I don't feel too good about letting users
117 // overload entire definitions.
118 generate_mock_once('HTMLPurifier_DefinitionCache');
119 $cache_mock = new HTMLPurifier_DefinitionCacheMock();
120 $cache_mock->returns('get', $uri_def);
122 generate_mock_once('HTMLPurifier_DefinitionCacheFactory');
123 $factory_mock = new HTMLPurifier_DefinitionCacheFactoryMock();
124 $old = HTMLPurifier_DefinitionCacheFactory::instance();
125 HTMLPurifier_DefinitionCacheFactory::instance($factory_mock);
126 $factory_mock->returns('create', $cache_mock);
128 $this->assertDef('http://example.com');
130 HTMLPurifier_DefinitionCacheFactory::instance($old);
133 public function test_make()
135 $factory = new HTMLPurifier_AttrDef_URI();
136 $def = $factory->make('');
137 $def2 = new HTMLPurifier_AttrDef_URI();
138 $this->assertIdentical($def, $def2);
140 $def = $factory->make('embedded');
141 $def2 = new HTMLPurifier_AttrDef_URI(true);
142 $this->assertIdentical($def, $def2);
146 public function test_validate_configWhitelist()
148 $this->config->set('URI.HostPolicy', 'DenyAll');
149 $this->config->set('URI.HostWhitelist', array(null, 'google.com'));
151 $this->assertDef('http://example.com/fo/google.com', false);
152 $this->assertDef('server.txt');
153 $this->assertDef('ftp://www.google.com/?t=a');
154 $this->assertDef('http://google.com.tricky.spamsite.net', false);
161 // vim: et sw=4 sts=4