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.
   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  // Note: Technically this namespace is incorrect, but we should be moving to namespace things and core anyway.
  18  namespace core;
  19  
  20  /**
  21   * Unit tests specifically for context_block.
  22   *
  23   * @package   core
  24   * @category  phpunit
  25   * @copyright 2022 Andrew Lyons <andrew@nicols.co.uk>
  26   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   * @coversDefaultClass \context_block
  28   */
  29  class context_block_test extends \advanced_testcase {
  30  
  31      /**
  32       * Test setup.
  33       */
  34      public function setUp(): void {
  35          global $CFG;
  36          require_once("{$CFG->libdir}/accesslib.php");
  37      }
  38  
  39      /**
  40       * Ensure that block contexts are correctly created for blocks where they are missing.
  41       *
  42       * @covers ::create_level_instances
  43       */
  44      public function test_context_creation(): void {
  45          global $DB;
  46  
  47          $this->resetAfterTest();
  48  
  49          // Create some parent contexts.
  50          $generator = $this->getDataGenerator();
  51          $user = $generator->create_user();
  52          $coursecat = $generator->create_category();
  53          $course = $generator->create_course(['category' => $coursecat->id]);
  54          $activity = $generator->create_module('forum', ['course' => $course->id]);
  55  
  56          $contextlist = [
  57              \context_system::instance(),
  58              \context_user::instance($user->id),
  59              \context_coursecat::instance($coursecat->id),
  60              \context_course::instance($course->id),
  61              \context_module::instance($activity->cmid),
  62          ];
  63  
  64          // Create a number of blocks of different types in the DB only.
  65          // This is typically seen when creating large numbers in an upgrade script.
  66          $blocks = [];
  67          for ($i = 0; $i < 10; $i++) {
  68              foreach ($contextlist as $context) {
  69                  $blocks[] = $DB->insert_record('block_instances', [
  70                      'blockname' => 'calendar_month',
  71                      'parentcontextid' => $context->id,
  72                      'showinsubcontexts' => 1,
  73                      'requiredbytheme' => 0,
  74                      'pagetypepattern' => 'my-index',
  75                      'subpagepattern' => 1,
  76                      'defaultregion' => 'content',
  77                      'defaultweight' => 1,
  78                      'timecreated' => time(),
  79                      'timemodified' => time(),
  80                  ]);
  81              }
  82          }
  83  
  84          // Test data created. Call \context_helper::create_instances() which will create the records, and fix the paths.
  85          \context_helper::create_instances(CONTEXT_BLOCK);
  86  
  87          foreach ($blocks as $blockid) {
  88              $block = $DB->get_record('block_instances', ['id' => $blockid]);
  89              $context = \context_block::instance($block->id);
  90              $this->assertInstanceOf(\context_block::class, $context);
  91  
  92              // Note. There is no point checking the instanceid because the context was fetched using this.
  93  
  94              // Ensure that the contextlevel is correct.
  95              $this->assertEquals(CONTEXT_BLOCK, $context->contextlevel);
  96  
  97              // Fetch the parent context.
  98              $parentcontext = $context->get_parent_context();
  99  
 100              // This hsould match the parent context specified in the block instance configuration.
 101              $this->assertEquals($block->parentcontextid, $parentcontext->id);
 102  
 103              // Ensure that the path and depth are correctly specified.
 104              $this->assertEquals($parentcontext->path . "/{$context->id}", $context->path);
 105              $this->assertEquals($parentcontext->depth + 1, $context->depth);
 106          }
 107      }
 108  }