Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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.
   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   * Unit tests for the context locking events.
  19   *
  20   * @package    core
  21   * @category   test
  22   * @copyright  2019 University of Nottingham
  23   * @author     Neill Magill <neill.magill@nottingham.ac.uk>
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  use core\event\context_locked;
  28  use core\event\context_unlocked;
  29  
  30  defined('MOODLE_INTERNAL') || die();
  31  
  32  /**
  33   * Unit tests for the context_locked  and context_unlocked events.
  34   *
  35   * @package    core
  36   * @category   test
  37   * @copyright  2019 University of Nottingham
  38   * @author     Neill Magill <neill.magill@nottingham.ac.uk>
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class core_event_context_locked_testcase extends \advanced_testcase {
  42      /**
  43       * Locks an unlocked context and checks that a core\event\context_locked event is created.
  44       *
  45       * @param \context $context
  46       */
  47      protected function lock_context(\context $context) {
  48          self::assertFalse($context->is_locked());
  49  
  50          $locksink = $this->redirectEvents();
  51          $context->set_locked(true);
  52          // This second call should not create an event as the lock status has not changed.
  53          $context->set_locked(true);
  54          $lockevents = $locksink->get_events();
  55          $locksink->close();
  56  
  57          self::assertCount(1, $lockevents);
  58          self::assertContainsOnlyInstancesOf('core\event\context_locked', $lockevents);
  59          self::assertEquals($context->id, $lockevents[0]->objectid);
  60          $this->assertSame('context', $lockevents[0]->objecttable);
  61          $this->assertEquals($context, $lockevents[0]->get_context());
  62      }
  63  
  64      /**
  65       * Tests that events are created when contexts are locked and unlocked.
  66       */
  67      public function test_creation() {
  68          $this->resetAfterTest();
  69  
  70          $category = self::getDataGenerator()->create_category();
  71          $catcontext = \context_coursecat::instance($category->id);
  72          $course = self::getDataGenerator()->create_course(['category' => $category->id]);
  73          $coursecontext = \context_course::instance($course->id);
  74          $activitygenerator = self::getDataGenerator()->get_plugin_generator('mod_forum');
  75          $activity = $activitygenerator->create_instance(['course' => $course->id]);
  76          $activitycontext = \context_module::instance($activity->cmid);
  77  
  78          $this->lock_context($catcontext);
  79          $this->unlock_context($catcontext);
  80  
  81          $this->lock_context($coursecontext);
  82          $this->unlock_context($coursecontext);
  83  
  84          $this->lock_context($activitycontext);
  85          $this->unlock_context($activitycontext);
  86      }
  87  
  88      /**
  89       * Unlocks a locked context and checks that a core\event\context_unlocked event is created.
  90       *
  91       * @param \context $context
  92       */
  93      protected function unlock_context(\context $context) {
  94          self::assertTrue($context->is_locked());
  95  
  96          $unlocksink = $this->redirectEvents();
  97          $context->set_locked(false);
  98          // This second call should not create an event as the lock status has not changed.
  99          $context->set_locked(false);
 100          $unlockevents = $unlocksink->get_events();
 101          $unlocksink->close();
 102  
 103          self::assertCount(1, $unlockevents);
 104          self::assertContainsOnlyInstancesOf('core\event\context_unlocked', $unlockevents);
 105          self::assertEquals($context->id, $unlockevents[0]->objectid);
 106          $this->assertSame('context', $unlockevents[0]->objecttable);
 107          $this->assertEquals($context, $unlockevents[0]->get_context());
 108      }
 109  }