See Release Notes
Long Term Support Release
Differences Between: [Versions 401 and 402] [Versions 401 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 * External filter functions unit tests. 19 * 20 * @package core_filters 21 * @category external 22 * @copyright 2017 Juan Leyva 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 * @since Moodle 3.4 25 */ 26 27 namespace core_filters\external; 28 29 use externallib_advanced_testcase; 30 use core_filters\external; 31 32 defined('MOODLE_INTERNAL') || die(); 33 34 global $CFG; 35 36 require_once($CFG->dirroot . '/webservice/tests/helpers.php'); 37 38 /** 39 * External filter functions unit tests. 40 * 41 * @package core_filters 42 * @category external 43 * @copyright 2017 Juan Leyva 44 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 45 * @since Moodle 3.4 46 */ 47 class external_test extends externallib_advanced_testcase { 48 49 /** 50 * Test get_available_in_context_system 51 */ 52 public function test_get_available_in_context_system() { 53 global $DB; 54 55 $this->resetAfterTest(true); 56 $this->setAdminUser(); 57 58 $this->expectException('moodle_exception'); 59 external::get_available_in_context(array(array('contextlevel' => 'system', 'instanceid' => 0))); 60 } 61 62 /** 63 * Test get_available_in_context_category 64 */ 65 public function test_get_available_in_context_category() { 66 global $DB; 67 68 $this->resetAfterTest(true); 69 $this->setAdminUser(); 70 71 $category = self::getDataGenerator()->create_category(); 72 73 // Get all filters and disable them all globally. 74 $allfilters = filter_get_all_installed(); 75 foreach ($allfilters as $filter => $filtername) { 76 filter_set_global_state($filter, TEXTFILTER_DISABLED); 77 } 78 79 $result = external::get_available_in_context(array(array('contextlevel' => 'coursecat', 'instanceid' => $category->id))); 80 $result = \external_api::clean_returnvalue(external::get_available_in_context_returns(), $result); 81 $this->assertEmpty($result['filters']); // No filters, all disabled. 82 $this->assertEmpty($result['warnings']); 83 84 // Enable one filter at global level. 85 reset($allfilters); 86 $firstfilter = key($allfilters); 87 filter_set_global_state($firstfilter, TEXTFILTER_ON); 88 89 $result = external::get_available_in_context(array(array('contextlevel' => 'coursecat', 'instanceid' => $category->id))); 90 $result = \external_api::clean_returnvalue(external::get_available_in_context_returns(), $result); 91 $this->assertEmpty($result['warnings']); 92 $this->assertEquals($firstfilter, $result['filters'][0]['filter']); // OK, the filter is enabled. 93 $this->assertEquals(TEXTFILTER_INHERIT, $result['filters'][0]['localstate']); // Inherits the parent context status. 94 $this->assertEquals(TEXTFILTER_ON, $result['filters'][0]['inheritedstate']); // In the parent context is available. 95 96 // Set off the same filter at local context level. 97 filter_set_local_state($firstfilter, \context_coursecat::instance($category->id)->id, TEXTFILTER_OFF); 98 $result = external::get_available_in_context(array(array('contextlevel' => 'coursecat', 'instanceid' => $category->id))); 99 $result = \external_api::clean_returnvalue(external::get_available_in_context_returns(), $result); 100 $this->assertEmpty($result['warnings']); 101 $this->assertEquals($firstfilter, $result['filters'][0]['filter']); // OK, the filter is enabled globally. 102 $this->assertEquals(TEXTFILTER_OFF, $result['filters'][0]['localstate']); // It is not available in this context. 103 $this->assertEquals(TEXTFILTER_ON, $result['filters'][0]['inheritedstate']); // In the parent context is available. 104 } 105 106 /** 107 * Test get_available_in_context_course 108 */ 109 public function test_get_available_in_context_course() { 110 global $DB; 111 112 $this->resetAfterTest(true); 113 $this->setAdminUser(); 114 115 $course = self::getDataGenerator()->create_course(); 116 117 // Get all filters and disable them all globally. 118 $allfilters = filter_get_all_installed(); 119 foreach ($allfilters as $filter => $filtername) { 120 filter_set_global_state($filter, TEXTFILTER_DISABLED); 121 } 122 123 $result = external::get_available_in_context(array(array('contextlevel' => 'course', 'instanceid' => $course->id))); 124 $result = \external_api::clean_returnvalue(external::get_available_in_context_returns(), $result); 125 $this->assertEmpty($result['filters']); // No filters, all disabled at global level. 126 $this->assertEmpty($result['warnings']); 127 128 // Enable one filter at global level. 129 reset($allfilters); 130 $firstfilter = key($allfilters); 131 filter_set_global_state($firstfilter, TEXTFILTER_ON); 132 133 $result = external::get_available_in_context(array(array('contextlevel' => 'course', 'instanceid' => $course->id))); 134 $result = \external_api::clean_returnvalue(external::get_available_in_context_returns(), $result); 135 $this->assertEmpty($result['warnings']); 136 $this->assertEquals($firstfilter, $result['filters'][0]['filter']); // OK, the filter is enabled. 137 $this->assertEquals(TEXTFILTER_INHERIT, $result['filters'][0]['localstate']); // Inherits the parent context status. 138 $this->assertEquals(TEXTFILTER_ON, $result['filters'][0]['inheritedstate']); // In the parent context is available. 139 140 // Set off the same filter at local context level. 141 filter_set_local_state($firstfilter, \context_course::instance($course->id)->id, TEXTFILTER_OFF); 142 $result = external::get_available_in_context(array(array('contextlevel' => 'course', 'instanceid' => $course->id))); 143 $result = \external_api::clean_returnvalue(external::get_available_in_context_returns(), $result); 144 $this->assertEmpty($result['warnings']); 145 $this->assertEquals($firstfilter, $result['filters'][0]['filter']); // OK, the filter is enabled globally. 146 $this->assertEquals(TEXTFILTER_OFF, $result['filters'][0]['localstate']); // It is not available in this context. 147 $this->assertEquals(TEXTFILTER_ON, $result['filters'][0]['inheritedstate']); // In the parent context is available. 148 } 149 150 /** 151 * Test get_available_in_context_module 152 */ 153 public function test_get_available_in_context_module() { 154 global $DB; 155 156 $this->resetAfterTest(true); 157 $this->setAdminUser(); 158 159 // Create one activity. 160 $course = self::getDataGenerator()->create_course(); 161 $forum = self::getDataGenerator()->create_module('forum', (object) array('course' => $course->id)); 162 163 // Get all filters and disable them all globally. 164 $allfilters = filter_get_all_installed(); 165 foreach ($allfilters as $filter => $filtername) { 166 filter_set_global_state($filter, TEXTFILTER_DISABLED); 167 } 168 169 $result = external::get_available_in_context(array(array('contextlevel' => 'module', 'instanceid' => $forum->cmid))); 170 $result = \external_api::clean_returnvalue(external::get_available_in_context_returns(), $result); 171 $this->assertEmpty($result['filters']); // No filters, all disabled at global level. 172 $this->assertEmpty($result['warnings']); 173 174 // Enable one filter at global level. 175 reset($allfilters); 176 $firstfilter = key($allfilters); 177 filter_set_global_state($firstfilter, TEXTFILTER_ON); 178 179 $result = external::get_available_in_context(array(array('contextlevel' => 'module', 'instanceid' => $forum->cmid))); 180 $result = \external_api::clean_returnvalue(external::get_available_in_context_returns(), $result); 181 $this->assertEmpty($result['warnings']); 182 $this->assertEquals($firstfilter, $result['filters'][0]['filter']); // OK, the filter is enabled. 183 $this->assertEquals(TEXTFILTER_INHERIT, $result['filters'][0]['localstate']); // Inherits the parent context status. 184 $this->assertEquals(TEXTFILTER_ON, $result['filters'][0]['inheritedstate']); // In the parent context is available. 185 186 // Set off the same filter at local context level. 187 filter_set_local_state($firstfilter, \context_module::instance($forum->cmid)->id, TEXTFILTER_OFF); 188 $result = external::get_available_in_context(array(array('contextlevel' => 'module', 'instanceid' => $forum->cmid))); 189 $result = \external_api::clean_returnvalue(external::get_available_in_context_returns(), $result); 190 $this->assertEmpty($result['warnings']); 191 $this->assertEquals($firstfilter, $result['filters'][0]['filter']); // OK, the filter is enabled globally. 192 $this->assertEquals(TEXTFILTER_OFF, $result['filters'][0]['localstate']); // It is not available in this context. 193 $this->assertEquals(TEXTFILTER_ON, $result['filters'][0]['inheritedstate']); // In the parent context is available. 194 195 // Try user without permission, warning expected. 196 $user = $this->getDataGenerator()->create_user(); 197 $this->setUser($user); 198 $result = external::get_available_in_context(array(array('contextlevel' => 'module', 'instanceid' => $forum->cmid))); 199 $result = \external_api::clean_returnvalue(external::get_available_in_context_returns(), $result); 200 $this->assertNotEmpty($result['warnings']); 201 $this->assertEquals('context', $result['warnings'][0]['item']); 202 $this->assertEquals($forum->cmid, $result['warnings'][0]['itemid']); 203 } 204 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body