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 defined('MOODLE_INTERNAL') || die(); 28 29 global $CFG; 30 31 require_once($CFG->dirroot . '/webservice/tests/helpers.php'); 32 use core_filters\external; 33 34 /** 35 * External filter functions unit tests. 36 * 37 * @package core_filters 38 * @category external 39 * @copyright 2017 Juan Leyva 40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 41 * @since Moodle 3.4 42 */ 43 class core_filter_external_testcase extends externallib_advanced_testcase { 44 45 /** 46 * Test get_available_in_context_system 47 */ 48 public function test_get_available_in_context_system() { 49 global $DB; 50 51 $this->resetAfterTest(true); 52 $this->setAdminUser(); 53 54 $this->expectException('moodle_exception'); 55 external::get_available_in_context(array(array('contextlevel' => 'system', 'instanceid' => 0))); 56 } 57 58 /** 59 * Test get_available_in_context_category 60 */ 61 public function test_get_available_in_context_category() { 62 global $DB; 63 64 $this->resetAfterTest(true); 65 $this->setAdminUser(); 66 67 $category = self::getDataGenerator()->create_category(); 68 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); 73 } 74 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']); 79 80 // Enable one filter at global level. 81 reset($allfilters); 82 $firstfilter = key($allfilters); 83 filter_set_global_state($firstfilter, TEXTFILTER_ON); 84 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. 91 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. 100 } 101 102 /** 103 * Test get_available_in_context_course 104 */ 105 public function test_get_available_in_context_course() { 106 global $DB; 107 108 $this->resetAfterTest(true); 109 $this->setAdminUser(); 110 111 $course = self::getDataGenerator()->create_course(); 112 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); 117 } 118 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']); 123 124 // Enable one filter at global level. 125 reset($allfilters); 126 $firstfilter = key($allfilters); 127 filter_set_global_state($firstfilter, TEXTFILTER_ON); 128 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. 135 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. 144 } 145 146 /** 147 * Test get_available_in_context_module 148 */ 149 public function test_get_available_in_context_module() { 150 global $DB; 151 152 $this->resetAfterTest(true); 153 $this->setAdminUser(); 154 155 // Create one activity. 156 $course = self::getDataGenerator()->create_course(); 157 $forum = self::getDataGenerator()->create_module('forum', (object) array('course' => $course->id)); 158 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); 163 } 164 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']); 169 170 // Enable one filter at global level. 171 reset($allfilters); 172 $firstfilter = key($allfilters); 173 filter_set_global_state($firstfilter, TEXTFILTER_ON); 174 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. 181 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. 190 191 // Try user without permission, warning expected. 192 $user = $this->getDataGenerator()->create_user(); 193 $this->setUser($user); 194 $result = external::get_available_in_context(array(array('contextlevel' => 'module', 'instanceid' => $forum->cmid))); 195 $result = external_api::clean_returnvalue(external::get_available_in_context_returns(), $result); 196 $this->assertNotEmpty($result['warnings']); 197 $this->assertEquals('context', $result['warnings'][0]['item']); 198 $this->assertEquals($forum->cmid, $result['warnings'][0]['itemid']); 199 } 200 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body