summaryrefslogtreecommitdiffstats
path: root/core/modules/filter/lib/Drupal/filter/Tests/FilterAPITest.php
blob: f32d30b84dd57fdb75b68b4bd05939f7d2b4ccd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php

/**
 * @file
 * Contains \Drupal\filter\Tests\FilterAPITest.
 */

namespace Drupal\filter\Tests;

use Drupal\simpletest\DrupalUnitTestBase;

/**
 * Tests the behavior of Filter's API.
 */
class FilterAPITest extends DrupalUnitTestBase {

  public static $modules = array('system', 'filter', 'filter_test');

  public static function getInfo() {
    return array(
      'name' => 'API',
      'description' => 'Test the behavior of the API of the Filter module.',
      'group' => 'Filter',
    );
  }

  function setUp() {
    parent::setUp();

    $this->installConfig(array('system'));

    // Create Filtered HTML format.
    $filtered_html_format = entity_create('filter_format', array(
      'format' => 'filtered_html',
      'name' => 'Filtered HTML',
      'filters' => array(
        // Note that the filter_html filter is of the type FILTER_TYPE_MARKUP_LANGUAGE.
        'filter_url' => array(
          'weight' => -1,
          'status' => 1,
        ),
        // Note that the filter_html filter is of the type FILTER_TYPE_HTML_RESTRICTOR.
        'filter_html' => array(
          'status' => 1,
          'settings' => array(
            'allowed_html' => '<p> <br> <strong> <a>',
          ),
        ),
      )
    ));
    $filtered_html_format->save();

    // Create Full HTML format.
    $full_html_format = entity_create('filter_format', array(
      'format' => 'full_html',
      'name' => 'Full HTML',
      'weight' => 1,
      'filters' => array(),
    ));
    $full_html_format->save();
  }

  /**
   * Tests the ability to apply only a subset of filters.
   */
  function testCheckMarkup() {
    $text = "Text with <marquee>evil content and</marquee> a URL: http://drupal.org!";
    $expected_filtered_text = "Text with evil content and a URL: <a href=\"http://drupal.org\">http://drupal.org</a>!";
    $expected_filter_text_without_html_generators = "Text with evil content and a URL: http://drupal.org!";

    $this->assertIdentical(
      check_markup($text, 'filtered_html', '', FALSE, array()),
      $expected_filtered_text,
      'Expected filter result.'
    );
    $this->assertIdentical(
      check_markup($text, 'filtered_html', '', FALSE, array(FILTER_TYPE_MARKUP_LANGUAGE)),
      $expected_filter_text_without_html_generators,
      'Expected filter result when skipping FILTER_TYPE_MARKUP_LANGUAGE filters.'
    );
    // Related to @see FilterSecurityTest.php/testSkipSecurityFilters(), but
    // this check focuses on the ability to filter multiple filter types at once.
    // Drupal core only ships with these two types of filters, so this is the
    // most extensive test possible.
    $this->assertIdentical(
      check_markup($text, 'filtered_html', '', FALSE, array(FILTER_TYPE_HTML_RESTRICTOR, FILTER_TYPE_MARKUP_LANGUAGE)),
      $expected_filter_text_without_html_generators,
      'Expected filter result when skipping FILTER_TYPE_MARKUP_LANGUAGE filters, even when trying to disable filters of the FILTER_TYPE_HTML_RESTRICTOR type.'
    );
  }

  /**
   * Tests the following functions for a variety of formats:
   *   - filter_get_html_restrictions_by_format()
   *   - filter_get_filter_types_by_format()
   */
  function testFilterFormatAPI() {
    // Test on filtered_html.
    $this->assertIdentical(
      filter_get_html_restrictions_by_format('filtered_html'),
      array('allowed' => array('p' => TRUE, 'br' => TRUE, 'strong' => TRUE, 'a' => TRUE, '*' => array('style' => FALSE, 'on*' => FALSE))),
      'filter_get_html_restrictions_by_format() works as expected for the filtered_html format.'
    );
    $this->assertIdentical(
      filter_get_filter_types_by_format('filtered_html'),
      array(FILTER_TYPE_HTML_RESTRICTOR, FILTER_TYPE_MARKUP_LANGUAGE),
      'filter_get_filter_types_by_format() works as expected for the filtered_html format.'
    );

    // Test on full_html.
    $this->assertIdentical(
      filter_get_html_restrictions_by_format('full_html'),
      FALSE, // Every tag is allowed.
      'filter_get_html_restrictions_by_format() works as expected for the full_html format.'
    );
    $this->assertIdentical(
      filter_get_filter_types_by_format('full_html'),
      array(),
      'filter_get_filter_types_by_format() works as expected for the full_html format.'
    );

    // Test on stupid_filtered_html, where nothing is allowed.
    $stupid_filtered_html_format = entity_create('filter_format', array(
      'format' => 'stupid_filtered_html',
      'name' => 'Stupid Filtered HTML',
      'filters' => array(
        'filter_html' => array(
          'status' => 1,
          'settings' => array(
            'allowed_html' => '', // Nothing is allowed.
          ),
        ),
      ),
    ));
    $stupid_filtered_html_format->save();
    $this->assertIdentical(
      filter_get_html_restrictions_by_format('stupid_filtered_html'),
      array('allowed' => array()), // No tag is allowed.
      'filter_get_html_restrictions_by_format() works as expected for the stupid_filtered_html format.'
    );
    $this->assertIdentical(
      filter_get_filter_types_by_format('stupid_filtered_html'),
      array(FILTER_TYPE_HTML_RESTRICTOR),
      'filter_get_filter_types_by_format() works as expected for the stupid_filtered_html format.'
    );

    // Test on very_restricted_html, where there's two different filters of the
    // FILTER_TYPE_HTML_RESTRICTOR type, each restricting in different ways.
    $very_restricted_html = entity_create('filter_format', array(
      'format' => 'very_restricted_html',
      'name' => 'Very Restricted HTML',
      'filters' => array(
        'filter_html' => array(
          'status' => 1,
          'settings' => array(
            'allowed_html' => '<p> <br> <a> <strong>',
          ),
        ),
        'filter_test_restrict_tags_and_attributes' => array(
          'status' => 1,
          'settings' => array(
            'restrictions' => array(
              'allowed' => array(
                'p' => TRUE,
                'br' => FALSE,
                'a' => array('href' => TRUE),
                'em' => TRUE,
              ),
            )
          ),
        ),
      )
    ));
    $very_restricted_html->save();
    $this->assertIdentical(
      filter_get_html_restrictions_by_format('very_restricted_html'),
      array('allowed' => array('p' => TRUE, 'br' => FALSE, 'a' => array('href' => TRUE), '*' => array('style' => FALSE, 'on*' => FALSE))),
      'filter_get_html_restrictions_by_format() works as expected for the very_restricted_html format.'
    );
    $this->assertIdentical(
      filter_get_filter_types_by_format('very_restricted_html'),
      array(FILTER_TYPE_HTML_RESTRICTOR),
      'filter_get_filter_types_by_format() works as expected for the very_restricted_html format.'
    );
  }

}