芝麻web文件管理V1.00
编辑当前文件:/home2/sdektunc/www/cepali/filter/activitynames/tests/filter_test.php
. /** * Unit tests. * * @package filter_activitynames * @category test * @copyright 2018 The Open University * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ defined('MOODLE_INTERNAL') || die(); global $CFG; require_once($CFG->dirroot . '/filter/activitynames/filter.php'); // Include the code to test. /** * Test case for the activity names auto-linking filter. * * @copyright 2018 The Open University * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class filter_activitynames_filter_testcase extends advanced_testcase { public function test_links() { $this->resetAfterTest(true); // Create a test course. $course = $this->getDataGenerator()->create_course(); $context = context_course::instance($course->id); // Create two pages that will be linked to. $page1 = $this->getDataGenerator()->create_module('page', ['course' => $course->id, 'name' => 'Test 1']); $page2 = $this->getDataGenerator()->create_module('page', ['course' => $course->id, 'name' => 'Test (2)']); // Format text with all three entries in HTML. $html = '
Please read the two pages Test 1 and
Test (2)
.
'; $filtered = format_text($html, FORMAT_HTML, array('context' => $context)); // Find all the glossary links in the result. $matches = []; preg_match_all('~
([^<]*)
~', $filtered, $matches); // There should be 2 links links. $this->assertCount(2, $matches[1]); // Check text of title attribute. $this->assertEquals($page1->name, $matches[1][0]); $this->assertEquals($page2->name, $matches[1][1]); // Check the ids in the links. $this->assertEquals($page1->cmid, $matches[2][0]); $this->assertEquals($page2->cmid, $matches[2][1]); // Check the link text. $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]); } }