'Pathologic path filtering', 'description' => 'Test Pathologic’s path translation and conversion.', 'group' => 'Filter', ); } function setUp() { parent::setUp('pathologic'); } function testPathologic() { // Start by testing our function to build protocol-relative URLs $this->assertEqual( _pathologic_url_to_protocol_relative('http://example.com/foo/bar'), '//example.com/foo/bar', t('Protocol-relative URL creation with http:// URL') ); $this->assertEqual( _pathologic_url_to_protocol_relative('https://example.org/baz'), '//example.org/baz', t('Protocol-relative URL creation with https:// URL') ); // Build a phony filter $filter = new stdClass; $filter->callback = '_pathologic'; $filter->settings = array( 'protocol_style' => 'full', 'local_paths' => '', ); $filter->format = 0; // Build some paths to check against $test_paths = array( 'foo' => array( 'path' => 'foo', 'opts' => array() ), 'foo/bar' => array( 'path' => 'foo/bar', 'opts' => array() ), 'foo/bar?baz' => array( 'path' => 'foo/bar', 'opts' => array('query' => array('baz' => NULL)) ), 'foo/bar?baz=qux' => array( 'path' => 'foo/bar', 'opts' => array('query' => array('baz' => 'qux')) ), 'foo/bar#baz' => array( 'path' => 'foo/bar', 'opts' => array('fragment' => 'baz'), ), 'foo/bar?baz=qux&quux=quuux#quuuux' => array( 'path' => 'foo/bar', 'opts' => array( 'query' => array('baz' => 'qux', 'quux' => 'quuux'), 'fragment' => 'quuuux', ), ), 'foo%20bar?baz=qux%26quux' => array( 'path' => 'foo bar', 'opts' => array( 'query' => array('baz' => 'qux&quux'), ), ), '/' => array( 'path' => '', 'opts' => array(), ), ); // Run tests with clean URLs both enabled and disabled foreach (array(TRUE, FALSE) as $clean_url) { variable_set('clean_url', $clean_url); // Run tests with absoulte filtering enabled and disabled foreach (array('full', 'proto-rel', 'path') as $protocol_style) { $filter->settings['protocol_style'] = $protocol_style; $filter->format++; $paths = array(); foreach ($test_paths as $path => $args) { $args['opts']['absolute'] = $protocol_style !== 'path'; $paths[$path] = _pathologic_content_url($args['path'], $args['opts']); if ($protocol_style === 'proto-rel') { $paths[$path] = _pathologic_url_to_protocol_relative($paths[$path]); } } $t10ns = array( '!clean' => $clean_url ? t('Yes') : t('No'), '!ps' => $protocol_style, ); $this->assertEqual( _pathologic_filter('', $filter, NULL, LANGUAGE_NONE, NULL, NULL), '', t('Simple paths. Clean URLs: !clean; protocol style: !ps.', $t10ns) ); $this->assertEqual( _pathologic_filter('
', $filter, NULL, LANGUAGE_NONE, NULL, NULL), '', t('Paths with query string. Clean URLs: !clean; protocol style: !ps.', $t10ns) ); $this->assertEqual( _pathologic_filter('', $filter, NULL, LANGUAGE_NONE, NULL, NULL), '', t('Path with fragment. Clean URLs: !clean; protocol style: !ps.', $t10ns) ); $this->assertEqual( _pathologic_filter('', $filter, NULL, LANGUAGE_NONE, NULL, NULL), '', t('Fragment-only links. Clean URLs: !clean; protocol style: !ps.', $t10ns) ); $this->assertEqual( _pathologic_filter('', $filter, NULL, LANGUAGE_NONE, NULL, NULL), '', t('Path with query string and fragment. Clean URLs: !clean; protocol style: !ps.', $t10ns) ); $this->assertEqual( _pathologic_filter('', $filter, NULL, LANGUAGE_NONE, NULL, NULL), '', t('Path with URL encoded parts') ); $this->assertEqual( _pathologic_filter('', $filter, NULL, LANGUAGE_NONE, NULL, NULL), '', t('Path with just slash. Clean URLs: !clean; protocol style: !ps', $t10ns) ); } } global $base_path; $this->assertEqual( _pathologic_filter('bar', $filter, NULL, LANGUAGE_NONE, NULL, NULL), 'bar', t('Paths beginning with $base_path (like WYSIWYG editors like to make)') ); global $base_url; $this->assertEqual( _pathologic_filter('bar', $filter, NULL, LANGUAGE_NONE, NULL, NULL), 'bar', t('Paths beginning with $base_url') ); // @see http://drupal.org/node/1617944 $this->assertEqual( _pathologic_filter('bar', $filter, NULL, LANGUAGE_NONE, NULL, NULL), 'bar', t('Off-site schemeless URLs (//example.com/foo) ignored') ); // Test internal: and all base paths $filter->settings = array( 'protocol_style' => 'full', 'local_paths' => "http://example.com/qux\nhttp://example.org\n/bananas", ); $filter->format++; // @see https://drupal.org/node/2030789 $this->assertEqual( _pathologic_filter('bar', $filter, NULL, LANGUAGE_NONE, NULL, NULL), 'bar', t('On-site schemeless URLs processed') ); $this->assertEqual( _pathologic_filter('', $filter, NULL, LANGUAGE_NONE, NULL, NULL), '', t('Path Filter compatibility (internal:)') ); $this->assertEqual( _pathologic_filter('', $filter, NULL, LANGUAGE_NONE, NULL, NULL), '', t('Path Filter compatibility (files:)') ); $this->assertEqual( _pathologic_filter('', $filter, NULL, LANGUAGE_NONE, NULL, NULL), '', t('"All base paths for this site" functionality') ); $this->assertEqual( _pathologic_filter('bar', $filter, NULL, LANGUAGE_NONE, NULL, NULL), 'bar', t('URLs with likely protocols are ignored') ); // Test hook_pathologic_alter() implementation. $this->assertEqual( _pathologic_filter('', $filter, NULL, LANGUAGE_NONE, NULL, NULL), '', t('hook_pathologic_alter(): Alter $url_params') ); $this->assertEqual( _pathologic_filter('', $filter, NULL, LANGUAGE_NONE, NULL, NULL), '', t('hook_pathologic_alter(): Passthrough with use_original option') ); // Test paths to existing files when clean URLs are disabled. // @see http://drupal.org/node/1672430 variable_set('clean_url', FALSE); $filtered_tag = _pathologic_filter('', $filter, NULL, LANGUAGE_NONE, NULL, NULL); $this->assertTrue( strpos($filtered_tag, 'q=') === FALSE, t('Paths to files don\'t have ?q= when clean URLs are off') ); } } /** * Wrapper around url() which does HTML entity decoding and encoding. * * Since Pathologic works with paths in content, it needs to decode paths which * have been HTML-encoded, and re-encode them when done. This is a wrapper * around url() which does the same thing so that we can expect the results * from it and from Pathologic to still match in our tests. * * @see url() * @see http://drupal.org/node/1672932 * @see http://www.w3.org/TR/xhtml1/guidelines.html#C_12 */ function _pathologic_content_url($path, $options) { // If we should pretend this is a path to a file, temporarily enable clean // URLs if necessary. // @see _pathologic_replace() // @see http://drupal.org/node/1672430 if (!empty($options['is_file'])) { $options['orig_clean_url'] = !empty($GLOBALS['conf']['clean_url']); if (!$options['orig_clean_url']) { $GLOBALS['conf']['clean_url'] = TRUE; } } $url = check_plain(url(htmlspecialchars_decode($path), $options)); if (!empty($options['is_file']) && !$options['orig_clean_url']) { $GLOBALS['conf']['clean_url'] = FALSE; } return $url; } /** * Implements hook_pathologic_alter(), for testing that functionality. */ function pathologic_pathologic_alter(&$url_params, $parts, $settings) { if (is_array($parts['qparts']) && isset($parts['qparts']['test'])) { if ($parts['qparts']['test'] === 'add_foo_qpart') { // Add a "foo" query part if (empty($url_params['options']['query'])) { $url_params['options']['query'] = array(); } $url_params['options']['query']['foo'] = 'bar'; } elseif ($parts['qparts']['test'] === 'use_original') { $url_params['options']['use_original'] = TRUE; } } }