1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Tests for filter.module.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Tests for text format and filter CRUD operations.
|
10
|
*/
|
11
|
class FilterCRUDTestCase extends DrupalWebTestCase {
|
12
|
public static function getInfo() {
|
13
|
return array(
|
14
|
'name' => 'Filter CRUD operations',
|
15
|
'description' => 'Test creation, loading, updating, deleting of text formats and filters.',
|
16
|
'group' => 'Filter',
|
17
|
);
|
18
|
}
|
19
|
|
20
|
function setUp() {
|
21
|
parent::setUp('filter_test');
|
22
|
}
|
23
|
|
24
|
/**
|
25
|
* Tests CRUD operations for text formats and filters.
|
26
|
*/
|
27
|
function testTextFormatCRUD() {
|
28
|
// Add a text format with minimum data only.
|
29
|
$format = new stdClass();
|
30
|
$format->format = 'empty_format';
|
31
|
$format->name = 'Empty format';
|
32
|
filter_format_save($format);
|
33
|
$this->verifyTextFormat($format);
|
34
|
$this->verifyFilters($format);
|
35
|
|
36
|
// Add another text format specifying all possible properties.
|
37
|
$format = new stdClass();
|
38
|
$format->format = 'custom_format';
|
39
|
$format->name = 'Custom format';
|
40
|
$format->filters = array(
|
41
|
'filter_url' => array(
|
42
|
'status' => 1,
|
43
|
'settings' => array(
|
44
|
'filter_url_length' => 30,
|
45
|
),
|
46
|
),
|
47
|
);
|
48
|
filter_format_save($format);
|
49
|
$this->verifyTextFormat($format);
|
50
|
$this->verifyFilters($format);
|
51
|
|
52
|
// Alter some text format properties and save again.
|
53
|
$format->name = 'Altered format';
|
54
|
$format->filters['filter_url']['status'] = 0;
|
55
|
$format->filters['filter_autop']['status'] = 1;
|
56
|
filter_format_save($format);
|
57
|
$this->verifyTextFormat($format);
|
58
|
$this->verifyFilters($format);
|
59
|
|
60
|
// Add a uncacheable filter and save again.
|
61
|
$format->filters['filter_test_uncacheable']['status'] = 1;
|
62
|
filter_format_save($format);
|
63
|
$this->verifyTextFormat($format);
|
64
|
$this->verifyFilters($format);
|
65
|
|
66
|
// Disable the text format.
|
67
|
filter_format_disable($format);
|
68
|
|
69
|
$db_format = db_query("SELECT * FROM {filter_format} WHERE format = :format", array(':format' => $format->format))->fetchObject();
|
70
|
$this->assertFalse($db_format->status, 'Database: Disabled text format is marked as disabled.');
|
71
|
$formats = filter_formats();
|
72
|
$this->assertTrue(!isset($formats[$format->format]), 'filter_formats: Disabled text format no longer exists.');
|
73
|
|
74
|
// Add a new format to check for Xss in format name.
|
75
|
$format = new stdClass();
|
76
|
$format->format = 'xss_format';
|
77
|
$format->name = '<script>alert(123)</script>';
|
78
|
filter_format_save($format);
|
79
|
user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(filter_permission_name($format) => 1));
|
80
|
$this->drupalGet('filter/tips');
|
81
|
$this->assertNoRaw($format->name, 'Text format name contains no xss.');
|
82
|
}
|
83
|
|
84
|
/**
|
85
|
* Verifies that a text format is properly stored.
|
86
|
*/
|
87
|
function verifyTextFormat($format) {
|
88
|
$t_args = array('%format' => $format->name);
|
89
|
// Verify text format database record.
|
90
|
$db_format = db_select('filter_format', 'ff')
|
91
|
->fields('ff')
|
92
|
->condition('format', $format->format)
|
93
|
->execute()
|
94
|
->fetchObject();
|
95
|
$this->assertEqual($db_format->format, $format->format, format_string('Database: Proper format id for text format %format.', $t_args));
|
96
|
$this->assertEqual($db_format->name, $format->name, format_string('Database: Proper title for text format %format.', $t_args));
|
97
|
$this->assertEqual($db_format->cache, $format->cache, format_string('Database: Proper cache indicator for text format %format.', $t_args));
|
98
|
$this->assertEqual($db_format->weight, $format->weight, format_string('Database: Proper weight for text format %format.', $t_args));
|
99
|
|
100
|
// Verify filter_format_load().
|
101
|
$filter_format = filter_format_load($format->format);
|
102
|
$this->assertEqual($filter_format->format, $format->format, format_string('filter_format_load: Proper format id for text format %format.', $t_args));
|
103
|
$this->assertEqual($filter_format->name, $format->name, format_string('filter_format_load: Proper title for text format %format.', $t_args));
|
104
|
$this->assertEqual($filter_format->cache, $format->cache, format_string('filter_format_load: Proper cache indicator for text format %format.', $t_args));
|
105
|
$this->assertEqual($filter_format->weight, $format->weight, format_string('filter_format_load: Proper weight for text format %format.', $t_args));
|
106
|
|
107
|
// Verify the 'cache' text format property according to enabled filters.
|
108
|
$filter_info = filter_get_filters();
|
109
|
$filters = filter_list_format($filter_format->format);
|
110
|
$cacheable = TRUE;
|
111
|
foreach ($filters as $name => $filter) {
|
112
|
// If this filter is not cacheable, update $cacheable accordingly, so we
|
113
|
// can verify $format->cache after iterating over all filters.
|
114
|
if ($filter->status && isset($filter_info[$name]['cache']) && !$filter_info[$name]['cache']) {
|
115
|
$cacheable = FALSE;
|
116
|
break;
|
117
|
}
|
118
|
}
|
119
|
$this->assertEqual($filter_format->cache, $cacheable, 'Text format contains proper cache property.');
|
120
|
}
|
121
|
|
122
|
/**
|
123
|
* Verifies that filters are properly stored for a text format.
|
124
|
*/
|
125
|
function verifyFilters($format) {
|
126
|
// Verify filter database records.
|
127
|
$filters = db_query("SELECT * FROM {filter} WHERE format = :format", array(':format' => $format->format))->fetchAllAssoc('name');
|
128
|
$format_filters = $format->filters;
|
129
|
foreach ($filters as $name => $filter) {
|
130
|
$t_args = array('%format' => $format->name, '%filter' => $name);
|
131
|
|
132
|
// Verify that filter status is properly stored.
|
133
|
$this->assertEqual($filter->status, $format_filters[$name]['status'], format_string('Database: Proper status for %filter in text format %format.', $t_args));
|
134
|
|
135
|
// Verify that filter settings were properly stored.
|
136
|
$this->assertEqual(unserialize($filter->settings), isset($format_filters[$name]['settings']) ? $format_filters[$name]['settings'] : array(), format_string('Database: Proper filter settings for %filter in text format %format.', $t_args));
|
137
|
|
138
|
// Verify that each filter has a module name assigned.
|
139
|
$this->assertTrue(!empty($filter->module), format_string('Database: Proper module name for %filter in text format %format.', $t_args));
|
140
|
|
141
|
// Remove the filter from the copy of saved $format to check whether all
|
142
|
// filters have been processed later.
|
143
|
unset($format_filters[$name]);
|
144
|
}
|
145
|
// Verify that all filters have been processed.
|
146
|
$this->assertTrue(empty($format_filters), 'Database contains values for all filters in the saved format.');
|
147
|
|
148
|
// Verify filter_list_format().
|
149
|
$filters = filter_list_format($format->format);
|
150
|
$format_filters = $format->filters;
|
151
|
foreach ($filters as $name => $filter) {
|
152
|
$t_args = array('%format' => $format->name, '%filter' => $name);
|
153
|
|
154
|
// Verify that filter status is properly stored.
|
155
|
$this->assertEqual($filter->status, $format_filters[$name]['status'], format_string('filter_list_format: Proper status for %filter in text format %format.', $t_args));
|
156
|
|
157
|
// Verify that filter settings were properly stored.
|
158
|
$this->assertEqual($filter->settings, isset($format_filters[$name]['settings']) ? $format_filters[$name]['settings'] : array(), format_string('filter_list_format: Proper filter settings for %filter in text format %format.', $t_args));
|
159
|
|
160
|
// Verify that each filter has a module name assigned.
|
161
|
$this->assertTrue(!empty($filter->module), format_string('filter_list_format: Proper module name for %filter in text format %format.', $t_args));
|
162
|
|
163
|
// Remove the filter from the copy of saved $format to check whether all
|
164
|
// filters have been processed later.
|
165
|
unset($format_filters[$name]);
|
166
|
}
|
167
|
// Verify that all filters have been processed.
|
168
|
$this->assertTrue(empty($format_filters), 'filter_list_format: Loaded filters contain values for all filters in the saved format.');
|
169
|
}
|
170
|
}
|
171
|
|
172
|
/**
|
173
|
* Tests the administrative functionality of the Filter module.
|
174
|
*/
|
175
|
class FilterAdminTestCase extends DrupalWebTestCase {
|
176
|
public static function getInfo() {
|
177
|
return array(
|
178
|
'name' => 'Filter administration functionality',
|
179
|
'description' => 'Thoroughly test the administrative interface of the filter module.',
|
180
|
'group' => 'Filter',
|
181
|
);
|
182
|
}
|
183
|
|
184
|
function setUp() {
|
185
|
parent::setUp();
|
186
|
|
187
|
// Create users.
|
188
|
$filtered_html_format = filter_format_load('filtered_html');
|
189
|
$full_html_format = filter_format_load('full_html');
|
190
|
$this->admin_user = $this->drupalCreateUser(array(
|
191
|
'administer filters',
|
192
|
filter_permission_name($filtered_html_format),
|
193
|
filter_permission_name($full_html_format),
|
194
|
));
|
195
|
|
196
|
$this->web_user = $this->drupalCreateUser(array('create page content', 'edit own page content'));
|
197
|
$this->drupalLogin($this->admin_user);
|
198
|
}
|
199
|
|
200
|
/**
|
201
|
* Tests the format administration functionality.
|
202
|
*/
|
203
|
function testFormatAdmin() {
|
204
|
// Add text format.
|
205
|
$this->drupalGet('admin/config/content/formats');
|
206
|
$this->clickLink('Add text format');
|
207
|
$format_id = drupal_strtolower($this->randomName());
|
208
|
$name = $this->randomName();
|
209
|
$edit = array(
|
210
|
'format' => $format_id,
|
211
|
'name' => $name,
|
212
|
);
|
213
|
$this->drupalPost(NULL, $edit, t('Save configuration'));
|
214
|
|
215
|
// Verify default weight of the text format.
|
216
|
$this->drupalGet('admin/config/content/formats');
|
217
|
$this->assertFieldByName("formats[$format_id][weight]", 0, 'Text format weight was saved.');
|
218
|
|
219
|
// Change the weight of the text format.
|
220
|
$edit = array(
|
221
|
"formats[$format_id][weight]" => 5,
|
222
|
);
|
223
|
$this->drupalPost('admin/config/content/formats', $edit, t('Save changes'));
|
224
|
$this->assertFieldByName("formats[$format_id][weight]", 5, 'Text format weight was saved.');
|
225
|
|
226
|
// Edit text format.
|
227
|
$this->drupalGet('admin/config/content/formats');
|
228
|
$this->assertLinkByHref('admin/config/content/formats/' . $format_id);
|
229
|
$this->drupalGet('admin/config/content/formats/' . $format_id);
|
230
|
$this->drupalPost(NULL, array(), t('Save configuration'));
|
231
|
|
232
|
// Verify that the custom weight of the text format has been retained.
|
233
|
$this->drupalGet('admin/config/content/formats');
|
234
|
$this->assertFieldByName("formats[$format_id][weight]", 5, 'Text format weight was retained.');
|
235
|
|
236
|
// Disable text format.
|
237
|
$this->assertLinkByHref('admin/config/content/formats/' . $format_id . '/disable');
|
238
|
$this->drupalGet('admin/config/content/formats/' . $format_id . '/disable');
|
239
|
$this->drupalPost(NULL, array(), t('Disable'));
|
240
|
|
241
|
// Verify that disabled text format no longer exists.
|
242
|
$this->drupalGet('admin/config/content/formats/' . $format_id);
|
243
|
$this->assertResponse(404, 'Disabled text format no longer exists.');
|
244
|
|
245
|
// Attempt to create a format of the same machine name as the disabled
|
246
|
// format but with a different human readable name.
|
247
|
$edit = array(
|
248
|
'format' => $format_id,
|
249
|
'name' => 'New format',
|
250
|
);
|
251
|
$this->drupalPost('admin/config/content/formats/add', $edit, t('Save configuration'));
|
252
|
$this->assertText('The machine-readable name is already in use. It must be unique.');
|
253
|
|
254
|
// Attempt to create a format of the same human readable name as the
|
255
|
// disabled format but with a different machine name.
|
256
|
$edit = array(
|
257
|
'format' => 'new_format',
|
258
|
'name' => $name,
|
259
|
);
|
260
|
$this->drupalPost('admin/config/content/formats/add', $edit, t('Save configuration'));
|
261
|
$this->assertRaw(t('Text format names must be unique. A format named %name already exists.', array(
|
262
|
'%name' => $name,
|
263
|
)));
|
264
|
}
|
265
|
|
266
|
/**
|
267
|
* Tests filter administration functionality.
|
268
|
*/
|
269
|
function testFilterAdmin() {
|
270
|
// URL filter.
|
271
|
$first_filter = 'filter_url';
|
272
|
// Line filter.
|
273
|
$second_filter = 'filter_autop';
|
274
|
|
275
|
$filtered = 'filtered_html';
|
276
|
$full = 'full_html';
|
277
|
$plain = 'plain_text';
|
278
|
|
279
|
// Check that the fallback format exists and cannot be disabled.
|
280
|
$this->assertTrue($plain == filter_fallback_format(), 'The fallback format is set to plain text.');
|
281
|
$this->drupalGet('admin/config/content/formats');
|
282
|
$this->assertNoRaw('admin/config/content/formats/' . $plain . '/disable', 'Disable link for the fallback format not found.');
|
283
|
$this->drupalGet('admin/config/content/formats/' . $plain . '/disable');
|
284
|
$this->assertResponse(403, 'The fallback format cannot be disabled.');
|
285
|
|
286
|
// Verify access permissions to Full HTML format.
|
287
|
$this->assertTrue(filter_access(filter_format_load($full), $this->admin_user), 'Admin user may use Full HTML.');
|
288
|
$this->assertFalse(filter_access(filter_format_load($full), $this->web_user), 'Web user may not use Full HTML.');
|
289
|
|
290
|
// Add an additional tag.
|
291
|
$edit = array();
|
292
|
$edit['filters[filter_html][settings][allowed_html]'] = '<a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <quote>';
|
293
|
$this->drupalPost('admin/config/content/formats/' . $filtered, $edit, t('Save configuration'));
|
294
|
$this->assertFieldByName('filters[filter_html][settings][allowed_html]', $edit['filters[filter_html][settings][allowed_html]'], 'Allowed HTML tag added.');
|
295
|
|
296
|
$result = db_query('SELECT * FROM {cache_filter}')->fetchObject();
|
297
|
$this->assertFalse($result, 'Cache cleared.');
|
298
|
|
299
|
$elements = $this->xpath('//select[@name=:first]/following::select[@name=:second]', array(
|
300
|
':first' => 'filters[' . $first_filter . '][weight]',
|
301
|
':second' => 'filters[' . $second_filter . '][weight]',
|
302
|
));
|
303
|
$this->assertTrue(!empty($elements), 'Order confirmed in admin interface.');
|
304
|
|
305
|
// Reorder filters.
|
306
|
$edit = array();
|
307
|
$edit['filters[' . $second_filter . '][weight]'] = 1;
|
308
|
$edit['filters[' . $first_filter . '][weight]'] = 2;
|
309
|
$this->drupalPost(NULL, $edit, t('Save configuration'));
|
310
|
$this->assertFieldByName('filters[' . $second_filter . '][weight]', 1, 'Order saved successfully.');
|
311
|
$this->assertFieldByName('filters[' . $first_filter . '][weight]', 2, 'Order saved successfully.');
|
312
|
|
313
|
$elements = $this->xpath('//select[@name=:first]/following::select[@name=:second]', array(
|
314
|
':first' => 'filters[' . $second_filter . '][weight]',
|
315
|
':second' => 'filters[' . $first_filter . '][weight]',
|
316
|
));
|
317
|
$this->assertTrue(!empty($elements), 'Reorder confirmed in admin interface.');
|
318
|
|
319
|
$result = db_query('SELECT * FROM {filter} WHERE format = :format ORDER BY weight ASC', array(':format' => $filtered));
|
320
|
$filters = array();
|
321
|
foreach ($result as $filter) {
|
322
|
if ($filter->name == $second_filter || $filter->name == $first_filter) {
|
323
|
$filters[] = $filter;
|
324
|
}
|
325
|
}
|
326
|
$this->assertTrue(($filters[0]->name == $second_filter && $filters[1]->name == $first_filter), 'Order confirmed in database.');
|
327
|
|
328
|
// Add format.
|
329
|
$edit = array();
|
330
|
$edit['format'] = drupal_strtolower($this->randomName());
|
331
|
$edit['name'] = $this->randomName();
|
332
|
$edit['roles[' . DRUPAL_AUTHENTICATED_RID . ']'] = 1;
|
333
|
$edit['filters[' . $second_filter . '][status]'] = TRUE;
|
334
|
$edit['filters[' . $first_filter . '][status]'] = TRUE;
|
335
|
$this->drupalPost('admin/config/content/formats/add', $edit, t('Save configuration'));
|
336
|
$this->assertRaw(t('Added text format %format.', array('%format' => $edit['name'])), 'New filter created.');
|
337
|
|
338
|
drupal_static_reset('filter_formats');
|
339
|
$format = filter_format_load($edit['format']);
|
340
|
$this->assertNotNull($format, 'Format found in database.');
|
341
|
|
342
|
$this->assertFieldByName('roles[' . DRUPAL_AUTHENTICATED_RID . ']', '', 'Role found.');
|
343
|
$this->assertFieldByName('filters[' . $second_filter . '][status]', '', 'Line break filter found.');
|
344
|
$this->assertFieldByName('filters[' . $first_filter . '][status]', '', 'Url filter found.');
|
345
|
|
346
|
// Disable new filter.
|
347
|
$this->drupalPost('admin/config/content/formats/' . $format->format . '/disable', array(), t('Disable'));
|
348
|
$this->assertRaw(t('Disabled text format %format.', array('%format' => $edit['name'])), 'Format successfully disabled.');
|
349
|
|
350
|
// Allow authenticated users on full HTML.
|
351
|
$format = filter_format_load($full);
|
352
|
$edit = array();
|
353
|
$edit['roles[' . DRUPAL_ANONYMOUS_RID . ']'] = 0;
|
354
|
$edit['roles[' . DRUPAL_AUTHENTICATED_RID . ']'] = 1;
|
355
|
$this->drupalPost('admin/config/content/formats/' . $full, $edit, t('Save configuration'));
|
356
|
$this->assertRaw(t('The text format %format has been updated.', array('%format' => $format->name)), 'Full HTML format successfully updated.');
|
357
|
|
358
|
// Switch user.
|
359
|
$this->drupalLogout();
|
360
|
$this->drupalLogin($this->web_user);
|
361
|
|
362
|
$this->drupalGet('node/add/page');
|
363
|
$this->assertRaw('<option value="' . $full . '">Full HTML</option>', 'Full HTML filter accessible.');
|
364
|
|
365
|
// Use filtered HTML and see if it removes tags that are not allowed.
|
366
|
$body = '<em>' . $this->randomName() . '</em>';
|
367
|
$extra_text = 'text';
|
368
|
$text = $body . '<random>' . $extra_text . '</random>';
|
369
|
|
370
|
$edit = array();
|
371
|
$langcode = LANGUAGE_NONE;
|
372
|
$edit["title"] = $this->randomName();
|
373
|
$edit["body[$langcode][0][value]"] = $text;
|
374
|
$edit["body[$langcode][0][format]"] = $filtered;
|
375
|
$this->drupalPost('node/add/page', $edit, t('Save'));
|
376
|
$this->assertRaw(t('Basic page %title has been created.', array('%title' => $edit["title"])), 'Filtered node created.');
|
377
|
|
378
|
$node = $this->drupalGetNodeByTitle($edit["title"]);
|
379
|
$this->assertTrue($node, 'Node found in database.');
|
380
|
|
381
|
$this->drupalGet('node/' . $node->nid);
|
382
|
$this->assertRaw($body . $extra_text, 'Filter removed invalid tag.');
|
383
|
|
384
|
// Use plain text and see if it escapes all tags, whether allowed or not.
|
385
|
$edit = array();
|
386
|
$edit["body[$langcode][0][format]"] = $plain;
|
387
|
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
|
388
|
$this->drupalGet('node/' . $node->nid);
|
389
|
$this->assertText(check_plain($text), 'The "Plain text" text format escapes all HTML tags.');
|
390
|
|
391
|
// Switch user.
|
392
|
$this->drupalLogout();
|
393
|
$this->drupalLogin($this->admin_user);
|
394
|
|
395
|
// Clean up.
|
396
|
// Allowed tags.
|
397
|
$edit = array();
|
398
|
$edit['filters[filter_html][settings][allowed_html]'] = '<a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>';
|
399
|
$this->drupalPost('admin/config/content/formats/' . $filtered, $edit, t('Save configuration'));
|
400
|
$this->assertFieldByName('filters[filter_html][settings][allowed_html]', $edit['filters[filter_html][settings][allowed_html]'], 'Changes reverted.');
|
401
|
|
402
|
// Full HTML.
|
403
|
$edit = array();
|
404
|
$edit['roles[' . DRUPAL_AUTHENTICATED_RID . ']'] = FALSE;
|
405
|
$this->drupalPost('admin/config/content/formats/' . $full, $edit, t('Save configuration'));
|
406
|
$this->assertRaw(t('The text format %format has been updated.', array('%format' => $format->name)), 'Full HTML format successfully reverted.');
|
407
|
$this->assertFieldByName('roles[' . DRUPAL_AUTHENTICATED_RID . ']', $edit['roles[' . DRUPAL_AUTHENTICATED_RID . ']'], 'Changes reverted.');
|
408
|
|
409
|
// Filter order.
|
410
|
$edit = array();
|
411
|
$edit['filters[' . $second_filter . '][weight]'] = 2;
|
412
|
$edit['filters[' . $first_filter . '][weight]'] = 1;
|
413
|
$this->drupalPost('admin/config/content/formats/' . $filtered, $edit, t('Save configuration'));
|
414
|
$this->assertFieldByName('filters[' . $second_filter . '][weight]', $edit['filters[' . $second_filter . '][weight]'], 'Changes reverted.');
|
415
|
$this->assertFieldByName('filters[' . $first_filter . '][weight]', $edit['filters[' . $first_filter . '][weight]'], 'Changes reverted.');
|
416
|
}
|
417
|
|
418
|
/**
|
419
|
* Tests the URL filter settings form is properly validated.
|
420
|
*/
|
421
|
function testUrlFilterAdmin() {
|
422
|
// The form does not save with an invalid filter URL length.
|
423
|
$edit = array(
|
424
|
'filters[filter_url][settings][filter_url_length]' => $this->randomName(4),
|
425
|
);
|
426
|
$this->drupalPost('admin/config/content/formats/filtered_html', $edit, t('Save configuration'));
|
427
|
$this->assertNoRaw(t('The text format %format has been updated.', array('%format' => 'Filtered HTML')));
|
428
|
}
|
429
|
}
|
430
|
|
431
|
/**
|
432
|
* Tests the filter format access functionality in the Filter module.
|
433
|
*/
|
434
|
class FilterFormatAccessTestCase extends DrupalWebTestCase {
|
435
|
/**
|
436
|
* A user with administrative permissions.
|
437
|
*
|
438
|
* @var object
|
439
|
*/
|
440
|
protected $admin_user;
|
441
|
|
442
|
/**
|
443
|
* A user with 'administer filters' permission.
|
444
|
*
|
445
|
* @var object
|
446
|
*/
|
447
|
protected $filter_admin_user;
|
448
|
|
449
|
/**
|
450
|
* A user with permission to create and edit own content.
|
451
|
*
|
452
|
* @var object
|
453
|
*/
|
454
|
protected $web_user;
|
455
|
|
456
|
/**
|
457
|
* An object representing an allowed text format.
|
458
|
*
|
459
|
* @var object
|
460
|
*/
|
461
|
protected $allowed_format;
|
462
|
|
463
|
/**
|
464
|
* An object representing a disallowed text format.
|
465
|
*
|
466
|
* @var object
|
467
|
*/
|
468
|
protected $disallowed_format;
|
469
|
|
470
|
public static function getInfo() {
|
471
|
return array(
|
472
|
'name' => 'Filter format access',
|
473
|
'description' => 'Tests access to text formats.',
|
474
|
'group' => 'Filter',
|
475
|
);
|
476
|
}
|
477
|
|
478
|
function setUp() {
|
479
|
parent::setUp();
|
480
|
|
481
|
// Create a user who can administer text formats, but does not have
|
482
|
// specific permission to use any of them.
|
483
|
$this->filter_admin_user = $this->drupalCreateUser(array(
|
484
|
'administer filters',
|
485
|
'create page content',
|
486
|
'edit any page content',
|
487
|
));
|
488
|
|
489
|
// Create two text formats.
|
490
|
$this->drupalLogin($this->filter_admin_user);
|
491
|
$formats = array();
|
492
|
for ($i = 0; $i < 2; $i++) {
|
493
|
$edit = array(
|
494
|
'format' => drupal_strtolower($this->randomName()),
|
495
|
'name' => $this->randomName(),
|
496
|
);
|
497
|
$this->drupalPost('admin/config/content/formats/add', $edit, t('Save configuration'));
|
498
|
$this->resetFilterCaches();
|
499
|
$formats[] = filter_format_load($edit['format']);
|
500
|
}
|
501
|
list($this->allowed_format, $this->disallowed_format) = $formats;
|
502
|
$this->drupalLogout();
|
503
|
|
504
|
// Create a regular user with access to one of the formats.
|
505
|
$this->web_user = $this->drupalCreateUser(array(
|
506
|
'create page content',
|
507
|
'edit any page content',
|
508
|
filter_permission_name($this->allowed_format),
|
509
|
));
|
510
|
|
511
|
// Create an administrative user who has access to use both formats.
|
512
|
$this->admin_user = $this->drupalCreateUser(array(
|
513
|
'administer filters',
|
514
|
'create page content',
|
515
|
'edit any page content',
|
516
|
filter_permission_name($this->allowed_format),
|
517
|
filter_permission_name($this->disallowed_format),
|
518
|
));
|
519
|
}
|
520
|
|
521
|
/**
|
522
|
* Tests the Filter format access permissions functionality.
|
523
|
*/
|
524
|
function testFormatPermissions() {
|
525
|
// Make sure that a regular user only has access to the text format they
|
526
|
// were granted access to, as well to the fallback format.
|
527
|
$this->assertTrue(filter_access($this->allowed_format, $this->web_user), 'A regular user has access to a text format they were granted access to.');
|
528
|
$this->assertFalse(filter_access($this->disallowed_format, $this->web_user), 'A regular user does not have access to a text format they were not granted access to.');
|
529
|
$this->assertTrue(filter_access(filter_format_load(filter_fallback_format()), $this->web_user), 'A regular user has access to the fallback format.');
|
530
|
|
531
|
// Perform similar checks as above, but now against the entire list of
|
532
|
// available formats for this user.
|
533
|
$this->assertTrue(in_array($this->allowed_format->format, array_keys(filter_formats($this->web_user))), 'The allowed format appears in the list of available formats for a regular user.');
|
534
|
$this->assertFalse(in_array($this->disallowed_format->format, array_keys(filter_formats($this->web_user))), 'The disallowed format does not appear in the list of available formats for a regular user.');
|
535
|
$this->assertTrue(in_array(filter_fallback_format(), array_keys(filter_formats($this->web_user))), 'The fallback format appears in the list of available formats for a regular user.');
|
536
|
|
537
|
// Make sure that a regular user only has permission to use the format
|
538
|
// they were granted access to.
|
539
|
$this->assertTrue(user_access(filter_permission_name($this->allowed_format), $this->web_user), 'A regular user has permission to use the allowed text format.');
|
540
|
$this->assertFalse(user_access(filter_permission_name($this->disallowed_format), $this->web_user), 'A regular user does not have permission to use the disallowed text format.');
|
541
|
|
542
|
// Make sure that the allowed format appears on the node form and that
|
543
|
// the disallowed format does not.
|
544
|
$this->drupalLogin($this->web_user);
|
545
|
$this->drupalGet('node/add/page');
|
546
|
$langcode = LANGUAGE_NONE;
|
547
|
$elements = $this->xpath('//select[@name=:name]/option', array(
|
548
|
':name' => "body[$langcode][0][format]",
|
549
|
':option' => $this->allowed_format->format,
|
550
|
));
|
551
|
$options = array();
|
552
|
foreach ($elements as $element) {
|
553
|
$options[(string) $element['value']] = $element;
|
554
|
}
|
555
|
$this->assertTrue(isset($options[$this->allowed_format->format]), 'The allowed text format appears as an option when adding a new node.');
|
556
|
$this->assertFalse(isset($options[$this->disallowed_format->format]), 'The disallowed text format does not appear as an option when adding a new node.');
|
557
|
$this->assertTrue(isset($options[filter_fallback_format()]), 'The fallback format appears as an option when adding a new node.');
|
558
|
}
|
559
|
|
560
|
/**
|
561
|
* Tests if text format is available to a role.
|
562
|
*/
|
563
|
function testFormatRoles() {
|
564
|
// Get the role ID assigned to the regular user; it must be the maximum.
|
565
|
$rid = max(array_keys($this->web_user->roles));
|
566
|
|
567
|
// Check that this role appears in the list of roles that have access to an
|
568
|
// allowed text format, but does not appear in the list of roles that have
|
569
|
// access to a disallowed text format.
|
570
|
$this->assertTrue(in_array($rid, array_keys(filter_get_roles_by_format($this->allowed_format))), 'A role which has access to a text format appears in the list of roles that have access to that format.');
|
571
|
$this->assertFalse(in_array($rid, array_keys(filter_get_roles_by_format($this->disallowed_format))), 'A role which does not have access to a text format does not appear in the list of roles that have access to that format.');
|
572
|
|
573
|
// Check that the correct text format appears in the list of formats
|
574
|
// available to that role.
|
575
|
$this->assertTrue(in_array($this->allowed_format->format, array_keys(filter_get_formats_by_role($rid))), 'A text format which a role has access to appears in the list of formats available to that role.');
|
576
|
$this->assertFalse(in_array($this->disallowed_format->format, array_keys(filter_get_formats_by_role($rid))), 'A text format which a role does not have access to does not appear in the list of formats available to that role.');
|
577
|
|
578
|
// Check that the fallback format is always allowed.
|
579
|
$this->assertEqual(filter_get_roles_by_format(filter_format_load(filter_fallback_format())), user_roles(), 'All roles have access to the fallback format.');
|
580
|
$this->assertTrue(in_array(filter_fallback_format(), array_keys(filter_get_formats_by_role($rid))), 'The fallback format appears in the list of allowed formats for any role.');
|
581
|
}
|
582
|
|
583
|
/**
|
584
|
* Tests editing a page using a disallowed text format.
|
585
|
*
|
586
|
* Verifies that regular users and administrators are able to edit a page, but
|
587
|
* not allowed to change the fields which use an inaccessible text format.
|
588
|
* Also verifies that fields which use a text format that does not exist can
|
589
|
* be edited by administrators only, but that the administrator is forced to
|
590
|
* choose a new format before saving the page.
|
591
|
*/
|
592
|
function testFormatWidgetPermissions() {
|
593
|
$langcode = LANGUAGE_NONE;
|
594
|
$title_key = "title";
|
595
|
$body_value_key = "body[$langcode][0][value]";
|
596
|
$body_format_key = "body[$langcode][0][format]";
|
597
|
|
598
|
// Create node to edit.
|
599
|
$this->drupalLogin($this->admin_user);
|
600
|
$edit = array();
|
601
|
$edit['title'] = $this->randomName(8);
|
602
|
$edit[$body_value_key] = $this->randomName(16);
|
603
|
$edit[$body_format_key] = $this->disallowed_format->format;
|
604
|
$this->drupalPost('node/add/page', $edit, t('Save'));
|
605
|
$node = $this->drupalGetNodeByTitle($edit['title']);
|
606
|
|
607
|
// Try to edit with a less privileged user.
|
608
|
$this->drupalLogin($this->web_user);
|
609
|
$this->drupalGet('node/' . $node->nid);
|
610
|
$this->clickLink(t('Edit'));
|
611
|
|
612
|
// Verify that body field is read-only and contains replacement value.
|
613
|
$this->assertFieldByXPath("//textarea[@name='$body_value_key' and @disabled='disabled']", t('This field has been disabled because you do not have sufficient permissions to edit it.'), 'Text format access denied message found.');
|
614
|
|
615
|
// Verify that title can be changed, but preview displays original body.
|
616
|
$new_edit = array();
|
617
|
$new_edit['title'] = $this->randomName(8);
|
618
|
$this->drupalPost(NULL, $new_edit, t('Preview'));
|
619
|
$this->assertText($edit[$body_value_key], 'Old body found in preview.');
|
620
|
|
621
|
// Save and verify that only the title was changed.
|
622
|
$this->drupalPost(NULL, $new_edit, t('Save'));
|
623
|
$this->assertNoText($edit['title'], 'Old title not found.');
|
624
|
$this->assertText($new_edit['title'], 'New title found.');
|
625
|
$this->assertText($edit[$body_value_key], 'Old body found.');
|
626
|
|
627
|
// Check that even an administrator with "administer filters" permission
|
628
|
// cannot edit the body field if they do not have specific permission to
|
629
|
// use its stored format. (This must be disallowed so that the
|
630
|
// administrator is never forced to switch the text format to something
|
631
|
// else.)
|
632
|
$this->drupalLogin($this->filter_admin_user);
|
633
|
$this->drupalGet('node/' . $node->nid . '/edit');
|
634
|
$this->assertFieldByXPath("//textarea[@name='$body_value_key' and @disabled='disabled']", t('This field has been disabled because you do not have sufficient permissions to edit it.'), 'Text format access denied message found.');
|
635
|
|
636
|
// Disable the text format used above.
|
637
|
filter_format_disable($this->disallowed_format);
|
638
|
$this->resetFilterCaches();
|
639
|
|
640
|
// Log back in as the less privileged user and verify that the body field
|
641
|
// is still disabled, since the less privileged user should not be able to
|
642
|
// edit content that does not have an assigned format.
|
643
|
$this->drupalLogin($this->web_user);
|
644
|
$this->drupalGet('node/' . $node->nid . '/edit');
|
645
|
$this->assertFieldByXPath("//textarea[@name='$body_value_key' and @disabled='disabled']", t('This field has been disabled because you do not have sufficient permissions to edit it.'), 'Text format access denied message found.');
|
646
|
|
647
|
// Log back in as the filter administrator and verify that the body field
|
648
|
// can be edited.
|
649
|
$this->drupalLogin($this->filter_admin_user);
|
650
|
$this->drupalGet('node/' . $node->nid . '/edit');
|
651
|
$this->assertNoFieldByXPath("//textarea[@name='$body_value_key' and @disabled='disabled']", NULL, 'Text format access denied message not found.');
|
652
|
$this->assertFieldByXPath("//select[@name='$body_format_key']", NULL, 'Text format selector found.');
|
653
|
|
654
|
// Verify that trying to save the node without selecting a new text format
|
655
|
// produces an error message, and does not result in the node being saved.
|
656
|
$old_title = $new_edit['title'];
|
657
|
$new_title = $this->randomName(8);
|
658
|
$edit = array('title' => $new_title);
|
659
|
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
|
660
|
$this->assertText(t('!name field is required.', array('!name' => t('Text format'))), 'Error message is displayed.');
|
661
|
$this->drupalGet('node/' . $node->nid);
|
662
|
$this->assertText($old_title, 'Old title found.');
|
663
|
$this->assertNoText($new_title, 'New title not found.');
|
664
|
|
665
|
// Now select a new text format and make sure the node can be saved.
|
666
|
$edit[$body_format_key] = filter_fallback_format();
|
667
|
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
|
668
|
$this->assertUrl('node/' . $node->nid);
|
669
|
$this->assertText($new_title, 'New title found.');
|
670
|
$this->assertNoText($old_title, 'Old title not found.');
|
671
|
|
672
|
// Switch the text format to a new one, then disable that format and all
|
673
|
// other formats on the site (leaving only the fallback format).
|
674
|
$this->drupalLogin($this->admin_user);
|
675
|
$edit = array($body_format_key => $this->allowed_format->format);
|
676
|
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
|
677
|
$this->assertUrl('node/' . $node->nid);
|
678
|
foreach (filter_formats() as $format) {
|
679
|
if ($format->format != filter_fallback_format()) {
|
680
|
filter_format_disable($format);
|
681
|
}
|
682
|
}
|
683
|
|
684
|
// Since there is now only one available text format, the widget for
|
685
|
// selecting a text format would normally not display when the content is
|
686
|
// edited. However, we need to verify that the filter administrator still
|
687
|
// is forced to make a conscious choice to reassign the text to a different
|
688
|
// format.
|
689
|
$this->drupalLogin($this->filter_admin_user);
|
690
|
$old_title = $new_title;
|
691
|
$new_title = $this->randomName(8);
|
692
|
$edit = array('title' => $new_title);
|
693
|
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
|
694
|
$this->assertText(t('!name field is required.', array('!name' => t('Text format'))), 'Error message is displayed.');
|
695
|
$this->drupalGet('node/' . $node->nid);
|
696
|
$this->assertText($old_title, 'Old title found.');
|
697
|
$this->assertNoText($new_title, 'New title not found.');
|
698
|
$edit[$body_format_key] = filter_fallback_format();
|
699
|
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
|
700
|
$this->assertUrl('node/' . $node->nid);
|
701
|
$this->assertText($new_title, 'New title found.');
|
702
|
$this->assertNoText($old_title, 'Old title not found.');
|
703
|
}
|
704
|
|
705
|
/**
|
706
|
* Rebuilds text format and permission caches in the thread running the tests.
|
707
|
*/
|
708
|
protected function resetFilterCaches() {
|
709
|
filter_formats_reset();
|
710
|
$this->checkPermissions(array(), TRUE);
|
711
|
}
|
712
|
}
|
713
|
|
714
|
/**
|
715
|
* Tests the default filter functionality in the Filter module.
|
716
|
*/
|
717
|
class FilterDefaultFormatTestCase extends DrupalWebTestCase {
|
718
|
public static function getInfo() {
|
719
|
return array(
|
720
|
'name' => 'Default text format functionality',
|
721
|
'description' => 'Test the default text formats for different users.',
|
722
|
'group' => 'Filter',
|
723
|
);
|
724
|
}
|
725
|
|
726
|
/**
|
727
|
* Tests if the default text format is accessible to users.
|
728
|
*/
|
729
|
function testDefaultTextFormats() {
|
730
|
// Create two text formats, and two users. The first user has access to
|
731
|
// both formats, but the second user only has access to the second one.
|
732
|
$admin_user = $this->drupalCreateUser(array('administer filters'));
|
733
|
$this->drupalLogin($admin_user);
|
734
|
$formats = array();
|
735
|
for ($i = 0; $i < 2; $i++) {
|
736
|
$edit = array(
|
737
|
'format' => drupal_strtolower($this->randomName()),
|
738
|
'name' => $this->randomName(),
|
739
|
);
|
740
|
$this->drupalPost('admin/config/content/formats/add', $edit, t('Save configuration'));
|
741
|
$this->resetFilterCaches();
|
742
|
$formats[] = filter_format_load($edit['format']);
|
743
|
}
|
744
|
list($first_format, $second_format) = $formats;
|
745
|
$first_user = $this->drupalCreateUser(array(filter_permission_name($first_format), filter_permission_name($second_format)));
|
746
|
$second_user = $this->drupalCreateUser(array(filter_permission_name($second_format)));
|
747
|
|
748
|
// Adjust the weights so that the first and second formats (in that order)
|
749
|
// are the two lowest weighted formats available to any user.
|
750
|
$minimum_weight = db_query("SELECT MIN(weight) FROM {filter_format}")->fetchField();
|
751
|
$edit = array();
|
752
|
$edit['formats[' . $first_format->format . '][weight]'] = $minimum_weight - 2;
|
753
|
$edit['formats[' . $second_format->format . '][weight]'] = $minimum_weight - 1;
|
754
|
$this->drupalPost('admin/config/content/formats', $edit, t('Save changes'));
|
755
|
$this->resetFilterCaches();
|
756
|
|
757
|
// Check that each user's default format is the lowest weighted format that
|
758
|
// the user has access to.
|
759
|
$this->assertEqual(filter_default_format($first_user), $first_format->format, "The first user's default format is the lowest weighted format that the user has access to.");
|
760
|
$this->assertEqual(filter_default_format($second_user), $second_format->format, "The second user's default format is the lowest weighted format that the user has access to, and is different than the first user's.");
|
761
|
|
762
|
// Reorder the two formats, and check that both users now have the same
|
763
|
// default.
|
764
|
$edit = array();
|
765
|
$edit['formats[' . $second_format->format . '][weight]'] = $minimum_weight - 3;
|
766
|
$this->drupalPost('admin/config/content/formats', $edit, t('Save changes'));
|
767
|
$this->resetFilterCaches();
|
768
|
$this->assertEqual(filter_default_format($first_user), filter_default_format($second_user), 'After the formats are reordered, both users have the same default format.');
|
769
|
}
|
770
|
|
771
|
/**
|
772
|
* Rebuilds text format and permission caches in the thread running the tests.
|
773
|
*/
|
774
|
protected function resetFilterCaches() {
|
775
|
filter_formats_reset();
|
776
|
$this->checkPermissions(array(), TRUE);
|
777
|
}
|
778
|
}
|
779
|
|
780
|
/**
|
781
|
* Tests the behavior of check_markup() when it is called without text format.
|
782
|
*/
|
783
|
class FilterNoFormatTestCase extends DrupalWebTestCase {
|
784
|
public static function getInfo() {
|
785
|
return array(
|
786
|
'name' => 'Unassigned text format functionality',
|
787
|
'description' => 'Test the behavior of check_markup() when it is called without a text format.',
|
788
|
'group' => 'Filter',
|
789
|
);
|
790
|
}
|
791
|
|
792
|
/**
|
793
|
* Tests text without format.
|
794
|
*
|
795
|
* Tests if text with no format is filtered the same way as text in the
|
796
|
* fallback format.
|
797
|
*/
|
798
|
function testCheckMarkupNoFormat() {
|
799
|
// Create some text. Include some HTML and line breaks, so we get a good
|
800
|
// test of the filtering that is applied to it.
|
801
|
$text = "<strong>" . $this->randomName(32) . "</strong>\n\n<div>" . $this->randomName(32) . "</div>";
|
802
|
|
803
|
// Make sure that when this text is run through check_markup() with no text
|
804
|
// format, it is filtered as though it is in the fallback format.
|
805
|
$this->assertEqual(check_markup($text), check_markup($text, filter_fallback_format()), 'Text with no format is filtered the same as text in the fallback format.');
|
806
|
}
|
807
|
}
|
808
|
|
809
|
/**
|
810
|
* Security tests for missing/vanished text formats or filters.
|
811
|
*/
|
812
|
class FilterSecurityTestCase extends DrupalWebTestCase {
|
813
|
public static function getInfo() {
|
814
|
return array(
|
815
|
'name' => 'Security',
|
816
|
'description' => 'Test the behavior of check_markup() when a filter or text format vanishes.',
|
817
|
'group' => 'Filter',
|
818
|
);
|
819
|
}
|
820
|
|
821
|
function setUp() {
|
822
|
parent::setUp('php', 'filter_test');
|
823
|
$this->admin_user = $this->drupalCreateUser(array('administer modules', 'administer filters', 'administer site configuration'));
|
824
|
$this->drupalLogin($this->admin_user);
|
825
|
}
|
826
|
|
827
|
/**
|
828
|
* Tests removal of filtered content when an active filter is disabled.
|
829
|
*
|
830
|
* Tests that filtered content is emptied when an actively used filter module
|
831
|
* is disabled.
|
832
|
*/
|
833
|
function testDisableFilterModule() {
|
834
|
// Create a new node.
|
835
|
$node = $this->drupalCreateNode(array('promote' => 1));
|
836
|
$body_raw = $node->body[LANGUAGE_NONE][0]['value'];
|
837
|
$format_id = $node->body[LANGUAGE_NONE][0]['format'];
|
838
|
$this->drupalGet('node/' . $node->nid);
|
839
|
$this->assertText($body_raw, 'Node body found.');
|
840
|
|
841
|
// Enable the filter_test_replace filter.
|
842
|
$edit = array(
|
843
|
'filters[filter_test_replace][status]' => 1,
|
844
|
);
|
845
|
$this->drupalPost('admin/config/content/formats/' . $format_id, $edit, t('Save configuration'));
|
846
|
|
847
|
// Verify that filter_test_replace filter replaced the content.
|
848
|
$this->drupalGet('node/' . $node->nid);
|
849
|
$this->assertNoText($body_raw, 'Node body not found.');
|
850
|
$this->assertText('Filter: Testing filter', 'Testing filter output found.');
|
851
|
|
852
|
// Disable the text format entirely.
|
853
|
$this->drupalPost('admin/config/content/formats/' . $format_id . '/disable', array(), t('Disable'));
|
854
|
|
855
|
// Verify that the content is empty, because the text format does not exist.
|
856
|
$this->drupalGet('node/' . $node->nid);
|
857
|
$this->assertNoText($body_raw, 'Node body not found.');
|
858
|
}
|
859
|
}
|
860
|
|
861
|
/**
|
862
|
* Unit tests for core filters.
|
863
|
*/
|
864
|
class FilterUnitTestCase extends DrupalUnitTestCase {
|
865
|
public static function getInfo() {
|
866
|
return array(
|
867
|
'name' => 'Filter module filters',
|
868
|
'description' => 'Tests Filter module filters individually.',
|
869
|
'group' => 'Filter',
|
870
|
);
|
871
|
}
|
872
|
|
873
|
/**
|
874
|
* Tests the line break filter.
|
875
|
*/
|
876
|
function testLineBreakFilter() {
|
877
|
// Setup dummy filter object.
|
878
|
$filter = new stdClass();
|
879
|
$filter->callback = '_filter_autop';
|
880
|
|
881
|
// Since the line break filter naturally needs plenty of newlines in test
|
882
|
// strings and expectations, we're using "\n" instead of regular newlines
|
883
|
// here.
|
884
|
$tests = array(
|
885
|
// Single line breaks should be changed to <br /> tags, while paragraphs
|
886
|
// separated with double line breaks should be enclosed with <p></p> tags.
|
887
|
"aaa\nbbb\n\nccc" => array(
|
888
|
"<p>aaa<br />\nbbb</p>\n<p>ccc</p>" => TRUE,
|
889
|
),
|
890
|
// Skip contents of certain block tags entirely.
|
891
|
"<script>aaa\nbbb\n\nccc</script>
|
892
|
<style>aaa\nbbb\n\nccc</style>
|
893
|
<pre>aaa\nbbb\n\nccc</pre>
|
894
|
<object>aaa\nbbb\n\nccc</object>
|
895
|
<iframe>aaa\nbbb\n\nccc</iframe>
|
896
|
" => array(
|
897
|
"<script>aaa\nbbb\n\nccc</script>" => TRUE,
|
898
|
"<style>aaa\nbbb\n\nccc</style>" => TRUE,
|
899
|
"<pre>aaa\nbbb\n\nccc</pre>" => TRUE,
|
900
|
"<object>aaa\nbbb\n\nccc</object>" => TRUE,
|
901
|
"<iframe>aaa\nbbb\n\nccc</iframe>" => TRUE,
|
902
|
),
|
903
|
// Skip comments entirely.
|
904
|
"One. <!-- comment --> Two.\n<!--\nThree.\n-->\n" => array(
|
905
|
'<!-- comment -->' => TRUE,
|
906
|
"<!--\nThree.\n-->" => TRUE,
|
907
|
),
|
908
|
// Resulting HTML should produce matching paragraph tags.
|
909
|
'<p><div> </div></p>' => array(
|
910
|
"<p>\n<div> </div>\n</p>" => TRUE,
|
911
|
),
|
912
|
'<div><p> </p></div>' => array(
|
913
|
"<div>\n</div>" => TRUE,
|
914
|
),
|
915
|
'<blockquote><pre>aaa</pre></blockquote>' => array(
|
916
|
"<blockquote><pre>aaa</pre></blockquote>" => TRUE,
|
917
|
),
|
918
|
"<pre>aaa\nbbb\nccc</pre>\nddd\neee" => array(
|
919
|
"<pre>aaa\nbbb\nccc</pre>" => TRUE,
|
920
|
"<p>ddd<br />\neee</p>" => TRUE,
|
921
|
),
|
922
|
// Comments remain unchanged and subsequent lines/paragraphs are
|
923
|
// transformed normally.
|
924
|
"aaa<!--comment-->\n\nbbb\n\nccc\n\nddd<!--comment\nwith linebreak-->\n\neee\n\nfff" => array(
|
925
|
"<p>aaa</p>\n<!--comment--><p>\nbbb</p>\n<p>ccc</p>\n<p>ddd</p>" => TRUE,
|
926
|
"<!--comment\nwith linebreak--><p>\neee</p>\n<p>fff</p>" => TRUE,
|
927
|
),
|
928
|
// Check that a comment in a PRE will result that the text after
|
929
|
// the comment, but still in PRE, is not transformed.
|
930
|
"<pre>aaa\nbbb<!-- comment -->\n\nccc</pre>\nddd" => array(
|
931
|
"<pre>aaa\nbbb<!-- comment -->\n\nccc</pre>" => TRUE,
|
932
|
),
|
933
|
// Bug 810824, paragraphs were appearing around iframe tags.
|
934
|
"<iframe>aaa</iframe>\n\n" => array(
|
935
|
"<p><iframe>aaa</iframe></p>" => FALSE,
|
936
|
),
|
937
|
);
|
938
|
$this->assertFilteredString($filter, $tests);
|
939
|
|
940
|
// Very long string hitting PCRE limits.
|
941
|
$limit = max(ini_get('pcre.backtrack_limit'), ini_get('pcre.recursion_limit'));
|
942
|
$source = $this->randomName($limit);
|
943
|
$result = _filter_autop($source);
|
944
|
$success = $this->assertEqual($result, '<p>' . $source . "</p>\n", 'Line break filter can process very long strings.');
|
945
|
if (!$success) {
|
946
|
$this->verbose("\n" . $source . "\n<hr />\n" . $result);
|
947
|
}
|
948
|
}
|
949
|
|
950
|
/**
|
951
|
* Tests limiting allowed tags and XSS prevention.
|
952
|
*
|
953
|
* XSS tests assume that script is disallowed by default and src is allowed
|
954
|
* by default, but on* and style attributes are disallowed.
|
955
|
*
|
956
|
* Script injection vectors mostly adopted from http://ha.ckers.org/xss.html.
|
957
|
*
|
958
|
* Relevant CVEs:
|
959
|
* - CVE-2002-1806, ~CVE-2005-0682, ~CVE-2005-2106, CVE-2005-3973,
|
960
|
* CVE-2006-1226 (= rev. 1.112?), CVE-2008-0273, CVE-2008-3740.
|
961
|
*/
|
962
|
function testFilterXSS() {
|
963
|
// Tag stripping, different ways to work around removal of HTML tags.
|
964
|
$f = filter_xss('<script>alert(0)</script>');
|
965
|
$this->assertNoNormalized($f, 'script', 'HTML tag stripping -- simple script without special characters.');
|
966
|
|
967
|
$f = filter_xss('<script src="http://www.example.com" />');
|
968
|
$this->assertNoNormalized($f, 'script', 'HTML tag stripping -- empty script with source.');
|
969
|
|
970
|
$f = filter_xss('<ScRipt sRc=http://www.example.com/>');
|
971
|
$this->assertNoNormalized($f, 'script', 'HTML tag stripping evasion -- varying case.');
|
972
|
|
973
|
$f = filter_xss("<script\nsrc\n=\nhttp://www.example.com/\n>");
|
974
|
$this->assertNoNormalized($f, 'script', 'HTML tag stripping evasion -- multiline tag.');
|
975
|
|
976
|
$f = filter_xss('<script/a src=http://www.example.com/a.js></script>');
|
977
|
$this->assertNoNormalized($f, 'script', 'HTML tag stripping evasion -- non whitespace character after tag name.');
|
978
|
|
979
|
$f = filter_xss('<script/src=http://www.example.com/a.js></script>');
|
980
|
$this->assertNoNormalized($f, 'script', 'HTML tag stripping evasion -- no space between tag and attribute.');
|
981
|
|
982
|
// Null between < and tag name works at least with IE6.
|
983
|
$f = filter_xss("<\0scr\0ipt>alert(0)</script>");
|
984
|
$this->assertNoNormalized($f, 'ipt', 'HTML tag stripping evasion -- breaking HTML with nulls.');
|
985
|
|
986
|
$f = filter_xss("<scrscriptipt src=http://www.example.com/a.js>");
|
987
|
$this->assertNoNormalized($f, 'script', 'HTML tag stripping evasion -- filter just removing "script".');
|
988
|
|
989
|
$f = filter_xss('<<script>alert(0);//<</script>');
|
990
|
$this->assertNoNormalized($f, 'script', 'HTML tag stripping evasion -- double opening brackets.');
|
991
|
|
992
|
$f = filter_xss('<script src=http://www.example.com/a.js?<b>');
|
993
|
$this->assertNoNormalized($f, 'script', 'HTML tag stripping evasion -- no closing tag.');
|
994
|
|
995
|
// DRUPAL-SA-2008-047: This doesn't seem exploitable, but the filter should
|
996
|
// work consistently.
|
997
|
$f = filter_xss('<script>>');
|
998
|
$this->assertNoNormalized($f, 'script', 'HTML tag stripping evasion -- double closing tag.');
|
999
|
|
1000
|
$f = filter_xss('<script src=//www.example.com/.a>');
|
1001
|
$this->assertNoNormalized($f, 'script', 'HTML tag stripping evasion -- no scheme or ending slash.');
|
1002
|
|
1003
|
$f = filter_xss('<script src=http://www.example.com/.a');
|
1004
|
$this->assertNoNormalized($f, 'script', 'HTML tag stripping evasion -- no closing bracket.');
|
1005
|
|
1006
|
$f = filter_xss('<script src=http://www.example.com/ <');
|
1007
|
$this->assertNoNormalized($f, 'script', 'HTML tag stripping evasion -- opening instead of closing bracket.');
|
1008
|
|
1009
|
$f = filter_xss('<nosuchtag attribute="newScriptInjectionVector">');
|
1010
|
$this->assertNoNormalized($f, 'nosuchtag', 'HTML tag stripping evasion -- unknown tag.');
|
1011
|
|
1012
|
$f = filter_xss('<?xml:namespace ns="urn:schemas-microsoft-com:time">');
|
1013
|
$this->assertTrue(stripos($f, '<?xml') === FALSE, 'HTML tag stripping evasion -- starting with a question sign (processing instructions).');
|
1014
|
|
1015
|
$f = filter_xss('<t:set attributeName="innerHTML" to="<script defer>alert(0)</script>">');
|
1016
|
$this->assertNoNormalized($f, 't:set', 'HTML tag stripping evasion -- colon in the tag name (namespaces\' tricks).');
|
1017
|
|
1018
|
$f = filter_xss('<img """><script>alert(0)</script>', array('img'));
|
1019
|
$this->assertNoNormalized($f, 'script', 'HTML tag stripping evasion -- a malformed image tag.');
|
1020
|
|
1021
|
$f = filter_xss('<blockquote><script>alert(0)</script></blockquote>', array('blockquote'));
|
1022
|
$this->assertNoNormalized($f, 'script', 'HTML tag stripping evasion -- script in a blockqoute.');
|
1023
|
|
1024
|
$f = filter_xss("<!--[if true]><script>alert(0)</script><![endif]-->");
|
1025
|
$this->assertNoNormalized($f, 'script', 'HTML tag stripping evasion -- script within a comment.');
|
1026
|
|
1027
|
// Dangerous attributes removal.
|
1028
|
$f = filter_xss('<p onmouseover="http://www.example.com/">', array('p'));
|
1029
|
$this->assertNoNormalized($f, 'onmouseover', 'HTML filter attributes removal -- events, no evasion.');
|
1030
|
|
1031
|
$f = filter_xss('<li style="list-style-image: url(javascript:alert(0))">', array('li'));
|
1032
|
$this->assertNoNormalized($f, 'style', 'HTML filter attributes removal -- style, no evasion.');
|
1033
|
|
1034
|
$f = filter_xss('<img onerror =alert(0)>', array('img'));
|
1035
|
$this->assertNoNormalized($f, 'onerror', 'HTML filter attributes removal evasion -- spaces before equals sign.');
|
1036
|
|
1037
|
$f = filter_xss('<img onabort!#$%&()*~+-_.,:;?@[/|\]^`=alert(0)>', array('img'));
|
1038
|
$this->assertNoNormalized($f, 'onabort', 'HTML filter attributes removal evasion -- non alphanumeric characters before equals sign.');
|
1039
|
|
1040
|
$f = filter_xss('<img oNmediAError=alert(0)>', array('img'));
|
1041
|
$this->assertNoNormalized($f, 'onmediaerror', 'HTML filter attributes removal evasion -- varying case.');
|
1042
|
|
1043
|
// Works at least with IE6.
|
1044
|
$f = filter_xss("<img o\0nfocus\0=alert(0)>", array('img'));
|
1045
|
$this->assertNoNormalized($f, 'focus', 'HTML filter attributes removal evasion -- breaking with nulls.');
|
1046
|
|
1047
|
// Only whitelisted scheme names allowed in attributes.
|
1048
|
$f = filter_xss('<img src="javascript:alert(0)">', array('img'));
|
1049
|
$this->assertNoNormalized($f, 'javascript', 'HTML scheme clearing -- no evasion.');
|
1050
|
|
1051
|
$f = filter_xss('<img src=javascript:alert(0)>', array('img'));
|
1052
|
$this->assertNoNormalized($f, 'javascript', 'HTML scheme clearing evasion -- no quotes.');
|
1053
|
|
1054
|
// A bit like CVE-2006-0070.
|
1055
|
$f = filter_xss('<img src="javascript:confirm(0)">', array('img'));
|
1056
|
$this->assertNoNormalized($f, 'javascript', 'HTML scheme clearing evasion -- no alert ;)');
|
1057
|
|
1058
|
$f = filter_xss('<img src=`javascript:alert(0)`>', array('img'));
|
1059
|
$this->assertNoNormalized($f, 'javascript', 'HTML scheme clearing evasion -- grave accents.');
|
1060
|
|
1061
|
$f = filter_xss('<img dynsrc="javascript:alert(0)">', array('img'));
|
1062
|
$this->assertNoNormalized($f, 'javascript', 'HTML scheme clearing -- rare attribute.');
|
1063
|
|
1064
|
$f = filter_xss('<table background="javascript:alert(0)">', array('table'));
|
1065
|
$this->assertNoNormalized($f, 'javascript', 'HTML scheme clearing -- another tag.');
|
1066
|
|
1067
|
$f = filter_xss('<base href="javascript:alert(0);//">', array('base'));
|
1068
|
$this->assertNoNormalized($f, 'javascript', 'HTML scheme clearing -- one more attribute and tag.');
|
1069
|
|
1070
|
$f = filter_xss('<img src="jaVaSCriPt:alert(0)">', array('img'));
|
1071
|
$this->assertNoNormalized($f, 'javascript', 'HTML scheme clearing evasion -- varying case.');
|
1072
|
|
1073
|
$f = filter_xss('<img src=javascript:alert(0)>', array('img'));
|
1074
|
$this->assertNoNormalized($f, 'javascript', 'HTML scheme clearing evasion -- UTF-8 decimal encoding.');
|
1075
|
|
1076
|
$f = filter_xss('<img src=javascript:alert(0)>', array('img'));
|
1077
|
$this->assertNoNormalized($f, 'javascript', 'HTML scheme clearing evasion -- long UTF-8 encoding.');
|
1078
|
|
1079
|
$f = filter_xss('<img src=javascript:alert(0)>', array('img'));
|
1080
|
$this->assertNoNormalized($f, 'javascript', 'HTML scheme clearing evasion -- UTF-8 hex encoding.');
|
1081
|
|
1082
|
$f = filter_xss("<img src=\"jav\tascript:alert(0)\">", array('img'));
|
1083
|
$this->assertNoNormalized($f, 'script', 'HTML scheme clearing evasion -- an embedded tab.');
|
1084
|
|
1085
|
$f = filter_xss('<img src="jav	ascript:alert(0)">', array('img'));
|
1086
|
$this->assertNoNormalized($f, 'script', 'HTML scheme clearing evasion -- an encoded, embedded tab.');
|
1087
|
|
1088
|
$f = filter_xss('<img src="jav
ascript:alert(0)">', array('img'));
|
1089
|
$this->assertNoNormalized($f, 'script', 'HTML scheme clearing evasion -- an encoded, embedded newline.');
|
1090
|
|
1091
|
// With 
 this test would fail, but the entity gets turned into
|
1092
|
// &#xD;, so it's OK.
|
1093
|
$f = filter_xss('<img src="jav
ascript:alert(0)">', array('img'));
|
1094
|
$this->assertNoNormalized($f, 'script', 'HTML scheme clearing evasion -- an encoded, embedded carriage return.');
|
1095
|
|
1096
|
$f = filter_xss("<img src=\"\n\n\nj\na\nva\ns\ncript:alert(0)\">", array('img'));
|
1097
|
$this->assertNoNormalized($f, 'cript', 'HTML scheme clearing evasion -- broken into many lines.');
|
1098
|
|
1099
|
$f = filter_xss("<img src=\"jav\0a\0\0cript:alert(0)\">", array('img'));
|
1100
|
$this->assertNoNormalized($f, 'cript', 'HTML scheme clearing evasion -- embedded nulls.');
|
1101
|
|
1102
|
$f = filter_xss('<img src="  javascript:alert(0)">', array('img'));
|
1103
|
$this->assertNoNormalized($f, 'javascript', 'HTML scheme clearing evasion -- spaces and metacharacters before scheme.');
|
1104
|
|
1105
|
$f = filter_xss('<img src="vbscript:msgbox(0)">', array('img'));
|
1106
|
$this->assertNoNormalized($f, 'vbscript', 'HTML scheme clearing evasion -- another scheme.');
|
1107
|
|
1108
|
$f = filter_xss('<img src="nosuchscheme:notice(0)">', array('img'));
|
1109
|
$this->assertNoNormalized($f, 'nosuchscheme', 'HTML scheme clearing evasion -- unknown scheme.');
|
1110
|
|
1111
|
// Netscape 4.x javascript entities.
|
1112
|
$f = filter_xss('<br size="&{alert(0)}">', array('br'));
|
1113
|
$this->assertNoNormalized($f, 'alert', 'Netscape 4.x javascript entities.');
|
1114
|
|
1115
|
// DRUPAL-SA-2008-006: Invalid UTF-8, these only work as reflected XSS with
|
1116
|
// Internet Explorer 6.
|
1117
|
$f = filter_xss("<p arg=\"\xe0\">\" style=\"background-image: url(javascript:alert(0));\"\xe0<p>", array('p'));
|
1118
|
$this->assertNoNormalized($f, 'style', 'HTML filter -- invalid UTF-8.');
|
1119
|
|
1120
|
$f = filter_xss("\xc0aaa");
|
1121
|
$this->assertEqual($f, '', 'HTML filter -- overlong UTF-8 sequences.');
|
1122
|
|
1123
|
$f = filter_xss("Who's Online");
|
1124
|
$this->assertNormalized($f, "who's online", 'HTML filter -- html entity number');
|
1125
|
|
1126
|
$f = filter_xss("Who&#039;s Online");
|
1127
|
$this->assertNormalized($f, "who's online", 'HTML filter -- encoded html entity number');
|
1128
|
|
1129
|
$f = filter_xss("Who&amp;#039; Online");
|
1130
|
$this->assertNormalized($f, "who&#039; online", 'HTML filter -- double encoded html entity number');
|
1131
|
}
|
1132
|
|
1133
|
/**
|
1134
|
* Tests filter settings, defaults, access restrictions and similar.
|
1135
|
*
|
1136
|
* @todo This is for functions like filter_filter and check_markup, whose
|
1137
|
* functionality is not completely focused on filtering. Some ideas:
|
1138
|
* restricting formats according to user permissions, proper cache
|
1139
|
* handling, defaults -- allowed tags/attributes/protocols.
|
1140
|
*
|
1141
|
* @todo It is possible to add script, iframe etc. to allowed tags, but this
|
1142
|
* makes HTML filter completely ineffective.
|
1143
|
*
|
1144
|
* @todo Class, id, name and xmlns should be added to disallowed attributes,
|
1145
|
* or better a whitelist approach should be used for that too.
|
1146
|
*/
|
1147
|
function testHtmlFilter() {
|
1148
|
// Setup dummy filter object.
|
1149
|
$filter = new stdClass();
|
1150
|
$filter->settings = array(
|
1151
|
'allowed_html' => '<a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <test-element>',
|
1152
|
'filter_html_help' => 1,
|
1153
|
'filter_html_nofollow' => 0,
|
1154
|
);
|
1155
|
|
1156
|
// HTML filter is not able to secure some tags, these should never be
|
1157
|
// allowed.
|
1158
|
$f = _filter_html('<script />', $filter);
|
1159
|
$this->assertNoNormalized($f, 'script', 'HTML filter should always remove script tags.');
|
1160
|
|
1161
|
$f = _filter_html('<iframe />', $filter);
|
1162
|
$this->assertNoNormalized($f, 'iframe', 'HTML filter should always remove iframe tags.');
|
1163
|
|
1164
|
$f = _filter_html('<object />', $filter);
|
1165
|
$this->assertNoNormalized($f, 'object', 'HTML filter should always remove object tags.');
|
1166
|
|
1167
|
$f = _filter_html('<style />', $filter);
|
1168
|
$this->assertNoNormalized($f, 'style', 'HTML filter should always remove style tags.');
|
1169
|
|
1170
|
// Some tags make CSRF attacks easier, let the user take the risk herself.
|
1171
|
$f = _filter_html('<img />', $filter);
|
1172
|
$this->assertNoNormalized($f, 'img', 'HTML filter should remove img tags on default.');
|
1173
|
|
1174
|
$f = _filter_html('<input />', $filter);
|
1175
|
$this->assertNoNormalized($f, 'img', 'HTML filter should remove input tags on default.');
|
1176
|
|
1177
|
// Filtering content of some attributes is infeasible, these shouldn't be
|
1178
|
// allowed too.
|
1179
|
$f = _filter_html('<p style="display: none;" />', $filter);
|
1180
|
$this->assertNoNormalized($f, 'style', 'HTML filter should remove style attribute on default.');
|
1181
|
|
1182
|
$f = _filter_html('<p onerror="alert(0);" />', $filter);
|
1183
|
$this->assertNoNormalized($f, 'onerror', 'HTML filter should remove on* attributes on default.');
|
1184
|
|
1185
|
$f = _filter_html('<code onerror> </code>', $filter);
|
1186
|
$this->assertNoNormalized($f, 'onerror', 'HTML filter should remove empty on* attributes on default.');
|
1187
|
|
1188
|
// Custom tags are supported and should be allowed through.
|
1189
|
$f = _filter_html('<test-element></test-element>', $filter);
|
1190
|
$this->assertNormalized($f, 'test-element', 'HTML filter should allow custom elements.');
|
1191
|
}
|
1192
|
|
1193
|
/**
|
1194
|
* Tests the spam deterrent.
|
1195
|
*/
|
1196
|
function testNoFollowFilter() {
|
1197
|
// Setup dummy filter object.
|
1198
|
$filter = new stdClass();
|
1199
|
$filter->settings = array(
|
1200
|
'allowed_html' => '<a>',
|
1201
|
'filter_html_help' => 1,
|
1202
|
'filter_html_nofollow' => 1,
|
1203
|
);
|
1204
|
|
1205
|
// Test if the rel="nofollow" attribute is added, even if we try to prevent
|
1206
|
// it.
|
1207
|
$f = _filter_html('<a href="http://www.example.com/">text</a>', $filter);
|
1208
|
$this->assertNormalized($f, 'rel="nofollow"', 'Spam deterrent -- no evasion.');
|
1209
|
|
1210
|
$f = _filter_html('<A href="http://www.example.com/">text</a>', $filter);
|
1211
|
$this->assertNormalized($f, 'rel="nofollow"', 'Spam deterrent evasion -- capital A.');
|
1212
|
|
1213
|
$f = _filter_html("<a/href=\"http://www.example.com/\">text</a>", $filter);
|
1214
|
$this->assertNormalized($f, 'rel="nofollow"', 'Spam deterrent evasion -- non whitespace character after tag name.');
|
1215
|
|
1216
|
$f = _filter_html("<\0a\0 href=\"http://www.example.com/\">text</a>", $filter);
|
1217
|
$this->assertNormalized($f, 'rel="nofollow"', 'Spam deterrent evasion -- some nulls.');
|
1218
|
|
1219
|
$f = _filter_html('<a href="http://www.example.com/" rel="follow">text</a>', $filter);
|
1220
|
$this->assertNoNormalized($f, 'rel="follow"', 'Spam deterrent evasion -- with rel set - rel="follow" removed.');
|
1221
|
$this->assertNormalized($f, 'rel="nofollow"', 'Spam deterrent evasion -- with rel set - rel="nofollow" added.');
|
1222
|
}
|
1223
|
|
1224
|
/**
|
1225
|
* Tests the loose, admin HTML filter.
|
1226
|
*/
|
1227
|
function testFilterXSSAdmin() {
|
1228
|
// DRUPAL-SA-2008-044
|
1229
|
$f = filter_xss_admin('<object />');
|
1230
|
$this->assertNoNormalized($f, 'object', 'Admin HTML filter -- should not allow object tag.');
|
1231
|
|
1232
|
$f = filter_xss_admin('<script />');
|
1233
|
$this->assertNoNormalized($f, 'script', 'Admin HTML filter -- should not allow script tag.');
|
1234
|
|
1235
|
$f = filter_xss_admin('<style /><iframe /><frame /><frameset /><meta /><link /><embed /><applet /><param /><layer />');
|
1236
|
$this->assertEqual($f, '', 'Admin HTML filter -- should never allow some tags.');
|
1237
|
}
|
1238
|
|
1239
|
/**
|
1240
|
* Tests the HTML escaping filter.
|
1241
|
*
|
1242
|
* check_plain() is not tested here.
|
1243
|
*/
|
1244
|
function testHtmlEscapeFilter() {
|
1245
|
// Setup dummy filter object.
|
1246
|
$filter = new stdClass();
|
1247
|
$filter->callback = '_filter_html_escape';
|
1248
|
|
1249
|
$tests = array(
|
1250
|
" One. <!-- \"comment\" --> Two'.\n<p>Three.</p>\n " => array(
|
1251
|
"One. <!-- "comment" --> Two'.\n<p>Three.</p>" => TRUE,
|
1252
|
' One.' => FALSE,
|
1253
|
"</p>\n " => FALSE,
|
1254
|
),
|
1255
|
);
|
1256
|
$this->assertFilteredString($filter, $tests);
|
1257
|
}
|
1258
|
|
1259
|
/**
|
1260
|
* Tests the URL filter.
|
1261
|
*/
|
1262
|
function testUrlFilter() {
|
1263
|
// Setup dummy filter object.
|
1264
|
$filter = new stdClass();
|
1265
|
$filter->callback = '_filter_url';
|
1266
|
$filter->settings = array(
|
1267
|
'filter_url_length' => 496,
|
1268
|
);
|
1269
|
// @todo Possible categories:
|
1270
|
// - absolute, mail, partial
|
1271
|
// - characters/encoding, surrounding markup, security
|
1272
|
|
1273
|
// Create a e-mail that is too long.
|
1274
|
$long_email = str_repeat('a', 254) . '@example.com';
|
1275
|
$too_long_email = str_repeat('b', 255) . '@example.com';
|
1276
|
|
1277
|
|
1278
|
// Filter selection/pattern matching.
|
1279
|
$tests = array(
|
1280
|
// HTTP URLs.
|
1281
|
'
|
1282
|
http://example.com or www.example.com
|
1283
|
' => array(
|
1284
|
'<a href="http://example.com">http://example.com</a>' => TRUE,
|
1285
|
'<a href="http://www.example.com">www.example.com</a>' => TRUE,
|
1286
|
),
|
1287
|
// MAILTO URLs.
|
1288
|
'
|
1289
|
person@example.com or mailto:person2@example.com or ' . $long_email . ' but not ' . $too_long_email . '
|
1290
|
' => array(
|
1291
|
'<a href="mailto:person@example.com">person@example.com</a>' => TRUE,
|
1292
|
'<a href="mailto:person2@example.com">mailto:person2@example.com</a>' => TRUE,
|
1293
|
'<a href="mailto:' . $long_email . '">' . $long_email . '</a>' => TRUE,
|
1294
|
'<a href="mailto:' . $too_long_email . '">' . $too_long_email . '</a>' => FALSE,
|
1295
|
),
|
1296
|
// URI parts and special characters.
|
1297
|
'
|
1298
|
http://trailingslash.com/ or www.trailingslash.com/
|
1299
|
http://host.com/some/path?query=foo&bar[baz]=beer#fragment or www.host.com/some/path?query=foo&bar[baz]=beer#fragment
|
1300
|
http://twitter.com/#!/example/status/22376963142324226
|
1301
|
ftp://user:pass@ftp.example.com/~home/dir1
|
1302
|
sftp://user@nonstandardport:222/dir
|
1303
|
ssh://192.168.0.100/srv/git/drupal.git
|
1304
|
' => array(
|
1305
|
'<a href="http://trailingslash.com/">http://trailingslash.com/</a>' => TRUE,
|
1306
|
'<a href="http://www.trailingslash.com/">www.trailingslash.com/</a>' => TRUE,
|
1307
|
'<a href="http://host.com/some/path?query=foo&bar[baz]=beer#fragment">http://host.com/some/path?query=foo&bar[baz]=beer#fragment</a>' => TRUE,
|
1308
|
'<a href="http://www.host.com/some/path?query=foo&bar[baz]=beer#fragment">www.host.com/some/path?query=foo&bar[baz]=beer#fragment</a>' => TRUE,
|
1309
|
'<a href="http://twitter.com/#!/example/status/22376963142324226">http://twitter.com/#!/example/status/22376963142324226</a>' => TRUE,
|
1310
|
'<a href="ftp://user:pass@ftp.example.com/~home/dir1">ftp://user:pass@ftp.example.com/~home/dir1</a>' => TRUE,
|
1311
|
'<a href="sftp://user@nonstandardport:222/dir">sftp://user@nonstandardport:222/dir</a>' => TRUE,
|
1312
|
'<a href="ssh://192.168.0.100/srv/git/drupal.git">ssh://192.168.0.100/srv/git/drupal.git</a>' => TRUE,
|
1313
|
),
|
1314
|
// Encoding.
|
1315
|
'
|
1316
|
http://ampersand.com/?a=1&b=2
|
1317
|
http://encoded.com/?a=1&b=2
|
1318
|
' => array(
|
1319
|
'<a href="http://ampersand.com/?a=1&b=2">http://ampersand.com/?a=1&b=2</a>' => TRUE,
|
1320
|
'<a href="http://encoded.com/?a=1&b=2">http://encoded.com/?a=1&b=2</a>' => TRUE,
|
1321
|
),
|
1322
|
// Domain name length.
|
1323
|
'
|
1324
|
www.ex.ex or www.example.example or www.toolongdomainexampledomainexampledomainexampledomainexampledomain or
|
1325
|
me@me.tv
|
1326
|
' => array(
|
1327
|
'<a href="http://www.ex.ex">www.ex.ex</a>' => TRUE,
|
1328
|
'<a href="http://www.example.example">www.example.example</a>' => TRUE,
|
1329
|
'http://www.toolong' => FALSE,
|
1330
|
'<a href="mailto:me@me.tv">me@me.tv</a>' => TRUE,
|
1331
|
),
|
1332
|
// Absolute URL protocols.
|
1333
|
// The list to test is found in the beginning of _filter_url() at
|
1334
|
// $protocols = variable_get('filter_allowed_protocols'... (approx line 1325).
|
1335
|
'
|
1336
|
https://example.com,
|
1337
|
ftp://ftp.example.com,
|
1338
|
news://example.net,
|
1339
|
telnet://example,
|
1340
|
irc://example.host,
|
1341
|
ssh://odd.geek,
|
1342
|
sftp://secure.host?,
|
1343
|
webcal://calendar,
|
1344
|
rtsp://127.0.0.1,
|
1345
|
not foo://disallowed.com.
|
1346
|
' => array(
|
1347
|
'href="https://example.com"' => TRUE,
|
1348
|
'href="ftp://ftp.example.com"' => TRUE,
|
1349
|
'href="news://example.net"' => TRUE,
|
1350
|
'href="telnet://example"' => TRUE,
|
1351
|
'href="irc://example.host"' => TRUE,
|
1352
|
'href="ssh://odd.geek"' => TRUE,
|
1353
|
'href="sftp://secure.host"' => TRUE,
|
1354
|
'href="webcal://calendar"' => TRUE,
|
1355
|
'href="rtsp://127.0.0.1"' => TRUE,
|
1356
|
'href="foo://disallowed.com"' => FALSE,
|
1357
|
'not foo://disallowed.com.' => TRUE,
|
1358
|
),
|
1359
|
);
|
1360
|
$this->assertFilteredString($filter, $tests);
|
1361
|
|
1362
|
// Surrounding text/punctuation.
|
1363
|
$tests = array(
|
1364
|
'
|
1365
|
Partial URL with trailing period www.partial.com.
|
1366
|
E-mail with trailing comma person@example.com,
|
1367
|
Absolute URL with trailing question http://www.absolute.com?
|
1368
|
Query string with trailing exclamation www.query.com/index.php?a=!
|
1369
|
Partial URL with 3 trailing www.partial.periods...
|
1370
|
E-mail with 3 trailing exclamations@example.com!!!
|
1371
|
Absolute URL and query string with 2 different punctuation characters (http://www.example.com/q=abc).
|
1372
|
' => array(
|
1373
|
'period <a href="http://www.partial.com">www.partial.com</a>.' => TRUE,
|
1374
|
'comma <a href="mailto:person@example.com">person@example.com</a>,' => TRUE,
|
1375
|
'question <a href="http://www.absolute.com">http://www.absolute.com</a>?' => TRUE,
|
1376
|
'exclamation <a href="http://www.query.com/index.php?a=">www.query.com/index.php?a=</a>!' => TRUE,
|
1377
|
'trailing <a href="http://www.partial.periods">www.partial.periods</a>...' => TRUE,
|
1378
|
'trailing <a href="mailto:exclamations@example.com">exclamations@example.com</a>!!!' => TRUE,
|
1379
|
'characters (<a href="http://www.example.com/q=abc">http://www.example.com/q=abc</a>).' => TRUE,
|
1380
|
),
|
1381
|
'
|
1382
|
(www.parenthesis.com/dir?a=1&b=2#a)
|
1383
|
' => array(
|
1384
|
'(<a href="http://www.parenthesis.com/dir?a=1&b=2#a">www.parenthesis.com/dir?a=1&b=2#a</a>)' => TRUE,
|
1385
|
),
|
1386
|
);
|
1387
|
$this->assertFilteredString($filter, $tests);
|
1388
|
|
1389
|
// Surrounding markup.
|
1390
|
$tests = array(
|
1391
|
'
|
1392
|
<p xmlns="www.namespace.com" />
|
1393
|
<p xmlns="http://namespace.com">
|
1394
|
An <a href="http://example.com" title="Read more at www.example.info...">anchor</a>.
|
1395
|
</p>
|
1396
|
' => array(
|
1397
|
'<p xmlns="www.namespace.com" />' => TRUE,
|
1398
|
'<p xmlns="http://namespace.com">' => TRUE,
|
1399
|
'href="http://www.namespace.com"' => FALSE,
|
1400
|
'href="http://namespace.com"' => FALSE,
|
1401
|
'An <a href="http://example.com" title="Read more at www.example.info...">anchor</a>.' => TRUE,
|
1402
|
),
|
1403
|
'
|
1404
|
Not <a href="foo">www.relative.com</a> or <a href="http://absolute.com">www.absolute.com</a>
|
1405
|
but <strong>http://www.strong.net</strong> or <em>www.emphasis.info</em>
|
1406
|
' => array(
|
1407
|
'<a href="foo">www.relative.com</a>' => TRUE,
|
1408
|
'href="http://www.relative.com"' => FALSE,
|
1409
|
'<a href="http://absolute.com">www.absolute.com</a>' => TRUE,
|
1410
|
'<strong><a href="http://www.strong.net">http://www.strong.net</a></strong>' => TRUE,
|
1411
|
'<em><a href="http://www.emphasis.info">www.emphasis.info</a></em>' => TRUE,
|
1412
|
),
|
1413
|
'
|
1414
|
Test <code>using www.example.com the code tag</code>.
|
1415
|
' => array(
|
1416
|
'href' => FALSE,
|
1417
|
'http' => FALSE,
|
1418
|
),
|
1419
|
'
|
1420
|
Intro.
|
1421
|
<blockquote>
|
1422
|
Quoted text linking to www.example.com, written by person@example.com, originating from http://origin.example.com. <code>@see www.usage.example.com or <em>www.example.info</em> bla bla</code>.
|
1423
|
</blockquote>
|
1424
|
|
1425
|
Outro.
|
1426
|
' => array(
|
1427
|
'href="http://www.example.com"' => TRUE,
|
1428
|
'href="mailto:person@example.com"' => TRUE,
|
1429
|
'href="http://origin.example.com"' => TRUE,
|
1430
|
'http://www.usage.example.com' => FALSE,
|
1431
|
'http://www.example.info' => FALSE,
|
1432
|
'Intro.' => TRUE,
|
1433
|
'Outro.' => TRUE,
|
1434
|
),
|
1435
|
'
|
1436
|
Unknown tag <x>containing x and www.example.com</x>? And a tag <pooh>beginning with p and containing www.example.pooh with p?</pooh>
|
1437
|
' => array(
|
1438
|
'href="http://www.example.com"' => TRUE,
|
1439
|
'href="http://www.example.pooh"' => TRUE,
|
1440
|
),
|
1441
|
'
|
1442
|
<p>Test <br/>: This is a www.example17.com example <strong>with</strong> various http://www.example18.com tags. *<br/>
|
1443
|
It is important www.example19.com to *<br/>test different URLs and http://www.example20.com in the same paragraph. *<br>
|
1444
|
HTML www.example21.com soup by person@example22.com can litererally http://www.example23.com contain *img*<img> anything. Just a www.example24.com with http://www.example25.com thrown in. www.example26.com from person@example27.com with extra http://www.example28.com.
|
1445
|
' => array(
|
1446
|
'href="http://www.example17.com"' => TRUE,
|
1447
|
'href="http://www.example18.com"' => TRUE,
|
1448
|
'href="http://www.example19.com"' => TRUE,
|
1449
|
'href="http://www.example20.com"' => TRUE,
|
1450
|
'href="http://www.example21.com"' => TRUE,
|
1451
|
'href="mailto:person@example22.com"' => TRUE,
|
1452
|
'href="http://www.example23.com"' => TRUE,
|
1453
|
'href="http://www.example24.com"' => TRUE,
|
1454
|
'href="http://www.example25.com"' => TRUE,
|
1455
|
'href="http://www.example26.com"' => TRUE,
|
1456
|
'href="mailto:person@example27.com"' => TRUE,
|
1457
|
'href="http://www.example28.com"' => TRUE,
|
1458
|
),
|
1459
|
'
|
1460
|
<script>
|
1461
|
<!--
|
1462
|
// @see www.example.com
|
1463
|
var exampleurl = "http://example.net";
|
1464
|
-->
|
1465
|
<!--//--><![CDATA[//><!--
|
1466
|
// @see www.example.com
|
1467
|
var exampleurl = "http://example.net";
|
1468
|
//--><!]]>
|
1469
|
</script>
|
1470
|
' => array(
|
1471
|
'href="http://www.example.com"' => FALSE,
|
1472
|
'href="http://example.net"' => FALSE,
|
1473
|
),
|
1474
|
'
|
1475
|
<style>body {
|
1476
|
background: url(http://example.com/pixel.gif);
|
1477
|
}</style>
|
1478
|
' => array(
|
1479
|
'href' => FALSE,
|
1480
|
),
|
1481
|
'
|
1482
|
<!-- Skip any URLs like www.example.com in comments -->
|
1483
|
' => array(
|
1484
|
'href' => FALSE,
|
1485
|
),
|
1486
|
'
|
1487
|
<!-- Skip any URLs like
|
1488
|
www.example.com with a newline in comments -->
|
1489
|
' => array(
|
1490
|
'href' => FALSE,
|
1491
|
),
|
1492
|
'
|
1493
|
<!-- Skip any URLs like www.comment.com in comments. <p>Also ignore http://commented.out/markup.</p> -->
|
1494
|
' => array(
|
1495
|
'href' => FALSE,
|
1496
|
),
|
1497
|
'
|
1498
|
<dl>
|
1499
|
<dt>www.example.com</dt>
|
1500
|
<dd>http://example.com</dd>
|
1501
|
<dd>person@example.com</dd>
|
1502
|
<dt>Check www.example.net</dt>
|
1503
|
<dd>Some text around http://www.example.info by person@example.info?</dd>
|
1504
|
</dl>
|
1505
|
' => array(
|
1506
|
'href="http://www.example.com"' => TRUE,
|
1507
|
'href="http://example.com"' => TRUE,
|
1508
|
'href="mailto:person@example.com"' => TRUE,
|
1509
|
'href="http://www.example.net"' => TRUE,
|
1510
|
'href="http://www.example.info"' => TRUE,
|
1511
|
'href="mailto:person@example.info"' => TRUE,
|
1512
|
),
|
1513
|
'
|
1514
|
<div>www.div.com</div>
|
1515
|
<ul>
|
1516
|
<li>http://listitem.com</li>
|
1517
|
<li class="odd">www.class.listitem.com</li>
|
1518
|
</ul>
|
1519
|
' => array(
|
1520
|
'<div><a href="http://www.div.com">www.div.com</a></div>' => TRUE,
|
1521
|
'<li><a href="http://listitem.com">http://listitem.com</a></li>' => TRUE,
|
1522
|
'<li class="odd"><a href="http://www.class.listitem.com">www.class.listitem.com</a></li>' => TRUE,
|
1523
|
),
|
1524
|
);
|
1525
|
$this->assertFilteredString($filter, $tests);
|
1526
|
|
1527
|
// URL trimming.
|
1528
|
$filter->settings['filter_url_length'] = 20;
|
1529
|
$tests = array(
|
1530
|
'www.trimmed.com/d/ff.ext?a=1&b=2#a1' => array(
|
1531
|
'<a href="http://www.trimmed.com/d/ff.ext?a=1&b=2#a1">www.trimmed.com/d/ff...</a>' => TRUE,
|
1532
|
),
|
1533
|
);
|
1534
|
$this->assertFilteredString($filter, $tests);
|
1535
|
}
|
1536
|
|
1537
|
/**
|
1538
|
* Asserts multiple filter output expectations for multiple input strings.
|
1539
|
*
|
1540
|
* @param $filter
|
1541
|
* A input filter object.
|
1542
|
* @param $tests
|
1543
|
* An associative array, whereas each key is an arbitrary input string and
|
1544
|
* each value is again an associative array whose keys are filter output
|
1545
|
* strings and whose values are Booleans indicating whether the output is
|
1546
|
* expected or not.
|
1547
|
*
|
1548
|
* For example:
|
1549
|
* @code
|
1550
|
* $tests = array(
|
1551
|
* 'Input string' => array(
|
1552
|
* '<p>Input string</p>' => TRUE,
|
1553
|
* 'Input string<br' => FALSE,
|
1554
|
* ),
|
1555
|
* );
|
1556
|
* @endcode
|
1557
|
*/
|
1558
|
function assertFilteredString($filter, $tests) {
|
1559
|
foreach ($tests as $source => $tasks) {
|
1560
|
$function = $filter->callback;
|
1561
|
$result = $function($source, $filter);
|
1562
|
foreach ($tasks as $value => $is_expected) {
|
1563
|
// Not using assertIdentical, since combination with strpos() is hard to grok.
|
1564
|
if ($is_expected) {
|
1565
|
$success = $this->assertTrue(strpos($result, $value) !== FALSE, format_string('@source: @value found.', array(
|
1566
|
'@source' => var_export($source, TRUE),
|
1567
|
'@value' => var_export($value, TRUE),
|
1568
|
)));
|
1569
|
}
|
1570
|
else {
|
1571
|
$success = $this->assertTrue(strpos($result, $value) === FALSE, format_string('@source: @value not found.', array(
|
1572
|
'@source' => var_export($source, TRUE),
|
1573
|
'@value' => var_export($value, TRUE),
|
1574
|
)));
|
1575
|
}
|
1576
|
if (!$success) {
|
1577
|
$this->verbose('Source:<pre>' . check_plain(var_export($source, TRUE)) . '</pre>'
|
1578
|
. '<hr />' . 'Result:<pre>' . check_plain(var_export($result, TRUE)) . '</pre>'
|
1579
|
. '<hr />' . ($is_expected ? 'Expected:' : 'Not expected:')
|
1580
|
. '<pre>' . check_plain(var_export($value, TRUE)) . '</pre>'
|
1581
|
);
|
1582
|
}
|
1583
|
}
|
1584
|
}
|
1585
|
}
|
1586
|
|
1587
|
/**
|
1588
|
* Tests URL filter on longer content.
|
1589
|
*
|
1590
|
* Filters based on regular expressions should also be tested with a more
|
1591
|
* complex content than just isolated test lines.
|
1592
|
* The most common errors are:
|
1593
|
* - accidental '*' (greedy) match instead of '*?' (minimal) match.
|
1594
|
* - only matching first occurrence instead of all.
|
1595
|
* - newlines not matching '.*'.
|
1596
|
*
|
1597
|
* This test covers:
|
1598
|
* - Document with multiple newlines and paragraphs (two newlines).
|
1599
|
* - Mix of several HTML tags, invalid non-HTML tags, tags to ignore and HTML
|
1600
|
* comments.
|
1601
|
* - Empty HTML tags (BR, IMG).
|
1602
|
* - Mix of absolute and partial URLs, and e-mail addresses in one content.
|
1603
|
*/
|
1604
|
function testUrlFilterContent() {
|
1605
|
// Setup dummy filter object.
|
1606
|
$filter = new stdClass();
|
1607
|
$filter->settings = array(
|
1608
|
'filter_url_length' => 496,
|
1609
|
);
|
1610
|
$path = drupal_get_path('module', 'filter') . '/tests';
|
1611
|
|
1612
|
$input = file_get_contents($path . '/filter.url-input.txt');
|
1613
|
$expected = file_get_contents($path . '/filter.url-output.txt');
|
1614
|
$result = _filter_url($input, $filter);
|
1615
|
$this->assertIdentical($result, $expected, 'Complex HTML document was correctly processed.');
|
1616
|
}
|
1617
|
|
1618
|
/**
|
1619
|
* Tests the HTML corrector filter.
|
1620
|
*
|
1621
|
* @todo This test could really use some validity checking function.
|
1622
|
*/
|
1623
|
function testHtmlCorrectorFilter() {
|
1624
|
// Tag closing.
|
1625
|
$f = _filter_htmlcorrector('<p>text');
|
1626
|
$this->assertEqual($f, '<p>text</p>', 'HTML corrector -- tag closing at the end of input.');
|
1627
|
|
1628
|
$f = _filter_htmlcorrector('<p>text<p><p>text');
|
1629
|
$this->assertEqual($f, '<p>text</p><p></p><p>text</p>', 'HTML corrector -- tag closing.');
|
1630
|
|
1631
|
$f = _filter_htmlcorrector("<ul><li>e1<li>e2");
|
1632
|
$this->assertEqual($f, "<ul><li>e1</li><li>e2</li></ul>", 'HTML corrector -- unclosed list tags.');
|
1633
|
|
1634
|
$f = _filter_htmlcorrector('<div id="d">content');
|
1635
|
$this->assertEqual($f, '<div id="d">content</div>', 'HTML corrector -- unclosed tag with attribute.');
|
1636
|
|
1637
|
// XHTML slash for empty elements.
|
1638
|
$f = _filter_htmlcorrector('<hr><br>');
|
1639
|
$this->assertEqual($f, '<hr /><br />', 'HTML corrector -- XHTML closing slash.');
|
1640
|
|
1641
|
$f = _filter_htmlcorrector('<P>test</P>');
|
1642
|
$this->assertEqual($f, '<p>test</p>', 'HTML corrector -- Convert uppercased tags to proper lowercased ones.');
|
1643
|
|
1644
|
$f = _filter_htmlcorrector('<P>test</p>');
|
1645
|
$this->assertEqual($f, '<p>test</p>', 'HTML corrector -- Convert uppercased tags to proper lowercased ones.');
|
1646
|
|
1647
|
$f = _filter_htmlcorrector('test<hr />');
|
1648
|
$this->assertEqual($f, 'test<hr />', 'HTML corrector -- Let proper XHTML pass through.');
|
1649
|
|
1650
|
$f = _filter_htmlcorrector('test<hr/>');
|
1651
|
$this->assertEqual($f, 'test<hr />', 'HTML corrector -- Let proper XHTML pass through, but ensure there is a single space before the closing slash.');
|
1652
|
|
1653
|
$f = _filter_htmlcorrector('test<hr />');
|
1654
|
$this->assertEqual($f, 'test<hr />', 'HTML corrector -- Let proper XHTML pass through, but ensure there are not too many spaces before the closing slash.');
|
1655
|
|
1656
|
$f = _filter_htmlcorrector('<span class="test" />');
|
1657
|
$this->assertEqual($f, '<span class="test"></span>', 'HTML corrector -- Convert XHTML that is properly formed but that would not be compatible with typical HTML user agents.');
|
1658
|
|
1659
|
$f = _filter_htmlcorrector('test1<br class="test">test2');
|
1660
|
$this->assertEqual($f, 'test1<br class="test" />test2', 'HTML corrector -- Automatically close single tags.');
|
1661
|
|
1662
|
$f = _filter_htmlcorrector('line1<hr>line2');
|
1663
|
$this->assertEqual($f, 'line1<hr />line2', 'HTML corrector -- Automatically close single tags.');
|
1664
|
|
1665
|
$f = _filter_htmlcorrector('line1<HR>line2');
|
1666
|
$this->assertEqual($f, 'line1<hr />line2', 'HTML corrector -- Automatically close single tags.');
|
1667
|
|
1668
|
$f = _filter_htmlcorrector('<img src="http://example.com/test.jpg">test</img>');
|
1669
|
$this->assertEqual($f, '<img src="http://example.com/test.jpg" />test', 'HTML corrector -- Automatically close single tags.');
|
1670
|
|
1671
|
$f = _filter_htmlcorrector('<br></br>');
|
1672
|
$this->assertEqual($f, '<br />', "HTML corrector -- Transform empty tags to a single closed tag if the tag's content model is EMPTY.");
|
1673
|
|
1674
|
$f = _filter_htmlcorrector('<div></div>');
|
1675
|
$this->assertEqual($f, '<div></div>', "HTML corrector -- Do not transform empty tags to a single closed tag if the tag's content model is not EMPTY.");
|
1676
|
|
1677
|
$f = _filter_htmlcorrector('<p>line1<br/><hr/>line2</p>');
|
1678
|
$this->assertEqual($f, '<p>line1<br /></p><hr />line2', 'HTML corrector -- Move non-inline elements outside of inline containers.');
|
1679
|
|
1680
|
$f = _filter_htmlcorrector('<p>line1<div>line2</div></p>');
|
1681
|
$this->assertEqual($f, '<p>line1</p><div>line2</div>', 'HTML corrector -- Move non-inline elements outside of inline containers.');
|
1682
|
|
1683
|
$f = _filter_htmlcorrector('<p>test<p>test</p>\n');
|
1684
|
$this->assertEqual($f, '<p>test</p><p>test</p>\n', 'HTML corrector -- Auto-close improperly nested tags.');
|
1685
|
|
1686
|
$f = _filter_htmlcorrector('<p>Line1<br><STRONG>bold stuff</b>');
|
1687
|
$this->assertEqual($f, '<p>Line1<br /><strong>bold stuff</strong></p>', 'HTML corrector -- Properly close unclosed tags, and remove useless closing tags.');
|
1688
|
|
1689
|
$f = _filter_htmlcorrector('test <!-- this is a comment -->');
|
1690
|
$this->assertEqual($f, 'test <!-- this is a comment -->', 'HTML corrector -- Do not touch HTML comments.');
|
1691
|
|
1692
|
$f = _filter_htmlcorrector('test <!--this is a comment-->');
|
1693
|
$this->assertEqual($f, 'test <!--this is a comment-->', 'HTML corrector -- Do not touch HTML comments.');
|
1694
|
|
1695
|
$f = _filter_htmlcorrector('test <!-- comment <p>another
|
1696
|
<strong>multiple</strong> line
|
1697
|
comment</p> -->');
|
1698
|
$this->assertEqual($f, 'test <!-- comment <p>another
|
1699
|
<strong>multiple</strong> line
|
1700
|
comment</p> -->', 'HTML corrector -- Do not touch HTML comments.');
|
1701
|
|
1702
|
$f = _filter_htmlcorrector('test <!-- comment <p>another comment</p> -->');
|
1703
|
$this->assertEqual($f, 'test <!-- comment <p>another comment</p> -->', 'HTML corrector -- Do not touch HTML comments.');
|
1704
|
|
1705
|
$f = _filter_htmlcorrector('test <!--break-->');
|
1706
|
$this->assertEqual($f, 'test <!--break-->', 'HTML corrector -- Do not touch HTML comments.');
|
1707
|
|
1708
|
$f = _filter_htmlcorrector('<p>test\n</p>\n');
|
1709
|
$this->assertEqual($f, '<p>test\n</p>\n', 'HTML corrector -- New-lines are accepted and kept as-is.');
|
1710
|
|
1711
|
$f = _filter_htmlcorrector('<p>دروبال');
|
1712
|
$this->assertEqual($f, '<p>دروبال</p>', 'HTML corrector -- Encoding is correctly kept.');
|
1713
|
|
1714
|
$f = _filter_htmlcorrector('<script type="text/javascript">alert("test")</script>');
|
1715
|
$this->assertEqual($f, '<script type="text/javascript">
|
1716
|
<!--//--><![CDATA[// ><!--
|
1717
|
alert("test")
|
1718
|
//--><!]]>
|
1719
|
</script>', 'HTML corrector -- CDATA added to script element');
|
1720
|
|
1721
|
$f = _filter_htmlcorrector('<p><script type="text/javascript">alert("test")</script></p>');
|
1722
|
$this->assertEqual($f, '<p><script type="text/javascript">
|
1723
|
<!--//--><![CDATA[// ><!--
|
1724
|
alert("test")
|
1725
|
//--><!]]>
|
1726
|
</script></p>', 'HTML corrector -- CDATA added to a nested script element');
|
1727
|
|
1728
|
$f = _filter_htmlcorrector('<p><style> /* Styling */ body {color:red}</style></p>');
|
1729
|
$this->assertEqual($f, '<p><style>
|
1730
|
<!--/*--><![CDATA[/* ><!--*/
|
1731
|
/* Styling */ body {color:red}
|
1732
|
/*--><!]]>*/
|
1733
|
</style></p>', 'HTML corrector -- CDATA added to a style element.');
|
1734
|
|
1735
|
$filtered_data = _filter_htmlcorrector('<p><style>
|
1736
|
/*<![CDATA[*/
|
1737
|
/* Styling */
|
1738
|
body {color:red}
|
1739
|
/*]]>*/
|
1740
|
</style></p>');
|
1741
|
$this->assertEqual($filtered_data, '<p><style>
|
1742
|
<!--/*--><![CDATA[/* ><!--*/
|
1743
|
|
1744
|
/*<![CDATA[*/
|
1745
|
/* Styling */
|
1746
|
body {color:red}
|
1747
|
/*]]]]><![CDATA[>*/
|
1748
|
|
1749
|
/*--><!]]>*/
|
1750
|
</style></p>',
|
1751
|
format_string('HTML corrector -- Existing cdata section @pattern_name properly escaped', array('@pattern_name' => '/*<![CDATA[*/'))
|
1752
|
);
|
1753
|
|
1754
|
$filtered_data = _filter_htmlcorrector('<p><style>
|
1755
|
<!--/*--><![CDATA[/* ><!--*/
|
1756
|
/* Styling */
|
1757
|
body {color:red}
|
1758
|
/*--><!]]>*/
|
1759
|
</style></p>');
|
1760
|
$this->assertEqual($filtered_data, '<p><style>
|
1761
|
<!--/*--><![CDATA[/* ><!--*/
|
1762
|
|
1763
|
<!--/*--><![CDATA[/* ><!--*/
|
1764
|
/* Styling */
|
1765
|
body {color:red}
|
1766
|
/*--><!]]]]><![CDATA[>*/
|
1767
|
|
1768
|
/*--><!]]>*/
|
1769
|
</style></p>',
|
1770
|
format_string('HTML corrector -- Existing cdata section @pattern_name properly escaped', array('@pattern_name' => '<!--/*--><![CDATA[/* ><!--*/'))
|
1771
|
);
|
1772
|
|
1773
|
$filtered_data = _filter_htmlcorrector('<p><script type="text/javascript">
|
1774
|
<!--//--><![CDATA[// ><!--
|
1775
|
alert("test");
|
1776
|
//--><!]]>
|
1777
|
</script></p>');
|
1778
|
$this->assertEqual($filtered_data, '<p><script type="text/javascript">
|
1779
|
<!--//--><![CDATA[// ><!--
|
1780
|
|
1781
|
<!--//--><![CDATA[// ><!--
|
1782
|
alert("test");
|
1783
|
//--><!]]]]><![CDATA[>
|
1784
|
|
1785
|
//--><!]]>
|
1786
|
</script></p>',
|
1787
|
format_string('HTML corrector -- Existing cdata section @pattern_name properly escaped', array('@pattern_name' => '<!--//--><![CDATA[// ><!--'))
|
1788
|
);
|
1789
|
|
1790
|
$filtered_data = _filter_htmlcorrector('<p><script type="text/javascript">
|
1791
|
// <![CDATA[
|
1792
|
alert("test");
|
1793
|
// ]]>
|
1794
|
</script></p>');
|
1795
|
$this->assertEqual($filtered_data, '<p><script type="text/javascript">
|
1796
|
<!--//--><![CDATA[// ><!--
|
1797
|
|
1798
|
// <![CDATA[
|
1799
|
alert("test");
|
1800
|
// ]]]]><![CDATA[>
|
1801
|
|
1802
|
//--><!]]>
|
1803
|
</script></p>',
|
1804
|
format_string('HTML corrector -- Existing cdata section @pattern_name properly escaped', array('@pattern_name' => '// <![CDATA['))
|
1805
|
);
|
1806
|
|
1807
|
}
|
1808
|
|
1809
|
/**
|
1810
|
* Asserts that a text transformed to lowercase with HTML entities decoded does contains a given string.
|
1811
|
*
|
1812
|
* Otherwise fails the test with a given message, similar to all the
|
1813
|
* SimpleTest assert* functions.
|
1814
|
*
|
1815
|
* Note that this does not remove nulls, new lines and other characters that
|
1816
|
* could be used to obscure a tag or an attribute name.
|
1817
|
*
|
1818
|
* @param $haystack
|
1819
|
* Text to look in.
|
1820
|
* @param $needle
|
1821
|
* Lowercase, plain text to look for.
|
1822
|
* @param $message
|
1823
|
* (optional) Message to display if failed. Defaults to an empty string.
|
1824
|
* @param $group
|
1825
|
* (optional) The group this message belongs to. Defaults to 'Other'.
|
1826
|
* @return
|
1827
|
* TRUE on pass, FALSE on fail.
|
1828
|
*/
|
1829
|
function assertNormalized($haystack, $needle, $message = '', $group = 'Other') {
|
1830
|
return $this->assertTrue(strpos(strtolower(decode_entities($haystack)), $needle) !== FALSE, $message, $group);
|
1831
|
}
|
1832
|
|
1833
|
/**
|
1834
|
* Asserts that text transformed to lowercase with HTML entities decoded does not contain a given string.
|
1835
|
*
|
1836
|
* Otherwise fails the test with a given message, similar to all the
|
1837
|
* SimpleTest assert* functions.
|
1838
|
*
|
1839
|
* Note that this does not remove nulls, new lines, and other character that
|
1840
|
* could be used to obscure a tag or an attribute name.
|
1841
|
*
|
1842
|
* @param $haystack
|
1843
|
* Text to look in.
|
1844
|
* @param $needle
|
1845
|
* Lowercase, plain text to look for.
|
1846
|
* @param $message
|
1847
|
* (optional) Message to display if failed. Defaults to an empty string.
|
1848
|
* @param $group
|
1849
|
* (optional) The group this message belongs to. Defaults to 'Other'.
|
1850
|
* @return
|
1851
|
* TRUE on pass, FALSE on fail.
|
1852
|
*/
|
1853
|
function assertNoNormalized($haystack, $needle, $message = '', $group = 'Other') {
|
1854
|
return $this->assertTrue(strpos(strtolower(decode_entities($haystack)), $needle) === FALSE, $message, $group);
|
1855
|
}
|
1856
|
}
|
1857
|
|
1858
|
/**
|
1859
|
* Tests for Filter's hook invocations.
|
1860
|
*/
|
1861
|
class FilterHooksTestCase extends DrupalWebTestCase {
|
1862
|
public static function getInfo() {
|
1863
|
return array(
|
1864
|
'name' => 'Filter format hooks',
|
1865
|
'description' => 'Test hooks for text formats insert/update/disable.',
|
1866
|
'group' => 'Filter',
|
1867
|
);
|
1868
|
}
|
1869
|
|
1870
|
function setUp() {
|
1871
|
parent::setUp('block', 'filter_test');
|
1872
|
$admin_user = $this->drupalCreateUser(array('administer filters', 'administer blocks'));
|
1873
|
$this->drupalLogin($admin_user);
|
1874
|
}
|
1875
|
|
1876
|
/**
|
1877
|
* Tests hooks on format management.
|
1878
|
*
|
1879
|
* Tests that hooks run correctly on creating, editing, and deleting a text
|
1880
|
* format.
|
1881
|
*/
|
1882
|
function testFilterHooks() {
|
1883
|
// Add a text format.
|
1884
|
$name = $this->randomName();
|
1885
|
$edit = array();
|
1886
|
$edit['format'] = drupal_strtolower($this->randomName());
|
1887
|
$edit['name'] = $name;
|
1888
|
$edit['roles[' . DRUPAL_ANONYMOUS_RID . ']'] = 1;
|
1889
|
$this->drupalPost('admin/config/content/formats/add', $edit, t('Save configuration'));
|
1890
|
$this->assertRaw(t('Added text format %format.', array('%format' => $name)), 'New format created.');
|
1891
|
$this->assertText('hook_filter_format_insert invoked.', 'hook_filter_format_insert was invoked.');
|
1892
|
|
1893
|
$format_id = $edit['format'];
|
1894
|
|
1895
|
// Update text format.
|
1896
|
$edit = array();
|
1897
|
$edit['roles[' . DRUPAL_AUTHENTICATED_RID . ']'] = 1;
|
1898
|
$this->drupalPost('admin/config/content/formats/' . $format_id, $edit, t('Save configuration'));
|
1899
|
$this->assertRaw(t('The text format %format has been updated.', array('%format' => $name)), 'Format successfully updated.');
|
1900
|
$this->assertText('hook_filter_format_update invoked.', 'hook_filter_format_update() was invoked.');
|
1901
|
|
1902
|
// Add a new custom block.
|
1903
|
$custom_block = array();
|
1904
|
$custom_block['info'] = $this->randomName(8);
|
1905
|
$custom_block['title'] = $this->randomName(8);
|
1906
|
$custom_block['body[value]'] = $this->randomName(32);
|
1907
|
// Use the format created.
|
1908
|
$custom_block['body[format]'] = $format_id;
|
1909
|
$this->drupalPost('admin/structure/block/add', $custom_block, t('Save block'));
|
1910
|
$this->assertText(t('The block has been created.'), 'New block successfully created.');
|
1911
|
|
1912
|
// Verify the new block is in the database.
|
1913
|
$bid = db_query("SELECT bid FROM {block_custom} WHERE info = :info", array(':info' => $custom_block['info']))->fetchField();
|
1914
|
$this->assertNotNull($bid, 'New block found in database');
|
1915
|
|
1916
|
// Disable the text format.
|
1917
|
$this->drupalPost('admin/config/content/formats/' . $format_id . '/disable', array(), t('Disable'));
|
1918
|
$this->assertRaw(t('Disabled text format %format.', array('%format' => $name)), 'Format successfully disabled.');
|
1919
|
$this->assertText('hook_filter_format_disable invoked.', 'hook_filter_format_disable() was invoked.');
|
1920
|
}
|
1921
|
}
|
1922
|
|
1923
|
/**
|
1924
|
* Tests filter settings.
|
1925
|
*/
|
1926
|
class FilterSettingsTestCase extends DrupalWebTestCase {
|
1927
|
/**
|
1928
|
* The installation profile to use with this test class.
|
1929
|
*
|
1930
|
* @var string
|
1931
|
*/
|
1932
|
protected $profile = 'testing';
|
1933
|
|
1934
|
public static function getInfo() {
|
1935
|
return array(
|
1936
|
'name' => 'Filter settings',
|
1937
|
'description' => 'Tests filter settings.',
|
1938
|
'group' => 'Filter',
|
1939
|
);
|
1940
|
}
|
1941
|
|
1942
|
/**
|
1943
|
* Tests explicit and implicit default settings for filters.
|
1944
|
*/
|
1945
|
function testFilterDefaults() {
|
1946
|
$filter_info = filter_filter_info();
|
1947
|
$filters = array_fill_keys(array_keys($filter_info), array());
|
1948
|
|
1949
|
// Create text format using filter default settings.
|
1950
|
$filter_defaults_format = (object) array(
|
1951
|
'format' => 'filter_defaults',
|
1952
|
'name' => 'Filter defaults',
|
1953
|
'filters' => $filters,
|
1954
|
);
|
1955
|
filter_format_save($filter_defaults_format);
|
1956
|
|
1957
|
// Verify that default weights defined in hook_filter_info() were applied.
|
1958
|
$saved_settings = array();
|
1959
|
foreach ($filter_defaults_format->filters as $name => $settings) {
|
1960
|
$expected_weight = (isset($filter_info[$name]['weight']) ? $filter_info[$name]['weight'] : 0);
|
1961
|
$this->assertEqual($settings['weight'], $expected_weight, format_string('@name filter weight %saved equals %default', array(
|
1962
|
'@name' => $name,
|
1963
|
'%saved' => $settings['weight'],
|
1964
|
'%default' => $expected_weight,
|
1965
|
)));
|
1966
|
$saved_settings[$name]['weight'] = $expected_weight;
|
1967
|
}
|
1968
|
|
1969
|
// Re-save the text format.
|
1970
|
filter_format_save($filter_defaults_format);
|
1971
|
// Reload it from scratch.
|
1972
|
filter_formats_reset();
|
1973
|
$filter_defaults_format = filter_format_load($filter_defaults_format->format);
|
1974
|
$filter_defaults_format->filters = filter_list_format($filter_defaults_format->format);
|
1975
|
|
1976
|
// Verify that saved filter settings have not been changed.
|
1977
|
foreach ($filter_defaults_format->filters as $name => $settings) {
|
1978
|
$this->assertEqual($settings->weight, $saved_settings[$name]['weight'], format_string('@name filter weight %saved equals %previous', array(
|
1979
|
'@name' => $name,
|
1980
|
'%saved' => $settings->weight,
|
1981
|
'%previous' => $saved_settings[$name]['weight'],
|
1982
|
)));
|
1983
|
}
|
1984
|
}
|
1985
|
}
|