summaryrefslogtreecommitdiff
path: root/kolab.org/www/drupal-7.26/sites/all/modules/pathologic/pathologic.test
blob: c82d011ba10a111b1638e6c408e08760a08555b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php

/**
 * @file
 * Pathologic behavior testing.
 */

/**
 * Tests that Pathologic ain't broke.
 *
 * We extend FilterUnitTestCase because it has some nice methods that we also
 * want to be able to use.
 *
 * Note to self: The method to pass bits of text through are fail() or pass().
 */
class PathologicTestCase extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Pathologic path filtering',
      'description' => 'Test Pathologic&rsquo;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&amp;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' => '<front>',
        '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('<a href="foo"><img src="foo/bar" /></a>', $filter, NULL, LANGUAGE_NONE, NULL, NULL),
          '<a href="' . $paths['foo'] . '"><img src="' . $paths['foo/bar'] . '" /></a>',
          t('Simple paths. Clean URLs: !clean; protocol style: !ps.', $t10ns)
        );
        $this->assertEqual(
          _pathologic_filter('<form action="foo/bar?baz"><IMG LONGDESC="foo/bar?baz=qux" /></a>', $filter, NULL, LANGUAGE_NONE, NULL, NULL),
          '<form action="' . $paths['foo/bar?baz'] . '"><IMG LONGDESC="' . $paths['foo/bar?baz=qux'] . '" /></a>',
          t('Paths with query string. Clean URLs: !clean; protocol style: !ps.', $t10ns)
        );
        $this->assertEqual(
          _pathologic_filter('<a href="foo/bar#baz">', $filter, NULL, LANGUAGE_NONE, NULL, NULL),
          '<a href="' . $paths['foo/bar#baz'] . '">',
          t('Path with fragment. Clean URLs: !clean; protocol style: !ps.', $t10ns)
        );
        $this->assertEqual(
          _pathologic_filter('<a href="#foo">', $filter, NULL, LANGUAGE_NONE, NULL, NULL),
          '<a href="#foo">',
          t('Fragment-only links. Clean URLs: !clean; protocol style: !ps.', $t10ns)
        );
        $this->assertEqual(
          _pathologic_filter('<a href="foo/bar?baz=qux&amp;quux=quuux#quuuux">', $filter, NULL, LANGUAGE_NONE, NULL, NULL),
          '<a href="' . $paths['foo/bar?baz=qux&amp;quux=quuux#quuuux'] . '">',
          t('Path with query string and fragment. Clean URLs: !clean; protocol style: !ps.', $t10ns)
        );
        $this->assertEqual(
          _pathologic_filter('<a href="foo%20bar?baz=qux%26quux">', $filter, NULL, LANGUAGE_NONE, NULL, NULL),
          '<a href="' . $paths['foo%20bar?baz=qux%26quux'] . '">',
          t('Path with URL encoded parts')
        );
        $this->assertEqual(
          _pathologic_filter('<a href="/"></a>', $filter, NULL, LANGUAGE_NONE, NULL, NULL),
          '<a href="' . $paths['/'] . '"></a>',
          t('Path with just slash. Clean URLs: !clean; protocol style: !ps', $t10ns)
        );
      }
    }

    global $base_path;
    $this->assertEqual(
      _pathologic_filter('<a href="' . $base_path . 'foo">bar</a>', $filter, NULL, LANGUAGE_NONE, NULL, NULL),
      '<a href="' . _pathologic_content_url('foo', array('absolute' => FALSE)) .'">bar</a>',
      t('Paths beginning with $base_path (like WYSIWYG editors like to make)')
    );
    global $base_url;
    $this->assertEqual(
      _pathologic_filter('<a href="' . $base_url . '/foo">bar</a>', $filter, NULL, LANGUAGE_NONE, NULL, NULL),
      '<a href="' . _pathologic_content_url('foo', array('absolute' => FALSE)) .'">bar</a>',
      t('Paths beginning with $base_url')
    );

    // @see http://drupal.org/node/1617944
    $this->assertEqual(
      _pathologic_filter('<a href="//example.com/foo">bar</a>', $filter, NULL, LANGUAGE_NONE, NULL, NULL),
      '<a href="//example.com/foo">bar</a>',
      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('<a href="//example.org/foo">bar</a>', $filter, NULL, LANGUAGE_NONE, NULL, NULL),
      '<a href="' . _pathologic_content_url('foo', array('absolute' => TRUE)) . '">bar</a>',
      t('On-site schemeless URLs processed')
    );
    $this->assertEqual(
      _pathologic_filter('<a href="internal:foo">', $filter, NULL, LANGUAGE_NONE, NULL, NULL),
      '<a href="' . _pathologic_content_url('foo', array('absolute' => TRUE)) . '">',
      t('Path Filter compatibility (internal:)')
    );
    $this->assertEqual(
      _pathologic_filter('<a href="files:image.jpeg">', $filter, NULL, LANGUAGE_NONE, NULL, NULL),
      '<a href="' . _pathologic_content_url(file_create_url('public://image.jpeg'), array('absolute' => TRUE, 'is_file' => TRUE)) . '">',
      t('Path Filter compatibility (files:)')
    );
    $this->assertEqual(
      _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),
      '<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>',
      t('"All base paths for this site" functionality')
    );
    $this->assertEqual(
      _pathologic_filter('<a href="webcal:foo">bar</a>', $filter, NULL, LANGUAGE_NONE, NULL, NULL),
      '<a href="webcal:foo">bar</a>',
      t('URLs with likely protocols are ignored')
    );
    // Test hook_pathologic_alter() implementation.
    $this->assertEqual(
      _pathologic_filter('<a href="foo?test=add_foo_qpart">', $filter, NULL, LANGUAGE_NONE, NULL, NULL),
      '<a href="' . _pathologic_content_url('foo', array('absolute' => TRUE, 'query' => array('test' => 'add_foo_qpart', 'foo' => 'bar'))) . '">',
      t('hook_pathologic_alter(): Alter $url_params')
    );
    $this->assertEqual(
      _pathologic_filter('<a href="bar?test=use_original">', $filter, NULL, LANGUAGE_NONE, NULL, NULL),
      '<a href="bar?test=use_original">',
      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('<img src="misc/druplicon.png" />', $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;
    }
  }
}