Moodle release 3.3.8
[moodle.git] / competency / tests / external_test.php
blob1a26d38198563cb1aaa6096f85516e5b318f34c9
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.
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/>.
17 /**
18 * External tests.
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
25 defined('MOODLE_INTERNAL') || die();
26 global $CFG;
28 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
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;
42 /**
43 * External testcase.
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
49 class core_competency_external_testcase extends externallib_advanced_testcase {
51 /** @var stdClass $creator User with enough permissions to create insystem context. */
52 protected $creator = null;
54 /** @var stdClass $learningplancreator User with enough permissions to create incategory context. */
55 protected $catcreator = null;
57 /** @var stdClass $category Category */
58 protected $category = null;
60 /** @var stdClass $user User with enough permissions to view insystem context */
61 protected $user = null;
63 /** @var stdClass $catuser User with enough permissions to view incategory context */
64 protected $catuser = null;
66 /** @var int Creator role id */
67 protected $creatorrole = null;
69 /** @var int User role id */
70 protected $userrole = null;
72 /** @var stdClass $scale1 Scale */
73 protected $scale1 = null;
75 /** @var stdClass $scale2 Scale */
76 protected $scale2 = null;
78 /** @var stdClass $scale3 Scale */
79 protected $scale3 = null;
81 /** @var stdClass $scale4 Scale */
82 protected $scale4 = null;
84 /** @var string scaleconfiguration */
85 protected $scaleconfiguration1 = null;
87 /** @var string scaleconfiguration */
88 protected $scaleconfiguration2 = null;
90 /** @var string catscaleconfiguration */
91 protected $scaleconfiguration3 = null;
93 /** @var string catscaleconfiguration */
94 protected $catscaleconfiguration4 = null;
96 /**
97 * Setup function- we will create a course and add an assign instance to it.
99 protected function setUp() {
100 global $DB;
102 $this->resetAfterTest(true);
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();
112 $syscontext = context_system::instance();
113 $catcontext = context_coursecat::instance($category->id);
114 $othercatcontext = context_coursecat::instance($othercategory->id);
116 // Fetching default authenticated user role.
117 $userroles = get_archetype_roles('user');
118 $this->assertCount(1, $userroles);
119 $authrole = array_pop($userroles);
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);
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');
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);
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);
161 $this->creator = $creator;
162 $this->catcreator = $catcreator;
163 $this->user = $user;
164 $this->catuser = $catuser;
165 $this->category = $category;
166 $this->othercategory = $othercategory;
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"));
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();
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
202 $result = external::create_competency_framework($framework);
203 return (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result);
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
216 $result = external::create_plan($plan);
217 return (object) external_api::clean_returnvalue(external::create_plan_returns(), $result);
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
229 $result = external::create_template($template);
230 return (object) external_api::clean_returnvalue(external::create_template_returns(), $result);
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
241 $result = external::update_template($template);
242 return external_api::clean_returnvalue(external::update_template_returns(), $result);
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
256 $result = external::update_plan($plan);
257 return external_api::clean_returnvalue(external::update_plan_returns(), $result);
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
274 $result = external::update_competency_framework($framework);
275 return external_api::clean_returnvalue(external::update_competency_framework_returns(), $result);
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
286 $result = external::create_competency($competency);
287 return (object) external_api::clean_returnvalue(external::create_competency_returns(), $result);
290 protected function update_competency($id, $number) {
291 $competency = array(
292 'id' => $id,
293 'shortname' => 'shortname' . $number,
294 'idnumber' => 'idnumber' . $number,
295 'description' => 'description' . $number,
296 'descriptionformat' => FORMAT_HTML
298 $result = external::update_competency($competency);
299 return external_api::clean_returnvalue(external::update_competency_returns(), $result);
303 * Test we can't create a competency framework with only read permissions.
305 * @expectedException required_capability_exception
307 public function test_create_competency_frameworks_with_read_permissions() {
308 $this->setUser($this->user);
310 $result = $this->create_competency_framework(1, true);
314 * Test we can't create a competency framework with only read permissions.
316 * @expectedException required_capability_exception
318 public function test_create_competency_frameworks_with_read_permissions_in_category() {
319 $this->setUser($this->catuser);
320 $result = $this->create_competency_framework(1, false);
324 * Test we can create a competency framework with manage permissions.
326 public function test_create_competency_frameworks_with_manage_permissions() {
327 $this->setUser($this->creator);
328 $result = $this->create_competency_framework(1, true);
330 $this->assertGreaterThan(0, $result->timecreated);
331 $this->assertGreaterThan(0, $result->timemodified);
332 $this->assertEquals($this->creator->id, $result->usermodified);
333 $this->assertEquals('shortname1', $result->shortname);
334 $this->assertEquals('idnumber1', $result->idnumber);
335 $this->assertEquals('description1', $result->description);
336 $this->assertEquals(FORMAT_HTML, $result->descriptionformat);
337 $this->assertEquals($this->scale1->id, $result->scaleid);
338 $this->assertEquals($this->scaleconfiguration1, $result->scaleconfiguration);
339 $this->assertEquals(true, $result->visible);
343 * Test we can create a competency framework with manage permissions.
345 public function test_create_competency_frameworks_with_manage_permissions_in_category() {
346 $this->setUser($this->catcreator);
347 $result = $this->create_competency_framework(1, false);
349 $this->assertGreaterThan(0, $result->timecreated);
350 $this->assertGreaterThan(0, $result->timemodified);
351 $this->assertEquals($this->catcreator->id, $result->usermodified);
352 $this->assertEquals('shortname1', $result->shortname);
353 $this->assertEquals('idnumber1', $result->idnumber);
354 $this->assertEquals('description1', $result->description);
355 $this->assertEquals(FORMAT_HTML, $result->descriptionformat);
356 $this->assertEquals($this->scale1->id, $result->scaleid);
357 $this->assertEquals($this->scaleconfiguration1, $result->scaleconfiguration);
358 $this->assertEquals(true, $result->visible);
360 try {
361 $result = $this->create_competency_framework(1, true);
362 $this->fail('User cannot create a framework at system level.');
363 } catch (required_capability_exception $e) {
364 // All good.
369 * Test we cannot create a competency framework with nasty data.
371 * @expectedException invalid_parameter_exception
373 public function test_create_competency_frameworks_with_nasty_data() {
374 $this->setUser($this->creator);
375 $framework = array(
376 'shortname' => 'short<a href="">',
377 'idnumber' => 'id;"number',
378 'description' => 'de<>\\..scription',
379 'descriptionformat' => FORMAT_HTML,
380 'scaleid' => $this->scale1->id,
381 'scaleconfiguration' => $this->scaleconfiguration1,
382 'visible' => true,
383 'contextid' => context_system::instance()->id
385 $result = external::create_competency_framework($framework);
389 * Test we can read a competency framework with manage permissions.
391 public function test_read_competency_frameworks_with_manage_permissions() {
392 $this->setUser($this->creator);
393 $result = $this->create_competency_framework(1, true);
395 $id = $result->id;
396 $result = external::read_competency_framework($id);
397 $result = (object) external_api::clean_returnvalue(external::read_competency_framework_returns(), $result);
399 $this->assertGreaterThan(0, $result->timecreated);
400 $this->assertGreaterThan(0, $result->timemodified);
401 $this->assertEquals($this->creator->id, $result->usermodified);
402 $this->assertEquals('shortname1', $result->shortname);
403 $this->assertEquals('idnumber1', $result->idnumber);
404 $this->assertEquals('description1', $result->description);
405 $this->assertEquals(FORMAT_HTML, $result->descriptionformat);
406 $this->assertEquals($this->scale1->id, $result->scaleid);
407 $this->assertEquals($this->scaleconfiguration1, $result->scaleconfiguration);
408 $this->assertEquals(true, $result->visible);
412 * Test we can read a competency framework with manage permissions.
414 public function test_read_competency_frameworks_with_manage_permissions_in_category() {
415 $this->setUser($this->creator);
417 $insystem = $this->create_competency_framework(1, true);
418 $incat = $this->create_competency_framework(2, false);
420 $this->setUser($this->catcreator);
421 $id = $incat->id;
422 $result = external::read_competency_framework($id);
423 $result = (object) external_api::clean_returnvalue(external::read_competency_framework_returns(), $result);
425 $this->assertGreaterThan(0, $result->timecreated);
426 $this->assertGreaterThan(0, $result->timemodified);
427 $this->assertEquals($this->creator->id, $result->usermodified);
428 $this->assertEquals('shortname2', $result->shortname);
429 $this->assertEquals('idnumber2', $result->idnumber);
430 $this->assertEquals('description2', $result->description);
431 $this->assertEquals(FORMAT_HTML, $result->descriptionformat);
432 $this->assertEquals($this->scale2->id, $result->scaleid);
433 $this->assertEquals($this->scaleconfiguration2, $result->scaleconfiguration);
434 $this->assertEquals(true, $result->visible);
436 try {
437 $id = $insystem->id;
438 $result = external::read_competency_framework($id);
439 $result = (object) external_api::clean_returnvalue(external::read_competency_framework_returns(), $result);
440 $this->fail('User cannot read a framework at system level.');
441 } catch (required_capability_exception $e) {
442 // All good.
447 * Test we can read a competency framework with read permissions.
449 public function test_read_competency_frameworks_with_read_permissions() {
450 $this->setUser($this->creator);
451 $result = $this->create_competency_framework(1, true);
453 // Switch users to someone with less permissions.
454 $this->setUser($this->user);
455 $id = $result->id;
456 $result = external::read_competency_framework($id);
457 $result = (object) external_api::clean_returnvalue(external::read_competency_framework_returns(), $result);
459 $this->assertGreaterThan(0, $result->timecreated);
460 $this->assertGreaterThan(0, $result->timemodified);
461 $this->assertEquals($this->creator->id, $result->usermodified);
462 $this->assertEquals('shortname1', $result->shortname);
463 $this->assertEquals('idnumber1', $result->idnumber);
464 $this->assertEquals('description1', $result->description);
465 $this->assertEquals(FORMAT_HTML, $result->descriptionformat);
466 $this->assertEquals($this->scale1->id, $result->scaleid);
467 $this->assertEquals($this->scaleconfiguration1, $result->scaleconfiguration);
468 $this->assertEquals(true, $result->visible);
471 * Test we can read a competency framework with read permissions.
473 public function test_read_competency_frameworks_with_read_permissions_in_category() {
474 $this->setUser($this->creator);
476 $insystem = $this->create_competency_framework(1, true);
477 $incat = $this->create_competency_framework(2, false);
479 // Switch users to someone with less permissions.
480 $this->setUser($this->catuser);
481 $id = $incat->id;
482 $result = external::read_competency_framework($id);
483 $result = (object) external_api::clean_returnvalue(external::read_competency_framework_returns(), $result);
485 $this->assertGreaterThan(0, $result->timecreated);
486 $this->assertGreaterThan(0, $result->timemodified);
487 $this->assertEquals($this->creator->id, $result->usermodified);
488 $this->assertEquals('shortname2', $result->shortname);
489 $this->assertEquals('idnumber2', $result->idnumber);
490 $this->assertEquals('description2', $result->description);
491 $this->assertEquals(FORMAT_HTML, $result->descriptionformat);
492 $this->assertEquals($this->scale2->id, $result->scaleid);
493 $this->assertEquals($this->scaleconfiguration2, $result->scaleconfiguration);
494 $this->assertEquals(true, $result->visible);
496 // Switching to user with no permissions.
497 try {
498 $result = external::read_competency_framework($insystem->id);
499 $this->fail('Current user cannot should not be able to read the framework.');
500 } catch (required_capability_exception $e) {
501 // All good.
506 * Test we can delete a competency framework with manage permissions.
508 public function test_delete_competency_frameworks_with_manage_permissions() {
509 $this->setUser($this->creator);
510 $result = $this->create_competency_framework(1, true);
512 $id = $result->id;
513 $result = external::delete_competency_framework($id);
514 $result = external_api::clean_returnvalue(external::delete_competency_framework_returns(), $result);
516 $this->assertTrue($result);
520 * Test we can delete a competency framework with manage permissions.
522 public function test_delete_competency_frameworks_with_manage_permissions_in_category() {
523 $this->setUser($this->creator);
525 $insystem = $this->create_competency_framework(1, true);
526 $incat = $this->create_competency_framework(2, false);
528 $this->setUser($this->catcreator);
529 $id = $incat->id;
530 $result = external::delete_competency_framework($id);
531 $result = external_api::clean_returnvalue(external::delete_competency_framework_returns(), $result);
533 $this->assertTrue($result);
535 try {
536 $id = $insystem->id;
537 $result = external::delete_competency_framework($id);
538 $result = external_api::clean_returnvalue(external::delete_competency_framework_returns(), $result);
539 $this->fail('Current user cannot should not be able to delete the framework.');
540 } catch (required_capability_exception $e) {
541 // All good.
546 * Test we can delete a competency framework with read permissions.
548 * @expectedException required_capability_exception
550 public function test_delete_competency_frameworks_with_read_permissions() {
551 $this->setUser($this->creator);
552 $result = $this->create_competency_framework(1, true);
554 $id = $result->id;
555 // Switch users to someone with less permissions.
556 $this->setUser($this->user);
557 $result = external::delete_competency_framework($id);
561 * Test we can update a competency framework with manage permissions.
563 public function test_update_competency_frameworks_with_manage_permissions() {
564 $this->setUser($this->creator);
565 $result = $this->create_competency_framework(1, true);
567 $result = $this->update_competency_framework($result->id, 2, true);
569 $this->assertTrue($result);
573 * Test we can update a competency framework with manage permissions.
575 public function test_update_competency_frameworks_with_manage_permissions_in_category() {
576 $this->setUser($this->creator);
578 $insystem = $this->create_competency_framework(1, true);
579 $incat = $this->create_competency_framework(2, false);
581 $this->setUser($this->catcreator);
582 $id = $incat->id;
584 $result = $this->update_competency_framework($incat->id, 3, false);
586 $this->assertTrue($result);
588 try {
589 $result = $this->update_competency_framework($insystem->id, 4, true);
590 $this->fail('Current user should not be able to update the framework.');
591 } catch (required_capability_exception $e) {
592 // All good.
596 public function test_update_framework_scale() {
597 $this->setUser($this->creator);
598 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
600 $s1 = $this->getDataGenerator()->create_scale();
602 $f1 = $lpg->create_framework(array('scaleid' => $s1->id));
603 $f2 = $lpg->create_framework(array('scaleid' => $s1->id));
604 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
605 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f2->get('id')));
607 $this->assertEquals($s1->id, $f1->get('scaleid'));
609 // Make the scale of f2 being used.
610 $lpg->create_user_competency(array('userid' => $this->user->id, 'competencyid' => $c2->get('id')));
612 // Changing the framework where the scale is not used.
613 $result = $this->update_competency_framework($f1->get('id'), 3, true);
615 $f1 = new \core_competency\competency_framework($f1->get('id'));
616 $this->assertEquals($this->scale3->id, $f1->get('scaleid'));
618 // Changing the framework where the scale is used.
619 try {
620 $result = $this->update_competency_framework($f2->get('id'), 4, true);
621 $this->fail('The scale cannot be changed once used.');
622 } catch (\core\invalid_persistent_exception $e) {
623 $this->assertRegexp('/scaleid/', $e->getMessage());
628 * Test we can update a competency framework with read permissions.
630 * @expectedException required_capability_exception
632 public function test_update_competency_frameworks_with_read_permissions() {
633 $this->setUser($this->creator);
634 $result = $this->create_competency_framework(1, true);
636 $this->setUser($this->user);
637 $result = $this->update_competency_framework($result->id, 2, true);
641 * Test we can list and count competency frameworks with manage permissions.
643 public function test_list_and_count_competency_frameworks_with_manage_permissions() {
644 $this->setUser($this->creator);
645 $result = $this->create_competency_framework(1, true);
646 $result = $this->create_competency_framework(2, true);
647 $result = $this->create_competency_framework(3, true);
648 $result = $this->create_competency_framework(4, false);
650 $result = external::count_competency_frameworks(array('contextid' => context_system::instance()->id), 'self');
651 $result = external_api::clean_returnvalue(external::count_competency_frameworks_returns(), $result);
653 $this->assertEquals($result, 3);
655 $result = external::list_competency_frameworks('shortname', 'ASC', 0, 10,
656 array('contextid' => context_system::instance()->id), 'self', false);
657 $result = external_api::clean_returnvalue(external::list_competency_frameworks_returns(), $result);
659 $this->assertEquals(count($result), 3);
660 $result = (object) $result[0];
662 $this->assertGreaterThan(0, $result->timecreated);
663 $this->assertGreaterThan(0, $result->timemodified);
664 $this->assertEquals($this->creator->id, $result->usermodified);
665 $this->assertEquals('shortname1', $result->shortname);
666 $this->assertEquals('idnumber1', $result->idnumber);
667 $this->assertEquals('description1', $result->description);
668 $this->assertEquals(FORMAT_HTML, $result->descriptionformat);
669 $this->assertEquals($this->scale1->id, $result->scaleid);
670 $this->assertEquals($this->scaleconfiguration1, $result->scaleconfiguration);
671 $this->assertEquals(true, $result->visible);
674 public function test_list_competency_frameworks_with_query() {
675 $this->setUser($this->creator);
676 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
677 $framework1 = $lpg->create_framework(array(
678 'shortname' => 'shortname_beetroot',
679 'idnumber' => 'idnumber_cinnamon',
680 'description' => 'description',
681 'descriptionformat' => FORMAT_HTML,
682 'visible' => true,
683 'contextid' => context_system::instance()->id
685 $framework2 = $lpg->create_framework(array(
686 'shortname' => 'shortname_citrus',
687 'idnumber' => 'idnumber_beer',
688 'description' => 'description',
689 'descriptionformat' => FORMAT_HTML,
690 'visible' => true,
691 'contextid' => context_system::instance()->id
694 // Search on both ID number and shortname.
695 $result = external::list_competency_frameworks('shortname', 'ASC', 0, 10,
696 array('contextid' => context_system::instance()->id), 'self', false, 'bee');
697 $result = external_api::clean_returnvalue(external::list_competency_frameworks_returns(), $result);
698 $this->assertCount(2, $result);
699 $f = (object) array_shift($result);
700 $this->assertEquals($framework1->get('id'), $f->id);
701 $f = (object) array_shift($result);
702 $this->assertEquals($framework2->get('id'), $f->id);
704 // Search on ID number.
705 $result = external::list_competency_frameworks('shortname', 'ASC', 0, 10,
706 array('contextid' => context_system::instance()->id), 'self', false, 'beer');
707 $result = external_api::clean_returnvalue(external::list_competency_frameworks_returns(), $result);
708 $this->assertCount(1, $result);
709 $f = (object) array_shift($result);
710 $this->assertEquals($framework2->get('id'), $f->id);
712 // Search on shortname.
713 $result = external::list_competency_frameworks('shortname', 'ASC', 0, 10,
714 array('contextid' => context_system::instance()->id), 'self', false, 'cinnamon');
715 $result = external_api::clean_returnvalue(external::list_competency_frameworks_returns(), $result);
716 $this->assertCount(1, $result);
717 $f = (object) array_shift($result);
718 $this->assertEquals($framework1->get('id'), $f->id);
720 // No match.
721 $result = external::list_competency_frameworks('shortname', 'ASC', 0, 10,
722 array('contextid' => context_system::instance()->id), 'self', false, 'pwnd!');
723 $result = external_api::clean_returnvalue(external::list_competency_frameworks_returns(), $result);
724 $this->assertCount(0, $result);
728 * Test we can list and count competency frameworks with read permissions.
730 public function test_list_and_count_competency_frameworks_with_read_permissions() {
731 $this->setUser($this->creator);
732 $result = $this->create_competency_framework(1, true);
733 $result = $this->create_competency_framework(2, true);
734 $result = $this->create_competency_framework(3, true);
735 $result = $this->create_competency_framework(4, false);
737 $this->setUser($this->user);
738 $result = external::count_competency_frameworks(array('contextid' => context_system::instance()->id), 'self');
739 $result = external_api::clean_returnvalue(external::count_competency_frameworks_returns(), $result);
740 $this->assertEquals($result, 3);
742 $result = external::list_competency_frameworks('shortname', 'ASC', 0, 10,
743 array('contextid' => context_system::instance()->id), 'self', false);
744 $result = external_api::clean_returnvalue(external::list_competency_frameworks_returns(), $result);
746 $this->assertEquals(count($result), 3);
747 $result = (object) $result[0];
749 $this->assertGreaterThan(0, $result->timecreated);
750 $this->assertGreaterThan(0, $result->timemodified);
751 $this->assertEquals($this->creator->id, $result->usermodified);
752 $this->assertEquals('shortname1', $result->shortname);
753 $this->assertEquals('idnumber1', $result->idnumber);
754 $this->assertEquals('description1', $result->description);
755 $this->assertEquals(FORMAT_HTML, $result->descriptionformat);
756 $this->assertEquals($this->scale1->id, $result->scaleid);
757 $this->assertEquals($this->scaleconfiguration1, $result->scaleconfiguration);
758 $this->assertEquals(true, $result->visible);
762 * Test we can't create a competency with only read permissions.
764 * @expectedException required_capability_exception
766 public function test_create_competency_with_read_permissions() {
767 $framework = $this->getDataGenerator()->get_plugin_generator('core_competency')->create_framework();
768 $this->setUser($this->user);
769 $competency = $this->create_competency(1, $framework->get('id'));
773 * Test we can create a competency with manage permissions.
775 public function test_create_competency_with_manage_permissions() {
776 $this->setUser($this->creator);
777 $framework = $this->create_competency_framework(1, true);
778 $competency = $this->create_competency(1, $framework->id);
780 $this->assertGreaterThan(0, $competency->timecreated);
781 $this->assertGreaterThan(0, $competency->timemodified);
782 $this->assertEquals($this->creator->id, $competency->usermodified);
783 $this->assertEquals('shortname1', $competency->shortname);
784 $this->assertEquals('idnumber1', $competency->idnumber);
785 $this->assertEquals('description1', $competency->description);
786 $this->assertEquals(FORMAT_HTML, $competency->descriptionformat);
787 $this->assertEquals(0, $competency->parentid);
788 $this->assertEquals($framework->id, $competency->competencyframeworkid);
793 * Test we can create a competency with manage permissions.
795 public function test_create_competency_with_manage_permissions_in_category() {
796 $this->setUser($this->creator);
798 $insystem = $this->create_competency_framework(1, true);
799 $incat = $this->create_competency_framework(2, false);
801 $this->setUser($this->catcreator);
803 $competency = $this->create_competency(1, $incat->id);
805 $this->assertGreaterThan(0, $competency->timecreated);
806 $this->assertGreaterThan(0, $competency->timemodified);
807 $this->assertEquals($this->catcreator->id, $competency->usermodified);
808 $this->assertEquals('shortname1', $competency->shortname);
809 $this->assertEquals('idnumber1', $competency->idnumber);
810 $this->assertEquals('description1', $competency->description);
811 $this->assertEquals(FORMAT_HTML, $competency->descriptionformat);
812 $this->assertEquals(0, $competency->parentid);
813 $this->assertEquals($incat->id, $competency->competencyframeworkid);
815 try {
816 $competency = $this->create_competency(2, $insystem->id);
817 $this->fail('User should not be able to create a competency in system context.');
818 } catch (required_capability_exception $e) {
819 // All good.
824 * Test we cannot create a competency with nasty data.
826 * @expectedException invalid_parameter_exception
828 public function test_create_competency_with_nasty_data() {
829 $this->setUser($this->creator);
830 $framework = $this->create_competency_framework(1, true);
831 $competency = array(
832 'shortname' => 'shortname<a href="">',
833 'idnumber' => 'id;"number',
834 'description' => 'de<>\\..scription',
835 'descriptionformat' => FORMAT_HTML,
836 'competencyframeworkid' => $framework->id,
837 'sortorder' => 0
839 $result = external::create_competency($competency);
840 $result = (object) external_api::clean_returnvalue(external::create_competency_returns(), $result);
844 * Test we can read a competency with manage permissions.
846 public function test_read_competencies_with_manage_permissions() {
847 $this->setUser($this->creator);
848 $framework = $this->create_competency_framework(1, true);
849 $competency = $this->create_competency(1, $framework->id);
851 $id = $competency->id;
852 $result = external::read_competency($id);
853 $result = (object) external_api::clean_returnvalue(external::read_competency_returns(), $result);
855 $this->assertGreaterThan(0, $result->timecreated);
856 $this->assertGreaterThan(0, $result->timemodified);
857 $this->assertEquals($this->creator->id, $result->usermodified);
858 $this->assertEquals('shortname1', $result->shortname);
859 $this->assertEquals('idnumber1', $result->idnumber);
860 $this->assertEquals('description1', $result->description);
861 $this->assertEquals(FORMAT_HTML, $result->descriptionformat);
862 $this->assertEquals(0, $result->parentid);
863 $this->assertEquals($framework->id, $result->competencyframeworkid);
867 * Test we can read a competency with manage permissions.
869 public function test_read_competencies_with_manage_permissions_in_category() {
870 $this->setUser($this->creator);
872 $sysframework = $this->create_competency_framework(1, true);
873 $insystem = $this->create_competency(1, $sysframework->id);
875 $catframework = $this->create_competency_framework(2, false);
876 $incat = $this->create_competency(2, $catframework->id);
878 $this->setUser($this->catcreator);
879 $id = $incat->id;
880 $result = external::read_competency($id);
881 $result = (object) external_api::clean_returnvalue(external::read_competency_returns(), $result);
883 $this->assertGreaterThan(0, $result->timecreated);
884 $this->assertGreaterThan(0, $result->timemodified);
885 $this->assertEquals($this->creator->id, $result->usermodified);
886 $this->assertEquals('shortname2', $result->shortname);
887 $this->assertEquals('idnumber2', $result->idnumber);
888 $this->assertEquals('description2', $result->description);
889 $this->assertEquals(FORMAT_HTML, $result->descriptionformat);
890 $this->assertEquals(0, $result->parentid);
891 $this->assertEquals($catframework->id, $result->competencyframeworkid);
893 try {
894 external::read_competency($insystem->id);
895 $this->fail('User should not be able to read a competency in system context.');
896 } catch (required_capability_exception $e) {
897 // All good.
902 * Test we can read a competency with read permissions.
904 public function test_read_competencies_with_read_permissions() {
905 $this->setUser($this->creator);
906 $framework = $this->create_competency_framework(1, true);
907 $competency = $this->create_competency(1, $framework->id);
909 // Switch users to someone with less permissions.
910 $this->setUser($this->user);
911 $id = $competency->id;
912 $result = external::read_competency($id);
913 $result = (object) external_api::clean_returnvalue(external::read_competency_returns(), $result);
915 $this->assertGreaterThan(0, $result->timecreated);
916 $this->assertGreaterThan(0, $result->timemodified);
917 $this->assertEquals($this->creator->id, $result->usermodified);
918 $this->assertEquals('shortname1', $result->shortname);
919 $this->assertEquals('idnumber1', $result->idnumber);
920 $this->assertEquals('description1', $result->description);
921 $this->assertEquals(FORMAT_HTML, $result->descriptionformat);
922 $this->assertEquals(0, $result->parentid);
923 $this->assertEquals($framework->id, $result->competencyframeworkid);
927 * Test we can read a competency with read permissions.
929 public function test_read_competencies_with_read_permissions_in_category() {
930 $this->setUser($this->creator);
931 $sysframework = $this->create_competency_framework(1, true);
932 $insystem = $this->create_competency(1, $sysframework->id);
933 $catframework = $this->create_competency_framework(2, false);
934 $incat = $this->create_competency(2, $catframework->id);
936 // Switch users to someone with less permissions.
937 $this->setUser($this->catuser);
938 $id = $incat->id;
939 $result = external::read_competency($id);
940 $result = (object) external_api::clean_returnvalue(external::read_competency_returns(), $result);
942 $this->assertGreaterThan(0, $result->timecreated);
943 $this->assertGreaterThan(0, $result->timemodified);
944 $this->assertEquals($this->creator->id, $result->usermodified);
945 $this->assertEquals('shortname2', $result->shortname);
946 $this->assertEquals('idnumber2', $result->idnumber);
947 $this->assertEquals('description2', $result->description);
948 $this->assertEquals(FORMAT_HTML, $result->descriptionformat);
949 $this->assertEquals(0, $result->parentid);
950 $this->assertEquals($catframework->id, $result->competencyframeworkid);
952 try {
953 external::read_competency($insystem->id);
954 $this->fail('User should not be able to read a competency in system context.');
955 } catch (required_capability_exception $e) {
956 // All good.
961 * Test we can delete a competency with manage permissions.
963 public function test_delete_competency_with_manage_permissions() {
964 $this->setUser($this->creator);
965 $framework = $this->create_competency_framework(1, true);
966 $result = $this->create_competency(1, $framework->id);
968 $id = $result->id;
969 $result = external::delete_competency($id);
970 $result = external_api::clean_returnvalue(external::delete_competency_returns(), $result);
972 $this->assertTrue($result);
976 * Test we can delete a competency with manage permissions.
978 public function test_delete_competency_with_manage_permissions_in_category() {
979 $this->setUser($this->creator);
981 $sysframework = $this->create_competency_framework(1, true);
982 $insystem = $this->create_competency(1, $sysframework->id);
983 $catframework = $this->create_competency_framework(2, false);
984 $incat = $this->create_competency(2, $catframework->id);
986 $this->setUser($this->catcreator);
987 $id = $incat->id;
988 $result = external::delete_competency($id);
989 $result = external_api::clean_returnvalue(external::delete_competency_returns(), $result);
991 $this->assertTrue($result);
993 try {
994 $result = external::delete_competency($insystem->id);
995 $this->fail('User should not be able to delete a competency in system context.');
996 } catch (required_capability_exception $e) {
997 // All good.
1002 * Test we can delete a competency with read permissions.
1004 * @expectedException required_capability_exception
1006 public function test_delete_competency_with_read_permissions() {
1007 $this->setUser($this->creator);
1008 $framework = $this->create_competency_framework(1, true);
1009 $result = $this->create_competency(1, $framework->id);
1011 $id = $result->id;
1012 // Switch users to someone with less permissions.
1013 $this->setUser($this->user);
1014 $result = external::delete_competency($id);
1018 * Test we can update a competency with manage permissions.
1020 public function test_update_competency_with_manage_permissions() {
1021 $this->setUser($this->creator);
1022 $framework = $this->create_competency_framework(1, true);
1023 $result = $this->create_competency(1, $framework->id);
1025 $result = $this->update_competency($result->id, 2);
1027 $this->assertTrue($result);
1031 * Test we can update a competency with manage permissions.
1033 public function test_update_competency_with_manage_permissions_in_category() {
1034 $this->setUser($this->creator);
1036 $sysframework = $this->create_competency_framework(1, true);
1037 $insystem = $this->create_competency(1, $sysframework->id);
1038 $catframework = $this->create_competency_framework(2, false);
1039 $incat = $this->create_competency(2, $catframework->id);
1041 $this->setUser($this->catcreator);
1043 $result = $this->update_competency($incat->id, 2);
1045 $this->assertTrue($result);
1047 try {
1048 $result = $this->update_competency($insystem->id, 3);
1049 $this->fail('User should not be able to update a competency in system context.');
1050 } catch (required_capability_exception $e) {
1051 // All good.
1056 * Test we can update a competency with read permissions.
1058 * @expectedException required_capability_exception
1060 public function test_update_competency_with_read_permissions() {
1061 $this->setUser($this->creator);
1062 $framework = $this->create_competency_framework(1, true);
1063 $result = $this->create_competency(1, $framework->id);
1065 $this->setUser($this->user);
1066 $result = $this->update_competency($result->id, 2);
1070 * Test count competencies with filters.
1072 public function test_count_competencies_with_filters() {
1073 $this->setUser($this->creator);
1075 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1076 $f1 = $lpg->create_framework();
1077 $f2 = $lpg->create_framework();
1078 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
1079 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id'), 'shortname' => 'A'));
1080 $c3 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
1081 $c4 = $lpg->create_competency(array('competencyframeworkid' => $f2->get('id')));
1082 $c5 = $lpg->create_competency(array('competencyframeworkid' => $f2->get('id')));
1084 $result = external::count_competencies(array(array('column' => 'competencyframeworkid', 'value' => $f2->get('id'))));
1085 $result = external_api::clean_returnvalue(external::count_competencies_returns(), $result);
1086 $this->assertEquals(2, $result);
1088 $result = external::count_competencies(array(array('column' => 'competencyframeworkid', 'value' => $f1->get('id'))));
1089 $result = external_api::clean_returnvalue(external::count_competencies_returns(), $result);
1090 $this->assertEquals(3, $result);
1092 $result = external::count_competencies(array(array('column' => 'shortname', 'value' => 'A')));
1093 $result = external_api::clean_returnvalue(external::count_competencies_returns(), $result);
1094 $this->assertEquals(1, $result);
1098 * Test we can list and count competencies with manage permissions.
1100 public function test_list_and_count_competencies_with_manage_permissions() {
1101 $this->setUser($this->creator);
1102 $framework = $this->create_competency_framework(1, true);
1103 $result = $this->create_competency(1, $framework->id);
1104 $result = $this->create_competency(2, $framework->id);
1105 $result = $this->create_competency(3, $framework->id);
1107 $result = external::count_competencies(array());
1108 $result = external_api::clean_returnvalue(external::count_competencies_returns(), $result);
1110 $this->assertEquals($result, 3);
1112 array('id' => $result = external::list_competencies(array(), 'shortname', 'ASC', 0, 10, context_system::instance()->id));
1113 $result = external_api::clean_returnvalue(external::list_competencies_returns(), $result);
1115 $this->assertEquals(count($result), 3);
1116 $result = (object) $result[0];
1118 $this->assertGreaterThan(0, $result->timecreated);
1119 $this->assertGreaterThan(0, $result->timemodified);
1120 $this->assertEquals($this->creator->id, $result->usermodified);
1121 $this->assertEquals('shortname1', $result->shortname);
1122 $this->assertEquals('idnumber1', $result->idnumber);
1123 $this->assertEquals('description1', $result->description);
1127 * Test we can list and count competencies with read permissions.
1129 public function test_list_and_count_competencies_with_read_permissions() {
1130 $this->setUser($this->creator);
1131 $framework = $this->create_competency_framework(1, true);
1132 $result = $this->create_competency(1, $framework->id);
1133 $result = $this->create_competency(2, $framework->id);
1134 $result = $this->create_competency(3, $framework->id);
1136 $this->setUser($this->user);
1138 $result = external::count_competencies(array());
1139 $result = external_api::clean_returnvalue(external::count_competencies_returns(), $result);
1141 $this->assertEquals($result, 3);
1143 array('id' => $result = external::list_competencies(array(), 'shortname', 'ASC', 0, 10, context_system::instance()->id));
1144 $result = external_api::clean_returnvalue(external::list_competencies_returns(), $result);
1146 $this->assertEquals(count($result), 3);
1147 $result = (object) $result[0];
1149 $this->assertGreaterThan(0, $result->timecreated);
1150 $this->assertGreaterThan(0, $result->timemodified);
1151 $this->assertEquals($this->creator->id, $result->usermodified);
1152 $this->assertEquals('shortname1', $result->shortname);
1153 $this->assertEquals('idnumber1', $result->idnumber);
1154 $this->assertEquals('description1', $result->description);
1158 * Test we can search for competencies.
1160 public function test_search_competencies_with_read_permissions() {
1161 $this->setUser($this->creator);
1162 $framework = $this->create_competency_framework(1, true);
1163 $result = $this->create_competency(1, $framework->id);
1164 $result = $this->create_competency(2, $framework->id);
1165 $result = $this->create_competency(3, $framework->id);
1167 $this->setUser($this->user);
1169 $result = external::search_competencies('short', $framework->id);
1170 $result = external_api::clean_returnvalue(external::search_competencies_returns(), $result);
1172 $this->assertEquals(count($result), 3);
1173 $result = (object) $result[0];
1175 $this->assertGreaterThan(0, $result->timecreated);
1176 $this->assertGreaterThan(0, $result->timemodified);
1177 $this->assertEquals($this->creator->id, $result->usermodified);
1178 $this->assertEquals('shortname1', $result->shortname);
1179 $this->assertEquals('idnumber1', $result->idnumber);
1180 $this->assertEquals('description1', $result->description);
1184 * Test plans creation and updates.
1186 public function test_create_and_update_plans() {
1187 $syscontext = context_system::instance();
1189 $this->setUser($this->creator);
1190 $plan0 = $this->create_plan(1, $this->creator->id, 0, plan::STATUS_ACTIVE, 0);
1192 $this->setUser($this->user);
1194 try {
1195 $plan1 = $this->create_plan(2, $this->user->id, 0, plan::STATUS_DRAFT, 0);
1196 $this->fail('Exception expected due to not permissions to create draft plans');
1197 } catch (moodle_exception $e) {
1198 $this->assertEquals('nopermissions', $e->errorcode);
1201 assign_capability('moodle/competency:planmanageowndraft', CAP_ALLOW, $this->userrole, $syscontext->id);
1202 accesslib_clear_all_caches_for_unit_testing();
1204 $this->setUser($this->user);
1206 $plan2 = $this->create_plan(3, $this->user->id, 0, plan::STATUS_DRAFT, 0);
1208 // Basic update on the plan.
1209 $this->assertNotEquals('Updated plan 2 name', $plan2->name);
1210 $plan2 = external::update_plan(['id' => $plan2->id, 'name' => 'Updated plan 2 name']);
1211 $this->assertEquals('Updated plan 2 name', $plan2->name);
1213 try {
1214 $plan3 = $this->create_plan(4, $this->user->id, 0, plan::STATUS_ACTIVE, 0);
1215 $this->fail('Exception expected due to not permissions to create active plans');
1216 } catch (moodle_exception $e) {
1217 $this->assertEquals('nopermissions', $e->errorcode);
1219 try {
1220 $plan3 = $this->update_plan($plan2->id, 4, $this->user->id, 0, plan::STATUS_COMPLETE, 0);
1221 $this->fail('We cannot complete a plan using api::update_plan().');
1222 } catch (coding_exception $e) {
1223 $this->assertTrue(true);
1226 assign_capability('moodle/competency:planmanageown', CAP_ALLOW, $this->userrole, $syscontext->id);
1227 accesslib_clear_all_caches_for_unit_testing();
1229 $plan3 = $this->create_plan(4, $this->user->id, 0, plan::STATUS_ACTIVE, 0);
1230 try {
1231 $plan4 = $this->create_plan(6, $this->creator->id, 0, plan::STATUS_COMPLETE, 0);
1232 $this->fail('Plans cannot be created as complete.');
1233 } catch (coding_exception $e) {
1234 $this->assertRegexp('/A plan cannot be created as complete./', $e->getMessage());
1237 try {
1238 $plan0 = $this->update_plan($plan0->id, 1, $this->user->id, 0, plan::STATUS_ACTIVE, 0);
1239 } catch (moodle_exception $e) {
1240 $this->assertEquals('nopermissions', $e->errorcode);
1243 unassign_capability('moodle/competency:planmanageown', $this->userrole, $syscontext->id);
1244 unassign_capability('moodle/competency:planmanageowndraft', $this->userrole, $syscontext->id);
1245 accesslib_clear_all_caches_for_unit_testing();
1247 try {
1248 // Cannot be updated even if they created it.
1249 $this->update_plan($plan2->id, 1, $this->user->id, 0, plan::STATUS_ACTIVE, 0);
1250 $this->fail('The user can not update their own plan without permissions.');
1251 } catch (required_capability_exception $e) {
1252 $this->assertRegexp('/Manage learning plans./', $e->getMessage());
1257 * Test complete plan.
1259 public function test_complete_plan() {
1260 $syscontext = context_system::instance();
1262 $this->setUser($this->creator);
1264 $this->setUser($this->user);
1266 assign_capability('moodle/competency:planmanageowndraft', CAP_ALLOW, $this->userrole, $syscontext->id);
1267 assign_capability('moodle/competency:planmanageown', CAP_ALLOW, $this->userrole, $syscontext->id);
1268 accesslib_clear_all_caches_for_unit_testing();
1270 $this->setUser($this->user);
1272 $plan = $this->create_plan(1, $this->user->id, 0, plan::STATUS_ACTIVE, 0);
1274 $result = external::complete_plan($plan->id);
1275 $this->assertTrue($result);
1279 * Test reopen plan.
1281 public function test_reopen_plan() {
1282 $syscontext = context_system::instance();
1284 $this->setUser($this->creator);
1286 $this->setUser($this->user);
1288 assign_capability('moodle/competency:planmanageowndraft', CAP_ALLOW, $this->userrole, $syscontext->id);
1289 assign_capability('moodle/competency:planmanageown', CAP_ALLOW, $this->userrole, $syscontext->id);
1290 accesslib_clear_all_caches_for_unit_testing();
1292 $this->setUser($this->user);
1294 $plan = $this->create_plan(1, $this->user->id, 0, plan::STATUS_ACTIVE, 0);
1295 external::complete_plan($plan->id);
1297 $result = external::reopen_plan($plan->id);
1298 $this->assertTrue($result);
1302 * Test that we can read plans.
1304 public function test_read_plans() {
1305 global $OUTPUT;
1306 $this->setUser($this->creator);
1308 $syscontext = context_system::instance();
1310 $plan1 = $this->create_plan(1, $this->user->id, 0, plan::STATUS_DRAFT, 0);
1311 $plan2 = $this->create_plan(2, $this->user->id, 0, plan::STATUS_ACTIVE, 0);
1312 $plan3 = $this->create_plan(3, $this->user->id, 0, plan::STATUS_ACTIVE, 0);
1313 external::complete_plan($plan3->id);
1314 $plan3 = (object) external::read_plan($plan3->id);
1316 $data = external::read_plan($plan1->id);
1317 $this->assertEquals((array)$plan1, external::read_plan($plan1->id));
1318 $data = external::read_plan($plan2->id);
1319 $this->assertEquals((array)$plan2, external::read_plan($plan2->id));
1320 $data = external::read_plan($plan3->id);
1321 $this->assertEquals((array)$plan3, external::read_plan($plan3->id));
1323 $this->setUser($this->user);
1325 // The normal user can not edit these plans.
1326 $plan1->canmanage = false;
1327 $plan2->canmanage = false;
1328 $plan3->canmanage = false;
1329 $plan1->canbeedited = false;
1330 $plan2->canbeedited = false;
1331 $plan3->canbeedited = false;
1332 $plan1->canrequestreview = true;
1333 $plan2->canrequestreview = true;
1334 $plan3->canrequestreview = true;
1335 $plan1->canreview = false;
1336 $plan2->canreview = false;
1337 $plan3->canreview = false;
1338 $plan1->iscompleteallowed = false;
1339 $plan2->iscompleteallowed = false;
1340 $plan3->iscompleteallowed = false;
1341 $plan1->isrequestreviewallowed = true;
1342 $plan2->isrequestreviewallowed = true;
1343 $plan3->isrequestreviewallowed = true;
1344 $plan1->isapproveallowed = false;
1345 $plan2->isapproveallowed = false;
1346 $plan3->isapproveallowed = false;
1347 $plan1->isunapproveallowed = false;
1348 $plan2->isunapproveallowed = false;
1349 $plan3->isunapproveallowed = false;
1350 $plan3->isreopenallowed = false;
1351 $plan1->commentarea['canpost'] = false;
1352 $plan1->commentarea['canview'] = true;
1354 // Prevent the user from seeing their own non-draft plans.
1355 assign_capability('moodle/competency:plancommentown', CAP_PROHIBIT, $this->userrole, $syscontext->id, true);
1356 assign_capability('moodle/competency:planviewown', CAP_PROHIBIT, $this->userrole, $syscontext->id, true);
1357 assign_capability('moodle/competency:planviewowndraft', CAP_ALLOW, $this->userrole, $syscontext->id, true);
1358 accesslib_clear_all_caches_for_unit_testing();
1360 $this->assertEquals((array)$plan1, external::read_plan($plan1->id));
1362 try {
1363 external::read_plan($plan2->id);
1364 $this->fail('Exception expected due to not permissions to read plan');
1365 } catch (moodle_exception $e) {
1366 $this->assertEquals('nopermissions', $e->errorcode);
1368 try {
1369 external::read_plan($plan3->id);
1370 $this->fail('Exception expected due to not permissions to read plan');
1371 } catch (moodle_exception $e) {
1372 $this->assertEquals('nopermissions', $e->errorcode);
1375 // Allow user to see their plan.
1376 assign_capability('moodle/competency:plancommentown', CAP_ALLOW, $this->userrole, $syscontext->id, true);
1377 assign_capability('moodle/competency:planviewown', CAP_ALLOW, $this->userrole, $syscontext->id, true);
1378 assign_capability('moodle/competency:planmanageowndraft', CAP_PROHIBIT, $this->userrole, $syscontext->id, true);
1379 accesslib_clear_all_caches_for_unit_testing();
1381 $plan1->commentarea['canpost'] = true;
1382 $plan1->commentarea['canview'] = true;
1383 $plan2->commentarea['canpost'] = true;
1384 $plan2->isrequestreviewallowed = false;
1385 $plan3->commentarea['canpost'] = true;
1386 $plan3->isrequestreviewallowed = false;
1387 $plan1->commentarea['canpostorhascomments'] = true;
1388 $plan2->commentarea['canpostorhascomments'] = true;
1389 $plan3->commentarea['canpostorhascomments'] = true;
1391 $this->assertEquals((array)$plan1, external::read_plan($plan1->id));
1392 $this->assertEquals((array)$plan2, external::read_plan($plan2->id));
1393 $this->assertEquals((array)$plan3, external::read_plan($plan3->id));
1395 // Allow use to manage their own draft plan.
1396 assign_capability('moodle/competency:planviewown', CAP_PROHIBIT, $this->userrole, $syscontext->id, true);
1397 assign_capability('moodle/competency:planmanageown', CAP_PROHIBIT, $this->userrole, $syscontext->id, true);
1398 assign_capability('moodle/competency:planmanageowndraft', CAP_ALLOW, $this->userrole, $syscontext->id, true);
1399 accesslib_clear_all_caches_for_unit_testing();
1401 $plan1->canmanage = true;
1402 $plan1->canbeedited = true;
1403 $plan1->canrequestreview = true;
1404 $plan1->isrequestreviewallowed = true;
1405 $this->assertEquals((array)$plan1, external::read_plan($plan1->id));
1406 try {
1407 external::read_plan($plan2->id);
1408 $this->fail('Exception expected due to not permissions to read plan');
1409 } catch (moodle_exception $e) {
1410 $this->assertEquals('nopermissions', $e->errorcode);
1412 try {
1413 external::read_plan($plan3->id);
1414 $this->fail('Exception expected due to not permissions to read plan');
1415 } catch (moodle_exception $e) {
1416 $this->assertEquals('nopermissions', $e->errorcode);
1419 // Allow use to manage their plan.
1420 assign_capability('moodle/competency:planviewown', CAP_PROHIBIT, $this->userrole, $syscontext->id, true);
1421 assign_capability('moodle/competency:planmanageowndraft', CAP_PROHIBIT, $this->userrole, $syscontext->id, true);
1422 assign_capability('moodle/competency:planmanageown', CAP_ALLOW, $this->userrole, $syscontext->id, true);
1423 accesslib_clear_all_caches_for_unit_testing();
1425 $plan1->canmanage = false;
1426 $plan1->canbeedited = false;
1427 $plan1->canrequestreview = true;
1428 $plan1->canreview = true;
1429 $plan1->isrequestreviewallowed = true;
1430 $plan1->isapproveallowed = true;
1431 $plan1->iscompleteallowed = false;
1433 $plan2->canmanage = true;
1434 $plan2->canbeedited = true;
1435 $plan2->canreview = true;
1436 $plan2->iscompleteallowed = true;
1437 $plan2->isunapproveallowed = true;
1439 $plan3->canmanage = true;
1440 $plan3->canreview = true;
1441 $plan3->isreopenallowed = true;
1443 $this->assertEquals((array)$plan1, external::read_plan($plan1->id));
1444 $this->assertEquals((array)$plan2, external::read_plan($plan2->id));
1445 $this->assertEquals((array)$plan3, external::read_plan($plan3->id));
1448 public function test_delete_plans() {
1449 $this->setUser($this->creator);
1451 $syscontext = context_system::instance();
1453 $plan1 = $this->create_plan(1, $this->user->id, 0, plan::STATUS_ACTIVE, 0);
1454 $plan2 = $this->create_plan(2, $this->user->id, 0, plan::STATUS_ACTIVE, 0);
1455 $plan3 = $this->create_plan(3, $this->creator->id, 0, plan::STATUS_ACTIVE, 0);
1457 $this->assertTrue(external::delete_plan($plan1->id));
1459 unassign_capability('moodle/competency:planmanage', $this->creatorrole, $syscontext->id);
1460 accesslib_clear_all_caches_for_unit_testing();
1462 try {
1463 external::delete_plan($plan2->id);
1464 $this->fail('Exception expected due to not permissions to manage plans');
1465 } catch (moodle_exception $e) {
1466 $this->assertEquals('nopermissions', $e->errorcode);
1469 $this->setUser($this->user);
1471 // Can not delete plans created by other users.
1472 try {
1473 external::delete_plan($plan2->id);
1474 $this->fail('Exception expected due to not permissions to manage plans');
1475 } catch (moodle_exception $e) {
1476 $this->assertEquals('nopermissions', $e->errorcode);
1479 assign_capability('moodle/competency:planmanageown', CAP_ALLOW, $this->userrole, $syscontext->id);
1480 accesslib_clear_all_caches_for_unit_testing();
1482 $this->assertTrue(external::delete_plan($plan2->id));
1484 // Can not delete plans created for other users.
1485 try {
1486 external::delete_plan($plan3->id);
1487 $this->fail('Exception expected due to not permissions to manage plans');
1488 } catch (moodle_exception $e) {
1489 $this->assertEquals('nopermissions', $e->errorcode);
1492 $plan4 = $this->create_plan(4, $this->user->id, 0, plan::STATUS_ACTIVE, 0);
1493 $this->assertTrue(external::delete_plan($plan4->id));
1496 public function test_delete_plan_removes_relations() {
1497 $this->setAdminUser();
1498 $dg = $this->getDataGenerator();
1499 $lpg = $dg->get_plugin_generator('core_competency');
1501 $user = $dg->create_user();
1502 $plan = $lpg->create_plan(array('userid' => $user->id));
1503 $framework = $lpg->create_framework();
1504 $comp1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
1505 $comp2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
1506 $comp3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
1507 $pc1 = $lpg->create_plan_competency(array('planid' => $plan->get('id'), 'competencyid' => $comp1->get('id')));
1508 $pc2 = $lpg->create_plan_competency(array('planid' => $plan->get('id'), 'competencyid' => $comp2->get('id')));
1509 $pc3 = $lpg->create_plan_competency(array('planid' => $plan->get('id'), 'competencyid' => $comp3->get('id')));
1511 // Complete the plan to generate user_competency_plan entries.
1512 api::complete_plan($plan);
1514 // Confirm the data we have.
1515 $this->assertEquals(3, plan_competency::count_records(array('planid' => $plan->get('id'))));
1516 $this->assertEquals(3, user_competency_plan::count_records(array('planid' => $plan->get('id'), 'userid' => $user->id)));
1518 // Delete the plan now.
1519 api::delete_plan($plan->get('id'));
1520 $this->assertEquals(0, plan_competency::count_records(array('planid' => $plan->get('id'))));
1521 $this->assertEquals(0, user_competency_plan::count_records(array('planid' => $plan->get('id'), 'userid' => $user->id)));
1524 public function test_list_plan_competencies() {
1525 $this->setUser($this->creator);
1527 $dg = $this->getDataGenerator();
1528 $lpg = $dg->get_plugin_generator('core_competency');
1530 $f1 = $lpg->create_framework();
1531 $f2 = $lpg->create_framework();
1533 $c1a = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
1534 $c1b = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
1535 $c1c = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
1536 $c2a = $lpg->create_competency(array('competencyframeworkid' => $f2->get('id')));
1537 $c2b = $lpg->create_competency(array('competencyframeworkid' => $f2->get('id')));
1539 $tpl = $lpg->create_template();
1540 $lpg->create_template_competency(array('templateid' => $tpl->get('id'), 'competencyid' => $c1a->get('id')));
1541 $lpg->create_template_competency(array('templateid' => $tpl->get('id'), 'competencyid' => $c1c->get('id')));
1542 $lpg->create_template_competency(array('templateid' => $tpl->get('id'), 'competencyid' => $c2b->get('id')));
1544 $plan = $lpg->create_plan(array('userid' => $this->user->id, 'templateid' => $tpl->get('id')));
1546 $uc1a = $lpg->create_user_competency(array('userid' => $this->user->id, 'competencyid' => $c1a->get('id'),
1547 'status' => user_competency::STATUS_IN_REVIEW, 'reviewerid' => $this->creator->id));
1548 $uc1b = $lpg->create_user_competency(array('userid' => $this->user->id, 'competencyid' => $c1b->get('id')));
1549 $uc2b = $lpg->create_user_competency(array('userid' => $this->user->id, 'competencyid' => $c2b->get('id'),
1550 'grade' => 2, 'proficiency' => 1));
1551 $ux1a = $lpg->create_user_competency(array('userid' => $this->creator->id, 'competencyid' => $c1a->get('id')));
1553 $result = external::list_plan_competencies($plan->get('id'));
1554 $result = external::clean_returnvalue(external::list_plan_competencies_returns(), $result);
1556 $this->assertCount(3, $result);
1557 $this->assertEquals($c1a->get('id'), $result[0]['competency']['id']);
1558 $this->assertEquals($this->user->id, $result[0]['usercompetency']['userid']);
1559 $this->assertArrayNotHasKey('usercompetencyplan', $result[0]);
1560 $this->assertEquals($c1c->get('id'), $result[1]['competency']['id']);
1561 $this->assertEquals($this->user->id, $result[1]['usercompetency']['userid']);
1562 $this->assertArrayNotHasKey('usercompetencyplan', $result[1]);
1563 $this->assertEquals($c2b->get('id'), $result[2]['competency']['id']);
1564 $this->assertEquals($this->user->id, $result[2]['usercompetency']['userid']);
1565 $this->assertArrayNotHasKey('usercompetencyplan', $result[2]);
1566 $this->assertEquals(user_competency::STATUS_IN_REVIEW, $result[0]['usercompetency']['status']);
1567 $this->assertEquals(null, $result[1]['usercompetency']['grade']);
1568 $this->assertEquals(2, $result[2]['usercompetency']['grade']);
1569 $this->assertEquals(1, $result[2]['usercompetency']['proficiency']);
1571 // Check the return values when the plan status is complete.
1572 $completedplan = $lpg->create_plan(array('userid' => $this->user->id, 'templateid' => $tpl->get('id'),
1573 'status' => plan::STATUS_COMPLETE));
1575 $uc1a = $lpg->create_user_competency_plan(array('userid' => $this->user->id, 'competencyid' => $c1a->get('id'),
1576 'planid' => $completedplan->get('id')));
1577 $uc1b = $lpg->create_user_competency_plan(array('userid' => $this->user->id, 'competencyid' => $c1c->get('id'),
1578 'planid' => $completedplan->get('id')));
1579 $uc2b = $lpg->create_user_competency_plan(array('userid' => $this->user->id, 'competencyid' => $c2b->get('id'),
1580 'planid' => $completedplan->get('id'), 'grade' => 2, 'proficiency' => 1));
1581 $ux1a = $lpg->create_user_competency_plan(array('userid' => $this->creator->id, 'competencyid' => $c1a->get('id'),
1582 'planid' => $completedplan->get('id')));
1584 $result = external::list_plan_competencies($completedplan->get('id'));
1585 $result = external::clean_returnvalue(external::list_plan_competencies_returns(), $result);
1587 $this->assertCount(3, $result);
1588 $this->assertEquals($c1a->get('id'), $result[0]['competency']['id']);
1589 $this->assertEquals($this->user->id, $result[0]['usercompetencyplan']['userid']);
1590 $this->assertArrayNotHasKey('usercompetency', $result[0]);
1591 $this->assertEquals($c1c->get('id'), $result[1]['competency']['id']);
1592 $this->assertEquals($this->user->id, $result[1]['usercompetencyplan']['userid']);
1593 $this->assertArrayNotHasKey('usercompetency', $result[1]);
1594 $this->assertEquals($c2b->get('id'), $result[2]['competency']['id']);
1595 $this->assertEquals($this->user->id, $result[2]['usercompetencyplan']['userid']);
1596 $this->assertArrayNotHasKey('usercompetency', $result[2]);
1597 $this->assertEquals(null, $result[1]['usercompetencyplan']['grade']);
1598 $this->assertEquals(2, $result[2]['usercompetencyplan']['grade']);
1599 $this->assertEquals(1, $result[2]['usercompetencyplan']['proficiency']);
1602 public function test_add_competency_to_template() {
1603 $this->setUser($this->creator);
1605 $syscontext = context_system::instance();
1607 // Create a template.
1608 $template = $this->create_template(1, true);
1610 // Create a competency.
1611 $framework = $this->create_competency_framework(1, true);
1612 $competency = $this->create_competency(1, $framework->id);
1614 // Add the competency.
1615 external::add_competency_to_template($template->id, $competency->id);
1617 // Check that it was added.
1618 $this->assertEquals(1, external::count_competencies_in_template($template->id));
1620 // Unassign capability.
1621 unassign_capability('moodle/competency:templatemanage', $this->creatorrole, $syscontext->id);
1622 accesslib_clear_all_caches_for_unit_testing();
1624 // Check we can not add the competency now.
1625 try {
1626 external::add_competency_to_template($template->id, $competency->id);
1627 $this->fail('Exception expected due to not permissions to manage template competencies');
1628 } catch (moodle_exception $e) {
1629 $this->assertEquals('nopermissions', $e->errorcode);
1633 public function test_remove_competency_from_template() {
1634 $syscontext = context_system::instance();
1635 $this->setUser($this->creator);
1636 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1638 // Create a template.
1639 $template = $this->create_template(1, true);
1641 // Create a competency.
1642 $framework = $lpg->create_framework();
1643 $competency = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
1645 // Add the competency.
1646 external::add_competency_to_template($template->id, $competency->get('id'));
1648 // Check that it was added.
1649 $this->assertEquals(1, external::count_competencies_in_template($template->id));
1651 // Check that we can remove the competency.
1652 external::remove_competency_from_template($template->id, $competency->get('id'));
1654 // Check that it was removed.
1655 $this->assertEquals(0, external::count_competencies_in_template($template->id));
1657 // Unassign capability.
1658 unassign_capability('moodle/competency:templatemanage', $this->creatorrole, $syscontext->id);
1659 accesslib_clear_all_caches_for_unit_testing();
1661 // Check we can not remove the competency now.
1662 try {
1663 external::add_competency_to_template($template->id, $competency->get('id'));
1664 $this->fail('Exception expected due to not permissions to manage template competencies');
1665 } catch (moodle_exception $e) {
1666 $this->assertEquals('nopermissions', $e->errorcode);
1671 * Test we can re-order competency frameworks.
1673 public function test_reorder_template_competencies() {
1674 $this->setUser($this->creator);
1676 $syscontext = context_system::instance();
1677 $onehour = time() + 60 * 60;
1679 // Create a template.
1680 $template = $this->create_template(1, true);
1682 // Create a competency framework.
1683 $framework = $this->create_competency_framework(1, true);
1685 // Create multiple competencies.
1686 $competency1 = $this->create_competency(1, $framework->id);
1687 $competency2 = $this->create_competency(2, $framework->id);
1688 $competency3 = $this->create_competency(3, $framework->id);
1689 $competency4 = $this->create_competency(4, $framework->id);
1691 // Add the competencies.
1692 external::add_competency_to_template($template->id, $competency1->id);
1693 external::add_competency_to_template($template->id, $competency2->id);
1694 external::add_competency_to_template($template->id, $competency3->id);
1695 external::add_competency_to_template($template->id, $competency4->id);
1697 // Test if removing competency from template don't create sortorder holes.
1698 external::remove_competency_from_template($template->id, $competency3->id);
1699 $templcomp4 = template_competency::get_record(array(
1700 'templateid' => $template->id,
1701 'competencyid' => $competency4->id
1704 $this->assertEquals(2, $templcomp4->get('sortorder'));
1706 // This is a move up.
1707 external::reorder_template_competency($template->id, $competency4->id, $competency2->id);
1708 $result = external::list_competencies_in_template($template->id);
1709 $result = external_api::clean_returnvalue(external::list_competencies_in_template_returns(), $result);
1711 $r1 = (object) $result[0];
1712 $r2 = (object) $result[1];
1713 $r3 = (object) $result[2];
1715 $this->assertEquals($competency1->id, $r1->id);
1716 $this->assertEquals($competency4->id, $r2->id);
1717 $this->assertEquals($competency2->id, $r3->id);
1719 // This is a move down.
1720 external::reorder_template_competency($template->id, $competency1->id, $competency4->id);
1721 $result = external::list_competencies_in_template($template->id);
1722 $result = external_api::clean_returnvalue(external::list_competencies_in_template_returns(), $result);
1724 $r1 = (object) $result[0];
1725 $r2 = (object) $result[1];
1726 $r3 = (object) $result[2];
1728 $this->assertEquals($competency4->id, $r1->id);
1729 $this->assertEquals($competency1->id, $r2->id);
1730 $this->assertEquals($competency2->id, $r3->id);
1732 $this->expectException('required_capability_exception');
1733 $this->setUser($this->user);
1734 external::reorder_template_competency($template->id, $competency1->id, $competency2->id);
1738 * Test we can duplicate learning plan template.
1740 public function test_duplicate_learning_plan_template() {
1741 $this->setUser($this->creator);
1743 $syscontext = context_system::instance();
1744 $onehour = time() + 60 * 60;
1746 // Create a template.
1747 $template = $this->create_template(1, true);
1749 // Create a competency framework.
1750 $framework = $this->create_competency_framework(1, true);
1752 // Create multiple competencies.
1753 $competency1 = $this->create_competency(1, $framework->id);
1754 $competency2 = $this->create_competency(2, $framework->id);
1755 $competency3 = $this->create_competency(3, $framework->id);
1757 // Add the competencies.
1758 external::add_competency_to_template($template->id, $competency1->id);
1759 external::add_competency_to_template($template->id, $competency2->id);
1760 external::add_competency_to_template($template->id, $competency3->id);
1762 // Duplicate the learning plan template.
1763 $duplicatedtemplate = external::duplicate_template($template->id);
1765 $result = external::list_competencies_in_template($template->id);
1766 $resultduplicated = external::list_competencies_in_template($duplicatedtemplate->id);
1768 $this->assertEquals(count($result), count($resultduplicated));
1769 $this->assertContains($template->shortname, $duplicatedtemplate->shortname);
1770 $this->assertEquals($duplicatedtemplate->description, $template->description);
1771 $this->assertEquals($duplicatedtemplate->descriptionformat, $template->descriptionformat);
1772 $this->assertEquals($duplicatedtemplate->visible, $template->visible);
1776 * Test that we can return scale values for a scale with the scale ID.
1778 public function test_get_scale_values() {
1779 global $DB;
1781 $this->setUser($this->creator);
1783 // Create a scale.
1784 $record = new stdClass();
1785 $record->courseid = 0;
1786 $record->userid = $this->creator->id;
1787 $record->name = 'Test scale';
1788 $record->scale = 'Poor, Not good, Okay, Fine, Excellent';
1789 $record->description = '<p>Test scale description.</p>';
1790 $record->descriptionformat = 1;
1791 $record->timemodified = time();
1792 $scaleid = $DB->insert_record('scale', $record);
1793 // Expected return value.
1794 $expected = array(array(
1795 'id' => 1,
1796 'name' => 'Poor'
1797 ), array(
1798 'id' => 2,
1799 'name' => 'Not good'
1800 ), array(
1801 'id' => 3,
1802 'name' => 'Okay'
1803 ), array(
1804 'id' => 4,
1805 'name' => 'Fine'
1806 ), array(
1807 'id' => 5,
1808 'name' => 'Excellent'
1811 // Call the webservice.
1812 $result = external::get_scale_values($scaleid);
1813 $this->assertEquals($expected, $result);
1817 * Create a template.
1819 public function test_create_template() {
1820 $syscontextid = context_system::instance()->id;
1821 $catcontextid = context_coursecat::instance($this->category->id)->id;
1823 // A user without permission.
1824 $this->setUser($this->user);
1825 try {
1826 $result = $this->create_template(1, true);
1827 $this->fail('Invalid permissions');
1828 } catch (required_capability_exception $e) {
1829 // All good.
1832 // A user without permission in a category.
1833 $this->setUser($this->catuser);
1834 try {
1835 $result = $this->create_template(1, false);
1836 $this->fail('Invalid permissions');
1837 } catch (required_capability_exception $e) {
1838 // All good.
1841 // A user with permissions in the system.
1842 $this->setUser($this->creator);
1843 $result = $this->create_template(1, true);
1844 $this->assertEquals('shortname1', $result->shortname);
1845 $this->assertEquals($syscontextid, $result->contextid);
1846 $this->assertNotEmpty($result->id);
1848 $result = $this->create_template(2, false);
1849 $this->assertEquals('shortname2', $result->shortname);
1850 $this->assertEquals($catcontextid, $result->contextid);
1851 $this->assertNotEmpty($result->id);
1853 // A user with permissions in the category.
1854 $this->setUser($this->catcreator);
1855 try {
1856 $result = $this->create_template(3, true);
1857 $this->fail('Invalid permissions');
1858 } catch (required_capability_exception $e) {
1859 // All good.
1862 $result = $this->create_template(3, false);
1863 $this->assertEquals('shortname3', $result->shortname);
1864 $this->assertEquals($catcontextid, $result->contextid);
1865 $this->assertNotEmpty($result->id);
1869 * Read a template.
1871 public function test_read_template() {
1872 $syscontextid = context_system::instance()->id;
1873 $catcontextid = context_coursecat::instance($this->category->id)->id;
1875 // Set a due date for the next year.
1876 $date = new DateTime('now');
1877 $date->modify('+1 year');
1878 $duedate = $date->getTimestamp();
1880 // Creating two templates.
1881 $this->setUser($this->creator);
1882 $systemplate = $this->create_template(1, true);
1883 $cattemplate = $this->create_template(2, false);
1885 // User without permissions to read in system.
1886 assign_capability('moodle/competency:templateview', CAP_PROHIBIT, $this->userrole, $syscontextid, true);
1887 accesslib_clear_all_caches_for_unit_testing();
1888 $this->setUser($this->user);
1889 $this->assertFalse(has_capability('moodle/competency:templateview', context_system::instance()));
1890 try {
1891 external::read_template($systemplate->id);
1892 $this->fail('Invalid permissions');
1893 } catch (required_capability_exception $e) {
1894 // All good.
1896 try {
1897 external::read_template($cattemplate->id);
1898 $this->fail('Invalid permissions');
1899 } catch (required_capability_exception $e) {
1900 // All good.
1903 // User with permissions to read in a category.
1904 assign_capability('moodle/competency:templateview', CAP_PREVENT, $this->userrole, $syscontextid, true);
1905 assign_capability('moodle/competency:templateview', CAP_ALLOW, $this->userrole, $catcontextid, true);
1906 accesslib_clear_all_caches_for_unit_testing();
1907 $this->assertFalse(has_capability('moodle/competency:templateview', context_system::instance()));
1908 $this->assertTrue(has_capability('moodle/competency:templateview', context_coursecat::instance($this->category->id)));
1909 try {
1910 external::read_template($systemplate->id);
1911 $this->fail('Invalid permissions');
1912 } catch (required_capability_exception $e) {
1913 // All good.
1916 $result = external::read_template($cattemplate->id);
1917 $result = external_api::clean_returnvalue(external::read_template_returns(), $result);
1918 $this->assertEquals($cattemplate->id, $result['id']);
1919 $this->assertEquals('shortname2', $result['shortname']);
1920 $this->assertEquals('description2', $result['description']);
1921 $this->assertEquals(FORMAT_HTML, $result['descriptionformat']);
1922 $this->assertEquals(1, $result['visible']);
1923 $this->assertEquals(0, $result['duedate']);
1924 $this->assertEquals(userdate(0), $result['duedateformatted']);
1926 // User with permissions to read in the system.
1927 assign_capability('moodle/competency:templateview', CAP_ALLOW, $this->userrole, $syscontextid, true);
1928 accesslib_clear_all_caches_for_unit_testing();
1929 $this->assertTrue(has_capability('moodle/competency:templateview', context_system::instance()));
1930 $result = external::read_template($systemplate->id);
1931 $result = external_api::clean_returnvalue(external::read_template_returns(), $result);
1932 $this->assertEquals($systemplate->id, $result['id']);
1933 $this->assertEquals('shortname1', $result['shortname']);
1934 $this->assertEquals('description1', $result['description']);
1935 $this->assertEquals(FORMAT_HTML, $result['descriptionformat']);
1936 $this->assertEquals(true, $result['visible']);
1937 $this->assertEquals(0, $result['duedate']);
1938 $this->assertEquals(userdate(0), $result['duedateformatted']);
1940 $result = external::read_template($cattemplate->id);
1941 $result = external_api::clean_returnvalue(external::read_template_returns(), $result);
1942 $this->assertEquals($cattemplate->id, $result['id']);
1943 $this->assertEquals('shortname2', $result['shortname']);
1944 $this->assertEquals('description2', $result['description']);
1945 $this->assertEquals(FORMAT_HTML, $result['descriptionformat']);
1946 $this->assertEquals(true, $result['visible']);
1947 $this->assertEquals(0, $result['duedate']);
1948 $this->assertEquals(userdate(0), $result['duedateformatted']);
1952 * Update a template.
1954 public function test_update_template() {
1955 $syscontextid = context_system::instance()->id;
1956 $catcontextid = context_coursecat::instance($this->category->id)->id;
1958 // Set a due date for the next year.
1959 $date = new DateTime('now');
1960 $date->modify('+1 year');
1961 $duedate = $date->getTimestamp();
1963 // Creating two templates.
1964 $this->setUser($this->creator);
1965 $systemplate = $this->create_template(1, true);
1966 $cattemplate = $this->create_template(2, false);
1968 // Trying to update in a without permissions.
1969 $this->setUser($this->user);
1970 try {
1971 $this->update_template($systemplate->id, 3);
1972 $this->fail('Invalid permissions');
1973 } catch (required_capability_exception $e) {
1974 // All good.
1977 try {
1978 $this->update_template($cattemplate->id, 3);
1979 $this->fail('Invalid permissions');
1980 } catch (required_capability_exception $e) {
1981 // All good.
1984 // User with permissions to update in category.
1985 $this->setUser($this->catcreator);
1986 try {
1987 $this->update_template($systemplate->id, 3);
1988 $this->fail('Invalid permissions');
1989 } catch (required_capability_exception $e) {
1990 // All good.
1993 $result = $this->update_template($cattemplate->id, 3);
1994 $this->assertTrue($result);
1995 $result = external::read_template($cattemplate->id);
1996 $result = external_api::clean_returnvalue(external::read_template_returns(), $result);
1997 $this->assertEquals($cattemplate->id, $result['id']);
1998 $this->assertEquals('shortname3', $result['shortname']);
1999 $this->assertEquals("description3", $result['description']);
2000 $this->assertEquals(FORMAT_HTML, $result['descriptionformat']);
2001 $this->assertEquals(true, $result['visible']);
2002 $this->assertEquals(0, $result['duedate']);
2003 $this->assertEquals(userdate(0), $result['duedateformatted']);
2005 // User with permissions to update in the system.
2006 $this->setUser($this->creator);
2007 $result = $this->update_template($systemplate->id, 4);
2008 $this->assertTrue($result);
2009 $result = external::read_template($systemplate->id);
2010 $result = external_api::clean_returnvalue(external::read_template_returns(), $result);
2011 $this->assertEquals($systemplate->id, $result['id']);
2012 $this->assertEquals('shortname4', $result['shortname']);
2013 $this->assertEquals('description4', $result['description']);
2014 $this->assertEquals(FORMAT_HTML, $result['descriptionformat']);
2015 $this->assertEquals(true, $result['visible']);
2016 $this->assertEquals(0, $result['duedate']);
2017 $this->assertEquals(userdate(0), $result['duedateformatted']);
2019 $result = $this->update_template($cattemplate->id, 5);
2020 $this->assertTrue($result);
2021 $result = external::read_template($cattemplate->id);
2022 $result = external_api::clean_returnvalue(external::read_template_returns(), $result);
2023 $this->assertEquals($cattemplate->id, $result['id']);
2024 $this->assertEquals('shortname5', $result['shortname']);
2025 $this->assertEquals('description5', $result['description']);
2026 $this->assertEquals(FORMAT_HTML, $result['descriptionformat']);
2027 $this->assertEquals(1, $result['visible']);
2028 $this->assertEquals(0, $result['duedate']);
2029 $this->assertEquals(userdate(0), $result['duedateformatted']);
2033 * Delete a template.
2035 public function test_delete_template() {
2036 global $DB;
2037 $syscontextid = context_system::instance()->id;
2038 $catcontextid = context_coursecat::instance($this->category->id)->id;
2040 // Creating a few templates.
2041 $this->setUser($this->creator);
2042 $sys1 = $this->create_template(1, true);
2043 $cat1 = $this->create_template(2, false);
2044 $cat2 = $this->create_template(3, false);
2045 $this->assertTrue($DB->record_exists(template::TABLE, array('id' => $sys1->id)));
2046 $this->assertTrue($DB->record_exists(template::TABLE, array('id' => $cat1->id)));
2047 $this->assertTrue($DB->record_exists(template::TABLE, array('id' => $cat2->id)));
2049 // User without permissions.
2050 $this->setUser($this->user);
2051 try {
2052 external::delete_template($sys1->id);
2053 $this->fail('Invalid permissions');
2054 } catch (required_capability_exception $e) {
2055 // All good.
2057 try {
2058 external::delete_template($cat1->id);
2059 $this->fail('Invalid permissions');
2060 } catch (required_capability_exception $e) {
2061 // All good.
2064 // User with category permissions.
2065 $this->setUser($this->catcreator);
2066 try {
2067 external::delete_template($sys1->id);
2068 $this->fail('Invalid permissions');
2069 } catch (required_capability_exception $e) {
2070 // All good.
2073 $result = external::delete_template($cat1->id);
2074 $result = external_api::clean_returnvalue(external::delete_template_returns(), $result);
2075 $this->assertTrue($result);
2076 $this->assertFalse($DB->record_exists(template::TABLE, array('id' => $cat1->id)));
2078 // User with system permissions.
2079 $this->setUser($this->creator);
2080 $result = external::delete_template($sys1->id);
2081 $result = external_api::clean_returnvalue(external::delete_template_returns(), $result);
2082 $this->assertTrue($result);
2083 $result = external::delete_template($cat2->id);
2084 $result = external_api::clean_returnvalue(external::delete_template_returns(), $result);
2085 $this->assertTrue($result);
2086 $this->assertFalse($DB->record_exists(template::TABLE, array('id' => $sys1->id)));
2087 $this->assertFalse($DB->record_exists(template::TABLE, array('id' => $cat2->id)));
2091 * List templates.
2093 public function test_list_templates() {
2094 $syscontextid = context_system::instance()->id;
2095 $catcontextid = context_coursecat::instance($this->category->id)->id;
2097 // Creating a few templates.
2098 $this->setUser($this->creator);
2099 $sys1 = $this->create_template(1, true);
2100 $sys2 = $this->create_template(2, true);
2101 $cat1 = $this->create_template(3, false);
2102 $cat2 = $this->create_template(4, false);
2104 // User without permission.
2105 $this->setUser($this->user);
2106 assign_capability('moodle/competency:templateview', CAP_PROHIBIT, $this->userrole, $syscontextid, true);
2107 accesslib_clear_all_caches_for_unit_testing();
2108 try {
2109 external::list_templates('id', 'ASC', 0, 10, array('contextid' => $syscontextid), 'children', false);
2110 $this->fail('Invalid permissions');
2111 } catch (required_capability_exception $e) {
2112 // All good.
2115 // User with category permissions.
2116 assign_capability('moodle/competency:templateview', CAP_PREVENT, $this->userrole, $syscontextid, true);
2117 assign_capability('moodle/competency:templateview', CAP_ALLOW, $this->userrole, $catcontextid, true);
2118 accesslib_clear_all_caches_for_unit_testing();
2119 $result = external::list_templates('id', 'ASC', 0, 10, array('contextid' => $syscontextid), 'children', false);
2120 $result = external_api::clean_returnvalue(external::list_templates_returns(), $result);
2121 $this->assertCount(2, $result);
2122 $this->assertEquals($cat1->id, $result[0]['id']);
2123 $this->assertEquals($cat2->id, $result[1]['id']);
2125 // User with system permissions.
2126 assign_capability('moodle/competency:templateview', CAP_ALLOW, $this->userrole, $syscontextid, true);
2127 accesslib_clear_all_caches_for_unit_testing();
2128 $result = external::list_templates('id', 'DESC', 0, 3, array('contextid' => $catcontextid), 'parents', false);
2129 $result = external_api::clean_returnvalue(external::list_templates_returns(), $result);
2130 $this->assertCount(3, $result);
2131 $this->assertEquals($cat2->id, $result[0]['id']);
2132 $this->assertEquals($cat1->id, $result[1]['id']);
2133 $this->assertEquals($sys2->id, $result[2]['id']);
2137 * List templates using competency.
2139 public function test_list_templates_using_competency() {
2140 $this->setUser($this->creator);
2142 // Create a template.
2143 $template1 = $this->create_template(1, true);
2144 $template2 = $this->create_template(2, true);
2145 $template3 = $this->create_template(3, true);
2146 $template4 = $this->create_template(4, true);
2148 // Create a competency.
2149 $framework = $this->create_competency_framework(1, true);
2150 $competency1 = $this->create_competency(1, $framework->id);
2151 $competency2 = $this->create_competency(2, $framework->id);
2153 // Add the competency.
2154 external::add_competency_to_template($template1->id, $competency1->id);
2155 external::add_competency_to_template($template2->id, $competency1->id);
2156 external::add_competency_to_template($template3->id, $competency1->id);
2158 external::add_competency_to_template($template4->id, $competency2->id);
2160 $listcomp1 = external::list_templates_using_competency($competency1->id);
2161 $listcomp2 = external::list_templates_using_competency($competency2->id);
2163 // Test count_templates_using_competency.
2164 $counttempcomp1 = external::count_templates_using_competency($competency1->id);
2165 $counttempcomp2 = external::count_templates_using_competency($competency2->id);
2167 $comptemp1 = $listcomp1[0];
2168 $comptemp2 = $listcomp1[1];
2169 $comptemp3 = $listcomp1[2];
2171 $comptemp4 = $listcomp2[0];
2173 $this->assertCount(3, $listcomp1);
2174 $this->assertCount(1, $listcomp2);
2175 $this->assertEquals(3, $counttempcomp1);
2176 $this->assertEquals(1, $counttempcomp2);
2177 $this->assertEquals($template1->id, $comptemp1->id);
2178 $this->assertEquals($template2->id, $comptemp2->id);
2179 $this->assertEquals($template3->id, $comptemp3->id);
2180 $this->assertEquals($template4->id, $comptemp4->id);
2183 public function test_count_templates() {
2184 $syscontextid = context_system::instance()->id;
2185 $catcontextid = context_coursecat::instance($this->category->id)->id;
2187 // Creating a few templates.
2188 $this->setUser($this->creator);
2189 $sys1 = $this->create_template(1, true);
2190 $sys2 = $this->create_template(2, true);
2191 $cat1 = $this->create_template(3, false);
2192 $cat2 = $this->create_template(4, false);
2193 $cat3 = $this->create_template(5, false);
2195 // User without permission.
2196 $this->setUser($this->user);
2197 assign_capability('moodle/competency:templateview', CAP_PROHIBIT, $this->userrole, $syscontextid, true);
2198 accesslib_clear_all_caches_for_unit_testing();
2199 try {
2200 external::count_templates(array('contextid' => $syscontextid), 'children');
2201 $this->fail('Invalid permissions');
2202 } catch (required_capability_exception $e) {
2203 // All good.
2206 // User with category permissions.
2207 assign_capability('moodle/competency:templateview', CAP_PREVENT, $this->userrole, $syscontextid, true);
2208 assign_capability('moodle/competency:templateview', CAP_ALLOW, $this->userrole, $catcontextid, true);
2209 accesslib_clear_all_caches_for_unit_testing();
2210 $result = external::count_templates(array('contextid' => $syscontextid), 'children');
2211 $result = external_api::clean_returnvalue(external::count_templates_returns(), $result);
2212 $this->assertEquals(3, $result);
2214 // User with system permissions.
2215 assign_capability('moodle/competency:templateview', CAP_ALLOW, $this->userrole, $syscontextid, true);
2216 accesslib_clear_all_caches_for_unit_testing();
2217 $result = external::count_templates(array('contextid' => $catcontextid), 'parents');
2218 $result = external_api::clean_returnvalue(external::count_templates_returns(), $result);
2219 $this->assertEquals(5, $result);
2223 * Test that we can add related competencies.
2225 * @return void
2227 public function test_add_related_competency() {
2228 global $DB;
2229 $this->setUser($this->creator);
2231 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
2232 $framework = $lpg->create_framework();
2233 $framework2 = $lpg->create_framework();
2234 $competency1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2235 $competency2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2236 $competency3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2237 $competency4 = $lpg->create_competency(array('competencyframeworkid' => $framework2->get('id')));
2239 // The lower one always as competencyid.
2240 $result = external::add_related_competency($competency1->get('id'), $competency2->get('id'));
2241 $result = external_api::clean_returnvalue(external::add_related_competency_returns(), $result);
2242 $this->assertTrue($result);
2243 $this->assertTrue($DB->record_exists_select(
2244 related_competency::TABLE, 'competencyid = :cid AND relatedcompetencyid = :rid',
2245 array(
2246 'cid' => $competency1->get('id'),
2247 'rid' => $competency2->get('id')
2250 $this->assertFalse($DB->record_exists_select(
2251 related_competency::TABLE, 'competencyid = :cid AND relatedcompetencyid = :rid',
2252 array(
2253 'cid' => $competency2->get('id'),
2254 'rid' => $competency1->get('id')
2258 $result = external::add_related_competency($competency3->get('id'), $competency1->get('id'));
2259 $result = external_api::clean_returnvalue(external::add_related_competency_returns(), $result);
2260 $this->assertTrue($result);
2261 $this->assertTrue($DB->record_exists_select(
2262 related_competency::TABLE, 'competencyid = :cid AND relatedcompetencyid = :rid',
2263 array(
2264 'cid' => $competency1->get('id'),
2265 'rid' => $competency3->get('id')
2268 $this->assertFalse($DB->record_exists_select(
2269 related_competency::TABLE, 'competencyid = :cid AND relatedcompetencyid = :rid',
2270 array(
2271 'cid' => $competency3->get('id'),
2272 'rid' => $competency1->get('id')
2276 // We can not allow a duplicate relation, not even in the other direction.
2277 $this->assertEquals(1, $DB->count_records_select(related_competency::TABLE,
2278 'competencyid = :cid AND relatedcompetencyid = :rid',
2279 array('cid' => $competency1->get('id'), 'rid' => $competency2->get('id'))));
2280 $this->assertEquals(0, $DB->count_records_select(related_competency::TABLE,
2281 'competencyid = :cid AND relatedcompetencyid = :rid',
2282 array('rid' => $competency1->get('id'), 'cid' => $competency2->get('id'))));
2283 $result = external::add_related_competency($competency2->get('id'), $competency1->get('id'));
2284 $result = external_api::clean_returnvalue(external::add_related_competency_returns(), $result);
2285 $this->assertTrue($result);
2286 $this->assertEquals(1, $DB->count_records_select(related_competency::TABLE,
2287 'competencyid = :cid AND relatedcompetencyid = :rid',
2288 array('cid' => $competency1->get('id'), 'rid' => $competency2->get('id'))));
2289 $this->assertEquals(0, $DB->count_records_select(related_competency::TABLE,
2290 'competencyid = :cid AND relatedcompetencyid = :rid',
2291 array('rid' => $competency1->get('id'), 'cid' => $competency2->get('id'))));
2293 // Check that we cannot create links across frameworks.
2294 try {
2295 external::add_related_competency($competency1->get('id'), $competency4->get('id'));
2296 $this->fail('Exception expected due mis-use of shared competencies');
2297 } catch (invalid_persistent_exception $e) {
2298 // Yay!
2301 // User without permission.
2302 $this->setUser($this->user);
2304 // Check we can not add the related competency now.
2305 try {
2306 external::add_related_competency($competency1->get('id'), $competency3->get('id'));
2307 $this->fail('Exception expected due to not permissions to manage template competencies');
2308 } catch (moodle_exception $e) {
2309 $this->assertEquals('nopermissions', $e->errorcode);
2315 * Test that we can remove related competencies.
2317 * @return void
2319 public function test_remove_related_competency() {
2320 $this->setUser($this->creator);
2322 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
2323 $framework = $lpg->create_framework();
2324 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2325 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2326 $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2327 $rc1 = $lpg->create_related_competency(array('competencyid' => $c1->get('id'), 'relatedcompetencyid' => $c2->get('id')));
2328 $rc2 = $lpg->create_related_competency(array('competencyid' => $c2->get('id'), 'relatedcompetencyid' => $c3->get('id')));
2330 $this->assertEquals(2, related_competency::count_records());
2332 // Returns false when the relation does not exist.
2333 $result = external::remove_related_competency($c1->get('id'), $c3->get('id'));
2334 $result = external_api::clean_returnvalue(external::remove_related_competency_returns(), $result);
2335 $this->assertFalse($result);
2337 // Returns true on success.
2338 $result = external::remove_related_competency($c2->get('id'), $c3->get('id'));
2339 $result = external_api::clean_returnvalue(external::remove_related_competency_returns(), $result);
2340 $this->assertTrue($result);
2341 $this->assertEquals(1, related_competency::count_records());
2343 // We don't need to specify competencyid and relatedcompetencyid in the right order.
2344 $result = external::remove_related_competency($c2->get('id'), $c1->get('id'));
2345 $result = external_api::clean_returnvalue(external::remove_related_competency_returns(), $result);
2346 $this->assertTrue($result);
2347 $this->assertEquals(0, related_competency::count_records());
2351 * Test that we can search and include related competencies.
2353 * @return void
2355 public function test_search_competencies_including_related() {
2356 $this->setUser($this->creator);
2358 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
2359 $framework = $lpg->create_framework();
2360 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2361 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2362 $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2363 $c4 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2364 $c5 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2366 // We have 1-2, 1-3, 2-4, and no relation between 2-3 nor 1-4 nor 5.
2367 $rc12 = $lpg->create_related_competency(array('competencyid' => $c1->get('id'), 'relatedcompetencyid' => $c2->get('id')));
2368 $rc13 = $lpg->create_related_competency(array('competencyid' => $c1->get('id'), 'relatedcompetencyid' => $c3->get('id')));
2369 $rc24 = $lpg->create_related_competency(array('competencyid' => $c2->get('id'), 'relatedcompetencyid' => $c4->get('id')));
2371 $result = external::search_competencies('comp', $framework->get('id'), true);
2372 $result = external_api::clean_returnvalue(external::search_competencies_returns(), $result);
2374 $this->assertCount(5, $result);
2379 * Test that we can add competency to plan if we have the right capability.
2381 * @return void
2383 public function test_add_competency_to_plan() {
2384 $this->resetAfterTest(true);
2385 $dg = $this->getDataGenerator();
2386 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
2387 $usermanage = $dg->create_user();
2388 $user = $dg->create_user();
2390 $syscontext = context_system::instance();
2392 // Creating specific roles.
2393 $managerole = $dg->create_role(array(
2394 'name' => 'User manage',
2395 'shortname' => 'manage'
2398 assign_capability('moodle/competency:planmanage', CAP_ALLOW, $managerole, $syscontext->id);
2399 assign_capability('moodle/competency:planview', CAP_ALLOW, $managerole, $syscontext->id);
2401 $dg->role_assign($managerole, $usermanage->id, $syscontext->id);
2403 $this->setUser($usermanage);
2404 $plan = array (
2405 'userid' => $usermanage->id,
2406 'status' => \core_competency\plan::STATUS_ACTIVE
2408 $pl1 = $lpg->create_plan($plan);
2409 $framework = $lpg->create_framework();
2410 $competency = $lpg->create_competency(
2411 array('competencyframeworkid' => $framework->get('id'))
2413 $this->assertTrue(external::add_competency_to_plan($pl1->get('id'), $competency->get('id')));
2415 // A competency cannot be added to plan based on template.
2416 $template = $lpg->create_template();
2417 $plan = array (
2418 'userid' => $usermanage->id,
2419 'status' => \core_competency\plan::STATUS_ACTIVE,
2420 'templateid' => $template->get('id')
2422 $pl2 = $lpg->create_plan($plan);
2423 try {
2424 external::add_competency_to_plan($pl2->get('id'), $competency->get('id'));
2425 $this->fail('A competency cannot be added to plan based on template');
2426 } catch (coding_exception $ex) {
2427 $this->assertTrue(true);
2430 // User without capability cannot add competency to a plan.
2431 $this->setUser($user);
2432 try {
2433 external::add_competency_to_plan($pl1->get('id'), $competency->get('id'));
2434 $this->fail('User without capability cannot add competency to a plan');
2435 } catch (required_capability_exception $ex) {
2436 $this->assertTrue(true);
2441 * Test that we can add competency to plan if we have the right capability.
2443 * @return void
2445 public function test_remove_competency_from_plan() {
2446 $this->resetAfterTest(true);
2447 $dg = $this->getDataGenerator();
2448 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
2449 $usermanage = $dg->create_user();
2450 $user = $dg->create_user();
2452 $syscontext = context_system::instance();
2454 // Creating specific roles.
2455 $managerole = $dg->create_role(array(
2456 'name' => 'User manage',
2457 'shortname' => 'manage'
2460 assign_capability('moodle/competency:planmanage', CAP_ALLOW, $managerole, $syscontext->id);
2461 assign_capability('moodle/competency:planview', CAP_ALLOW, $managerole, $syscontext->id);
2463 $dg->role_assign($managerole, $usermanage->id, $syscontext->id);
2465 $this->setUser($usermanage);
2466 $plan = array (
2467 'userid' => $usermanage->id,
2468 'status' => \core_competency\plan::STATUS_ACTIVE
2470 $pl1 = $lpg->create_plan($plan);
2471 $framework = $lpg->create_framework();
2472 $competency = $lpg->create_competency(
2473 array('competencyframeworkid' => $framework->get('id'))
2475 $lpg->create_plan_competency(
2476 array(
2477 'planid' => $pl1->get('id'),
2478 'competencyid' => $competency->get('id')
2481 $this->assertTrue(external::remove_competency_from_plan($pl1->get('id'), $competency->get('id')));
2482 $this->assertCount(0, $pl1->get_competencies());
2486 * Test that we can add competency to plan if we have the right capability.
2488 * @return void
2490 public function test_reorder_plan_competency() {
2491 $this->resetAfterTest(true);
2492 $dg = $this->getDataGenerator();
2493 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
2494 $usermanage = $dg->create_user();
2495 $user = $dg->create_user();
2497 $syscontext = context_system::instance();
2499 // Creating specific roles.
2500 $managerole = $dg->create_role(array(
2501 'name' => 'User manage',
2502 'shortname' => 'manage'
2505 assign_capability('moodle/competency:planmanage', CAP_ALLOW, $managerole, $syscontext->id);
2506 assign_capability('moodle/competency:planview', CAP_ALLOW, $managerole, $syscontext->id);
2508 $dg->role_assign($managerole, $usermanage->id, $syscontext->id);
2510 $this->setUser($usermanage);
2511 $plan = array (
2512 'userid' => $usermanage->id,
2513 'status' => \core_competency\plan::STATUS_ACTIVE
2515 $pl1 = $lpg->create_plan($plan);
2516 $framework = $lpg->create_framework();
2517 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2518 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2519 $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2520 $c4 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2521 $c5 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2523 $lpg->create_plan_competency(array('planid' => $pl1->get('id'), 'competencyid' => $c1->get('id'), 'sortorder' => 1));
2524 $lpg->create_plan_competency(array('planid' => $pl1->get('id'), 'competencyid' => $c2->get('id'), 'sortorder' => 2));
2525 $lpg->create_plan_competency(array('planid' => $pl1->get('id'), 'competencyid' => $c3->get('id'), 'sortorder' => 3));
2526 $lpg->create_plan_competency(array('planid' => $pl1->get('id'), 'competencyid' => $c4->get('id'), 'sortorder' => 4));
2527 $lpg->create_plan_competency(array('planid' => $pl1->get('id'), 'competencyid' => $c5->get('id'), 'sortorder' => 5));
2529 // Test if removing competency from plan don't create sortorder holes.
2530 external::remove_competency_from_plan($pl1->get('id'), $c4->get('id'));
2531 $plancomp5 = plan_competency::get_record(array(
2532 'planid' => $pl1->get('id'),
2533 'competencyid' => $c5->get('id')
2536 $this->assertEquals(3, $plancomp5->get('sortorder'));
2538 $this->assertTrue(external::reorder_plan_competency($pl1->get('id'), $c2->get('id'), $c5->get('id')));
2539 $this->assertTrue(external::reorder_plan_competency($pl1->get('id'), $c3->get('id'), $c1->get('id')));
2540 $plancompetencies = plan_competency::get_records(array('planid' => $pl1->get('id')), 'sortorder', 'ASC');
2541 $plcmp1 = $plancompetencies[0];
2542 $plcmp2 = $plancompetencies[1];
2543 $plcmp3 = $plancompetencies[2];
2544 $plcmp4 = $plancompetencies[3];
2546 $this->assertEquals($plcmp1->get('competencyid'), $c3->get('id'));
2547 $this->assertEquals($plcmp2->get('competencyid'), $c1->get('id'));
2548 $this->assertEquals($plcmp3->get('competencyid'), $c5->get('id'));
2549 $this->assertEquals($plcmp4->get('competencyid'), $c2->get('id'));
2553 * Test resolving sortorder when we creating competency.
2555 public function test_fix_sortorder_when_creating_competency() {
2556 $this->resetAfterTest(true);
2557 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
2558 $framework = $lpg->create_framework();
2560 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2561 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'), 'sortorder' => 20));
2562 $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'), 'sortorder' => 1));
2564 $this->assertEquals(0, $c1->get('sortorder'));
2565 $this->assertEquals(1, $c2->get('sortorder'));
2566 $this->assertEquals(2, $c3->get('sortorder'));
2570 * Test resolving sortorder when we delete competency.
2572 public function test_fix_sortorder_when_delete_competency() {
2573 $this->resetAfterTest(true);
2574 $this->setUser($this->creator);
2575 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
2577 $framework = $lpg->create_framework();
2579 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2580 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2581 $c2a = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'), 'parentid' => $c2->get('id')));
2582 $c2b = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'), 'parentid' => $c2->get('id')));
2583 $c2c = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'), 'parentid' => $c2->get('id')));
2584 $c2d = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'), 'parentid' => $c2->get('id')));
2586 $this->assertEquals(0, $c1->get('sortorder'));
2587 $this->assertEquals(1, $c2->get('sortorder'));
2588 $this->assertEquals(0, $c2a->get('sortorder'));
2589 $this->assertEquals(1, $c2b->get('sortorder'));
2590 $this->assertEquals(2, $c2c->get('sortorder'));
2591 $this->assertEquals(3, $c2d->get('sortorder'));
2593 $result = external::delete_competency($c1->get('id'));
2594 $result = external_api::clean_returnvalue(external::delete_competency_returns(), $result);
2596 $c2->read();
2597 $c2a->read();
2598 $c2b->read();
2599 $c2c->read();
2600 $c2d->read();
2602 $this->assertEquals(0, $c2->get('sortorder'));
2603 $this->assertEquals(0, $c2a->get('sortorder'));
2604 $this->assertEquals(1, $c2b->get('sortorder'));
2605 $this->assertEquals(2, $c2c->get('sortorder'));
2606 $this->assertEquals(3, $c2d->get('sortorder'));
2608 $result = external::delete_competency($c2b->get('id'));
2609 $result = external_api::clean_returnvalue(external::delete_competency_returns(), $result);
2611 $c2->read();
2612 $c2a->read();
2613 $c2c->read();
2614 $c2d->read();
2616 $this->assertEquals(0, $c2->get('sortorder'));
2617 $this->assertEquals(0, $c2a->get('sortorder'));
2618 $this->assertEquals(1, $c2c->get('sortorder'));
2619 $this->assertEquals(2, $c2d->get('sortorder'));
2623 * Test resolving sortorder when moving a competency.
2625 public function test_fix_sortorder_when_moving_competency() {
2626 $this->resetAfterTest(true);
2627 $this->setUser($this->creator);
2628 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
2630 $framework = $lpg->create_framework();
2632 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2633 $c1a = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'), 'parentid' => $c1->get('id')));
2634 $c1b = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'), 'parentid' => $c1->get('id')));
2635 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2636 $c2a = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'), 'parentid' => $c2->get('id')));
2637 $c2b = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'), 'parentid' => $c2->get('id')));
2639 $this->assertEquals(0, $c1->get('sortorder'));
2640 $this->assertEquals(0, $c1a->get('sortorder'));
2641 $this->assertEquals(1, $c1b->get('sortorder'));
2642 $this->assertEquals(1, $c2->get('sortorder'));
2643 $this->assertEquals(0, $c2a->get('sortorder'));
2644 $this->assertEquals(1, $c2b->get('sortorder'));
2646 $result = external::set_parent_competency($c2a->get('id'), $c1->get('id'));
2647 $result = external_api::clean_returnvalue(external::set_parent_competency_returns(), $result);
2649 $c1->read();
2650 $c1a->read();
2651 $c1b->read();
2652 $c2->read();
2653 $c2a->read();
2654 $c2b->read();
2656 $this->assertEquals(0, $c1->get('sortorder'));
2657 $this->assertEquals(0, $c1a->get('sortorder'));
2658 $this->assertEquals(1, $c1b->get('sortorder'));
2659 $this->assertEquals(2, $c2a->get('sortorder'));
2660 $this->assertEquals(1, $c2->get('sortorder'));
2661 $this->assertEquals(0, $c2b->get('sortorder'));
2663 // Move a root node.
2664 $result = external::set_parent_competency($c2->get('id'), $c1b->get('id'));
2665 $result = external_api::clean_returnvalue(external::set_parent_competency_returns(), $result);
2667 $c1->read();
2668 $c1a->read();
2669 $c1b->read();
2670 $c2->read();
2671 $c2a->read();
2672 $c2b->read();
2674 $this->assertEquals(0, $c1->get('sortorder'));
2675 $this->assertEquals(0, $c1a->get('sortorder'));
2676 $this->assertEquals(1, $c1b->get('sortorder'));
2677 $this->assertEquals(0, $c2->get('sortorder'));
2678 $this->assertEquals(0, $c2b->get('sortorder'));
2679 $this->assertEquals(2, $c2a->get('sortorder'));
2682 public function test_grade_competency() {
2683 global $CFG;
2685 $this->setUser($this->creator);
2686 $dg = $this->getDataGenerator();
2687 $lpg = $dg->get_plugin_generator('core_competency');
2689 $f1 = $lpg->create_framework();
2690 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
2691 $evidence = external::grade_competency($this->user->id, $c1->get('id'), 1, 'Evil note');
2693 $this->assertEquals('The competency rating was manually set.', $evidence->description);
2694 $this->assertEquals('A', $evidence->gradename);
2695 $this->assertEquals('Evil note', $evidence->note);
2697 $this->setUser($this->user);
2699 $this->expectException('required_capability_exception');
2700 $evidence = external::grade_competency($this->user->id, $c1->get('id'), 1);
2703 public function test_grade_competency_in_course() {
2704 global $CFG;
2706 $this->setUser($this->creator);
2707 $dg = $this->getDataGenerator();
2708 $lpg = $dg->get_plugin_generator('core_competency');
2710 $course = $dg->create_course(['fullname' => 'Evil course']);
2711 $dg->enrol_user($this->creator->id, $course->id, 'editingteacher');
2712 $dg->enrol_user($this->user->id, $course->id, 'student');
2713 $f1 = $lpg->create_framework();
2714 $c1 = $lpg->create_competency(['competencyframeworkid' => $f1->get('id')]);
2715 $lpg->create_course_competency(['courseid' => $course->id, 'competencyid' => $c1->get('id')]);
2717 $evidence = external::grade_competency_in_course($course->id, $this->user->id, $c1->get('id'), 1, 'Evil note');
2719 $this->assertEquals('The competency rating was manually set in the course \'Course: Evil course\'.', $evidence->description);
2720 $this->assertEquals('A', $evidence->gradename);
2721 $this->assertEquals('Evil note', $evidence->note);
2723 $this->setUser($this->user);
2725 $this->expectException('required_capability_exception');
2726 $evidence = external::grade_competency_in_course($course->id, $this->user->id, $c1->get('id'), 1);
2729 public function test_grade_competency_in_plan() {
2730 global $CFG;
2732 $this->setUser($this->creator);
2734 $dg = $this->getDataGenerator();
2735 $lpg = $dg->get_plugin_generator('core_competency');
2737 $f1 = $lpg->create_framework();
2739 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
2741 $tpl = $lpg->create_template();
2742 $lpg->create_template_competency(array('templateid' => $tpl->get('id'), 'competencyid' => $c1->get('id')));
2744 $plan = $lpg->create_plan(array('userid' => $this->user->id, 'templateid' => $tpl->get('id'), 'name' => 'Evil'));
2746 $uc = $lpg->create_user_competency(array('userid' => $this->user->id, 'competencyid' => $c1->get('id')));
2748 $evidence = external::grade_competency_in_plan($plan->get('id'), $c1->get('id'), 1, 'Evil note');
2750 $this->assertEquals('The competency rating was manually set in the learning plan \'Evil\'.', $evidence->description);
2751 $this->assertEquals('A', $evidence->gradename);
2752 $this->assertEquals('Evil note', $evidence->note);
2754 $this->setUser($this->user);
2756 $this->expectException('required_capability_exception');
2757 $evidence = external::grade_competency_in_plan($plan->get('id'), $c1->get('id'), 1);
2761 * Test update course competency settings.
2763 public function test_update_course_competency_settings() {
2764 $this->resetAfterTest(true);
2766 $dg = $this->getDataGenerator();
2768 $course = $dg->create_course();
2769 $roleid = $dg->create_role();
2770 $noobroleid = $dg->create_role();
2771 $context = context_course::instance($course->id);
2772 $compmanager = $this->getDataGenerator()->create_user();
2773 $compnoob = $this->getDataGenerator()->create_user();
2775 assign_capability('moodle/competency:coursecompetencyconfigure', CAP_ALLOW, $roleid, $context->id, true);
2776 assign_capability('moodle/competency:coursecompetencyview', CAP_ALLOW, $roleid, $context->id, true);
2777 assign_capability('moodle/competency:coursecompetencyview', CAP_ALLOW, $noobroleid, $context->id, true);
2779 role_assign($roleid, $compmanager->id, $context->id);
2780 role_assign($noobroleid, $compnoob->id, $context->id);
2781 $dg->enrol_user($compmanager->id, $course->id, $roleid);
2782 $dg->enrol_user($compnoob->id, $course->id, $noobroleid);
2784 $this->setUser($compmanager);
2786 // Start the test.
2787 $result = external::update_course_competency_settings($course->id, array('pushratingstouserplans' => true));
2789 $settings = course_competency_settings::get_by_courseid($course->id);
2791 $this->assertTrue((bool)$settings->get('pushratingstouserplans'));
2793 $result = external::update_course_competency_settings($course->id, array('pushratingstouserplans' => false));
2795 $settings = course_competency_settings::get_by_courseid($course->id);
2797 $this->assertFalse((bool)$settings->get('pushratingstouserplans'));
2798 $this->setUser($compnoob);
2800 $this->expectException('required_capability_exception');
2801 $result = external::update_course_competency_settings($course->id, array('pushratingstouserplans' => true));
2805 * Test that we can list competencies with a filter.
2807 * @return void
2809 public function test_list_competencies_with_filter() {
2810 $this->resetAfterTest(true);
2811 $this->setAdminUser();
2812 $dg = $this->getDataGenerator();
2813 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
2815 $framework = $lpg->create_framework();
2816 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2817 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2818 $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2819 $c4 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2820 $c5 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2822 // Test if removing competency from plan don't create sortorder holes.
2823 $filters = [];
2824 $sort = 'id';
2825 $order = 'ASC';
2826 $skip = 0;
2827 $limit = 0;
2828 $result = external::list_competencies($filters, $sort, $order, $skip, $limit);
2829 $this->assertCount(5, $result);
2831 $result = external::list_competencies($filters, $sort, $order, 2, $limit);
2832 $this->assertCount(3, $result);
2833 $result = external::list_competencies($filters, $sort, $order, 2, 2);
2834 $this->assertCount(2, $result);
2836 $filter = $result[0]->shortname;
2837 $filters[0] = ['column' => 'shortname', 'value' => $filter];
2838 $result = external::list_competencies($filters, $sort, $order, $skip, $limit);
2839 $this->assertCount(1, $result);
2840 $this->assertEquals($filter, $result[0]->shortname);