MDL-53700 competency: Migrating comment related APIs to core
[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;
31 /**
32  * Event tests.
33  *
34  * @package    core_competency
35  * @copyright  2016 Serge Gauthier <serge.gauthier.2@umontreal.ca>
36  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37  */
38 class core_competency_event_testcase extends advanced_testcase {
40     /**
41      * Test the competency framework created event.
42      *
43      */
44     public function test_competency_framework_created() {
45         $this->resetAfterTest(true);
46         $this->setAdminUser();
47         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
49         // Use DataGenerator to have a record framework with the right format.
50         $record = $lpg->create_framework()->to_record();
51         $record->id = 0;
52         $record->shortname = "New shortname";
53         $record->idnumber = "New idnumber";
55         // Trigger and capture the event.
56         $sink = $this->redirectEvents();
57         $framework = api::create_framework((object) $record);
59         // Get our event event.
60         $events = $sink->get_events();
61         $event = reset($events);
63         // Check that the event data is valid.
64         $this->assertInstanceOf('\core\event\competency_framework_created', $event);
65         $this->assertEquals($framework->get_id(), $event->objectid);
66         $this->assertEquals($framework->get_contextid(), $event->contextid);
67         $this->assertEventContextNotUsed($event);
68         $this->assertDebuggingNotCalled();
69     }
71     /**
72      * Test the competency framework deleted event.
73      *
74      */
75     public function test_competency_framework_deleted() {
76         $this->resetAfterTest(true);
77         $this->setAdminUser();
78         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
80         $framework = $lpg->create_framework();
82         // Trigger and capture the event.
83         $sink = $this->redirectEvents();
84         api::delete_framework($framework->get_id());
86         // Get our event event.
87         $events = $sink->get_events();
88         $event = reset($events);
90         // Check that the event data is valid.
91         $this->assertInstanceOf('\core\event\competency_framework_deleted', $event);
92         $this->assertEquals($framework->get_id(), $event->objectid);
93         $this->assertEquals($framework->get_contextid(), $event->contextid);
94         $this->assertEventContextNotUsed($event);
95         $this->assertDebuggingNotCalled();
96     }
98     /**
99      * Test the competency framework updated event.
100      *
101      */
102     public function test_competency_framework_updated() {
103         $this->resetAfterTest(true);
104         $this->setAdminUser();
105         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
107         $framework = $lpg->create_framework();
109         // Trigger and capture the event.
110         $sink = $this->redirectEvents();
111         $framework->set_shortname('Shortname modified');
112         api::update_framework($framework->to_record());
114         // Get our event event.
115         $events = $sink->get_events();
116         $event = reset($events);
118         // Check that the event data is valid.
119         $this->assertInstanceOf('\core\event\competency_framework_updated', $event);
120         $this->assertEquals($framework->get_id(), $event->objectid);
121         $this->assertEquals($framework->get_contextid(), $event->contextid);
122         $this->assertEventContextNotUsed($event);
123         $this->assertDebuggingNotCalled();
124     }
126     /**
127      * Test the competency framework viewed event.
128      *
129      */
130     public function test_competency_framework_viewed() {
131         $this->resetAfterTest(true);
132         $this->setAdminUser();
133         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
135         $framework = $lpg->create_framework();
137         // Trigger and capture the event.
138         $sink = $this->redirectEvents();
139         api::competency_framework_viewed($framework);
141         // Get our event event.
142         $events = $sink->get_events();
143         $event = reset($events);
145         // Check that the event data is valid.
146         $this->assertInstanceOf('\core\event\competency_framework_viewed', $event);
147         $this->assertEquals($framework->get_id(), $event->objectid);
148         $this->assertEquals($framework->get_contextid(), $event->contextid);
149         $this->assertEventContextNotUsed($event);
150         $this->assertDebuggingNotCalled();
151     }
153     /**
154      * Test the competency viewed event.
155      *
156      */
157     public function test_competency_viewed() {
158         $this->resetAfterTest(true);
159         $this->setAdminUser();
160         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
161         $framework = $lpg->create_framework();
162         $competency = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
164         // Trigger and capture the event.
165         $sink = $this->redirectEvents();
166         api::competency_viewed($competency);
167         // Get our event event.
168         $events = $sink->get_events();
169         $event = reset($events);
170         // Check that the event data is valid.
171         $this->assertInstanceOf('\core\event\competency_viewed', $event);
172         $this->assertEquals($competency->get_id(), $event->objectid);
173         $this->assertEquals($competency->get_context()->id, $event->contextid);
174         $this->assertEventContextNotUsed($event);
175         $this->assertDebuggingNotCalled();
176     }
178     /**
179      * Test the template viewed event.
180      *
181      */
182     public function test_template_viewed() {
183         $this->resetAfterTest(true);
184         $this->setAdminUser();
185         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
186         $template = $lpg->create_template();
187         // Trigger and capture the event.
188         $sink = $this->redirectEvents();
189         api::template_viewed($template);
190         // Get our event event.
191         $events = $sink->get_events();
192         $event = reset($events);
193         // Check that the event data is valid.
194         $this->assertInstanceOf('\core\event\competency_template_viewed', $event);
195         $this->assertEquals($template->get_id(), $event->objectid);
196         $this->assertEquals($template->get_contextid(), $event->contextid);
197         $this->assertEventContextNotUsed($event);
198         $this->assertDebuggingNotCalled();
199     }
201     /**
202      * Test the template created event.
203      *
204      */
205     public function test_template_created() {
206         $this->resetAfterTest(true);
207         $this->setAdminUser();
208         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
210         // Use DataGenerator to have a template record with the right format.
211         $record = $lpg->create_template()->to_record();
212         $record->id = 0;
213         $record->shortname = "New shortname";
215         // Trigger and capture the event.
216         $sink = $this->redirectEvents();
217         $template = api::create_template((object) $record);
219         // Get our event event.
220         $events = $sink->get_events();
221         $event = reset($events);
223         $this->assertInstanceOf('\core\event\competency_template_created', $event);
224         $this->assertEquals($template->get_id(), $event->objectid);
225         $this->assertEquals($template->get_contextid(), $event->contextid);
226         $this->assertEventContextNotUsed($event);
227         $this->assertDebuggingNotCalled();
228     }
230     /**
231      * Test the template deleted event.
232      *
233      */
234     public function test_template_deleted() {
235         $this->resetAfterTest(true);
236         $this->setAdminUser();
237         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
239         $template = $lpg->create_template();
241         // Trigger and capture the event.
242         $sink = $this->redirectEvents();
243         api::delete_template($template->get_id());
245         // Get our event event.
246         $events = $sink->get_events();
247         $event = reset($events);
249         // Check that the event data is valid.
250         $this->assertInstanceOf('\core\event\competency_template_deleted', $event);
251         $this->assertEquals($template->get_id(), $event->objectid);
252         $this->assertEquals($template->get_contextid(), $event->contextid);
253         $this->assertEventContextNotUsed($event);
254         $this->assertDebuggingNotCalled();
255     }
257     /**
258      * Test the template updated event.
259      *
260      */
261     public function test_template_updated() {
262         $this->resetAfterTest(true);
263         $this->setAdminUser();
264         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
266         $template = $lpg->create_template();
268         // Trigger and capture the event.
269         $sink = $this->redirectEvents();
270         $template->set_shortname('Shortname modified');
271         api::update_template($template->to_record());
273         // Get our event event.
274         $events = $sink->get_events();
275         $event = reset($events);
277         // Check that the event data is valid.
278         $this->assertInstanceOf('\core\event\competency_template_updated', $event);
279         $this->assertEquals($template->get_id(), $event->objectid);
280         $this->assertEquals($template->get_contextid(), $event->contextid);
281         $this->assertEventContextNotUsed($event);
282         $this->assertDebuggingNotCalled();
283     }
285     /**
286      * Test the competency updated event.
287      *
288      */
289     public function test_competency_updated() {
290         $this->resetAfterTest(true);
291         $this->setAdminUser();
292         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
294         $f1 = $lpg->create_framework();
295         $competency = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
296         $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
297         $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
298         $c12 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1->get_id()));
299         $c13 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1->get_id()));
301         // Trigger and capture the event.
302         $sink = $this->redirectEvents();
303         $competency->set_shortname('Shortname modified');
304         api::update_competency($competency->to_record());
306         // Get our event event.
307         $events = $sink->get_events();
308         $event = reset($events);
310         // Check that the event data is valid.
311         $this->assertInstanceOf('\core\event\competency_updated', $event);
312         $this->assertEquals($competency->get_id(), $event->objectid);
313         $this->assertEquals($competency->get_context()->id, $event->contextid);
314         $this->assertEventContextNotUsed($event);
315         $this->assertDebuggingNotCalled();
316     }
318     /**
319      * Test the competency created event.
320      *
321      */
322     public function test_competency_created() {
323         $this->resetAfterTest(true);
324         $this->setAdminUser();
325         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
327         $f1 = $lpg->create_framework();
328         $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
329         $record = $c1->to_record();
330         $record->id = 0;
331         $record->idnumber = 'comp idnumber';
333         // Trigger and capture the event.
334         $sink = $this->redirectEvents();
335         // Create competency should trigger a created event.
336         $competency = api::create_competency($record);
338         // Get our event event.
339         $events = $sink->get_events();
340         $event = reset($events);
342         $this->assertInstanceOf('\core\event\competency_created', $event);
343         $this->assertEquals($competency->get_id(), $event->objectid);
344         $this->assertEquals($competency->get_context()->id, $event->contextid);
345         $this->assertEventContextNotUsed($event);
346         $this->assertDebuggingNotCalled();
347     }
349     /**
350      * Test the competency created event by duplicate framework.
351      *
352      */
353     public function test_competency_created_by_duplicateframework() {
354         $this->resetAfterTest(true);
355         $this->setAdminUser();
356         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
358         $f1 = $lpg->create_framework();
359         $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
360         $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
361         $c12 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1->get_id()));
363         // Trigger and capture the event.
364         $sink = $this->redirectEvents();
365         // Create framework should trigger a created event for competencies.
366         api::duplicate_framework($f1->get_id());
368         // Get our event event.
369         $events = $sink->get_events();
370         $this->assertEquals(4, count($events));
372         $event = array_shift($events);
373         $this->assertInstanceOf('\core\event\competency_created', $event);
375         $event = array_shift($events);
376         $this->assertInstanceOf('\core\event\competency_created', $event);
378         $event = array_shift($events);
379         $this->assertInstanceOf('\core\event\competency_created', $event);
381         $event = array_shift($events);
382         $this->assertInstanceOf('\core\event\competency_framework_created', $event);
383     }
385     /**
386      * Test the competency deleted event.
387      *
388      */
389     public function test_competency_deleted() {
390         $this->resetAfterTest(true);
391         $this->setAdminUser();
392         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
394         $f1 = $lpg->create_framework();
395         $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
396         $c1id = $c1->get_id();
397         $contextid = $c1->get_context()->id;
399         // Trigger and capture the event.
400         $sink = $this->redirectEvents();
401         // Delete competency should trigger a deleted event.
402         api::delete_competency($c1id);
404         // Get our event event.
405         $events = $sink->get_events();
406         $event = reset($events);
408         $this->assertInstanceOf('\core\event\competency_deleted', $event);
409         $this->assertEquals($c1id, $event->objectid);
410         $this->assertEquals($contextid, $event->contextid);
411         $this->assertEventContextNotUsed($event);
412         $this->assertDebuggingNotCalled();
413     }
415     /**
416      * Test the competency deleted event by delete framework.
417      *
418      */
419     public function test_competency_deleted_by_deleteframework() {
420         $this->resetAfterTest(true);
421         $this->setAdminUser();
422         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
424         $f1 = $lpg->create_framework();
425         $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
426         $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
427         $c12 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'parentid' => $c1->get_id()));
429         // Trigger and capture the event.
430         $sink = $this->redirectEvents();
431         // Delete framework should trigger a deleted event for competencies.
432         api::delete_framework($f1->get_id());
434         // Get our event event.
435         $events = $sink->get_events();
436         $this->assertEquals(4, count($events));
438         $event = array_shift($events);
439         $this->assertInstanceOf('\core\event\competency_framework_deleted', $event);
441         $event = array_shift($events);
442         $this->assertInstanceOf('\core\event\competency_deleted', $event);
444         $event = array_shift($events);
445         $this->assertInstanceOf('\core\event\competency_deleted', $event);
447         $event = array_shift($events);
448         $this->assertInstanceOf('\core\event\competency_deleted', $event);
449     }
451     /**
452      * Test the plan created event.
453      *
454      */
455     public function test_plan_created() {
456         $this->resetAfterTest(true);
457         $this->setAdminUser();
458         $dg = $this->getDataGenerator();
459         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
460         $user = $dg->create_user();
461         $plan = array (
462             'name' => 'plan',
463             'userid' => $user->id
464         );
465         // Trigger and capture the event.
466         $sink = $this->redirectEvents();
467         $plan = api::create_plan((object)$plan);
468         // Get our event event.
469         $events = $sink->get_events();
470         $event = reset($events);
471         // Check that the event data is valid.
472         $this->assertInstanceOf('\core\event\competency_plan_created', $event);
473         $this->assertEquals($plan->get_id(), $event->objectid);
474         $this->assertEquals($plan->get_context()->id, $event->contextid);
475         $this->assertEventContextNotUsed($event);
476         $this->assertDebuggingNotCalled();
477     }
479     /**
480      * Test the plan created event using template_cohort.
481      *
482      */
483     public function test_plan_created_using_templatecohort() {
484         $this->resetAfterTest(true);
485         $this->setAdminUser();
486         $dg = $this->getDataGenerator();
487         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
488         $user1 = $dg->create_user();
489         $user2 = $dg->create_user();
490         $c1 = $dg->create_cohort();
491         // Add 2 users to the cohort.
492         cohort_add_member($c1->id, $user1->id);
493         cohort_add_member($c1->id, $user2->id);
494         $t1 = $lpg->create_template();
495         $tc = $lpg->create_template_cohort(array(
496             'templateid' => $t1->get_id(),
497             'cohortid' => $c1->id
498         ));
499         // Trigger and capture the event.
500         $sink = $this->redirectEvents();
501         api::create_plans_from_template_cohort($t1->get_id(), $c1->id);
502         // Get our event event.
503         $plans = core_competency\plan::get_records(array('templateid' => $t1->get_id()), 'id');
504         $events = $sink->get_events();
505         $this->assertCount(2, $events);
506         $this->assertCount(2, $plans);
507         $event = $events[0];
508         $plan = $plans[0];
509         // Check that the event data is valid.
510         $this->assertInstanceOf('\core\event\competency_plan_created', $event);
511         $this->assertEquals($plan->get_id(), $event->objectid);
512         $this->assertEquals($plan->get_context()->id, $event->contextid);
513         $event = $events[1];
514         $plan = $plans[1];
515         $this->assertInstanceOf('\core\event\competency_plan_created', $event);
516         $this->assertEquals($plan->get_id(), $event->objectid);
517         $this->assertEquals($plan->get_context()->id, $event->contextid);
518         $this->assertEventContextNotUsed($event);
519         $this->assertDebuggingNotCalled();
520     }
522     /**
523      * Test the plan updated event.
524      *
525      */
526     public function test_plan_updated() {
527         $this->resetAfterTest(true);
528         $this->setAdminUser();
529         $dg = $this->getDataGenerator();
530         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
531         $user1 = $dg->create_user();
532         $plan = $lpg->create_plan(array('userid' => $user1->id));
533         $record = $plan->to_record();
534         $record->name = 'Plan updated';
535         // Trigger and capture the event.
536         $sink = $this->redirectEvents();
537         $plan = api::update_plan($record);
538         $this->assertEquals('Plan updated', $plan->get_name());
540         // Get our event event.
541         $events = $sink->get_events();
542         $event = reset($events);
543         $this->assertInstanceOf('\core\event\competency_plan_updated', $event);
544         $this->assertEquals($plan->get_id(), $event->objectid);
545         $this->assertEquals($plan->get_context()->id, $event->contextid);
546         $this->assertEventContextNotUsed($event);
547         $this->assertDebuggingNotCalled();
548     }
550     /**
551      * Test the plan deleted event.
552      *
553      */
554     public function test_plan_deleted() {
555         $this->resetAfterTest(true);
556         $this->setAdminUser();
557         $dg = $this->getDataGenerator();
558         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
559         $user1 = $dg->create_user();
560         $plan = $lpg->create_plan(array('userid' => $user1->id));
561         $planid = $plan->get_id();
562         $contextid = $plan->get_context()->id;
563         // Trigger and capture the event.
564         $sink = $this->redirectEvents();
565         $result = api::delete_plan($plan->get_id());
566         $this->assertTrue($result);
567         // Get our event event.
568         $events = $sink->get_events();
569         $event = reset($events);
570         $this->assertInstanceOf('\core\event\competency_plan_deleted', $event);
571         $this->assertEquals($planid, $event->objectid);
572         $this->assertEquals($contextid, $event->contextid);
573         $this->assertEventContextNotUsed($event);
574         $this->assertDebuggingNotCalled();
575     }
577     /**
578      * Test the plan viewed event.
579      *
580      */
581     public function test_plan_viewed() {
582         $this->resetAfterTest(true);
583         $this->setAdminUser();
584         $dg = $this->getDataGenerator();
585         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
586         $user1 = $dg->create_user();
587         $plan = $lpg->create_plan(array('userid' => $user1->id));
588         // Trigger and capture the event.
589         $sink = $this->redirectEvents();
590         api::plan_viewed($plan);
591         // Get our event event.
592         $events = $sink->get_events();
593         $event = reset($events);
594         // Check that the event data is valid.
595         $this->assertInstanceOf('\core\event\competency_plan_viewed', $event);
596         $this->assertEquals($plan->get_id(), $event->objectid);
597         $this->assertEquals($plan->get_context()->id, $event->contextid);
598         $this->assertEventContextNotUsed($event);
599         $this->assertDebuggingNotCalled();
600     }
602     /**
603      * Test the evidence of prior learning created event.
604      *
605      */
606     public function test_user_evidence_created() {
607         $this->resetAfterTest(true);
608         $this->setAdminUser();
609         $dg = $this->getDataGenerator();
610         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
612         $user = $dg->create_user();
613         // Use DataGenerator to have a user_evidence record with the right format.
614         $record = $userevidence = $lpg->create_user_evidence(array('userid' => $user->id))->to_record();
615         $record->id = 0;
616         $record->name = "New name";
618         // Trigger and capture the event.
619         $sink = $this->redirectEvents();
620         $userevidence = api::create_user_evidence((object) $record);
622          // Get our event event.
623         $events = $sink->get_events();
624         $event = reset($events);
626         $this->assertInstanceOf('\core\event\competency_user_evidence_created', $event);
627         $this->assertEquals($userevidence->get_id(), $event->objectid);
628         $this->assertEquals($userevidence->get_context()->id, $event->contextid);
629         $this->assertEventContextNotUsed($event);
630         $this->assertDebuggingNotCalled();
631     }
633     /**
634      * Test the evidence of prior learning  deleted event.
635      *
636      */
637     public function test_user_evidence_deleted() {
638         $this->resetAfterTest(true);
639         $this->setAdminUser();
640         $dg = $this->getDataGenerator();
641         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
643         $user = $dg->create_user();
644         $userevidence = $lpg->create_user_evidence(array('userid' => $user->id));
646         // Trigger and capture the event.
647         $sink = $this->redirectEvents();
648         api::delete_user_evidence($userevidence->get_id());
650         // Get our event event.
651         $events = $sink->get_events();
652         $event = reset($events);
654         // Check that the event data is valid.
655         $this->assertInstanceOf('\core\event\competency_user_evidence_deleted', $event);
656         $this->assertEquals($userevidence->get_id(), $event->objectid);
657         $this->assertEquals($userevidence->get_context()->id, $event->contextid);
658         $this->assertEventContextNotUsed($event);
659         $this->assertDebuggingNotCalled();
660     }
662     /**
663      * Test the evidence of prior learning  updated event.
664      *
665      */
666     public function test_user_evidence_updated() {
667         $this->resetAfterTest(true);
668         $this->setAdminUser();
669         $dg = $this->getDataGenerator();
670         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
672         $user = $dg->create_user();
673         $userevidence = $lpg->create_user_evidence(array('userid' => $user->id));
675         // Trigger and capture the event.
676         $sink = $this->redirectEvents();
677         $userevidence->set_name('Name modified');
678         api::update_user_evidence($userevidence->to_record());
680          // Get our event event.
681         $events = $sink->get_events();
682         $event = reset($events);
684         // Check that the event data is valid.
685         $this->assertInstanceOf('\core\event\competency_user_evidence_updated', $event);
686         $this->assertEquals($userevidence->get_id(), $event->objectid);
687         $this->assertEquals($userevidence->get_context()->id, $event->contextid);
688         $this->assertEventContextNotUsed($event);
689         $this->assertDebuggingNotCalled();
690     }
692     /**
693      * Test the user competency viewed event in plan.
694      *
695      */
696     public function test_user_competency_viewed_in_plan() {
697         $this->resetAfterTest(true);
698         $this->setAdminUser();
699         $dg = $this->getDataGenerator();
700         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
701         $user = $dg->create_user();
702         $plan = $lpg->create_plan(array('userid' => $user->id));
703         $fr = $lpg->create_framework();
704         $c = $lpg->create_competency(array('competencyframeworkid' => $fr->get_id()));
705         $pc = $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c->get_id()));
706         $uc = $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c->get_id()));
708         // Can not log the event for user competency using completed plan.
709         api::complete_plan($plan);
711         try {
712             api::user_competency_viewed_in_plan($uc, $plan->get_id());
713             $this->fail('To log the user competency in completed plan '
714                     . 'use user_competency_plan_viewed method.');
715         } catch (coding_exception $e) {
716             $this->assertRegExp('/To log the user competency in completed plan '
717                     . 'use user_competency_plan_viewed method./', $e->getMessage());
718         }
720         api::reopen_plan($plan);
721         // Trigger and capture the event.
722         $sink = $this->redirectEvents();
723         api::user_competency_viewed_in_plan($uc, $plan->get_id());
725         // Get our event event.
726         $events = $sink->get_events();
727         $event = reset($events);
729         // Check that the event data is valid.
730         $this->assertInstanceOf('\core\event\competency_user_competency_viewed_in_plan', $event);
731         $this->assertEquals($uc->get_id(), $event->objectid);
732         $this->assertEquals($uc->get_context()->id, $event->contextid);
733         $this->assertEquals($uc->get_userid(), $event->relateduserid);
734         $this->assertEquals($plan->get_id(), $event->other['planid']);
735         $this->assertEquals($c->get_id(), $event->other['competencyid']);
737         $this->assertEventContextNotUsed($event);
738         $this->assertDebuggingNotCalled();
740         // Test validation.
741         $params = array (
742             'objectid' => $uc->get_id(),
743             'contextid' => $uc->get_context()->id,
744             'other' => null
745         );
747         // Other value null.
748         try {
749             \core\event\competency_user_competency_viewed_in_plan::create($params)->trigger();
750             $this->fail('The \'competencyid\' and \'planid\' values must be set.');
751         } catch (coding_exception $e) {
752             $this->assertRegExp("/The 'competencyid' and 'planid' values must be set./", $e->getMessage());
753         }
755         $params['other']['anythingelse'] = '';
756         // Missing competencyid.
757         try {
758             \core\event\competency_user_competency_viewed_in_plan::create($params)->trigger();
759             $this->fail('The \'competencyid\' value must be set.');
760         } catch (coding_exception $e) {
761             $this->assertRegExp("/The 'competencyid' value must be set./", $e->getMessage());
762         }
764         $params['other']['competencyid'] = $c->get_id();
765         // Missing planid.
766         try {
767             \core\event\competency_user_competency_viewed_in_plan::create($params)->trigger();
768             $this->fail('The \'planid\' value must be set.');
769         } catch (coding_exception $e) {
770             $this->assertRegExp("/The 'planid' value must be set./", $e->getMessage());
771         }
772     }
774     /**
775      * Test the user competency viewed event in course.
776      *
777      */
778     public function test_user_competency_viewed_in_course() {
779         $this->resetAfterTest(true);
780         $this->setAdminUser();
781         $dg = $this->getDataGenerator();
782         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
783         $user = $dg->create_user();
784         $course = $dg->create_course();
785         $fr = $lpg->create_framework();
786         $c = $lpg->create_competency(array('competencyframeworkid' => $fr->get_id()));
787         $pc = $lpg->create_course_competency(array('courseid' => $course->id, 'competencyid' => $c->get_id()));
788         $params = array('userid' => $user->id, 'competencyid' => $c->get_id(), 'courseid' => $course->id);
789         $ucc = $lpg->create_user_competency_course($params);
791         // Trigger and capture the event.
792         $sink = $this->redirectEvents();
793         api::user_competency_viewed_in_course($ucc);
795         // Get our event event.
796         $events = $sink->get_events();
797         $event = reset($events);
799         // Check that the event data is valid.
800         $this->assertInstanceOf('\core\event\competency_user_competency_viewed_in_course', $event);
801         $this->assertEquals($ucc->get_id(), $event->objectid);
802         $this->assertEquals(context_course::instance($course->id)->id, $event->contextid);
803         $this->assertEquals($ucc->get_userid(), $event->relateduserid);
804         $this->assertEquals($course->id, $event->courseid);
805         $this->assertEquals($c->get_id(), $event->other['competencyid']);
807         $this->assertEventContextNotUsed($event);
808         $this->assertDebuggingNotCalled();
810         // Test validation.
811         $params = array (
812             'objectid' => $ucc->get_id(),
813             'contextid' => $ucc->get_context()->id,
814             'other' => null
815         );
817         // Missing courseid.
818         try {
819             \core\event\competency_user_competency_viewed_in_course::create($params)->trigger();
820             $this->fail('The \'courseid\' value must be set.');
821         } catch (coding_exception $e) {
822             $this->assertRegExp("/The 'courseid' value must be set./", $e->getMessage());
823         }
825         $params['contextid'] = context_course::instance($course->id)->id;
826         $params['courseid'] = $course->id;
827         // Missing competencyid.
828         try {
829             \core\event\competency_user_competency_viewed_in_course::create($params)->trigger();
830             $this->fail('The \'competencyid\' value must be set.');
831         } catch (coding_exception $e) {
832             $this->assertRegExp("/The 'competencyid' value must be set./", $e->getMessage());
833         }
834     }
836     /**
837      * Test the user competency plan viewed event.
838      *
839      */
840     public function test_user_competency_plan_viewed() {
841         $this->resetAfterTest(true);
842         $this->setAdminUser();
843         $dg = $this->getDataGenerator();
844         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
845         $user = $dg->create_user();
846         $plan = $lpg->create_plan(array('userid' => $user->id));
847         $fr = $lpg->create_framework();
848         $c = $lpg->create_competency(array('competencyframeworkid' => $fr->get_id()));
849         $ucp = $lpg->create_user_competency_plan(array(
850             'userid' => $user->id,
851             'competencyid' => $c->get_id(),
852             'planid' => $plan->get_id()
853         ));
855         // Can not log the event for user competency using non completed plan.
856         try {
857             api::user_competency_plan_viewed($ucp);
858             $this->fail('To log the user competency in non-completed plan '
859                     . 'use user_competency_viewed_in_plan method.');
860         } catch (coding_exception $e) {
861             $this->assertRegExp('/To log the user competency in non-completed plan '
862                     . 'use user_competency_viewed_in_plan method./', $e->getMessage());
863         }
865         // Complete the plan.
866         api::complete_plan($plan);
868         // Trigger and capture the event.
869         $sink = $this->redirectEvents();
870         api::user_competency_plan_viewed($ucp);
872         // Get our event event.
873         $events = $sink->get_events();
874         $event = reset($events);
876         // Check that the event data is valid.
877         $this->assertInstanceOf('\core\event\competency_user_competency_plan_viewed', $event);
878         $this->assertEquals($ucp->get_id(), $event->objectid);
879         $this->assertEquals($ucp->get_context()->id, $event->contextid);
880         $this->assertEquals($ucp->get_userid(), $event->relateduserid);
881         $this->assertEquals($plan->get_id(), $event->other['planid']);
882         $this->assertEquals($c->get_id(), $event->other['competencyid']);
884         $this->assertEventContextNotUsed($event);
885         $this->assertDebuggingNotCalled();
887         // Test validation.
888         $params = array (
889             'objectid' => $ucp->get_id(),
890             'contextid' => $ucp->get_context()->id,
891             'other' => null
892         );
894         // Other value null.
895         try {
896             \core\event\competency_user_competency_plan_viewed::create($params)->trigger();
897             $this->fail('The \'competencyid\' and \'planid\' values must be set.');
898         } catch (coding_exception $e) {
899             $this->assertRegExp("/The 'competencyid' and 'planid' values must be set./", $e->getMessage());
900         }
902         $params['other']['anythingelse'] = '';
903         // Missing competencyid.
904         try {
905             \core\event\competency_user_competency_plan_viewed::create($params)->trigger();
906             $this->fail('The \'competencyid\' value must be set.');
907         } catch (coding_exception $e) {
908             $this->assertRegExp("/The 'competencyid' value must be set./", $e->getMessage());
909         }
911         $params['other']['competencyid'] = $c->get_id();
912         // Missing planid.
913         try {
914             \core\event\competency_user_competency_plan_viewed::create($params)->trigger();
915             $this->fail('The \'planid\' value must be set.');
916         } catch (coding_exception $e) {
917             $this->assertRegExp("/The 'planid' value must be set./", $e->getMessage());
918         }
919     }
921     /**
922      * Test the user competency viewed event.
923      *
924      */
925     public function test_user_competency_viewed() {
926         $this->resetAfterTest(true);
927         $this->setAdminUser();
928         $dg = $this->getDataGenerator();
929         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
930         $user = $dg->create_user();
931         $fr = $lpg->create_framework();
932         $c = $lpg->create_competency(array('competencyframeworkid' => $fr->get_id()));
933         $uc = $lpg->create_user_competency(array(
934             'userid' => $user->id,
935             'competencyid' => $c->get_id()
936         ));
938         // Trigger and capture the event.
939         $sink = $this->redirectEvents();
940         api::user_competency_viewed($uc);
942         // Get our event event.
943         $events = $sink->get_events();
944         $event = reset($events);
946         // Check that the event data is valid.
947         $this->assertInstanceOf('\core\event\competency_user_competency_viewed', $event);
948         $this->assertEquals($uc->get_id(), $event->objectid);
949         $this->assertEquals($uc->get_context()->id, $event->contextid);
950         $this->assertEquals($uc->get_userid(), $event->relateduserid);
951         $this->assertEquals($c->get_id(), $event->other['competencyid']);
953         $this->assertEventContextNotUsed($event);
954         $this->assertDebuggingNotCalled();
956         // Test validation.
957         $params = array (
958             'objectid' => $uc->get_id(),
959             'contextid' => $uc->get_context()->id
960         );
962         // Missing competencyid.
963         try {
964             \core\event\competency_user_competency_viewed::create($params)->trigger();
965             $this->fail('The \'competencyid\' value must be set.');
966         } catch (coding_exception $e) {
967             $this->assertRegExp("/The 'competencyid' value must be set./", $e->getMessage());
968         }
969     }
971     /**
972      * Test the plan approved event.
973      *
974      */
975     public function test_plan_approved() {
976         $this->resetAfterTest(true);
977         $this->setAdminUser();
978         $dg = $this->getDataGenerator();
979         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
980         $user1 = $dg->create_user();
981         $plan = $lpg->create_plan(array('userid' => $user1->id));
982         $planid = $plan->get_id();
983         $contextid = $plan->get_context()->id;
984         // Trigger and capture the event.
985         $sink = $this->redirectEvents();
986         $result = api::approve_plan($plan->get_id());
987         $this->assertTrue($result);
988         // Get our event event.
989         $events = $sink->get_events();
990         $event = reset($events);
991         $this->assertInstanceOf('\core\event\competency_plan_approved', $event);
992         $this->assertEquals($planid, $event->objectid);
993         $this->assertEquals($contextid, $event->contextid);
994         $this->assertEquals($plan->get_userid(), $event->relateduserid);
995         $this->assertEventContextNotUsed($event);
996         $this->assertDebuggingNotCalled();
997     }
999     /**
1000      * Test the plan unapproved event.
1001      *
1002      */
1003     public function test_plan_unapproved() {
1004         $this->resetAfterTest(true);
1005         $this->setAdminUser();
1006         $dg = $this->getDataGenerator();
1007         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1008         $user1 = $dg->create_user();
1009         $plan = $lpg->create_plan(array('userid' => $user1->id, 'status' => \core_competency\plan::STATUS_ACTIVE));
1010         $planid = $plan->get_id();
1011         $contextid = $plan->get_context()->id;
1012         // Trigger and capture the event.
1013         $sink = $this->redirectEvents();
1014         $result = api::unapprove_plan($plan->get_id());
1015         $this->assertTrue($result);
1016         // Get our event event.
1017         $events = $sink->get_events();
1018         $event = reset($events);
1019         $this->assertInstanceOf('\core\event\competency_plan_unapproved', $event);
1020         $this->assertEquals($planid, $event->objectid);
1021         $this->assertEquals($contextid, $event->contextid);
1022         $this->assertEquals($plan->get_userid(), $event->relateduserid);
1023         $this->assertEventContextNotUsed($event);
1024         $this->assertDebuggingNotCalled();
1025     }
1027     /**
1028      * Test the plan reopened event.
1029      *
1030      */
1031     public function test_plan_reopened() {
1032         $this->resetAfterTest(true);
1033         $this->setAdminUser();
1034         $dg = $this->getDataGenerator();
1035         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1036         $user1 = $dg->create_user();
1037         $plan = $lpg->create_plan(array('userid' => $user1->id, 'status' => \core_competency\plan::STATUS_COMPLETE));
1038         $planid = $plan->get_id();
1039         $contextid = $plan->get_context()->id;
1040         // Trigger and capture the event.
1041         $sink = $this->redirectEvents();
1042         $result = api::reopen_plan($plan->get_id());
1043         $this->assertTrue($result);
1044         // Get our event event.
1045         $events = $sink->get_events();
1046         $event = reset($events);
1047         $this->assertInstanceOf('\core\event\competency_plan_reopened', $event);
1048         $this->assertEquals($planid, $event->objectid);
1049         $this->assertEquals($contextid, $event->contextid);
1050         $this->assertEquals($plan->get_userid(), $event->relateduserid);
1051         $this->assertEventContextNotUsed($event);
1052         $this->assertDebuggingNotCalled();
1053     }
1055     /**
1056      * Test the plan completed event.
1057      *
1058      */
1059     public function test_plan_completed() {
1060         $this->resetAfterTest(true);
1061         $this->setAdminUser();
1062         $dg = $this->getDataGenerator();
1063         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1064         $user1 = $dg->create_user();
1065         $plan = $lpg->create_plan(array('userid' => $user1->id, 'status' => \core_competency\plan::STATUS_ACTIVE));
1066         $planid = $plan->get_id();
1067         $contextid = $plan->get_context()->id;
1068         // Trigger and capture the event.
1069         $sink = $this->redirectEvents();
1070         $result = api::complete_plan($plan->get_id());
1071         $this->assertTrue($result);
1072         // Get our event event.
1073         $events = $sink->get_events();
1074         $event = reset($events);
1075         $this->assertInstanceOf('\core\event\competency_plan_completed', $event);
1076         $this->assertEquals($planid, $event->objectid);
1077         $this->assertEquals($contextid, $event->contextid);
1078         $this->assertEquals($plan->get_userid(), $event->relateduserid);
1079         $this->assertEventContextNotUsed($event);
1080         $this->assertDebuggingNotCalled();
1081     }
1083     /**
1084      * Test the plan unlinked event.
1085      *
1086      */
1087     public function test_plan_unlinked() {
1088         $this->resetAfterTest(true);
1089         $this->setAdminUser();
1090         $dg = $this->getDataGenerator();
1091         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1092         $user1 = $dg->create_user();
1093         $template = $lpg->create_template();
1094         $plan = $lpg->create_plan(array(
1095             'userid' => $user1->id,
1096             'status' => \core_competency\plan::STATUS_ACTIVE,
1097             'templateid' => $template->get_id()
1098         ));
1099         $planid = $plan->get_id();
1100         $contextid = $plan->get_context()->id;
1101         // Trigger and capture the event.
1102         $sink = $this->redirectEvents();
1103         $result = api::unlink_plan_from_template($plan->get_id());
1104         $this->assertTrue($result);
1105         // Get our event event.
1106         $events = $sink->get_events();
1107         $event = reset($events);
1108         $this->assertInstanceOf('\core\event\competency_plan_unlinked', $event);
1109         $this->assertEquals($planid, $event->objectid);
1110         $this->assertEquals($contextid, $event->contextid);
1111         $this->assertEquals($plan->get_userid(), $event->relateduserid);
1112         $this->assertEventContextNotUsed($event);
1113         $this->assertDebuggingNotCalled();
1114     }
1116     /**
1117      * Test the plan review requested event.
1118      *
1119      */
1120     public function test_plan_review_requested() {
1121         $this->resetAfterTest(true);
1122         $this->setAdminUser();
1123         $dg = $this->getDataGenerator();
1124         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1125         $user1 = $dg->create_user();
1126         $plan = $lpg->create_plan(array('userid' => $user1->id));
1127         $planid = $plan->get_id();
1128         $contextid = $plan->get_context()->id;
1129         // Trigger and capture the event.
1130         $sink = $this->redirectEvents();
1131         $result = api::plan_request_review($plan->get_id());
1132         $this->assertTrue($result);
1133         // Get our event event.
1134         $events = $sink->get_events();
1135         $event = reset($events);
1136         $this->assertInstanceOf('\core\event\competency_plan_review_requested', $event);
1137         $this->assertEquals($planid, $event->objectid);
1138         $this->assertEquals($contextid, $event->contextid);
1139         $this->assertEquals($plan->get_userid(), $event->relateduserid);
1140         $this->assertEventContextNotUsed($event);
1141         $this->assertDebuggingNotCalled();
1142     }
1144     /**
1145      * Test the plan review request cancelled event.
1146      *
1147      */
1148     public function test_plan_review_request_cancelled() {
1149         $this->resetAfterTest(true);
1150         $this->setAdminUser();
1151         $dg = $this->getDataGenerator();
1152         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1153         $user1 = $dg->create_user();
1154         $plan = $lpg->create_plan(array('userid' => $user1->id, 'status' => \core_competency\plan::STATUS_WAITING_FOR_REVIEW));
1155         $planid = $plan->get_id();
1156         $contextid = $plan->get_context()->id;
1157         // Trigger and capture the event.
1158         $sink = $this->redirectEvents();
1159         $result = api::plan_cancel_review_request($plan->get_id());
1160         $this->assertTrue($result);
1161         // Get our event event.
1162         $events = $sink->get_events();
1163         $event = reset($events);
1164         $this->assertInstanceOf('\core\event\competency_plan_review_request_cancelled', $event);
1165         $this->assertEquals($planid, $event->objectid);
1166         $this->assertEquals($contextid, $event->contextid);
1167         $this->assertEquals($plan->get_userid(), $event->relateduserid);
1168         $this->assertEventContextNotUsed($event);
1169         $this->assertDebuggingNotCalled();
1170     }
1172     /**
1173      * Test the plan review started event.
1174      *
1175      */
1176     public function test_plan_review_started() {
1177         $this->resetAfterTest(true);
1178         $this->setAdminUser();
1179         $dg = $this->getDataGenerator();
1180         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1181         $user1 = $dg->create_user();
1182         $plan = $lpg->create_plan(array('userid' => $user1->id, 'status' => \core_competency\plan::STATUS_WAITING_FOR_REVIEW));
1183         $planid = $plan->get_id();
1184         $contextid = $plan->get_context()->id;
1185         // Trigger and capture the event.
1186         $sink = $this->redirectEvents();
1187         $result = api::plan_start_review($plan->get_id());
1188         $this->assertTrue($result);
1189         // Get our event event.
1190         $events = $sink->get_events();
1191         $event = reset($events);
1192         $this->assertInstanceOf('\core\event\competency_plan_review_started', $event);
1193         $this->assertEquals($planid, $event->objectid);
1194         $this->assertEquals($contextid, $event->contextid);
1195         $this->assertEquals($plan->get_userid(), $event->relateduserid);
1196         $this->assertEventContextNotUsed($event);
1197         $this->assertDebuggingNotCalled();
1198     }
1200     /**
1201      * Test the plan review stopped event.
1202      *
1203      */
1204     public function test_plan_review_stopped() {
1205         $this->resetAfterTest(true);
1206         $this->setAdminUser();
1207         $dg = $this->getDataGenerator();
1208         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1209         $user1 = $dg->create_user();
1210         $plan = $lpg->create_plan(array('userid' => $user1->id, 'status' => \core_competency\plan::STATUS_IN_REVIEW));
1211         $planid = $plan->get_id();
1212         $contextid = $plan->get_context()->id;
1213         // Trigger and capture the event.
1214         $sink = $this->redirectEvents();
1215         $result = api::plan_stop_review($plan->get_id());
1216         $this->assertTrue($result);
1217         // Get our event event.
1218         $events = $sink->get_events();
1219         $event = reset($events);
1220         $this->assertInstanceOf('\core\event\competency_plan_review_stopped', $event);
1221         $this->assertEquals($planid, $event->objectid);
1222         $this->assertEquals($contextid, $event->contextid);
1223         $this->assertEquals($plan->get_userid(), $event->relateduserid);
1224         $this->assertEventContextNotUsed($event);
1225         $this->assertDebuggingNotCalled();
1226     }
1228     /**
1229      * Test plan comment created event.
1230      */
1231     public function test_plan_comment_created() {
1232         $this->resetAfterTest(true);
1233         $dg = $this->getDataGenerator();
1234         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1235         $user = $dg->create_user();
1236         $this->setUser($user);
1237         $plan = $lpg->create_plan(array('userid' => $user->id));
1238         $context = context_user::instance($user->id);
1240         $cmt = new stdClass();
1241         $cmt->context = $context;
1242         $cmt->area = 'plan';
1243         $cmt->itemid = $plan->get_id();
1244         $cmt->component = 'competency';
1245         $cmt->showcount = 1;
1246         $manager = new comment($cmt);
1247         $manager->set_post_permission(true);
1249         // Triggering and capturing the event.
1250         $sink = $this->redirectEvents();
1251         $manager->add("New comment for plan");
1252         $events = $sink->get_events();
1253         // Add comment will trigger 2 other events message_viewed and message_sent.
1254         $this->assertCount(1, $events);
1255         $event = array_pop($events);
1257         // Checking that the event contains the expected values.
1258         $this->assertInstanceOf('\core\event\competency_comment_created', $event);
1259         $this->assertEquals($context, $event->get_context());
1260         $this->assertEquals($plan->get_id(), $event->other['itemid']);
1261         $this->assertEventContextNotUsed($event);
1262         $this->assertDebuggingNotCalled();
1263     }
1265     /**
1266      * Test plan comment deleted event.
1267      */
1268     public function test_plan_comment_deleted() {
1269         $this->resetAfterTest(true);
1270         $this->setAdminUser();
1271         $dg = $this->getDataGenerator();
1272         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1273         $user1 = $dg->create_user();
1274         $plan = $lpg->create_plan(array('userid' => $user1->id));
1276         $context = context_user::instance($user1->id);
1278         $cmt = new stdClass();
1279         $cmt->context = $context;
1280         $cmt->area = 'plan';
1281         $cmt->itemid = $plan->get_id();
1282         $cmt->component = 'competency';
1283         $manager = new comment($cmt);
1284         $newcomment = $manager->add("Comment to be deleted");
1286         // Triggering and capturing the event.
1287         $sink = $this->redirectEvents();
1288         $manager->delete($newcomment->id);
1289         $events = $sink->get_events();
1290         $this->assertCount(1, $events);
1291         $event = reset($events);
1293         // Checking that the event contains the expected values.
1294         $this->assertInstanceOf('\core\event\competency_comment_deleted', $event);
1295         $this->assertEquals($context, $event->get_context());
1296         $this->assertEquals($plan->get_id(), $event->other['itemid']);
1297         $this->assertEventContextNotUsed($event);
1298         $this->assertDebuggingNotCalled();
1299     }
1301     /**
1302      * Test evidence_created event.
1303      */
1304     public function test_evidence_created() {
1305         global $USER;
1307         $this->resetAfterTest(true);
1308         $dg = $this->getDataGenerator();
1309         $syscontext = context_system::instance();
1311         // Create a student.
1312         $student = $dg->create_user();
1314         // Create a competency for the course.
1315         $lpg = $dg->get_plugin_generator('core_competency');
1316         $framework = $lpg->create_framework();
1317         $comp = $lpg->create_competency(['competencyframeworkid' => $framework->get_id()]);
1319         // Trigger and capture the event.
1320         $sink = $this->redirectEvents();
1322         // Add evidence.
1323         $recommend = false;
1324         $evidence = api::add_evidence($student->id, $comp, $syscontext, \core_competency\evidence::ACTION_OVERRIDE,
1325             'commentincontext', 'core', null, $recommend, null, 1);
1327         // Get event.
1328         $events = $sink->get_events();
1329         $event = reset($events);
1331         // Check that the event data is valid.
1332         $this->assertInstanceOf('\core\event\competency_evidence_created', $event);
1333         $this->assertEquals($evidence->get_contextid(), $event->contextid);
1334         $this->assertEquals($evidence->get_id(), $event->objectid);
1335         $this->assertEquals($evidence->get_actionuserid(), $event->userid);
1336         $this->assertEquals($student->id, $event->relateduserid);
1337         $this->assertEquals($evidence->get_usercompetencyid(), $event->other['usercompetencyid']);
1338         $this->assertEquals($comp->get_id(), $event->other['competencyid']);
1339         $this->assertEquals($evidence->get_action(), $event->other['action']);
1340         $this->assertEquals($recommend, $event->other['recommend']);
1342         // Test get_name().
1343         $this->assertEquals(get_string('eventevidencecreated', 'tool_lp'), $event->get_name());
1345         // Test get_description().
1346         $description = "The user with id '$USER->id' created an evidence with id '{$evidence->get_id()}'.";
1347         $this->assertEquals($description, $event->get_description());
1349         // Test get_url().
1350         $url = new moodle_url('/admin/tool/lp/user_competency.php', ['id' => $evidence->get_usercompetencyid()]);
1351         $this->assertEquals($url, $event->get_url());
1353         // Test get_objectid_mapping().
1354         $this->assertEquals(\core\event\base::NOT_MAPPED, $event->get_objectid_mapping());
1356         $this->assertEventContextNotUsed($event);
1357         $this->assertDebuggingNotCalled();
1358     }
1360     /**
1361      * Test evidence_created event by linking an invalid user competency to an evidence.
1362      */
1363     public function test_evidence_created_with_invalid_user_competency() {
1364         $this->resetAfterTest(true);
1365         $dg = $this->getDataGenerator();
1366         $syscontext = context_system::instance();
1368         // Create students.
1369         $student = $dg->create_user();
1370         $student2 = $dg->create_user();
1372         // Create a competency for the course.
1373         $lpg = $dg->get_plugin_generator('core_competency');
1374         $framework = $lpg->create_framework();
1375         $comp = $lpg->create_competency(['competencyframeworkid' => $framework->get_id()]);
1377         // Create a different user competency.
1378         $otheruc = \core_competency\user_competency::create_relation($student2->id, $comp->get_id());
1379         $otheruc->create();
1381         // Add evidence.
1382         $recommend = false;
1383         $evidence = api::add_evidence($student->id, $comp, $syscontext, \core_competency\evidence::ACTION_OVERRIDE,
1384             'commentincontext', 'core', null, $recommend, null, 1);
1386         // We expect this to fail and throw a coding exception.
1387         $this->setExpectedException('coding_exception', 'The user competency linked with this evidence is invalid.');
1388         \core\event\competency_evidence_created::create_from_evidence($evidence, $otheruc, $recommend)->trigger();
1389     }
1391     /**
1392      * Test creation of evidence_created event with missing data.
1393      *
1394      * These data are validated by \core_competency\evidence_created::validate_data().
1395      */
1396     public function test_evidence_created_with_missing_data() {
1397         $eventdata = [
1398             'contextid'  => 1,
1399             'objectid' => 1,
1400             'userid' => 1
1401         ];
1403         // No relateduserid.
1404         $errormsg = 'The \'relateduserid\' must be set.';
1405         try {
1406             \core\event\competency_evidence_created::create($eventdata)->trigger();
1407             $this->fail('Coding exception should have been thrown: ' . $errormsg);
1408         } catch (coding_exception $e) {
1409             $this->assertContains($errormsg, $e->getMessage());
1410         }
1411         $eventdata['relateduserid'] = 1;
1413         // No other['usercompetencyid'].
1414         $errormsg = 'The \'usercompetencyid\' data in \'other\' must be set.';
1415         try {
1416             \core\event\competency_evidence_created::create($eventdata)->trigger();
1417             $this->fail('Coding exception should have been thrown: ' . $errormsg);
1418         } catch (coding_exception $e) {
1419             $this->assertContains($errormsg, $e->getMessage());
1420         }
1421         $eventdata['other']['usercompetencyid'] = 1;
1423         // No other['competencyid'].
1424         $errormsg = 'The \'competencyid\' data in \'other\' must be set.';
1425         try {
1426             \core\event\competency_evidence_created::create($eventdata)->trigger();
1427             $this->fail('Coding exception should have been thrown: ' . $errormsg);
1428         } catch (coding_exception $e) {
1429             $this->assertContains($errormsg, $e->getMessage());
1430         }
1431         $eventdata['other']['competencyid'] = 1;
1433         // No other['action'].
1434         $errormsg = 'The \'action\' data in \'other\' must be set.';
1435         try {
1436             \core\event\competency_evidence_created::create($eventdata)->trigger();
1437             $this->fail('Coding exception should have been thrown: ' . $errormsg);
1438         } catch (coding_exception $e) {
1439             $this->assertContains($errormsg, $e->getMessage());
1440         }
1441         $eventdata['other']['action'] = 1;
1443         // No other['recommend'].
1444         $errormsg = 'The \'recommend\' data in \'other\' must be set.';
1445         try {
1446             \core\event\competency_evidence_created::create($eventdata)->trigger();
1447             $this->fail('Coding exception should have been thrown: ' . $errormsg);
1448         } catch (coding_exception $e) {
1449             $this->assertContains($errormsg, $e->getMessage());
1450         }
1451         $eventdata['other']['recommend'] = 1;
1453         // Event should be triggered without any problems.
1454         \core\event\competency_evidence_created::create($eventdata)->trigger();
1455     }
1457     /**
1458      * Test the user competency grade rated event.
1459      *
1460      */
1461     public function test_user_competency_rated() {
1462         $this->resetAfterTest(true);
1463         $this->setAdminUser();
1464         $dg = $this->getDataGenerator();
1465         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1466         $scale = $dg->create_scale(array('scale' => 'A,B,C,D'));
1467         $scaleconfig = array(array('scaleid' => $scale->id));
1468         $scaleconfig[] = array('name' => 'A', 'id' => 1, 'scaledefault' => 0, 'proficient' => 0);
1469         $scaleconfig[] = array('name' => 'B', 'id' => 2, 'scaledefault' => 1, 'proficient' => 0);
1470         $scaleconfig[] = array('name' => 'C', 'id' => 3, 'scaledefault' => 0, 'proficient' => 1);
1471         $scaleconfig[] = array('name' => 'D', 'id' => 4, 'scaledefault' => 0, 'proficient' => 1);
1472         $fr = $lpg->create_framework();
1473         $c = $lpg->create_competency(array(
1474             'competencyframeworkid' => $fr->get_id(),
1475             'scaleid' => $scale->id,
1476             'scaleconfiguration' => $scaleconfig
1477         ));
1479         $user = $dg->create_user();
1480         $uc = $lpg->create_user_competency(array(
1481             'userid' => $user->id,
1482             'competencyid' => $c->get_id()));
1484         // Trigger and capture the event.
1485         $sink = $this->redirectEvents();
1486         api::grade_competency($user->id, $c->get_id(), 2, true);
1488         // Get our event event.
1489         $events = $sink->get_events();
1490         // Evidence created.
1491         $this->assertCount(2, $events);
1492         $evidencecreatedevent = $events[0];
1493         $event = $events[1];
1495         // Check that the event data is valid.
1496         $this->assertInstanceOf('\core\event\competency_evidence_created', $evidencecreatedevent);
1497         $this->assertInstanceOf('\core\event\competency_user_competency_rated', $event);
1498         $this->assertEquals($uc->get_id(), $event->objectid);
1499         $this->assertEquals($uc->get_context()->id, $event->contextid);
1500         $this->assertEquals($uc->get_userid(), $event->relateduserid);
1501         $this->assertEquals(2, $event->other['grade']);
1502         $this->assertEventContextNotUsed($event);
1503         $this->assertDebuggingNotCalled();
1504     }
1506     /**
1507      * Test the user competency grade rated in course event.
1508      *
1509      */
1510     public function test_user_competency_rated_in_course() {
1511         $this->resetAfterTest(true);
1512         $this->setAdminUser();
1513         $dg = $this->getDataGenerator();
1514         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1515         $scale = $dg->create_scale(array('scale' => 'A,B,C,D'));
1516         $course = $dg->create_course();
1517         $user = $dg->create_user();
1518         $studentarch = get_archetype_roles('student');
1519         $studentrole = array_shift($studentarch);
1520         $scaleconfig = array(array('scaleid' => $scale->id));
1521         $scaleconfig[] = array('name' => 'A', 'id' => 1, 'scaledefault' => 0, 'proficient' => 0);
1522         $scaleconfig[] = array('name' => 'B', 'id' => 2, 'scaledefault' => 1, 'proficient' => 0);
1523         $scaleconfig[] = array('name' => 'C', 'id' => 3, 'scaledefault' => 0, 'proficient' => 1);
1524         $scaleconfig[] = array('name' => 'D', 'id' => 4, 'scaledefault' => 0, 'proficient' => 1);
1525         $fr = $lpg->create_framework();
1526         $c = $lpg->create_competency(array(
1527             'competencyframeworkid' => $fr->get_id(),
1528             'scaleid' => $scale->id,
1529             'scaleconfiguration' => $scaleconfig
1530         ));
1531         // Enrol the user as students in course.
1532         $dg->enrol_user($user->id, $course->id, $studentrole->id);
1533         $lpg->create_course_competency(array(
1534             'courseid' => $course->id,
1535             'competencyid' => $c->get_id()));
1536         $uc = $lpg->create_user_competency(array(
1537             'userid' => $user->id,
1538             'competencyid' => $c->get_id()));
1540         // Trigger and capture the event.
1541         $sink = $this->redirectEvents();
1542         api::grade_competency_in_course($course->id, $user->id, $c->get_id(), 2, true);
1544         // Get our event event.
1545         $events = $sink->get_events();
1546         // Evidence created.
1547         $this->assertCount(2, $events);
1548         $evidencecreatedevent = $events[0];
1549         $event = $events[1];
1551         // Check that the event data is valid.
1552         $this->assertInstanceOf('\core\event\competency_evidence_created', $evidencecreatedevent);
1553         $this->assertInstanceOf('\core\event\competency_user_competency_rated_in_course', $event);
1554         $this->assertEquals(context_course::instance($course->id)->id, $event->contextid);
1555         $this->assertEquals($course->id, $event->courseid);
1556         $this->assertEquals($uc->get_userid(), $event->relateduserid);
1557         $this->assertEquals($uc->get_competencyid(), $event->other['competencyid']);
1558         $this->assertEquals(2, $event->other['grade']);
1559         $this->assertEventContextNotUsed($event);
1560         $this->assertDebuggingNotCalled();
1561     }
1563     /**
1564      * Test the user competency grade rated in plan event.
1565      *
1566      */
1567     public function test_user_competency_rated_in_plan() {
1568          $this->resetAfterTest(true);
1569         $this->setAdminUser();
1570         $dg = $this->getDataGenerator();
1571         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1572         $scale = $dg->create_scale(array('scale' => 'A,B,C,D'));
1573         $user = $dg->create_user();
1574         $scaleconfig = array(array('scaleid' => $scale->id));
1575         $scaleconfig[] = array('name' => 'A', 'id' => 1, 'scaledefault' => 0, 'proficient' => 0);
1576         $scaleconfig[] = array('name' => 'B', 'id' => 2, 'scaledefault' => 1, 'proficient' => 0);
1577         $scaleconfig[] = array('name' => 'C', 'id' => 3, 'scaledefault' => 0, 'proficient' => 1);
1578         $scaleconfig[] = array('name' => 'D', 'id' => 4, 'scaledefault' => 0, 'proficient' => 1);
1579         $plan = $lpg->create_plan(array('userid' => $user->id));
1580         $fr = $lpg->create_framework();
1581         $c = $lpg->create_competency(array(
1582             'competencyframeworkid' => $fr->get_id(),
1583             'scaleid' => $scale->id,
1584             'scaleconfiguration' => $scaleconfig
1585         ));
1586         $pc = $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c->get_id()));
1587         $uc = $lpg->create_user_competency(array(
1588             'userid' => $user->id,
1589             'competencyid' => $c->get_id()));
1591         // Trigger and capture the event.
1592         $sink = $this->redirectEvents();
1593         api::grade_competency_in_plan($plan->get_id(), $c->get_id(), 3, true);
1595         // Get our event event.
1596         $events = $sink->get_events();
1597         // Evidence created.
1598         $this->assertCount(2, $events);
1599         $evidencecreatedevent = $events[0];
1600         $event = $events[1];
1602         // Check that the event data is valid.
1603         $this->assertInstanceOf('\core\event\competency_evidence_created', $evidencecreatedevent);
1604         $this->assertInstanceOf('\core\event\competency_user_competency_rated_in_plan', $event);
1605         $this->assertEquals($uc->get_id(), $event->objectid);
1606         $this->assertEquals($uc->get_context()->id, $event->contextid);
1607         $this->assertEquals($uc->get_userid(), $event->relateduserid);
1608         $this->assertEquals($uc->get_competencyid(), $event->other['competencyid']);
1609         $this->assertEquals(3, $event->other['grade']);
1610         $this->assertEventContextNotUsed($event);
1611         $this->assertDebuggingNotCalled();
1612     }
1614     /**
1615      * Test user competency comment created event.
1616      */
1617     public function test_user_competency_comment_created() {
1618         $this->resetAfterTest(true);
1620         $dg = $this->getDataGenerator();
1621         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1622         $user = $dg->create_user();
1623         $this->setUser($user);
1624         $fr = $lpg->create_framework();
1625         $c = $lpg->create_competency(array('competencyframeworkid' => $fr->get_id()));
1626         $uc = $lpg->create_user_competency(array(
1627             'userid' => $user->id,
1628             'competencyid' => $c->get_id()
1629         ));
1631         $context = context_user::instance($user->id);
1632         $cmt = new stdClass();
1633         $cmt->context = $context;
1634         $cmt->area = 'user_competency';
1635         $cmt->itemid = $uc->get_id();
1636         $cmt->component = 'competency';
1637         $cmt->showcount = 1;
1638         $manager = new comment($cmt);
1640         // Triggering and capturing the event.
1641         $sink = $this->redirectEvents();
1642         $manager->add("New comment for user competency");
1643         $events = $sink->get_events();
1644         // Add comment will trigger 2 other events message_viewed and message_sent.
1645         $this->assertCount(1, $events);
1646         $event = reset($events);
1648         // Checking that the event contains the expected values.
1649         $this->assertInstanceOf('\core\event\competency_comment_created', $event);
1650         $this->assertEquals($context, $event->get_context());
1651         $this->assertEquals($uc->get_id(), $event->other['itemid']);
1652         $this->assertEventContextNotUsed($event);
1653         $this->assertDebuggingNotCalled();
1654     }
1656     /**
1657      * Test plan comment deleted event.
1658      */
1659     public function test_user_competency_comment_deleted() {
1660         $this->resetAfterTest(true);
1661         $this->setAdminUser();
1662         $dg = $this->getDataGenerator();
1663         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1664         $user = $dg->create_user();
1665         $fr = $lpg->create_framework();
1666         $c = $lpg->create_competency(array('competencyframeworkid' => $fr->get_id()));
1667         $uc = $lpg->create_user_competency(array(
1668             'userid' => $user->id,
1669             'competencyid' => $c->get_id()
1670         ));
1671         $context = context_user::instance($user->id);
1673         $cmt = new stdClass();
1674         $cmt->context = $context;
1675         $cmt->area = 'user_competency';
1676         $cmt->itemid = $uc->get_id();
1677         $cmt->component = 'competency';
1678         $manager = new comment($cmt);
1679         $newcomment = $manager->add("Comment to be deleted");
1681         // Triggering and capturing the event.
1682         $sink = $this->redirectEvents();
1683         $manager->delete($newcomment->id);
1684         $events = $sink->get_events();
1685         $this->assertCount(1, $events);
1686         $event = reset($events);
1688         // Checking that the event contains the expected values.
1689         $this->assertInstanceOf('\core\event\competency_comment_deleted', $event);
1690         $this->assertEquals($context, $event->get_context());
1691         $this->assertEquals($uc->get_id(), $event->other['itemid']);
1692         $this->assertEventContextNotUsed($event);
1693         $this->assertDebuggingNotCalled();
1694     }