Differences Between: [Versions 310 and 403] [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_data 21 * @category test 22 * @copyright 2015 David Monllao 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 namespace filter_data; 27 28 /** 29 * Tests for filter_data. 30 * 31 * @package filter_data 32 * @copyright 2015 David Monllao 33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 34 */ 35 class filter_test extends \advanced_testcase { 36 37 /** 38 * Tests that the filter applies the required changes. 39 * 40 * @return void 41 */ 42 public function test_filter() { 43 44 $this->resetAfterTest(true); 45 $this->setAdminUser(); 46 \filter_manager::reset_caches(); 47 48 filter_set_global_state('data', TEXTFILTER_ON); 49 50 $course1 = $this->getDataGenerator()->create_course(); 51 $coursecontext1 = \context_course::instance($course1->id); 52 53 $course2 = $this->getDataGenerator()->create_course(); 54 $coursecontext2 = \context_course::instance($course2->id); 55 56 $sitecontext = \context_course::instance(SITEID); 57 58 $site = get_site(); 59 $this->add_simple_database_instance($site, array('SiteEntry')); 60 $this->add_simple_database_instance($course1, array('CourseEntry')); 61 62 $html = '<p>I like CourseEntry and SiteEntry</p>'; 63 64 // Testing at course level (both site and course). 65 $filtered = format_text($html, FORMAT_HTML, array('context' => $coursecontext1)); 66 $this->assertMatchesRegularExpression('/title=(\'|")CourseEntry(\'|")/', $filtered); 67 $this->assertMatchesRegularExpression('/title=(\'|")SiteEntry(\'|")/', $filtered); 68 69 // Testing at site level (only site). 70 $filtered = format_text($html, FORMAT_HTML, array('context' => $sitecontext)); 71 $this->assertDoesNotMatchRegularExpression('/title=(\'|")CourseEntry(\'|")/', $filtered); 72 $this->assertMatchesRegularExpression('/title=(\'|")SiteEntry(\'|")/', $filtered); 73 74 // Changing to another course to test the caches invalidation (only site). 75 $filtered = format_text($html, FORMAT_HTML, array('context' => $coursecontext2)); 76 $this->assertDoesNotMatchRegularExpression('/title=(\'|")CourseEntry(\'|")/', $filtered); 77 $this->assertMatchesRegularExpression('/title=(\'|")SiteEntry(\'|")/', $filtered); 78 } 79 80 /** 81 * Adds a database instance to the provided course + a text field + adds all attached entries. 82 * 83 * @param \stdClass $course 84 * @param array $entries A list of entry names. 85 * @return void 86 */ 87 protected function add_simple_database_instance($course, $entries = false) { 88 global $DB; 89 90 $database = $this->getDataGenerator()->create_module('data', 91 array('course' => $course->id)); 92 93 // A database field. 94 $field = data_get_field_new('text', $database); 95 $fielddetail = new \stdClass(); 96 $fielddetail->d = $database->id; 97 $fielddetail->mode = 'add'; 98 $fielddetail->type = 'text'; 99 $fielddetail->sesskey = sesskey(); 100 $fielddetail->name = 'Name'; 101 $fielddetail->description = 'Some name'; 102 $fielddetail->param1 = '1'; 103 $field->define_field($fielddetail); 104 $field->insert_field(); 105 $recordid = data_add_record($database); 106 107 // Database entries. 108 foreach ($entries as $entrytext) { 109 $datacontent = array(); 110 $datacontent['fieldid'] = $field->field->id; 111 $datacontent['recordid'] = $recordid; 112 $datacontent['content'] = $entrytext; 113 $contentid = $DB->insert_record('data_content', $datacontent); 114 } 115 } 116 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body