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_data
22 * @copyright 2015 David Monllao
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/data/filter.php');
32 * Tests for filter_data.
34 * @package filter_data
35 * @copyright 2015 David Monllao
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class filter_data_filter_testcase extends advanced_testcase {
41 * Tests that the filter applies the required changes.
45 public function test_filter() {
47 $this->resetAfterTest(true);
48 $this->setAdminUser();
49 filter_manager::reset_caches();
51 filter_set_global_state('data', TEXTFILTER_ON);
53 $course1 = $this->getDataGenerator()->create_course();
54 $coursecontext1 = context_course::instance($course1->id);
56 $course2 = $this->getDataGenerator()->create_course();
57 $coursecontext2 = context_course::instance($course2->id);
59 $sitecontext = context_course::instance(SITEID);
62 $this->add_simple_database_instance($site, array('SiteEntry'));
63 $this->add_simple_database_instance($course1, array('CourseEntry'));
65 $html = '<p>I like CourseEntry and SiteEntry</p>';
67 // Testing at course level (both site and course).
68 $filtered = format_text($html, FORMAT_HTML, array('context' => $coursecontext1));
69 $this->assertRegExp('/title=(\'|")CourseEntry(\'|")/', $filtered);
70 $this->assertRegExp('/title=(\'|")SiteEntry(\'|")/', $filtered);
72 // Testing at site level (only site).
73 $filtered = format_text($html, FORMAT_HTML, array('context' => $sitecontext));
74 $this->assertNotRegExp('/title=(\'|")CourseEntry(\'|")/', $filtered);
75 $this->assertRegExp('/title=(\'|")SiteEntry(\'|")/', $filtered);
77 // Changing to another course to test the caches invalidation (only site).
78 $filtered = format_text($html, FORMAT_HTML, array('context' => $coursecontext2));
79 $this->assertNotRegExp('/title=(\'|")CourseEntry(\'|")/', $filtered);
80 $this->assertRegExp('/title=(\'|")SiteEntry(\'|")/', $filtered);
84 * Adds a database instance to the provided course + a text field + adds all attached entries.
86 * @param stdClass $course
87 * @param array $entries A list of entry names.
90 protected function add_simple_database_instance($course, $entries = false) {
93 $database = $this->getDataGenerator()->create_module('data',
94 array('course' => $course->id));
97 $field = data_get_field_new('text', $database);
98 $fielddetail = new stdClass();
99 $fielddetail->d = $database->id;
100 $fielddetail->mode = 'add';
101 $fielddetail->type = 'text';
102 $fielddetail->sesskey = sesskey();
103 $fielddetail->name = 'Name';
104 $fielddetail->description = 'Some name';
105 $fielddetail->param1 = '1';
106 $field->define_field($fielddetail);
107 $field->insert_field();
108 $recordid = data_add_record($database);
111 foreach ($entries as $entrytext) {
112 $datacontent = array();
113 $datacontent['fieldid'] = $field->field->id;
114 $datacontent['recordid'] = $recordid;
115 $datacontent['content'] = $entrytext;
116 $contentid = $DB->insert_record('data_content', $datacontent);