Differences Between: [Versions 310 and 311] [Versions 39 and 311]
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 namespace filter_activitynames; 27 28 /** 29 * Test case for the activity names auto-linking filter. 30 * 31 * @copyright 2018 The Open University 32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 33 */ 34 class filter_test extends \advanced_testcase { 35 36 public function test_links() { 37 $this->resetAfterTest(true); 38 39 // Create a test course. 40 $course = $this->getDataGenerator()->create_course(); 41 $context = \context_course::instance($course->id); 42 43 // Create two pages that will be linked to. 44 $page1 = $this->getDataGenerator()->create_module('page', 45 ['course' => $course->id, 'name' => 'Test 1']); 46 $page2 = $this->getDataGenerator()->create_module('page', 47 ['course' => $course->id, 'name' => 'Test (2)']); 48 49 // Format text with all three entries in HTML. 50 $html = '<p>Please read the two pages Test 1 and <i>Test (2)</i>.</p>'; 51 $filtered = format_text($html, FORMAT_HTML, array('context' => $context)); 52 53 // Find all the glossary links in the result. 54 $matches = []; 55 preg_match_all('~<a class="autolink" title="([^"]*)" href="[^"]*/mod/page/view.php\?id=([0-9]+)">([^<]*)</a>~', 56 $filtered, $matches); 57 58 // There should be 2 links links. 59 $this->assertCount(2, $matches[1]); 60 61 // Check text of title attribute. 62 $this->assertEquals($page1->name, $matches[1][0]); 63 $this->assertEquals($page2->name, $matches[1][1]); 64 65 // Check the ids in the links. 66 $this->assertEquals($page1->cmid, $matches[2][0]); 67 $this->assertEquals($page2->cmid, $matches[2][1]); 68 69 // Check the link text. 70 $this->assertEquals($page1->name, $matches[3][0]); 71 $this->assertEquals($page2->name, $matches[3][1]); 72 } 73 74 public function test_links_activity_named_hyphen() { 75 $this->resetAfterTest(true); 76 77 // Create a test course. 78 $course = $this->getDataGenerator()->create_course(); 79 $context = \context_course::instance($course->id); 80 81 // Work around an issue with the activity names filter which maintains a static cache 82 // of activities for current course ID. We can re-build the cache by switching user. 83 $this->setUser($this->getDataGenerator()->create_user()); 84 85 // Create a page activity named '-' (single hyphen). 86 $page = $this->getDataGenerator()->create_module('page', ['course' => $course->id, 'name' => '-']); 87 88 $html = '<p>Please read the - page.</p>'; 89 $filtered = format_text($html, FORMAT_HTML, array('context' => $context)); 90 91 // Find the page link in the filtered html. 92 preg_match_all('~<a class="autolink" title="([^"]*)" href="[^"]*/mod/page/view.php\?id=([0-9]+)">([^<]*)</a>~', 93 $filtered, $matches); 94 95 // We should have exactly one match. 96 $this->assertCount(1, $matches[1]); 97 98 $this->assertEquals($page->name, $matches[1][0]); 99 $this->assertEquals($page->cmid, $matches[2][0]); 100 $this->assertEquals($page->name, $matches[3][0]); 101 } 102 103 public function test_cache() { 104 $this->resetAfterTest(true); 105 106 // Create a test courses. 107 $course1 = $this->getDataGenerator()->create_course(); 108 $course2 = $this->getDataGenerator()->create_course(); 109 $context1 = \context_course::instance($course1->id); 110 $context2 = \context_course::instance($course2->id); 111 112 // Create page 1. 113 $page1 = $this->getDataGenerator()->create_module('page', 114 ['course' => $course1->id, 'name' => 'Test 1']); 115 // Format text with page 1 in HTML. 116 $html = '<p>Please read the two pages Test 1 and Test 2.</p>'; 117 $filtered1 = format_text($html, FORMAT_HTML, array('context' => $context1)); 118 // Find all the activity links in the result. 119 $matches = []; 120 preg_match_all('~<a class="autolink" title="([^"]*)" href="[^"]*/mod/page/view.php\?id=([0-9]+)">([^<]*)</a>~', 121 $filtered1, $matches); 122 // There should be 1 link. 123 $this->assertCount(1, $matches[1]); 124 $this->assertEquals($page1->name, $matches[1][0]); 125 126 // Create page 2. 127 $page2 = $this->getDataGenerator()->create_module('page', 128 ['course' => $course1->id, 'name' => 'Test 2']); 129 // Filter the text again. 130 $filtered2 = format_text($html, FORMAT_HTML, array('context' => $context1)); 131 // The filter result does not change due to caching. 132 $this->assertEquals($filtered1, $filtered2); 133 134 // Change context, so that cache for course 1 is cleared. 135 $filtered3 = format_text($html, FORMAT_HTML, array('context' => $context2)); 136 $this->assertNotEquals($filtered1, $filtered3); 137 $matches = []; 138 preg_match_all('~<a class="autolink" title="([^"]*)" href="[^"]*/mod/page/view.php\?id=([0-9]+)">([^<]*)</a>~', 139 $filtered3, $matches); 140 // There should be no links. 141 $this->assertCount(0, $matches[1]); 142 143 // Filter the text for course 1. 144 $filtered4 = format_text($html, FORMAT_HTML, array('context' => $context1)); 145 // Find all the activity links in the result. 146 $matches = []; 147 preg_match_all('~<a class="autolink" title="([^"]*)" href="[^"]*/mod/page/view.php\?id=([0-9]+)">([^<]*)</a>~', 148 $filtered4, $matches); 149 // There should be 2 links. 150 $this->assertCount(2, $matches[1]); 151 $this->assertEquals($page1->name, $matches[1][0]); 152 $this->assertEquals($page2->name, $matches[1][1]); 153 } 154 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body