3 namespace Drupal\pathologic\Tests;
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\simpletest\WebTestBase;
7 use Drupal\pathologic\Plugin\Filter\FilterPathologic;
8 use Drupal\Core\Language\Language;
12 * Tests Pathologic functionality.
16 class PathologicTest extends WebTestBase {
18 public static $modules = ['filter', 'pathologic', 'pathologic_test'];
20 function testPathologic() {
23 // Start by testing our function to build protocol-relative URLs
25 _pathologic_url_to_protocol_relative('http://example.com/foo/bar'),
26 '//example.com/foo/bar',
27 t('Protocol-relative URL creation with http:// URL')
30 _pathologic_url_to_protocol_relative('https://example.org/baz'),
32 t('Protocol-relative URL creation with https:// URL')
35 // Build some paths to check against
47 'opts' => ['query' => ['baz' => NULL]]
49 'foo/bar?baz=qux' => [
51 'opts' => ['query' => ['baz' => 'qux']]
55 'opts' => ['fragment' => 'baz'],
57 'foo/bar?baz=qux&quux=quuux#quuuux' => [
60 'query' => ['baz' => 'qux', 'quux' => 'quuux'],
61 'fragment' => 'quuuux',
64 'foo%20bar?baz=qux%26quux' => [
67 'query' => ['baz' => 'qux&quux'],
76 foreach (['full', 'proto-rel', 'path'] as $protocol_style) {
77 $format_id = _pathologic_build_format(['settings_source' => 'local', 'local_settings' => ['protocol_style' => $protocol_style]]);
79 foreach ($test_paths as $path => $args) {
80 $args['opts']['absolute'] = $protocol_style !== 'path';
81 $paths[$path] = _pathologic_content_url($args['path'], $args['opts']);
82 if ($protocol_style === 'proto-rel') {
83 $paths[$path] = _pathologic_url_to_protocol_relative($paths[$path]);
87 '@clean' => empty($script_path) ? t('Yes') : t('No'),
88 '@ps' => $protocol_style,
92 check_markup('<a href="foo"><img src="foo/bar" /></a>', $format_id),
93 '<a href="' . $paths['foo'] . '"><img src="' . $paths['foo/bar'] . '" /></a>',
94 t('Simple paths. Clean URLs: @clean; protocol style: @ps.', $t10ns)
97 check_markup('<a href="index.php?q=foo"></a><a href="index.php?q=foo/bar&baz=qux"></a>', $format_id),
98 '<a href="' . $paths['foo'] . '"></a><a href="' . $paths['foo/bar?baz=qux'] . '"></a>',
99 t('D7 and earlier-style non-clean URLs. Clean URLs: @clean; protocol style: @ps.', $t10ns)
102 check_markup('<a href="index.php/foo"></a><a href="index.php/foo/bar?baz=qux"></a>', $format_id),
103 '<a href="' . $paths['foo'] . '"></a><a href="' . $paths['foo/bar?baz=qux'] . '"></a>',
104 t('D8-style non-clean URLs. Clean URLs: @clean; protocol style: @ps.', $t10ns)
107 check_markup('<form action="foo/bar?baz"><IMG LONGDESC="foo/bar?baz=qux" /></a>', $format_id),
108 '<form action="' . $paths['foo/bar?baz'] . '"><IMG LONGDESC="' . $paths['foo/bar?baz=qux'] . '" /></a>',
109 t('Paths with query string. Clean URLs: @clean; protocol style: @ps.', $t10ns)
112 check_markup('<a href="foo/bar#baz">', $format_id),
113 '<a href="' . $paths['foo/bar#baz'] . '">',
114 t('Path with fragment. Clean URLs: @clean; protocol style: @ps.', $t10ns)
117 check_markup('<a href="#foo">', $format_id),
119 t('Fragment-only href. Clean URLs: @clean; protocol style: @ps.', $t10ns)
121 // @see https://drupal.org/node/2208223
123 check_markup('<a href="#">', $format_id),
125 t('Hash-only href. Clean URLs: @clean; protocol style: @ps.', $t10ns)
128 check_markup('<a href="foo/bar?baz=qux&quux=quuux#quuuux">', $format_id),
129 '<a href="' . $paths['foo/bar?baz=qux&quux=quuux#quuuux'] . '">',
130 t('Path with query string and fragment. Clean URLs: @clean; protocol style: @ps.', $t10ns)
133 check_markup('<a href="foo%20bar?baz=qux%26quux">', $format_id),
134 '<a href="' . $paths['foo%20bar?baz=qux%26quux'] . '">',
135 t('Path with URL encoded parts. Clean URLs: @clean; protocol style: @ps.', $t10ns)
138 check_markup('<a href="/"></a>', $format_id),
139 '<a href="' . $paths['/'] . '"></a>',
140 t('Path with just slash. Clean URLs: @clean; protocol style: @ps', $t10ns)
146 check_markup('<a href="' . $base_path . 'foo">bar</a>', $format_id),
147 '<a href="' . _pathologic_content_url('foo', ['absolute' => FALSE]) .'">bar</a>',
148 t('Paths beginning with $base_path (like WYSIWYG editors like to make)')
152 check_markup('<a href="' . $base_url . '/foo">bar</a>', $format_id),
153 '<a href="' . _pathologic_content_url('foo', ['absolute' => FALSE]) .'">bar</a>',
154 t('Paths beginning with $base_url')
157 // @see http://drupal.org/node/1617944
159 check_markup('<a href="//example.com/foo">bar</a>', $format_id),
160 '<a href="//example.com/foo">bar</a>',
161 t('Off-site schemeless URLs (//example.com/foo) ignored')
164 // Test internal: and all base paths
165 $format_id = _pathologic_build_format([
166 'settings_source' => 'local',
167 'local_settings' => [
168 'local_paths' => "http://example.com/qux\nhttp://example.org\n/bananas",
169 'protocol_style' => 'full',
173 // @see https://drupal.org/node/2030789
175 check_markup('<a href="//example.org/foo">bar</a>', $format_id),
176 '<a href="' . _pathologic_content_url('foo', ['absolute' => TRUE]) . '">bar</a>',
177 t('On-site schemeless URLs processed')
180 check_markup('<a href="internal:foo">', $format_id),
181 '<a href="' . _pathologic_content_url('foo', ['absolute' => TRUE]) . '">',
182 t('Path Filter compatibility (internal:)')
185 check_markup('<a href="files:image.jpeg">look</a>', $format_id),
186 '<a href="' . _pathologic_content_url(file_create_url(file_default_scheme() . '://image.jpeg'), ['absolute' => TRUE]) . '">look</a>',
187 t('Path Filter compatibility (files:)')
190 check_markup('<a href="http://example.com/qux/foo"><img src="http://example.org/bar.jpeg" longdesc="/bananas/baz" /></a>', $format_id),
191 '<a href="' . _pathologic_content_url('foo', ['absolute' => TRUE]) . '"><img src="' . _pathologic_content_url('bar.jpeg', ['absolute' => TRUE]) . '" longdesc="' . _pathologic_content_url('baz', ['absolute' => TRUE]) . '" /></a>',
192 t('"All base paths for this site" functionality')
195 check_markup('<a href="webcal:foo">bar</a>', $format_id),
196 '<a href="webcal:foo">bar</a>',
197 t('URLs with likely protocols are ignored')
199 // Test hook_pathologic_alter() implementation.
201 check_markup('<a href="foo?test=add_foo_qpart">', $format_id),
202 '<a href="' . _pathologic_content_url('foo', ['absolute' => TRUE, 'query' => ['test' => 'add_foo_qpart', 'foo' => 'bar']]) . '">',
203 t('hook_pathologic_alter(): Alter $url_params')
206 check_markup('<a href="bar?test=use_original">', $format_id),
207 '<a href="bar?test=use_original">',
208 t('hook_pathologic_alter(): Passthrough with use_original option')
211 // Test paths to existing files when clean URLs are disabled.
212 // @see http://drupal.org/node/1672430
214 $filtered_tag = check_markup('<img src="misc/druplicon.png" />', $format_id);
216 strpos($filtered_tag, 'q=') === FALSE,
217 t('Paths to files don\'t have ?q= when clean URLs are off')
220 $format_id = _pathologic_build_format([
221 'settings_source' => 'global',
222 'local_settings' => [
223 'protocol_style' => 'rel',
226 $this->config('pathologic.settings')
227 ->set('protocol_style', 'proto-rel')
228 ->set('local_paths', 'http://example.com/')
231 check_markup('<img src="http://example.com/foo.jpeg" />', $format_id),
232 '<img src="' . _pathologic_url_to_protocol_relative(_pathologic_content_url('foo.jpeg', ['absolute' => TRUE])) . '" />',
233 t('Use global settings when so configured on the format')
236 // Test really broken URLs.
237 // @see https://www.drupal.org/node/2602312
238 $original = '<a href="/Epic:failure">foo</a>';
239 $message = t('Fails sensibly when \Drupal\Core\Url::fromUri() throws exception');
241 $filtered = check_markup($original, $format_id);
248 catch (\Exception $e) {
249 $this->fail($message);
257 * Wrapper around url() which does HTML entity decoding and encoding.
259 * Since Pathologic works with paths in content, it needs to decode paths which
260 * have been HTML-encoded, and re-encode them when done. This is a wrapper
261 * around url() which does the same thing so that we can expect the results
262 * from it and from Pathologic to still match in our tests.
265 * @see http://drupal.org/node/1672932
266 * @see http://www.w3.org/TR/xhtml1/guidelines.html#C_12
268 function _pathologic_content_url($path, $options) {
269 // If we should pretend this is a path to a file, make url() behave like clean
271 // @see _pathologic_replace()
272 // @see http://drupal.org/node/1672430
273 if (!empty($options['is_file'])) {
274 $options['script_path'] = '';
277 if (parse_url($path, PHP_URL_SCHEME) === NULL) {
278 if ($path == '<front>') {
279 return SafeMarkup::checkPlain(Url::fromRoute('<front>', [], $options)->toString());
281 $path = 'base://' . $path;
283 return SafeMarkup::checkPlain(Url::fromUri(htmlspecialchars_decode($path), $options)->toString());
288 * Build a text format with Pathologic configured a certain way.
291 * An array of settings for the Pathologic instance on the format.
293 * A format machine name (consisting of random characters) for the format.
295 function _pathologic_build_format($settings) {
296 $format_id = user_password();
297 $format = entity_create('filter_format', [
298 'format' => $format_id,
299 'name' => $format_id,
301 $format->setFilterConfig('filter_pathologic', [
303 'settings' => $settings,