. /** * Tests our HTMLPurifier hacks * * @package core * @subpackage lib * @copyright 2010 Petr Skoda {@link http://skodak.org} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ defined('MOODLE_INTERNAL') || die(); class htmlpurifier_test extends UnitTestCase { public static $includecoverage = array('lib/htmlpurifier/HTMLPurifier.php'); private $cachetext = null; function setUp() { global $CFG; $this->cachetext = $CFG->cachetext; $CFG->cachetext = 0; } function tearDown() { global $CFG; $CFG->cachetext = $this->cachetext; } /** * Verify _blank target is allowed * @return void */ public function test_allow_blank_target() { $text = 'Some link'; $result = format_text($text, FORMAT_HTML); $this->assertIdentical($text, $result); $result = format_text('Some link', FORMAT_HTML); $this->assertIdentical('Some link', $result); } /** * Verify our nolink tag accepted * @return void */ public function test_nolink() { // we can not use format text because nolink changes result $text = '
no filters
'; $result = purify_html($text, array()); $this->assertIdentical($text, $result); $text = 'xxxxx
xxx
'; $result = purify_html($text, array()); $this->assertIdentical($text, $result); } /** * Verify our tex tag accepted * @return void */ public function test_tex() { $text = 'a+b=c'; $result = purify_html($text, array()); $this->assertIdentical($text, $result); } /** * Verify our algebra tag accepted * @return void */ public function test_algebra() { $text = 'a+b=c'; $result = purify_html($text, array()); $this->assertIdentical($text, $result); } /** * Verify our hacky multilang works * @return void */ public function test_multilang() { $text = 'hmmmhm'; $result = purify_html($text, array()); $this->assertIdentical($text, $result); $text = 'hmmmhm'; $result = purify_html($text, array()); $this->assertIdentical($text, $result); $text = 'hmmm'; $result = purify_html($text, array()); $this->assertNotIdentical($text, $result); // keep standard lang tags $text = 'asas'; $result = purify_html($text, array()); $this->assertIdentical($text, $result); $text = 'xxxxxx'; $result = purify_html($text, array()); $this->assertIdentical($text, $result); } /** * Tests the 'allowid' option for format_text. */ public function test_format_text_allowid() { // Start off by not allowing ids (default) $options = array( 'nocache' => true ); $result = format_text('
Frog
', FORMAT_HTML, $options); $this->assertIdentical('
Frog
', $result); // Now allow ids $options['allowid'] = true; $result = format_text('
Frog
', FORMAT_HTML, $options); $this->assertIdentical('
Frog
', $result); } /** * Test if linebreaks kept unchanged. */ function test_line_breaking() { $text = "\n\raa\rsss\nsss\r"; $this->assertIdentical($text, purify_html($text)); } /** * Test fixing of strict problems. */ function test_tidy() { $text = "

xx"; $this->assertIdentical('

xx

', purify_html($text)); $text = "

xx

"; $this->assertIdentical('

xx

', purify_html($text)); $text = "xx
"; $this->assertIdentical('xx
', purify_html($text)); } /** * Test nesting - this used to cause problems in earlier versions */ function test_nested_lists() { $text = ""; $this->assertIdentical($text, purify_html($text)); } /** * Test that XSS protection works, complete smoke tests are in htmlpurifier itself. */ function test_cleaning_nastiness() { $text = "xx"; $this->assertIdentical('xx', purify_html($text)); $text = '
xx
'; $this->assertIdentical('
xx
', purify_html($text)); $text = '
xx
'; $this->assertIdentical('
xx
', purify_html($text)); $text = 'xx'; $this->assertIdentical('xx', purify_html($text)); $text = 'xx'; $this->assertIdentical('xx', purify_html($text)); $text = 'xx'; $this->assertIdentical('xx', purify_html($text)); $text = 'x
x'; $this->assertIdentical('xx', purify_html($text)); } }