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
namespace tool_monitor;
18
 
19
/**
20
 * Unit tests for the tool_monitor clean events task.
21
 * @since 3.2.0
22
 *
23
 * @package    tool_monitor
24
 * @category   test
25
 * @copyright  2016 Jake Dallimore <jrhdallimore@gmail.com>
26
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 */
1441 ariadna 28
final class task_check_subscriptions_test extends \advanced_testcase {
1 efrain 29
 
30
    private $course;
31
    private $user;
32
    private $rule;
33
    private $subscription;
34
    private $teacherrole;
35
    private $studentrole;
36
 
37
    /**
38
     * Test set up.
39
     */
40
    public function setUp(): void {
41
        global $DB;
1441 ariadna 42
        parent::setUp();
1 efrain 43
        set_config('enablemonitor', 1, 'tool_monitor');
44
        $this->resetAfterTest(true);
45
 
46
        // All tests defined herein need a user, course, rule and subscription, so set these up.
47
        $this->user = $this->getDataGenerator()->create_user();
48
        $this->course = $this->getDataGenerator()->create_course();
49
 
50
        $rule = new \stdClass();
51
        $rule->userid = 2; // Rule created by admin.
52
        $rule->courseid = $this->course->id;
53
        $rule->plugin = 'mod_book';
54
        $rule->eventname = '\mod_book\event\course_module_viewed';
55
        $rule->timewindow = 500;
56
        $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
57
        $this->rule = $monitorgenerator->create_rule($rule);
58
 
59
        $sub = new \stdClass();
60
        $sub->courseid = $this->course->id;
61
        $sub->userid = $this->user->id;
62
        $sub->ruleid = $this->rule->id;
63
        $this->subscription = $monitorgenerator->create_subscription($sub);
64
 
65
        // Also set up a student and a teacher role for use in some tests.
66
        $this->teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
67
        $this->studentrole = $DB->get_record('role', array('shortname' => 'student'));
68
    }
69
 
70
    /**
71
     * Reloads the subscription object from the DB.
72
     *
73
     * @return void.
74
     */
75
    private function reload_subscription() {
76
        global $DB;
77
        $sub = $DB->get_record('tool_monitor_subscriptions', array('id' => $this->subscription->id));
78
        $this->subscription = new \tool_monitor\subscription($sub);
79
    }
80
 
81
    /**
82
     * Test to confirm the task is named correctly.
83
     */
11 efrain 84
    public function test_task_name(): void {
1 efrain 85
        $task = new \tool_monitor\task\check_subscriptions();
86
        $this->assertEquals(get_string('taskchecksubscriptions', 'tool_monitor'), $task->get_name());
87
    }
88
 
89
    /**
90
     * Test to confirm that site level subscriptions are activated and deactivated according to system capabilities.
91
     */
11 efrain 92
    public function test_site_level_subscription(): void {
1 efrain 93
        // Create a site level subscription.
94
        $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
95
        $sub = new \stdClass();
96
        $sub->userid = $this->user->id;
97
        $sub->ruleid = $this->rule->id;
98
        $this->subscription = $monitorgenerator->create_subscription($sub);
99
 
100
        // Run the task.
101
        $task = new \tool_monitor\task\check_subscriptions();
102
        $task->execute();
103
 
104
        // The subscription should be inactive as the user doesn't have the capability. Pass in the id only to refetch the data.
105
        $this->reload_subscription();
106
        $this->assertEquals(false, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
107
 
108
        // Now, assign the user as a teacher role at system context.
109
        $this->getDataGenerator()->role_assign($this->teacherrole->id, $this->user->id, \context_system::instance());
110
 
111
        // Run the task.
112
        $task = new \tool_monitor\task\check_subscriptions();
113
        $task->execute();
114
 
115
        // The subscription should be active now. Pass in the id only to refetch the data.
116
        $this->reload_subscription();
117
        $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
118
    }
119
 
120
    /**
121
     * Test to confirm that if the module is disabled, no changes are made to active subscriptions.
122
     */
11 efrain 123
    public function test_module_disabled(): void {
1 efrain 124
        set_config('enablemonitor', 0, 'tool_monitor');
125
 
126
        // Subscription should be active to start with.
127
        $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
128
 
129
        // Run the task. Note, we never enrolled the user.
130
        $task = new \tool_monitor\task\check_subscriptions();
131
        $task->execute();
132
 
133
        // The subscription should still be active. Pass in the id only to refetch the data.
134
        $this->reload_subscription();
135
        $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
136
    }
137
 
138
    /**
139
     * Test to confirm an active, valid subscription stays active once the scheduled task is run.
140
     */
11 efrain 141
    public function test_active_unaffected(): void {
1 efrain 142
        // Enrol the user as a teacher. This role should have the required capability.
143
        $this->getDataGenerator()->enrol_user($this->user->id, $this->course->id, $this->teacherrole->id);
144
 
145
        // Subscription should be active to start with.
146
        $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
147
 
148
        // Run the task.
149
        $task = new \tool_monitor\task\check_subscriptions();
150
        $task->execute();
151
 
152
        // The subscription should still be active. Pass in the id only to refetch the data.
153
        $this->reload_subscription();
154
        $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
155
    }
156
 
157
    /**
158
     * Test to confirm that a subscription for a user without an enrolment to the course is made inactive.
159
     */
11 efrain 160
    public function test_course_enrolment(): void {
1 efrain 161
        // Subscription should be active until deactivated by the scheduled task. Remember, by default the test setup
162
        // doesn't enrol the user, so the first run of the task should deactivate it.
163
        $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
164
 
165
        // Run the task.
166
        $task = new \tool_monitor\task\check_subscriptions();
167
        $task->execute();
168
 
169
        // The subscription should NOT be active. Pass in the id only to refetch the data.
170
        $this->reload_subscription();
171
        $this->assertEquals(false, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
172
 
173
        // Enrol the user.
174
        $this->getDataGenerator()->enrol_user($this->user->id, $this->course->id, $this->teacherrole->id);
175
 
176
        // Run the task.
177
        $task = new \tool_monitor\task\check_subscriptions();
178
        $task->execute();
179
 
180
        // Subscription should now be active again.
181
        $this->reload_subscription();
182
        $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
183
    }
184
 
185
    /**
186
     * Test to confirm that subscriptions for enrolled users without the required capability are made inactive.
187
     */
11 efrain 188
    public function test_enrolled_user_with_no_capability(): void {
1 efrain 189
        // Enrol the user. By default, students won't have the required capability.
190
        $this->getDataGenerator()->enrol_user($this->user->id, $this->course->id, $this->studentrole->id);
191
 
192
        // The subscription should be active to start with. Pass in the id only to refetch the data.
193
        $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
194
 
195
        // Run the task.
196
        $task = new \tool_monitor\task\check_subscriptions();
197
        $task->execute();
198
 
199
        // The subscription should NOT be active. Pass in the id only to refetch the data.
200
        $this->reload_subscription();
201
        $this->assertEquals(false, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
202
    }
203
 
204
    /**
205
     * Test to confirm that subscriptions for users who fail can_access_course(), are deactivated.
206
     */
11 efrain 207
    public function test_can_access_course(): void {
1 efrain 208
        // Enrol the user as a teacher. This role should have the required capability.
209
        $this->getDataGenerator()->enrol_user($this->user->id, $this->course->id, $this->teacherrole->id);
210
 
211
        // Strip the ability to see hidden courses, so we'll fail the check_subscriptions->user_can_access_course call.
212
        $context = \context_course::instance($this->course->id);
213
        assign_capability('moodle/course:viewhiddencourses', CAP_PROHIBIT, $this->teacherrole->id, $context);
214
 
215
        // Subscription should be active to start with.
216
        $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
217
 
218
        // Hide the course.
219
        course_change_visibility($this->course->id, false);
220
 
221
        // Run the task.
222
        $task = new \tool_monitor\task\check_subscriptions();
223
        $task->execute();
224
 
225
        // The subscription should be inactive. Pass in the id only to refetch the data.
226
        $this->reload_subscription();
227
        $this->assertEquals(false, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
228
    }
229
 
230
    /**
231
     * Test to confirm that subscriptions for enrolled users who don't have CM access, are deactivated.
232
     */
11 efrain 233
    public function test_cm_access(): void {
1 efrain 234
        // Enrol the user as a student but grant to ability to subscribe. Students cannot view hidden activities.
235
        $context = \context_course::instance($this->course->id);
236
        assign_capability('tool/monitor:subscribe', CAP_ALLOW, $this->studentrole->id, $context);
237
        $this->getDataGenerator()->enrol_user($this->user->id, $this->course->id, $this->studentrole->id);
238
 
239
        // Generate a course module.
240
        $book = $this->getDataGenerator()->create_module('book', array('course' => $this->course->id));
241
 
242
        // And add a subscription to it.
243
        $sub = new \stdClass();
244
        $sub->courseid = $this->course->id;
245
        $sub->userid = $this->user->id;
246
        $sub->ruleid = $this->rule->id;
247
        $sub->cmid = $book->cmid;
248
        $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
249
        $this->subscription = $monitorgenerator->create_subscription($sub);
250
 
251
        // The subscription should be active to start with. Pass in the id only to refetch the data.
252
        $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
253
 
254
        // Run the task.
255
        $task = new \tool_monitor\task\check_subscriptions();
256
        $task->execute();
257
 
258
        // The subscription should still be active. Pass in the id only to refetch the data.
259
        $this->reload_subscription();
260
        $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
261
 
262
        // Make the course module invisible, which should in turn make the subscription inactive.
263
        set_coursemodule_visible($book->cmid, false);
264
 
265
        // Run the task.
266
        $task = new \tool_monitor\task\check_subscriptions();
267
        $task->execute();
268
 
269
        // The subscription should NOT be active. Pass in the id only to refetch the data.
270
        $this->reload_subscription();
271
        $this->assertEquals(false, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
272
 
273
        // Make the course module visible again.
274
        set_coursemodule_visible($book->cmid, true);
275
 
276
        // Run the task.
277
        $task = new \tool_monitor\task\check_subscriptions();
278
        $task->execute();
279
 
280
        // The subscription should be active. Pass in the id only to refetch the data.
281
        $this->reload_subscription();
282
        $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
283
    }
284
 
285
    /**
286
     * Test to confirm that long term inactive subscriptions are removed entirely.
287
     */
11 efrain 288
    public function test_stale_subscription_removal(): void {
1 efrain 289
        global $DB;
290
        // Manually set the inactivedate to 1 day older than the limit allowed.
291
        $daysold = 1 + \tool_monitor\subscription_manager::INACTIVE_SUBSCRIPTION_LIFESPAN_IN_DAYS;
292
 
293
        $inactivedate = strtotime("-$daysold days", time());
294
        $DB->set_field('tool_monitor_subscriptions', 'inactivedate', $inactivedate, array('id' => $this->subscription->id));
295
 
296
        // Subscription should be inactive to start with.
297
        $this->reload_subscription();
298
        $this->assertEquals(false, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
299
 
300
        // Run the task.
301
        $task = new \tool_monitor\task\check_subscriptions();
302
        $task->execute();
303
 
304
        // Subscription should now not exist at all.
305
        $this->assertEquals(false, $DB->record_exists('tool_monitor_subscriptions', array('id' => $this->subscription->id)));
306
    }
307
 
308
    /**
309
     * Test to confirm that subscriptions for a partially set up user are deactivated.
310
     */
11 efrain 311
    public function test_user_not_fully_set_up(): void {
1 efrain 312
        global $DB;
313
 
314
        // Enrol the user as a teacher.
315
        $this->getDataGenerator()->enrol_user($this->user->id, $this->course->id, $this->teacherrole->id);
316
 
317
        // The subscription should be active to start.
318
        $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
319
 
320
        // Unset the user's email address, so we fail the check_subscriptions->is_user_setup() call.
321
        $DB->set_field('user', 'email', '', array('id' => $this->user->id));
322
 
323
        // Run the task.
324
        $task = new \tool_monitor\task\check_subscriptions();
325
        $task->execute();
326
 
327
        // The subscription should now be inactive.
328
        $this->reload_subscription();
329
        $this->assertEquals(false, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
330
    }
331
 
332
    /**
333
     * Test to confirm that a suspended user's subscriptions are deactivated properly.
334
     */
11 efrain 335
    public function test_suspended_user(): void {
1 efrain 336
        global $DB;
337
 
338
        // Enrol the user as a teacher. This role should have the required capability.
339
        $this->getDataGenerator()->enrol_user($this->user->id, $this->course->id, $this->teacherrole->id);
340
 
341
        // Subscription should be active to start with.
342
        $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
343
 
344
        // Suspend the user.
345
        $DB->set_field('user', 'suspended', '1', array('id' => $this->user->id));
346
 
347
        // Run the task.
348
        $task = new \tool_monitor\task\check_subscriptions();
349
        $task->execute();
350
 
351
        // The subscription should now be inactive.
352
        $this->reload_subscription();
353
        $this->assertEquals(false, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
354
 
355
        // Unsuspend the user.
356
        $DB->set_field('user', 'suspended', '0', array('id' => $this->user->id));
357
 
358
        // Run the task.
359
        $task = new \tool_monitor\task\check_subscriptions();
360
        $task->execute();
361
 
362
        // The subscription should now be active again.
363
        $this->reload_subscription();
364
        $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
365
    }
366
}