Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 311 and 402] [Versions 400 and 402] [Versions 401 and 402]

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