Add vim modelines to all files.
[htmlpurifier.git] / tests / HTMLPurifier / AttrDef / URITest.php
blobee7ff29c51ace191de88d84e912e909b8b83de58
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 testURIDefinitionValidation() {
78 $parser = new HTMLPurifier_URIParser();
79 $uri = $parser->parse('http://example.com');
80 $this->config->set('URI', 'DefinitionID', 'HTMLPurifier_AttrDef_URITest->testURIDefinitionValidation');
82 generate_mock_once('HTMLPurifier_URIDefinition');
83 $uri_def = new HTMLPurifier_URIDefinitionMock();
84 $uri_def->expectOnce('filter', array($uri, '*', '*'));
85 $uri_def->setReturnValue('filter', true, array($uri, '*', '*'));
86 $uri_def->expectOnce('postFilter', array($uri, '*', '*'));
87 $uri_def->setReturnValue('postFilter', true, array($uri, '*', '*'));
88 $uri_def->setup = true;
90 // Since definitions are no longer passed by reference, we need
91 // to muck around with the cache to insert our mock. This is
92 // technically a little bad, since the cache shouldn't change
93 // behavior, but I don't feel too good about letting users
94 // overload entire definitions.
95 generate_mock_once('HTMLPurifier_DefinitionCache');
96 $cache_mock = new HTMLPurifier_DefinitionCacheMock();
97 $cache_mock->setReturnValue('get', $uri_def);
99 generate_mock_once('HTMLPurifier_DefinitionCacheFactory');
100 $factory_mock = new HTMLPurifier_DefinitionCacheFactoryMock();
101 $old = HTMLPurifier_DefinitionCacheFactory::instance();
102 HTMLPurifier_DefinitionCacheFactory::instance($factory_mock);
103 $factory_mock->setReturnValue('create', $cache_mock);
105 $this->assertDef('http://example.com');
107 HTMLPurifier_DefinitionCacheFactory::instance($old);
110 function test_make() {
111 $factory = new HTMLPurifier_AttrDef_URI();
112 $def = $factory->make('');
113 $def2 = new HTMLPurifier_AttrDef_URI();
114 $this->assertIdentical($def, $def2);
116 $def = $factory->make('embedded');
117 $def2 = new HTMLPurifier_AttrDef_URI(true);
118 $this->assertIdentical($def, $def2);
122 function test_validate_configWhitelist() {
124 $this->config->set('URI', 'HostPolicy', 'DenyAll');
125 $this->config->set('URI', 'HostWhitelist', array(null, 'google.com'));
127 $this->assertDef('http://example.com/fo/google.com', false);
128 $this->assertDef('server.txt');
129 $this->assertDef('ftp://www.google.com/?t=a');
130 $this->assertDef('http://google.com.tricky.spamsite.net', false);
137 // vim: et sw=4 sts=4