Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 402] [Versions 400 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  /**
  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          $expected = array($course->id, 'book', 'add chapter', 'view.php?id='.$book->cmid.'&chapterid='.$chapter->id,
  67              $chapter->id, $book->cmid);
  68          $this->assertEventLegacyLogData($expected, $event);
  69          $this->assertEventContextNotUsed($event);
  70      }
  71  
  72      public function test_chapter_updated() {
  73          // There is no proper API to call to generate chapters for a book, so what we are
  74          // doing here is simply making sure that the events returns the right information.
  75  
  76          $course = $this->getDataGenerator()->create_course();
  77          $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
  78          $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
  79          $context = \context_module::instance($book->cmid);
  80  
  81          $chapter = $bookgenerator->create_chapter(array('bookid' => $book->id));
  82  
  83          $event = \mod_book\event\chapter_updated::create_from_chapter($book, $context, $chapter);
  84  
  85          // Triggering and capturing the event.
  86          $sink = $this->redirectEvents();
  87          $event->trigger();
  88          $events = $sink->get_events();
  89          $this->assertCount(1, $events);
  90          $event = reset($events);
  91  
  92          // Checking that the event contains the expected values.
  93          $this->assertInstanceOf('\mod_book\event\chapter_updated', $event);
  94          $this->assertEquals(\context_module::instance($book->cmid), $event->get_context());
  95          $this->assertEquals($chapter->id, $event->objectid);
  96          $expected = array($course->id, 'book', 'update chapter', 'view.php?id='.$book->cmid.'&chapterid='.$chapter->id,
  97              $chapter->id, $book->cmid);
  98          $this->assertEventLegacyLogData($expected, $event);
  99          $this->assertEventContextNotUsed($event);
 100      }
 101  
 102      public function test_chapter_deleted() {
 103          // There is no proper API to call to delete chapters for a book, so what we are
 104          // doing here is simply making sure that the events returns the right information.
 105  
 106          $course = $this->getDataGenerator()->create_course();
 107          $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
 108          $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
 109          $context = \context_module::instance($book->cmid);
 110  
 111          $chapter = $bookgenerator->create_chapter(array('bookid' => $book->id));
 112  
 113          $event = \mod_book\event\chapter_deleted::create_from_chapter($book, $context, $chapter);
 114          $legacy = array($course->id, 'book', 'update', 'view.php?id='.$book->cmid, $book->id, $book->cmid);
 115  
 116          // Triggering and capturing the event.
 117          $sink = $this->redirectEvents();
 118          $event->trigger();
 119          $events = $sink->get_events();
 120          $this->assertCount(1, $events);
 121          $event = reset($events);
 122  
 123          // Checking that the event contains the expected values.
 124          $this->assertInstanceOf('\mod_book\event\chapter_deleted', $event);
 125          $this->assertEquals(\context_module::instance($book->cmid), $event->get_context());
 126          $this->assertEquals($chapter->id, $event->objectid);
 127          $this->assertEquals($chapter, $event->get_record_snapshot('book_chapters', $chapter->id));
 128          $this->assertEventLegacyLogData($legacy, $event);
 129          $this->assertEventContextNotUsed($event);
 130      }
 131  
 132      public function test_course_module_instance_list_viewed() {
 133          // There is no proper API to call to trigger this event, so what we are
 134          // doing here is simply making sure that the events returns the right information.
 135  
 136          $course = $this->getDataGenerator()->create_course();
 137          $params = array(
 138              'context' => \context_course::instance($course->id)
 139          );
 140          $event = \mod_book\event\course_module_instance_list_viewed::create($params);
 141  
 142          // Triggering and capturing the event.
 143          $sink = $this->redirectEvents();
 144          $event->trigger();
 145          $events = $sink->get_events();
 146          $this->assertCount(1, $events);
 147          $event = reset($events);
 148  
 149          // Checking that the event contains the expected values.
 150          $this->assertInstanceOf('\mod_book\event\course_module_instance_list_viewed', $event);
 151          $this->assertEquals(\context_course::instance($course->id), $event->get_context());
 152          $expected = array($course->id, 'book', 'view all', 'index.php?id='.$course->id, '');
 153          $this->assertEventLegacyLogData($expected, $event);
 154          $this->assertEventContextNotUsed($event);
 155      }
 156  
 157      public function test_course_module_viewed() {
 158          // There is no proper API to call to trigger this event, so what we are
 159          // doing here is simply making sure that the events returns the right information.
 160  
 161          $course = $this->getDataGenerator()->create_course();
 162          $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
 163  
 164          $params = array(
 165              'context' => \context_module::instance($book->cmid),
 166              'objectid' => $book->id
 167          );
 168          $event = \mod_book\event\course_module_viewed::create($params);
 169  
 170          // Triggering and capturing the event.
 171          $sink = $this->redirectEvents();
 172          $event->trigger();
 173          $events = $sink->get_events();
 174          $this->assertCount(1, $events);
 175          $event = reset($events);
 176  
 177          // Checking that the event contains the expected values.
 178          $this->assertInstanceOf('\mod_book\event\course_module_viewed', $event);
 179          $this->assertEquals(\context_module::instance($book->cmid), $event->get_context());
 180          $this->assertEquals($book->id, $event->objectid);
 181          $expected = array($course->id, 'book', 'view', 'view.php?id=' . $book->cmid, $book->id, $book->cmid);
 182          $this->assertEventLegacyLogData($expected, $event);
 183          $this->assertEventContextNotUsed($event);
 184      }
 185  
 186      public function test_chapter_viewed() {
 187          // There is no proper API to call to trigger this event, so what we are
 188          // doing here is simply making sure that the events returns the right information.
 189  
 190          $course = $this->getDataGenerator()->create_course();
 191          $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
 192          $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
 193          $context = \context_module::instance($book->cmid);
 194  
 195          $chapter = $bookgenerator->create_chapter(array('bookid' => $book->id));
 196  
 197          $event = \mod_book\event\chapter_viewed::create_from_chapter($book, $context, $chapter);
 198  
 199          // Triggering and capturing the event.
 200          $sink = $this->redirectEvents();
 201          $event->trigger();
 202          $events = $sink->get_events();
 203          $this->assertCount(1, $events);
 204          $event = reset($events);
 205  
 206          // Checking that the event contains the expected values.
 207          $this->assertInstanceOf('\mod_book\event\chapter_viewed', $event);
 208          $this->assertEquals(\context_module::instance($book->cmid), $event->get_context());
 209          $this->assertEquals($chapter->id, $event->objectid);
 210          $expected = array($course->id, 'book', 'view chapter', 'view.php?id=' . $book->cmid . '&amp;chapterid=' .
 211              $chapter->id, $chapter->id, $book->cmid);
 212          $this->assertEventLegacyLogData($expected, $event);
 213          $this->assertEventContextNotUsed($event);
 214      }
 215  
 216  }