MDL-28666 Files API and db changes
[moodle.git] / lib / tests / html2text_test.php
blobc4f54ff21f85efff504026784a94589f0f0bb27c
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 /**
18 * Tests our html2text hacks
20 * Note: includes original tests from testweblib.php
22 * @package core
23 * @category phpunit
24 * @copyright 2012 Petr Skoda {@link http://skodak.org}
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 defined('MOODLE_INTERNAL') || die();
32 class html2text_testcase extends basic_testcase {
34 /**
35 * ALT as image replacements
37 public function test_images() {
38 $this->assertEquals('[edit]', html_to_text('<img src="edit.png" alt="edit" />'));
40 $text = 'xx<img src="gif.gif" alt="some gif" />xx';
41 $result = html_to_text($text, null, false, false);
42 $this->assertSame($result, 'xx[some gif]xx');
45 /**
46 * No magic quotes messing
48 public function test_no_strip_slashes() {
49 $this->assertEquals('[\edit]', html_to_text('<img src="edit.png" alt="\edit" />'));
51 $text = '\\magic\\quotes\\are\\\\horrible';
52 $result = html_to_text($text, null, false, false);
53 $this->assertSame($result, $text);
56 /**
57 * Textlib integration
59 public function test_textlib() {
60 $text = '<strong>Žluťoučký koníček</strong>';
61 $result = html_to_text($text, null, false, false);
62 $this->assertSame($result, 'ŽLUŤOUČKÝ KONÍČEK');
65 /**
66 * Protect 0
68 public function test_zero() {
69 $text = '0';
70 $result = html_to_text($text, null, false, false);
71 $this->assertSame($result, $text);
73 $this->assertSame('0', html_to_text('0'));
76 // ======= Standard html2text conversion features =======
78 /**
79 * Various invalid HTML typed by users that ignore html strict
80 **/
81 public function test_invalid_html() {
82 $text = 'Gin & Tonic';
83 $result = html_to_text($text, null, false, false);
84 $this->assertSame($result, $text);
86 $text = 'Gin > Tonic';
87 $result = html_to_text($text, null, false, false);
88 $this->assertSame($result, $text);
90 $text = 'Gin < Tonic';
91 $result = html_to_text($text, null, false, false);
92 $this->assertSame($result, $text);
95 /**
96 * Basic text formatting.
98 public function test_simple() {
99 $this->assertEquals("_Hello_ WORLD!", html_to_text('<p><i>Hello</i> <b>world</b>!</p>'));
100 $this->assertEquals("All the WORLD’S a stage.\n\n-- William Shakespeare", html_to_text('<p>All the <strong>world’s</strong> a stage.</p><p>-- William Shakespeare</p>'));
101 $this->assertEquals("HELLO WORLD!\n\n", html_to_text('<h1>Hello world!</h1>'));
102 $this->assertEquals("Hello\nworld!", html_to_text('Hello<br />world!'));
106 * Test line wrapping
108 public function test_text_nowrap() {
109 $long = "Here is a long string, more than 75 characters long, since by default html_to_text wraps text at 75 chars.";
110 $wrapped = "Here is a long string, more than 75 characters long, since by default\nhtml_to_text wraps text at 75 chars.";
111 $this->assertEquals($long, html_to_text($long, 0));
112 $this->assertEquals($wrapped, html_to_text($long));
116 * Whitespace removal
118 public function test_trailing_whitespace() {
119 $this->assertEquals('With trailing whitespace and some more text', html_to_text("With trailing whitespace \nand some more text", 0));
123 * PRE parsing
125 public function test_html_to_text_pre_parsing_problem() {
126 $strorig = 'Consider the following function:<br /><pre><span style="color: rgb(153, 51, 102);">void FillMeUp(char* in_string) {'.
127 '<br /> int i = 0;<br /> while (in_string[i] != \'\0\') {<br /> in_string[i] = \'X\';<br /> i++;<br /> }<br />'.
128 '}</span></pre>What would happen if a non-terminated string were input to this function?<br /><br />';
130 $strconv = 'Consider the following function:
132 void FillMeUp(char* in_string) {
133 int i = 0;
134 while (in_string[i] != \'\0\') {
135 in_string[i] = \'X\';
136 i++;
139 What would happen if a non-terminated string were input to this function?
143 $this->assertSame($strconv, html_to_text($strorig));