Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 and 403]

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