2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
20 * @package filter_activitynames
22 * @copyright 2018 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->dirroot . '/filter/activitynames/filter.php'); // Include the code to test.
32 * Test case for the activity names auto-linking filter.
34 * @copyright 2018 The Open University
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class filter_activitynames_filter_testcase extends advanced_testcase {
39 public function test_links() {
40 $this->resetAfterTest(true);
42 // Create a test course.
43 $course = $this->getDataGenerator()->create_course();
44 $context = context_course::instance($course->id);
46 // Create two pages that will be linked to.
47 $page1 = $this->getDataGenerator()->create_module('page',
48 ['course' => $course->id, 'name' => 'Test 1']);
49 $page2 = $this->getDataGenerator()->create_module('page',
50 ['course' => $course->id, 'name' => 'Test (2)']);
52 // Format text with all three entries in HTML.
53 $html = '<p>Please read the two pages Test 1 and <i>Test (2)</i>.</p>';
54 $filtered = format_text($html, FORMAT_HTML, array('context' => $context));
56 // Find all the glossary links in the result.
58 preg_match_all('~<a class="autolink" title="([^"]*)" href="[^"]*/mod/page/view.php\?id=([0-9]+)">([^<]*)</a>~',
61 // There should be 2 links links.
62 $this->assertCount(2, $matches[1]);
64 // Check text of title attribute.
65 $this->assertEquals($page1->name, $matches[1][0]);
66 $this->assertEquals($page2->name, $matches[1][1]);
68 // Check the ids in the links.
69 $this->assertEquals($page1->cmid, $matches[2][0]);
70 $this->assertEquals($page2->cmid, $matches[2][1]);
72 // Check the link text.
73 $this->assertEquals($page1->name, $matches[3][0]);
74 $this->assertEquals($page2->name, $matches[3][1]);
77 public function test_links_activity_named_hyphen() {
78 $this->resetAfterTest(true);
80 // Create a test course.
81 $course = $this->getDataGenerator()->create_course();
82 $context = context_course::instance($course->id);
84 // Work around an issue with the activity names filter which maintains a static cache
85 // of activities for current course ID. We can re-build the cache by switching user.
86 $this->setUser($this->getDataGenerator()->create_user());
88 // Create a page activity named '-' (single hyphen).
89 $page = $this->getDataGenerator()->create_module('page', ['course' => $course->id, 'name' => '-']);
91 $html = '<p>Please read the - page.</p>';
92 $filtered = format_text($html, FORMAT_HTML, array('context' => $context));
94 // Find the page link in the filtered html.
95 preg_match_all('~<a class="autolink" title="([^"]*)" href="[^"]*/mod/page/view.php\?id=([0-9]+)">([^<]*)</a>~',
98 // We should have exactly one match.
99 $this->assertCount(1, $matches[1]);
101 $this->assertEquals($page->name, $matches[1][0]);
102 $this->assertEquals($page->cmid, $matches[2][0]);
103 $this->assertEquals($page->name, $matches[3][0]);