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.
   1  <?php
   2  // This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
  16  
  17  namespace core\context;
  18  
  19  use core\context, core\context_helper;
  20  
  21  /**
  22   * Unit tests for coursecat context class.
  23   *
  24   * NOTE: more tests are in lib/tests/accesslib_test.php
  25   *
  26   * @package   core
  27   * @copyright Petr Skoda
  28   * @license   https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   * @coversDefaultClass \core\context\coursecat
  30   */
  31  class coursecat_test extends \advanced_testcase {
  32      /**
  33       * Tests legacy class name.
  34       * @coversNothing
  35       */
  36      public function test_legacy_classname() {
  37          $category = \core_course_category::get_default();
  38          $context = \context_coursecat::instance($category->id);
  39          $this->assertInstanceOf(coursecat::class, $context);
  40          $this->assertInstanceOf(\context_coursecat::class, $context);
  41      }
  42  
  43      /**
  44       * Tests covered methods.
  45       * @covers ::instance
  46       * @covers \core\context::instance_by_id
  47       */
  48      public function test_factory_methods() {
  49          $category = \core_course_category::get_default();
  50          $context = coursecat::instance($category->id);
  51          $this->assertInstanceOf(coursecat::class, $context);
  52          $this->assertSame($category->id, $context->instanceid);
  53  
  54          $context = context::instance_by_id($context->id);
  55          $this->assertInstanceOf(coursecat::class, $context);
  56          $this->assertSame($category->id, $context->instanceid);
  57      }
  58  
  59      /**
  60       * Tests covered method.
  61       * @covers ::get_short_name
  62       */
  63      public function test_get_short_name() {
  64          $this->assertSame('coursecat', coursecat::get_short_name());
  65      }
  66  
  67      /**
  68       * Tests levels.
  69       * @coversNothing
  70       */
  71      public function test_level() {
  72          $this->assertSame(40, coursecat::LEVEL);
  73          $this->assertSame(CONTEXT_COURSECAT, coursecat::LEVEL);
  74      }
  75  
  76      /**
  77       * Tests covered method.
  78       * @covers ::get_level_name
  79       */
  80      public function test_get_level_name() {
  81          $this->assertSame('Category', coursecat::get_level_name());
  82      }
  83  
  84      /**
  85       * Tests covered method.
  86       * @covers ::get_context_name
  87       */
  88      public function test_get_context_name() {
  89          $category = \core_course_category::get_default();
  90          $context = coursecat::instance($category->id);
  91          $this->assertSame('Category: Category 1', $context->get_context_name());
  92          $this->assertSame('Category: Category 1', $context->get_context_name(true));
  93          $this->assertSame('Category 1', $context->get_context_name(false));
  94          $this->assertSame('Category 1', $context->get_context_name(false, true));
  95          $this->assertSame('Category: Category 1', $context->get_context_name(true, true, false));
  96      }
  97  
  98      /**
  99       * Tests covered method.
 100       * @covers ::get_url
 101       */
 102      public function test_get_url() {
 103          $category = \core_course_category::get_default();
 104          $context = coursecat::instance($category->id);
 105          $expected = new \moodle_url('/course/index.php', ['categoryid' => $category->id]);
 106          $url = $context->get_url();
 107          $this->assertInstanceOf(\moodle_url::class, $url);
 108          $this->assertSame($expected->out(), $url->out());
 109      }
 110  
 111      /**
 112       * Tests covered methods.
 113       * @covers ::get_instance_table()
 114       * @covers ::get_behat_reference_columns()
 115       * @covers \core\context_helper::resolve_behat_reference
 116       */
 117      public function test_resolve_behat_reference() {
 118          $this->resetAfterTest();
 119  
 120          $instance = $this->getDataGenerator()->create_category(['idnumber' => 'xyz']);
 121          $context = context\coursecat::instance($instance->id);
 122  
 123          $result = context_helper::resolve_behat_reference('Category', $instance->idnumber);
 124          $this->assertSame($context->id, $result->id);
 125  
 126          $result = context_helper::resolve_behat_reference('coursecat', $instance->idnumber);
 127          $this->assertSame($context->id, $result->id);
 128  
 129          $result = context_helper::resolve_behat_reference('40', $instance->idnumber);
 130          $this->assertSame($context->id, $result->id);
 131  
 132          $result = context_helper::resolve_behat_reference('Category', 'dshjkdshjkhjsadjhdsa');
 133          $this->assertNull($result);
 134  
 135          $result = context_helper::resolve_behat_reference('Category', '');
 136          $this->assertNull($result);
 137      }
 138  
 139      /**
 140       * Tests covered method.
 141       * @covers ::get_compatible_role_archetypes
 142       */
 143      public function test_get_compatible_role_archetypes() {
 144          global $DB;
 145  
 146          $allarchetypes = $DB->get_fieldset_select('role', 'DISTINCT archetype', 'archetype IS NOT NULL');
 147          foreach ($allarchetypes as $allarchetype) {
 148              $levels = context_helper::get_compatible_levels($allarchetype);
 149              if ($allarchetype === 'manager' || $allarchetype === 'coursecreator') {
 150                  $this->assertContains(coursecat::LEVEL, $levels, "$allarchetype is expected to be compatible with context");
 151              } else {
 152                  $this->assertNotContains(coursecat::LEVEL, $levels, "$allarchetype is not expected to be compatible with context");
 153              }
 154          }
 155      }
 156  
 157      /**
 158       * Tests covered method.
 159       * @covers ::get_possible_parent_levels
 160       */
 161      public function test_get_possible_parent_levels() {
 162          $this->assertSame([system::LEVEL, coursecat::LEVEL], coursecat::get_possible_parent_levels());
 163      }
 164  
 165      /**
 166       * Tests covered method.
 167       * @covers ::get_capabilities
 168       */
 169      public function test_get_capabilities() {
 170          $category = \core_course_category::get_default();
 171  
 172          $context = coursecat::instance($category->id);
 173          $capabilities = $context->get_capabilities();
 174          $capabilities = convert_to_array($capabilities);
 175          $capabilities = array_column($capabilities, 'name');
 176          $this->assertContains('moodle/category:manage', $capabilities);
 177          $this->assertContains('moodle/course:view', $capabilities);
 178          $this->assertNotContains('moodle/user:viewalldetails', $capabilities);
 179      }
 180  
 181      /**
 182       * Tests covered method.
 183       * @covers ::create_level_instances
 184       */
 185      public function test_create_level_instances() {
 186          global $DB;
 187          $this->resetAfterTest();
 188  
 189          $coursecat = $this->getDataGenerator()->create_category();
 190          $coursecatcontext = coursecat::instance($coursecat->id);
 191  
 192          $DB->delete_records('context', ['id' => $coursecatcontext->id]);
 193          context_helper::create_instances(coursecat::LEVEL);
 194          $record = $DB->get_record('context', ['contextlevel' => coursecat::LEVEL, 'instanceid' => $coursecat->id], '*', MUST_EXIST);
 195      }
 196  
 197      /**
 198       * Tests covered method.
 199       * @covers ::get_child_contexts
 200       */
 201      public function test_get_child_contexts() {
 202          $this->resetAfterTest();
 203  
 204          $category = \core_course_category::get_default();
 205  
 206          $context = coursecat::instance($category->id);
 207          $children = $context->get_child_contexts();
 208          $this->assertCount(0, $children);
 209  
 210          $course = $this->getDataGenerator()->create_course(['categoryid' => $category->id]);
 211  
 212          // This may fail if some plugin auto-creates activities.
 213          $children = $context->get_child_contexts();
 214          $this->assertCount(1, $children);
 215      }
 216  
 217      /**
 218       * Tests covered method.
 219       * @covers ::get_cleanup_sql
 220       */
 221      public function test_get_cleanup_sql() {
 222          global $DB;
 223          $this->resetAfterTest();
 224  
 225          $coursecat = $this->getDataGenerator()->create_category();
 226          $coursecatcontext = coursecat::instance($coursecat->id);
 227  
 228          $DB->delete_records('course_categories', ['id' => $coursecat->id]);
 229  
 230          context_helper::cleanup_instances();
 231          $this->assertFalse($DB->record_exists('context', ['contextlevel' => coursecat::LEVEL, 'instanceid' => $coursecat->id]));
 232      }
 233  
 234      /**
 235       * Tests covered method.
 236       * @covers ::build_paths
 237       */
 238      public function test_build_paths() {
 239          global $DB;
 240          $this->resetAfterTest();
 241  
 242          $coursecat = $this->getDataGenerator()->create_category();
 243          $coursecatcontext = coursecat::instance($coursecat->id);
 244          $syscontext = system::instance();
 245  
 246          $DB->set_field('context', 'depth', 1, ['id' => $coursecatcontext->id]);
 247          $DB->set_field('context', 'path', '/0', ['id' => $coursecatcontext->id]);
 248  
 249          context_helper::build_all_paths(true);
 250  
 251          $record = $DB->get_record('context', ['id' => $coursecatcontext->id]);
 252          $this->assertSame('2', $record->depth);
 253          $this->assertSame('/' . $syscontext->id . '/' . $record->id, $record->path);
 254      }
 255  
 256      /**
 257       * Tests covered method.
 258       * @covers ::set_locked
 259       */
 260      public function test_set_locked() {
 261          global $DB;
 262          $this->resetAfterTest();
 263  
 264          $category1 = $this->getDataGenerator()->create_category();
 265          $category2 = $this->getDataGenerator()->create_category(['parent' => $category1->id]);
 266          $context1 = coursecat::instance($category1->id);
 267          $context2 = coursecat::instance($category2->id);
 268  
 269          $context1->set_locked(true);
 270          $context1 = coursecat::instance($category1->id);
 271          $context2 = coursecat::instance($category2->id);
 272          $this->assertTrue($context1->locked);
 273          $this->assertTrue($context2->locked);
 274          $record = $DB->get_record('context', ['id' => $context1->id]);
 275          $this->assertSame('1', $record->locked);
 276          $record = $DB->get_record('context', ['id' => $context2->id]);
 277          $this->assertSame('0', $record->locked);
 278  
 279          $context1->set_locked(false);
 280          $context1 = coursecat::instance($category1->id);
 281          $context2 = coursecat::instance($category2->id);
 282          $this->assertFalse($context1->locked);
 283          $this->assertFalse($context2->locked);
 284          $record = $DB->get_record('context', ['id' => $context1->id]);
 285          $this->assertSame('0', $record->locked);
 286          $record = $DB->get_record('context', ['id' => $context2->id]);
 287          $this->assertSame('0', $record->locked);
 288      }
 289  }