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 310 and 402] [Versions 311 and 402] [Versions 39 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   * Code quality unit tests that are fast enough to run each time.
  19   *
  20   * @package    core
  21   * @category   test
  22   * @copyright  (C) 2013 onwards Remote Learner.net Inc (http://www.remote-learner.net)
  23   * @author     Brent Boghosian (brent.boghosian@remote-learner.net)
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  namespace core;
  28  
  29  use context;
  30  use context_helper;
  31  
  32  defined('MOODLE_INTERNAL') || die();
  33  
  34  
  35  /**
  36   * Code quality unit tests that are fast enough to run each time.
  37   *
  38   * @package    core
  39   * @category   test
  40   * @copyright  (C) 2013 onwards Remote Learner.net Inc (http://www.remote-learner.net)
  41   * @author     Brent Boghosian (brent.boghosian@remote-learner.net)
  42   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   */
  44  class customcontext_test extends \advanced_testcase {
  45  
  46      /**
  47       * Perform setup before every test. This tells Moodle's phpunit to reset the database after every test.
  48       */
  49      protected function setUp(): void {
  50          parent::setUp();
  51          $this->resetAfterTest(true);
  52      }
  53  
  54      /**
  55       * Test case for custom context classes
  56       */
  57      public function test_customcontexts() {
  58          global $CFG;
  59          static $customcontexts = array(
  60              11 => 'context_bogus1',
  61              12 => 'context_bogus2',
  62              13 => 'context_bogus3'
  63          );
  64  
  65          // save any existing custom contexts
  66          $existingcustomcontexts = get_config(null, 'custom_context_classes');
  67  
  68          set_config('custom_context_classes', serialize($customcontexts));
  69          initialise_cfg();
  70          context_helper::reset_levels();
  71          $alllevels = context_helper::get_all_levels();
  72          $this->assertEquals($alllevels[11], 'context_bogus1');
  73          $this->assertEquals($alllevels[12], 'context_bogus2');
  74          $this->assertEquals($alllevels[13], 'context_bogus3');
  75  
  76          // clean-up & restore any custom contexts
  77          set_config('custom_context_classes', ($existingcustomcontexts === false) ? null : $existingcustomcontexts);
  78          initialise_cfg();
  79          context_helper::reset_levels();
  80      }
  81  }
  82  
  83  /**
  84   * Bogus custom context class for testing
  85   */
  86  class context_bogus1 extends context {
  87      /**
  88       * Returns context shortname.
  89       *
  90       * @return string
  91       */
  92      public static function get_short_name(): string {
  93          return 'bogus1';
  94      }
  95  
  96      /**
  97       * Returns the most relevant URL for this context.
  98       *
  99       * @return \moodle_url
 100       */
 101      public function get_url() {
 102          global $ME;
 103          return $ME;
 104      }
 105  
 106      /**
 107       * Returns array of relevant context capability records.
 108       *
 109       * @return array
 110       */
 111      public function get_capabilities(string $sort = self::DEFAULT_CAPABILITY_SORT) {
 112          return array();
 113      }
 114  }
 115  
 116  /**
 117   * Bogus custom context class for testing
 118   */
 119  class context_bogus2 extends context {
 120      /**
 121       * Returns context shortname.
 122       *
 123       * @return string
 124       */
 125      public static function get_short_name(): string {
 126          return 'bogus2';
 127      }
 128  
 129      /**
 130       * Returns the most relevant URL for this context.
 131       *
 132       * @return \moodle_url
 133       */
 134      public function get_url() {
 135          global $ME;
 136          return $ME;
 137      }
 138  
 139      /**
 140       * Returns array of relevant context capability records.
 141       *
 142       * @return array
 143       */
 144      public function get_capabilities(string $sort = self::DEFAULT_CAPABILITY_SORT) {
 145          return array();
 146      }
 147  }
 148  
 149  /**
 150   * Bogus custom context class for testing
 151   */
 152  class context_bogus3 extends context {
 153      /**
 154       * Returns context shortname.
 155       *
 156       * @return string
 157       */
 158      public static function get_short_name(): string {
 159          return 'bogus3';
 160      }
 161  
 162      /**
 163       * Returns the most relevant URL for this context.
 164       *
 165       * @return \moodle_url
 166       */
 167      public function get_url() {
 168          global $ME;
 169          return $ME;
 170      }
 171  
 172      /**
 173       * Returns array of relevant context capability records.
 174       *
 175       * @return array
 176       */
 177      public function get_capabilities(string $sort = self::DEFAULT_CAPABILITY_SORT) {
 178          return array();
 179      }
 180  }