MDL-40919 mod_survey: replaced add_to_log with events
[moodle.git] / mod / survey / tests / events_test.php
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/>.
17 /**
18  * Events tests.
19  *
20  * @package    mod_survey
21  * @copyright  2014 Rajesh Taneja <rajesh@moodle.com>
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 defined('MOODLE_INTERNAL') || die();
27 /**
28  * Events tests class.
29  *
30  * @package    mod_survey
31  * @copyright  2014 Rajesh Taneja <rajesh@moodle.com>
32  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33  */
34 class mod_survey_events_testcase extends advanced_testcase {
36     /**
37      * Setup.
38      */
39     public function setUp() {
40         $this->resetAfterTest();
41     }
43     /**
44      * Test report downloaded event.
45      */
46     public function test_report_downloaded() {
47         // There is no proper API to call to generate chapters for a book, so what we are
48         // doing here is simply making sure that the events returns the right information.
50         $course = $this->getDataGenerator()->create_course();
51         $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course->id));
53         $params = array(
54             'objectid' => $survey->id,
55             'context' => context_module::instance($survey->cmid),
56             'courseid' => $course->id,
57             'other' => array('type' => 'xls')
58         );
59         $event = \mod_survey\event\report_downloaded::create($params);
61         // Triggering and capturing the event.
62         $sink = $this->redirectEvents();
63         $event->trigger();
64         $events = $sink->get_events();
65         $this->assertCount(1, $events);
66         $event = reset($events);
68         // Checking that the event contains the expected values.
69         $this->assertInstanceOf('\mod_survey\event\report_downloaded', $event);
70         $this->assertEquals(context_module::instance($survey->cmid), $event->get_context());
71         $this->assertEquals($survey->id, $event->objectid);
72         $url = new moodle_url('/mod/survey/download.php', array('id' => $survey->cmid, 'type' => 'xls'));
73         $expected = array($course->id, "survey", "download", $url->out(), $survey->id, $survey->cmid);
74         $this->assertEventLegacyLogData($expected, $event);
75         $this->assertEventContextNotUsed($event);
76     }
78     /**
79      * Test report viewed event.
80      */
81     public function test_report_viewed() {
82         // There is no proper API to call to generate chapters for a book, so what we are
83         // doing here is simply making sure that the events returns the right information.
85         $course = $this->getDataGenerator()->create_course();
86         $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course->id));
88         $params = array(
89             'objectid' => $survey->id,
90             'context' => context_module::instance($survey->cmid),
91             'courseid' => $course->id
92         );
93         $event = \mod_survey\event\report_viewed::create($params);
95         // Triggering and capturing the event.
96         $sink = $this->redirectEvents();
97         $event->trigger();
98         $events = $sink->get_events();
99         $this->assertCount(1, $events);
100         $event = reset($events);
102         // Checking that the event contains the expected values.
103         $this->assertInstanceOf('\mod_survey\event\report_viewed', $event);
104         $this->assertEquals(context_module::instance($survey->cmid), $event->get_context());
105         $this->assertEquals($survey->id, $event->objectid);
106         $expected = array($course->id, "survey", "view report", 'report.php?id=' . $survey->cmid, $survey->id, $survey->cmid);
107         $this->assertEventLegacyLogData($expected, $event);
108         $this->assertEventContextNotUsed($event);
109     }
111     /**
112      * Test response submitted event.
113      */
114     public function test_response_submitted() {
115         // There is no proper API to call to generate chapters for a book, so what we are
116         // doing here is simply making sure that the events returns the right information.
118         $course = $this->getDataGenerator()->create_course();
119         $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course->id));
121         $params = array(
122             'context' => context_module::instance($survey->cmid),
123             'courseid' => $course->id,
124             'other' => array('surveyid' => $survey->id)
125         );
126         $event = \mod_survey\event\response_submitted::create($params);
128         // Triggering and capturing the event.
129         $sink = $this->redirectEvents();
130         $event->trigger();
131         $events = $sink->get_events();
132         $this->assertCount(1, $events);
133         $event = reset($events);
135         // Checking that the event contains the expected values.
136         $this->assertInstanceOf('\mod_survey\event\response_submitted', $event);
137         $this->assertEquals(context_module::instance($survey->cmid), $event->get_context());
138         $this->assertEquals($survey->id, $event->other['surveyid']);
139         $expected = array($course->id, "survey", "submit", 'view.php?id=' . $survey->cmid, $survey->id, $survey->cmid);
140         $this->assertEventLegacyLogData($expected, $event);
141         $this->assertEventContextNotUsed($event);
142     }