MDL-67271 core: Add test to find missing SVG icons
[moodle.git] / lib / tests / html2text_test.php
blobaf027171ecc8d12181ad478b7637ad3a21a0123a
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 namespace core;
19 /**
20 * Tests our html2text hacks
22 * Note: includes original tests from testweblib.php
24 * @package core
25 * @category test
26 * @copyright 2012 Petr Skoda {@link http://skodak.org}
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 class html2text_test extends \basic_testcase {
31 /**
32 * ALT as image replacements.
34 public function test_images() {
35 $this->assertSame('[edit]', html_to_text('<img src="edit.png" alt="edit" />'));
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');
42 /**
43 * No magic quotes messing.
45 public function test_no_strip_slashes() {
46 $this->assertSame('[\edit]', html_to_text('<img src="edit.png" alt="\edit" />'));
48 $text = '\\magic\\quotes\\are\\\\horrible';
49 $result = html_to_text($text, null, false, false);
50 $this->assertSame($result, $text);
53 /**
54 * core_text integration.
56 public function test_core_text() {
57 $text = '<strong>Žluťoučký koníček</strong>';
58 $result = html_to_text($text, null, false, false);
59 $this->assertSame($result, 'ŽLUŤOUČKÝ KONÍČEK');
62 /**
63 * Protect 0.
65 public function test_zero() {
66 $text = '0';
67 $result = html_to_text($text, null, false, false);
68 $this->assertSame($result, $text);
70 $this->assertSame('0', html_to_text('0'));
73 /**
74 * Test the links list enumeration.
76 public function test_build_link_list() {
78 // Note the trailing whitespace left intentionally in the text after first link.
79 $text = 'Total of <a title="List of integrated issues"
80 href="http://tr.mdl.org/sh.jspa?r=1&j=p+%3D+%22I+d%22+%3D"> ' . '
81 <strong>27 issues</strong></a> and <a href="http://another.url/?f=a&amp;b=2">some</a> other
82 have been fixed <strong><a href="http://third.url/view.php">last week</a></strong>';
84 // Do not collect links.
85 $result = html_to_text($text, 5000, false);
86 $this->assertSame('Total of 27 ISSUES and some other have been fixed LAST WEEK', $result);
88 // Collect and enumerate links.
89 $result = html_to_text($text, 5000, true);
90 $this->assertSame(0, strpos($result, 'Total of 27 ISSUES [1] and some [2] other have been fixed LAST WEEK [3]'));
91 $this->assertSame(false, strpos($result, '[0]'));
92 $this->assertSame(1, preg_match('|^'.preg_quote('[1] http://tr.mdl.org/sh.jspa?r=1&j=p+%3D+%22I+d%22+%3D').'$|m', $result));
93 $this->assertSame(1, preg_match('|^'.preg_quote('[2] http://another.url/?f=a&amp;b=2').'$|m', $result));
94 $this->assertSame(1, preg_match('|^'.preg_quote('[3] http://third.url/view.php').'$|m', $result));
95 $this->assertSame(false, strpos($result, '[4]'));
97 // Test multiple occurrences of the same URL.
98 $text = '<p>See <a href="http://moodle.org">moodle.org</a>,
99 <a href="http://www.google.fr">google</a>, <a href="http://www.univ-lemans.fr">univ-lemans</a>
100 and <a href="http://www.google.fr">google</a>.
101 Also try <a href="https://www.google.fr">google via HTTPS</a>.';
102 $result = html_to_text($text, 5000, true);
103 $this->assertSame(0, strpos($result, 'See moodle.org [1], google [2], univ-lemans [3] and google [2]. Also try google via HTTPS [4].'));
104 $this->assertSame(false, strpos($result, '[0]'));
105 $this->assertSame(1, preg_match('|^'.preg_quote('[1] http://moodle.org').'$|m', $result));
106 $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));
108 $this->assertSame(1, preg_match('|^'.preg_quote('[4] https://www.google.fr').'$|m', $result));
109 $this->assertSame(false, strpos($result, '[5]'));
113 * Various invalid HTML typed by users that ignore html strict.
115 public function test_invalid_html() {
116 $text = 'Gin & Tonic';
117 $result = html_to_text($text, null, false, false);
118 $this->assertSame($result, $text);
120 $text = 'Gin > Tonic';
121 $result = html_to_text($text, null, false, false);
122 $this->assertSame($result, $text);
124 $text = 'Gin < Tonic';
125 $result = html_to_text($text, null, false, false);
126 $this->assertSame($result, $text);
130 * Basic text formatting.
132 public function test_simple() {
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!'));
140 * Test line wrapping.
142 public function test_text_nowrap() {
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));
150 * Whitespace removal.
152 public function test_trailing_whitespace() {
153 $this->assertSame('With trailing whitespace and some more text', html_to_text("With trailing whitespace \nand some more text", 0));
157 * PRE parsing.
159 public function test_html_to_text_pre_parsing_problem() {
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 />';
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:
167 void FillMeUp(char* in_string) {
168   int i = 0;
169   while (in_string[i] != \'\0\') {
170     in_string[i] = \'X\';
171     i++;
172   }
174 What would happen if a non-terminated string were input to this function?
178 $this->assertSame($strconv, html_to_text($strorig));
182 * Scripts should be stripped.
184 public function test_strip_scripts() {
185 $this->assertSame('Interesting text',
186 html_to_text('Interesting <script type="text/javascript">var what_a_mess = "Yuck!";</script> text', 0));