Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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  /**
  18   * Plan persistent class tests.
  19   *
  20   * @package    core_competency
  21   * @copyright  2015 Frédéric Massart - FMCorz.net
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  global $CFG;
  27  
  28  use core_competency\api;
  29  use core_competency\plan;
  30  
  31  /**
  32   * Plan persistent testcase.
  33   *
  34   * @package    core_competency
  35   * @copyright  2015 Frédéric Massart - FMCorz.net
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class core_competency_plan_testcase extends advanced_testcase {
  39  
  40      public function test_can_manage_user() {
  41          $this->resetAfterTest(true);
  42  
  43          $manage = create_role('Manage', 'manage', 'Plan manager');
  44          $manageown = create_role('Manageown', 'manageown', 'Own plan manager');
  45  
  46          $u1 = $this->getDataGenerator()->create_user();
  47          $u2 = $this->getDataGenerator()->create_user();
  48          $u3 = $this->getDataGenerator()->create_user();
  49  
  50          $syscontext = context_system::instance();
  51          $u1context = context_user::instance($u1->id);
  52          $u2context = context_user::instance($u2->id);
  53          $u3context = context_user::instance($u3->id);
  54  
  55          assign_capability('moodle/competency:planmanage', CAP_ALLOW, $manage, $syscontext->id);
  56          assign_capability('moodle/competency:planmanageown', CAP_ALLOW, $manageown, $u2context->id);
  57  
  58          role_assign($manage, $u1->id, $syscontext->id);
  59          role_assign($manageown, $u2->id, $syscontext->id);
  60          role_assign($manage, $u3->id, $u2context->id);
  61          accesslib_clear_all_caches_for_unit_testing();
  62  
  63          $this->setUser($u1);
  64          $this->assertTrue(plan::can_manage_user($u1->id));
  65          $this->assertTrue(plan::can_manage_user($u2->id));
  66          $this->assertTrue(plan::can_manage_user($u3->id));
  67  
  68          $this->setUser($u2);
  69          $this->assertFalse(plan::can_manage_user($u1->id));
  70          $this->assertTrue(plan::can_manage_user($u2->id));
  71          $this->assertFalse(plan::can_manage_user($u3->id));
  72  
  73          $this->setUser($u3);
  74          $this->assertFalse(plan::can_manage_user($u1->id));
  75          $this->assertTrue(plan::can_manage_user($u2->id));
  76          $this->assertFalse(plan::can_manage_user($u3->id));
  77      }
  78  
  79      public function test_can_manage_user_draft() {
  80          $this->resetAfterTest(true);
  81  
  82          $manage = create_role('Manage', 'manage', 'Plan manager');
  83          $manageown = create_role('Manageown', 'manageown', 'Own plan manager');
  84          $managedraft = create_role('Managedraft', 'managedraft', 'Draft plan manager');
  85          $manageowndraft = create_role('Manageowndraft', 'manageowndraft', 'Own draft plan manager');
  86  
  87          $u1 = $this->getDataGenerator()->create_user();
  88          $u2 = $this->getDataGenerator()->create_user();
  89          $u3 = $this->getDataGenerator()->create_user();
  90          $u4 = $this->getDataGenerator()->create_user();
  91          $u5 = $this->getDataGenerator()->create_user();
  92  
  93          $syscontext = context_system::instance();
  94          $u1context = context_user::instance($u1->id);
  95          $u2context = context_user::instance($u2->id);
  96          $u3context = context_user::instance($u3->id);
  97          $u4context = context_user::instance($u4->id);
  98          $u5context = context_user::instance($u5->id);
  99  
 100          assign_capability('moodle/competency:planmanage', CAP_ALLOW, $manage, $syscontext->id);
 101          assign_capability('moodle/competency:planmanageown', CAP_ALLOW, $manageown, $syscontext->id);
 102          assign_capability('moodle/competency:planmanagedraft', CAP_ALLOW, $managedraft, $syscontext->id);
 103          assign_capability('moodle/competency:planmanageowndraft', CAP_ALLOW, $manageowndraft, $syscontext->id);
 104  
 105          role_assign($manage, $u1->id, $syscontext->id);
 106          role_assign($manageown, $u2->id, $syscontext->id);
 107          role_assign($managedraft, $u3->id, $syscontext->id);
 108          role_assign($managedraft, $u4->id, $u2context->id);
 109          role_assign($manageowndraft, $u5->id, $syscontext->id);
 110          accesslib_clear_all_caches_for_unit_testing();
 111  
 112          $this->setUser($u1);
 113          $this->assertFalse(plan::can_manage_user_draft($u1->id));
 114          $this->assertFalse(plan::can_manage_user_draft($u2->id));
 115          $this->assertFalse(plan::can_manage_user_draft($u3->id));
 116          $this->assertFalse(plan::can_manage_user_draft($u4->id));
 117          $this->assertFalse(plan::can_manage_user_draft($u5->id));
 118  
 119          $this->setUser($u2);
 120          $this->assertFalse(plan::can_manage_user_draft($u1->id));
 121          $this->assertFalse(plan::can_manage_user_draft($u2->id));
 122          $this->assertFalse(plan::can_manage_user_draft($u3->id));
 123          $this->assertFalse(plan::can_manage_user_draft($u4->id));
 124          $this->assertFalse(plan::can_manage_user_draft($u5->id));
 125  
 126          $this->setUser($u3);
 127          $this->assertTrue(plan::can_manage_user_draft($u1->id));
 128          $this->assertTrue(plan::can_manage_user_draft($u2->id));
 129          $this->assertTrue(plan::can_manage_user_draft($u3->id));
 130          $this->assertTrue(plan::can_manage_user_draft($u4->id));
 131          $this->assertTrue(plan::can_manage_user_draft($u5->id));
 132  
 133          $this->setUser($u4);
 134          $this->assertFalse(plan::can_manage_user_draft($u1->id));
 135          $this->assertTrue(plan::can_manage_user_draft($u2->id));
 136          $this->assertFalse(plan::can_manage_user_draft($u3->id));
 137          $this->assertFalse(plan::can_manage_user_draft($u4->id));
 138          $this->assertFalse(plan::can_manage_user_draft($u5->id));
 139  
 140          $this->setUser($u5);
 141          $this->assertFalse(plan::can_manage_user_draft($u1->id));
 142          $this->assertFalse(plan::can_manage_user_draft($u2->id));
 143          $this->assertFalse(plan::can_manage_user_draft($u3->id));
 144          $this->assertFalse(plan::can_manage_user_draft($u4->id));
 145          $this->assertTrue(plan::can_manage_user_draft($u5->id));
 146      }
 147  
 148      public function test_can_read_user() {
 149          $this->resetAfterTest(true);
 150  
 151          $read = create_role('Read', 'read', 'Plan reader');
 152          $readown = create_role('Readown', 'readown', 'Own plan reader');
 153  
 154          $u1 = $this->getDataGenerator()->create_user();
 155          $u2 = $this->getDataGenerator()->create_user();
 156          $u3 = $this->getDataGenerator()->create_user();
 157  
 158          $syscontext = context_system::instance();
 159          $u1context = context_user::instance($u1->id);
 160          $u2context = context_user::instance($u2->id);
 161          $u3context = context_user::instance($u3->id);
 162  
 163          assign_capability('moodle/competency:planview', CAP_ALLOW, $read, $syscontext->id);
 164          assign_capability('moodle/competency:planviewown', CAP_ALLOW, $readown, $u2context->id);
 165  
 166          role_assign($read, $u1->id, $syscontext->id);
 167          role_assign($readown, $u2->id, $syscontext->id);
 168          role_assign($read, $u3->id, $u2context->id);
 169          accesslib_clear_all_caches_for_unit_testing();
 170  
 171          $this->setUser($u1);
 172          $this->assertTrue(plan::can_read_user($u1->id));
 173          $this->assertTrue(plan::can_read_user($u2->id));
 174          $this->assertTrue(plan::can_read_user($u3->id));
 175  
 176          $this->setUser($u2);
 177          $this->assertFalse(plan::can_read_user($u1->id));
 178          $this->assertTrue(plan::can_read_user($u2->id));
 179          $this->assertFalse(plan::can_read_user($u3->id));
 180  
 181          $this->setUser($u3);
 182          $this->assertFalse(plan::can_read_user($u1->id));
 183          $this->assertTrue(plan::can_read_user($u2->id));
 184          $this->assertTrue(plan::can_read_user($u3->id));    // Due to the default capability.
 185      }
 186  
 187      public function test_can_read_user_draft() {
 188          $this->resetAfterTest(true);
 189  
 190          $read = create_role('Read', 'read', 'Plan readr');
 191          $readown = create_role('Readown', 'readown', 'Own plan readr');
 192          $readdraft = create_role('Readdraft', 'readdraft', 'Draft plan readr');
 193          $readowndraft = create_role('Readowndraft', 'readowndraft', 'Own draft plan readr');
 194  
 195          $u1 = $this->getDataGenerator()->create_user();
 196          $u2 = $this->getDataGenerator()->create_user();
 197          $u3 = $this->getDataGenerator()->create_user();
 198          $u4 = $this->getDataGenerator()->create_user();
 199          $u5 = $this->getDataGenerator()->create_user();
 200  
 201          $syscontext = context_system::instance();
 202          $u1context = context_user::instance($u1->id);
 203          $u2context = context_user::instance($u2->id);
 204          $u3context = context_user::instance($u3->id);
 205          $u4context = context_user::instance($u4->id);
 206          $u5context = context_user::instance($u5->id);
 207  
 208          assign_capability('moodle/competency:planview', CAP_ALLOW, $read, $syscontext->id);
 209          assign_capability('moodle/competency:planviewown', CAP_ALLOW, $readown, $syscontext->id);
 210          assign_capability('moodle/competency:planviewdraft', CAP_ALLOW, $readdraft, $syscontext->id);
 211          assign_capability('moodle/competency:planviewowndraft', CAP_ALLOW, $readowndraft, $syscontext->id);
 212          assign_capability('moodle/competency:planviewown', CAP_PROHIBIT, $readowndraft, $syscontext->id);
 213  
 214          role_assign($read, $u1->id, $syscontext->id);
 215          role_assign($readown, $u2->id, $syscontext->id);
 216          role_assign($readdraft, $u3->id, $syscontext->id);
 217          role_assign($readdraft, $u4->id, $u2context->id);
 218          role_assign($readowndraft, $u5->id, $syscontext->id);
 219          accesslib_clear_all_caches_for_unit_testing();
 220  
 221          $this->setUser($u1);
 222          $this->assertFalse(plan::can_read_user_draft($u1->id));
 223          $this->assertFalse(plan::can_read_user_draft($u2->id));
 224          $this->assertFalse(plan::can_read_user_draft($u3->id));
 225          $this->assertFalse(plan::can_read_user_draft($u4->id));
 226          $this->assertFalse(plan::can_read_user_draft($u5->id));
 227  
 228          $this->setUser($u2);
 229          $this->assertFalse(plan::can_read_user_draft($u1->id));
 230          $this->assertFalse(plan::can_read_user_draft($u2->id));
 231          $this->assertFalse(plan::can_read_user_draft($u3->id));
 232          $this->assertFalse(plan::can_read_user_draft($u4->id));
 233          $this->assertFalse(plan::can_read_user_draft($u5->id));
 234  
 235          $this->setUser($u3);
 236          $this->assertTrue(plan::can_read_user_draft($u1->id));
 237          $this->assertTrue(plan::can_read_user_draft($u2->id));
 238          $this->assertTrue(plan::can_read_user_draft($u3->id));
 239          $this->assertTrue(plan::can_read_user_draft($u4->id));
 240          $this->assertTrue(plan::can_read_user_draft($u5->id));
 241  
 242          $this->setUser($u4);
 243          $this->assertFalse(plan::can_read_user_draft($u1->id));
 244          $this->assertTrue(plan::can_read_user_draft($u2->id));
 245          $this->assertFalse(plan::can_read_user_draft($u3->id));
 246          $this->assertFalse(plan::can_read_user_draft($u4->id));
 247          $this->assertFalse(plan::can_read_user_draft($u5->id));
 248  
 249          $this->setUser($u5);
 250          $this->assertFalse(plan::can_read_user_draft($u1->id));
 251          $this->assertFalse(plan::can_read_user_draft($u2->id));
 252          $this->assertFalse(plan::can_read_user_draft($u3->id));
 253          $this->assertFalse(plan::can_read_user_draft($u4->id));
 254          $this->assertTrue(plan::can_read_user_draft($u5->id));
 255      }
 256  
 257      public function test_validate_duedate() {
 258          global $DB;
 259          $this->resetAfterTest(true);
 260          $this->setAdminUser();
 261          $dg = $this->getDataGenerator();
 262          $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
 263          $user = $dg->create_user();
 264  
 265          $record = array('userid' => $user->id,
 266                          'status' => plan::STATUS_DRAFT,
 267                          'duedate' => time() - 8000);
 268  
 269          // Ignore duedate validation on create/update draft plan.
 270          $plan = $lpg->create_plan($record);
 271          $this->assertInstanceOf('core_competency\plan', $plan);
 272  
 273          // Passing from draft to active.
 274          $plan->set('status', plan::STATUS_ACTIVE);
 275  
 276          // Draft to active with duedate in the past.
 277          $expected = array(
 278              'duedate' => new lang_string('errorcannotsetduedateinthepast', 'core_competency'),
 279          );
 280          $this->assertEquals($expected, $plan->validate());
 281  
 282          // Draft to active: past date => past date(fail).
 283          $plan->set('duedate', time() - 100);
 284          $expected = array(
 285              'duedate' => new lang_string('errorcannotsetduedateinthepast', 'core_competency'),
 286          );
 287          $this->assertEquals($expected, $plan->validate());
 288  
 289          // Draft to active: past date => too soon (fail).
 290          $plan->set('duedate', time() + 100);
 291          $expected = array(
 292              'duedate' => new lang_string('errorcannotsetduedatetoosoon', 'core_competency'),
 293          );
 294          $this->assertEquals($expected, $plan->validate());
 295  
 296          // Draft to active: past date => future date (pass).
 297          $plan->set('duedate', time() + plan::DUEDATE_THRESHOLD + 10);
 298          $this->assertEquals(true, $plan->validate());
 299  
 300          // Draft to active: past date => unset date (pass).
 301          $plan->set('duedate', 0);
 302          $this->assertEquals(true, $plan->validate());
 303  
 304          // Updating active plan.
 305          $plan->update();
 306  
 307          // Active to active: past => same past (pass).
 308          $record = $plan->to_record();
 309          $record->duedate = 1;
 310          $DB->update_record(plan::TABLE, $record);
 311          $plan->read();
 312          $plan->set('description', uniqid()); // Force revalidation.
 313          $this->assertTrue($plan->is_valid());
 314  
 315          // Active to active: past => unset (pass).
 316          $plan->set('duedate', 0);
 317          $this->assertTrue($plan->is_valid());
 318          $plan->update();
 319  
 320          // Active to active: unset => unset (pass).
 321          $plan->set('description', uniqid()); // Force revalidation.
 322          $this->assertTrue($plan->is_valid());
 323  
 324          // Active to active: unset date => past date(fail).
 325          $plan->set('duedate', time() - 100);
 326          $expected = array(
 327              'duedate' => new lang_string('errorcannotsetduedateinthepast', 'core_competency'),
 328          );
 329          $this->assertEquals($expected, $plan->validate());
 330  
 331          // Active to active: unset date => too soon (fail).
 332          $plan->set('duedate', time() + 100);
 333          $expected = array(
 334              'duedate' => new lang_string('errorcannotsetduedatetoosoon', 'core_competency'),
 335          );
 336          $this->assertEquals($expected, $plan->validate());
 337  
 338          // Active to active: unset date => future date (pass).
 339          $plan->set('duedate', time() + plan::DUEDATE_THRESHOLD + 10);
 340          $this->assertEquals(true, $plan->validate());
 341  
 342          // Updating active plan with future date.
 343          $plan->update();
 344  
 345          // Active to active: future => same future (pass).
 346          $plan->set('description', uniqid()); // Force revalidation.
 347          $this->assertTrue($plan->is_valid());
 348  
 349          // Active to active: future date => unset date (pass).
 350          $plan->set('duedate', 0);
 351          $this->assertEquals(true, $plan->validate());
 352  
 353          // Active to active: future date => past date(fail).
 354          $plan->set('duedate', time() - 100);
 355          $expected = array(
 356              'duedate' => new lang_string('errorcannotsetduedateinthepast', 'core_competency'),
 357          );
 358          $this->assertEquals($expected, $plan->validate());
 359  
 360          // Active to active: future date => too soon (fail).
 361          $plan->set('duedate', time() + 100);
 362          $expected = array(
 363              'duedate' => new lang_string('errorcannotsetduedatetoosoon', 'core_competency'),
 364          );
 365          $this->assertEquals($expected, $plan->validate());
 366  
 367          // Active to active: future date => future date (pass).
 368          $plan->set('duedate', time() + plan::DUEDATE_THRESHOLD + 10);
 369          $this->assertEquals(true, $plan->validate());
 370  
 371          // Completing plan: with due date in the past.
 372          $record = $plan->to_record();
 373          $record->status = plan::STATUS_ACTIVE;
 374          $record->duedate = time() - 200;
 375          $DB->update_record(plan::TABLE, $record);
 376  
 377          $success = core_competency\api::complete_plan($plan->get('id'));
 378          $this->assertTrue($success);
 379  
 380          // Completing plan: with due date too soon (pass).
 381          $record = $plan->to_record();
 382          $record->status = plan::STATUS_ACTIVE;
 383          $record->duedate = time() + 200;
 384          $DB->update_record(plan::TABLE, $record);
 385  
 386          $success = core_competency\api::complete_plan($plan->get('id'));
 387          $this->assertTrue($success);
 388  
 389          // Completing plan: with due date in the future (pass).
 390          $record = $plan->to_record();
 391          $record->status = plan::STATUS_ACTIVE;
 392          $record->duedate = time() + plan::DUEDATE_THRESHOLD + 10;
 393          $DB->update_record(plan::TABLE, $record);
 394  
 395          $success = core_competency\api::complete_plan($plan->get('id'));
 396          $this->assertTrue($success);
 397  
 398          // Completing plan: with due date unset (pass).
 399          $record = $plan->to_record();
 400          $record->status = plan::STATUS_ACTIVE;
 401          $record->duedate = 0;
 402          $DB->update_record(plan::TABLE, $record);
 403  
 404          $success = core_competency\api::complete_plan($plan->get('id'));
 405          $this->assertTrue($success);
 406  
 407          // Reopening plan: with due date in the past => duedate unset.
 408          $record = $plan->to_record();
 409          $record->status = plan::STATUS_COMPLETE;
 410          $record->duedate = time() - 200;
 411          $DB->update_record(plan::TABLE, $record);
 412  
 413          $success = core_competency\api::reopen_plan($plan->get('id'));
 414          $this->assertTrue($success);
 415          $plan->read();
 416          $this->assertEquals(0, $plan->get('duedate'));
 417  
 418          // Reopening plan: with due date too soon => duedate unset.
 419          $record = $plan->to_record();
 420          $record->status = plan::STATUS_COMPLETE;
 421          $record->duedate = time() + 100;
 422          $DB->update_record(plan::TABLE, $record);
 423  
 424          $success = core_competency\api::reopen_plan($plan->get('id'));
 425          $this->assertTrue($success);
 426          $plan->read();
 427          $this->assertEquals(0, $plan->get('duedate'));
 428  
 429          // Reopening plan: with due date in the future => duedate unchanged.
 430          $record = $plan->to_record();
 431          $record->status = plan::STATUS_COMPLETE;
 432          $duedate = time() + plan::DUEDATE_THRESHOLD + 10;
 433          $record->duedate = $duedate;
 434          $DB->update_record(plan::TABLE, $record);
 435  
 436          $success = core_competency\api::reopen_plan($plan->get('id'));
 437          $this->assertTrue($success);
 438          $plan->read();
 439  
 440          // Check that the due date has not changed.
 441          $this->assertNotEquals(0, $plan->get('duedate'));
 442          $this->assertEquals($duedate, $plan->get('duedate'));
 443      }
 444  
 445      public function test_get_by_user_and_competency() {
 446          $this->resetAfterTest();
 447          $this->setAdminUser();
 448  
 449          $dg = $this->getDataGenerator();
 450          $lpg = $dg->get_plugin_generator('core_competency');
 451  
 452          $u1 = $dg->create_user();
 453          $u2 = $dg->create_user();
 454          $u3 = $dg->create_user();
 455          $u4 = $dg->create_user();
 456  
 457          $f1 = $lpg->create_framework();
 458          $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
 459          $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
 460  
 461          $tpl1 = $lpg->create_template();
 462          $lpg->create_template_competency(array('competencyid' => $c1->get('id'), 'templateid' => $tpl1->get('id')));
 463  
 464          $p1 = $lpg->create_plan(array('userid' => $u1->id));
 465          $lpg->create_plan_competency(array('planid' => $p1->get('id'), 'competencyid' => $c1->get('id')));
 466          $p2 = $lpg->create_plan(array('userid' => $u2->id));
 467          $lpg->create_plan_competency(array('planid' => $p2->get('id'), 'competencyid' => $c1->get('id')));
 468          $p3 = $lpg->create_plan(array('userid' => $u3->id, 'templateid' => $tpl1->get('id')));
 469          $p4 = $lpg->create_plan(array('userid' => $u4->id, 'templateid' => $tpl1->get('id')));
 470          api::complete_plan($p2);
 471          api::complete_plan($p4);
 472  
 473          // Finding a plan, not completed.
 474          $plans = plan::get_by_user_and_competency($u1->id, $c1->get('id'));
 475          $this->assertCount(1, $plans);
 476          $plan = array_shift($plans);
 477          $this->assertEquals($p1->get('id'), $plan->get('id'));
 478          $this->assertNotEquals(plan::STATUS_COMPLETE, $plan->get('status'));
 479  
 480          // Finding a completed plan.
 481          $plans = plan::get_by_user_and_competency($u2->id, $c1->get('id'));
 482          $this->assertCount(1, $plans);
 483          $plan = array_shift($plans);
 484          $this->assertEquals($p2->get('id'), $plan->get('id'));
 485          $this->assertEquals(plan::STATUS_COMPLETE, $plan->get('status'));
 486  
 487          // Finding a plan based on a template, not completed.
 488          $plans = plan::get_by_user_and_competency($u3->id, $c1->get('id'));
 489          $this->assertCount(1, $plans);
 490          $plan = array_shift($plans);
 491          $this->assertEquals($p3->get('id'), $plan->get('id'));
 492          $this->assertTrue($plan->is_based_on_template());
 493          $this->assertNotEquals(plan::STATUS_COMPLETE, $plan->get('status'));
 494  
 495          // Finding a plan based on a template.
 496          $plans = plan::get_by_user_and_competency($u4->id, $c1->get('id'));
 497          $this->assertCount(1, $plans);
 498          $plan = array_shift($plans);
 499          $this->assertEquals($p4->get('id'), $plan->get('id'));
 500          $this->assertTrue($plan->is_based_on_template());
 501          $this->assertEquals(plan::STATUS_COMPLETE, $plan->get('status'));
 502  
 503          // Finding more than one plan, no template.
 504          $p5 = $lpg->create_plan(array('userid' => $u1->id));
 505          $lpg->create_plan_competency(array('planid' => $p5->get('id'), 'competencyid' => $c1->get('id')));
 506          $plans = plan::get_by_user_and_competency($u1->id, $c1->get('id'));
 507          $this->assertCount(2, $plans);
 508          $plan = array_shift($plans);
 509          $this->assertEquals($p1->get('id'), $plan->get('id'));
 510          $plan = array_shift($plans);
 511          $this->assertEquals($p5->get('id'), $plan->get('id'));
 512  
 513          // Finding more than one plan, with template.
 514          $p6 = $lpg->create_plan(array('userid' => $u1->id, 'templateid' => $tpl1->get('id')));
 515          $plans = plan::get_by_user_and_competency($u1->id, $c1->get('id'));
 516          $this->assertCount(3, $plans);
 517          $plan = array_shift($plans);
 518          $this->assertEquals($p1->get('id'), $plan->get('id'));
 519          $plan = array_shift($plans);
 520          $this->assertEquals($p5->get('id'), $plan->get('id'));
 521          $plan = array_shift($plans);
 522          $this->assertEquals($p6->get('id'), $plan->get('id'));
 523  
 524          // Finding no plans.
 525          $plans = plan::get_by_user_and_competency($u1->id, $c2->get('id'));
 526          $this->assertCount(0, $plans);
 527      }
 528  
 529      public function test_get_competency() {
 530          $this->resetAfterTest();
 531          $this->setAdminUser();
 532  
 533          $dg = $this->getDataGenerator();
 534          $lpg = $dg->get_plugin_generator('core_competency');
 535  
 536          $u1 = $dg->create_user();
 537          $u2 = $dg->create_user();
 538          $u3 = $dg->create_user();
 539          $u4 = $dg->create_user();
 540  
 541          $f1 = $lpg->create_framework();
 542          $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
 543          $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
 544          $c3 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
 545          $c4 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
 546  
 547          $tpl1 = $lpg->create_template();
 548          $p1 = $lpg->create_plan(array('userid' => $u1->id));
 549          $p2 = $lpg->create_plan(array('userid' => $u2->id));
 550          $p3 = $lpg->create_plan(array('userid' => $u3->id, 'templateid' => $tpl1->get('id')));
 551          $p4 = $lpg->create_plan(array('userid' => $u4->id, 'templateid' => $tpl1->get('id')));
 552  
 553          $lpg->create_plan_competency(array('planid' => $p1->get('id'), 'competencyid' => $c1->get('id')));
 554          $lpg->create_plan_competency(array('planid' => $p2->get('id'), 'competencyid' => $c2->get('id')));
 555          $lpg->create_template_competency(array('templateid' => $tpl1->get('id'), 'competencyid' => $c3->get('id')));
 556          $lpg->create_template_competency(array('templateid' => $tpl1->get('id'), 'competencyid' => $c4->get('id')));
 557  
 558          // Completing the plans and removing a competency from the template.
 559          api::complete_plan($p2);
 560          api::complete_plan($p4);
 561          api::remove_competency_from_template($tpl1->get('id'), $c4->get('id'));
 562  
 563          // We can find all competencies.
 564          $this->assertEquals($c1->to_record(), $p1->get_competency($c1->get('id'))->to_record());
 565          $this->assertEquals($c2->to_record(), $p2->get_competency($c2->get('id'))->to_record());
 566          $this->assertEquals($c3->to_record(), $p3->get_competency($c3->get('id'))->to_record());
 567          $this->assertEquals($c4->to_record(), $p4->get_competency($c4->get('id'))->to_record());
 568  
 569          // Getting the competency 4 from the non-completed plan based on a template p4, will throw an exception.
 570          $this->expectException('coding_exception');
 571          $this->expectExceptionMessage('The competency does not belong to this template:');
 572          $p3->get_competency($c4->get('id'));
 573      }
 574  }