Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 311 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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    mod_book
  21   * @category   phpunit
  22   * @copyright  2013 Frédéric Massart
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace mod_book\event;
  27  
  28  /**
  29   * Events tests class.
  30   *
  31   * @package    mod_book
  32   * @category   phpunit
  33   * @copyright  2013 Frédéric Massart
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class events_test extends \advanced_testcase {
  37  
  38      public function setUp(): void {
  39          $this->resetAfterTest();
  40      }
  41  
  42      public function test_chapter_created() {
  43          // There is no proper API to call to generate chapters for a book, so what we are
  44          // doing here is simply making sure that the events returns the right information.
  45  
  46          $course = $this->getDataGenerator()->create_course();
  47          $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
  48          $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
  49          $context = \context_module::instance($book->cmid);
  50  
  51          $chapter = $bookgenerator->create_chapter(array('bookid' => $book->id));
  52  
  53          $event = \mod_book\event\chapter_created::create_from_chapter($book, $context, $chapter);
  54  
  55          // Triggering and capturing the event.
  56          $sink = $this->redirectEvents();
  57          $event->trigger();
  58          $events = $sink->get_events();
  59          $this->assertCount(1, $events);
  60          $event = reset($events);
  61  
  62          // Checking that the event contains the expected values.
  63          $this->assertInstanceOf('\mod_book\event\chapter_created', $event);
  64          $this->assertEquals(\context_module::instance($book->cmid), $event->get_context());
  65          $this->assertEquals($chapter->id, $event->objectid);
  66      }
  67  
  68      public function test_chapter_updated() {
  69          // There is no proper API to call to generate chapters for a book, so what we are
  70          // doing here is simply making sure that the events returns the right information.
  71  
  72          $course = $this->getDataGenerator()->create_course();
  73          $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
  74          $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
  75          $context = \context_module::instance($book->cmid);
  76  
  77          $chapter = $bookgenerator->create_chapter(array('bookid' => $book->id));
  78  
  79          $event = \mod_book\event\chapter_updated::create_from_chapter($book, $context, $chapter);
  80  
  81          // Triggering and capturing the event.
  82          $sink = $this->redirectEvents();
  83          $event->trigger();
  84          $events = $sink->get_events();
  85          $this->assertCount(1, $events);
  86          $event = reset($events);
  87  
  88          // Checking that the event contains the expected values.
  89          $this->assertInstanceOf('\mod_book\event\chapter_updated', $event);
  90          $this->assertEquals(\context_module::instance($book->cmid), $event->get_context());
  91          $this->assertEquals($chapter->id, $event->objectid);
  92      }
  93  
  94      public function test_chapter_deleted() {
  95          // There is no proper API to call to delete chapters for a book, so what we are
  96          // doing here is simply making sure that the events returns the right information.
  97  
  98          $course = $this->getDataGenerator()->create_course();
  99          $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
 100          $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
 101          $context = \context_module::instance($book->cmid);
 102  
 103          $chapter = $bookgenerator->create_chapter(array('bookid' => $book->id));
 104  
 105          $event = \mod_book\event\chapter_deleted::create_from_chapter($book, $context, $chapter);
 106          $legacy = array($course->id, 'book', 'update', 'view.php?id='.$book->cmid, $book->id, $book->cmid);
 107  
 108          // Triggering and capturing the event.
 109          $sink = $this->redirectEvents();
 110          $event->trigger();
 111          $events = $sink->get_events();
 112          $this->assertCount(1, $events);
 113          $event = reset($events);
 114  
 115          // Checking that the event contains the expected values.
 116          $this->assertInstanceOf('\mod_book\event\chapter_deleted', $event);
 117          $this->assertEquals(\context_module::instance($book->cmid), $event->get_context());
 118          $this->assertEquals($chapter->id, $event->objectid);
 119          $this->assertEquals($chapter, $event->get_record_snapshot('book_chapters', $chapter->id));
 120      }
 121  
 122      public function test_course_module_instance_list_viewed() {
 123          // There is no proper API to call to trigger this event, so what we are
 124          // doing here is simply making sure that the events returns the right information.
 125  
 126          $course = $this->getDataGenerator()->create_course();
 127          $params = array(
 128              'context' => \context_course::instance($course->id)
 129          );
 130          $event = \mod_book\event\course_module_instance_list_viewed::create($params);
 131  
 132          // Triggering and capturing the event.
 133          $sink = $this->redirectEvents();
 134          $event->trigger();
 135          $events = $sink->get_events();
 136          $this->assertCount(1, $events);
 137          $event = reset($events);
 138  
 139          // Checking that the event contains the expected values.
 140          $this->assertInstanceOf('\mod_book\event\course_module_instance_list_viewed', $event);
 141          $this->assertEquals(\context_course::instance($course->id), $event->get_context());
 142      }
 143  
 144      public function test_course_module_viewed() {
 145          // There is no proper API to call to trigger this event, so what we are
 146          // doing here is simply making sure that the events returns the right information.
 147  
 148          $course = $this->getDataGenerator()->create_course();
 149          $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
 150  
 151          $params = array(
 152              'context' => \context_module::instance($book->cmid),
 153              'objectid' => $book->id
 154          );
 155          $event = \mod_book\event\course_module_viewed::create($params);
 156  
 157          // Triggering and capturing the event.
 158          $sink = $this->redirectEvents();
 159          $event->trigger();
 160          $events = $sink->get_events();
 161          $this->assertCount(1, $events);
 162          $event = reset($events);
 163  
 164          // Checking that the event contains the expected values.
 165          $this->assertInstanceOf('\mod_book\event\course_module_viewed', $event);
 166          $this->assertEquals(\context_module::instance($book->cmid), $event->get_context());
 167          $this->assertEquals($book->id, $event->objectid);
 168      }
 169  
 170      public function test_chapter_viewed() {
 171          // There is no proper API to call to trigger this event, so what we are
 172          // doing here is simply making sure that the events returns the right information.
 173  
 174          $course = $this->getDataGenerator()->create_course();
 175          $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
 176          $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
 177          $context = \context_module::instance($book->cmid);
 178  
 179          $chapter = $bookgenerator->create_chapter(array('bookid' => $book->id));
 180  
 181          $event = \mod_book\event\chapter_viewed::create_from_chapter($book, $context, $chapter);
 182  
 183          // Triggering and capturing the event.
 184          $sink = $this->redirectEvents();
 185          $event->trigger();
 186          $events = $sink->get_events();
 187          $this->assertCount(1, $events);
 188          $event = reset($events);
 189  
 190          // Checking that the event contains the expected values.
 191          $this->assertInstanceOf('\mod_book\event\chapter_viewed', $event);
 192          $this->assertEquals(\context_module::instance($book->cmid), $event->get_context());
 193          $this->assertEquals($chapter->id, $event->objectid);
 194      }
 195  
 196  }