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.
   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   * Events tests.
  19   *
  20   * @package tool_recyclebin
  21   * @category test
  22   * @copyright 2016 Mark Nelson <markn@moodle.com>
  23   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace tool_recyclebin\event;
  27  
  28  /**
  29   * Events tests class.
  30   *
  31   * @package tool_recyclebin
  32   * @category test
  33   * @copyright 2016 Mark Nelson <markn@moodle.com>
  34   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class events_test extends \advanced_testcase {
  37  
  38      /**
  39       * Test set up.
  40       *
  41       * This is executed before running any test in this file.
  42       */
  43      public function setUp(): void {
  44          $this->resetAfterTest();
  45  
  46          // We want the category and course bin to be enabled.
  47          set_config('categorybinenable', 1, 'tool_recyclebin');
  48          set_config('coursebinenable', 1, 'tool_recyclebin');
  49      }
  50  
  51      /**
  52       * Test the category bin item created event.
  53       */
  54      public function test_category_bin_item_created() {
  55          // Create a course.
  56          $course = $this->getDataGenerator()->create_course();
  57  
  58          // Trigger and capture the event.
  59          $sink = $this->redirectEvents();
  60          delete_course($course, false);
  61          $events = $sink->get_events();
  62          $event = reset($events);
  63          // Need the second event here, the first is backup created.
  64          $event = next($events);
  65  
  66          // Get the item from the recycle bin.
  67          $rb = new \tool_recyclebin\category_bin($course->category);
  68          $items = $rb->get_items();
  69          $item = reset($items);
  70  
  71          // Check that the event contains the expected values.
  72          $this->assertInstanceOf('\tooL_recyclebin\event\category_bin_item_created', $event);
  73          $this->assertEquals(\context_coursecat::instance($course->category), $event->get_context());
  74          $this->assertEquals($item->id, $event->objectid);
  75          $this->assertEventContextNotUsed($event);
  76      }
  77  
  78      /**
  79       * Test the category bin item deleted event.
  80       */
  81      public function test_category_bin_item_deleted() {
  82          // Create a course.
  83          $course = $this->getDataGenerator()->create_course();
  84  
  85          // Delete the course.
  86          delete_course($course, false);
  87  
  88          // Get the item from the recycle bin.
  89          $rb = new \tool_recyclebin\category_bin($course->category);
  90          $items = $rb->get_items();
  91          $item = reset($items);
  92  
  93          // Trigger and capture the event.
  94          $sink = $this->redirectEvents();
  95          $rb->delete_item($item);
  96          $events = $sink->get_events();
  97          $this->assertCount(1, $events);
  98          $event = reset($events);
  99  
 100          // Check that the event contains the expected values.
 101          $this->assertInstanceOf('\tooL_recyclebin\event\category_bin_item_deleted', $event);
 102          $this->assertEquals(\context_coursecat::instance($course->category), $event->get_context());
 103          $this->assertEquals($item->id, $event->objectid);
 104          $this->assertEventContextNotUsed($event);
 105      }
 106  
 107      /**
 108       * Test the category bin item restored event.
 109       */
 110      public function test_category_bin_item_restored() {
 111          // Create a course.
 112          $course = $this->getDataGenerator()->create_course();
 113  
 114          // Delete the course.
 115          delete_course($course, false);
 116  
 117          // Get the item from the recycle bin.
 118          $rb = new \tool_recyclebin\category_bin($course->category);
 119          $items = $rb->get_items();
 120          $item = reset($items);
 121  
 122          // Trigger and capture the event.
 123          $sink = $this->redirectEvents();
 124          $rb->restore_item($item);
 125          $events = $sink->get_events();
 126          $event = $events[count($events) - 2];
 127  
 128          // Check that the event contains the expected values.
 129          $this->assertInstanceOf('\tooL_recyclebin\event\category_bin_item_restored', $event);
 130          $this->assertEquals(\context_coursecat::instance($course->category), $event->get_context());
 131          $this->assertEquals($item->id, $event->objectid);
 132          $this->assertEventContextNotUsed($event);
 133      }
 134  
 135      /**
 136       * Test the course bin item created event.
 137       */
 138      public function test_course_bin_item_created() {
 139          // Create a course.
 140          $course = $this->getDataGenerator()->create_course();
 141  
 142          // Create the assignment.
 143          $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
 144          $instance = $generator->create_instance(array('course' => $course->id));
 145  
 146          // Trigger and capture the event.
 147          $sink = $this->redirectEvents();
 148          course_delete_module($instance->cmid);
 149          $events = $sink->get_events();
 150          $event = reset($events);
 151  
 152          // Get the item from the recycle bin.
 153          $rb = new \tool_recyclebin\course_bin($course->id);
 154          $items = $rb->get_items();
 155          $item = reset($items);
 156  
 157          // Check that the event contains the expected values.
 158          $this->assertInstanceOf('\tooL_recyclebin\event\course_bin_item_created', $event);
 159          $this->assertEquals(\context_course::instance($course->id), $event->get_context());
 160          $this->assertEquals($item->id, $event->objectid);
 161          $this->assertEventContextNotUsed($event);
 162      }
 163  
 164      /**
 165       * Test the course bin item deleted event.
 166       */
 167      public function test_course_bin_item_deleted() {
 168          // Create a course.
 169          $course = $this->getDataGenerator()->create_course();
 170  
 171          // Create the assignment.
 172          $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
 173          $instance = $generator->create_instance(array('course' => $course->id));
 174  
 175          // Delete the module.
 176          course_delete_module($instance->cmid);
 177  
 178          // Get the item from the recycle bin.
 179          $rb = new \tool_recyclebin\course_bin($course->id);
 180          $items = $rb->get_items();
 181          $item = reset($items);
 182  
 183          // Trigger and capture the event.
 184          $sink = $this->redirectEvents();
 185          $rb->delete_item($item);
 186          $events = $sink->get_events();
 187          $this->assertCount(1, $events);
 188          $event = reset($events);
 189  
 190          // Check that the event contains the expected values.
 191          $this->assertInstanceOf('\tooL_recyclebin\event\course_bin_item_deleted', $event);
 192          $this->assertEquals(\context_course::instance($course->id), $event->get_context());
 193          $this->assertEquals($item->id, $event->objectid);
 194          $this->assertEventContextNotUsed($event);
 195      }
 196  
 197      /**
 198       * Test the course bin item restored event.
 199       */
 200      public function test_course_bin_item_restored() {
 201          // Create a course.
 202          $course = $this->getDataGenerator()->create_course();
 203  
 204          // Create the assignment.
 205          $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
 206          $instance = $generator->create_instance(array('course' => $course->id));
 207  
 208          course_delete_module($instance->cmid);
 209  
 210          // Get the item from the recycle bin.
 211          $rb = new \tool_recyclebin\course_bin($course->id);
 212          $items = $rb->get_items();
 213          $item = reset($items);
 214  
 215          // Trigger and capture the event.
 216          $sink = $this->redirectEvents();
 217          $rb->restore_item($item);
 218          $events = $sink->get_events();
 219          $eventscount = 0;
 220  
 221          foreach ($events as $event) {
 222              if ($event instanceof \tooL_recyclebin\event\course_bin_item_restored) {
 223                  // Check that the event contains the expected values.
 224                  $this->assertEquals(\context_course::instance($course->id), $event->get_context());
 225                  $this->assertEquals($item->id, $event->objectid);
 226                  $this->assertEventContextNotUsed($event);
 227                  $eventscount++;
 228              }
 229          }
 230          // Only one course_bin_item_restored event should be triggered.
 231          $this->assertEquals(1, $eventscount);
 232      }
 233  }