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