Fix CSS URL innerHTML/cssText escaping bug.
[htmlpurifier.git] / tests / HTMLPurifier / AttrDef / URITest.php
blob3044367a2f27b4fe630dc071262ecb8899880ddb
1 <?php
3 /**
4 * @todo Aim for complete code coverage with mocks
5 */
6 class HTMLPurifier_AttrDef_URITest extends HTMLPurifier_AttrDefHarness
9 function setUp() {
10 $this->def = new HTMLPurifier_AttrDef_URI();
11 parent::setUp();
14 function testIntegration() {
15 $this->assertDef('http://www.google.com/');
16 $this->assertDef('http:', '');
17 $this->assertDef('http:/foo', '/foo');
18 $this->assertDef('javascript:bad_stuff();', false);
19 $this->assertDef('ftp://www.example.com/');
20 $this->assertDef('news:rec.alt');
21 $this->assertDef('nntp://news.example.com/324234');
22 $this->assertDef('mailto:bob@example.com');
25 function testIntegrationWithPercentEncoder() {
26 $this->assertDef(
27 'http://www.example.com/%56%fc%GJ%5%FC',
28 'http://www.example.com/V%FC%25GJ%255%FC'
32 function testPercentEncoding() {
33 $this->assertDef(
34 'http:colon:mercenary',
35 'colon%3Amercenary'
39 function testPercentEncodingPreserve() {
40 $this->assertDef(
41 'http://www.example.com/abcABC123-_.!~*()\''
45 function testEmbeds() {
46 $this->def = new HTMLPurifier_AttrDef_URI(true);
47 $this->assertDef('http://sub.example.com/alas?foo=asd');
48 $this->assertDef('mailto:foo@example.com', false);
51 function testConfigMunge() {
52 $this->config->set('URI.Munge', 'http://www.google.com/url?q=%s');
53 $this->assertDef(
54 'http://www.example.com/',
55 'http://www.google.com/url?q=http%3A%2F%2Fwww.example.com%2F'
57 $this->assertDef('index.html');
58 $this->assertDef('javascript:foobar();', false);
61 function testDefaultSchemeRemovedInBlank() {
62 $this->assertDef('http:', '');
65 function testDefaultSchemeRemovedInRelativeURI() {
66 $this->assertDef('http:/foo/bar', '/foo/bar');
69 function testDefaultSchemeNotRemovedInAbsoluteURI() {
70 $this->assertDef('http://example.com/foo/bar');
73 function testAltSchemeNotRemoved() {
74 $this->assertDef('mailto:this-looks-like-a-path@example.com');
77 function testResolveNullSchemeAmbiguity() {
78 $this->assertDef('///foo', '/foo');
81 function testResolveNullSchemeDoubleAmbiguity() {
82 $this->config->set('URI.Host', 'example.com');
83 $this->assertDef('////foo', '//example.com//foo');
86 function testURIDefinitionValidation() {
87 $parser = new HTMLPurifier_URIParser();
88 $uri = $parser->parse('http://example.com');
89 $this->config->set('URI.DefinitionID', 'HTMLPurifier_AttrDef_URITest->testURIDefinitionValidation');
91 generate_mock_once('HTMLPurifier_URIDefinition');
92 $uri_def = new HTMLPurifier_URIDefinitionMock();
93 $uri_def->expectOnce('filter', array($uri, '*', '*'));
94 $uri_def->setReturnValue('filter', true, array($uri, '*', '*'));
95 $uri_def->expectOnce('postFilter', array($uri, '*', '*'));
96 $uri_def->setReturnValue('postFilter', true, array($uri, '*', '*'));
97 $uri_def->setup = true;
99 // Since definitions are no longer passed by reference, we need
100 // to muck around with the cache to insert our mock. This is
101 // technically a little bad, since the cache shouldn't change
102 // behavior, but I don't feel too good about letting users
103 // overload entire definitions.
104 generate_mock_once('HTMLPurifier_DefinitionCache');
105 $cache_mock = new HTMLPurifier_DefinitionCacheMock();
106 $cache_mock->setReturnValue('get', $uri_def);
108 generate_mock_once('HTMLPurifier_DefinitionCacheFactory');
109 $factory_mock = new HTMLPurifier_DefinitionCacheFactoryMock();
110 $old = HTMLPurifier_DefinitionCacheFactory::instance();
111 HTMLPurifier_DefinitionCacheFactory::instance($factory_mock);
112 $factory_mock->setReturnValue('create', $cache_mock);
114 $this->assertDef('http://example.com');
116 HTMLPurifier_DefinitionCacheFactory::instance($old);
119 function test_make() {
120 $factory = new HTMLPurifier_AttrDef_URI();
121 $def = $factory->make('');
122 $def2 = new HTMLPurifier_AttrDef_URI();
123 $this->assertIdentical($def, $def2);
125 $def = $factory->make('embedded');
126 $def2 = new HTMLPurifier_AttrDef_URI(true);
127 $this->assertIdentical($def, $def2);
131 function test_validate_configWhitelist() {
133 $this->config->set('URI.HostPolicy', 'DenyAll');
134 $this->config->set('URI.HostWhitelist', array(null, 'google.com'));
136 $this->assertDef('http://example.com/fo/google.com', false);
137 $this->assertDef('server.txt');
138 $this->assertDef('ftp://www.google.com/?t=a');
139 $this->assertDef('http://google.com.tricky.spamsite.net', false);
146 // vim: et sw=4 sts=4