Search moodle.org's
Developer Documentation

See Release Notes

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