Mercurial > hg > rr-repo
comparison sites/all/modules/pathologic/pathologic.test @ 4:ce11bbd8f642
added modules
author | danieleb <danielebarchiesi@me.com> |
---|---|
date | Thu, 19 Sep 2013 10:38:44 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
3:b28be78d8160 | 4:ce11bbd8f642 |
---|---|
1 <?php | |
2 | |
3 /** | |
4 * @file | |
5 * Pathologic behavior testing. | |
6 */ | |
7 | |
8 /** | |
9 * Tests that Pathologic ain't broke. | |
10 * | |
11 * We extend FilterUnitTestCase because it has some nice methods that we also | |
12 * want to be able to use. | |
13 * | |
14 * Note to self: The method to pass bits of text through are fail() or pass(). | |
15 */ | |
16 class PathologicTestCase extends DrupalWebTestCase { | |
17 public static function getInfo() { | |
18 return array( | |
19 'name' => 'Pathologic path filtering', | |
20 'description' => 'Test Pathologic’s path translation and conversion.', | |
21 'group' => 'Filter', | |
22 ); | |
23 } | |
24 | |
25 function setUp() { | |
26 parent::setUp('pathologic'); | |
27 } | |
28 | |
29 function testPathologic() { | |
30 // Start by testing our function to build protocol-relative URLs | |
31 $this->assertEqual( | |
32 _pathologic_url_to_protocol_relative('http://example.com/foo/bar'), | |
33 '//example.com/foo/bar', | |
34 t('Protocol-relative URL creation with http:// URL') | |
35 ); | |
36 $this->assertEqual( | |
37 _pathologic_url_to_protocol_relative('https://example.org/baz'), | |
38 '//example.org/baz', | |
39 t('Protocol-relative URL creation with https:// URL') | |
40 ); | |
41 | |
42 // Build a phony filter | |
43 $filter = new stdClass; | |
44 $filter->callback = '_pathologic'; | |
45 $filter->settings = array( | |
46 'protocol_style' => 'full', | |
47 'local_paths' => '', | |
48 ); | |
49 $filter->format = 0; | |
50 | |
51 // Build some paths to check against | |
52 $test_paths = array( | |
53 'foo' => array( | |
54 'path' => 'foo', | |
55 'opts' => array() | |
56 ), | |
57 'foo/bar' => array( | |
58 'path' => 'foo/bar', | |
59 'opts' => array() | |
60 ), | |
61 'foo/bar?baz' => array( | |
62 'path' => 'foo/bar', | |
63 'opts' => array('query' => array('baz' => NULL)) | |
64 ), | |
65 'foo/bar?baz=qux' => array( | |
66 'path' => 'foo/bar', | |
67 'opts' => array('query' => array('baz' => 'qux')) | |
68 ), | |
69 'foo/bar#baz' => array( | |
70 'path' => 'foo/bar', | |
71 'opts' => array('fragment' => 'baz'), | |
72 ), | |
73 'foo/bar?baz=qux&quux=quuux#quuuux' => array( | |
74 'path' => 'foo/bar', | |
75 'opts' => array( | |
76 'query' => array('baz' => 'qux', 'quux' => 'quuux'), | |
77 'fragment' => 'quuuux', | |
78 ), | |
79 ), | |
80 'foo%20bar?baz=qux%26quux' => array( | |
81 'path' => 'foo bar', | |
82 'opts' => array( | |
83 'query' => array('baz' => 'qux&quux'), | |
84 ), | |
85 ), | |
86 '/' => array( | |
87 'path' => '<front>', | |
88 'opts' => array(), | |
89 ), | |
90 ); | |
91 | |
92 // Run tests with clean URLs both enabled and disabled | |
93 foreach (array(TRUE, FALSE) as $clean_url) { | |
94 variable_set('clean_url', $clean_url); | |
95 // Run tests with absoulte filtering enabled and disabled | |
96 foreach (array('full', 'proto-rel', 'path') as $protocol_style) { | |
97 $filter->settings['protocol_style'] = $protocol_style; | |
98 $filter->format++; | |
99 $paths = array(); | |
100 foreach ($test_paths as $path => $args) { | |
101 $args['opts']['absolute'] = $protocol_style !== 'path'; | |
102 $paths[$path] = _pathologic_content_url($args['path'], $args['opts']); | |
103 if ($protocol_style === 'proto-rel') { | |
104 $paths[$path] = _pathologic_url_to_protocol_relative($paths[$path]); | |
105 } | |
106 } | |
107 $t10ns = array( | |
108 '!clean' => $clean_url ? t('Yes') : t('No'), | |
109 '!ps' => $protocol_style, | |
110 ); | |
111 | |
112 $this->assertEqual( | |
113 _pathologic_filter('<a href="foo"><img src="foo/bar" /></a>', $filter, NULL, LANGUAGE_NONE, NULL, NULL), | |
114 '<a href="' . $paths['foo'] . '"><img src="' . $paths['foo/bar'] . '" /></a>', | |
115 t('Simple paths. Clean URLs: !clean; protocol style: !ps.', $t10ns) | |
116 ); | |
117 $this->assertEqual( | |
118 _pathologic_filter('<form action="foo/bar?baz"><IMG LONGDESC="foo/bar?baz=qux" /></a>', $filter, NULL, LANGUAGE_NONE, NULL, NULL), | |
119 '<form action="' . $paths['foo/bar?baz'] . '"><IMG LONGDESC="' . $paths['foo/bar?baz=qux'] . '" /></a>', | |
120 t('Paths with query string. Clean URLs: !clean; protocol style: !ps.', $t10ns) | |
121 ); | |
122 $this->assertEqual( | |
123 _pathologic_filter('<a href="foo/bar#baz">', $filter, NULL, LANGUAGE_NONE, NULL, NULL), | |
124 '<a href="' . $paths['foo/bar#baz'] . '">', | |
125 t('Path with fragment. Clean URLs: !clean; protocol style: !ps.', $t10ns) | |
126 ); | |
127 $this->assertEqual( | |
128 _pathologic_filter('<a href="#foo">', $filter, NULL, LANGUAGE_NONE, NULL, NULL), | |
129 '<a href="#foo">', | |
130 t('Fragment-only links. Clean URLs: !clean; protocol style: !ps.', $t10ns) | |
131 ); | |
132 $this->assertEqual( | |
133 _pathologic_filter('<a href="foo/bar?baz=qux&quux=quuux#quuuux">', $filter, NULL, LANGUAGE_NONE, NULL, NULL), | |
134 '<a href="' . $paths['foo/bar?baz=qux&quux=quuux#quuuux'] . '">', | |
135 t('Path with query string and fragment. Clean URLs: !clean; protocol style: !ps.', $t10ns) | |
136 ); | |
137 $this->assertEqual( | |
138 _pathologic_filter('<a href="foo%20bar?baz=qux%26quux">', $filter, NULL, LANGUAGE_NONE, NULL, NULL), | |
139 '<a href="' . $paths['foo%20bar?baz=qux%26quux'] . '">', | |
140 t('Path with URL encoded parts') | |
141 ); | |
142 $this->assertEqual( | |
143 _pathologic_filter('<a href="/"></a>', $filter, NULL, LANGUAGE_NONE, NULL, NULL), | |
144 '<a href="' . $paths['/'] . '"></a>', | |
145 t('Path with just slash. Clean URLs: !clean; protocol style: !ps', $t10ns) | |
146 ); | |
147 } | |
148 } | |
149 | |
150 global $base_path; | |
151 $this->assertEqual( | |
152 _pathologic_filter('<a href="' . $base_path . 'foo">bar</a>', $filter, NULL, LANGUAGE_NONE, NULL, NULL), | |
153 '<a href="' . _pathologic_content_url('foo', array('absolute' => FALSE)) .'">bar</a>', | |
154 t('Paths beginning with $base_path (like WYSIWYG editors like to make)') | |
155 ); | |
156 global $base_url; | |
157 $this->assertEqual( | |
158 _pathologic_filter('<a href="' . $base_url . '/foo">bar</a>', $filter, NULL, LANGUAGE_NONE, NULL, NULL), | |
159 '<a href="' . _pathologic_content_url('foo', array('absolute' => FALSE)) .'">bar</a>', | |
160 t('Paths beginning with $base_url') | |
161 ); | |
162 | |
163 // @see http://drupal.org/node/1617944 | |
164 $this->assertEqual( | |
165 _pathologic_filter('<a href="//example.com/foo">bar</a>', $filter, NULL, LANGUAGE_NONE, NULL, NULL), | |
166 '<a href="//example.com/foo">bar</a>', | |
167 t('Off-site schemeless URLs (//example.com/foo) ignored') | |
168 ); | |
169 | |
170 // Test internal: and all base paths | |
171 $filter->settings = array( | |
172 'protocol_style' => 'full', | |
173 'local_paths' => "http://example.com/qux\nhttp://example.org\n/bananas", | |
174 ); | |
175 $filter->format++; | |
176 | |
177 // @see https://drupal.org/node/2030789 | |
178 $this->assertEqual( | |
179 _pathologic_filter('<a href="//example.org/foo">bar</a>', $filter, NULL, LANGUAGE_NONE, NULL, NULL), | |
180 '<a href="' . _pathologic_content_url('foo', array('absolute' => TRUE)) . '">bar</a>', | |
181 t('On-site schemeless URLs processed') | |
182 ); | |
183 $this->assertEqual( | |
184 _pathologic_filter('<a href="internal:foo">', $filter, NULL, LANGUAGE_NONE, NULL, NULL), | |
185 '<a href="' . _pathologic_content_url('foo', array('absolute' => TRUE)) . '">', | |
186 t('Path Filter compatibility (internal:)') | |
187 ); | |
188 $this->assertEqual( | |
189 _pathologic_filter('<a href="files:image.jpeg">', $filter, NULL, LANGUAGE_NONE, NULL, NULL), | |
190 '<a href="' . _pathologic_content_url(file_create_url('public://image.jpeg'), array('absolute' => TRUE, 'is_file' => TRUE)) . '">', | |
191 t('Path Filter compatibility (files:)') | |
192 ); | |
193 $this->assertEqual( | |
194 _pathologic_filter('<a href="http://example.com/qux/foo"><img src="http://example.org/bar.jpeg" longdesc="/bananas/baz" /></a>', $filter, NULL, LANGUAGE_NONE, NULL, NULL), | |
195 '<a href="' . _pathologic_content_url('foo', array('absolute' => TRUE)) . '"><img src="' . _pathologic_content_url('bar.jpeg', array('absolute' => TRUE)) . '" longdesc="' . _pathologic_content_url('baz', array('absolute' => TRUE)) . '" /></a>', | |
196 t('"All base paths for this site" functionality') | |
197 ); | |
198 $this->assertEqual( | |
199 _pathologic_filter('<a href="webcal:foo">bar</a>', $filter, NULL, LANGUAGE_NONE, NULL, NULL), | |
200 '<a href="webcal:foo">bar</a>', | |
201 t('URLs with likely protocols are ignored') | |
202 ); | |
203 // Test hook_pathologic_alter() implementation. | |
204 $this->assertEqual( | |
205 _pathologic_filter('<a href="foo?test=add_foo_qpart">', $filter, NULL, LANGUAGE_NONE, NULL, NULL), | |
206 '<a href="' . _pathologic_content_url('foo', array('absolute' => TRUE, 'query' => array('test' => 'add_foo_qpart', 'foo' => 'bar'))) . '">', | |
207 t('hook_pathologic_alter(): Alter $url_params') | |
208 ); | |
209 $this->assertEqual( | |
210 _pathologic_filter('<a href="bar?test=use_original">', $filter, NULL, LANGUAGE_NONE, NULL, NULL), | |
211 '<a href="bar?test=use_original">', | |
212 t('hook_pathologic_alter(): Passthrough with use_original option') | |
213 ); | |
214 | |
215 // Test paths to existing files when clean URLs are disabled. | |
216 // @see http://drupal.org/node/1672430 | |
217 variable_set('clean_url', FALSE); | |
218 $filtered_tag = _pathologic_filter('<img src="misc/druplicon.png" />', $filter, NULL, LANGUAGE_NONE, NULL, NULL); | |
219 $this->assertTrue( | |
220 strpos($filtered_tag, 'q=') === FALSE, | |
221 t('Paths to files don\'t have ?q= when clean URLs are off') | |
222 ); | |
223 | |
224 | |
225 } | |
226 } | |
227 | |
228 /** | |
229 * Wrapper around url() which does HTML entity decoding and encoding. | |
230 * | |
231 * Since Pathologic works with paths in content, it needs to decode paths which | |
232 * have been HTML-encoded, and re-encode them when done. This is a wrapper | |
233 * around url() which does the same thing so that we can expect the results | |
234 * from it and from Pathologic to still match in our tests. | |
235 * | |
236 * @see url() | |
237 * @see http://drupal.org/node/1672932 | |
238 * @see http://www.w3.org/TR/xhtml1/guidelines.html#C_12 | |
239 */ | |
240 function _pathologic_content_url($path, $options) { | |
241 // If we should pretend this is a path to a file, temporarily enable clean | |
242 // URLs if necessary. | |
243 // @see _pathologic_replace() | |
244 // @see http://drupal.org/node/1672430 | |
245 if (!empty($options['is_file'])) { | |
246 $options['orig_clean_url'] = !empty($GLOBALS['conf']['clean_url']); | |
247 if (!$options['orig_clean_url']) { | |
248 $GLOBALS['conf']['clean_url'] = TRUE; | |
249 } | |
250 } | |
251 | |
252 $url = check_plain(url(htmlspecialchars_decode($path), $options)); | |
253 | |
254 if (!empty($options['is_file']) && !$options['orig_clean_url']) { | |
255 $GLOBALS['conf']['clean_url'] = FALSE; | |
256 } | |
257 return $url; | |
258 } | |
259 | |
260 /** | |
261 * Implements hook_pathologic_alter(), for testing that functionality. | |
262 */ | |
263 function pathologic_pathologic_alter(&$url_params, $parts, $settings) { | |
264 if (is_array($parts['qparts']) && isset($parts['qparts']['test'])) { | |
265 if ($parts['qparts']['test'] === 'add_foo_qpart') { | |
266 // Add a "foo" query part | |
267 if (empty($url_params['options']['query'])) { | |
268 $url_params['options']['query'] = array(); | |
269 } | |
270 $url_params['options']['query']['foo'] = 'bar'; | |
271 } | |
272 elseif ($parts['qparts']['test'] === 'use_original') { | |
273 $url_params['options']['use_original'] = TRUE; | |
274 } | |
275 } | |
276 } |