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/>.
18 * External filter functions unit tests.
20 * @package core_filters
22 * @copyright 2017 Juan Leyva
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') ||
die();
31 require_once($CFG->dirroot
. '/webservice/tests/helpers.php');
32 use core_filters\external
;
35 * External filter functions unit tests.
37 * @package core_filters
39 * @copyright 2017 Juan Leyva
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 class core_filter_external_testcase
extends externallib_advanced_testcase
{
46 * Test get_available_in_context_system
48 public function test_get_available_in_context_system() {
51 $this->resetAfterTest(true);
52 $this->setAdminUser();
54 $this->expectException('moodle_exception');
55 external
::get_available_in_context(array(array('contextlevel' => 'system', 'instanceid' => 0)));
59 * Test get_available_in_context_category
61 public function test_get_available_in_context_category() {
64 $this->resetAfterTest(true);
65 $this->setAdminUser();
67 $category = self
::getDataGenerator()->create_category();
69 // Get all filters and disable them all globally.
70 $allfilters = filter_get_all_installed();
71 foreach ($allfilters as $filter => $filtername) {
72 filter_set_global_state($filter, TEXTFILTER_DISABLED
);
75 $result = external
::get_available_in_context(array(array('contextlevel' => 'coursecat', 'instanceid' => $category->id
)));
76 $result = external_api
::clean_returnvalue(external
::get_available_in_context_returns(), $result);
77 $this->assertEmpty($result['filters']); // No filters, all disabled.
78 $this->assertEmpty($result['warnings']);
80 // Enable one filter at global level.
82 $firstfilter = key($allfilters);
83 filter_set_global_state($firstfilter, TEXTFILTER_ON
);
85 $result = external
::get_available_in_context(array(array('contextlevel' => 'coursecat', 'instanceid' => $category->id
)));
86 $result = external_api
::clean_returnvalue(external
::get_available_in_context_returns(), $result);
87 $this->assertEmpty($result['warnings']);
88 $this->assertEquals($firstfilter, $result['filters'][0]['filter']); // OK, the filter is enabled.
89 $this->assertEquals(TEXTFILTER_INHERIT
, $result['filters'][0]['localstate']); // Inherits the parent context status.
90 $this->assertEquals(TEXTFILTER_ON
, $result['filters'][0]['inheritedstate']); // In the parent context is available.
92 // Set off the same filter at local context level.
93 filter_set_local_state($firstfilter, context_coursecat
::instance($category->id
)->id
, TEXTFILTER_OFF
);
94 $result = external
::get_available_in_context(array(array('contextlevel' => 'coursecat', 'instanceid' => $category->id
)));
95 $result = external_api
::clean_returnvalue(external
::get_available_in_context_returns(), $result);
96 $this->assertEmpty($result['warnings']);
97 $this->assertEquals($firstfilter, $result['filters'][0]['filter']); // OK, the filter is enabled globally.
98 $this->assertEquals(TEXTFILTER_OFF
, $result['filters'][0]['localstate']); // It is not available in this context.
99 $this->assertEquals(TEXTFILTER_ON
, $result['filters'][0]['inheritedstate']); // In the parent context is available.
103 * Test get_available_in_context_course
105 public function test_get_available_in_context_course() {
108 $this->resetAfterTest(true);
109 $this->setAdminUser();
111 $course = self
::getDataGenerator()->create_course();
113 // Get all filters and disable them all globally.
114 $allfilters = filter_get_all_installed();
115 foreach ($allfilters as $filter => $filtername) {
116 filter_set_global_state($filter, TEXTFILTER_DISABLED
);
119 $result = external
::get_available_in_context(array(array('contextlevel' => 'course', 'instanceid' => $course->id
)));
120 $result = external_api
::clean_returnvalue(external
::get_available_in_context_returns(), $result);
121 $this->assertEmpty($result['filters']); // No filters, all disabled at global level.
122 $this->assertEmpty($result['warnings']);
124 // Enable one filter at global level.
126 $firstfilter = key($allfilters);
127 filter_set_global_state($firstfilter, TEXTFILTER_ON
);
129 $result = external
::get_available_in_context(array(array('contextlevel' => 'course', 'instanceid' => $course->id
)));
130 $result = external_api
::clean_returnvalue(external
::get_available_in_context_returns(), $result);
131 $this->assertEmpty($result['warnings']);
132 $this->assertEquals($firstfilter, $result['filters'][0]['filter']); // OK, the filter is enabled.
133 $this->assertEquals(TEXTFILTER_INHERIT
, $result['filters'][0]['localstate']); // Inherits the parent context status.
134 $this->assertEquals(TEXTFILTER_ON
, $result['filters'][0]['inheritedstate']); // In the parent context is available.
136 // Set off the same filter at local context level.
137 filter_set_local_state($firstfilter, context_course
::instance($course->id
)->id
, TEXTFILTER_OFF
);
138 $result = external
::get_available_in_context(array(array('contextlevel' => 'course', 'instanceid' => $course->id
)));
139 $result = external_api
::clean_returnvalue(external
::get_available_in_context_returns(), $result);
140 $this->assertEmpty($result['warnings']);
141 $this->assertEquals($firstfilter, $result['filters'][0]['filter']); // OK, the filter is enabled globally.
142 $this->assertEquals(TEXTFILTER_OFF
, $result['filters'][0]['localstate']); // It is not available in this context.
143 $this->assertEquals(TEXTFILTER_ON
, $result['filters'][0]['inheritedstate']); // In the parent context is available.
147 * Test get_available_in_context_module
149 public function test_get_available_in_context_module() {
152 $this->resetAfterTest(true);
153 $this->setAdminUser();
155 // Create one activity.
156 $course = self
::getDataGenerator()->create_course();
157 $forum = self
::getDataGenerator()->create_module('forum', (object) array('course' => $course->id
));
159 // Get all filters and disable them all globally.
160 $allfilters = filter_get_all_installed();
161 foreach ($allfilters as $filter => $filtername) {
162 filter_set_global_state($filter, TEXTFILTER_DISABLED
);
165 $result = external
::get_available_in_context(array(array('contextlevel' => 'module', 'instanceid' => $forum->cmid
)));
166 $result = external_api
::clean_returnvalue(external
::get_available_in_context_returns(), $result);
167 $this->assertEmpty($result['filters']); // No filters, all disabled at global level.
168 $this->assertEmpty($result['warnings']);
170 // Enable one filter at global level.
172 $firstfilter = key($allfilters);
173 filter_set_global_state($firstfilter, TEXTFILTER_ON
);
175 $result = external
::get_available_in_context(array(array('contextlevel' => 'module', 'instanceid' => $forum->cmid
)));
176 $result = external_api
::clean_returnvalue(external
::get_available_in_context_returns(), $result);
177 $this->assertEmpty($result['warnings']);
178 $this->assertEquals($firstfilter, $result['filters'][0]['filter']); // OK, the filter is enabled.
179 $this->assertEquals(TEXTFILTER_INHERIT
, $result['filters'][0]['localstate']); // Inherits the parent context status.
180 $this->assertEquals(TEXTFILTER_ON
, $result['filters'][0]['inheritedstate']); // In the parent context is available.
182 // Set off the same filter at local context level.
183 filter_set_local_state($firstfilter, context_module
::instance($forum->cmid
)->id
, TEXTFILTER_OFF
);
184 $result = external
::get_available_in_context(array(array('contextlevel' => 'module', 'instanceid' => $forum->cmid
)));
185 $result = external_api
::clean_returnvalue(external
::get_available_in_context_returns(), $result);
186 $this->assertEmpty($result['warnings']);
187 $this->assertEquals($firstfilter, $result['filters'][0]['filter']); // OK, the filter is enabled globally.
188 $this->assertEquals(TEXTFILTER_OFF
, $result['filters'][0]['localstate']); // It is not available in this context.
189 $this->assertEquals(TEXTFILTER_ON
, $result['filters'][0]['inheritedstate']); // In the parent context is available.