Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

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