2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Code quality unit tests that are fast enough to run each time.
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
27 defined('MOODLE_INTERNAL') ||
die();
30 * Bogus custom context class for testing
32 class context_bogus1
extends context
{
34 * Returns the most relevant URL for this context.
38 public function get_url() {
44 * Returns array of relevant context capability records.
48 public function get_capabilities(string $sort = self
::DEFAULT_CAPABILITY_SORT
) {
54 * Bogus custom context class for testing
56 class context_bogus2
extends context
{
58 * Returns the most relevant URL for this context.
62 public function get_url() {
68 * Returns array of relevant context capability records.
72 public function get_capabilities(string $sort = self
::DEFAULT_CAPABILITY_SORT
) {
78 * Bogus custom context class for testing
80 class context_bogus3
extends context
{
82 * Returns the most relevant URL for this context.
86 public function get_url() {
92 * Returns array of relevant context capability records.
96 public function get_capabilities(string $sort = self
::DEFAULT_CAPABILITY_SORT
) {
101 class customcontext_testcase
extends advanced_testcase
{
104 * Perform setup before every test. This tells Moodle's phpunit to reset the database after every test.
106 protected function setUp(): void
{
108 $this->resetAfterTest(true);
112 * Test case for custom context classes
114 public function test_customcontexts() {
116 static $customcontexts = array(
117 11 => 'context_bogus1',
118 12 => 'context_bogus2',
119 13 => 'context_bogus3'
122 // save any existing custom contexts
123 $existingcustomcontexts = get_config(null, 'custom_context_classes');
125 set_config('custom_context_classes', serialize($customcontexts));
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');
133 // clean-up & restore any custom contexts
134 set_config('custom_context_classes', ($existingcustomcontexts === false) ?
null : $existingcustomcontexts);
136 context_helper
::reset_levels();