MDL-50015 mod_imscp: New WS mod_imscp_view_imscp
[moodle.git] / mod / book / tests / events_test.php
CommitLineData
7f6e3eef
FM
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
26defined('MOODLE_INTERNAL') || die();
27global $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 */
37class mod_book_events_testcase extends advanced_testcase {
38
39 public function setUp() {
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');
ef2a9ece 50 $context = context_module::instance($book->cmid);
7f6e3eef
FM
51
52 $chapter = $bookgenerator->create_chapter(array('bookid' => $book->id));
53
ef2a9ece 54 $event = \mod_book\event\chapter_created::create_from_chapter($book, $context, $chapter);
7f6e3eef
FM
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);
74b63eae 65 $this->assertEquals(context_module::instance($book->cmid), $event->get_context());
7f6e3eef
FM
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);
623a32e5 70 $this->assertEventContextNotUsed($event);
7f6e3eef
FM
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');
ef2a9ece 80 $context = context_module::instance($book->cmid);
7f6e3eef
FM
81
82 $chapter = $bookgenerator->create_chapter(array('bookid' => $book->id));
83
ef2a9ece 84 $event = \mod_book\event\chapter_updated::create_from_chapter($book, $context, $chapter);
7f6e3eef
FM
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);
74b63eae 95 $this->assertEquals(context_module::instance($book->cmid), $event->get_context());
7f6e3eef
FM
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);
623a32e5 100 $this->assertEventContextNotUsed($event);
7f6e3eef
FM
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');
ef2a9ece 110 $context = context_module::instance($book->cmid);
7f6e3eef
FM
111
112 $chapter = $bookgenerator->create_chapter(array('bookid' => $book->id));
113
ef2a9ece
PS
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);
7f6e3eef
FM
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);
74b63eae 126 $this->assertEquals(context_module::instance($book->cmid), $event->get_context());
7f6e3eef
FM
127 $this->assertEquals($chapter->id, $event->objectid);
128 $this->assertEquals($chapter, $event->get_record_snapshot('book_chapters', $chapter->id));
ef2a9ece 129 $this->assertEventLegacyLogData($legacy, $event);
623a32e5 130 $this->assertEventContextNotUsed($event);
7f6e3eef
FM
131 }
132
1f2aeda9 133 public function test_course_module_instance_list_viewed() {
7f6e3eef
FM
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 );
1f2aeda9 141 $event = \mod_book\event\course_module_instance_list_viewed::create($params);
7f6e3eef
FM
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.
1f2aeda9 151 $this->assertInstanceOf('\mod_book\event\course_module_instance_list_viewed', $event);
7f6e3eef
FM
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);
623a32e5 155 $this->assertEventContextNotUsed($event);
7f6e3eef
FM
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);
623a32e5 184 $this->assertEventContextNotUsed($event);
7f6e3eef
FM
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');
ef2a9ece 194 $context = context_module::instance($book->cmid);
7f6e3eef
FM
195
196 $chapter = $bookgenerator->create_chapter(array('bookid' => $book->id));
197
ef2a9ece 198 $event = \mod_book\event\chapter_viewed::create_from_chapter($book, $context, $chapter);
7f6e3eef
FM
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);
623a32e5 214 $this->assertEventContextNotUsed($event);
7f6e3eef
FM
215 }
216
217}