Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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    tool_monitor
21
 * @category   test
22
 * @copyright  2014 Mark Nelson <markn@moodle.com>
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
namespace tool_monitor\event;
27
 
28
/**
29
 * Tests that the tool_monitor events are valid and triggered correctly.
30
 */
1441 ariadna 31
final class events_test extends \advanced_testcase {
1 efrain 32
 
33
    /**
34
     * Tests set up.
35
     */
36
    public function setUp(): void {
1441 ariadna 37
        parent::setUp();
1 efrain 38
        set_config('enablemonitor', 1, 'tool_monitor');
39
        $this->resetAfterTest();
40
    }
41
 
42
    /**
43
     * Test the rule created event.
44
     */
11 efrain 45
    public function test_rule_created(): void {
1 efrain 46
        // Create the items we need to create a rule.
47
        $course = $this->getDataGenerator()->create_course();
48
        $user = $this->getDataGenerator()->create_user();
49
 
50
        // Create the variables for the rule we want to create.
51
        $ruledata = new \stdClass();
52
        $ruledata->userid = $user->id;
53
        $ruledata->courseid = $course->id;
54
        $ruledata->plugin = 'mod_assign';
55
        $ruledata->eventname = '\mod_assign\event\submission_viewed';
56
        $ruledata->description = 'Rule description';
57
        $ruledata->descriptionformat = FORMAT_HTML;
58
        $ruledata->template = 'A message template';
59
        $ruledata->templateformat = FORMAT_HTML;
60
        $ruledata->frequency = 1;
61
        $ruledata->timewindow = 60;
62
 
63
        // Trigger and capture the event.
64
        $sink = $this->redirectEvents();
65
        $rule = \tool_monitor\rule_manager::add_rule($ruledata);
66
        $events = $sink->get_events();
67
        $this->assertCount(1, $events);
68
        $event = reset($events);
69
 
70
        // Confirm that the event contains the expected values.
71
        $this->assertInstanceOf('\tool_monitor\event\rule_created', $event);
72
        $this->assertEquals(\context_course::instance($course->id), $event->get_context());
73
        $this->assertEquals($rule->id, $event->objectid);
74
        $this->assertEventContextNotUsed($event);
75
 
76
        // Now let's add a system rule (courseid = 0).
77
        $ruledata->courseid = 0;
78
 
79
        // Trigger and capture the event.
80
        $sink = $this->redirectEvents();
81
        \tool_monitor\rule_manager::add_rule($ruledata);
82
        $events = $sink->get_events();
83
        $this->assertCount(1, $events);
84
        $event = reset($events);
85
 
86
        // Confirm that the event uses the system context.
87
        $this->assertInstanceOf('\tool_monitor\event\rule_created', $event);
88
        $this->assertEquals(\context_system::instance(), $event->get_context());
89
    }
90
 
91
    /**
92
     * Test the rule updated event.
93
     */
11 efrain 94
    public function test_rule_updated(): void {
1 efrain 95
        // Create the items we need.
96
        $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
97
        $course = $this->getDataGenerator()->create_course();
98
 
99
        // Create the rule we are going to update.
100
        $createrule = new \stdClass();
101
        $createrule->courseid = $course->id;
102
        $rule = $monitorgenerator->create_rule($createrule);
103
 
104
        // Trigger and capture the event.
105
        $sink = $this->redirectEvents();
106
        $updaterule = new \stdClass();
107
        $updaterule->id = $rule->id;
108
        \tool_monitor\rule_manager::update_rule($updaterule);
109
        $events = $sink->get_events();
110
        $this->assertCount(1, $events);
111
        $event = reset($events);
112
 
113
        // Confirm that the event contains the expected values.
114
        $this->assertInstanceOf('\tool_monitor\event\rule_updated', $event);
115
        $this->assertEquals(\context_course::instance($course->id), $event->get_context());
116
        $this->assertEquals($rule->id, $event->objectid);
117
        $this->assertEventContextNotUsed($event);
118
 
119
        // Now let's update a system rule (courseid = 0).
120
        $createrule->courseid = 0;
121
        $rule = $monitorgenerator->create_rule($createrule);
122
 
123
        // Trigger and capture the event.
124
        $sink = $this->redirectEvents();
125
        $updaterule = new \stdClass();
126
        $updaterule->id = $rule->id;
127
        \tool_monitor\rule_manager::update_rule($updaterule);
128
        $events = $sink->get_events();
129
        $this->assertCount(1, $events);
130
        $event = reset($events);
131
 
132
        // Confirm that the event uses the system context.
133
        $this->assertInstanceOf('\tool_monitor\event\rule_updated', $event);
134
        $this->assertEquals(\context_system::instance(), $event->get_context());
135
    }
136
 
137
    /**
138
     * Test the rule deleted event.
139
     */
11 efrain 140
    public function test_rule_deleted(): void {
1 efrain 141
        // Create the items we need.
142
        $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
143
        $course = $this->getDataGenerator()->create_course();
144
 
145
        // Create the rule we are going to delete.
146
        $createrule = new \stdClass();
147
        $createrule->courseid = $course->id;
148
        $rule = $monitorgenerator->create_rule($createrule);
149
 
150
        // Trigger and capture the event.
151
        $sink = $this->redirectEvents();
152
        \tool_monitor\rule_manager::delete_rule($rule->id);
153
        $events = $sink->get_events();
154
        $this->assertCount(1, $events);
155
        $event = reset($events);
156
 
157
        // Confirm that the event contains the expected values.
158
        $this->assertInstanceOf('\tool_monitor\event\rule_deleted', $event);
159
        $this->assertEquals(\context_course::instance($course->id), $event->get_context());
160
        $this->assertEquals($rule->id, $event->objectid);
161
        $this->assertEventContextNotUsed($event);
162
 
163
        // Now let's delete a system rule (courseid = 0).
164
        $createrule = new \stdClass();
165
        $createrule->courseid = 0;
166
        $rule = $monitorgenerator->create_rule($createrule);
167
 
168
        // Trigger and capture the event.
169
        $sink = $this->redirectEvents();
170
        \tool_monitor\rule_manager::delete_rule($rule->id);
171
        $events = $sink->get_events();
172
        $this->assertCount(1, $events);
173
        $event = reset($events);
174
 
175
        // Confirm that the event uses the system context.
176
        $this->assertInstanceOf('\tool_monitor\event\rule_deleted', $event);
177
        $this->assertEquals(\context_system::instance(), $event->get_context());
178
    }
179
 
180
    /**
181
     * Test the subscription created event.
182
     */
11 efrain 183
    public function test_subscription_created(): void {
1 efrain 184
        // Create the items we need to test this.
185
        $user = $this->getDataGenerator()->create_user();
186
        $course = $this->getDataGenerator()->create_course();
187
        $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
188
 
189
        // Create a rule to subscribe to.
190
        $rule = $monitorgenerator->create_rule();
191
 
192
        // Trigger and capture the event.
193
        $sink = $this->redirectEvents();
194
        $subscriptionid = \tool_monitor\subscription_manager::create_subscription($rule->id, $course->id, 0, $user->id);
195
        $events = $sink->get_events();
196
        $this->assertCount(1, $events);
197
        $event = reset($events);
198
 
199
        // Confirm that the event contains the expected values.
200
        $this->assertInstanceOf('\tool_monitor\event\subscription_created', $event);
201
        $this->assertEquals(\context_course::instance($course->id), $event->get_context());
202
        $this->assertEquals($subscriptionid, $event->objectid);
203
        $this->assertEventContextNotUsed($event);
204
 
205
        // Create a system subscription - trigger and capture the event.
206
        $sink = $this->redirectEvents();
207
        \tool_monitor\subscription_manager::create_subscription($rule->id, 0, 0, $user->id);
208
        $events = $sink->get_events();
209
        $this->assertCount(1, $events);
210
        $event = reset($events);
211
 
212
        // Confirm that the event uses the system context.
213
        $this->assertInstanceOf('\tool_monitor\event\subscription_created', $event);
214
        $this->assertEquals(\context_system::instance(), $event->get_context());
215
    }
216
 
217
    /**
218
     * Test the subscription deleted event.
219
     */
11 efrain 220
    public function test_subscription_deleted(): void {
1 efrain 221
        // Create the items we need to test this.
222
        $user = $this->getDataGenerator()->create_user();
223
        $course = $this->getDataGenerator()->create_course();
224
        $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
225
 
226
        // Create a rule to subscribe to.
227
        $rule = $monitorgenerator->create_rule();
228
 
229
        $sub = new \stdClass();
230
        $sub->courseid = $course->id;
231
        $sub->userid = $user->id;
232
        $sub->ruleid = $rule->id;
233
 
234
        // Create the subscription we are going to delete.
235
        $subscription = $monitorgenerator->create_subscription($sub);
236
 
237
        // Trigger and capture the event.
238
        $sink = $this->redirectEvents();
239
        \tool_monitor\subscription_manager::delete_subscription($subscription->id, false);
240
        $events = $sink->get_events();
241
        $this->assertCount(1, $events);
242
        $event = reset($events);
243
 
244
        // Confirm that the event contains the expected values.
245
        $this->assertInstanceOf('\tool_monitor\event\subscription_deleted', $event);
246
        $this->assertEquals(\context_course::instance($course->id), $event->get_context());
247
        $this->assertEquals($subscription->id, $event->objectid);
248
        $this->assertEventContextNotUsed($event);
249
 
250
        // Now let's delete a system subscription.
251
        $sub = new \stdClass();
252
        $sub->courseid = 0;
253
        $sub->userid = $user->id;
254
        $sub->ruleid = $rule->id;
255
 
256
        // Create the subscription we are going to delete.
257
        $subscription = $monitorgenerator->create_subscription($sub);
258
 
259
        // Trigger and capture the event.
260
        $sink = $this->redirectEvents();
261
        \tool_monitor\subscription_manager::delete_subscription($subscription->id, false);
262
        $events = $sink->get_events();
263
        $this->assertCount(1, $events);
264
        $event = reset($events);
265
 
266
        // Confirm that the event uses the system context.
267
        $this->assertInstanceOf('\tool_monitor\event\subscription_deleted', $event);
268
        $this->assertEquals(\context_system::instance(), $event->get_context());
269
 
270
        // Now, create a bunch of subscriptions for the rule we created.
271
        $subids = array();
272
        $sub->courseid = $course->id;
273
        for ($i = 1; $i <= 10; $i++) {
274
            $sub->userid = $i;
275
            $subscription = $monitorgenerator->create_subscription($sub);
276
            $subids[$subscription->id] = $subscription;
277
        }
278
 
279
        // Trigger and capture the events.
280
        $sink = $this->redirectEvents();
281
        \tool_monitor\subscription_manager::remove_all_subscriptions_for_rule($rule->id);
282
        $events = $sink->get_events();
283
 
284
        // Check that there were 10 events in total.
285
        $this->assertCount(10, $events);
286
 
287
        // Get all the events and ensure they are valid.
288
        foreach ($events as $event) {
289
            $this->assertInstanceOf('\tool_monitor\event\subscription_deleted', $event);
290
            $this->assertEquals(\context_course::instance($course->id), $event->get_context());
291
            $this->assertEventContextNotUsed($event);
292
            $this->assertArrayHasKey($event->objectid, $subids);
293
            unset($subids[$event->objectid]);
294
        }
295
 
296
        // We should have found all the subscriptions.
297
        $this->assertEmpty($subids);
298
    }
299
 
300
    /**
301
     * Test the subscription criteria met event.
302
     */
11 efrain 303
    public function test_subscription_criteria_met(): void {
1 efrain 304
        // Create the items we need to test this.
305
        $user = $this->getDataGenerator()->create_user();
306
        $course = $this->getDataGenerator()->create_course();
307
        $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
308
        $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
309
        $chapter = $bookgenerator->create_chapter(array('bookid' => $book->id));
310
        $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
311
 
312
        // Create a rule we want to subscribe to.
313
        $rule = new \stdClass();
314
        $rule->userid = $user->id;
315
        $rule->courseid = $course->id;
316
        $rule->plugin = 'mod_book';
317
        $rule->eventname = '\mod_book\event\chapter_viewed';
318
        $rule->frequency = 1;
319
        $rule->timewindow = 60;
320
        $rule = $monitorgenerator->create_rule($rule);
321
 
322
        // Create the subscription.
323
        $sub = new \stdClass();
324
        $sub->courseid = $course->id;
325
        $sub->userid = $user->id;
326
        $sub->ruleid = $rule->id;
327
        $monitorgenerator->create_subscription($sub);
328
 
329
        // Now create the \mod_book\event\chapter_viewed event we are listening for.
330
        $context = \context_module::instance($book->cmid);
331
        $event = \mod_book\event\chapter_viewed::create_from_chapter($book, $context, $chapter);
332
 
333
        // Trigger and capture the event.
334
        $sink = $this->redirectEvents();
335
        \tool_monitor\eventobservers::process_event($event);
336
        $events = $sink->get_events();
337
        $this->assertCount(1, $events);
338
        $event = reset($events);
339
 
340
        // Confirm that the event contains the expected values.
341
        $this->assertInstanceOf('\tool_monitor\event\subscription_criteria_met', $event);
342
        $this->assertEquals(\context_course::instance($course->id), $event->get_context());
343
        $this->assertEventContextNotUsed($event);
344
    }
345
}