[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/competency/tests/ -> external_test.php (source)

   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   * External tests.
  19   *
  20   * @package    core_competency
  21   * @copyright  2016 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  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  29  
  30  use core_competency\api;
  31  use core_competency\course_competency_settings;
  32  use core_competency\external;
  33  use core_competency\invalid_persistent_exception;
  34  use core_competency\plan;
  35  use core_competency\plan_competency;
  36  use core_competency\related_competency;
  37  use core_competency\template;
  38  use core_competency\template_competency;
  39  use core_competency\user_competency;
  40  use core_competency\user_competency_plan;
  41  
  42  /**
  43   * External testcase.
  44   *
  45   * @package    core_competency
  46   * @copyright  2016 Frédéric Massart - FMCorz.net
  47   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  48   */
  49  class core_competency_external_testcase extends externallib_advanced_testcase {
  50  
  51      /** @var stdClass $creator User with enough permissions to create insystem context. */
  52      protected $creator = null;
  53  
  54      /** @var stdClass $learningplancreator User with enough permissions to create incategory context. */
  55      protected $catcreator = null;
  56  
  57      /** @var stdClass $category Category */
  58      protected $category = null;
  59  
  60      /** @var stdClass $user User with enough permissions to view insystem context */
  61      protected $user = null;
  62  
  63      /** @var stdClass $catuser User with enough permissions to view incategory context */
  64      protected $catuser = null;
  65  
  66      /** @var int Creator role id */
  67      protected $creatorrole = null;
  68  
  69      /** @var int User role id */
  70      protected $userrole = null;
  71  
  72      /** @var stdClass $scale1 Scale */
  73      protected $scale1 = null;
  74  
  75      /** @var stdClass $scale2 Scale */
  76      protected $scale2 = null;
  77  
  78      /** @var stdClass $scale3 Scale */
  79      protected $scale3 = null;
  80  
  81      /** @var stdClass $scale4 Scale */
  82      protected $scale4 = null;
  83  
  84      /** @var string scaleconfiguration */
  85      protected $scaleconfiguration1 = null;
  86  
  87      /** @var string scaleconfiguration */
  88      protected $scaleconfiguration2 = null;
  89  
  90      /** @var string catscaleconfiguration */
  91      protected $scaleconfiguration3 = null;
  92  
  93      /** @var string catscaleconfiguration */
  94      protected $catscaleconfiguration4 = null;
  95  
  96      /**
  97       * Setup function- we will create a course and add an assign instance to it.
  98       */
  99      protected function setUp() {
 100          global $DB;
 101  
 102          $this->resetAfterTest(true);
 103  
 104          // Create some users.
 105          $creator = $this->getDataGenerator()->create_user();
 106          $user = $this->getDataGenerator()->create_user();
 107          $catuser = $this->getDataGenerator()->create_user();
 108          $category = $this->getDataGenerator()->create_category();
 109          $othercategory = $this->getDataGenerator()->create_category();
 110          $catcreator = $this->getDataGenerator()->create_user();
 111  
 112          $syscontext = context_system::instance();
 113          $catcontext = context_coursecat::instance($category->id);
 114          $othercatcontext = context_coursecat::instance($othercategory->id);
 115  
 116          // Fetching default authenticated user role.
 117          $userroles = get_archetype_roles('user');
 118          $this->assertCount(1, $userroles);
 119          $authrole = array_pop($userroles);
 120  
 121          // Reset all default authenticated users permissions.
 122          unassign_capability('moodle/competency:competencygrade', $authrole->id);
 123          unassign_capability('moodle/competency:competencymanage', $authrole->id);
 124          unassign_capability('moodle/competency:competencyview', $authrole->id);
 125          unassign_capability('moodle/competency:planmanage', $authrole->id);
 126          unassign_capability('moodle/competency:planmanagedraft', $authrole->id);
 127          unassign_capability('moodle/competency:planmanageown', $authrole->id);
 128          unassign_capability('moodle/competency:planview', $authrole->id);
 129          unassign_capability('moodle/competency:planviewdraft', $authrole->id);
 130          unassign_capability('moodle/competency:planviewown', $authrole->id);
 131          unassign_capability('moodle/competency:planviewowndraft', $authrole->id);
 132          unassign_capability('moodle/competency:templatemanage', $authrole->id);
 133          unassign_capability('moodle/competency:templateview', $authrole->id);
 134          unassign_capability('moodle/cohort:manage', $authrole->id);
 135          unassign_capability('moodle/competency:coursecompetencyconfigure', $authrole->id);
 136  
 137          // Creating specific roles.
 138          $this->creatorrole = create_role('Creator role', 'creatorrole', 'learning plan creator role description');
 139          $this->userrole = create_role('User role', 'userrole', 'learning plan user role description');
 140  
 141          assign_capability('moodle/competency:competencymanage', CAP_ALLOW, $this->creatorrole, $syscontext->id);
 142          assign_capability('moodle/competency:competencycompetencyconfigure', CAP_ALLOW, $this->creatorrole, $syscontext->id);
 143          assign_capability('moodle/competency:competencyview', CAP_ALLOW, $this->userrole, $syscontext->id);
 144          assign_capability('moodle/competency:planmanage', CAP_ALLOW, $this->creatorrole, $syscontext->id);
 145          assign_capability('moodle/competency:planmanagedraft', CAP_ALLOW, $this->creatorrole, $syscontext->id);
 146          assign_capability('moodle/competency:planmanageown', CAP_ALLOW, $this->creatorrole, $syscontext->id);
 147          assign_capability('moodle/competency:planview', CAP_ALLOW, $this->creatorrole, $syscontext->id);
 148          assign_capability('moodle/competency:planviewdraft', CAP_ALLOW, $this->creatorrole, $syscontext->id);
 149          assign_capability('moodle/competency:templatemanage', CAP_ALLOW, $this->creatorrole, $syscontext->id);
 150          assign_capability('moodle/competency:competencygrade', CAP_ALLOW, $this->creatorrole, $syscontext->id);
 151          assign_capability('moodle/cohort:manage', CAP_ALLOW, $this->creatorrole, $syscontext->id);
 152          assign_capability('moodle/competency:templateview', CAP_ALLOW, $this->userrole, $syscontext->id);
 153          assign_capability('moodle/competency:planviewown', CAP_ALLOW, $this->userrole, $syscontext->id);
 154          assign_capability('moodle/competency:planviewowndraft', CAP_ALLOW, $this->userrole, $syscontext->id);
 155  
 156          role_assign($this->creatorrole, $creator->id, $syscontext->id);
 157          role_assign($this->creatorrole, $catcreator->id, $catcontext->id);
 158          role_assign($this->userrole, $user->id, $syscontext->id);
 159          role_assign($this->userrole, $catuser->id, $catcontext->id);
 160  
 161          $this->creator = $creator;
 162          $this->catcreator = $catcreator;
 163          $this->user = $user;
 164          $this->catuser = $catuser;
 165          $this->category = $category;
 166          $this->othercategory = $othercategory;
 167  
 168          $this->scale1 = $this->getDataGenerator()->create_scale(array("scale" => "value1, value2"));
 169          $this->scale2 = $this->getDataGenerator()->create_scale(array("scale" => "value3, value4"));
 170          $this->scale3 = $this->getDataGenerator()->create_scale(array("scale" => "value5, value6"));
 171          $this->scale4 = $this->getDataGenerator()->create_scale(array("scale" => "value7, value8"));
 172  
 173          $this->scaleconfiguration1 = '[{"scaleid":"'.$this->scale1->id.'"},' .
 174                  '{"name":"value1","id":1,"scaledefault":1,"proficient":0},' .
 175                  '{"name":"value2","id":2,"scaledefault":0,"proficient":1}]';
 176          $this->scaleconfiguration2 = '[{"scaleid":"'.$this->scale2->id.'"},' .
 177                  '{"name":"value3","id":1,"scaledefault":1,"proficient":0},' .
 178                  '{"name":"value4","id":2,"scaledefault":0,"proficient":1}]';
 179          $this->scaleconfiguration3 = '[{"scaleid":"'.$this->scale3->id.'"},' .
 180                  '{"name":"value5","id":1,"scaledefault":1,"proficient":0},' .
 181                  '{"name":"value6","id":2,"scaledefault":0,"proficient":1}]';
 182          $this->scaleconfiguration4 = '[{"scaleid":"'.$this->scale4->id.'"},'.
 183                  '{"name":"value8","id":1,"scaledefault":1,"proficient":0},' .
 184                  '{"name":"value8","id":2,"scaledefault":0,"proficient":1}]';
 185          accesslib_clear_all_caches_for_unit_testing();
 186      }
 187  
 188  
 189      protected function create_competency_framework($number = 1, $system = true) {
 190          $scalename = 'scale' . $number;
 191          $scalepropname = 'scaleconfiguration' . $number;
 192          $framework = array(
 193              'shortname' => 'shortname' . $number,
 194              'idnumber' => 'idnumber' . $number,
 195              'description' => 'description' . $number,
 196              'descriptionformat' => FORMAT_HTML,
 197              'scaleid' => $this->$scalename->id,
 198              'scaleconfiguration' => $this->$scalepropname,
 199              'visible' => true,
 200              'contextid' => $system ? context_system::instance()->id : context_coursecat::instance($this->category->id)->id
 201          );
 202          $result = external::create_competency_framework($framework);
 203          return (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result);
 204      }
 205  
 206      protected function create_plan($number, $userid, $templateid, $status, $duedate) {
 207          $plan = array(
 208              'name' => 'name' . $number,
 209              'description' => 'description' . $number,
 210              'descriptionformat' => FORMAT_HTML,
 211              'userid' => $userid,
 212              'templateid' => empty($templateid) ? null : $templateid,
 213              'status' => $status,
 214              'duedate' => $duedate
 215          );
 216          $result = external::create_plan($plan);
 217          return (object) external_api::clean_returnvalue(external::create_plan_returns(), $result);
 218      }
 219  
 220      protected function create_template($number, $system) {
 221          $template = array(
 222              'shortname' => 'shortname' . $number,
 223              'description' => 'description' . $number,
 224              'descriptionformat' => FORMAT_HTML,
 225              'duedate' => 0,
 226              'visible' => true,
 227              'contextid' => $system ? context_system::instance()->id : context_coursecat::instance($this->category->id)->id
 228          );
 229          $result = external::create_template($template);
 230          return (object) external_api::clean_returnvalue(external::create_template_returns(), $result);
 231      }
 232  
 233      protected function update_template($templateid, $number) {
 234          $template = array(
 235              'id' => $templateid,
 236              'shortname' => 'shortname' . $number,
 237              'description' => 'description' . $number,
 238              'descriptionformat' => FORMAT_HTML,
 239              'visible' => true
 240          );
 241          $result = external::update_template($template);
 242          return external_api::clean_returnvalue(external::update_template_returns(), $result);
 243      }
 244  
 245      protected function update_plan($planid, $number, $userid, $templateid, $status, $duedate) {
 246          $plan = array(
 247              'id' => $planid,
 248              'name' => 'name' . $number,
 249              'description' => 'description' . $number,
 250              'descriptionformat' => FORMAT_HTML,
 251              'userid' => $userid,
 252              'templateid' => $templateid,
 253              'status' => $status,
 254              'duedate' => $duedate
 255          );
 256          $result = external::update_plan($plan);
 257          return external_api::clean_returnvalue(external::update_plan_returns(), $result);
 258      }
 259  
 260      protected function update_competency_framework($id, $number = 1, $system = true) {
 261          $scalename = 'scale' . $number;
 262          $scalepropname = 'scaleconfiguration' . $number;
 263          $framework = array(
 264              'id' => $id,
 265              'shortname' => 'shortname' . $number,
 266              'idnumber' => 'idnumber' . $number,
 267              'description' => 'description' . $number,
 268              'descriptionformat' => FORMAT_HTML,
 269              'scaleid' => $this->$scalename->id,
 270              'scaleconfiguration' => $this->$scalepropname,
 271              'visible' => true,
 272              'contextid' => $system ? context_system::instance()->id : context_coursecat::instance($this->category->id)->id
 273          );
 274          $result = external::update_competency_framework($framework);
 275          return external_api::clean_returnvalue(external::update_competency_framework_returns(), $result);
 276      }
 277  
 278      protected function create_competency($number, $frameworkid) {
 279          $competency = array(
 280              'shortname' => 'shortname' . $number,
 281              'idnumber' => 'idnumber' . $number,
 282              'description' => 'description' . $number,
 283              'descriptionformat' => FORMAT_HTML,
 284              'competencyframeworkid' => $frameworkid,
 285              'sortorder' => 0
 286          );
 287          $result = external::create_competency($competency);
 288          return (object) external_api::clean_returnvalue(external::create_competency_returns(), $result);
 289      }
 290  
 291      protected function update_competency($id, $number) {
 292          $competency = array(
 293              'id' => $id,
 294              'shortname' => 'shortname' . $number,
 295              'idnumber' => 'idnumber' . $number,
 296              'description' => 'description' . $number,
 297              'descriptionformat' => FORMAT_HTML,
 298              'sortorder' => 0
 299          );
 300          $result = external::update_competency($competency);
 301          return external_api::clean_returnvalue(external::update_competency_returns(), $result);
 302      }
 303  
 304      /**
 305       * Test we can't create a competency framework with only read permissions.
 306       *
 307       * @expectedException required_capability_exception
 308       */
 309      public function test_create_competency_frameworks_with_read_permissions() {
 310          $this->setUser($this->user);
 311  
 312          $result = $this->create_competency_framework(1, true);
 313      }
 314  
 315      /**
 316       * Test we can't create a competency framework with only read permissions.
 317       *
 318       * @expectedException required_capability_exception
 319       */
 320      public function test_create_competency_frameworks_with_read_permissions_in_category() {
 321          $this->setUser($this->catuser);
 322          $result = $this->create_competency_framework(1, false);
 323      }
 324  
 325      /**
 326       * Test we can create a competency framework with manage permissions.
 327       */
 328      public function test_create_competency_frameworks_with_manage_permissions() {
 329          $this->setUser($this->creator);
 330          $result = $this->create_competency_framework(1, true);
 331  
 332          $this->assertGreaterThan(0, $result->timecreated);
 333          $this->assertGreaterThan(0, $result->timemodified);
 334          $this->assertEquals($this->creator->id, $result->usermodified);
 335          $this->assertEquals('shortname1', $result->shortname);
 336          $this->assertEquals('idnumber1', $result->idnumber);
 337          $this->assertEquals('description1', $result->description);
 338          $this->assertEquals(FORMAT_HTML, $result->descriptionformat);
 339          $this->assertEquals($this->scale1->id, $result->scaleid);
 340          $this->assertEquals($this->scaleconfiguration1, $result->scaleconfiguration);
 341          $this->assertEquals(true, $result->visible);
 342      }
 343  
 344      /**
 345       * Test we can create a competency framework with manage permissions.
 346       */
 347      public function test_create_competency_frameworks_with_manage_permissions_in_category() {
 348          $this->setUser($this->catcreator);
 349          $result = $this->create_competency_framework(1, false);
 350  
 351          $this->assertGreaterThan(0, $result->timecreated);
 352          $this->assertGreaterThan(0, $result->timemodified);
 353          $this->assertEquals($this->catcreator->id, $result->usermodified);
 354          $this->assertEquals('shortname1', $result->shortname);
 355          $this->assertEquals('idnumber1', $result->idnumber);
 356          $this->assertEquals('description1', $result->description);
 357          $this->assertEquals(FORMAT_HTML, $result->descriptionformat);
 358          $this->assertEquals($this->scale1->id, $result->scaleid);
 359          $this->assertEquals($this->scaleconfiguration1, $result->scaleconfiguration);
 360          $this->assertEquals(true, $result->visible);
 361  
 362          try {
 363              $result = $this->create_competency_framework(1, true);
 364              $this->fail('User cannot create a framework at system level.');
 365          } catch (required_capability_exception $e) {
 366              // All good.
 367          }
 368      }
 369  
 370      /**
 371       * Test we cannot create a competency framework with nasty data.
 372       *
 373       * @expectedException invalid_parameter_exception
 374       */
 375      public function test_create_competency_frameworks_with_nasty_data() {
 376          $this->setUser($this->creator);
 377          $framework = array(
 378              'shortname' => 'short<a href="">',
 379              'idnumber' => 'id;"number',
 380              'description' => 'de<>\\..scription',
 381              'descriptionformat' => FORMAT_HTML,
 382              'scaleid' => $this->scale1->id,
 383              'scaleconfiguration' => $this->scaleconfiguration1,
 384              'visible' => true,
 385              'contextid' => context_system::instance()->id
 386          );
 387          $result = external::create_competency_framework($framework);
 388      }
 389  
 390      /**
 391       * Test we can read a competency framework with manage permissions.
 392       */
 393      public function test_read_competency_frameworks_with_manage_permissions() {
 394          $this->setUser($this->creator);
 395          $result = $this->create_competency_framework(1, true);
 396  
 397          $id = $result->id;
 398          $result = external::read_competency_framework($id);
 399          $result = (object) external_api::clean_returnvalue(external::read_competency_framework_returns(), $result);
 400  
 401          $this->assertGreaterThan(0, $result->timecreated);
 402          $this->assertGreaterThan(0, $result->timemodified);
 403          $this->assertEquals($this->creator->id, $result->usermodified);
 404          $this->assertEquals('shortname1', $result->shortname);
 405          $this->assertEquals('idnumber1', $result->idnumber);
 406          $this->assertEquals('description1', $result->description);
 407          $this->assertEquals(FORMAT_HTML, $result->descriptionformat);
 408          $this->assertEquals($this->scale1->id, $result->scaleid);
 409          $this->assertEquals($this->scaleconfiguration1, $result->scaleconfiguration);
 410          $this->assertEquals(true, $result->visible);
 411      }
 412  
 413      /**
 414       * Test we can read a competency framework with manage permissions.
 415       */
 416      public function test_read_competency_frameworks_with_manage_permissions_in_category() {
 417          $this->setUser($this->creator);
 418  
 419          $insystem = $this->create_competency_framework(1, true);
 420          $incat = $this->create_competency_framework(2, false);
 421  
 422          $this->setUser($this->catcreator);
 423          $id = $incat->id;
 424          $result = external::read_competency_framework($id);
 425          $result = (object) external_api::clean_returnvalue(external::read_competency_framework_returns(), $result);
 426  
 427          $this->assertGreaterThan(0, $result->timecreated);
 428          $this->assertGreaterThan(0, $result->timemodified);
 429          $this->assertEquals($this->creator->id, $result->usermodified);
 430          $this->assertEquals('shortname2', $result->shortname);
 431          $this->assertEquals('idnumber2', $result->idnumber);
 432          $this->assertEquals('description2', $result->description);
 433          $this->assertEquals(FORMAT_HTML, $result->descriptionformat);
 434          $this->assertEquals($this->scale2->id, $result->scaleid);
 435          $this->assertEquals($this->scaleconfiguration2, $result->scaleconfiguration);
 436          $this->assertEquals(true, $result->visible);
 437  
 438          try {
 439              $id = $insystem->id;
 440              $result = external::read_competency_framework($id);
 441              $result = (object) external_api::clean_returnvalue(external::read_competency_framework_returns(), $result);
 442              $this->fail('User cannot read a framework at system level.');
 443          } catch (required_capability_exception $e) {
 444              // All good.
 445          }
 446      }
 447  
 448      /**
 449       * Test we can read a competency framework with read permissions.
 450       */
 451      public function test_read_competency_frameworks_with_read_permissions() {
 452          $this->setUser($this->creator);
 453          $result = $this->create_competency_framework(1, true);
 454  
 455          // Switch users to someone with less permissions.
 456          $this->setUser($this->user);
 457          $id = $result->id;
 458          $result = external::read_competency_framework($id);
 459          $result = (object) external_api::clean_returnvalue(external::read_competency_framework_returns(), $result);
 460  
 461          $this->assertGreaterThan(0, $result->timecreated);
 462          $this->assertGreaterThan(0, $result->timemodified);
 463          $this->assertEquals($this->creator->id, $result->usermodified);
 464          $this->assertEquals('shortname1', $result->shortname);
 465          $this->assertEquals('idnumber1', $result->idnumber);
 466          $this->assertEquals('description1', $result->description);
 467          $this->assertEquals(FORMAT_HTML, $result->descriptionformat);
 468          $this->assertEquals($this->scale1->id, $result->scaleid);
 469          $this->assertEquals($this->scaleconfiguration1, $result->scaleconfiguration);
 470          $this->assertEquals(true, $result->visible);
 471      }
 472      /**
 473       * Test we can read a competency framework with read permissions.
 474       */
 475      public function test_read_competency_frameworks_with_read_permissions_in_category() {
 476          $this->setUser($this->creator);
 477  
 478          $insystem = $this->create_competency_framework(1, true);
 479          $incat = $this->create_competency_framework(2, false);
 480  
 481          // Switch users to someone with less permissions.
 482          $this->setUser($this->catuser);
 483          $id = $incat->id;
 484          $result = external::read_competency_framework($id);
 485          $result = (object) external_api::clean_returnvalue(external::read_competency_framework_returns(), $result);
 486  
 487          $this->assertGreaterThan(0, $result->timecreated);
 488          $this->assertGreaterThan(0, $result->timemodified);
 489          $this->assertEquals($this->creator->id, $result->usermodified);
 490          $this->assertEquals('shortname2', $result->shortname);
 491          $this->assertEquals('idnumber2', $result->idnumber);
 492          $this->assertEquals('description2', $result->description);
 493          $this->assertEquals(FORMAT_HTML, $result->descriptionformat);
 494          $this->assertEquals($this->scale2->id, $result->scaleid);
 495          $this->assertEquals($this->scaleconfiguration2, $result->scaleconfiguration);
 496          $this->assertEquals(true, $result->visible);
 497  
 498          // Switching to user with no permissions.
 499          try {
 500              $result = external::read_competency_framework($insystem->id);
 501              $this->fail('Current user cannot should not be able to read the framework.');
 502          } catch (required_capability_exception $e) {
 503              // All good.
 504          }
 505      }
 506  
 507      /**
 508       * Test we can delete a competency framework with manage permissions.
 509       */
 510      public function test_delete_competency_frameworks_with_manage_permissions() {
 511          $this->setUser($this->creator);
 512          $result = $this->create_competency_framework(1, true);
 513  
 514          $id = $result->id;
 515          $result = external::delete_competency_framework($id);
 516          $result = external_api::clean_returnvalue(external::delete_competency_framework_returns(), $result);
 517  
 518          $this->assertTrue($result);
 519      }
 520  
 521      /**
 522       * Test we can delete a competency framework with manage permissions.
 523       */
 524      public function test_delete_competency_frameworks_with_manage_permissions_in_category() {
 525          $this->setUser($this->creator);
 526  
 527          $insystem = $this->create_competency_framework(1, true);
 528          $incat = $this->create_competency_framework(2, false);
 529  
 530          $this->setUser($this->catcreator);
 531          $id = $incat->id;
 532          $result = external::delete_competency_framework($id);
 533          $result = external_api::clean_returnvalue(external::delete_competency_framework_returns(), $result);
 534  
 535          $this->assertTrue($result);
 536  
 537          try {
 538              $id = $insystem->id;
 539              $result = external::delete_competency_framework($id);
 540              $result = external_api::clean_returnvalue(external::delete_competency_framework_returns(), $result);
 541              $this->fail('Current user cannot should not be able to delete the framework.');
 542          } catch (required_capability_exception $e) {
 543              // All good.
 544          }
 545      }
 546  
 547      /**
 548       * Test we can delete a competency framework with read permissions.
 549       *
 550       * @expectedException required_capability_exception
 551       */
 552      public function test_delete_competency_frameworks_with_read_permissions() {
 553          $this->setUser($this->creator);
 554          $result = $this->create_competency_framework(1, true);
 555  
 556          $id = $result->id;
 557          // Switch users to someone with less permissions.
 558          $this->setUser($this->user);
 559          $result = external::delete_competency_framework($id);
 560      }
 561  
 562      /**
 563       * Test we can update a competency framework with manage permissions.
 564       */
 565      public function test_update_competency_frameworks_with_manage_permissions() {
 566          $this->setUser($this->creator);
 567          $result = $this->create_competency_framework(1, true);
 568  
 569          $result = $this->update_competency_framework($result->id, 2, true);
 570  
 571          $this->assertTrue($result);
 572      }
 573  
 574      /**
 575       * Test we can update a competency framework with manage permissions.
 576       */
 577      public function test_update_competency_frameworks_with_manage_permissions_in_category() {
 578          $this->setUser($this->creator);
 579  
 580          $insystem = $this->create_competency_framework(1, true);
 581          $incat = $this->create_competency_framework(2, false);
 582  
 583          $this->setUser($this->catcreator);
 584          $id = $incat->id;
 585  
 586          $result = $this->update_competency_framework($incat->id, 3, false);
 587  
 588          $this->assertTrue($result);
 589  
 590          try {
 591              $result = $this->update_competency_framework($insystem->id, 4, true);
 592              $this->fail('Current user should not be able to update the framework.');
 593          } catch (required_capability_exception $e) {
 594              // All good.
 595          }
 596      }
 597  
 598      public function test_update_framework_scale() {
 599          $this->setUser($this->creator);
 600          $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
 601  
 602          $s1 = $this->getDataGenerator()->create_scale();
 603  
 604          $f1 = $lpg->create_framework(array('scaleid' => $s1->id));
 605          $f2 = $lpg->create_framework(array('scaleid' => $s1->id));
 606          $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
 607          $c2 = $lpg->create_competency(array('competencyframeworkid' => $f2->get_id()));
 608  
 609          $this->assertEquals($s1->id, $f1->get_scaleid());
 610  
 611          // Make the scale of f2 being used.
 612          $lpg->create_user_competency(array('userid' => $this->user->id, 'competencyid' => $c2->get_id()));
 613  
 614          // Changing the framework where the scale is not used.
 615          $result = $this->update_competency_framework($f1->get_id(), 3, true);
 616  
 617          $f1 = new \core_competency\competency_framework($f1->get_id());
 618          $this->assertEquals($this->scale3->id, $f1->get_scaleid());
 619  
 620          // Changing the framework where the scale is used.
 621          try {
 622              $result = $this->update_competency_framework($f2->get_id(), 4, true);
 623              $this->fail('The scale cannot be changed once used.');
 624          } catch (\core_competency\invalid_persistent_exception $e) {
 625              $this->assertRegexp('/scaleid/', $e->getMessage());
 626          }
 627      }
 628  
 629      /**
 630       * Test we can update a competency framework with read permissions.
 631       *
 632       * @expectedException required_capability_exception
 633       */
 634      public function test_update_competency_frameworks_with_read_permissions() {
 635          $this->setUser($this->creator);
 636          $result = $this->create_competency_framework(1, true);
 637  
 638          $this->setUser($this->user);
 639          $result = $this->update_competency_framework($result->id, 2, true);
 640      }
 641  
 642      /**
 643       * Test we can list and count competency frameworks with manage permissions.
 644       */
 645      public function test_list_and_count_competency_frameworks_with_manage_permissions() {
 646          $this->setUser($this->creator);
 647          $result = $this->create_competency_framework(1, true);
 648          $result = $this->create_competency_framework(2, true);
 649          $result = $this->create_competency_framework(3, true);
 650          $result = $this->create_competency_framework(4, false);
 651  
 652          $result = external::count_competency_frameworks(array('contextid' => context_system::instance()->id), 'self');
 653          $result = external_api::clean_returnvalue(external::count_competency_frameworks_returns(), $result);
 654  
 655          $this->assertEquals($result, 3);
 656  
 657          $result = external::list_competency_frameworks('shortname', 'ASC', 0, 10,
 658              array('contextid' => context_system::instance()->id), 'self', false);
 659          $result = external_api::clean_returnvalue(external::list_competency_frameworks_returns(), $result);
 660  
 661          $this->assertEquals(count($result), 3);
 662          $result = (object) $result[0];
 663  
 664          $this->assertGreaterThan(0, $result->timecreated);
 665          $this->assertGreaterThan(0, $result->timemodified);
 666          $this->assertEquals($this->creator->id, $result->usermodified);
 667          $this->assertEquals('shortname1', $result->shortname);
 668          $this->assertEquals('idnumber1', $result->idnumber);
 669          $this->assertEquals('description1', $result->description);
 670          $this->assertEquals(FORMAT_HTML, $result->descriptionformat);
 671          $this->assertEquals($this->scale1->id, $result->scaleid);
 672          $this->assertEquals($this->scaleconfiguration1, $result->scaleconfiguration);
 673          $this->assertEquals(true, $result->visible);
 674      }
 675  
 676      public function test_list_competency_frameworks_with_query() {
 677          $this->setUser($this->creator);
 678          $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
 679          $framework1 = $lpg->create_framework(array(
 680              'shortname' => 'shortname_beetroot',
 681              'idnumber' => 'idnumber_cinnamon',
 682              'description' => 'description',
 683              'descriptionformat' => FORMAT_HTML,
 684              'visible' => true,
 685              'contextid' => context_system::instance()->id
 686          ));
 687          $framework2 = $lpg->create_framework(array(
 688              'shortname' => 'shortname_citrus',
 689              'idnumber' => 'idnumber_beer',
 690              'description' => 'description',
 691              'descriptionformat' => FORMAT_HTML,
 692              'visible' => true,
 693              'contextid' => context_system::instance()->id
 694          ));
 695  
 696          // Search on both ID number and shortname.
 697          $result = external::list_competency_frameworks('shortname', 'ASC', 0, 10,
 698              array('contextid' => context_system::instance()->id), 'self', false, 'bee');
 699          $result = external_api::clean_returnvalue(external::list_competency_frameworks_returns(), $result);
 700          $this->assertCount(2, $result);
 701          $f = (object) array_shift($result);
 702          $this->assertEquals($framework1->get_id(), $f->id);
 703          $f = (object) array_shift($result);
 704          $this->assertEquals($framework2->get_id(), $f->id);
 705  
 706          // Search on ID number.
 707          $result = external::list_competency_frameworks('shortname', 'ASC', 0, 10,
 708              array('contextid' => context_system::instance()->id), 'self', false, 'beer');
 709          $result = external_api::clean_returnvalue(external::list_competency_frameworks_returns(), $result);
 710          $this->assertCount(1, $result);
 711          $f = (object) array_shift($result);
 712          $this->assertEquals($framework2->get_id(), $f->id);
 713  
 714          // Search on shortname.
 715          $result = external::list_competency_frameworks('shortname', 'ASC', 0, 10,
 716              array('contextid' => context_system::instance()->id), 'self', false, 'cinnamon');
 717          $result = external_api::clean_returnvalue(external::list_competency_frameworks_returns(), $result);
 718          $this->assertCount(1, $result);
 719          $f = (object) array_shift($result);
 720          $this->assertEquals($framework1->get_id(), $f->id);
 721  
 722          // No match.
 723          $result = external::list_competency_frameworks('shortname', 'ASC', 0, 10,
 724              array('contextid' => context_system::instance()->id), 'self', false, 'pwnd!');
 725          $result = external_api::clean_returnvalue(external::list_competency_frameworks_returns(), $result);
 726          $this->assertCount(0, $result);
 727      }
 728  
 729      /**
 730       * Test we can list and count competency frameworks with read permissions.
 731       */
 732      public function test_list_and_count_competency_frameworks_with_read_permissions() {
 733          $this->setUser($this->creator);
 734          $result = $this->create_competency_framework(1, true);
 735          $result = $this->create_competency_framework(2, true);
 736          $result = $this->create_competency_framework(3, true);
 737          $result = $this->create_competency_framework(4, false);
 738  
 739          $this->setUser($this->user);
 740          $result = external::count_competency_frameworks(array('contextid' => context_system::instance()->id), 'self');
 741          $result = external_api::clean_returnvalue(external::count_competency_frameworks_returns(), $result);
 742          $this->assertEquals($result, 3);
 743  
 744          $result = external::list_competency_frameworks('shortname', 'ASC', 0, 10,
 745              array('contextid' => context_system::instance()->id), 'self', false);
 746          $result = external_api::clean_returnvalue(external::list_competency_frameworks_returns(), $result);
 747  
 748          $this->assertEquals(count($result), 3);
 749          $result = (object) $result[0];
 750  
 751          $this->assertGreaterThan(0, $result->timecreated);
 752          $this->assertGreaterThan(0, $result->timemodified);
 753          $this->assertEquals($this->creator->id, $result->usermodified);
 754          $this->assertEquals('shortname1', $result->shortname);
 755          $this->assertEquals('idnumber1', $result->idnumber);
 756          $this->assertEquals('description1', $result->description);
 757          $this->assertEquals(FORMAT_HTML, $result->descriptionformat);
 758          $this->assertEquals($this->scale1->id, $result->scaleid);
 759          $this->assertEquals($this->scaleconfiguration1, $result->scaleconfiguration);
 760          $this->assertEquals(true, $result->visible);
 761      }
 762  
 763      /**
 764       * Test we can't create a competency with only read permissions.
 765       *
 766       * @expectedException required_capability_exception
 767       */
 768      public function test_create_competency_with_read_permissions() {
 769          $framework = $this->getDataGenerator()->get_plugin_generator('core_competency')->create_framework();
 770          $this->setUser($this->user);
 771          $competency = $this->create_competency(1, $framework->get_id());
 772      }
 773  
 774      /**
 775       * Test we can create a competency with manage permissions.
 776       */
 777      public function test_create_competency_with_manage_permissions() {
 778          $this->setUser($this->creator);
 779          $framework = $this->create_competency_framework(1, true);
 780          $competency = $this->create_competency(1, $framework->id);
 781  
 782          $this->assertGreaterThan(0, $competency->timecreated);
 783          $this->assertGreaterThan(0, $competency->timemodified);
 784          $this->assertEquals($this->creator->id, $competency->usermodified);
 785          $this->assertEquals('shortname1', $competency->shortname);
 786          $this->assertEquals('idnumber1', $competency->idnumber);
 787          $this->assertEquals('description1', $competency->description);
 788          $this->assertEquals(FORMAT_HTML, $competency->descriptionformat);
 789          $this->assertEquals(0, $competency->parentid);
 790          $this->assertEquals($framework->id, $competency->competencyframeworkid);
 791      }
 792  
 793  
 794      /**
 795       * Test we can create a competency with manage permissions.
 796       */
 797      public function test_create_competency_with_manage_permissions_in_category() {
 798          $this->setUser($this->creator);
 799  
 800          $insystem = $this->create_competency_framework(1, true);
 801          $incat = $this->create_competency_framework(2, false);
 802  
 803          $this->setUser($this->catcreator);
 804  
 805          $competency = $this->create_competency(1, $incat->id);
 806  
 807          $this->assertGreaterThan(0, $competency->timecreated);
 808          $this->assertGreaterThan(0, $competency->timemodified);
 809          $this->assertEquals($this->catcreator->id, $competency->usermodified);
 810          $this->assertEquals('shortname1', $competency->shortname);
 811          $this->assertEquals('idnumber1', $competency->idnumber);
 812          $this->assertEquals('description1', $competency->description);
 813          $this->assertEquals(FORMAT_HTML, $competency->descriptionformat);
 814          $this->assertEquals(0, $competency->parentid);
 815          $this->assertEquals($incat->id, $competency->competencyframeworkid);
 816  
 817          try {
 818              $competency = $this->create_competency(2, $insystem->id);
 819              $this->fail('User should not be able to create a competency in system context.');
 820          } catch (required_capability_exception $e) {
 821              // All good.
 822          }
 823      }
 824  
 825      /**
 826       * Test we cannot create a competency with nasty data.
 827       *
 828       * @expectedException invalid_parameter_exception
 829       */
 830      public function test_create_competency_with_nasty_data() {
 831          $this->setUser($this->creator);
 832          $framework = $this->create_competency_framework(1, true);
 833          $competency = array(
 834              'shortname' => 'shortname<a href="">',
 835              'idnumber' => 'id;"number',
 836              'description' => 'de<>\\..scription',
 837              'descriptionformat' => FORMAT_HTML,
 838              'competencyframeworkid' => $framework->id,
 839              'sortorder' => 0
 840          );
 841          $result = external::create_competency($competency);
 842          $result = (object) external_api::clean_returnvalue(external::create_competency_returns(), $result);
 843      }
 844  
 845      /**
 846       * Test we can read a competency with manage permissions.
 847       */
 848      public function test_read_competencies_with_manage_permissions() {
 849          $this->setUser($this->creator);
 850          $framework = $this->create_competency_framework(1, true);
 851          $competency = $this->create_competency(1, $framework->id);
 852  
 853          $id = $competency->id;
 854          $result = external::read_competency($id);
 855          $result = (object) external_api::clean_returnvalue(external::read_competency_returns(), $result);
 856  
 857          $this->assertGreaterThan(0, $result->timecreated);
 858          $this->assertGreaterThan(0, $result->timemodified);
 859          $this->assertEquals($this->creator->id, $result->usermodified);
 860          $this->assertEquals('shortname1', $result->shortname);
 861          $this->assertEquals('idnumber1', $result->idnumber);
 862          $this->assertEquals('description1', $result->description);
 863          $this->assertEquals(FORMAT_HTML, $result->descriptionformat);
 864          $this->assertEquals(0, $result->parentid);
 865          $this->assertEquals($framework->id, $result->competencyframeworkid);
 866      }
 867  
 868      /**
 869       * Test we can read a competency with manage permissions.
 870       */
 871      public function test_read_competencies_with_manage_permissions_in_category() {
 872          $this->setUser($this->creator);
 873  
 874          $sysframework = $this->create_competency_framework(1, true);
 875          $insystem = $this->create_competency(1, $sysframework->id);
 876  
 877          $catframework = $this->create_competency_framework(2, false);
 878          $incat = $this->create_competency(2, $catframework->id);
 879  
 880          $this->setUser($this->catcreator);
 881          $id = $incat->id;
 882          $result = external::read_competency($id);
 883          $result = (object) external_api::clean_returnvalue(external::read_competency_returns(), $result);
 884  
 885          $this->assertGreaterThan(0, $result->timecreated);
 886          $this->assertGreaterThan(0, $result->timemodified);
 887          $this->assertEquals($this->creator->id, $result->usermodified);
 888          $this->assertEquals('shortname2', $result->shortname);
 889          $this->assertEquals('idnumber2', $result->idnumber);
 890          $this->assertEquals('description2', $result->description);
 891          $this->assertEquals(FORMAT_HTML, $result->descriptionformat);
 892          $this->assertEquals(0, $result->parentid);
 893          $this->assertEquals($catframework->id, $result->competencyframeworkid);
 894  
 895          try {
 896              external::read_competency($insystem->id);
 897              $this->fail('User should not be able to read a competency in system context.');
 898          } catch (required_capability_exception $e) {
 899              // All good.
 900          }
 901      }
 902  
 903      /**
 904       * Test we can read a competency with read permissions.
 905       */
 906      public function test_read_competencies_with_read_permissions() {
 907          $this->setUser($this->creator);
 908          $framework = $this->create_competency_framework(1, true);
 909          $competency = $this->create_competency(1, $framework->id);
 910  
 911          // Switch users to someone with less permissions.
 912          $this->setUser($this->user);
 913          $id = $competency->id;
 914          $result = external::read_competency($id);
 915          $result = (object) external_api::clean_returnvalue(external::read_competency_returns(), $result);
 916  
 917          $this->assertGreaterThan(0, $result->timecreated);
 918          $this->assertGreaterThan(0, $result->timemodified);
 919          $this->assertEquals($this->creator->id, $result->usermodified);
 920          $this->assertEquals('shortname1', $result->shortname);
 921          $this->assertEquals('idnumber1', $result->idnumber);
 922          $this->assertEquals('description1', $result->description);
 923          $this->assertEquals(FORMAT_HTML, $result->descriptionformat);
 924          $this->assertEquals(0, $result->parentid);
 925          $this->assertEquals($framework->id, $result->competencyframeworkid);
 926      }
 927  
 928      /**
 929       * Test we can read a competency with read permissions.
 930       */
 931      public function test_read_competencies_with_read_permissions_in_category() {
 932          $this->setUser($this->creator);
 933          $sysframework = $this->create_competency_framework(1, true);
 934          $insystem = $this->create_competency(1, $sysframework->id);
 935          $catframework = $this->create_competency_framework(2, false);
 936          $incat = $this->create_competency(2, $catframework->id);
 937  
 938          // Switch users to someone with less permissions.
 939          $this->setUser($this->catuser);
 940          $id = $incat->id;
 941          $result = external::read_competency($id);
 942          $result = (object) external_api::clean_returnvalue(external::read_competency_returns(), $result);
 943  
 944          $this->assertGreaterThan(0, $result->timecreated);
 945          $this->assertGreaterThan(0, $result->timemodified);
 946          $this->assertEquals($this->creator->id, $result->usermodified);
 947          $this->assertEquals('shortname2', $result->shortname);
 948          $this->assertEquals('idnumber2', $result->idnumber);
 949          $this->assertEquals('description2', $result->description);
 950          $this->assertEquals(FORMAT_HTML, $result->descriptionformat);
 951          $this->assertEquals(0, $result->parentid);
 952          $this->assertEquals($catframework->id, $result->competencyframeworkid);
 953  
 954          try {
 955              external::read_competency($insystem->id);
 956              $this->fail('User should not be able to read a competency in system context.');
 957          } catch (required_capability_exception $e) {
 958              // All good.
 959          }
 960      }
 961  
 962      /**
 963       * Test we can delete a competency with manage permissions.
 964       */
 965      public function test_delete_competency_with_manage_permissions() {
 966          $this->setUser($this->creator);
 967          $framework = $this->create_competency_framework(1, true);
 968          $result = $this->create_competency(1, $framework->id);
 969  
 970          $id = $result->id;
 971          $result = external::delete_competency($id);
 972          $result = external_api::clean_returnvalue(external::delete_competency_returns(), $result);
 973  
 974          $this->assertTrue($result);
 975      }
 976  
 977      /**
 978       * Test we can delete a competency with manage permissions.
 979       */
 980      public function test_delete_competency_with_manage_permissions_in_category() {
 981          $this->setUser($this->creator);
 982  
 983          $sysframework = $this->create_competency_framework(1, true);
 984          $insystem = $this->create_competency(1, $sysframework->id);
 985          $catframework = $this->create_competency_framework(2, false);
 986          $incat = $this->create_competency(2, $catframework->id);
 987  
 988          $this->setUser($this->catcreator);
 989          $id = $incat->id;
 990          $result = external::delete_competency($id);
 991          $result = external_api::clean_returnvalue(external::delete_competency_returns(), $result);
 992  
 993          $this->assertTrue($result);
 994  
 995          try {
 996              $result = external::delete_competency($insystem->id);
 997              $this->fail('User should not be able to delete a competency in system context.');
 998          } catch (required_capability_exception $e) {
 999              // All good.
1000          }
1001      }
1002  
1003      /**
1004       * Test we can delete a competency with read permissions.
1005       *
1006       * @expectedException required_capability_exception
1007       */
1008      public function test_delete_competency_with_read_permissions() {
1009          $this->setUser($this->creator);
1010          $framework = $this->create_competency_framework(1, true);
1011          $result = $this->create_competency(1, $framework->id);
1012  
1013          $id = $result->id;
1014          // Switch users to someone with less permissions.
1015          $this->setUser($this->user);
1016          $result = external::delete_competency($id);
1017      }
1018  
1019      /**
1020       * Test we can update a competency with manage permissions.
1021       */
1022      public function test_update_competency_with_manage_permissions() {
1023          $this->setUser($this->creator);
1024          $framework = $this->create_competency_framework(1, true);
1025          $result = $this->create_competency(1, $framework->id);
1026  
1027          $result = $this->update_competency($result->id, 2);
1028  
1029          $this->assertTrue($result);
1030      }
1031  
1032      /**
1033       * Test we can update a competency with manage permissions.
1034       */
1035      public function test_update_competency_with_manage_permissions_in_category() {
1036          $this->setUser($this->creator);
1037  
1038          $sysframework = $this->create_competency_framework(1, true);
1039          $insystem = $this->create_competency(1, $sysframework->id);
1040          $catframework = $this->create_competency_framework(2, false);
1041          $incat = $this->create_competency(2, $catframework->id);
1042  
1043          $this->setUser($this->catcreator);
1044  
1045          $result = $this->update_competency($incat->id, 2);
1046  
1047          $this->assertTrue($result);
1048  
1049          try {
1050              $result = $this->update_competency($insystem->id, 3);
1051              $this->fail('User should not be able to update a competency in system context.');
1052          } catch (required_capability_exception $e) {
1053              // All good.
1054          }
1055      }
1056  
1057      /**
1058       * Test we can update a competency with read permissions.
1059       *
1060       * @expectedException required_capability_exception
1061       */
1062      public function test_update_competency_with_read_permissions() {
1063          $this->setUser($this->creator);
1064          $framework = $this->create_competency_framework(1, true);
1065          $result = $this->create_competency(1, $framework->id);
1066  
1067          $this->setUser($this->user);
1068          $result = $this->update_competency($result->id, 2);
1069      }
1070  
1071      /**
1072       * Test count competencies with filters.
1073       */
1074      public function test_count_competencies_with_filters() {
1075          $this->setUser($this->creator);
1076  
1077          $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1078          $f1 = $lpg->create_framework();
1079          $f2 = $lpg->create_framework();
1080          $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
1081          $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'shortname' => 'A'));
1082          $c3 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
1083          $c4 = $lpg->create_competency(array('competencyframeworkid' => $f2->get_id()));
1084          $c5 = $lpg->create_competency(array('competencyframeworkid' => $f2->get_id()));
1085  
1086          $result = external::count_competencies(array(array('column' => 'competencyframeworkid', 'value' => $f2->get_id())));
1087          $result = external_api::clean_returnvalue(external::count_competencies_returns(), $result);
1088          $this->assertEquals(2, $result);
1089  
1090          $result = external::count_competencies(array(array('column' => 'competencyframeworkid', 'value' => $f1->get_id())));
1091          $result = external_api::clean_returnvalue(external::count_competencies_returns(), $result);
1092          $this->assertEquals(3, $result);
1093  
1094          $result = external::count_competencies(array(array('column' => 'shortname', 'value' => 'A')));
1095          $result = external_api::clean_returnvalue(external::count_competencies_returns(), $result);
1096          $this->assertEquals(1, $result);
1097      }
1098  
1099      /**
1100       * Test we can list and count competencies with manage permissions.
1101       */
1102      public function test_list_and_count_competencies_with_manage_permissions() {
1103          $this->setUser($this->creator);
1104          $framework = $this->create_competency_framework(1, true);
1105          $result = $this->create_competency(1, $framework->id);
1106          $result = $this->create_competency(2, $framework->id);
1107          $result = $this->create_competency(3, $framework->id);
1108  
1109          $result = external::count_competencies(array());
1110          $result = external_api::clean_returnvalue(external::count_competencies_returns(), $result);
1111  
1112          $this->assertEquals($result, 3);
1113  
1114          array('id' => $result = external::list_competencies(array(), 'shortname', 'ASC', 0, 10, context_system::instance()->id));
1115          $result = external_api::clean_returnvalue(external::list_competencies_returns(), $result);
1116  
1117          $this->assertEquals(count($result), 3);
1118          $result = (object) $result[0];
1119  
1120          $this->assertGreaterThan(0, $result->timecreated);
1121          $this->assertGreaterThan(0, $result->timemodified);
1122          $this->assertEquals($this->creator->id, $result->usermodified);
1123          $this->assertEquals('shortname1', $result->shortname);
1124          $this->assertEquals('idnumber1', $result->idnumber);
1125          $this->assertEquals('description1', $result->description);
1126      }
1127  
1128      /**
1129       * Test we can list and count competencies with read permissions.
1130       */
1131      public function test_list_and_count_competencies_with_read_permissions() {
1132          $this->setUser($this->creator);
1133          $framework = $this->create_competency_framework(1, true);
1134          $result = $this->create_competency(1, $framework->id);
1135          $result = $this->create_competency(2, $framework->id);
1136          $result = $this->create_competency(3, $framework->id);
1137  
1138          $this->setUser($this->user);
1139  
1140          $result = external::count_competencies(array());
1141          $result = external_api::clean_returnvalue(external::count_competencies_returns(), $result);
1142  
1143          $this->assertEquals($result, 3);
1144  
1145          array('id' => $result = external::list_competencies(array(), 'shortname', 'ASC', 0, 10, context_system::instance()->id));
1146          $result = external_api::clean_returnvalue(external::list_competencies_returns(), $result);
1147  
1148          $this->assertEquals(count($result), 3);
1149          $result = (object) $result[0];
1150  
1151          $this->assertGreaterThan(0, $result->timecreated);
1152          $this->assertGreaterThan(0, $result->timemodified);
1153          $this->assertEquals($this->creator->id, $result->usermodified);
1154          $this->assertEquals('shortname1', $result->shortname);
1155          $this->assertEquals('idnumber1', $result->idnumber);
1156          $this->assertEquals('description1', $result->description);
1157      }
1158  
1159      /**
1160       * Test we can search for competencies.
1161       */
1162      public function test_search_competencies_with_read_permissions() {
1163          $this->setUser($this->creator);
1164          $framework = $this->create_competency_framework(1, true);
1165          $result = $this->create_competency(1, $framework->id);
1166          $result = $this->create_competency(2, $framework->id);
1167          $result = $this->create_competency(3, $framework->id);
1168  
1169          $this->setUser($this->user);
1170  
1171          $result = external::search_competencies('short', $framework->id);
1172          $result = external_api::clean_returnvalue(external::search_competencies_returns(), $result);
1173  
1174          $this->assertEquals(count($result), 3);
1175          $result = (object) $result[0];
1176  
1177          $this->assertGreaterThan(0, $result->timecreated);
1178          $this->assertGreaterThan(0, $result->timemodified);
1179          $this->assertEquals($this->creator->id, $result->usermodified);
1180          $this->assertEquals('shortname1', $result->shortname);
1181          $this->assertEquals('idnumber1', $result->idnumber);
1182          $this->assertEquals('description1', $result->description);
1183      }
1184  
1185      /**
1186       * Test plans creation and updates.
1187       */
1188      public function test_create_and_update_plans() {
1189          $syscontext = context_system::instance();
1190  
1191          $this->setUser($this->creator);
1192          $plan0 = $this->create_plan(1, $this->creator->id, 0, plan::STATUS_ACTIVE, 0);
1193  
1194          $this->setUser($this->user);
1195  
1196          try {
1197              $plan1 = $this->create_plan(2, $this->user->id, 0, plan::STATUS_DRAFT, 0);
1198              $this->fail('Exception expected due to not permissions to create draft plans');
1199          } catch (moodle_exception $e) {
1200              $this->assertEquals('nopermissions', $e->errorcode);
1201          }
1202  
1203          assign_capability('moodle/competency:planmanageowndraft', CAP_ALLOW, $this->userrole, $syscontext->id);
1204          accesslib_clear_all_caches_for_unit_testing();
1205  
1206          $this->setUser($this->user);
1207  
1208          $plan2 = $this->create_plan(3, $this->user->id, 0, plan::STATUS_DRAFT, 0);
1209  
1210          try {
1211              $plan3 = $this->create_plan(4, $this->user->id, 0, plan::STATUS_ACTIVE, 0);
1212              $this->fail('Exception expected due to not permissions to create active plans');
1213          } catch (moodle_exception $e) {
1214              $this->assertEquals('nopermissions', $e->errorcode);
1215          }
1216          try {
1217              $plan3 = $this->update_plan($plan2->id, 4, $this->user->id, 0, plan::STATUS_COMPLETE, 0);
1218              $this->fail('We cannot complete a plan using api::update_plan().');
1219          } catch (coding_exception $e) {
1220              $this->assertTrue(true);
1221          }
1222  
1223          assign_capability('moodle/competency:planmanageown', CAP_ALLOW, $this->userrole, $syscontext->id);
1224          accesslib_clear_all_caches_for_unit_testing();
1225  
1226          $plan3 = $this->create_plan(4, $this->user->id, 0, plan::STATUS_ACTIVE, 0);
1227          try {
1228              $plan4 = $this->create_plan(6, $this->creator->id, 0, plan::STATUS_COMPLETE, 0);
1229              $this->fail('Plans cannot be created as complete.');
1230          } catch (coding_exception $e) {
1231              $this->assertRegexp('/A plan cannot be created as complete./', $e->getMessage());
1232          }
1233  
1234          try {
1235              $plan0 = $this->update_plan($plan0->id, 1, $this->user->id, 0, plan::STATUS_ACTIVE, 0);
1236          } catch (moodle_exception $e) {
1237              $this->assertEquals('nopermissions', $e->errorcode);
1238          }
1239  
1240          unassign_capability('moodle/competency:planmanageown', $this->userrole, $syscontext->id);
1241          unassign_capability('moodle/competency:planmanageowndraft', $this->userrole, $syscontext->id);
1242          accesslib_clear_all_caches_for_unit_testing();
1243  
1244          try {
1245              // Cannot be updated even if they created it.
1246              $this->update_plan($plan2->id, 1, $this->user->id, 0, plan::STATUS_ACTIVE, 0);
1247              $this->fail('The user can not update their own plan without permissions.');
1248          } catch (required_capability_exception $e) {
1249              $this->assertRegexp('/Manage learning plans./', $e->getMessage());
1250          }
1251      }
1252  
1253      /**
1254       * Test complete plan.
1255       */
1256      public function test_complete_plan() {
1257          $syscontext = context_system::instance();
1258  
1259          $this->setUser($this->creator);
1260  
1261          $this->setUser($this->user);
1262  
1263          assign_capability('moodle/competency:planmanageowndraft', CAP_ALLOW, $this->userrole, $syscontext->id);
1264          assign_capability('moodle/competency:planmanageown', CAP_ALLOW, $this->userrole, $syscontext->id);
1265          accesslib_clear_all_caches_for_unit_testing();
1266  
1267          $this->setUser($this->user);
1268  
1269          $plan = $this->create_plan(1, $this->user->id, 0, plan::STATUS_ACTIVE, 0);
1270  
1271          $result = external::complete_plan($plan->id);
1272          $this->assertTrue($result);
1273      }
1274  
1275      /**
1276       * Test reopen plan.
1277       */
1278      public function test_reopen_plan() {
1279          $syscontext = context_system::instance();
1280  
1281          $this->setUser($this->creator);
1282  
1283          $this->setUser($this->user);
1284  
1285          assign_capability('moodle/competency:planmanageowndraft', CAP_ALLOW, $this->userrole, $syscontext->id);
1286          assign_capability('moodle/competency:planmanageown', CAP_ALLOW, $this->userrole, $syscontext->id);
1287          accesslib_clear_all_caches_for_unit_testing();
1288  
1289          $this->setUser($this->user);
1290  
1291          $plan = $this->create_plan(1, $this->user->id, 0, plan::STATUS_ACTIVE, 0);
1292          external::complete_plan($plan->id);
1293  
1294          $result = external::reopen_plan($plan->id);
1295          $this->assertTrue($result);
1296      }
1297  
1298      /**
1299       * Test that we can read plans.
1300       */
1301      public function test_read_plans() {
1302          global $OUTPUT;
1303          $this->setUser($this->creator);
1304  
1305          $syscontext = context_system::instance();
1306  
1307          $plan1 = $this->create_plan(1, $this->user->id, 0, plan::STATUS_DRAFT, 0);
1308          $plan2 = $this->create_plan(2, $this->user->id, 0, plan::STATUS_ACTIVE, 0);
1309          $plan3 = $this->create_plan(3, $this->user->id, 0, plan::STATUS_ACTIVE, 0);
1310          external::complete_plan($plan3->id);
1311          $plan3 = (object) external::read_plan($plan3->id);
1312  
1313          $data = external::read_plan($plan1->id);
1314          $this->assertEquals((array)$plan1, external::read_plan($plan1->id));
1315          $data = external::read_plan($plan2->id);
1316          $this->assertEquals((array)$plan2, external::read_plan($plan2->id));
1317          $data = external::read_plan($plan3->id);
1318          $this->assertEquals((array)$plan3, external::read_plan($plan3->id));
1319  
1320          $this->setUser($this->user);
1321  
1322          // The normal user can not edit these plans.
1323          $plan1->canmanage = false;
1324          $plan2->canmanage = false;
1325          $plan3->canmanage = false;
1326          $plan1->canbeedited = false;
1327          $plan2->canbeedited = false;
1328          $plan3->canbeedited = false;
1329          $plan1->canrequestreview = true;
1330          $plan2->canrequestreview = true;
1331          $plan3->canrequestreview = true;
1332          $plan1->canreview = false;
1333          $plan2->canreview = false;
1334          $plan3->canreview = false;
1335          $plan1->iscompleteallowed = false;
1336          $plan2->iscompleteallowed = false;
1337          $plan3->iscompleteallowed = false;
1338          $plan1->isrequestreviewallowed = true;
1339          $plan2->isrequestreviewallowed = true;
1340          $plan3->isrequestreviewallowed = true;
1341          $plan1->isapproveallowed = false;
1342          $plan2->isapproveallowed = false;
1343          $plan3->isapproveallowed = false;
1344          $plan1->isunapproveallowed = false;
1345          $plan2->isunapproveallowed = false;
1346          $plan3->isunapproveallowed = false;
1347          $plan3->isreopenallowed = false;
1348          $plan1->commentarea['canpost'] = false;
1349          $plan1->commentarea['canview'] = true;
1350  
1351          // Prevent the user from seeing their own non-draft plans.
1352          assign_capability('moodle/competency:plancommentown', CAP_PROHIBIT, $this->userrole, $syscontext->id, true);
1353          assign_capability('moodle/competency:planviewown', CAP_PROHIBIT, $this->userrole, $syscontext->id, true);
1354          assign_capability('moodle/competency:planviewowndraft', CAP_ALLOW, $this->userrole, $syscontext->id, true);
1355          accesslib_clear_all_caches_for_unit_testing();
1356  
1357          $this->assertEquals((array)$plan1, external::read_plan($plan1->id));
1358  
1359          try {
1360              external::read_plan($plan2->id);
1361              $this->fail('Exception expected due to not permissions to read plan');
1362          } catch (moodle_exception $e) {
1363              $this->assertEquals('nopermissions', $e->errorcode);
1364          }
1365          try {
1366              external::read_plan($plan3->id);
1367              $this->fail('Exception expected due to not permissions to read plan');
1368          } catch (moodle_exception $e) {
1369              $this->assertEquals('nopermissions', $e->errorcode);
1370          }
1371  
1372          // Allow user to see their plan.
1373          assign_capability('moodle/competency:plancommentown', CAP_ALLOW, $this->userrole, $syscontext->id, true);
1374          assign_capability('moodle/competency:planviewown', CAP_ALLOW, $this->userrole, $syscontext->id, true);
1375          assign_capability('moodle/competency:planmanageowndraft', CAP_PROHIBIT, $this->userrole, $syscontext->id, true);
1376          accesslib_clear_all_caches_for_unit_testing();
1377  
1378          $plan1->commentarea['canpost'] = true;
1379          $plan1->commentarea['canview'] = true;
1380          $plan2->commentarea['canpost'] = true;
1381          $plan2->isrequestreviewallowed = false;
1382          $plan3->commentarea['canpost'] = true;
1383          $plan3->isrequestreviewallowed = false;
1384          $plan1->commentarea['canpostorhascomments'] = true;
1385          $plan2->commentarea['canpostorhascomments'] = true;
1386          $plan3->commentarea['canpostorhascomments'] = true;
1387  
1388          $this->assertEquals((array)$plan1, external::read_plan($plan1->id));
1389          $this->assertEquals((array)$plan2, external::read_plan($plan2->id));
1390          $this->assertEquals((array)$plan3, external::read_plan($plan3->id));
1391  
1392          // Allow use to manage their own draft plan.
1393          assign_capability('moodle/competency:planviewown', CAP_PROHIBIT, $this->userrole, $syscontext->id, true);
1394          assign_capability('moodle/competency:planmanageown', CAP_PROHIBIT, $this->userrole, $syscontext->id, true);
1395          assign_capability('moodle/competency:planmanageowndraft', CAP_ALLOW, $this->userrole, $syscontext->id, true);
1396          accesslib_clear_all_caches_for_unit_testing();
1397  
1398          $plan1->canmanage = true;
1399          $plan1->canbeedited = true;
1400          $plan1->canrequestreview = true;
1401          $plan1->isrequestreviewallowed = true;
1402          $this->assertEquals((array)$plan1, external::read_plan($plan1->id));
1403          try {
1404              external::read_plan($plan2->id);
1405              $this->fail('Exception expected due to not permissions to read plan');
1406          } catch (moodle_exception $e) {
1407              $this->assertEquals('nopermissions', $e->errorcode);
1408          }
1409          try {
1410              external::read_plan($plan3->id);
1411              $this->fail('Exception expected due to not permissions to read plan');
1412          } catch (moodle_exception $e) {
1413              $this->assertEquals('nopermissions', $e->errorcode);
1414          }
1415  
1416          // Allow use to manage their plan.
1417          assign_capability('moodle/competency:planviewown', CAP_PROHIBIT, $this->userrole, $syscontext->id, true);
1418          assign_capability('moodle/competency:planmanageowndraft', CAP_PROHIBIT, $this->userrole, $syscontext->id, true);
1419          assign_capability('moodle/competency:planmanageown', CAP_ALLOW, $this->userrole, $syscontext->id, true);
1420          accesslib_clear_all_caches_for_unit_testing();
1421  
1422          $plan1->canmanage = false;
1423          $plan1->canbeedited = false;
1424          $plan1->canrequestreview = true;
1425          $plan1->canreview = true;
1426          $plan1->isrequestreviewallowed = true;
1427          $plan1->isapproveallowed = true;
1428          $plan1->iscompleteallowed = false;
1429  
1430          $plan2->canmanage = true;
1431          $plan2->canbeedited = true;
1432          $plan2->canreview = true;
1433          $plan2->iscompleteallowed = true;
1434          $plan2->isunapproveallowed = true;
1435  
1436          $plan3->canmanage = true;
1437          $plan3->canreview = true;
1438          $plan3->isreopenallowed = true;
1439  
1440          $this->assertEquals((array)$plan1, external::read_plan($plan1->id));
1441          $this->assertEquals((array)$plan2, external::read_plan($plan2->id));
1442          $this->assertEquals((array)$plan3, external::read_plan($plan3->id));
1443      }
1444  
1445      public function test_delete_plans() {
1446          $this->setUser($this->creator);
1447  
1448          $syscontext = context_system::instance();
1449  
1450          $plan1 = $this->create_plan(1, $this->user->id, 0, plan::STATUS_ACTIVE, 0);
1451          $plan2 = $this->create_plan(2, $this->user->id, 0, plan::STATUS_ACTIVE, 0);
1452          $plan3 = $this->create_plan(3, $this->creator->id, 0, plan::STATUS_ACTIVE, 0);
1453  
1454          $this->assertTrue(external::delete_plan($plan1->id));
1455  
1456          unassign_capability('moodle/competency:planmanage', $this->creatorrole, $syscontext->id);
1457          accesslib_clear_all_caches_for_unit_testing();
1458  
1459          try {
1460              external::delete_plan($plan2->id);
1461              $this->fail('Exception expected due to not permissions to manage plans');
1462          } catch (moodle_exception $e) {
1463              $this->assertEquals('nopermissions', $e->errorcode);
1464          }
1465  
1466          $this->setUser($this->user);
1467  
1468          // Can not delete plans created by other users.
1469          try {
1470              external::delete_plan($plan2->id);
1471              $this->fail('Exception expected due to not permissions to manage plans');
1472          } catch (moodle_exception $e) {
1473              $this->assertEquals('nopermissions', $e->errorcode);
1474          }
1475  
1476          assign_capability('moodle/competency:planmanageown', CAP_ALLOW, $this->userrole, $syscontext->id);
1477          accesslib_clear_all_caches_for_unit_testing();
1478  
1479          $this->assertTrue(external::delete_plan($plan2->id));
1480  
1481          // Can not delete plans created for other users.
1482          try {
1483              external::delete_plan($plan3->id);
1484              $this->fail('Exception expected due to not permissions to manage plans');
1485          } catch (moodle_exception $e) {
1486              $this->assertEquals('nopermissions', $e->errorcode);
1487          }
1488  
1489          $plan4 = $this->create_plan(4, $this->user->id, 0, plan::STATUS_ACTIVE, 0);
1490          $this->assertTrue(external::delete_plan($plan4->id));
1491      }
1492  
1493      public function test_delete_plan_removes_relations() {
1494          $this->setAdminUser();
1495          $dg = $this->getDataGenerator();
1496          $lpg = $dg->get_plugin_generator('core_competency');
1497  
1498          $user = $dg->create_user();
1499          $plan = $lpg->create_plan(array('userid' => $user->id));
1500          $framework = $lpg->create_framework();
1501          $comp1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
1502          $comp2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
1503          $comp3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
1504          $pc1 = $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $comp1->get_id()));
1505          $pc2 = $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $comp2->get_id()));
1506          $pc3 = $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $comp3->get_id()));
1507  
1508          // Complete the plan to generate user_competency_plan entries.
1509          api::complete_plan($plan);
1510  
1511          // Confirm the data we have.
1512          $this->assertEquals(3, plan_competency::count_records(array('planid' => $plan->get_id())));
1513          $this->assertEquals(3, user_competency_plan::count_records(array('planid' => $plan->get_id(), 'userid' => $user->id)));
1514  
1515          // Delete the plan now.
1516          api::delete_plan($plan->get_id());
1517          $this->assertEquals(0, plan_competency::count_records(array('planid' => $plan->get_id())));
1518          $this->assertEquals(0, user_competency_plan::count_records(array('planid' => $plan->get_id(), 'userid' => $user->id)));
1519      }
1520  
1521      public function test_list_plan_competencies() {
1522          $this->setUser($this->creator);
1523  
1524          $dg = $this->getDataGenerator();
1525          $lpg = $dg->get_plugin_generator('core_competency');
1526  
1527          $f1 = $lpg->create_framework();
1528          $f2 = $lpg->create_framework();
1529  
1530          $c1a = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
1531          $c1b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
1532          $c1c = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
1533          $c2a = $lpg->create_competency(array('competencyframeworkid' => $f2->get_id()));
1534          $c2b = $lpg->create_competency(array('competencyframeworkid' => $f2->get_id()));
1535  
1536          $tpl = $lpg->create_template();
1537          $lpg->create_template_competency(array('templateid' => $tpl->get_id(), 'competencyid' => $c1a->get_id()));
1538          $lpg->create_template_competency(array('templateid' => $tpl->get_id(), 'competencyid' => $c1c->get_id()));
1539          $lpg->create_template_competency(array('templateid' => $tpl->get_id(), 'competencyid' => $c2b->get_id()));
1540  
1541          $plan = $lpg->create_plan(array('userid' => $this->user->id, 'templateid' => $tpl->get_id()));
1542  
1543          $uc1a = $lpg->create_user_competency(array('userid' => $this->user->id, 'competencyid' => $c1a->get_id(),
1544              'status' => user_competency::STATUS_IN_REVIEW, 'reviewerid' => $this->creator->id));
1545          $uc1b = $lpg->create_user_competency(array('userid' => $this->user->id, 'competencyid' => $c1b->get_id()));
1546          $uc2b = $lpg->create_user_competency(array('userid' => $this->user->id, 'competencyid' => $c2b->get_id(),
1547              'grade' => 2, 'proficiency' => 1));
1548          $ux1a = $lpg->create_user_competency(array('userid' => $this->creator->id, 'competencyid' => $c1a->get_id()));
1549  
1550          $result = external::list_plan_competencies($plan->get_id());
1551          $result = external::clean_returnvalue(external::list_plan_competencies_returns(), $result);
1552  
1553          $this->assertCount(3, $result);
1554          $this->assertEquals($c1a->get_id(), $result[0]['competency']['id']);
1555          $this->assertEquals($this->user->id, $result[0]['usercompetency']['userid']);
1556          $this->assertArrayNotHasKey('usercompetencyplan', $result[0]);
1557          $this->assertEquals($c1c->get_id(), $result[1]['competency']['id']);
1558          $this->assertEquals($this->user->id, $result[1]['usercompetency']['userid']);
1559          $this->assertArrayNotHasKey('usercompetencyplan', $result[1]);
1560          $this->assertEquals($c2b->get_id(), $result[2]['competency']['id']);
1561          $this->assertEquals($this->user->id, $result[2]['usercompetency']['userid']);
1562          $this->assertArrayNotHasKey('usercompetencyplan', $result[2]);
1563          $this->assertEquals(user_competency::STATUS_IN_REVIEW, $result[0]['usercompetency']['status']);
1564          $this->assertEquals(null, $result[1]['usercompetency']['grade']);
1565          $this->assertEquals(2, $result[2]['usercompetency']['grade']);
1566          $this->assertEquals(1, $result[2]['usercompetency']['proficiency']);
1567  
1568          // Check the return values when the plan status is complete.
1569          $completedplan = $lpg->create_plan(array('userid' => $this->user->id, 'templateid' => $tpl->get_id(),
1570                  'status' => plan::STATUS_COMPLETE));
1571  
1572          $uc1a = $lpg->create_user_competency_plan(array('userid' => $this->user->id, 'competencyid' => $c1a->get_id(),
1573                  'planid' => $completedplan->get_id()));
1574          $uc1b = $lpg->create_user_competency_plan(array('userid' => $this->user->id, 'competencyid' => $c1c->get_id(),
1575                  'planid' => $completedplan->get_id()));
1576          $uc2b = $lpg->create_user_competency_plan(array('userid' => $this->user->id, 'competencyid' => $c2b->get_id(),
1577                  'planid' => $completedplan->get_id(), 'grade' => 2, 'proficiency' => 1));
1578          $ux1a = $lpg->create_user_competency_plan(array('userid' => $this->creator->id, 'competencyid' => $c1a->get_id(),
1579                  'planid' => $completedplan->get_id()));
1580  
1581          $result = external::list_plan_competencies($completedplan->get_id());
1582          $result = external::clean_returnvalue(external::list_plan_competencies_returns(), $result);
1583  
1584          $this->assertCount(3, $result);
1585          $this->assertEquals($c1a->get_id(), $result[0]['competency']['id']);
1586          $this->assertEquals($this->user->id, $result[0]['usercompetencyplan']['userid']);
1587          $this->assertArrayNotHasKey('usercompetency', $result[0]);
1588          $this->assertEquals($c1c->get_id(), $result[1]['competency']['id']);
1589          $this->assertEquals($this->user->id, $result[1]['usercompetencyplan']['userid']);
1590          $this->assertArrayNotHasKey('usercompetency', $result[1]);
1591          $this->assertEquals($c2b->get_id(), $result[2]['competency']['id']);
1592          $this->assertEquals($this->user->id, $result[2]['usercompetencyplan']['userid']);
1593          $this->assertArrayNotHasKey('usercompetency', $result[2]);
1594          $this->assertEquals(null, $result[1]['usercompetencyplan']['grade']);
1595          $this->assertEquals(2, $result[2]['usercompetencyplan']['grade']);
1596          $this->assertEquals(1, $result[2]['usercompetencyplan']['proficiency']);
1597      }
1598  
1599      public function test_add_competency_to_template() {
1600          $this->setUser($this->creator);
1601  
1602          $syscontext = context_system::instance();
1603  
1604          // Create a template.
1605          $template = $this->create_template(1, true);
1606  
1607          // Create a competency.
1608          $framework = $this->create_competency_framework(1, true);
1609          $competency = $this->create_competency(1, $framework->id);
1610  
1611          // Add the competency.
1612          external::add_competency_to_template($template->id, $competency->id);
1613  
1614          // Check that it was added.
1615          $this->assertEquals(1, external::count_competencies_in_template($template->id));
1616  
1617          // Unassign capability.
1618          unassign_capability('moodle/competency:templatemanage', $this->creatorrole, $syscontext->id);
1619          accesslib_clear_all_caches_for_unit_testing();
1620  
1621          // Check we can not add the competency now.
1622          try {
1623              external::add_competency_to_template($template->id, $competency->id);
1624              $this->fail('Exception expected due to not permissions to manage template competencies');
1625          } catch (moodle_exception $e) {
1626              $this->assertEquals('nopermissions', $e->errorcode);
1627          }
1628      }
1629  
1630      public function test_remove_competency_from_template() {
1631          $syscontext = context_system::instance();
1632          $this->setUser($this->creator);
1633          $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1634  
1635          // Create a template.
1636          $template = $this->create_template(1, true);
1637  
1638          // Create a competency.
1639          $framework = $lpg->create_framework();
1640          $competency = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
1641  
1642          // Add the competency.
1643          external::add_competency_to_template($template->id, $competency->get_id());
1644  
1645          // Check that it was added.
1646          $this->assertEquals(1, external::count_competencies_in_template($template->id));
1647  
1648          // Check that we can remove the competency.
1649          external::remove_competency_from_template($template->id, $competency->get_id());
1650  
1651          // Check that it was removed.
1652          $this->assertEquals(0, external::count_competencies_in_template($template->id));
1653  
1654          // Unassign capability.
1655          unassign_capability('moodle/competency:templatemanage', $this->creatorrole, $syscontext->id);
1656          accesslib_clear_all_caches_for_unit_testing();
1657  
1658          // Check we can not remove the competency now.
1659          try {
1660              external::add_competency_to_template($template->id, $competency->get_id());
1661              $this->fail('Exception expected due to not permissions to manage template competencies');
1662          } catch (moodle_exception $e) {
1663              $this->assertEquals('nopermissions', $e->errorcode);
1664          }
1665      }
1666  
1667      /**
1668       * Test we can re-order competency frameworks.
1669       */
1670      public function test_reorder_template_competencies() {
1671          $this->setUser($this->creator);
1672  
1673          $syscontext = context_system::instance();
1674          $onehour = time() + 60 * 60;
1675  
1676          // Create a template.
1677          $template = $this->create_template(1, true);
1678  
1679          // Create a competency framework.
1680          $framework = $this->create_competency_framework(1, true);
1681  
1682          // Create multiple competencies.
1683          $competency1 = $this->create_competency(1, $framework->id);
1684          $competency2 = $this->create_competency(2, $framework->id);
1685          $competency3 = $this->create_competency(3, $framework->id);
1686          $competency4 = $this->create_competency(4, $framework->id);
1687  
1688          // Add the competencies.
1689          external::add_competency_to_template($template->id, $competency1->id);
1690          external::add_competency_to_template($template->id, $competency2->id);
1691          external::add_competency_to_template($template->id, $competency3->id);
1692          external::add_competency_to_template($template->id, $competency4->id);
1693  
1694          // Test if removing competency from template don't create sortorder holes.
1695          external::remove_competency_from_template($template->id, $competency3->id);
1696          $templcomp4 = template_competency::get_record(array(
1697              'templateid' => $template->id,
1698              'competencyid' => $competency4->id
1699          ));
1700  
1701          $this->assertEquals(2, $templcomp4->get_sortorder());
1702  
1703          // This is a move up.
1704          external::reorder_template_competency($template->id, $competency4->id, $competency2->id);
1705          $result = external::list_competencies_in_template($template->id);
1706          $result = external_api::clean_returnvalue(external::list_competencies_in_template_returns(), $result);
1707  
1708          $r1 = (object) $result[0];
1709          $r2 = (object) $result[1];
1710          $r3 = (object) $result[2];
1711  
1712          $this->assertEquals($competency1->id, $r1->id);
1713          $this->assertEquals($competency4->id, $r2->id);
1714          $this->assertEquals($competency2->id, $r3->id);
1715  
1716          // This is a move down.
1717          external::reorder_template_competency($template->id, $competency1->id, $competency4->id);
1718          $result = external::list_competencies_in_template($template->id);
1719          $result = external_api::clean_returnvalue(external::list_competencies_in_template_returns(), $result);
1720  
1721          $r1 = (object) $result[0];
1722          $r2 = (object) $result[1];
1723          $r3 = (object) $result[2];
1724  
1725          $this->assertEquals($competency4->id, $r1->id);
1726          $this->assertEquals($competency1->id, $r2->id);
1727          $this->assertEquals($competency2->id, $r3->id);
1728  
1729          $this->expectException('required_capability_exception');
1730          $this->setUser($this->user);
1731          external::reorder_template_competency($template->id, $competency1->id, $competency2->id);
1732      }
1733  
1734      /**
1735       * Test we can duplicate learning plan template.
1736       */
1737      public function test_duplicate_learning_plan_template() {
1738          $this->setUser($this->creator);
1739  
1740          $syscontext = context_system::instance();
1741          $onehour = time() + 60 * 60;
1742  
1743          // Create a template.
1744          $template = $this->create_template(1, true);
1745  
1746          // Create a competency framework.
1747          $framework = $this->create_competency_framework(1, true);
1748  
1749          // Create multiple competencies.
1750          $competency1 = $this->create_competency(1, $framework->id);
1751          $competency2 = $this->create_competency(2, $framework->id);
1752          $competency3 = $this->create_competency(3, $framework->id);
1753  
1754          // Add the competencies.
1755          external::add_competency_to_template($template->id, $competency1->id);
1756          external::add_competency_to_template($template->id, $competency2->id);
1757          external::add_competency_to_template($template->id, $competency3->id);
1758  
1759          // Duplicate the learning plan template.
1760          $duplicatedtemplate = external::duplicate_template($template->id);
1761  
1762          $result = external::list_competencies_in_template($template->id);
1763          $resultduplicated = external::list_competencies_in_template($duplicatedtemplate->id);
1764  
1765          $this->assertEquals(count($result), count($resultduplicated));
1766          $this->assertContains($template->shortname, $duplicatedtemplate->shortname);
1767          $this->assertEquals($duplicatedtemplate->description, $template->description);
1768          $this->assertEquals($duplicatedtemplate->descriptionformat, $template->descriptionformat);
1769          $this->assertEquals($duplicatedtemplate->visible, $template->visible);
1770      }
1771  
1772      /**
1773       * Test that we can return scale values for a scale with the scale ID.
1774       */
1775      public function test_get_scale_values() {
1776          global $DB;
1777  
1778          $this->setUser($this->creator);
1779  
1780          // Create a scale.
1781          $record = new stdClass();
1782          $record->courseid = 0;
1783          $record->userid = $this->creator->id;
1784          $record->name = 'Test scale';
1785          $record->scale = 'Poor, Not good, Okay, Fine, Excellent';
1786          $record->description = '<p>Test scale description.</p>';
1787          $record->descriptionformat = 1;
1788          $record->timemodified = time();
1789          $scaleid = $DB->insert_record('scale', $record);
1790          // Expected return value.
1791          $expected = array(array(
1792                  'id' => 1,
1793                  'name' => 'Poor'
1794              ), array(
1795                  'id' => 2,
1796                  'name' => 'Not good'
1797              ), array(
1798                  'id' => 3,
1799                  'name' => 'Okay'
1800              ), array(
1801                  'id' => 4,
1802                  'name' => 'Fine'
1803              ), array(
1804                  'id' => 5,
1805                  'name' => 'Excellent'
1806              )
1807          );
1808          // Call the webservice.
1809          $result = external::get_scale_values($scaleid);
1810          $this->assertEquals($expected, $result);
1811      }
1812  
1813      /**
1814       * Create a template.
1815       */
1816      public function test_create_template() {
1817          $syscontextid = context_system::instance()->id;
1818          $catcontextid = context_coursecat::instance($this->category->id)->id;
1819  
1820          // A user without permission.
1821          $this->setUser($this->user);
1822          try {
1823              $result = $this->create_template(1, true);
1824              $this->fail('Invalid permissions');
1825          } catch (required_capability_exception $e) {
1826              // All good.
1827          }
1828  
1829          // A user without permission in a category.
1830          $this->setUser($this->catuser);
1831          try {
1832              $result = $this->create_template(1, false);
1833              $this->fail('Invalid permissions');
1834          } catch (required_capability_exception $e) {
1835              // All good.
1836          }
1837  
1838          // A user with permissions in the system.
1839          $this->setUser($this->creator);
1840          $result = $this->create_template(1, true);
1841          $this->assertEquals('shortname1', $result->shortname);
1842          $this->assertEquals($syscontextid, $result->contextid);
1843          $this->assertNotEmpty($result->id);
1844  
1845          $result = $this->create_template(2, false);
1846          $this->assertEquals('shortname2', $result->shortname);
1847          $this->assertEquals($catcontextid, $result->contextid);
1848          $this->assertNotEmpty($result->id);
1849  
1850          // A user with permissions in the category.
1851          $this->setUser($this->catcreator);
1852          try {
1853              $result = $this->create_template(3, true);
1854              $this->fail('Invalid permissions');
1855          } catch (required_capability_exception $e) {
1856              // All good.
1857          }
1858  
1859          $result = $this->create_template(3, false);
1860          $this->assertEquals('shortname3', $result->shortname);
1861          $this->assertEquals($catcontextid, $result->contextid);
1862          $this->assertNotEmpty($result->id);
1863      }
1864  
1865      /**
1866       * Read a template.
1867       */
1868      public function test_read_template() {
1869          $syscontextid = context_system::instance()->id;
1870          $catcontextid = context_coursecat::instance($this->category->id)->id;
1871  
1872          // Set a due date for the next year.
1873          $date = new DateTime('now');
1874          $date->modify('+1 year');
1875          $duedate = $date->getTimestamp();
1876  
1877          // Creating two templates.
1878          $this->setUser($this->creator);
1879          $systemplate = $this->create_template(1, true);
1880          $cattemplate = $this->create_template(2, false);
1881  
1882          // User without permissions to read in system.
1883          assign_capability('moodle/competency:templateview', CAP_PROHIBIT, $this->userrole, $syscontextid, true);
1884          accesslib_clear_all_caches_for_unit_testing();
1885          $this->setUser($this->user);
1886          $this->assertFalse(has_capability('moodle/competency:templateview', context_system::instance()));
1887          try {
1888              external::read_template($systemplate->id);
1889              $this->fail('Invalid permissions');
1890          } catch (required_capability_exception $e) {
1891              // All good.
1892          }
1893          try {
1894              external::read_template($cattemplate->id);
1895              $this->fail('Invalid permissions');
1896          } catch (required_capability_exception $e) {
1897              // All good.
1898          }
1899  
1900          // User with permissions to read in a category.
1901          assign_capability('moodle/competency:templateview', CAP_PREVENT, $this->userrole, $syscontextid, true);
1902          assign_capability('moodle/competency:templateview', CAP_ALLOW, $this->userrole, $catcontextid, true);
1903          accesslib_clear_all_caches_for_unit_testing();
1904          $this->assertFalse(has_capability('moodle/competency:templateview', context_system::instance()));
1905          $this->assertTrue(has_capability('moodle/competency:templateview', context_coursecat::instance($this->category->id)));
1906          try {
1907              external::read_template($systemplate->id);
1908              $this->fail('Invalid permissions');
1909          } catch (required_capability_exception $e) {
1910              // All good.
1911          }
1912  
1913          $result = external::read_template($cattemplate->id);
1914          $result = external_api::clean_returnvalue(external::read_template_returns(), $result);
1915          $this->assertEquals($cattemplate->id, $result['id']);
1916          $this->assertEquals('shortname2', $result['shortname']);
1917          $this->assertEquals('description2', $result['description']);
1918          $this->assertEquals(FORMAT_HTML, $result['descriptionformat']);
1919          $this->assertEquals(1, $result['visible']);
1920          $this->assertEquals(0, $result['duedate']);
1921          $this->assertEquals(userdate(0), $result['duedateformatted']);
1922  
1923          // User with permissions to read in the system.
1924          assign_capability('moodle/competency:templateview', CAP_ALLOW, $this->userrole, $syscontextid, true);
1925          accesslib_clear_all_caches_for_unit_testing();
1926          $this->assertTrue(has_capability('moodle/competency:templateview', context_system::instance()));
1927          $result = external::read_template($systemplate->id);
1928          $result = external_api::clean_returnvalue(external::read_template_returns(), $result);
1929          $this->assertEquals($systemplate->id, $result['id']);
1930          $this->assertEquals('shortname1', $result['shortname']);
1931          $this->assertEquals('description1', $result['description']);
1932          $this->assertEquals(FORMAT_HTML, $result['descriptionformat']);
1933          $this->assertEquals(true, $result['visible']);
1934          $this->assertEquals(0, $result['duedate']);
1935          $this->assertEquals(userdate(0), $result['duedateformatted']);
1936  
1937          $result = external::read_template($cattemplate->id);
1938          $result = external_api::clean_returnvalue(external::read_template_returns(), $result);
1939          $this->assertEquals($cattemplate->id, $result['id']);
1940          $this->assertEquals('shortname2', $result['shortname']);
1941          $this->assertEquals('description2', $result['description']);
1942          $this->assertEquals(FORMAT_HTML, $result['descriptionformat']);
1943          $this->assertEquals(true, $result['visible']);
1944          $this->assertEquals(0, $result['duedate']);
1945          $this->assertEquals(userdate(0), $result['duedateformatted']);
1946      }
1947  
1948      /**
1949       * Update a template.
1950       */
1951      public function test_update_template() {
1952          $syscontextid = context_system::instance()->id;
1953          $catcontextid = context_coursecat::instance($this->category->id)->id;
1954  
1955          // Set a due date for the next year.
1956          $date = new DateTime('now');
1957          $date->modify('+1 year');
1958          $duedate = $date->getTimestamp();
1959  
1960          // Creating two templates.
1961          $this->setUser($this->creator);
1962          $systemplate = $this->create_template(1, true);
1963          $cattemplate = $this->create_template(2, false);
1964  
1965          // Trying to update in a without permissions.
1966          $this->setUser($this->user);
1967          try {
1968              $this->update_template($systemplate->id, 3);
1969              $this->fail('Invalid permissions');
1970          } catch (required_capability_exception $e) {
1971              // All good.
1972          }
1973  
1974          try {
1975              $this->update_template($cattemplate->id, 3);
1976              $this->fail('Invalid permissions');
1977          } catch (required_capability_exception $e) {
1978              // All good.
1979          }
1980  
1981          // User with permissions to update in category.
1982          $this->setUser($this->catcreator);
1983          try {
1984              $this->update_template($systemplate->id, 3);
1985              $this->fail('Invalid permissions');
1986          } catch (required_capability_exception $e) {
1987              // All good.
1988          }
1989  
1990          $result = $this->update_template($cattemplate->id, 3);
1991          $this->assertTrue($result);
1992          $result = external::read_template($cattemplate->id);
1993          $result = external_api::clean_returnvalue(external::read_template_returns(), $result);
1994          $this->assertEquals($cattemplate->id, $result['id']);
1995          $this->assertEquals('shortname3', $result['shortname']);
1996          $this->assertEquals("description3", $result['description']);
1997          $this->assertEquals(FORMAT_HTML, $result['descriptionformat']);
1998          $this->assertEquals(true, $result['visible']);
1999          $this->assertEquals(0, $result['duedate']);
2000          $this->assertEquals(userdate(0), $result['duedateformatted']);
2001  
2002          // User with permissions to update in the system.
2003          $this->setUser($this->creator);
2004          $result = $this->update_template($systemplate->id, 4);
2005          $this->assertTrue($result);
2006          $result = external::read_template($systemplate->id);
2007          $result = external_api::clean_returnvalue(external::read_template_returns(), $result);
2008          $this->assertEquals($systemplate->id, $result['id']);
2009          $this->assertEquals('shortname4', $result['shortname']);
2010          $this->assertEquals('description4', $result['description']);
2011          $this->assertEquals(FORMAT_HTML, $result['descriptionformat']);
2012          $this->assertEquals(true, $result['visible']);
2013          $this->assertEquals(0, $result['duedate']);
2014          $this->assertEquals(userdate(0), $result['duedateformatted']);
2015  
2016          $result = $this->update_template($cattemplate->id, 5);
2017          $this->assertTrue($result);
2018          $result = external::read_template($cattemplate->id);
2019          $result = external_api::clean_returnvalue(external::read_template_returns(), $result);
2020          $this->assertEquals($cattemplate->id, $result['id']);
2021          $this->assertEquals('shortname5', $result['shortname']);
2022          $this->assertEquals('description5', $result['description']);
2023          $this->assertEquals(FORMAT_HTML, $result['descriptionformat']);
2024          $this->assertEquals(1, $result['visible']);
2025          $this->assertEquals(0, $result['duedate']);
2026          $this->assertEquals(userdate(0), $result['duedateformatted']);
2027      }
2028  
2029      /**
2030       * Delete a template.
2031       */
2032      public function test_delete_template() {
2033          global $DB;
2034          $syscontextid = context_system::instance()->id;
2035          $catcontextid = context_coursecat::instance($this->category->id)->id;
2036  
2037          // Creating a few templates.
2038          $this->setUser($this->creator);
2039          $sys1 = $this->create_template(1, true);
2040          $cat1 = $this->create_template(2, false);
2041          $cat2 = $this->create_template(3, false);
2042          $this->assertTrue($DB->record_exists(template::TABLE, array('id' => $sys1->id)));
2043          $this->assertTrue($DB->record_exists(template::TABLE, array('id' => $cat1->id)));
2044          $this->assertTrue($DB->record_exists(template::TABLE, array('id' => $cat2->id)));
2045  
2046          // User without permissions.
2047          $this->setUser($this->user);
2048          try {
2049              external::delete_template($sys1->id);
2050              $this->fail('Invalid permissions');
2051          } catch (required_capability_exception $e) {
2052              // All good.
2053          }
2054          try {
2055              external::delete_template($cat1->id);
2056              $this->fail('Invalid permissions');
2057          } catch (required_capability_exception $e) {
2058              // All good.
2059          }
2060  
2061          // User with category permissions.
2062          $this->setUser($this->catcreator);
2063          try {
2064              external::delete_template($sys1->id);
2065              $this->fail('Invalid permissions');
2066          } catch (required_capability_exception $e) {
2067              // All good.
2068          }
2069  
2070          $result = external::delete_template($cat1->id);
2071          $result = external_api::clean_returnvalue(external::delete_template_returns(), $result);
2072          $this->assertTrue($result);
2073          $this->assertFalse($DB->record_exists(template::TABLE, array('id' => $cat1->id)));
2074  
2075          // User with system permissions.
2076          $this->setUser($this->creator);
2077          $result = external::delete_template($sys1->id);
2078          $result = external_api::clean_returnvalue(external::delete_template_returns(), $result);
2079          $this->assertTrue($result);
2080          $result = external::delete_template($cat2->id);
2081          $result = external_api::clean_returnvalue(external::delete_template_returns(), $result);
2082          $this->assertTrue($result);
2083          $this->assertFalse($DB->record_exists(template::TABLE, array('id' => $sys1->id)));
2084          $this->assertFalse($DB->record_exists(template::TABLE, array('id' => $cat2->id)));
2085      }
2086  
2087      /**
2088       * List templates.
2089       */
2090      public function test_list_templates() {
2091          $syscontextid = context_system::instance()->id;
2092          $catcontextid = context_coursecat::instance($this->category->id)->id;
2093  
2094          // Creating a few templates.
2095          $this->setUser($this->creator);
2096          $sys1 = $this->create_template(1, true);
2097          $sys2 = $this->create_template(2, true);
2098          $cat1 = $this->create_template(3, false);
2099          $cat2 = $this->create_template(4, false);
2100  
2101          // User without permission.
2102          $this->setUser($this->user);
2103          assign_capability('moodle/competency:templateview', CAP_PROHIBIT, $this->userrole, $syscontextid, true);
2104          accesslib_clear_all_caches_for_unit_testing();
2105          try {
2106              external::list_templates('id', 'ASC', 0, 10, array('contextid' => $syscontextid), 'children', false);
2107              $this->fail('Invalid permissions');
2108          } catch (required_capability_exception $e) {
2109              // All good.
2110          }
2111  
2112          // User with category permissions.
2113          assign_capability('moodle/competency:templateview', CAP_PREVENT, $this->userrole, $syscontextid, true);
2114          assign_capability('moodle/competency:templateview', CAP_ALLOW, $this->userrole, $catcontextid, true);
2115          accesslib_clear_all_caches_for_unit_testing();
2116          $result = external::list_templates('id', 'ASC', 0, 10, array('contextid' => $syscontextid), 'children', false);
2117          $result = external_api::clean_returnvalue(external::list_templates_returns(), $result);
2118          $this->assertCount(2, $result);
2119          $this->assertEquals($cat1->id, $result[0]['id']);
2120          $this->assertEquals($cat2->id, $result[1]['id']);
2121  
2122          // User with system permissions.
2123          assign_capability('moodle/competency:templateview', CAP_ALLOW, $this->userrole, $syscontextid, true);
2124          accesslib_clear_all_caches_for_unit_testing();
2125          $result = external::list_templates('id', 'DESC', 0, 3, array('contextid' => $catcontextid), 'parents', false);
2126          $result = external_api::clean_returnvalue(external::list_templates_returns(), $result);
2127          $this->assertCount(3, $result);
2128          $this->assertEquals($cat2->id, $result[0]['id']);
2129          $this->assertEquals($cat1->id, $result[1]['id']);
2130          $this->assertEquals($sys2->id, $result[2]['id']);
2131      }
2132  
2133      /**
2134       * List templates using competency.
2135       */
2136      public function test_list_templates_using_competency() {
2137          $this->setUser($this->creator);
2138  
2139          // Create a template.
2140          $template1 = $this->create_template(1, true);
2141          $template2 = $this->create_template(2, true);
2142          $template3 = $this->create_template(3, true);
2143          $template4 = $this->create_template(4, true);
2144  
2145          // Create a competency.
2146          $framework = $this->create_competency_framework(1, true);
2147          $competency1 = $this->create_competency(1, $framework->id);
2148          $competency2 = $this->create_competency(2, $framework->id);
2149  
2150          // Add the competency.
2151          external::add_competency_to_template($template1->id, $competency1->id);
2152          external::add_competency_to_template($template2->id, $competency1->id);
2153          external::add_competency_to_template($template3->id, $competency1->id);
2154  
2155          external::add_competency_to_template($template4->id, $competency2->id);
2156  
2157          $listcomp1 = external::list_templates_using_competency($competency1->id);
2158          $listcomp2 = external::list_templates_using_competency($competency2->id);
2159  
2160          // Test count_templates_using_competency.
2161          $counttempcomp1 = external::count_templates_using_competency($competency1->id);
2162          $counttempcomp2 = external::count_templates_using_competency($competency2->id);
2163  
2164          $comptemp1 = $listcomp1[0];
2165          $comptemp2 = $listcomp1[1];
2166          $comptemp3 = $listcomp1[2];
2167  
2168          $comptemp4 = $listcomp2[0];
2169  
2170          $this->assertCount(3, $listcomp1);
2171          $this->assertCount(1, $listcomp2);
2172          $this->assertEquals(3, $counttempcomp1);
2173          $this->assertEquals(1, $counttempcomp2);
2174          $this->assertEquals($template1->id, $comptemp1->id);
2175          $this->assertEquals($template2->id, $comptemp2->id);
2176          $this->assertEquals($template3->id, $comptemp3->id);
2177          $this->assertEquals($template4->id, $comptemp4->id);
2178      }
2179  
2180      public function test_count_templates() {
2181          $syscontextid = context_system::instance()->id;
2182          $catcontextid = context_coursecat::instance($this->category->id)->id;
2183  
2184          // Creating a few templates.
2185          $this->setUser($this->creator);
2186          $sys1 = $this->create_template(1, true);
2187          $sys2 = $this->create_template(2, true);
2188          $cat1 = $this->create_template(3, false);
2189          $cat2 = $this->create_template(4, false);
2190          $cat3 = $this->create_template(5, false);
2191  
2192          // User without permission.
2193          $this->setUser($this->user);
2194          assign_capability('moodle/competency:templateview', CAP_PROHIBIT, $this->userrole, $syscontextid, true);
2195          accesslib_clear_all_caches_for_unit_testing();
2196          try {
2197              external::count_templates(array('contextid' => $syscontextid), 'children');
2198              $this->fail('Invalid permissions');
2199          } catch (required_capability_exception $e) {
2200              // All good.
2201          }
2202  
2203          // User with category permissions.
2204          assign_capability('moodle/competency:templateview', CAP_PREVENT, $this->userrole, $syscontextid, true);
2205          assign_capability('moodle/competency:templateview', CAP_ALLOW, $this->userrole, $catcontextid, true);
2206          accesslib_clear_all_caches_for_unit_testing();
2207          $result = external::count_templates(array('contextid' => $syscontextid), 'children');
2208          $result = external_api::clean_returnvalue(external::count_templates_returns(), $result);
2209          $this->assertEquals(3, $result);
2210  
2211          // User with system permissions.
2212          assign_capability('moodle/competency:templateview', CAP_ALLOW, $this->userrole, $syscontextid, true);
2213          accesslib_clear_all_caches_for_unit_testing();
2214          $result = external::count_templates(array('contextid' => $catcontextid), 'parents');
2215          $result = external_api::clean_returnvalue(external::count_templates_returns(), $result);
2216          $this->assertEquals(5, $result);
2217      }
2218  
2219      /**
2220       * Test that we can add related competencies.
2221       *
2222       * @return void
2223       */
2224      public function test_add_related_competency() {
2225          global $DB;
2226          $this->setUser($this->creator);
2227  
2228          $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
2229          $framework = $lpg->create_framework();
2230          $framework2 = $lpg->create_framework();
2231          $competency1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
2232          $competency2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
2233          $competency3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
2234          $competency4 = $lpg->create_competency(array('competencyframeworkid' => $framework2->get_id()));
2235  
2236          // The lower one always as competencyid.
2237          $result = external::add_related_competency($competency1->get_id(), $competency2->get_id());
2238          $result = external_api::clean_returnvalue(external::add_related_competency_returns(), $result);
2239          $this->assertTrue($result);
2240          $this->assertTrue($DB->record_exists_select(
2241              related_competency::TABLE, 'competencyid = :cid AND relatedcompetencyid = :rid',
2242              array(
2243                  'cid' => $competency1->get_id(),
2244                  'rid' => $competency2->get_id()
2245              )
2246          ));
2247          $this->assertFalse($DB->record_exists_select(
2248              related_competency::TABLE, 'competencyid = :cid AND relatedcompetencyid = :rid',
2249              array(
2250                  'cid' => $competency2->get_id(),
2251                  'rid' => $competency1->get_id()
2252              )
2253          ));
2254  
2255          $result = external::add_related_competency($competency3->get_id(), $competency1->get_id());
2256          $result = external_api::clean_returnvalue(external::add_related_competency_returns(), $result);
2257          $this->assertTrue($result);
2258          $this->assertTrue($DB->record_exists_select(
2259              related_competency::TABLE, 'competencyid = :cid AND relatedcompetencyid = :rid',
2260              array(
2261                  'cid' => $competency1->get_id(),
2262                  'rid' => $competency3->get_id()
2263              )
2264          ));
2265          $this->assertFalse($DB->record_exists_select(
2266              related_competency::TABLE, 'competencyid = :cid AND relatedcompetencyid = :rid',
2267              array(
2268                  'cid' => $competency3->get_id(),
2269                  'rid' => $competency1->get_id()
2270              )
2271          ));
2272  
2273          // We can not allow a duplicate relation, not even in the other direction.
2274          $this->assertEquals(1, $DB->count_records_select(related_competency::TABLE,
2275              'competencyid = :cid AND relatedcompetencyid = :rid',
2276              array('cid' => $competency1->get_id(), 'rid' => $competency2->get_id())));
2277          $this->assertEquals(0, $DB->count_records_select(related_competency::TABLE,
2278              'competencyid = :cid AND relatedcompetencyid = :rid',
2279              array('rid' => $competency1->get_id(), 'cid' => $competency2->get_id())));
2280          $result = external::add_related_competency($competency2->get_id(), $competency1->get_id());
2281          $result = external_api::clean_returnvalue(external::add_related_competency_returns(), $result);
2282          $this->assertTrue($result);
2283          $this->assertEquals(1, $DB->count_records_select(related_competency::TABLE,
2284              'competencyid = :cid AND relatedcompetencyid = :rid',
2285              array('cid' => $competency1->get_id(), 'rid' => $competency2->get_id())));
2286          $this->assertEquals(0, $DB->count_records_select(related_competency::TABLE,
2287              'competencyid = :cid AND relatedcompetencyid = :rid',
2288              array('rid' => $competency1->get_id(), 'cid' => $competency2->get_id())));
2289  
2290          // Check that we cannot create links across frameworks.
2291          try {
2292              external::add_related_competency($competency1->get_id(), $competency4->get_id());
2293              $this->fail('Exception expected due mis-use of shared competencies');
2294          } catch (invalid_persistent_exception $e) {
2295              // Yay!
2296          }
2297  
2298          // User without permission.
2299          $this->setUser($this->user);
2300  
2301          // Check we can not add the related competency now.
2302          try {
2303              external::add_related_competency($competency1->get_id(), $competency3->get_id());
2304              $this->fail('Exception expected due to not permissions to manage template competencies');
2305          } catch (moodle_exception $e) {
2306              $this->assertEquals('nopermissions', $e->errorcode);
2307          }
2308  
2309      }
2310  
2311      /**
2312       * Test that we can remove related competencies.
2313       *
2314       * @return void
2315       */
2316      public function test_remove_related_competency() {
2317          $this->setUser($this->creator);
2318  
2319          $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
2320          $framework = $lpg->create_framework();
2321          $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
2322          $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
2323          $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
2324          $rc1 = $lpg->create_related_competency(array('competencyid' => $c1->get_id(), 'relatedcompetencyid' => $c2->get_id()));
2325          $rc2 = $lpg->create_related_competency(array('competencyid' => $c2->get_id(), 'relatedcompetencyid' => $c3->get_id()));
2326  
2327          $this->assertEquals(2, related_competency::count_records());
2328  
2329          // Returns false when the relation does not exist.
2330          $result = external::remove_related_competency($c1->get_id(), $c3->get_id());
2331          $result = external_api::clean_returnvalue(external::remove_related_competency_returns(), $result);
2332          $this->assertFalse($result);
2333  
2334          // Returns true on success.
2335          $result = external::remove_related_competency($c2->get_id(), $c3->get_id());
2336          $result = external_api::clean_returnvalue(external::remove_related_competency_returns(), $result);
2337          $this->assertTrue($result);
2338          $this->assertEquals(1, related_competency::count_records());
2339  
2340          // We don't need to specify competencyid and relatedcompetencyid in the right order.
2341          $result = external::remove_related_competency($c2->get_id(), $c1->get_id());
2342          $result = external_api::clean_returnvalue(external::remove_related_competency_returns(), $result);
2343          $this->assertTrue($result);
2344          $this->assertEquals(0, related_competency::count_records());
2345      }
2346  
2347      /**
2348       * Test that we can search and include related competencies.
2349       *
2350       * @return void
2351       */
2352      public function test_search_competencies_including_related() {
2353          $this->setUser($this->creator);
2354  
2355          $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
2356          $framework = $lpg->create_framework();
2357          $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
2358          $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
2359          $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
2360          $c4 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
2361          $c5 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
2362  
2363          // We have 1-2, 1-3, 2-4, and no relation between 2-3 nor 1-4 nor 5.
2364          $rc12 = $lpg->create_related_competency(array('competencyid' => $c1->get_id(), 'relatedcompetencyid' => $c2->get_id()));
2365          $rc13 = $lpg->create_related_competency(array('competencyid' => $c1->get_id(), 'relatedcompetencyid' => $c3->get_id()));
2366          $rc24 = $lpg->create_related_competency(array('competencyid' => $c2->get_id(), 'relatedcompetencyid' => $c4->get_id()));
2367  
2368          $result = external::search_competencies('comp', $framework->get_id(), true);
2369          $result = external_api::clean_returnvalue(external::search_competencies_returns(), $result);
2370  
2371          $this->assertCount(5, $result);
2372  
2373      }
2374  
2375      /**
2376       * Test that we can add competency to plan if we have the right capability.
2377       *
2378       * @return void
2379       */
2380      public function test_add_competency_to_plan() {
2381          $this->resetAfterTest(true);
2382          $dg = $this->getDataGenerator();
2383          $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
2384          $usermanage = $dg->create_user();
2385          $user = $dg->create_user();
2386  
2387          $syscontext = context_system::instance();
2388  
2389          // Creating specific roles.
2390          $managerole = $dg->create_role(array(
2391              'name' => 'User manage',
2392              'shortname' => 'manage'
2393          ));
2394  
2395          assign_capability('moodle/competency:planmanage', CAP_ALLOW, $managerole, $syscontext->id);
2396          assign_capability('moodle/competency:planview', CAP_ALLOW, $managerole, $syscontext->id);
2397  
2398          $dg->role_assign($managerole, $usermanage->id, $syscontext->id);
2399  
2400          $this->setUser($usermanage);
2401          $plan = array (
2402              'userid' => $usermanage->id,
2403              'status' => \core_competency\plan::STATUS_ACTIVE
2404          );
2405          $pl1 = $lpg->create_plan($plan);
2406          $framework = $lpg->create_framework();
2407          $competency = $lpg->create_competency(
2408                  array('competencyframeworkid' => $framework->get_id())
2409                  );
2410          $this->assertTrue(external::add_competency_to_plan($pl1->get_id(), $competency->get_id()));
2411  
2412          // A competency cannot be added to plan based on template.
2413          $template = $lpg->create_template();
2414          $plan = array (
2415              'userid' => $usermanage->id,
2416              'status' => \core_competency\plan::STATUS_ACTIVE,
2417              'templateid' => $template->get_id()
2418          );
2419          $pl2 = $lpg->create_plan($plan);
2420          try {
2421              external::add_competency_to_plan($pl2->get_id(), $competency->get_id());
2422              $this->fail('A competency cannot be added to plan based on template');
2423          } catch (coding_exception $ex) {
2424              $this->assertTrue(true);
2425          }
2426  
2427          // User without capability cannot add competency to a plan.
2428          $this->setUser($user);
2429          try {
2430              external::add_competency_to_plan($pl1->get_id(), $competency->get_id());
2431              $this->fail('User without capability cannot add competency to a plan');
2432          } catch (required_capability_exception $ex) {
2433              $this->assertTrue(true);
2434          }
2435      }
2436  
2437      /**
2438       * Test that we can add competency to plan if we have the right capability.
2439       *
2440       * @return void
2441       */
2442      public function test_remove_competency_from_plan() {
2443          $this->resetAfterTest(true);
2444          $dg = $this->getDataGenerator();
2445          $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
2446          $usermanage = $dg->create_user();
2447          $user = $dg->create_user();
2448  
2449          $syscontext = context_system::instance();
2450  
2451          // Creating specific roles.
2452          $managerole = $dg->create_role(array(
2453              'name' => 'User manage',
2454              'shortname' => 'manage'
2455          ));
2456  
2457          assign_capability('moodle/competency:planmanage', CAP_ALLOW, $managerole, $syscontext->id);
2458          assign_capability('moodle/competency:planview', CAP_ALLOW, $managerole, $syscontext->id);
2459  
2460          $dg->role_assign($managerole, $usermanage->id, $syscontext->id);
2461  
2462          $this->setUser($usermanage);
2463          $plan = array (
2464              'userid' => $usermanage->id,
2465              'status' => \core_competency\plan::STATUS_ACTIVE
2466          );
2467          $pl1 = $lpg->create_plan($plan);
2468          $framework = $lpg->create_framework();
2469          $competency = $lpg->create_competency(
2470                  array('competencyframeworkid' => $framework->get_id())
2471                  );
2472          $lpg->create_plan_competency(
2473                  array(
2474                      'planid' => $pl1->get_id(),
2475                      'competencyid' => $competency->get_id()
2476                      )
2477                  );
2478          $this->assertTrue(external::remove_competency_from_plan($pl1->get_id(), $competency->get_id()));
2479          $this->assertCount(0, $pl1->get_competencies());
2480      }
2481  
2482      /**
2483       * Test that we can add competency to plan if we have the right capability.
2484       *
2485       * @return void
2486       */
2487      public function test_reorder_plan_competency() {
2488          $this->resetAfterTest(true);
2489          $dg = $this->getDataGenerator();
2490          $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
2491          $usermanage = $dg->create_user();
2492          $user = $dg->create_user();
2493  
2494          $syscontext = context_system::instance();
2495  
2496          // Creating specific roles.
2497          $managerole = $dg->create_role(array(
2498              'name' => 'User manage',
2499              'shortname' => 'manage'
2500          ));
2501  
2502          assign_capability('moodle/competency:planmanage', CAP_ALLOW, $managerole, $syscontext->id);
2503          assign_capability('moodle/competency:planview', CAP_ALLOW, $managerole, $syscontext->id);
2504  
2505          $dg->role_assign($managerole, $usermanage->id, $syscontext->id);
2506  
2507          $this->setUser($usermanage);
2508          $plan = array (
2509              'userid' => $usermanage->id,
2510              'status' => \core_competency\plan::STATUS_ACTIVE
2511          );
2512          $pl1 = $lpg->create_plan($plan);
2513          $framework = $lpg->create_framework();
2514          $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
2515          $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
2516          $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
2517          $c4 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
2518          $c5 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
2519  
2520          $lpg->create_plan_competency(array('planid' => $pl1->get_id(), 'competencyid' => $c1->get_id(), 'sortorder' => 1));
2521          $lpg->create_plan_competency(array('planid' => $pl1->get_id(), 'competencyid' => $c2->get_id(), 'sortorder' => 2));
2522          $lpg->create_plan_competency(array('planid' => $pl1->get_id(), 'competencyid' => $c3->get_id(), 'sortorder' => 3));
2523          $lpg->create_plan_competency(array('planid' => $pl1->get_id(), 'competencyid' => $c4->get_id(), 'sortorder' => 4));
2524          $lpg->create_plan_competency(array('planid' => $pl1->get_id(), 'competencyid' => $c5->get_id(), 'sortorder' => 5));
2525  
2526          // Test if removing competency from plan don't create sortorder holes.
2527          external::remove_competency_from_plan($pl1->get_id(), $c4->get_id());
2528          $plancomp5 = plan_competency::get_record(array(
2529              'planid' => $pl1->get_id(),
2530              'competencyid' => $c5->get_id()
2531          ));
2532  
2533          $this->assertEquals(3, $plancomp5->get_sortorder());
2534  
2535          $this->assertTrue(external::reorder_plan_competency($pl1->get_id(), $c2->get_id(), $c5->get_id()));
2536          $this->assertTrue(external::reorder_plan_competency($pl1->get_id(), $c3->get_id(), $c1->get_id()));
2537          $plancompetencies = plan_competency::get_records(array('planid' => $pl1->get_id()), 'sortorder', 'ASC');
2538          $plcmp1 = $plancompetencies[0];
2539          $plcmp2 = $plancompetencies[1];
2540          $plcmp3 = $plancompetencies[2];
2541          $plcmp4 = $plancompetencies[3];
2542  
2543          $this->assertEquals($plcmp1->get_competencyid(), $c3->get_id());
2544          $this->assertEquals($plcmp2->get_competencyid(), $c1->get_id());
2545          $this->assertEquals($plcmp3->get_competencyid(), $c5->get_id());
2546          $this->assertEquals($plcmp4->get_competencyid(), $c2->get_id());
2547      }
2548  
2549      /**
2550       * Test resolving sortorder when we creating competency.
2551       */
2552      public function test_fix_sortorder_when_creating_competency() {
2553          $this->resetAfterTest(true);
2554          $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
2555          $framework = $lpg->create_framework();
2556  
2557          $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
2558          $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id(), 'sortorder' => 20));
2559          $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id(), 'sortorder' => 1));
2560  
2561          $this->assertEquals(0, $c1->get_sortorder());
2562          $this->assertEquals(1, $c2->get_sortorder());
2563          $this->assertEquals(2, $c3->get_sortorder());
2564      }
2565  
2566      /**
2567       * Test resolving sortorder when we delete competency.
2568       */
2569      public function test_fix_sortorder_when_delete_competency() {
2570          $this->resetAfterTest(true);
2571          $this->setUser($this->creator);
2572          $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
2573  
2574          $framework = $lpg->create_framework();
2575  
2576          $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
2577          $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
2578          $c2a = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id(), 'parentid' => $c2->get_id()));
2579          $c2b = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id(), 'parentid' => $c2->get_id()));
2580          $c2c = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id(), 'parentid' => $c2->get_id()));
2581          $c2d = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id(), 'parentid' => $c2->get_id()));
2582  
2583          $this->assertEquals(0, $c1->get_sortorder());
2584          $this->assertEquals(1, $c2->get_sortorder());
2585          $this->assertEquals(0, $c2a->get_sortorder());
2586          $this->assertEquals(1, $c2b->get_sortorder());
2587          $this->assertEquals(2, $c2c->get_sortorder());
2588          $this->assertEquals(3, $c2d->get_sortorder());
2589  
2590          $result = external::delete_competency($c1->get_id());
2591          $result = external_api::clean_returnvalue(external::delete_competency_returns(), $result);
2592  
2593          $c2->read();
2594          $c2a->read();
2595          $c2b->read();
2596          $c2c->read();
2597          $c2d->read();
2598  
2599          $this->assertEquals(0, $c2->get_sortorder());
2600          $this->assertEquals(0, $c2a->get_sortorder());
2601          $this->assertEquals(1, $c2b->get_sortorder());
2602          $this->assertEquals(2, $c2c->get_sortorder());
2603          $this->assertEquals(3, $c2d->get_sortorder());
2604  
2605          $result = external::delete_competency($c2b->get_id());
2606          $result = external_api::clean_returnvalue(external::delete_competency_returns(), $result);
2607  
2608          $c2->read();
2609          $c2a->read();
2610          $c2c->read();
2611          $c2d->read();
2612  
2613          $this->assertEquals(0, $c2->get_sortorder());
2614          $this->assertEquals(0, $c2a->get_sortorder());
2615          $this->assertEquals(1, $c2c->get_sortorder());
2616          $this->assertEquals(2, $c2d->get_sortorder());
2617      }
2618  
2619      /**
2620       * Test resolving sortorder when moving a competency.
2621       */
2622      public function test_fix_sortorder_when_moving_competency() {
2623          $this->resetAfterTest(true);
2624          $this->setUser($this->creator);
2625          $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
2626  
2627          $framework = $lpg->create_framework();
2628  
2629          $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
2630          $c1a = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id(), 'parentid' => $c1->get_id()));
2631          $c1b = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id(), 'parentid' => $c1->get_id()));
2632          $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
2633          $c2a = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id(), 'parentid' => $c2->get_id()));
2634          $c2b = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id(), 'parentid' => $c2->get_id()));
2635  
2636          $this->assertEquals(0, $c1->get_sortorder());
2637          $this->assertEquals(0, $c1a->get_sortorder());
2638          $this->assertEquals(1, $c1b->get_sortorder());
2639          $this->assertEquals(1, $c2->get_sortorder());
2640          $this->assertEquals(0, $c2a->get_sortorder());
2641          $this->assertEquals(1, $c2b->get_sortorder());
2642  
2643          $result = external::set_parent_competency($c2a->get_id(), $c1->get_id());
2644          $result = external_api::clean_returnvalue(external::set_parent_competency_returns(), $result);
2645  
2646          $c1->read();
2647          $c1a->read();
2648          $c1b->read();
2649          $c2->read();
2650          $c2a->read();
2651          $c2b->read();
2652  
2653          $this->assertEquals(0, $c1->get_sortorder());
2654          $this->assertEquals(0, $c1a->get_sortorder());
2655          $this->assertEquals(1, $c1b->get_sortorder());
2656          $this->assertEquals(2, $c2a->get_sortorder());
2657          $this->assertEquals(1, $c2->get_sortorder());
2658          $this->assertEquals(0, $c2b->get_sortorder());
2659  
2660          // Move a root node.
2661          $result = external::set_parent_competency($c2->get_id(), $c1b->get_id());
2662          $result = external_api::clean_returnvalue(external::set_parent_competency_returns(), $result);
2663  
2664          $c1->read();
2665          $c1a->read();
2666          $c1b->read();
2667          $c2->read();
2668          $c2a->read();
2669          $c2b->read();
2670  
2671          $this->assertEquals(0, $c1->get_sortorder());
2672          $this->assertEquals(0, $c1a->get_sortorder());
2673          $this->assertEquals(1, $c1b->get_sortorder());
2674          $this->assertEquals(0, $c2->get_sortorder());
2675          $this->assertEquals(0, $c2b->get_sortorder());
2676          $this->assertEquals(2, $c2a->get_sortorder());
2677      }
2678  
2679      public function test_grade_competency_in_plan() {
2680          global $CFG;
2681  
2682          $this->setUser($this->creator);
2683  
2684          $dg = $this->getDataGenerator();
2685          $lpg = $dg->get_plugin_generator('core_competency');
2686  
2687          $f1 = $lpg->create_framework();
2688  
2689          $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
2690  
2691          $tpl = $lpg->create_template();
2692          $lpg->create_template_competency(array('templateid' => $tpl->get_id(), 'competencyid' => $c1->get_id()));
2693  
2694          $plan = $lpg->create_plan(array('userid' => $this->user->id, 'templateid' => $tpl->get_id(), 'name' => 'Evil'));
2695  
2696          $uc = $lpg->create_user_competency(array('userid' => $this->user->id, 'competencyid' => $c1->get_id()));
2697  
2698          $evidence = external::grade_competency_in_plan($plan->get_id(), $c1->get_id(), 1, 'Evil note');
2699  
2700          $this->assertEquals('The competency rating was manually set in the learning plan \'Evil\'.', $evidence->description);
2701          $this->assertEquals('A', $evidence->gradename);
2702          $this->assertEquals('Evil note', $evidence->note);
2703  
2704          $this->setUser($this->user);
2705  
2706          $this->expectException('required_capability_exception');
2707          $evidence = external::grade_competency_in_plan($plan->get_id(), $c1->get_id(), 1);
2708      }
2709  
2710      /**
2711       * Test update course competency settings.
2712       */
2713      public function test_update_course_competency_settings() {
2714          $this->resetAfterTest(true);
2715  
2716          $dg = $this->getDataGenerator();
2717  
2718          $course = $dg->create_course();
2719          $roleid = $dg->create_role();
2720          $noobroleid = $dg->create_role();
2721          $context = context_course::instance($course->id);
2722          $compmanager = $this->getDataGenerator()->create_user();
2723          $compnoob = $this->getDataGenerator()->create_user();
2724  
2725          assign_capability('moodle/competency:coursecompetencyconfigure', CAP_ALLOW, $roleid, $context->id, true);
2726          assign_capability('moodle/competency:coursecompetencyview', CAP_ALLOW, $roleid, $context->id, true);
2727          assign_capability('moodle/competency:coursecompetencyview', CAP_ALLOW, $noobroleid, $context->id, true);
2728  
2729          role_assign($roleid, $compmanager->id, $context->id);
2730          role_assign($noobroleid, $compnoob->id, $context->id);
2731          $dg->enrol_user($compmanager->id, $course->id, $roleid);
2732          $dg->enrol_user($compnoob->id, $course->id, $noobroleid);
2733  
2734          $this->setUser($compmanager);
2735  
2736          // Start the test.
2737          $result = external::update_course_competency_settings($course->id, array('pushratingstouserplans' => true));
2738  
2739          $settings = course_competency_settings::get_by_courseid($course->id);
2740  
2741          $this->assertTrue((bool)$settings->get_pushratingstouserplans());
2742  
2743          $result = external::update_course_competency_settings($course->id, array('pushratingstouserplans' => false));
2744  
2745          $settings = course_competency_settings::get_by_courseid($course->id);
2746  
2747          $this->assertFalse((bool)$settings->get_pushratingstouserplans());
2748          $this->setUser($compnoob);
2749  
2750          $this->expectException('required_capability_exception');
2751          $result = external::update_course_competency_settings($course->id, array('pushratingstouserplans' => true));
2752      }
2753  
2754  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1