Add %HTML.TargetNoreferrer, which adds rel="noreferrer" when target attribute is set
[htmlpurifier.git] / tests / HTMLPurifier / AttrDef / URITest.php
blobd2c5d7ab901f9d4aab66c7e3f298933a0c1cde6b
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 testAltSchemeNotRemoved()
86 $this->assertDef('mailto:this-looks-like-a-path@example.com');
89 public function testResolveNullSchemeAmbiguity()
91 $this->assertDef('///foo', '/foo');
94 public function testResolveNullSchemeDoubleAmbiguity()
96 $this->config->set('URI.Host', 'example.com');
97 $this->assertDef('////foo', '//example.com//foo');
100 public function testURIDefinitionValidation()
102 $parser = new HTMLPurifier_URIParser();
103 $uri = $parser->parse('http://example.com');
104 $this->config->set('URI.DefinitionID', 'HTMLPurifier_AttrDef_URITest->testURIDefinitionValidation');
106 generate_mock_once('HTMLPurifier_URIDefinition');
107 $uri_def = new HTMLPurifier_URIDefinitionMock();
108 $uri_def->expectOnce('filter', array($uri, '*', '*'));
109 $uri_def->returns('filter', true, array($uri, '*', '*'));
110 $uri_def->expectOnce('postFilter', array($uri, '*', '*'));
111 $uri_def->returns('postFilter', true, array($uri, '*', '*'));
112 $uri_def->setup = true;
114 // Since definitions are no longer passed by reference, we need
115 // to muck around with the cache to insert our mock. This is
116 // technically a little bad, since the cache shouldn't change
117 // behavior, but I don't feel too good about letting users
118 // overload entire definitions.
119 generate_mock_once('HTMLPurifier_DefinitionCache');
120 $cache_mock = new HTMLPurifier_DefinitionCacheMock();
121 $cache_mock->returns('get', $uri_def);
123 generate_mock_once('HTMLPurifier_DefinitionCacheFactory');
124 $factory_mock = new HTMLPurifier_DefinitionCacheFactoryMock();
125 $old = HTMLPurifier_DefinitionCacheFactory::instance();
126 HTMLPurifier_DefinitionCacheFactory::instance($factory_mock);
127 $factory_mock->returns('create', $cache_mock);
129 $this->assertDef('http://example.com');
131 HTMLPurifier_DefinitionCacheFactory::instance($old);
134 public function test_make()
136 $factory = new HTMLPurifier_AttrDef_URI();
137 $def = $factory->make('');
138 $def2 = new HTMLPurifier_AttrDef_URI();
139 $this->assertIdentical($def, $def2);
141 $def = $factory->make('embedded');
142 $def2 = new HTMLPurifier_AttrDef_URI(true);
143 $this->assertIdentical($def, $def2);
147 public function test_validate_configWhitelist()
149 $this->config->set('URI.HostPolicy', 'DenyAll');
150 $this->config->set('URI.HostWhitelist', array(null, 'google.com'));
152 $this->assertDef('http://example.com/fo/google.com', false);
153 $this->assertDef('server.txt');
154 $this->assertDef('ftp://www.google.com/?t=a');
155 $this->assertDef('http://google.com.tricky.spamsite.net', false);
162 // vim: et sw=4 sts=4