MDL-67673 phpunit: Remove deprecated assertContains() uses on strings
[moodle.git] / competency / tests / event_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  * Event tests.
19  *
20  * @package    core_competency
21  * @copyright  2016 Serge Gauthier <serge.gauthier.2@umontreal.ca>
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 defined('MOODLE_INTERNAL') || die();
26 global $CFG;
27 require_once($CFG->dirroot . '/comment/lib.php');
29 use core_competency\api;
30 use core_competency\url;
32 /**
33  * Event tests.
34  *
35  * @package    core_competency
36  * @copyright  2016 Serge Gauthier <serge.gauthier.2@umontreal.ca>
37  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38  */
39 class core_competency_event_testcase extends advanced_testcase {
41     /**
42      * Test the competency framework created event.
43      *
44      */
45     public function test_competency_framework_created() {
46         $this->resetAfterTest(true);
47         $this->setAdminUser();
48         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
50         // Use DataGenerator to have a record framework with the right format.
51         $record = $lpg->create_framework()->to_record();
52         $record->id = 0;
53         $record->shortname = "New shortname";
54         $record->idnumber = "New idnumber";
56         // Trigger and capture the event.
57         $sink = $this->redirectEvents();
58         $framework = api::create_framework((object) $record);
60         // Get our event event.
61         $events = $sink->get_events();
62         $event = reset($events);
64         // Check that the event data is valid.
65         $this->assertInstanceOf('\core\event\competency_framework_created', $event);
66         $this->assertEquals($framework->get('id'), $event->objectid);
67         $this->assertEquals($framework->get('contextid'), $event->contextid);
68         $this->assertEventContextNotUsed($event);
69         $this->assertDebuggingNotCalled();
70     }
72     /**
73      * Test the competency framework deleted event.
74      *
75      */
76     public function test_competency_framework_deleted() {
77         $this->resetAfterTest(true);
78         $this->setAdminUser();
79         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
81         $framework = $lpg->create_framework();
83         // Trigger and capture the event.
84         $sink = $this->redirectEvents();
85         api::delete_framework($framework->get('id'));
87         // Get our event event.
88         $events = $sink->get_events();
89         $event = reset($events);
91         // Check that the event data is valid.
92         $this->assertInstanceOf('\core\event\competency_framework_deleted', $event);
93         $this->assertEquals($framework->get('id'), $event->objectid);
94         $this->assertEquals($framework->get('contextid'), $event->contextid);
95         $this->assertEventContextNotUsed($event);
96         $this->assertDebuggingNotCalled();
97     }
99     /**
100      * Test the competency framework updated event.
101      *
102      */
103     public function test_competency_framework_updated() {
104         $this->resetAfterTest(true);
105         $this->setAdminUser();
106         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
108         $framework = $lpg->create_framework();
110         // Trigger and capture the event.
111         $sink = $this->redirectEvents();
112         $framework->set('shortname', 'Shortname modified');
113         api::update_framework($framework->to_record());
115         // Get our event event.
116         $events = $sink->get_events();
117         $event = reset($events);
119         // Check that the event data is valid.
120         $this->assertInstanceOf('\core\event\competency_framework_updated', $event);
121         $this->assertEquals($framework->get('id'), $event->objectid);
122         $this->assertEquals($framework->get('contextid'), $event->contextid);
123         $this->assertEventContextNotUsed($event);
124         $this->assertDebuggingNotCalled();
125     }
127     /**
128      * Test the competency framework viewed event.
129      *
130      */
131     public function test_competency_framework_viewed() {
132         $this->resetAfterTest(true);
133         $this->setAdminUser();
134         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
136         $framework = $lpg->create_framework();
138         // Trigger and capture the event.
139         $sink = $this->redirectEvents();
140         api::competency_framework_viewed($framework);
142         // Get our event event.
143         $events = $sink->get_events();
144         $event = reset($events);
146         // Check that the event data is valid.
147         $this->assertInstanceOf('\core\event\competency_framework_viewed', $event);
148         $this->assertEquals($framework->get('id'), $event->objectid);
149         $this->assertEquals($framework->get('contextid'), $event->contextid);
150         $this->assertEventContextNotUsed($event);
151         $this->assertDebuggingNotCalled();
152     }
154     /**
155      * Test the competency viewed event.
156      *
157      */
158     public function test_competency_viewed() {
159         $this->resetAfterTest(true);
160         $this->setAdminUser();
161         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
162         $framework = $lpg->create_framework();
163         $competency = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
165         // Trigger and capture the event.
166         $sink = $this->redirectEvents();
167         api::competency_viewed($competency);
168         // Get our event event.
169         $events = $sink->get_events();
170         $event = reset($events);
171         // Check that the event data is valid.
172         $this->assertInstanceOf('\core\event\competency_viewed', $event);
173         $this->assertEquals($competency->get('id'), $event->objectid);
174         $this->assertEquals($competency->get_context()->id, $event->contextid);
175         $this->assertEventContextNotUsed($event);
176         $this->assertDebuggingNotCalled();
177     }
179     /**
180      * Test the template viewed event.
181      *
182      */
183     public function test_template_viewed() {
184         $this->resetAfterTest(true);
185         $this->setAdminUser();
186         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
187         $template = $lpg->create_template();
188         // Trigger and capture the event.
189         $sink = $this->redirectEvents();
190         api::template_viewed($template);
191         // Get our event event.
192         $events = $sink->get_events();
193         $event = reset($events);
194         // Check that the event data is valid.
195         $this->assertInstanceOf('\core\event\competency_template_viewed', $event);
196         $this->assertEquals($template->get('id'), $event->objectid);
197         $this->assertEquals($template->get('contextid'), $event->contextid);
198         $this->assertEventContextNotUsed($event);
199         $this->assertDebuggingNotCalled();
200     }
202     /**
203      * Test the template created event.
204      *
205      */
206     public function test_template_created() {
207         $this->resetAfterTest(true);
208         $this->setAdminUser();
209         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
211         // Use DataGenerator to have a template record with the right format.
212         $record = $lpg->create_template()->to_record();
213         $record->id = 0;
214         $record->shortname = "New shortname";
216         // Trigger and capture the event.
217         $sink = $this->redirectEvents();
218         $template = api::create_template((object) $record);
220         // Get our event event.
221         $events = $sink->get_events();
222         $event = reset($events);
224         $this->assertInstanceOf('\core\event\competency_template_created', $event);
225         $this->assertEquals($template->get('id'), $event->objectid);
226         $this->assertEquals($template->get('contextid'), $event->contextid);
227         $this->assertEventContextNotUsed($event);
228         $this->assertDebuggingNotCalled();
229     }
231     /**
232      * Test the template deleted event.
233      *
234      */
235     public function test_template_deleted() {
236         $this->resetAfterTest(true);
237         $this->setAdminUser();
238         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
240         $template = $lpg->create_template();
242         // Trigger and capture the event.
243         $sink = $this->redirectEvents();
244         api::delete_template($template->get('id'));
246         // Get our event event.
247         $events = $sink->get_events();
248         $event = reset($events);
250         // Check that the event data is valid.
251         $this->assertInstanceOf('\core\event\competency_template_deleted', $event);
252         $this->assertEquals($template->get('id'), $event->objectid);
253         $this->assertEquals($template->get('contextid'), $event->contextid);
254         $this->assertEventContextNotUsed($event);
255         $this->assertDebuggingNotCalled();
256     }
258     /**
259      * Test the template updated event.
260      *
261      */
262     public function test_template_updated() {
263         $this->resetAfterTest(true);
264         $this->setAdminUser();
265         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
267         $template = $lpg->create_template();
269         // Trigger and capture the event.
270         $sink = $this->redirectEvents();
271         $template->set('shortname', 'Shortname modified');
272         api::update_template($template->to_record());
274         // Get our event event.
275         $events = $sink->get_events();
276         $event = reset($events);
278         // Check that the event data is valid.
279         $this->assertInstanceOf('\core\event\competency_template_updated', $event);
280         $this->assertEquals($template->get('id'), $event->objectid);
281         $this->assertEquals($template->get('contextid'), $event->contextid);
282         $this->assertEventContextNotUsed($event);
283         $this->assertDebuggingNotCalled();
284     }
286     /**
287      * Test the competency updated event.
288      *
289      */
290     public function test_competency_updated() {
291         $this->resetAfterTest(true);
292         $this->setAdminUser();
293         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
295         $f1 = $lpg->create_framework();
296         $competency = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
297         $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
298         $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
299         $c12 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id'), 'parentid' => $c1->get('id')));
300         $c13 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id'), 'parentid' => $c1->get('id')));
302         // Trigger and capture the event.
303         $sink = $this->redirectEvents();
304         $competency->set('shortname', 'Shortname modified');
305         api::update_competency($competency->to_record());
307         // Get our event event.
308         $events = $sink->get_events();
309         $event = reset($events);
311         // Check that the event data is valid.
312         $this->assertInstanceOf('\core\event\competency_updated', $event);
313         $this->assertEquals($competency->get('id'), $event->objectid);
314         $this->assertEquals($competency->get_context()->id, $event->contextid);
315         $this->assertEventContextNotUsed($event);
316         $this->assertDebuggingNotCalled();
317     }
319     /**
320      * Test the competency created event.
321      *
322      */
323     public function test_competency_created() {
324         $this->resetAfterTest(true);
325         $this->setAdminUser();
326         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
328         $f1 = $lpg->create_framework();
329         $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
330         $record = $c1->to_record();
331         $record->id = 0;
332         $record->idnumber = 'comp idnumber';
334         // Trigger and capture the event.
335         $sink = $this->redirectEvents();
336         // Create competency should trigger a created event.
337         $competency = api::create_competency($record);
339         // Get our event event.
340         $events = $sink->get_events();
341         $event = reset($events);
343         $this->assertInstanceOf('\core\event\competency_created', $event);
344         $this->assertEquals($competency->get('id'), $event->objectid);
345         $this->assertEquals($competency->get_context()->id, $event->contextid);
346         $this->assertEventContextNotUsed($event);
347         $this->assertDebuggingNotCalled();
348     }
350     /**
351      * Test the competency created event by duplicate framework.
352      *
353      */
354     public function test_competency_created_by_duplicateframework() {
355         $this->resetAfterTest(true);
356         $this->setAdminUser();
357         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
359         $f1 = $lpg->create_framework();
360         $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
361         $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
362         $c12 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id'), 'parentid' => $c1->get('id')));
364         // Trigger and capture the event.
365         $sink = $this->redirectEvents();
366         // Create framework should trigger a created event for competencies.
367         api::duplicate_framework($f1->get('id'));
369         // Get our event event.
370         $events = $sink->get_events();
371         $this->assertEquals(4, count($events));
373         $event = array_shift($events);
374         $this->assertInstanceOf('\core\event\competency_created', $event);
376         $event = array_shift($events);
377         $this->assertInstanceOf('\core\event\competency_created', $event);
379         $event = array_shift($events);
380         $this->assertInstanceOf('\core\event\competency_created', $event);
382         $event = array_shift($events);
383         $this->assertInstanceOf('\core\event\competency_framework_created', $event);
384     }
386     /**
387      * Test the competency deleted event.
388      *
389      */
390     public function test_competency_deleted() {
391         $this->resetAfterTest(true);
392         $this->setAdminUser();
393         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
395         $f1 = $lpg->create_framework();
396         $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
397         $c1id = $c1->get('id');
398         $contextid = $c1->get_context()->id;
400         // Trigger and capture the event.
401         $sink = $this->redirectEvents();
402         // Delete competency should trigger a deleted event.
403         api::delete_competency($c1id);
405         // Get our event event.
406         $events = $sink->get_events();
407         $event = reset($events);
409         $this->assertInstanceOf('\core\event\competency_deleted', $event);
410         $this->assertEquals($c1id, $event->objectid);
411         $this->assertEquals($contextid, $event->contextid);
412         $this->assertEventContextNotUsed($event);
413         $this->assertDebuggingNotCalled();
414     }
416     /**
417      * Test the competency deleted event by delete framework.
418      *
419      */
420     public function test_competency_deleted_by_deleteframework() {
421         $this->resetAfterTest(true);
422         $this->setAdminUser();
423         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
425         $f1 = $lpg->create_framework();
426         $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
427         $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
428         $c12 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id'), 'parentid' => $c1->get('id')));
430         // Trigger and capture the event.
431         $sink = $this->redirectEvents();
432         // Delete framework should trigger a deleted event for competencies.
433         api::delete_framework($f1->get('id'));
435         // Get our event event.
436         $events = $sink->get_events();
437         $this->assertEquals(4, count($events));
439         $event = array_shift($events);
440         $this->assertInstanceOf('\core\event\competency_framework_deleted', $event);
442         $event = array_shift($events);
443         $this->assertInstanceOf('\core\event\competency_deleted', $event);
445         $event = array_shift($events);
446         $this->assertInstanceOf('\core\event\competency_deleted', $event);
448         $event = array_shift($events);
449         $this->assertInstanceOf('\core\event\competency_deleted', $event);
450     }
452     /**
453      * Test the plan created event.
454      *
455      */
456     public function test_plan_created() {
457         $this->resetAfterTest(true);
458         $this->setAdminUser();
459         $dg = $this->getDataGenerator();
460         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
461         $user = $dg->create_user();
462         $plan = array (
463             'name' => 'plan',
464             'userid' => $user->id
465         );
466         // Trigger and capture the event.
467         $sink = $this->redirectEvents();
468         $plan = api::create_plan((object)$plan);
469         // Get our event event.
470         $events = $sink->get_events();
471         $event = reset($events);
472         // Check that the event data is valid.
473         $this->assertInstanceOf('\core\event\competency_plan_created', $event);
474         $this->assertEquals($plan->get('id'), $event->objectid);
475         $this->assertEquals($plan->get_context()->id, $event->contextid);
476         $this->assertEventContextNotUsed($event);
477         $this->assertDebuggingNotCalled();
478     }
480     /**
481      * Test the plan created event using template_cohort.
482      *
483      */
484     public function test_plan_created_using_templatecohort() {
485         $this->resetAfterTest(true);
486         $this->setAdminUser();
487         $dg = $this->getDataGenerator();
488         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
489         $user1 = $dg->create_user();
490         $user2 = $dg->create_user();
491         $c1 = $dg->create_cohort();
492         // Add 2 users to the cohort.
493         cohort_add_member($c1->id, $user1->id);
494         cohort_add_member($c1->id, $user2->id);
495         $t1 = $lpg->create_template();
496         $tc = $lpg->create_template_cohort(array(
497             'templateid' => $t1->get('id'),
498             'cohortid' => $c1->id
499         ));
500         // Trigger and capture the event.
501         $sink = $this->redirectEvents();
502         api::create_plans_from_template_cohort($t1->get('id'), $c1->id);
503         // Get our event event.
504         $plans = core_competency\plan::get_records(array('templateid' => $t1->get('id')), 'id');
505         $events = $sink->get_events();
506         $this->assertCount(2, $events);
507         $this->assertCount(2, $plans);
508         $event = $events[0];
509         $plan = $plans[0];
510         // Check that the event data is valid.
511         $this->assertInstanceOf('\core\event\competency_plan_created', $event);
512         $this->assertEquals($plan->get('id'), $event->objectid);
513         $this->assertEquals($plan->get_context()->id, $event->contextid);
514         $event = $events[1];
515         $plan = $plans[1];
516         $this->assertInstanceOf('\core\event\competency_plan_created', $event);
517         $this->assertEquals($plan->get('id'), $event->objectid);
518         $this->assertEquals($plan->get_context()->id, $event->contextid);
519         $this->assertEventContextNotUsed($event);
520         $this->assertDebuggingNotCalled();
521     }
523     /**
524      * Test the plan updated event.
525      *
526      */
527     public function test_plan_updated() {
528         $this->resetAfterTest(true);
529         $this->setAdminUser();
530         $dg = $this->getDataGenerator();
531         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
532         $user1 = $dg->create_user();
533         $plan = $lpg->create_plan(array('userid' => $user1->id));
534         $record = $plan->to_record();
535         $record->name = 'Plan updated';
536         // Trigger and capture the event.
537         $sink = $this->redirectEvents();
538         $plan = api::update_plan($record);
539         $this->assertEquals('Plan updated', $plan->get('name'));
541         // Get our event event.
542         $events = $sink->get_events();
543         $event = reset($events);
544         $this->assertInstanceOf('\core\event\competency_plan_updated', $event);
545         $this->assertEquals($plan->get('id'), $event->objectid);
546         $this->assertEquals($plan->get_context()->id, $event->contextid);
547         $this->assertEventContextNotUsed($event);
548         $this->assertDebuggingNotCalled();
549     }
551     /**
552      * Test the plan deleted event.
553      *
554      */
555     public function test_plan_deleted() {
556         $this->resetAfterTest(true);
557         $this->setAdminUser();
558         $dg = $this->getDataGenerator();
559         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
560         $user1 = $dg->create_user();
561         $plan = $lpg->create_plan(array('userid' => $user1->id));
562         $planid = $plan->get('id');
563         $contextid = $plan->get_context()->id;
564         // Trigger and capture the event.
565         $sink = $this->redirectEvents();
566         $result = api::delete_plan($plan->get('id'));
567         $this->assertTrue($result);
568         // Get our event event.
569         $events = $sink->get_events();
570         $event = reset($events);
571         $this->assertInstanceOf('\core\event\competency_plan_deleted', $event);
572         $this->assertEquals($planid, $event->objectid);
573         $this->assertEquals($contextid, $event->contextid);
574         $this->assertEventContextNotUsed($event);
575         $this->assertDebuggingNotCalled();
576     }
578     /**
579      * Test the plan viewed event.
580      *
581      */
582     public function test_plan_viewed() {
583         $this->resetAfterTest(true);
584         $this->setAdminUser();
585         $dg = $this->getDataGenerator();
586         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
587         $user1 = $dg->create_user();
588         $plan = $lpg->create_plan(array('userid' => $user1->id));
589         // Trigger and capture the event.
590         $sink = $this->redirectEvents();
591         api::plan_viewed($plan);
592         // Get our event event.
593         $events = $sink->get_events();
594         $event = reset($events);
595         // Check that the event data is valid.
596         $this->assertInstanceOf('\core\event\competency_plan_viewed', $event);
597         $this->assertEquals($plan->get('id'), $event->objectid);
598         $this->assertEquals($plan->get_context()->id, $event->contextid);
599         $this->assertEventContextNotUsed($event);
600         $this->assertDebuggingNotCalled();
601     }
603     /**
604      * Test the evidence of prior learning created event.
605      *
606      */
607     public function test_user_evidence_created() {
608         $this->resetAfterTest(true);
609         $this->setAdminUser();
610         $dg = $this->getDataGenerator();
611         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
613         $user = $dg->create_user();
614         // Use DataGenerator to have a user_evidence record with the right format.
615         $record = $userevidence = $lpg->create_user_evidence(array('userid' => $user->id))->to_record();
616         $record->id = 0;
617         $record->name = "New name";
619         // Trigger and capture the event.
620         $sink = $this->redirectEvents();
621         $userevidence = api::create_user_evidence((object) $record);
623          // Get our event event.
624         $events = $sink->get_events();
625         $event = reset($events);
627         $this->assertInstanceOf('\core\event\competency_user_evidence_created', $event);
628         $this->assertEquals($userevidence->get('id'), $event->objectid);
629         $this->assertEquals($userevidence->get_context()->id, $event->contextid);
630         $this->assertEventContextNotUsed($event);
631         $this->assertDebuggingNotCalled();
632     }
634     /**
635      * Test the evidence of prior learning  deleted event.
636      *
637      */
638     public function test_user_evidence_deleted() {
639         $this->resetAfterTest(true);
640         $this->setAdminUser();
641         $dg = $this->getDataGenerator();
642         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
644         $user = $dg->create_user();
645         $userevidence = $lpg->create_user_evidence(array('userid' => $user->id));
647         // Trigger and capture the event.
648         $sink = $this->redirectEvents();
649         api::delete_user_evidence($userevidence->get('id'));
651         // Get our event event.
652         $events = $sink->get_events();
653         $event = reset($events);
655         // Check that the event data is valid.
656         $this->assertInstanceOf('\core\event\competency_user_evidence_deleted', $event);
657         $this->assertEquals($userevidence->get('id'), $event->objectid);
658         $this->assertEquals($userevidence->get_context()->id, $event->contextid);
659         $this->assertEventContextNotUsed($event);
660         $this->assertDebuggingNotCalled();
661     }
663     /**
664      * Test the evidence of prior learning  updated event.
665      *
666      */
667     public function test_user_evidence_updated() {
668         $this->resetAfterTest(true);
669         $this->setAdminUser();
670         $dg = $this->getDataGenerator();
671         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
673         $user = $dg->create_user();
674         $userevidence = $lpg->create_user_evidence(array('userid' => $user->id));
676         // Trigger and capture the event.
677         $sink = $this->redirectEvents();
678         $userevidence->set('name', 'Name modified');
679         api::update_user_evidence($userevidence->to_record());
681          // Get our event event.
682         $events = $sink->get_events();
683         $event = reset($events);
685         // Check that the event data is valid.
686         $this->assertInstanceOf('\core\event\competency_user_evidence_updated', $event);
687         $this->assertEquals($userevidence->get('id'), $event->objectid);
688         $this->assertEquals($userevidence->get_context()->id, $event->contextid);
689         $this->assertEventContextNotUsed($event);
690         $this->assertDebuggingNotCalled();
691     }
693     /**
694      * Test the user competency viewed event in plan.
695      *
696      */
697     public function test_user_competency_viewed_in_plan() {
698         $this->resetAfterTest(true);
699         $this->setAdminUser();
700         $dg = $this->getDataGenerator();
701         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
702         $user = $dg->create_user();
703         $plan = $lpg->create_plan(array('userid' => $user->id));
704         $fr = $lpg->create_framework();
705         $c = $lpg->create_competency(array('competencyframeworkid' => $fr->get('id')));
706         $pc = $lpg->create_plan_competency(array('planid' => $plan->get('id'), 'competencyid' => $c->get('id')));
707         $uc = $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c->get('id')));
709         // Can not log the event for user competency using completed plan.
710         api::complete_plan($plan);
712         try {
713             api::user_competency_viewed_in_plan($uc, $plan->get('id'));
714             $this->fail('To log the user competency in completed plan '
715                     . 'use user_competency_plan_viewed method.');
716         } catch (coding_exception $e) {
717             $this->assertRegExp('/To log the user competency in completed plan '
718                     . 'use user_competency_plan_viewed method./', $e->getMessage());
719         }
721         api::reopen_plan($plan);
722         // Trigger and capture the event.
723         $sink = $this->redirectEvents();
724         api::user_competency_viewed_in_plan($uc, $plan->get('id'));
726         // Get our event event.
727         $events = $sink->get_events();
728         $event = reset($events);
730         // Check that the event data is valid.
731         $this->assertInstanceOf('\core\event\competency_user_competency_viewed_in_plan', $event);
732         $this->assertEquals($uc->get('id'), $event->objectid);
733         $this->assertEquals($uc->get_context()->id, $event->contextid);
734         $this->assertEquals($uc->get('userid'), $event->relateduserid);
735         $this->assertEquals($plan->get('id'), $event->other['planid']);
736         $this->assertEquals($c->get('id'), $event->other['competencyid']);
738         $this->assertEventContextNotUsed($event);
739         $this->assertDebuggingNotCalled();
741         // Test validation.
742         $params = array (
743             'objectid' => $uc->get('id'),
744             'contextid' => $uc->get_context()->id,
745             'other' => null
746         );
748         // Other value null.
749         try {
750             \core\event\competency_user_competency_viewed_in_plan::create($params)->trigger();
751             $this->fail('The \'competencyid\' and \'planid\' values must be set.');
752         } catch (coding_exception $e) {
753             $this->assertRegExp("/The 'competencyid' and 'planid' values must be set./", $e->getMessage());
754         }
756         $params['other']['anythingelse'] = '';
757         // Missing competencyid.
758         try {
759             \core\event\competency_user_competency_viewed_in_plan::create($params)->trigger();
760             $this->fail('The \'competencyid\' value must be set.');
761         } catch (coding_exception $e) {
762             $this->assertRegExp("/The 'competencyid' value must be set./", $e->getMessage());
763         }
765         $params['other']['competencyid'] = $c->get('id');
766         // Missing planid.
767         try {
768             \core\event\competency_user_competency_viewed_in_plan::create($params)->trigger();
769             $this->fail('The \'planid\' value must be set.');
770         } catch (coding_exception $e) {
771             $this->assertRegExp("/The 'planid' value must be set./", $e->getMessage());
772         }
773     }
775     /**
776      * Test the user competency viewed event in course.
777      *
778      */
779     public function test_user_competency_viewed_in_course() {
780         $this->resetAfterTest(true);
781         $this->setAdminUser();
782         $dg = $this->getDataGenerator();
783         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
784         $user = $dg->create_user();
785         $course = $dg->create_course();
786         $fr = $lpg->create_framework();
787         $c = $lpg->create_competency(array('competencyframeworkid' => $fr->get('id')));
788         $pc = $lpg->create_course_competency(array('courseid' => $course->id, 'competencyid' => $c->get('id')));
789         $params = array('userid' => $user->id, 'competencyid' => $c->get('id'), 'courseid' => $course->id);
790         $ucc = $lpg->create_user_competency_course($params);
792         // Trigger and capture the event.
793         $sink = $this->redirectEvents();
794         api::user_competency_viewed_in_course($ucc);
796         // Get our event event.
797         $events = $sink->get_events();
798         $event = reset($events);
800         // Check that the event data is valid.
801         $this->assertInstanceOf('\core\event\competency_user_competency_viewed_in_course', $event);
802         $this->assertEquals($ucc->get('id'), $event->objectid);
803         $this->assertEquals(context_course::instance($course->id)->id, $event->contextid);
804         $this->assertEquals($ucc->get('userid'), $event->relateduserid);
805         $this->assertEquals($course->id, $event->courseid);
806         $this->assertEquals($c->get('id'), $event->other['competencyid']);
808         $this->assertEventContextNotUsed($event);
809         $this->assertDebuggingNotCalled();
811         // Test validation.
812         $params = array (
813             'objectid' => $ucc->get('id'),
814             'contextid' => $ucc->get_context()->id,
815             'other' => null
816         );
818         // Missing courseid.
819         try {
820             \core\event\competency_user_competency_viewed_in_course::create($params)->trigger();
821             $this->fail('The \'courseid\' value must be set.');
822         } catch (coding_exception $e) {
823             $this->assertRegExp("/The 'courseid' value must be set./", $e->getMessage());
824         }
826         $params['contextid'] = context_course::instance($course->id)->id;
827         $params['courseid'] = $course->id;
828         // Missing competencyid.
829         try {
830             \core\event\competency_user_competency_viewed_in_course::create($params)->trigger();
831             $this->fail('The \'competencyid\' value must be set.');
832         } catch (coding_exception $e) {
833             $this->assertRegExp("/The 'competencyid' value must be set./", $e->getMessage());
834         }
835     }
837     /**
838      * Test the user competency plan viewed event.
839      *
840      */
841     public function test_user_competency_plan_viewed() {
842         $this->resetAfterTest(true);
843         $this->setAdminUser();
844         $dg = $this->getDataGenerator();
845         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
846         $user = $dg->create_user();
847         $plan = $lpg->create_plan(array('userid' => $user->id));
848         $fr = $lpg->create_framework();
849         $c = $lpg->create_competency(array('competencyframeworkid' => $fr->get('id')));
850         $ucp = $lpg->create_user_competency_plan(array(
851             'userid' => $user->id,
852             'competencyid' => $c->get('id'),
853             'planid' => $plan->get('id')
854         ));
856         // Can not log the event for user competency using non completed plan.
857         try {
858             api::user_competency_plan_viewed($ucp);
859             $this->fail('To log the user competency in non-completed plan '
860                     . 'use user_competency_viewed_in_plan method.');
861         } catch (coding_exception $e) {
862             $this->assertRegExp('/To log the user competency in non-completed plan '
863                     . 'use user_competency_viewed_in_plan method./', $e->getMessage());
864         }
866         // Complete the plan.
867         api::complete_plan($plan);
869         // Trigger and capture the event.
870         $sink = $this->redirectEvents();
871         api::user_competency_plan_viewed($ucp);
873         // Get our event event.
874         $events = $sink->get_events();
875         $event = reset($events);
877         // Check that the event data is valid.
878         $this->assertInstanceOf('\core\event\competency_user_competency_plan_viewed', $event);
879         $this->assertEquals($ucp->get('id'), $event->objectid);
880         $this->assertEquals($ucp->get_context()->id, $event->contextid);
881         $this->assertEquals($ucp->get('userid'), $event->relateduserid);
882         $this->assertEquals($plan->get('id'), $event->other['planid']);
883         $this->assertEquals($c->get('id'), $event->other['competencyid']);
885         $this->assertEventContextNotUsed($event);
886         $this->assertDebuggingNotCalled();
888         // Test validation.
889         $params = array (
890             'objectid' => $ucp->get('id'),
891             'contextid' => $ucp->get_context()->id,
892             'other' => null
893         );
895         // Other value null.
896         try {
897             \core\event\competency_user_competency_plan_viewed::create($params)->trigger();
898             $this->fail('The \'competencyid\' and \'planid\' values must be set.');
899         } catch (coding_exception $e) {
900             $this->assertRegExp("/The 'competencyid' and 'planid' values must be set./", $e->getMessage());
901         }
903         $params['other']['anythingelse'] = '';
904         // Missing competencyid.
905         try {
906             \core\event\competency_user_competency_plan_viewed::create($params)->trigger();
907             $this->fail('The \'competencyid\' value must be set.');
908         } catch (coding_exception $e) {
909             $this->assertRegExp("/The 'competencyid' value must be set./", $e->getMessage());
910         }
912         $params['other']['competencyid'] = $c->get('id');
913         // Missing planid.
914         try {
915             \core\event\competency_user_competency_plan_viewed::create($params)->trigger();
916             $this->fail('The \'planid\' value must be set.');
917         } catch (coding_exception $e) {
918             $this->assertRegExp("/The 'planid' value must be set./", $e->getMessage());
919         }
920     }
922     /**
923      * Test the user competency viewed event.
924      *
925      */
926     public function test_user_competency_viewed() {
927         $this->resetAfterTest(true);
928         $this->setAdminUser();
929         $dg = $this->getDataGenerator();
930         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
931         $user = $dg->create_user();
932         $fr = $lpg->create_framework();
933         $c = $lpg->create_competency(array('competencyframeworkid' => $fr->get('id')));
934         $uc = $lpg->create_user_competency(array(
935             'userid' => $user->id,
936             'competencyid' => $c->get('id')
937         ));
939         // Trigger and capture the event.
940         $sink = $this->redirectEvents();
941         api::user_competency_viewed($uc);
943         // Get our event event.
944         $events = $sink->get_events();
945         $event = reset($events);
947         // Check that the event data is valid.
948         $this->assertInstanceOf('\core\event\competency_user_competency_viewed', $event);
949         $this->assertEquals($uc->get('id'), $event->objectid);
950         $this->assertEquals($uc->get_context()->id, $event->contextid);
951         $this->assertEquals($uc->get('userid'), $event->relateduserid);
952         $this->assertEquals($c->get('id'), $event->other['competencyid']);
954         $this->assertEventContextNotUsed($event);
955         $this->assertDebuggingNotCalled();
957         // Test validation.
958         $params = array (
959             'objectid' => $uc->get('id'),
960             'contextid' => $uc->get_context()->id
961         );
963         // Missing competencyid.
964         try {
965             \core\event\competency_user_competency_viewed::create($params)->trigger();
966             $this->fail('The \'competencyid\' value must be set.');
967         } catch (coding_exception $e) {
968             $this->assertRegExp("/The 'competencyid' value must be set./", $e->getMessage());
969         }
970     }
972     /**
973      * Test the plan approved event.
974      *
975      */
976     public function test_plan_approved() {
977         $this->resetAfterTest(true);
978         $this->setAdminUser();
979         $dg = $this->getDataGenerator();
980         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
981         $user1 = $dg->create_user();
982         $plan = $lpg->create_plan(array('userid' => $user1->id));
983         $planid = $plan->get('id');
984         $contextid = $plan->get_context()->id;
985         // Trigger and capture the event.
986         $sink = $this->redirectEvents();
987         $result = api::approve_plan($plan->get('id'));
988         $this->assertTrue($result);
989         // Get our event event.
990         $events = $sink->get_events();
991         $event = reset($events);
992         $this->assertInstanceOf('\core\event\competency_plan_approved', $event);
993         $this->assertEquals($planid, $event->objectid);
994         $this->assertEquals($contextid, $event->contextid);
995         $this->assertEquals($plan->get('userid'), $event->relateduserid);
996         $this->assertEventContextNotUsed($event);
997         $this->assertDebuggingNotCalled();
998     }
1000     /**
1001      * Test the plan unapproved event.
1002      *
1003      */
1004     public function test_plan_unapproved() {
1005         $this->resetAfterTest(true);
1006         $this->setAdminUser();
1007         $dg = $this->getDataGenerator();
1008         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1009         $user1 = $dg->create_user();
1010         $plan = $lpg->create_plan(array('userid' => $user1->id, 'status' => \core_competency\plan::STATUS_ACTIVE));
1011         $planid = $plan->get('id');
1012         $contextid = $plan->get_context()->id;
1013         // Trigger and capture the event.
1014         $sink = $this->redirectEvents();
1015         $result = api::unapprove_plan($plan->get('id'));
1016         $this->assertTrue($result);
1017         // Get our event event.
1018         $events = $sink->get_events();
1019         $event = reset($events);
1020         $this->assertInstanceOf('\core\event\competency_plan_unapproved', $event);
1021         $this->assertEquals($planid, $event->objectid);
1022         $this->assertEquals($contextid, $event->contextid);
1023         $this->assertEquals($plan->get('userid'), $event->relateduserid);
1024         $this->assertEventContextNotUsed($event);
1025         $this->assertDebuggingNotCalled();
1026     }
1028     /**
1029      * Test the plan reopened event.
1030      *
1031      */
1032     public function test_plan_reopened() {
1033         $this->resetAfterTest(true);
1034         $this->setAdminUser();
1035         $dg = $this->getDataGenerator();
1036         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1037         $user1 = $dg->create_user();
1038         $plan = $lpg->create_plan(array('userid' => $user1->id, 'status' => \core_competency\plan::STATUS_COMPLETE));
1039         $planid = $plan->get('id');
1040         $contextid = $plan->get_context()->id;
1041         // Trigger and capture the event.
1042         $sink = $this->redirectEvents();
1043         $result = api::reopen_plan($plan->get('id'));
1044         $this->assertTrue($result);
1045         // Get our event event.
1046         $events = $sink->get_events();
1047         $event = reset($events);
1048         $this->assertInstanceOf('\core\event\competency_plan_reopened', $event);
1049         $this->assertEquals($planid, $event->objectid);
1050         $this->assertEquals($contextid, $event->contextid);
1051         $this->assertEquals($plan->get('userid'), $event->relateduserid);
1052         $this->assertEventContextNotUsed($event);
1053         $this->assertDebuggingNotCalled();
1054     }
1056     /**
1057      * Test the plan completed event.
1058      *
1059      */
1060     public function test_plan_completed() {
1061         $this->resetAfterTest(true);
1062         $this->setAdminUser();
1063         $dg = $this->getDataGenerator();
1064         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1065         $user1 = $dg->create_user();
1066         $plan = $lpg->create_plan(array('userid' => $user1->id, 'status' => \core_competency\plan::STATUS_ACTIVE));
1067         $planid = $plan->get('id');
1068         $contextid = $plan->get_context()->id;
1069         // Trigger and capture the event.
1070         $sink = $this->redirectEvents();
1071         $result = api::complete_plan($plan->get('id'));
1072         $this->assertTrue($result);
1073         // Get our event event.
1074         $events = $sink->get_events();
1075         $event = reset($events);
1076         $this->assertInstanceOf('\core\event\competency_plan_completed', $event);
1077         $this->assertEquals($planid, $event->objectid);
1078         $this->assertEquals($contextid, $event->contextid);
1079         $this->assertEquals($plan->get('userid'), $event->relateduserid);
1080         $this->assertEventContextNotUsed($event);
1081         $this->assertDebuggingNotCalled();
1082     }
1084     /**
1085      * Test the plan unlinked event.
1086      *
1087      */
1088     public function test_plan_unlinked() {
1089         $this->resetAfterTest(true);
1090         $this->setAdminUser();
1091         $dg = $this->getDataGenerator();
1092         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1093         $user1 = $dg->create_user();
1094         $template = $lpg->create_template();
1095         $plan = $lpg->create_plan(array(
1096             'userid' => $user1->id,
1097             'status' => \core_competency\plan::STATUS_ACTIVE,
1098             'templateid' => $template->get('id')
1099         ));
1100         $planid = $plan->get('id');
1101         $contextid = $plan->get_context()->id;
1102         // Trigger and capture the event.
1103         $sink = $this->redirectEvents();
1104         $result = api::unlink_plan_from_template($plan->get('id'));
1105         $this->assertTrue($result);
1106         // Get our event event.
1107         $events = $sink->get_events();
1108         $event = reset($events);
1109         $this->assertInstanceOf('\core\event\competency_plan_unlinked', $event);
1110         $this->assertEquals($planid, $event->objectid);
1111         $this->assertEquals($contextid, $event->contextid);
1112         $this->assertEquals($plan->get('userid'), $event->relateduserid);
1113         $this->assertEventContextNotUsed($event);
1114         $this->assertDebuggingNotCalled();
1115     }
1117     /**
1118      * Test the plan review requested event.
1119      *
1120      */
1121     public function test_plan_review_requested() {
1122         $this->resetAfterTest(true);
1123         $this->setAdminUser();
1124         $dg = $this->getDataGenerator();
1125         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1126         $user1 = $dg->create_user();
1127         $plan = $lpg->create_plan(array('userid' => $user1->id));
1128         $planid = $plan->get('id');
1129         $contextid = $plan->get_context()->id;
1130         // Trigger and capture the event.
1131         $sink = $this->redirectEvents();
1132         $result = api::plan_request_review($plan->get('id'));
1133         $this->assertTrue($result);
1134         // Get our event event.
1135         $events = $sink->get_events();
1136         $event = reset($events);
1137         $this->assertInstanceOf('\core\event\competency_plan_review_requested', $event);
1138         $this->assertEquals($planid, $event->objectid);
1139         $this->assertEquals($contextid, $event->contextid);
1140         $this->assertEquals($plan->get('userid'), $event->relateduserid);
1141         $this->assertEventContextNotUsed($event);
1142         $this->assertDebuggingNotCalled();
1143     }
1145     /**
1146      * Test the plan review request cancelled event.
1147      *
1148      */
1149     public function test_plan_review_request_cancelled() {
1150         $this->resetAfterTest(true);
1151         $this->setAdminUser();
1152         $dg = $this->getDataGenerator();
1153         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1154         $user1 = $dg->create_user();
1155         $plan = $lpg->create_plan(array('userid' => $user1->id, 'status' => \core_competency\plan::STATUS_WAITING_FOR_REVIEW));
1156         $planid = $plan->get('id');
1157         $contextid = $plan->get_context()->id;
1158         // Trigger and capture the event.
1159         $sink = $this->redirectEvents();
1160         $result = api::plan_cancel_review_request($plan->get('id'));
1161         $this->assertTrue($result);
1162         // Get our event event.
1163         $events = $sink->get_events();
1164         $event = reset($events);
1165         $this->assertInstanceOf('\core\event\competency_plan_review_request_cancelled', $event);
1166         $this->assertEquals($planid, $event->objectid);
1167         $this->assertEquals($contextid, $event->contextid);
1168         $this->assertEquals($plan->get('userid'), $event->relateduserid);
1169         $this->assertEventContextNotUsed($event);
1170         $this->assertDebuggingNotCalled();
1171     }
1173     /**
1174      * Test the plan review started event.
1175      *
1176      */
1177     public function test_plan_review_started() {
1178         $this->resetAfterTest(true);
1179         $this->setAdminUser();
1180         $dg = $this->getDataGenerator();
1181         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1182         $user1 = $dg->create_user();
1183         $plan = $lpg->create_plan(array('userid' => $user1->id, 'status' => \core_competency\plan::STATUS_WAITING_FOR_REVIEW));
1184         $planid = $plan->get('id');
1185         $contextid = $plan->get_context()->id;
1186         // Trigger and capture the event.
1187         $sink = $this->redirectEvents();
1188         $result = api::plan_start_review($plan->get('id'));
1189         $this->assertTrue($result);
1190         // Get our event event.
1191         $events = $sink->get_events();
1192         $event = reset($events);
1193         $this->assertInstanceOf('\core\event\competency_plan_review_started', $event);
1194         $this->assertEquals($planid, $event->objectid);
1195         $this->assertEquals($contextid, $event->contextid);
1196         $this->assertEquals($plan->get('userid'), $event->relateduserid);
1197         $this->assertEventContextNotUsed($event);
1198         $this->assertDebuggingNotCalled();
1199     }
1201     /**
1202      * Test the plan review stopped event.
1203      *
1204      */
1205     public function test_plan_review_stopped() {
1206         $this->resetAfterTest(true);
1207         $this->setAdminUser();
1208         $dg = $this->getDataGenerator();
1209         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1210         $user1 = $dg->create_user();
1211         $plan = $lpg->create_plan(array('userid' => $user1->id, 'status' => \core_competency\plan::STATUS_IN_REVIEW));
1212         $planid = $plan->get('id');
1213         $contextid = $plan->get_context()->id;
1214         // Trigger and capture the event.
1215         $sink = $this->redirectEvents();
1216         $result = api::plan_stop_review($plan->get('id'));
1217         $this->assertTrue($result);
1218         // Get our event event.
1219         $events = $sink->get_events();
1220         $event = reset($events);
1221         $this->assertInstanceOf('\core\event\competency_plan_review_stopped', $event);
1222         $this->assertEquals($planid, $event->objectid);
1223         $this->assertEquals($contextid, $event->contextid);
1224         $this->assertEquals($plan->get('userid'), $event->relateduserid);
1225         $this->assertEventContextNotUsed($event);
1226         $this->assertDebuggingNotCalled();
1227     }
1229     /**
1230      * Test plan comment created event.
1231      */
1232     public function test_plan_comment_created() {
1233         $this->resetAfterTest(true);
1234         $dg = $this->getDataGenerator();
1235         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1236         $user = $dg->create_user();
1237         $this->setUser($user);
1238         $plan = $lpg->create_plan(array('userid' => $user->id));
1239         $context = context_user::instance($user->id);
1241         $cmt = new stdClass();
1242         $cmt->context = $context;
1243         $cmt->area = 'plan';
1244         $cmt->itemid = $plan->get('id');
1245         $cmt->component = 'competency';
1246         $cmt->showcount = 1;
1247         $manager = new comment($cmt);
1248         $manager->set_post_permission(true);
1250         // Triggering and capturing the event.
1251         $sink = $this->redirectEvents();
1252         $manager->add("New comment for plan");
1253         $events = $sink->get_events();
1254         // Add comment will trigger 2 other events message_viewed and message_sent.
1255         $this->assertCount(1, $events);
1256         $event = array_pop($events);
1258         // Checking that the event contains the expected values.
1259         $this->assertInstanceOf('\core\event\competency_comment_created', $event);
1260         $this->assertEquals($context, $event->get_context());
1261         $this->assertEquals($plan->get('id'), $event->other['itemid']);
1262         $this->assertEventContextNotUsed($event);
1263         $this->assertDebuggingNotCalled();
1264     }
1266     /**
1267      * Test plan comment deleted event.
1268      */
1269     public function test_plan_comment_deleted() {
1270         $this->resetAfterTest(true);
1271         $this->setAdminUser();
1272         $dg = $this->getDataGenerator();
1273         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1274         $user1 = $dg->create_user();
1275         $plan = $lpg->create_plan(array('userid' => $user1->id));
1277         $context = context_user::instance($user1->id);
1279         $cmt = new stdClass();
1280         $cmt->context = $context;
1281         $cmt->area = 'plan';
1282         $cmt->itemid = $plan->get('id');
1283         $cmt->component = 'competency';
1284         $manager = new comment($cmt);
1285         $newcomment = $manager->add("Comment to be deleted");
1287         // Triggering and capturing the event.
1288         $sink = $this->redirectEvents();
1289         $manager->delete($newcomment->id);
1290         $events = $sink->get_events();
1291         $this->assertCount(1, $events);
1292         $event = reset($events);
1294         // Checking that the event contains the expected values.
1295         $this->assertInstanceOf('\core\event\competency_comment_deleted', $event);
1296         $this->assertEquals($context, $event->get_context());
1297         $this->assertEquals($plan->get('id'), $event->other['itemid']);
1298         $this->assertEventContextNotUsed($event);
1299         $this->assertDebuggingNotCalled();
1300     }
1302     /**
1303      * Test evidence_created event.
1304      */
1305     public function test_evidence_created() {
1306         global $USER;
1308         $this->resetAfterTest(true);
1309         $dg = $this->getDataGenerator();
1310         $syscontext = context_system::instance();
1312         // Create a student.
1313         $student = $dg->create_user();
1315         // Create a competency for the course.
1316         $lpg = $dg->get_plugin_generator('core_competency');
1317         $framework = $lpg->create_framework();
1318         $comp = $lpg->create_competency(['competencyframeworkid' => $framework->get('id')]);
1320         // Trigger and capture the event.
1321         $sink = $this->redirectEvents();
1323         // Add evidence.
1324         $recommend = false;
1325         $evidence = api::add_evidence($student->id, $comp, $syscontext, \core_competency\evidence::ACTION_OVERRIDE,
1326             'commentincontext', 'core', null, $recommend, null, 1);
1328         // Get event.
1329         $events = $sink->get_events();
1330         $event = reset($events);
1332         // Check that the event data is valid.
1333         $this->assertInstanceOf('\core\event\competency_evidence_created', $event);
1334         $this->assertEquals($evidence->get('contextid'), $event->contextid);
1335         $this->assertEquals($evidence->get('id'), $event->objectid);
1336         $this->assertEquals($evidence->get('actionuserid'), $event->userid);
1337         $this->assertEquals($student->id, $event->relateduserid);
1338         $this->assertEquals($evidence->get('usercompetencyid'), $event->other['usercompetencyid']);
1339         $this->assertEquals($comp->get('id'), $event->other['competencyid']);
1340         $this->assertEquals($evidence->get('action'), $event->other['action']);
1341         $this->assertEquals($recommend, $event->other['recommend']);
1343         // Test get_name().
1344         $this->assertEquals(get_string('eventevidencecreated', 'core_competency'), $event->get_name());
1346         // Test get_description().
1347         $description = "The user with id '$USER->id' created an evidence with id '{$evidence->get('id')}'.";
1348         $this->assertEquals($description, $event->get_description());
1350         // Test get_url().
1351         $url = url::user_competency($evidence->get('usercompetencyid'));
1352         $this->assertEquals($url, $event->get_url());
1354         // Test get_objectid_mapping().
1355         $this->assertEquals(\core\event\base::NOT_MAPPED, $event->get_objectid_mapping());
1357         $this->assertEventContextNotUsed($event);
1358         $this->assertDebuggingNotCalled();
1359     }
1361     /**
1362      * Test evidence_created event by linking an invalid user competency to an evidence.
1363      */
1364     public function test_evidence_created_with_invalid_user_competency() {
1365         $this->resetAfterTest(true);
1366         $dg = $this->getDataGenerator();
1367         $syscontext = context_system::instance();
1369         // Create students.
1370         $student = $dg->create_user();
1371         $student2 = $dg->create_user();
1373         // Create a competency for the course.
1374         $lpg = $dg->get_plugin_generator('core_competency');
1375         $framework = $lpg->create_framework();
1376         $comp = $lpg->create_competency(['competencyframeworkid' => $framework->get('id')]);
1378         // Create a different user competency.
1379         $otheruc = \core_competency\user_competency::create_relation($student2->id, $comp->get('id'));
1380         $otheruc->create();
1382         // Add evidence.
1383         $recommend = false;
1384         $evidence = api::add_evidence($student->id, $comp, $syscontext, \core_competency\evidence::ACTION_OVERRIDE,
1385             'commentincontext', 'core', null, $recommend, null, 1);
1387         // We expect this to fail and throw a coding exception.
1388         $this->expectException('coding_exception');
1389         $this->expectExceptionMessage('The user competency linked with this evidence is invalid.');
1390         \core\event\competency_evidence_created::create_from_evidence($evidence, $otheruc, $recommend)->trigger();
1391     }
1393     /**
1394      * Test creation of evidence_created event with missing data.
1395      *
1396      * These data are validated by \core_competency\evidence_created::validate_data().
1397      */
1398     public function test_evidence_created_with_missing_data() {
1399         $eventdata = [
1400             'contextid'  => 1,
1401             'objectid' => 1,
1402             'userid' => 1
1403         ];
1405         // No relateduserid.
1406         $errormsg = 'The \'relateduserid\' must be set.';
1407         try {
1408             \core\event\competency_evidence_created::create($eventdata)->trigger();
1409             $this->fail('Coding exception should have been thrown: ' . $errormsg);
1410         } catch (coding_exception $e) {
1411             $this->assertStringContainsString($errormsg, $e->getMessage());
1412         }
1413         $eventdata['relateduserid'] = 1;
1415         // No other['usercompetencyid'].
1416         $errormsg = 'The \'usercompetencyid\' data in \'other\' must be set.';
1417         try {
1418             \core\event\competency_evidence_created::create($eventdata)->trigger();
1419             $this->fail('Coding exception should have been thrown: ' . $errormsg);
1420         } catch (coding_exception $e) {
1421             $this->assertStringContainsString($errormsg, $e->getMessage());
1422         }
1423         $eventdata['other']['usercompetencyid'] = 1;
1425         // No other['competencyid'].
1426         $errormsg = 'The \'competencyid\' data in \'other\' must be set.';
1427         try {
1428             \core\event\competency_evidence_created::create($eventdata)->trigger();
1429             $this->fail('Coding exception should have been thrown: ' . $errormsg);
1430         } catch (coding_exception $e) {
1431             $this->assertStringContainsString($errormsg, $e->getMessage());
1432         }
1433         $eventdata['other']['competencyid'] = 1;
1435         // No other['action'].
1436         $errormsg = 'The \'action\' data in \'other\' must be set.';
1437         try {
1438             \core\event\competency_evidence_created::create($eventdata)->trigger();
1439             $this->fail('Coding exception should have been thrown: ' . $errormsg);
1440         } catch (coding_exception $e) {
1441             $this->assertStringContainsString($errormsg, $e->getMessage());
1442         }
1443         $eventdata['other']['action'] = 1;
1445         // No other['recommend'].
1446         $errormsg = 'The \'recommend\' data in \'other\' must be set.';
1447         try {
1448             \core\event\competency_evidence_created::create($eventdata)->trigger();
1449             $this->fail('Coding exception should have been thrown: ' . $errormsg);
1450         } catch (coding_exception $e) {
1451             $this->assertStringContainsString($errormsg, $e->getMessage());
1452         }
1453         $eventdata['other']['recommend'] = 1;
1455         // Event should be triggered without any problems.
1456         \core\event\competency_evidence_created::create($eventdata)->trigger();
1457     }
1459     /**
1460      * Test the user competency grade rated event.
1461      *
1462      */
1463     public function test_user_competency_rated() {
1464         $this->resetAfterTest(true);
1465         $this->setAdminUser();
1466         $dg = $this->getDataGenerator();
1467         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1468         $scale = $dg->create_scale(array('scale' => 'A,B,C,D'));
1469         $scaleconfig = array(array('scaleid' => $scale->id));
1470         $scaleconfig[] = array('name' => 'A', 'id' => 1, 'scaledefault' => 0, 'proficient' => 0);
1471         $scaleconfig[] = array('name' => 'B', 'id' => 2, 'scaledefault' => 1, 'proficient' => 0);
1472         $scaleconfig[] = array('name' => 'C', 'id' => 3, 'scaledefault' => 0, 'proficient' => 1);
1473         $scaleconfig[] = array('name' => 'D', 'id' => 4, 'scaledefault' => 0, 'proficient' => 1);
1474         $fr = $lpg->create_framework();
1475         $c = $lpg->create_competency(array(
1476             'competencyframeworkid' => $fr->get('id'),
1477             'scaleid' => $scale->id,
1478             'scaleconfiguration' => $scaleconfig
1479         ));
1481         $user = $dg->create_user();
1482         $uc = $lpg->create_user_competency(array(
1483             'userid' => $user->id,
1484             'competencyid' => $c->get('id')));
1486         // Trigger and capture the event.
1487         $sink = $this->redirectEvents();
1488         api::grade_competency($user->id, $c->get('id'), 2, true);
1490         // Get our event event.
1491         $events = $sink->get_events();
1492         // Evidence created.
1493         $this->assertCount(2, $events);
1494         $evidencecreatedevent = $events[0];
1495         $event = $events[1];
1497         // Check that the event data is valid.
1498         $this->assertInstanceOf('\core\event\competency_evidence_created', $evidencecreatedevent);
1499         $this->assertInstanceOf('\core\event\competency_user_competency_rated', $event);
1500         $this->assertEquals($uc->get('id'), $event->objectid);
1501         $this->assertEquals($uc->get_context()->id, $event->contextid);
1502         $this->assertEquals($uc->get('userid'), $event->relateduserid);
1503         $this->assertEquals(2, $event->other['grade']);
1504         $this->assertEventContextNotUsed($event);
1505         $this->assertDebuggingNotCalled();
1506     }
1508     /**
1509      * Test the user competency grade rated in course event.
1510      *
1511      */
1512     public function test_user_competency_rated_in_course() {
1513         $this->resetAfterTest(true);
1514         $this->setAdminUser();
1515         $dg = $this->getDataGenerator();
1516         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1517         $scale = $dg->create_scale(array('scale' => 'A,B,C,D'));
1518         $course = $dg->create_course();
1519         $user = $dg->create_user();
1520         $studentarch = get_archetype_roles('student');
1521         $studentrole = array_shift($studentarch);
1522         $scaleconfig = array(array('scaleid' => $scale->id));
1523         $scaleconfig[] = array('name' => 'A', 'id' => 1, 'scaledefault' => 0, 'proficient' => 0);
1524         $scaleconfig[] = array('name' => 'B', 'id' => 2, 'scaledefault' => 1, 'proficient' => 0);
1525         $scaleconfig[] = array('name' => 'C', 'id' => 3, 'scaledefault' => 0, 'proficient' => 1);
1526         $scaleconfig[] = array('name' => 'D', 'id' => 4, 'scaledefault' => 0, 'proficient' => 1);
1527         $fr = $lpg->create_framework();
1528         $c = $lpg->create_competency(array(
1529             'competencyframeworkid' => $fr->get('id'),
1530             'scaleid' => $scale->id,
1531             'scaleconfiguration' => $scaleconfig
1532         ));
1533         // Enrol the user as students in course.
1534         $dg->enrol_user($user->id, $course->id, $studentrole->id);
1535         $lpg->create_course_competency(array(
1536             'courseid' => $course->id,
1537             'competencyid' => $c->get('id')));
1538         $uc = $lpg->create_user_competency(array(
1539             'userid' => $user->id,
1540             'competencyid' => $c->get('id')));
1542         // Trigger and capture the event.
1543         $sink = $this->redirectEvents();
1544         api::grade_competency_in_course($course->id, $user->id, $c->get('id'), 2, true);
1546         // Get our event event.
1547         $events = $sink->get_events();
1548         // Evidence created.
1549         $this->assertCount(2, $events);
1550         $evidencecreatedevent = $events[0];
1551         $event = $events[1];
1553         // Check that the event data is valid.
1554         $this->assertInstanceOf('\core\event\competency_evidence_created', $evidencecreatedevent);
1555         $this->assertInstanceOf('\core\event\competency_user_competency_rated_in_course', $event);
1556         $this->assertEquals(context_course::instance($course->id)->id, $event->contextid);
1557         $this->assertEquals($course->id, $event->courseid);
1558         $this->assertEquals($uc->get('userid'), $event->relateduserid);
1559         $this->assertEquals($uc->get('competencyid'), $event->other['competencyid']);
1560         $this->assertEquals(2, $event->other['grade']);
1561         $this->assertEventContextNotUsed($event);
1562         $this->assertDebuggingNotCalled();
1563     }
1565     /**
1566      * Test the user competency grade rated in plan event.
1567      *
1568      */
1569     public function test_user_competency_rated_in_plan() {
1570          $this->resetAfterTest(true);
1571         $this->setAdminUser();
1572         $dg = $this->getDataGenerator();
1573         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1574         $scale = $dg->create_scale(array('scale' => 'A,B,C,D'));
1575         $user = $dg->create_user();
1576         $scaleconfig = array(array('scaleid' => $scale->id));
1577         $scaleconfig[] = array('name' => 'A', 'id' => 1, 'scaledefault' => 0, 'proficient' => 0);
1578         $scaleconfig[] = array('name' => 'B', 'id' => 2, 'scaledefault' => 1, 'proficient' => 0);
1579         $scaleconfig[] = array('name' => 'C', 'id' => 3, 'scaledefault' => 0, 'proficient' => 1);
1580         $scaleconfig[] = array('name' => 'D', 'id' => 4, 'scaledefault' => 0, 'proficient' => 1);
1581         $plan = $lpg->create_plan(array('userid' => $user->id));
1582         $fr = $lpg->create_framework();
1583         $c = $lpg->create_competency(array(
1584             'competencyframeworkid' => $fr->get('id'),
1585             'scaleid' => $scale->id,
1586             'scaleconfiguration' => $scaleconfig
1587         ));
1588         $pc = $lpg->create_plan_competency(array('planid' => $plan->get('id'), 'competencyid' => $c->get('id')));
1589         $uc = $lpg->create_user_competency(array(
1590             'userid' => $user->id,
1591             'competencyid' => $c->get('id')));
1593         // Trigger and capture the event.
1594         $sink = $this->redirectEvents();
1595         api::grade_competency_in_plan($plan->get('id'), $c->get('id'), 3, true);
1597         // Get our event event.
1598         $events = $sink->get_events();
1599         // Evidence created.
1600         $this->assertCount(2, $events);
1601         $evidencecreatedevent = $events[0];
1602         $event = $events[1];
1604         // Check that the event data is valid.
1605         $this->assertInstanceOf('\core\event\competency_evidence_created', $evidencecreatedevent);
1606         $this->assertInstanceOf('\core\event\competency_user_competency_rated_in_plan', $event);
1607         $this->assertEquals($uc->get('id'), $event->objectid);
1608         $this->assertEquals($uc->get_context()->id, $event->contextid);
1609         $this->assertEquals($uc->get('userid'), $event->relateduserid);
1610         $this->assertEquals($uc->get('competencyid'), $event->other['competencyid']);
1611         $this->assertEquals(3, $event->other['grade']);
1612         $this->assertEventContextNotUsed($event);
1613         $this->assertDebuggingNotCalled();
1614     }
1616     /**
1617      * Test user competency comment created event.
1618      */
1619     public function test_user_competency_comment_created() {
1620         $this->resetAfterTest(true);
1622         $dg = $this->getDataGenerator();
1623         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1624         $user = $dg->create_user();
1625         $this->setUser($user);
1626         $fr = $lpg->create_framework();
1627         $c = $lpg->create_competency(array('competencyframeworkid' => $fr->get('id')));
1628         $uc = $lpg->create_user_competency(array(
1629             'userid' => $user->id,
1630             'competencyid' => $c->get('id')
1631         ));
1633         $context = context_user::instance($user->id);
1634         $cmt = new stdClass();
1635         $cmt->context = $context;
1636         $cmt->area = 'user_competency';
1637         $cmt->itemid = $uc->get('id');
1638         $cmt->component = 'competency';
1639         $cmt->showcount = 1;
1640         $manager = new comment($cmt);
1642         // Triggering and capturing the event.
1643         $sink = $this->redirectEvents();
1644         $manager->add("New comment for user competency");
1645         $events = $sink->get_events();
1646         // Add comment will trigger 2 other events message_viewed and message_sent.
1647         $this->assertCount(1, $events);
1648         $event = reset($events);
1650         // Checking that the event contains the expected values.
1651         $this->assertInstanceOf('\core\event\competency_comment_created', $event);
1652         $this->assertEquals($context, $event->get_context());
1653         $this->assertEquals($uc->get('id'), $event->other['itemid']);
1654         $this->assertEventContextNotUsed($event);
1655         $this->assertDebuggingNotCalled();
1656     }
1658     /**
1659      * Test plan comment deleted event.
1660      */
1661     public function test_user_competency_comment_deleted() {
1662         $this->resetAfterTest(true);
1663         $this->setAdminUser();
1664         $dg = $this->getDataGenerator();
1665         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1666         $user = $dg->create_user();
1667         $fr = $lpg->create_framework();
1668         $c = $lpg->create_competency(array('competencyframeworkid' => $fr->get('id')));
1669         $uc = $lpg->create_user_competency(array(
1670             'userid' => $user->id,
1671             'competencyid' => $c->get('id')
1672         ));
1673         $context = context_user::instance($user->id);
1675         $cmt = new stdClass();
1676         $cmt->context = $context;
1677         $cmt->area = 'user_competency';
1678         $cmt->itemid = $uc->get('id');
1679         $cmt->component = 'competency';
1680         $manager = new comment($cmt);
1681         $newcomment = $manager->add("Comment to be deleted");
1683         // Triggering and capturing the event.
1684         $sink = $this->redirectEvents();
1685         $manager->delete($newcomment->id);
1686         $events = $sink->get_events();
1687         $this->assertCount(1, $events);
1688         $event = reset($events);
1690         // Checking that the event contains the expected values.
1691         $this->assertInstanceOf('\core\event\competency_comment_deleted', $event);
1692         $this->assertEquals($context, $event->get_context());
1693         $this->assertEquals($uc->get('id'), $event->other['itemid']);
1694         $this->assertEventContextNotUsed($event);
1695         $this->assertDebuggingNotCalled();
1696     }