See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]
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. 13 // 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/>. 16 17 /** 18 * Unit tests. 19 * 20 * @package filter_activitynames 21 * @category test 22 * @copyright 2018 The Open University 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 global $CFG; 29 require_once($CFG->dirroot . '/filter/activitynames/filter.php'); // Include the code to test. 30 31 /** 32 * Test case for the activity names auto-linking filter. 33 * 34 * @copyright 2018 The Open University 35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 36 */ 37 class filter_activitynames_filter_testcase extends advanced_testcase { 38 39 public function test_links() { 40 $this->resetAfterTest(true); 41 42 // Create a test course. 43 $course = $this->getDataGenerator()->create_course(); 44 $context = context_course::instance($course->id); 45 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)']); 51 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)); 55 56 // Find all the glossary links in the result. 57 $matches = []; 58 preg_match_all('~<a class="autolink" title="([^"]*)" href="[^"]*/mod/page/view.php\?id=([0-9]+)">([^<]*)</a>~', 59 $filtered, $matches); 60 61 // There should be 2 links links. 62 $this->assertCount(2, $matches[1]); 63 64 // Check text of title attribute. 65 $this->assertEquals($page1->name, $matches[1][0]); 66 $this->assertEquals($page2->name, $matches[1][1]); 67 68 // Check the ids in the links. 69 $this->assertEquals($page1->cmid, $matches[2][0]); 70 $this->assertEquals($page2->cmid, $matches[2][1]); 71 72 // Check the link text. 73 $this->assertEquals($page1->name, $matches[3][0]); 74 $this->assertEquals($page2->name, $matches[3][1]); 75 } 76 77 public function test_links_activity_named_hyphen() { 78 $this->resetAfterTest(true); 79 80 // Create a test course. 81 $course = $this->getDataGenerator()->create_course(); 82 $context = context_course::instance($course->id); 83 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()); 87 88 // Create a page activity named '-' (single hyphen). 89 $page = $this->getDataGenerator()->create_module('page', ['course' => $course->id, 'name' => '-']); 90 91 $html = '<p>Please read the - page.</p>'; 92 $filtered = format_text($html, FORMAT_HTML, array('context' => $context)); 93 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>~', 96 $filtered, $matches); 97 98 // We should have exactly one match. 99 $this->assertCount(1, $matches[1]); 100 101 $this->assertEquals($page->name, $matches[1][0]); 102 $this->assertEquals($page->cmid, $matches[2][0]); 103 $this->assertEquals($page->name, $matches[3][0]); 104 } 105 106 public function test_cache() { 107 $this->resetAfterTest(true); 108 109 // Create a test courses. 110 $course1 = $this->getDataGenerator()->create_course(); 111 $course2 = $this->getDataGenerator()->create_course(); 112 $context1 = context_course::instance($course1->id); 113 $context2 = context_course::instance($course2->id); 114 115 // Create page 1. 116 $page1 = $this->getDataGenerator()->create_module('page', 117 ['course' => $course1->id, 'name' => 'Test 1']); 118 // Format text with page 1 in HTML. 119 $html = '<p>Please read the two pages Test 1 and Test 2.</p>'; 120 $filtered1 = format_text($html, FORMAT_HTML, array('context' => $context1)); 121 // Find all the activity links in the result. 122 $matches = []; 123 preg_match_all('~<a class="autolink" title="([^"]*)" href="[^"]*/mod/page/view.php\?id=([0-9]+)">([^<]*)</a>~', 124 $filtered1, $matches); 125 // There should be 1 link. 126 $this->assertCount(1, $matches[1]); 127 $this->assertEquals($page1->name, $matches[1][0]); 128 129 // Create page 2. 130 $page2 = $this->getDataGenerator()->create_module('page', 131 ['course' => $course1->id, 'name' => 'Test 2']); 132 // Filter the text again. 133 $filtered2 = format_text($html, FORMAT_HTML, array('context' => $context1)); 134 // The filter result does not change due to caching. 135 $this->assertEquals($filtered1, $filtered2); 136 137 // Change context, so that cache for course 1 is cleared. 138 $filtered3 = format_text($html, FORMAT_HTML, array('context' => $context2)); 139 $this->assertNotEquals($filtered1, $filtered3); 140 $matches = []; 141 preg_match_all('~<a class="autolink" title="([^"]*)" href="[^"]*/mod/page/view.php\?id=([0-9]+)">([^<]*)</a>~', 142 $filtered3, $matches); 143 // There should be no links. 144 $this->assertCount(0, $matches[1]); 145 146 // Filter the text for course 1. 147 $filtered4 = format_text($html, FORMAT_HTML, array('context' => $context1)); 148 // Find all the activity links in the result. 149 $matches = []; 150 preg_match_all('~<a class="autolink" title="([^"]*)" href="[^"]*/mod/page/view.php\?id=([0-9]+)">([^<]*)</a>~', 151 $filtered4, $matches); 152 // There should be 2 links. 153 $this->assertCount(2, $matches[1]); 154 $this->assertEquals($page1->name, $matches[1][0]); 155 $this->assertEquals($page2->name, $matches[1][1]); 156 } 157 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body