Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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   phpunit
  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  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Bogus custom context class for testing
  31   */
  32  class context_bogus1 extends context {
  33      /**
  34       * Returns the most relevant URL for this context.
  35       *
  36       * @return moodle_url
  37       */
  38      public function get_url() {
  39          global $ME;
  40          return $ME;
  41      }
  42  
  43      /**
  44       * Returns array of relevant context capability records.
  45       *
  46       * @return array
  47       */
  48      public function get_capabilities() {
  49          return array();
  50      }
  51  }
  52  
  53  /**
  54   * Bogus custom context class for testing
  55   */
  56  class context_bogus2 extends context {
  57      /**
  58       * Returns the most relevant URL for this context.
  59       *
  60       * @return moodle_url
  61       */
  62      public function get_url() {
  63          global $ME;
  64          return $ME;
  65      }
  66  
  67      /**
  68       * Returns array of relevant context capability records.
  69       *
  70       * @return array
  71       */
  72      public function get_capabilities() {
  73          return array();
  74      }
  75  }
  76  
  77  /**
  78   * Bogus custom context class for testing
  79   */
  80  class context_bogus3 extends context {
  81      /**
  82       * Returns the most relevant URL for this context.
  83       *
  84       * @return moodle_url
  85       */
  86      public function get_url() {
  87          global $ME;
  88          return $ME;
  89      }
  90  
  91      /**
  92       * Returns array of relevant context capability records.
  93       *
  94       * @return array
  95       */
  96      public function get_capabilities() {
  97          return array();
  98      }
  99  }
 100  
 101  class customcontext_testcase extends advanced_testcase {
 102  
 103      /**
 104       * Perform setup before every test. This tells Moodle's phpunit to reset the database after every test.
 105       */
 106      protected function setUp(): void {
 107          parent::setUp();
 108          $this->resetAfterTest(true);
 109      }
 110  
 111      /**
 112       * Test case for custom context classes
 113       */
 114      public function test_customcontexts() {
 115          global $CFG;
 116          static $customcontexts = array(
 117              11 => 'context_bogus1',
 118              12 => 'context_bogus2',
 119              13 => 'context_bogus3'
 120          );
 121  
 122          // save any existing custom contexts
 123          $existingcustomcontexts = get_config(null, 'custom_context_classes');
 124  
 125          set_config('custom_context_classes', serialize($customcontexts));
 126          initialise_cfg();
 127          context_helper::reset_levels();
 128          $alllevels = context_helper::get_all_levels();
 129          $this->assertEquals($alllevels[11], 'context_bogus1');
 130          $this->assertEquals($alllevels[12], 'context_bogus2');
 131          $this->assertEquals($alllevels[13], 'context_bogus3');
 132  
 133          // clean-up & restore any custom contexts
 134          set_config('custom_context_classes', ($existingcustomcontexts === false) ? null : $existingcustomcontexts);
 135          initialise_cfg();
 136          context_helper::reset_levels();
 137      }
 138  }