[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/tool/recyclebin/tests/ -> events_test.php (source)

   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  defined('MOODLE_INTERNAL') || die();
  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 tool_recyclebin_events_testcase 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() {
  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  
  64          // Get the item from the recycle bin.
  65          $rb = new \tool_recyclebin\category_bin($course->category);
  66          $items = $rb->get_items();
  67          $item = reset($items);
  68  
  69          // Check that the event contains the expected values.
  70          $this->assertInstanceOf('\tooL_recyclebin\event\category_bin_item_created', $event);
  71          $this->assertEquals(context_coursecat::instance($course->category), $event->get_context());
  72          $this->assertEquals($item->id, $event->objectid);
  73          $this->assertEventContextNotUsed($event);
  74      }
  75  
  76      /**
  77       * Test the category bin item deleted event.
  78       */
  79      public function test_category_bin_item_deleted() {
  80          // Create a course.
  81          $course = $this->getDataGenerator()->create_course();
  82  
  83          // Delete the course.
  84          delete_course($course, false);
  85  
  86          // Get the item from the recycle bin.
  87          $rb = new \tool_recyclebin\category_bin($course->category);
  88          $items = $rb->get_items();
  89          $item = reset($items);
  90  
  91          // Trigger and capture the event.
  92          $sink = $this->redirectEvents();
  93          $rb->delete_item($item);
  94          $events = $sink->get_events();
  95          $this->assertCount(1, $events);
  96          $event = reset($events);
  97  
  98          // Check that the event contains the expected values.
  99          $this->assertInstanceOf('\tooL_recyclebin\event\category_bin_item_deleted', $event);
 100          $this->assertEquals(context_coursecat::instance($course->category), $event->get_context());
 101          $this->assertEquals($item->id, $event->objectid);
 102          $this->assertEventContextNotUsed($event);
 103      }
 104  
 105      /**
 106       * Test the category bin item restored event.
 107       */
 108      public function test_category_bin_item_restored() {
 109          // Create a course.
 110          $course = $this->getDataGenerator()->create_course();
 111  
 112          // Delete the course.
 113          delete_course($course, false);
 114  
 115          // Get the item from the recycle bin.
 116          $rb = new \tool_recyclebin\category_bin($course->category);
 117          $items = $rb->get_items();
 118          $item = reset($items);
 119  
 120          // Trigger and capture the event.
 121          $sink = $this->redirectEvents();
 122          $rb->restore_item($item);
 123          $events = $sink->get_events();
 124          $event = $events[6];
 125  
 126          // Check that the event contains the expected values.
 127          $this->assertInstanceOf('\tooL_recyclebin\event\category_bin_item_restored', $event);
 128          $this->assertEquals(context_coursecat::instance($course->category), $event->get_context());
 129          $this->assertEquals($item->id, $event->objectid);
 130          $this->assertEventContextNotUsed($event);
 131      }
 132  
 133      /**
 134       * Test the course bin item created event.
 135       */
 136      public function test_course_bin_item_created() {
 137          // Create a course.
 138          $course = $this->getDataGenerator()->create_course();
 139  
 140          // Create the assignment.
 141          $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
 142          $instance = $generator->create_instance(array('course' => $course->id));
 143  
 144          // Trigger and capture the event.
 145          $sink = $this->redirectEvents();
 146          course_delete_module($instance->cmid);
 147          $events = $sink->get_events();
 148          $event = reset($events);
 149  
 150          // Get the item from the recycle bin.
 151          $rb = new \tool_recyclebin\course_bin($course->id);
 152          $items = $rb->get_items();
 153          $item = reset($items);
 154  
 155          // Check that the event contains the expected values.
 156          $this->assertInstanceOf('\tooL_recyclebin\event\course_bin_item_created', $event);
 157          $this->assertEquals(context_course::instance($course->id), $event->get_context());
 158          $this->assertEquals($item->id, $event->objectid);
 159          $this->assertEventContextNotUsed($event);
 160      }
 161  
 162      /**
 163       * Test the course bin item deleted event.
 164       */
 165      public function test_course_bin_item_deleted() {
 166          // Create a course.
 167          $course = $this->getDataGenerator()->create_course();
 168  
 169          // Create the assignment.
 170          $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
 171          $instance = $generator->create_instance(array('course' => $course->id));
 172  
 173          // Delete the module.
 174          course_delete_module($instance->cmid);
 175  
 176          // Get the item from the recycle bin.
 177          $rb = new \tool_recyclebin\course_bin($course->id);
 178          $items = $rb->get_items();
 179          $item = reset($items);
 180  
 181          // Trigger and capture the event.
 182          $sink = $this->redirectEvents();
 183          $rb->delete_item($item);
 184          $events = $sink->get_events();
 185          $this->assertCount(1, $events);
 186          $event = reset($events);
 187  
 188          // Check that the event contains the expected values.
 189          $this->assertInstanceOf('\tooL_recyclebin\event\course_bin_item_deleted', $event);
 190          $this->assertEquals(context_course::instance($course->id), $event->get_context());
 191          $this->assertEquals($item->id, $event->objectid);
 192          $this->assertEventContextNotUsed($event);
 193      }
 194  
 195      /**
 196       * Test the course bin item restored event.
 197       */
 198      public function test_course_bin_item_restored() {
 199          // Create a course.
 200          $course = $this->getDataGenerator()->create_course();
 201  
 202          // Create the assignment.
 203          $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
 204          $instance = $generator->create_instance(array('course' => $course->id));
 205  
 206          course_delete_module($instance->cmid);
 207  
 208          // Get the item from the recycle bin.
 209          $rb = new \tool_recyclebin\course_bin($course->id);
 210          $items = $rb->get_items();
 211          $item = reset($items);
 212  
 213          // Trigger and capture the event.
 214          $sink = $this->redirectEvents();
 215          $rb->restore_item($item);
 216          $events = $sink->get_events();
 217          $event = reset($events);
 218  
 219          // Check that the event contains the expected values.
 220          $this->assertInstanceOf('\tooL_recyclebin\event\course_bin_item_restored', $event);
 221          $this->assertEquals(context_course::instance($course->id), $event->get_context());
 222          $this->assertEquals($item->id, $event->objectid);
 223          $this->assertEventContextNotUsed($event);
 224      }
 225  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1