MDL-30431 behat: Fixed wiki behat
[moodle.git] / mod / lesson / tests / events_test.php
blobdda3ee935d793cc19fdb2dfb90dd709b470e2676
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.
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/>.
17 /**
18 * Events tests.
20 * @package mod_lesson
21 * @category test
22 * @copyright 2013 Mark Nelson <markn@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
30 require_once($CFG->dirroot.'/mod/lesson/locallib.php');
32 class mod_lesson_events_testcase extends advanced_testcase {
34 /** @var stdClass the course used for testing */
35 private $course;
37 /** @var lesson the lesson used for testing */
38 private $lesson;
40 /**
41 * Test set up.
43 * This is executed before running any test in this file.
45 public function setUp() {
46 $this->resetAfterTest();
48 $this->setAdminUser();
49 $this->course = $this->getDataGenerator()->create_course();
50 $lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $this->course->id));
52 // Convert to a lesson object.
53 $this->lesson = new lesson($lesson);
56 /**
57 * Test the essay attempt viewed event.
59 * There is no external API for viewing an essay attempt, so the unit test will simply
60 * create and trigger the event and ensure the legacy log data is returned as expected.
62 public function test_essay_attempt_viewed() {
63 // Create a essays list viewed event
64 $event = \mod_lesson\event\essay_attempt_viewed::create(array(
65 'objectid' => $this->lesson->id,
66 'relateduserid' => 3,
67 'context' => context_module::instance($this->lesson->properties()->cmid),
68 'courseid' => $this->course->id
69 ));
71 // Trigger and capture the event.
72 $sink = $this->redirectEvents();
73 $event->trigger();
74 $events = $sink->get_events();
75 $event = reset($events);
77 // Check that the event data is valid.
78 $this->assertInstanceOf('\mod_lesson\event\essay_attempt_viewed', $event);
79 $this->assertEquals(context_module::instance($this->lesson->properties()->cmid), $event->get_context());
80 $expected = array($this->course->id, 'lesson', 'view grade', 'essay.php?id=' . $this->lesson->properties()->cmid .
81 '&mode=grade&attemptid=1', get_string('manualgrading', 'lesson'), $this->lesson->properties()->cmid);
82 $this->assertEventLegacyLogData($expected, $event);
83 $this->assertEventContextNotUsed($event);
86 /**
87 * Test the highscore added event.
89 * There is no external API for adding a highscore, so the unit test will simply create
90 * and trigger the event and ensure the legacy log data is returned as expected.
92 public function test_highscore_added() {
93 global $DB;
95 // Create a highscore.
96 $newhighscore = new stdClass;
97 $newhighscore->lessonid = $this->lesson->id;
98 $newhighscore->userid = 3;
99 $newhighscore->gradeid = 70;
100 $newhighscore->nickname = 'noob';
102 $newhighscore->id = $DB->insert_record('lesson_high_scores', $newhighscore);
104 // Create a highscore added event.
105 $event = \mod_lesson\event\highscore_added::create(array(
106 'objectid' => $newhighscore->id,
107 'context' => context_module::instance($this->lesson->properties()->cmid),
108 'courseid' => $this->course->id,
109 'other' => array(
110 'lessonid' => $this->lesson->id,
111 'nickname' => 'noob'
115 // Trigger and capture the event.
116 $sink = $this->redirectEvents();
117 $event->trigger();
118 $events = $sink->get_events();
119 $event = reset($events);
121 // Check that the event data is valid.
122 $this->assertInstanceOf('\mod_lesson\event\highscore_added', $event);
123 $this->assertEquals(context_module::instance($this->lesson->properties()->cmid), $event->get_context());
124 $expected = array($this->course->id, 'lesson', 'update highscores', 'highscores.php?id=' . $this->lesson->properties()->cmid,
125 'noob', $this->lesson->properties()->cmid);
126 $this->assertEventLegacyLogData($expected, $event);
127 $this->assertEventContextNotUsed($event);
131 * Test the highscores viewed event.
133 * There is no external API for viewing highscores, so the unit test will simply create
134 * and trigger the event and ensure the legacy log data is returned as expected.
136 public function test_highscores_viewed() {
137 // Create a highscore viewed event.
138 $event = \mod_lesson\event\highscores_viewed::create(array(
139 'objectid' => $this->lesson->id,
140 'context' => context_module::instance($this->lesson->properties()->cmid),
141 'courseid' => $this->course->id
144 // Trigger and capture the event.
145 $sink = $this->redirectEvents();
146 $event->trigger();
147 $events = $sink->get_events();
148 $event = reset($events);
150 // Check that the event data is valid.
151 $this->assertInstanceOf('\mod_lesson\event\highscores_viewed', $event);
152 $this->assertEquals(context_module::instance($this->lesson->properties()->cmid), $event->get_context());
153 $expected = array($this->course->id, 'lesson', 'view highscores', 'highscores.php?id=' . $this->lesson->properties()->cmid,
154 $this->lesson->properties()->name, $this->lesson->properties()->cmid);
155 $this->assertEventLegacyLogData($expected, $event);
156 $this->assertEventContextNotUsed($event);
160 * Test the lesson started event.
162 public function test_lesson_started() {
163 // Trigger and capture the event.
164 $sink = $this->redirectEvents();
165 $this->lesson->start_timer();
166 $events = $sink->get_events();
167 $event = reset($events);
169 // Check that the event data is valid.
170 $this->assertInstanceOf('\mod_lesson\event\lesson_started', $event);
171 $this->assertEquals(context_module::instance($this->lesson->properties()->cmid), $event->get_context());
172 $expected = array($this->course->id, 'lesson', 'start', 'view.php?id=' . $this->lesson->properties()->cmid,
173 $this->lesson->properties()->id, $this->lesson->properties()->cmid);
174 $this->assertEventLegacyLogData($expected, $event);
175 $this->assertEventContextNotUsed($event);
179 * Test the lesson ended event.
181 public function test_lesson_ended() {
182 global $DB, $USER;
184 // Add a lesson timer so that stop_timer() does not complain.
185 $lessontimer = new stdClass();
186 $lessontimer->lessonid = $this->lesson->properties()->id;
187 $lessontimer->userid = $USER->id;
188 $lessontimer->startime = time();
189 $lessontimer->lessontime = time();
190 $DB->insert_record('lesson_timer', $lessontimer);
192 // Trigger and capture the event.
193 $sink = $this->redirectEvents();
194 $this->lesson->stop_timer();
195 $events = $sink->get_events();
196 $event = reset($events);
198 // Check that the event data is valid.
199 $this->assertInstanceOf('\mod_lesson\event\lesson_ended', $event);
200 $this->assertEquals(context_module::instance($this->lesson->properties()->cmid), $event->get_context());
201 $expected = array($this->course->id, 'lesson', 'end', 'view.php?id=' . $this->lesson->properties()->cmid,
202 $this->lesson->properties()->id, $this->lesson->properties()->cmid);
203 $this->assertEventLegacyLogData($expected, $event);
204 $this->assertEventContextNotUsed($event);
208 * Test the essay assessed event.
210 * There is no external API for assessing an essay, so the unit test will simply
211 * create and trigger the event and ensure the legacy log data is returned as expected.
213 public function test_essay_assessed() {
214 // Create an essay assessed event
215 $gradeid = 5;
216 $attemptid = 7;
217 $event = \mod_lesson\event\essay_assessed::create(array(
218 'objectid' => $gradeid,
219 'relateduserid' => 3,
220 'context' => context_module::instance($this->lesson->properties()->cmid),
221 'courseid' => $this->course->id,
222 'other' => array(
223 'lessonid' => $this->lesson->id,
224 'attemptid' => $attemptid
228 // Trigger and capture the event.
229 $sink = $this->redirectEvents();
230 $event->trigger();
231 $events = $sink->get_events();
232 $event = reset($events);
234 // Check that the event data is valid.
235 $this->assertInstanceOf('\mod_lesson\event\essay_assessed', $event);
236 $this->assertEquals(context_module::instance($this->lesson->properties()->cmid), $event->get_context());
237 $expected = array($this->course->id, 'lesson', 'update grade', 'essay.php?id=' . $this->lesson->properties()->cmid,
238 $this->lesson->name, $this->lesson->properties()->cmid);
239 $this->assertEventLegacyLogData($expected, $event);
240 $this->assertEventContextNotUsed($event);