Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 39 and 400] [Versions 400 and 402] [Versions 400 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   * 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 the most relevant URL for this context.
  89       *
  90       * @return moodle_url
  91       */
  92      public function get_url() {
  93          global $ME;
  94          return $ME;
  95      }
  96  
  97      /**
  98       * Returns array of relevant context capability records.
  99       *
 100       * @return array
 101       */
 102      public function get_capabilities(string $sort = self::DEFAULT_CAPABILITY_SORT) {
 103          return array();
 104      }
 105  }
 106  
 107  /**
 108   * Bogus custom context class for testing
 109   */
 110  class context_bogus2 extends context {
 111      /**
 112       * Returns the most relevant URL for this context.
 113       *
 114       * @return moodle_url
 115       */
 116      public function get_url() {
 117          global $ME;
 118          return $ME;
 119      }
 120  
 121      /**
 122       * Returns array of relevant context capability records.
 123       *
 124       * @return array
 125       */
 126      public function get_capabilities(string $sort = self::DEFAULT_CAPABILITY_SORT) {
 127          return array();
 128      }
 129  }
 130  
 131  /**
 132   * Bogus custom context class for testing
 133   */
 134  class context_bogus3 extends context {
 135      /**
 136       * Returns the most relevant URL for this context.
 137       *
 138       * @return moodle_url
 139       */
 140      public function get_url() {
 141          global $ME;
 142          return $ME;
 143      }
 144  
 145      /**
 146       * Returns array of relevant context capability records.
 147       *
 148       * @return array
 149       */
 150      public function get_capabilities(string $sort = self::DEFAULT_CAPABILITY_SORT) {
 151          return array();
 152      }
 153  }