2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
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
25 defined('MOODLE_INTERNAL') ||
die();
27 require_once($CFG->dirroot
. '/comment/lib.php');
29 use core_competency\api
;
30 use core_competency\url
;
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
39 class core_competency_event_testcase
extends advanced_testcase
{
42 * Test the competency framework created event.
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();
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();
73 * Test the competency framework deleted event.
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();
100 * Test the competency framework updated event.
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();
128 * Test the competency framework viewed event.
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();
155 * Test the competency viewed event.
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();
180 * Test the template viewed event.
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();
203 * Test the template created event.
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();
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();
232 * Test the template deleted event.
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();
259 * Test the template updated event.
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();
287 * Test the competency updated event.
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();
320 * Test the competency created event.
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();
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();
351 * Test the competency created event by duplicate framework.
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);
387 * Test the competency deleted event.
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();
417 * Test the competency deleted event by delete framework.
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);
453 * Test the plan created event.
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();
464 'userid' => $user->id
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();
481 * Test the plan created event using template_cohort.
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
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);
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
);
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();
524 * Test the plan updated event.
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();
552 * Test the plan deleted event.
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();
579 * Test the plan viewed event.
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();
604 * Test the evidence of prior learning created event.
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();
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();
635 * Test the evidence of prior learning deleted event.
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();
664 * Test the evidence of prior learning updated event.
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();
694 * Test the user competency viewed event in plan.
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);
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());
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();
743 'objectid' => $uc->get_id(),
744 'contextid' => $uc->get_context()->id
,
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());
756 $params['other']['anythingelse'] = '';
757 // Missing competencyid.
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());
765 $params['other']['competencyid'] = $c->get_id();
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());
776 * Test the user competency viewed event in course.
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();
813 'objectid' => $ucc->get_id(),
814 'contextid' => $ucc->get_context()->id
,
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());
826 $params['contextid'] = context_course
::instance($course->id
)->id
;
827 $params['courseid'] = $course->id
;
828 // Missing competencyid.
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());
838 * Test the user competency plan viewed event.
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()
856 // Can not log the event for user competency using non completed plan.
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());
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();
890 'objectid' => $ucp->get_id(),
891 'contextid' => $ucp->get_context()->id
,
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());
903 $params['other']['anythingelse'] = '';
904 // Missing competencyid.
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());
912 $params['other']['competencyid'] = $c->get_id();
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());
923 * Test the user competency viewed event.
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()
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();
959 'objectid' => $uc->get_id(),
960 'contextid' => $uc->get_context()->id
963 // Missing competencyid.
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());
973 * Test the plan approved event.
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();
1001 * Test the plan unapproved event.
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();
1029 * Test the plan reopened event.
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();
1057 * Test the plan completed event.
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();
1085 * Test the plan unlinked event.
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()
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();
1118 * Test the plan review requested event.
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();
1146 * Test the plan review request cancelled event.
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();
1174 * Test the plan review started event.
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();
1202 * Test the plan review stopped event.
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();
1230 * Test plan comment created event.
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();
1267 * Test plan comment deleted event.
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();
1303 * Test evidence_created event.
1305 public function test_evidence_created() {
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();
1325 $evidence = api
::add_evidence($student->id
, $comp, $syscontext, \core_competency\evidence
::ACTION_OVERRIDE
,
1326 'commentincontext', 'core', null, $recommend, null, 1);
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']);
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());
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();
1362 * Test evidence_created event by linking an invalid user competency to an evidence.
1364 * @expectedException coding_exception
1365 * @expectedExceptionMessage The user competency linked with this evidence is invalid.
1367 public function test_evidence_created_with_invalid_user_competency() {
1368 $this->resetAfterTest(true);
1369 $dg = $this->getDataGenerator();
1370 $syscontext = context_system
::instance();
1373 $student = $dg->create_user();
1374 $student2 = $dg->create_user();
1376 // Create a competency for the course.
1377 $lpg = $dg->get_plugin_generator('core_competency');
1378 $framework = $lpg->create_framework();
1379 $comp = $lpg->create_competency(['competencyframeworkid' => $framework->get_id()]);
1381 // Create a different user competency.
1382 $otheruc = \core_competency\user_competency
::create_relation($student2->id
, $comp->get_id());
1387 $evidence = api
::add_evidence($student->id
, $comp, $syscontext, \core_competency\evidence
::ACTION_OVERRIDE
,
1388 'commentincontext', 'core', null, $recommend, null, 1);
1390 // We expect this to fail and throw a coding exception.
1391 \core\event\competency_evidence_created
::create_from_evidence($evidence, $otheruc, $recommend)->trigger();
1395 * Test creation of evidence_created event with missing data.
1397 * These data are validated by \core_competency\evidence_created::validate_data().
1399 public function test_evidence_created_with_missing_data() {
1406 // No relateduserid.
1407 $errormsg = 'The \'relateduserid\' must be set.';
1409 \core\event\competency_evidence_created
::create($eventdata)->trigger();
1410 $this->fail('Coding exception should have been thrown: ' . $errormsg);
1411 } catch (coding_exception
$e) {
1412 $this->assertContains($errormsg, $e->getMessage());
1414 $eventdata['relateduserid'] = 1;
1416 // No other['usercompetencyid'].
1417 $errormsg = 'The \'usercompetencyid\' data in \'other\' must be set.';
1419 \core\event\competency_evidence_created
::create($eventdata)->trigger();
1420 $this->fail('Coding exception should have been thrown: ' . $errormsg);
1421 } catch (coding_exception
$e) {
1422 $this->assertContains($errormsg, $e->getMessage());
1424 $eventdata['other']['usercompetencyid'] = 1;
1426 // No other['competencyid'].
1427 $errormsg = 'The \'competencyid\' data in \'other\' must be set.';
1429 \core\event\competency_evidence_created
::create($eventdata)->trigger();
1430 $this->fail('Coding exception should have been thrown: ' . $errormsg);
1431 } catch (coding_exception
$e) {
1432 $this->assertContains($errormsg, $e->getMessage());
1434 $eventdata['other']['competencyid'] = 1;
1436 // No other['action'].
1437 $errormsg = 'The \'action\' data in \'other\' must be set.';
1439 \core\event\competency_evidence_created
::create($eventdata)->trigger();
1440 $this->fail('Coding exception should have been thrown: ' . $errormsg);
1441 } catch (coding_exception
$e) {
1442 $this->assertContains($errormsg, $e->getMessage());
1444 $eventdata['other']['action'] = 1;
1446 // No other['recommend'].
1447 $errormsg = 'The \'recommend\' data in \'other\' must be set.';
1449 \core\event\competency_evidence_created
::create($eventdata)->trigger();
1450 $this->fail('Coding exception should have been thrown: ' . $errormsg);
1451 } catch (coding_exception
$e) {
1452 $this->assertContains($errormsg, $e->getMessage());
1454 $eventdata['other']['recommend'] = 1;
1456 // Event should be triggered without any problems.
1457 \core\event\competency_evidence_created
::create($eventdata)->trigger();
1461 * Test the user competency grade rated event.
1464 public function test_user_competency_rated() {
1465 $this->resetAfterTest(true);
1466 $this->setAdminUser();
1467 $dg = $this->getDataGenerator();
1468 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1469 $scale = $dg->create_scale(array('scale' => 'A,B,C,D'));
1470 $scaleconfig = array(array('scaleid' => $scale->id
));
1471 $scaleconfig[] = array('name' => 'A', 'id' => 1, 'scaledefault' => 0, 'proficient' => 0);
1472 $scaleconfig[] = array('name' => 'B', 'id' => 2, 'scaledefault' => 1, 'proficient' => 0);
1473 $scaleconfig[] = array('name' => 'C', 'id' => 3, 'scaledefault' => 0, 'proficient' => 1);
1474 $scaleconfig[] = array('name' => 'D', 'id' => 4, 'scaledefault' => 0, 'proficient' => 1);
1475 $fr = $lpg->create_framework();
1476 $c = $lpg->create_competency(array(
1477 'competencyframeworkid' => $fr->get_id(),
1478 'scaleid' => $scale->id
,
1479 'scaleconfiguration' => $scaleconfig
1482 $user = $dg->create_user();
1483 $uc = $lpg->create_user_competency(array(
1484 'userid' => $user->id
,
1485 'competencyid' => $c->get_id()));
1487 // Trigger and capture the event.
1488 $sink = $this->redirectEvents();
1489 api
::grade_competency($user->id
, $c->get_id(), 2, true);
1491 // Get our event event.
1492 $events = $sink->get_events();
1493 // Evidence created.
1494 $this->assertCount(2, $events);
1495 $evidencecreatedevent = $events[0];
1496 $event = $events[1];
1498 // Check that the event data is valid.
1499 $this->assertInstanceOf('\core\event\competency_evidence_created', $evidencecreatedevent);
1500 $this->assertInstanceOf('\core\event\competency_user_competency_rated', $event);
1501 $this->assertEquals($uc->get_id(), $event->objectid
);
1502 $this->assertEquals($uc->get_context()->id
, $event->contextid
);
1503 $this->assertEquals($uc->get_userid(), $event->relateduserid
);
1504 $this->assertEquals(2, $event->other
['grade']);
1505 $this->assertEventContextNotUsed($event);
1506 $this->assertDebuggingNotCalled();
1510 * Test the user competency grade rated in course event.
1513 public function test_user_competency_rated_in_course() {
1514 $this->resetAfterTest(true);
1515 $this->setAdminUser();
1516 $dg = $this->getDataGenerator();
1517 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1518 $scale = $dg->create_scale(array('scale' => 'A,B,C,D'));
1519 $course = $dg->create_course();
1520 $user = $dg->create_user();
1521 $studentarch = get_archetype_roles('student');
1522 $studentrole = array_shift($studentarch);
1523 $scaleconfig = array(array('scaleid' => $scale->id
));
1524 $scaleconfig[] = array('name' => 'A', 'id' => 1, 'scaledefault' => 0, 'proficient' => 0);
1525 $scaleconfig[] = array('name' => 'B', 'id' => 2, 'scaledefault' => 1, 'proficient' => 0);
1526 $scaleconfig[] = array('name' => 'C', 'id' => 3, 'scaledefault' => 0, 'proficient' => 1);
1527 $scaleconfig[] = array('name' => 'D', 'id' => 4, 'scaledefault' => 0, 'proficient' => 1);
1528 $fr = $lpg->create_framework();
1529 $c = $lpg->create_competency(array(
1530 'competencyframeworkid' => $fr->get_id(),
1531 'scaleid' => $scale->id
,
1532 'scaleconfiguration' => $scaleconfig
1534 // Enrol the user as students in course.
1535 $dg->enrol_user($user->id
, $course->id
, $studentrole->id
);
1536 $lpg->create_course_competency(array(
1537 'courseid' => $course->id
,
1538 'competencyid' => $c->get_id()));
1539 $uc = $lpg->create_user_competency(array(
1540 'userid' => $user->id
,
1541 'competencyid' => $c->get_id()));
1543 // Trigger and capture the event.
1544 $sink = $this->redirectEvents();
1545 api
::grade_competency_in_course($course->id
, $user->id
, $c->get_id(), 2, true);
1547 // Get our event event.
1548 $events = $sink->get_events();
1549 // Evidence created.
1550 $this->assertCount(2, $events);
1551 $evidencecreatedevent = $events[0];
1552 $event = $events[1];
1554 // Check that the event data is valid.
1555 $this->assertInstanceOf('\core\event\competency_evidence_created', $evidencecreatedevent);
1556 $this->assertInstanceOf('\core\event\competency_user_competency_rated_in_course', $event);
1557 $this->assertEquals(context_course
::instance($course->id
)->id
, $event->contextid
);
1558 $this->assertEquals($course->id
, $event->courseid
);
1559 $this->assertEquals($uc->get_userid(), $event->relateduserid
);
1560 $this->assertEquals($uc->get_competencyid(), $event->other
['competencyid']);
1561 $this->assertEquals(2, $event->other
['grade']);
1562 $this->assertEventContextNotUsed($event);
1563 $this->assertDebuggingNotCalled();
1567 * Test the user competency grade rated in plan event.
1570 public function test_user_competency_rated_in_plan() {
1571 $this->resetAfterTest(true);
1572 $this->setAdminUser();
1573 $dg = $this->getDataGenerator();
1574 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1575 $scale = $dg->create_scale(array('scale' => 'A,B,C,D'));
1576 $user = $dg->create_user();
1577 $scaleconfig = array(array('scaleid' => $scale->id
));
1578 $scaleconfig[] = array('name' => 'A', 'id' => 1, 'scaledefault' => 0, 'proficient' => 0);
1579 $scaleconfig[] = array('name' => 'B', 'id' => 2, 'scaledefault' => 1, 'proficient' => 0);
1580 $scaleconfig[] = array('name' => 'C', 'id' => 3, 'scaledefault' => 0, 'proficient' => 1);
1581 $scaleconfig[] = array('name' => 'D', 'id' => 4, 'scaledefault' => 0, 'proficient' => 1);
1582 $plan = $lpg->create_plan(array('userid' => $user->id
));
1583 $fr = $lpg->create_framework();
1584 $c = $lpg->create_competency(array(
1585 'competencyframeworkid' => $fr->get_id(),
1586 'scaleid' => $scale->id
,
1587 'scaleconfiguration' => $scaleconfig
1589 $pc = $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c->get_id()));
1590 $uc = $lpg->create_user_competency(array(
1591 'userid' => $user->id
,
1592 'competencyid' => $c->get_id()));
1594 // Trigger and capture the event.
1595 $sink = $this->redirectEvents();
1596 api
::grade_competency_in_plan($plan->get_id(), $c->get_id(), 3, true);
1598 // Get our event event.
1599 $events = $sink->get_events();
1600 // Evidence created.
1601 $this->assertCount(2, $events);
1602 $evidencecreatedevent = $events[0];
1603 $event = $events[1];
1605 // Check that the event data is valid.
1606 $this->assertInstanceOf('\core\event\competency_evidence_created', $evidencecreatedevent);
1607 $this->assertInstanceOf('\core\event\competency_user_competency_rated_in_plan', $event);
1608 $this->assertEquals($uc->get_id(), $event->objectid
);
1609 $this->assertEquals($uc->get_context()->id
, $event->contextid
);
1610 $this->assertEquals($uc->get_userid(), $event->relateduserid
);
1611 $this->assertEquals($uc->get_competencyid(), $event->other
['competencyid']);
1612 $this->assertEquals(3, $event->other
['grade']);
1613 $this->assertEventContextNotUsed($event);
1614 $this->assertDebuggingNotCalled();
1618 * Test user competency comment created event.
1620 public function test_user_competency_comment_created() {
1621 $this->resetAfterTest(true);
1623 $dg = $this->getDataGenerator();
1624 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1625 $user = $dg->create_user();
1626 $this->setUser($user);
1627 $fr = $lpg->create_framework();
1628 $c = $lpg->create_competency(array('competencyframeworkid' => $fr->get_id()));
1629 $uc = $lpg->create_user_competency(array(
1630 'userid' => $user->id
,
1631 'competencyid' => $c->get_id()
1634 $context = context_user
::instance($user->id
);
1635 $cmt = new stdClass();
1636 $cmt->context
= $context;
1637 $cmt->area
= 'user_competency';
1638 $cmt->itemid
= $uc->get_id();
1639 $cmt->component
= 'competency';
1640 $cmt->showcount
= 1;
1641 $manager = new comment($cmt);
1643 // Triggering and capturing the event.
1644 $sink = $this->redirectEvents();
1645 $manager->add("New comment for user competency");
1646 $events = $sink->get_events();
1647 // Add comment will trigger 2 other events message_viewed and message_sent.
1648 $this->assertCount(1, $events);
1649 $event = reset($events);
1651 // Checking that the event contains the expected values.
1652 $this->assertInstanceOf('\core\event\competency_comment_created', $event);
1653 $this->assertEquals($context, $event->get_context());
1654 $this->assertEquals($uc->get_id(), $event->other
['itemid']);
1655 $this->assertEventContextNotUsed($event);
1656 $this->assertDebuggingNotCalled();
1660 * Test plan comment deleted event.
1662 public function test_user_competency_comment_deleted() {
1663 $this->resetAfterTest(true);
1664 $this->setAdminUser();
1665 $dg = $this->getDataGenerator();
1666 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1667 $user = $dg->create_user();
1668 $fr = $lpg->create_framework();
1669 $c = $lpg->create_competency(array('competencyframeworkid' => $fr->get_id()));
1670 $uc = $lpg->create_user_competency(array(
1671 'userid' => $user->id
,
1672 'competencyid' => $c->get_id()
1674 $context = context_user
::instance($user->id
);
1676 $cmt = new stdClass();
1677 $cmt->context
= $context;
1678 $cmt->area
= 'user_competency';
1679 $cmt->itemid
= $uc->get_id();
1680 $cmt->component
= 'competency';
1681 $manager = new comment($cmt);
1682 $newcomment = $manager->add("Comment to be deleted");
1684 // Triggering and capturing the event.
1685 $sink = $this->redirectEvents();
1686 $manager->delete($newcomment->id
);
1687 $events = $sink->get_events();
1688 $this->assertCount(1, $events);
1689 $event = reset($events);
1691 // Checking that the event contains the expected values.
1692 $this->assertInstanceOf('\core\event\competency_comment_deleted', $event);
1693 $this->assertEquals($context, $event->get_context());
1694 $this->assertEquals($uc->get_id(), $event->other
['itemid']);
1695 $this->assertEventContextNotUsed($event);
1696 $this->assertDebuggingNotCalled();