Proyectos de Subversion Moodle

Rev

Rev 11 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 11 Rev 1441
Línea 23... Línea 23...
23
 *
23
 *
24
 * @package    core
24
 * @package    core
25
 * @category   test
25
 * @category   test
26
 * @copyright  2012 Petr Skoda {@link http://skodak.org}
26
 * @copyright  2012 Petr Skoda {@link http://skodak.org}
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
28
 * @covers ::html_to_text
28
 */
29
 */
29
class html2text_test extends \basic_testcase {
30
final class html2text_test extends \basic_testcase {
30
 
-
 
31
    /**
31
    /**
32
     * ALT as image replacements.
32
     * Data provider for general tests.
33
     */
33
     *
34
    public function test_images(): void {
-
 
35
        $this->assertSame('[edit]', html_to_text('<img src="edit.png" alt="edit" />'));
-
 
36
 
-
 
37
        $text = 'xx<img src="gif.gif" alt="some gif" />xx';
-
 
38
        $result = html_to_text($text, null, false, false);
-
 
39
        $this->assertSame($result, 'xx[some gif]xx');
-
 
40
    }
-
 
41
 
-
 
42
    /**
-
 
43
     * No magic quotes messing.
34
     * @return array
44
     */
35
     */
45
    public function test_no_strip_slashes(): void {
36
    public static function examples_provider(): array {
46
        $this->assertSame('[\edit]', html_to_text('<img src="edit.png" alt="\edit" />'));
37
        // Used in the line wrapping tests.
47
 
-
 
48
        $text = '\\magic\\quotes\\are\\\\horrible';
38
        // phpcs:ignore Generic.Files.LineLength.TooLong
49
        $result = html_to_text($text, null, false, false);
39
        $long = "Here is a long string, more than 75 characters long, since by default html_to_text wraps text at 75 chars.";
50
        $this->assertSame($result, $text);
40
        // phpcs:ignore Generic.Files.LineLength.TooLong
51
    }
-
 
-
 
41
        $wrapped = "Here is a long string, more than 75 characters long, since by default\nhtml_to_text wraps text at 75 chars.";
Línea 52... Línea -...
52
 
-
 
53
    /**
42
 
54
     * core_text integration.
-
 
55
     */
43
        // These two are used in the PRE parsing tests.
56
    public function test_core_text(): void {
44
        // phpcs:ignore Generic.Files.LineLength.TooLong
57
        $text = '<strong>Žluťoučký koníček</strong>';
45
        $strorig = 'Consider the following function:<br /><pre><span style="color: rgb(153, 51, 102);">void FillMeUp(char* in_string) {'.
58
        $result = html_to_text($text, null, false, false);
46
            '<br />  int i = 0;<br />  while (in_string[i] != \'\0\') {<br />    in_string[i] = \'X\';<br />    i++;<br />  }<br />'.
59
        $this->assertSame($result, 'ŽLUŤOUČKÝ KONÍČEK');
-
 
Línea 60... Línea -...
60
    }
-
 
61
 
-
 
62
    /**
-
 
63
     * Protect 0.
47
            '}</span></pre>What would happen if a non-terminated string were input to this function?<br /><br />';
64
     */
48
 
65
    public function test_zero(): void {
-
 
66
        $text = '0';
49
        // Note, the spaces in the <pre> section are Unicode NBSPs - they may not be displayed in your editor.
Línea -... Línea 50...
-
 
50
        $strconv = <<<EOF
-
 
51
        Consider the following function:
-
 
52
 
-
 
53
        void FillMeUp(char* in_string) {
-
 
54
          int i = 0;
-
 
55
          while (in_string[i] != '\\0') {
-
 
56
            in_string[i] = 'X';
-
 
57
            i++;
-
 
58
          }
-
 
59
        }
-
 
60
        What would happen if a non-terminated string were input to this function?
-
 
61
 
-
 
62
 
-
 
63
        EOF;
-
 
64
 
-
 
65
        return [
-
 
66
            // Image alt tag replacements.
-
 
67
            'Image alt tag' => [
-
 
68
                '[edit]',
-
 
69
                [],
-
 
70
                '<img src="edit.png" alt="edit" />',
-
 
71
            ],
-
 
72
            'Image alt tag between strings' => [
-
 
73
                'xx[some gif]xx',
-
 
74
                [
-
 
75
                    'dolinks' => false,
-
 
76
                ],
-
 
77
                'xx<img src="gif.gif" alt="some gif" />xx',
-
 
78
            ],
-
 
79
            'core_text integration' => [
-
 
80
                'ŽLUŤOUČKÝ KONÍČEK',
-
 
81
                ['dolinks' => false],
-
 
82
                '<strong>Žluťoučký koníček</strong>',
-
 
83
            ],
-
 
84
            'No strip slashes in a tag' => [
-
 
85
                '[\edit]',
-
 
86
                [],
-
 
87
                '<img src="edit.png" alt="\edit" />',
-
 
88
            ],
-
 
89
            'No strip slashes in a string' => [
-
 
90
                '\\magic\\quotes\\are\\\\horrible',
-
 
91
                [],
-
 
92
                '\\magic\\quotes\\are\\\\horrible',
-
 
93
            ],
-
 
94
            'Protect "0"' => [
-
 
95
                '0',
-
 
96
                ['dolinks' => false],
-
 
97
                '0',
-
 
98
            ],
-
 
99
            'Invalid HTML 1' => [
-
 
100
                'Gin & Tonic',
-
 
101
                [],
-
 
102
                'Gin & Tonic',
-
 
103
            ],
-
 
104
            'Invalid HTML 2' => [
-
 
105
                'Gin > Tonic',
-
 
106
                [],
-
 
107
                'Gin > Tonic',
-
 
108
            ],
-
 
109
            'Invalid HTML 3' => [
-
 
110
                'Gin < Tonic',
-
 
111
                [],
-
 
112
                'Gin < Tonic',
-
 
113
            ],
-
 
114
            'Simple test 1' => [
-
 
115
                "_Hello_ WORLD!\n",
-
 
116
                [],
-
 
117
                '<p><i>Hello</i> <b>world</b>!</p>',
-
 
118
            ],
-
 
119
            'Simple test 2' => [
-
 
120
                "All the WORLD’S a stage.\n\n-- William Shakespeare\n",
-
 
121
                [],
-
 
122
                '<p>All the <strong>world’s</strong> a stage.</p><p>-- William Shakespeare</p>',
-
 
123
            ],
-
 
124
            'Simple test 3' => [
-
 
125
                "HELLO WORLD!\n\n",
-
 
126
                [],
-
 
127
                '<h1>Hello world!</h1>',
-
 
128
            ],
-
 
129
            'Simple test 4' => [
-
 
130
                "Hello\nworld!",
-
 
131
                [],
-
 
132
                'Hello<br />world!',
-
 
133
            ],
-
 
134
            'No wrapping when width set to 0' => [
-
 
135
                $long,
-
 
136
                ['width' => 0],
-
 
137
                $long,
-
 
138
            ],
-
 
139
            'Wrapping when width set to default' => [
-
 
140
                $wrapped,
-
 
141
                [],
-
 
142
                $long,
-
 
143
            ],
-
 
144
            'Trailing whitespace removal' => [
-
 
145
                'With trailing whitespace and some more text',
-
 
146
                [],
-
 
147
                "With trailing whitespace   \nand some   more text",
-
 
148
            ],
-
 
149
            'PRE parsing' => [
-
 
150
                $strconv,
-
 
151
                [],
-
 
152
                $strorig,
-
 
153
            ],
-
 
154
            'Strip script tags' => [
-
 
155
                'Interesting text',
-
 
156
                [],
-
 
157
                'Interesting <script type="text/javascript">var what_a_mess = "Yuck!";</script> text',
-
 
158
            ],
-
 
159
            'Trailing spaces before newline or tab' => [
-
 
160
                "Some text with trailing space\n\nAnd some more text\n",
-
 
161
                [],
-
 
162
                '<p>Some text with trailing space </p> <p>And some more text</p>',
-
 
163
            ],
-
 
164
            'Trailing spaces before newline or tab (list)' => [
-
 
165
                "\t* Some text with trailing space\n\t* And some more text\n\n",
-
 
166
                [],
-
 
167
                '<ul><li>Some text with trailing space </li> <li> And some more text </li> </ul>',
-
 
168
            ],
-
 
169
        ];
-
 
170
    }
-
 
171
 
-
 
172
    /**
-
 
173
     * Test html2text with various examples.
-
 
174
     *
-
 
175
     * @dataProvider examples_provider
-
 
176
     * @param string $expected
-
 
177
     * @param array $options
-
 
178
     * @param string $html
-
 
179
     */
-
 
180
    public function test_runner(
-
 
181
        string $expected,
67
        $result = html_to_text($text, null, false, false);
182
        array $options,
68
        $this->assertSame($result, $text);
183
        string $html,
Línea 69... Línea 184...
69
 
184
    ): void {
70
        $this->assertSame('0', html_to_text('0'));
185
        $this->assertSame($expected, html_to_text($html, ...$options));
71
    }
186
    }
Línea 106... Línea 221...
106
        $this->assertSame(1, preg_match('|^'.preg_quote('[2] http://www.google.fr').'$|m', $result));
221
        $this->assertSame(1, preg_match('|^'.preg_quote('[2] http://www.google.fr').'$|m', $result));
107
        $this->assertSame(1, preg_match('|^'.preg_quote('[3] http://www.univ-lemans.fr').'$|m', $result));
222
        $this->assertSame(1, preg_match('|^'.preg_quote('[3] http://www.univ-lemans.fr').'$|m', $result));
108
        $this->assertSame(1, preg_match('|^'.preg_quote('[4] https://www.google.fr').'$|m', $result));
223
        $this->assertSame(1, preg_match('|^'.preg_quote('[4] https://www.google.fr').'$|m', $result));
109
        $this->assertSame(false, strpos($result, '[5]'));
224
        $this->assertSame(false, strpos($result, '[5]'));
110
    }
225
    }
111
 
-
 
112
    /**
-
 
113
     * Various invalid HTML typed by users that ignore html strict.
-
 
114
     **/
-
 
115
    public function test_invalid_html(): void {
-
 
116
        $text = 'Gin & Tonic';
-
 
117
        $result = html_to_text($text, null, false, false);
-
 
118
        $this->assertSame($result, $text);
-
 
119
 
-
 
120
        $text = 'Gin > Tonic';
-
 
121
        $result = html_to_text($text, null, false, false);
-
 
122
        $this->assertSame($result, $text);
-
 
123
 
-
 
124
        $text = 'Gin < Tonic';
-
 
125
        $result = html_to_text($text, null, false, false);
-
 
126
        $this->assertSame($result, $text);
-
 
127
    }
-
 
128
 
-
 
129
    /**
-
 
130
     * Basic text formatting.
-
 
131
     */
-
 
132
    public function test_simple(): void {
-
 
133
        $this->assertSame("_Hello_ WORLD!\n", html_to_text('<p><i>Hello</i> <b>world</b>!</p>'));
-
 
134
        $this->assertSame("All the WORLD’S a stage.\n\n-- William Shakespeare\n", html_to_text('<p>All the <strong>world’s</strong> a stage.</p><p>-- William Shakespeare</p>'));
-
 
135
        $this->assertSame("HELLO WORLD!\n\n", html_to_text('<h1>Hello world!</h1>'));
-
 
136
        $this->assertSame("Hello\nworld!", html_to_text('Hello<br />world!'));
-
 
137
    }
-
 
138
 
-
 
139
    /**
-
 
140
     * Test line wrapping.
-
 
141
     */
-
 
142
    public function test_text_nowrap(): void {
-
 
143
        $long = "Here is a long string, more than 75 characters long, since by default html_to_text wraps text at 75 chars.";
-
 
144
        $wrapped = "Here is a long string, more than 75 characters long, since by default\nhtml_to_text wraps text at 75 chars.";
-
 
145
        $this->assertSame($long, html_to_text($long, 0));
-
 
146
        $this->assertSame($wrapped, html_to_text($long));
-
 
147
    }
-
 
148
 
-
 
149
    /**
-
 
150
     * Whitespace removal.
-
 
151
     */
-
 
152
    public function test_trailing_whitespace(): void {
-
 
153
        $this->assertSame('With trailing whitespace and some more text', html_to_text("With trailing whitespace   \nand some   more text", 0));
-
 
154
    }
-
 
155
 
-
 
156
    /**
-
 
157
     * PRE parsing.
-
 
158
     */
-
 
159
    public function test_html_to_text_pre_parsing_problem(): void {
-
 
160
        $strorig = 'Consider the following function:<br /><pre><span style="color: rgb(153, 51, 102);">void FillMeUp(char* in_string) {'.
-
 
161
            '<br />  int i = 0;<br />  while (in_string[i] != \'\0\') {<br />    in_string[i] = \'X\';<br />    i++;<br />  }<br />'.
-
 
162
            '}</span></pre>What would happen if a non-terminated string were input to this function?<br /><br />';
-
 
163
 
-
 
164
        // Note, the spaces in the <pre> section are Unicode NBSPs - they may not be displayed in your editor.
-
 
165
        $strconv = 'Consider the following function:
-
 
166
 
-
 
167
void FillMeUp(char* in_string) {
-
 
168
  int i = 0;
-
 
169
  while (in_string[i] != \'\0\') {
-
 
170
    in_string[i] = \'X\';
-
 
171
    i++;
-
 
172
  }
-
 
173
}
-
 
174
What would happen if a non-terminated string were input to this function?
-
 
175
 
-
 
176
';
-
 
177
 
-
 
178
        $this->assertSame($strconv, html_to_text($strorig));
-
 
179
    }
-
 
180
 
-
 
181
    /**
-
 
182
     * Scripts should be stripped.
-
 
183
     */
-
 
184
    public function test_strip_scripts(): void {
-
 
185
        $this->assertSame('Interesting text',
-
 
186
                html_to_text('Interesting <script type="text/javascript">var what_a_mess = "Yuck!";</script> text', 0));
-
 
187
    }
-
 
188
}
226
}