From ad7d0f05de6396356ef845ab43ec8ab4e32b3cee Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Wed, 12 Jun 2019 16:13:29 +0100 Subject: [PATCH] MDL-65884 filters: update exclusive separator characters for tags. The previous characters mangled the output of filtered text, exhibited in the activity names filter for activities named '-' (single hyphen). --- filter/activitynames/tests/filter_test.php | 34 ++++++++++++++++++++-- lib/filterlib.php | 2 +- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/filter/activitynames/tests/filter_test.php b/filter/activitynames/tests/filter_test.php index 403170efffe..b21c924c1b7 100644 --- a/filter/activitynames/tests/filter_test.php +++ b/filter/activitynames/tests/filter_test.php @@ -37,7 +37,6 @@ require_once($CFG->dirroot . '/filter/activitynames/filter.php'); // Include the class filter_activitynames_filter_testcase extends advanced_testcase { public function test_links() { - global $CFG; $this->resetAfterTest(true); // Create a test course. @@ -59,8 +58,8 @@ class filter_activitynames_filter_testcase extends advanced_testcase { preg_match_all('~([^<]*)~', $filtered, $matches); - // There should be 3 links links. - $this->assertEquals(2, count($matches[1])); + // There should be 2 links links. + $this->assertCount(2, $matches[1]); // Check text of title attribute. $this->assertEquals($page1->name, $matches[1][0]); @@ -74,4 +73,33 @@ class filter_activitynames_filter_testcase extends advanced_testcase { $this->assertEquals($page1->name, $matches[3][0]); $this->assertEquals($page2->name, $matches[3][1]); } + + public function test_links_activity_named_hyphen() { + $this->resetAfterTest(true); + + // Create a test course. + $course = $this->getDataGenerator()->create_course(); + $context = context_course::instance($course->id); + + // Work around an issue with the activity names filter which maintains a static cache + // of activities for current course ID. We can re-build the cache by switching user. + $this->setUser($this->getDataGenerator()->create_user()); + + // Create a page activity named '-' (single hyphen). + $page = $this->getDataGenerator()->create_module('page', ['course' => $course->id, 'name' => '-']); + + $html = '

Please read the - page.

'; + $filtered = format_text($html, FORMAT_HTML, array('context' => $context)); + + // Find the page link in the filtered html. + preg_match_all('~([^<]*)~', + $filtered, $matches); + + // We should have exactly one match. + $this->assertCount(1, $matches[1]); + + $this->assertEquals($page->name, $matches[1][0]); + $this->assertEquals($page->cmid, $matches[2][0]); + $this->assertEquals($page->name, $matches[3][0]); + } } diff --git a/lib/filterlib.php b/lib/filterlib.php index f210a257de5..a68f222fe30 100644 --- a/lib/filterlib.php +++ b/lib/filterlib.php @@ -38,7 +38,7 @@ define('TEXTFILTER_DISABLED', -9999); * keys. It must be something rare enough to avoid having matches with * filterobjects. MDL-18165 */ -define('TEXTFILTER_EXCL_SEPARATOR', '-%-'); +define('TEXTFILTER_EXCL_SEPARATOR', chr(0x1F) . '%' . chr(0x1F)); /**