Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310]

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