2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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();
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
;
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;
97 * Setup function- we will create a course and add an assign instance to it.
99 protected function setUp() {
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;
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;
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,
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) {
208 'name' => 'name' . $number,
209 'description' => 'description' . $number,
210 'descriptionformat' => FORMAT_HTML
,
212 'templateid' => empty($templateid) ?
null : $templateid,
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) {
222 'shortname' => 'shortname' . $number,
223 'description' => 'description' . $number,
224 'descriptionformat' => FORMAT_HTML
,
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) {
236 'shortname' => 'shortname' . $number,
237 'description' => 'description' . $number,
238 'descriptionformat' => FORMAT_HTML
,
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) {
248 'name' => 'name' . $number,
249 'description' => 'description' . $number,
250 'descriptionformat' => FORMAT_HTML
,
252 'templateid' => $templateid,
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;
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,
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) {
280 'shortname' => 'shortname' . $number,
281 'idnumber' => 'idnumber' . $number,
282 'description' => 'description' . $number,
283 'descriptionformat' => FORMAT_HTML
,
284 'competencyframeworkid' => $frameworkid,
287 $result = external
::create_competency($competency);
288 return (object) external_api
::clean_returnvalue(external
::create_competency_returns(), $result);
291 protected function update_competency($id, $number) {
294 'shortname' => 'shortname' . $number,
295 'idnumber' => 'idnumber' . $number,
296 'description' => 'description' . $number,
297 'descriptionformat' => FORMAT_HTML
,
300 $result = external
::update_competency($competency);
301 return external_api
::clean_returnvalue(external
::update_competency_returns(), $result);
305 * Test we can't create a competency framework with only read permissions.
307 * @expectedException required_capability_exception
309 public function test_create_competency_frameworks_with_read_permissions() {
310 $this->setUser($this->user
);
312 $result = $this->create_competency_framework(1, true);
316 * Test we can't create a competency framework with only read permissions.
318 * @expectedException required_capability_exception
320 public function test_create_competency_frameworks_with_read_permissions_in_category() {
321 $this->setUser($this->catuser
);
322 $result = $this->create_competency_framework(1, false);
326 * Test we can create a competency framework with manage permissions.
328 public function test_create_competency_frameworks_with_manage_permissions() {
329 $this->setUser($this->creator
);
330 $result = $this->create_competency_framework(1, true);
332 $this->assertGreaterThan(0, $result->timecreated
);
333 $this->assertGreaterThan(0, $result->timemodified
);
334 $this->assertEquals($this->creator
->id
, $result->usermodified
);
335 $this->assertEquals('shortname1', $result->shortname
);
336 $this->assertEquals('idnumber1', $result->idnumber
);
337 $this->assertEquals('description1', $result->description
);
338 $this->assertEquals(FORMAT_HTML
, $result->descriptionformat
);
339 $this->assertEquals($this->scale1
->id
, $result->scaleid
);
340 $this->assertEquals($this->scaleconfiguration1
, $result->scaleconfiguration
);
341 $this->assertEquals(true, $result->visible
);
345 * Test we can create a competency framework with manage permissions.
347 public function test_create_competency_frameworks_with_manage_permissions_in_category() {
348 $this->setUser($this->catcreator
);
349 $result = $this->create_competency_framework(1, false);
351 $this->assertGreaterThan(0, $result->timecreated
);
352 $this->assertGreaterThan(0, $result->timemodified
);
353 $this->assertEquals($this->catcreator
->id
, $result->usermodified
);
354 $this->assertEquals('shortname1', $result->shortname
);
355 $this->assertEquals('idnumber1', $result->idnumber
);
356 $this->assertEquals('description1', $result->description
);
357 $this->assertEquals(FORMAT_HTML
, $result->descriptionformat
);
358 $this->assertEquals($this->scale1
->id
, $result->scaleid
);
359 $this->assertEquals($this->scaleconfiguration1
, $result->scaleconfiguration
);
360 $this->assertEquals(true, $result->visible
);
363 $result = $this->create_competency_framework(1, true);
364 $this->fail('User cannot create a framework at system level.');
365 } catch (required_capability_exception
$e) {
371 * Test we cannot create a competency framework with nasty data.
373 * @expectedException invalid_parameter_exception
375 public function test_create_competency_frameworks_with_nasty_data() {
376 $this->setUser($this->creator
);
378 'shortname' => 'short<a href="">',
379 'idnumber' => 'id;"number',
380 'description' => 'de<>\\..scription',
381 'descriptionformat' => FORMAT_HTML
,
382 'scaleid' => $this->scale1
->id
,
383 'scaleconfiguration' => $this->scaleconfiguration1
,
385 'contextid' => context_system
::instance()->id
387 $result = external
::create_competency_framework($framework);
391 * Test we can read a competency framework with manage permissions.
393 public function test_read_competency_frameworks_with_manage_permissions() {
394 $this->setUser($this->creator
);
395 $result = $this->create_competency_framework(1, true);
398 $result = external
::read_competency_framework($id);
399 $result = (object) external_api
::clean_returnvalue(external
::read_competency_framework_returns(), $result);
401 $this->assertGreaterThan(0, $result->timecreated
);
402 $this->assertGreaterThan(0, $result->timemodified
);
403 $this->assertEquals($this->creator
->id
, $result->usermodified
);
404 $this->assertEquals('shortname1', $result->shortname
);
405 $this->assertEquals('idnumber1', $result->idnumber
);
406 $this->assertEquals('description1', $result->description
);
407 $this->assertEquals(FORMAT_HTML
, $result->descriptionformat
);
408 $this->assertEquals($this->scale1
->id
, $result->scaleid
);
409 $this->assertEquals($this->scaleconfiguration1
, $result->scaleconfiguration
);
410 $this->assertEquals(true, $result->visible
);
414 * Test we can read a competency framework with manage permissions.
416 public function test_read_competency_frameworks_with_manage_permissions_in_category() {
417 $this->setUser($this->creator
);
419 $insystem = $this->create_competency_framework(1, true);
420 $incat = $this->create_competency_framework(2, false);
422 $this->setUser($this->catcreator
);
424 $result = external
::read_competency_framework($id);
425 $result = (object) external_api
::clean_returnvalue(external
::read_competency_framework_returns(), $result);
427 $this->assertGreaterThan(0, $result->timecreated
);
428 $this->assertGreaterThan(0, $result->timemodified
);
429 $this->assertEquals($this->creator
->id
, $result->usermodified
);
430 $this->assertEquals('shortname2', $result->shortname
);
431 $this->assertEquals('idnumber2', $result->idnumber
);
432 $this->assertEquals('description2', $result->description
);
433 $this->assertEquals(FORMAT_HTML
, $result->descriptionformat
);
434 $this->assertEquals($this->scale2
->id
, $result->scaleid
);
435 $this->assertEquals($this->scaleconfiguration2
, $result->scaleconfiguration
);
436 $this->assertEquals(true, $result->visible
);
440 $result = external
::read_competency_framework($id);
441 $result = (object) external_api
::clean_returnvalue(external
::read_competency_framework_returns(), $result);
442 $this->fail('User cannot read a framework at system level.');
443 } catch (required_capability_exception
$e) {
449 * Test we can read a competency framework with read permissions.
451 public function test_read_competency_frameworks_with_read_permissions() {
452 $this->setUser($this->creator
);
453 $result = $this->create_competency_framework(1, true);
455 // Switch users to someone with less permissions.
456 $this->setUser($this->user
);
458 $result = external
::read_competency_framework($id);
459 $result = (object) external_api
::clean_returnvalue(external
::read_competency_framework_returns(), $result);
461 $this->assertGreaterThan(0, $result->timecreated
);
462 $this->assertGreaterThan(0, $result->timemodified
);
463 $this->assertEquals($this->creator
->id
, $result->usermodified
);
464 $this->assertEquals('shortname1', $result->shortname
);
465 $this->assertEquals('idnumber1', $result->idnumber
);
466 $this->assertEquals('description1', $result->description
);
467 $this->assertEquals(FORMAT_HTML
, $result->descriptionformat
);
468 $this->assertEquals($this->scale1
->id
, $result->scaleid
);
469 $this->assertEquals($this->scaleconfiguration1
, $result->scaleconfiguration
);
470 $this->assertEquals(true, $result->visible
);
473 * Test we can read a competency framework with read permissions.
475 public function test_read_competency_frameworks_with_read_permissions_in_category() {
476 $this->setUser($this->creator
);
478 $insystem = $this->create_competency_framework(1, true);
479 $incat = $this->create_competency_framework(2, false);
481 // Switch users to someone with less permissions.
482 $this->setUser($this->catuser
);
484 $result = external
::read_competency_framework($id);
485 $result = (object) external_api
::clean_returnvalue(external
::read_competency_framework_returns(), $result);
487 $this->assertGreaterThan(0, $result->timecreated
);
488 $this->assertGreaterThan(0, $result->timemodified
);
489 $this->assertEquals($this->creator
->id
, $result->usermodified
);
490 $this->assertEquals('shortname2', $result->shortname
);
491 $this->assertEquals('idnumber2', $result->idnumber
);
492 $this->assertEquals('description2', $result->description
);
493 $this->assertEquals(FORMAT_HTML
, $result->descriptionformat
);
494 $this->assertEquals($this->scale2
->id
, $result->scaleid
);
495 $this->assertEquals($this->scaleconfiguration2
, $result->scaleconfiguration
);
496 $this->assertEquals(true, $result->visible
);
498 // Switching to user with no permissions.
500 $result = external
::read_competency_framework($insystem->id
);
501 $this->fail('Current user cannot should not be able to read the framework.');
502 } catch (required_capability_exception
$e) {
508 * Test we can delete a competency framework with manage permissions.
510 public function test_delete_competency_frameworks_with_manage_permissions() {
511 $this->setUser($this->creator
);
512 $result = $this->create_competency_framework(1, true);
515 $result = external
::delete_competency_framework($id);
516 $result = external_api
::clean_returnvalue(external
::delete_competency_framework_returns(), $result);
518 $this->assertTrue($result);
522 * Test we can delete a competency framework with manage permissions.
524 public function test_delete_competency_frameworks_with_manage_permissions_in_category() {
525 $this->setUser($this->creator
);
527 $insystem = $this->create_competency_framework(1, true);
528 $incat = $this->create_competency_framework(2, false);
530 $this->setUser($this->catcreator
);
532 $result = external
::delete_competency_framework($id);
533 $result = external_api
::clean_returnvalue(external
::delete_competency_framework_returns(), $result);
535 $this->assertTrue($result);
539 $result = external
::delete_competency_framework($id);
540 $result = external_api
::clean_returnvalue(external
::delete_competency_framework_returns(), $result);
541 $this->fail('Current user cannot should not be able to delete the framework.');
542 } catch (required_capability_exception
$e) {
548 * Test we can delete a competency framework with read permissions.
550 * @expectedException required_capability_exception
552 public function test_delete_competency_frameworks_with_read_permissions() {
553 $this->setUser($this->creator
);
554 $result = $this->create_competency_framework(1, true);
557 // Switch users to someone with less permissions.
558 $this->setUser($this->user
);
559 $result = external
::delete_competency_framework($id);
563 * Test we can update a competency framework with manage permissions.
565 public function test_update_competency_frameworks_with_manage_permissions() {
566 $this->setUser($this->creator
);
567 $result = $this->create_competency_framework(1, true);
569 $result = $this->update_competency_framework($result->id
, 2, true);
571 $this->assertTrue($result);
575 * Test we can update a competency framework with manage permissions.
577 public function test_update_competency_frameworks_with_manage_permissions_in_category() {
578 $this->setUser($this->creator
);
580 $insystem = $this->create_competency_framework(1, true);
581 $incat = $this->create_competency_framework(2, false);
583 $this->setUser($this->catcreator
);
586 $result = $this->update_competency_framework($incat->id
, 3, false);
588 $this->assertTrue($result);
591 $result = $this->update_competency_framework($insystem->id
, 4, true);
592 $this->fail('Current user should not be able to update the framework.');
593 } catch (required_capability_exception
$e) {
598 public function test_update_framework_scale() {
599 $this->setUser($this->creator
);
600 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
602 $s1 = $this->getDataGenerator()->create_scale();
604 $f1 = $lpg->create_framework(array('scaleid' => $s1->id
));
605 $f2 = $lpg->create_framework(array('scaleid' => $s1->id
));
606 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
607 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f2->get('id')));
609 $this->assertEquals($s1->id
, $f1->get('scaleid'));
611 // Make the scale of f2 being used.
612 $lpg->create_user_competency(array('userid' => $this->user
->id
, 'competencyid' => $c2->get('id')));
614 // Changing the framework where the scale is not used.
615 $result = $this->update_competency_framework($f1->get('id'), 3, true);
617 $f1 = new \core_competency\
competency_framework($f1->get('id'));
618 $this->assertEquals($this->scale3
->id
, $f1->get('scaleid'));
620 // Changing the framework where the scale is used.
622 $result = $this->update_competency_framework($f2->get('id'), 4, true);
623 $this->fail('The scale cannot be changed once used.');
624 } catch (\core\invalid_persistent_exception
$e) {
625 $this->assertRegexp('/scaleid/', $e->getMessage());
630 * Test we can update a competency framework with read permissions.
632 * @expectedException required_capability_exception
634 public function test_update_competency_frameworks_with_read_permissions() {
635 $this->setUser($this->creator
);
636 $result = $this->create_competency_framework(1, true);
638 $this->setUser($this->user
);
639 $result = $this->update_competency_framework($result->id
, 2, true);
643 * Test we can list and count competency frameworks with manage permissions.
645 public function test_list_and_count_competency_frameworks_with_manage_permissions() {
646 $this->setUser($this->creator
);
647 $result = $this->create_competency_framework(1, true);
648 $result = $this->create_competency_framework(2, true);
649 $result = $this->create_competency_framework(3, true);
650 $result = $this->create_competency_framework(4, false);
652 $result = external
::count_competency_frameworks(array('contextid' => context_system
::instance()->id
), 'self');
653 $result = external_api
::clean_returnvalue(external
::count_competency_frameworks_returns(), $result);
655 $this->assertEquals($result, 3);
657 $result = external
::list_competency_frameworks('shortname', 'ASC', 0, 10,
658 array('contextid' => context_system
::instance()->id
), 'self', false);
659 $result = external_api
::clean_returnvalue(external
::list_competency_frameworks_returns(), $result);
661 $this->assertEquals(count($result), 3);
662 $result = (object) $result[0];
664 $this->assertGreaterThan(0, $result->timecreated
);
665 $this->assertGreaterThan(0, $result->timemodified
);
666 $this->assertEquals($this->creator
->id
, $result->usermodified
);
667 $this->assertEquals('shortname1', $result->shortname
);
668 $this->assertEquals('idnumber1', $result->idnumber
);
669 $this->assertEquals('description1', $result->description
);
670 $this->assertEquals(FORMAT_HTML
, $result->descriptionformat
);
671 $this->assertEquals($this->scale1
->id
, $result->scaleid
);
672 $this->assertEquals($this->scaleconfiguration1
, $result->scaleconfiguration
);
673 $this->assertEquals(true, $result->visible
);
676 public function test_list_competency_frameworks_with_query() {
677 $this->setUser($this->creator
);
678 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
679 $framework1 = $lpg->create_framework(array(
680 'shortname' => 'shortname_beetroot',
681 'idnumber' => 'idnumber_cinnamon',
682 'description' => 'description',
683 'descriptionformat' => FORMAT_HTML
,
685 'contextid' => context_system
::instance()->id
687 $framework2 = $lpg->create_framework(array(
688 'shortname' => 'shortname_citrus',
689 'idnumber' => 'idnumber_beer',
690 'description' => 'description',
691 'descriptionformat' => FORMAT_HTML
,
693 'contextid' => context_system
::instance()->id
696 // Search on both ID number and shortname.
697 $result = external
::list_competency_frameworks('shortname', 'ASC', 0, 10,
698 array('contextid' => context_system
::instance()->id
), 'self', false, 'bee');
699 $result = external_api
::clean_returnvalue(external
::list_competency_frameworks_returns(), $result);
700 $this->assertCount(2, $result);
701 $f = (object) array_shift($result);
702 $this->assertEquals($framework1->get('id'), $f->id
);
703 $f = (object) array_shift($result);
704 $this->assertEquals($framework2->get('id'), $f->id
);
706 // Search on ID number.
707 $result = external
::list_competency_frameworks('shortname', 'ASC', 0, 10,
708 array('contextid' => context_system
::instance()->id
), 'self', false, 'beer');
709 $result = external_api
::clean_returnvalue(external
::list_competency_frameworks_returns(), $result);
710 $this->assertCount(1, $result);
711 $f = (object) array_shift($result);
712 $this->assertEquals($framework2->get('id'), $f->id
);
714 // Search on shortname.
715 $result = external
::list_competency_frameworks('shortname', 'ASC', 0, 10,
716 array('contextid' => context_system
::instance()->id
), 'self', false, 'cinnamon');
717 $result = external_api
::clean_returnvalue(external
::list_competency_frameworks_returns(), $result);
718 $this->assertCount(1, $result);
719 $f = (object) array_shift($result);
720 $this->assertEquals($framework1->get('id'), $f->id
);
723 $result = external
::list_competency_frameworks('shortname', 'ASC', 0, 10,
724 array('contextid' => context_system
::instance()->id
), 'self', false, 'pwnd!');
725 $result = external_api
::clean_returnvalue(external
::list_competency_frameworks_returns(), $result);
726 $this->assertCount(0, $result);
730 * Test we can list and count competency frameworks with read permissions.
732 public function test_list_and_count_competency_frameworks_with_read_permissions() {
733 $this->setUser($this->creator
);
734 $result = $this->create_competency_framework(1, true);
735 $result = $this->create_competency_framework(2, true);
736 $result = $this->create_competency_framework(3, true);
737 $result = $this->create_competency_framework(4, false);
739 $this->setUser($this->user
);
740 $result = external
::count_competency_frameworks(array('contextid' => context_system
::instance()->id
), 'self');
741 $result = external_api
::clean_returnvalue(external
::count_competency_frameworks_returns(), $result);
742 $this->assertEquals($result, 3);
744 $result = external
::list_competency_frameworks('shortname', 'ASC', 0, 10,
745 array('contextid' => context_system
::instance()->id
), 'self', false);
746 $result = external_api
::clean_returnvalue(external
::list_competency_frameworks_returns(), $result);
748 $this->assertEquals(count($result), 3);
749 $result = (object) $result[0];
751 $this->assertGreaterThan(0, $result->timecreated
);
752 $this->assertGreaterThan(0, $result->timemodified
);
753 $this->assertEquals($this->creator
->id
, $result->usermodified
);
754 $this->assertEquals('shortname1', $result->shortname
);
755 $this->assertEquals('idnumber1', $result->idnumber
);
756 $this->assertEquals('description1', $result->description
);
757 $this->assertEquals(FORMAT_HTML
, $result->descriptionformat
);
758 $this->assertEquals($this->scale1
->id
, $result->scaleid
);
759 $this->assertEquals($this->scaleconfiguration1
, $result->scaleconfiguration
);
760 $this->assertEquals(true, $result->visible
);
764 * Test we can't create a competency with only read permissions.
766 * @expectedException required_capability_exception
768 public function test_create_competency_with_read_permissions() {
769 $framework = $this->getDataGenerator()->get_plugin_generator('core_competency')->create_framework();
770 $this->setUser($this->user
);
771 $competency = $this->create_competency(1, $framework->get('id'));
775 * Test we can create a competency with manage permissions.
777 public function test_create_competency_with_manage_permissions() {
778 $this->setUser($this->creator
);
779 $framework = $this->create_competency_framework(1, true);
780 $competency = $this->create_competency(1, $framework->id
);
782 $this->assertGreaterThan(0, $competency->timecreated
);
783 $this->assertGreaterThan(0, $competency->timemodified
);
784 $this->assertEquals($this->creator
->id
, $competency->usermodified
);
785 $this->assertEquals('shortname1', $competency->shortname
);
786 $this->assertEquals('idnumber1', $competency->idnumber
);
787 $this->assertEquals('description1', $competency->description
);
788 $this->assertEquals(FORMAT_HTML
, $competency->descriptionformat
);
789 $this->assertEquals(0, $competency->parentid
);
790 $this->assertEquals($framework->id
, $competency->competencyframeworkid
);
795 * Test we can create a competency with manage permissions.
797 public function test_create_competency_with_manage_permissions_in_category() {
798 $this->setUser($this->creator
);
800 $insystem = $this->create_competency_framework(1, true);
801 $incat = $this->create_competency_framework(2, false);
803 $this->setUser($this->catcreator
);
805 $competency = $this->create_competency(1, $incat->id
);
807 $this->assertGreaterThan(0, $competency->timecreated
);
808 $this->assertGreaterThan(0, $competency->timemodified
);
809 $this->assertEquals($this->catcreator
->id
, $competency->usermodified
);
810 $this->assertEquals('shortname1', $competency->shortname
);
811 $this->assertEquals('idnumber1', $competency->idnumber
);
812 $this->assertEquals('description1', $competency->description
);
813 $this->assertEquals(FORMAT_HTML
, $competency->descriptionformat
);
814 $this->assertEquals(0, $competency->parentid
);
815 $this->assertEquals($incat->id
, $competency->competencyframeworkid
);
818 $competency = $this->create_competency(2, $insystem->id
);
819 $this->fail('User should not be able to create a competency in system context.');
820 } catch (required_capability_exception
$e) {
826 * Test we cannot create a competency with nasty data.
828 * @expectedException invalid_parameter_exception
830 public function test_create_competency_with_nasty_data() {
831 $this->setUser($this->creator
);
832 $framework = $this->create_competency_framework(1, true);
834 'shortname' => 'shortname<a href="">',
835 'idnumber' => 'id;"number',
836 'description' => 'de<>\\..scription',
837 'descriptionformat' => FORMAT_HTML
,
838 'competencyframeworkid' => $framework->id
,
841 $result = external
::create_competency($competency);
842 $result = (object) external_api
::clean_returnvalue(external
::create_competency_returns(), $result);
846 * Test we can read a competency with manage permissions.
848 public function test_read_competencies_with_manage_permissions() {
849 $this->setUser($this->creator
);
850 $framework = $this->create_competency_framework(1, true);
851 $competency = $this->create_competency(1, $framework->id
);
853 $id = $competency->id
;
854 $result = external
::read_competency($id);
855 $result = (object) external_api
::clean_returnvalue(external
::read_competency_returns(), $result);
857 $this->assertGreaterThan(0, $result->timecreated
);
858 $this->assertGreaterThan(0, $result->timemodified
);
859 $this->assertEquals($this->creator
->id
, $result->usermodified
);
860 $this->assertEquals('shortname1', $result->shortname
);
861 $this->assertEquals('idnumber1', $result->idnumber
);
862 $this->assertEquals('description1', $result->description
);
863 $this->assertEquals(FORMAT_HTML
, $result->descriptionformat
);
864 $this->assertEquals(0, $result->parentid
);
865 $this->assertEquals($framework->id
, $result->competencyframeworkid
);
869 * Test we can read a competency with manage permissions.
871 public function test_read_competencies_with_manage_permissions_in_category() {
872 $this->setUser($this->creator
);
874 $sysframework = $this->create_competency_framework(1, true);
875 $insystem = $this->create_competency(1, $sysframework->id
);
877 $catframework = $this->create_competency_framework(2, false);
878 $incat = $this->create_competency(2, $catframework->id
);
880 $this->setUser($this->catcreator
);
882 $result = external
::read_competency($id);
883 $result = (object) external_api
::clean_returnvalue(external
::read_competency_returns(), $result);
885 $this->assertGreaterThan(0, $result->timecreated
);
886 $this->assertGreaterThan(0, $result->timemodified
);
887 $this->assertEquals($this->creator
->id
, $result->usermodified
);
888 $this->assertEquals('shortname2', $result->shortname
);
889 $this->assertEquals('idnumber2', $result->idnumber
);
890 $this->assertEquals('description2', $result->description
);
891 $this->assertEquals(FORMAT_HTML
, $result->descriptionformat
);
892 $this->assertEquals(0, $result->parentid
);
893 $this->assertEquals($catframework->id
, $result->competencyframeworkid
);
896 external
::read_competency($insystem->id
);
897 $this->fail('User should not be able to read a competency in system context.');
898 } catch (required_capability_exception
$e) {
904 * Test we can read a competency with read permissions.
906 public function test_read_competencies_with_read_permissions() {
907 $this->setUser($this->creator
);
908 $framework = $this->create_competency_framework(1, true);
909 $competency = $this->create_competency(1, $framework->id
);
911 // Switch users to someone with less permissions.
912 $this->setUser($this->user
);
913 $id = $competency->id
;
914 $result = external
::read_competency($id);
915 $result = (object) external_api
::clean_returnvalue(external
::read_competency_returns(), $result);
917 $this->assertGreaterThan(0, $result->timecreated
);
918 $this->assertGreaterThan(0, $result->timemodified
);
919 $this->assertEquals($this->creator
->id
, $result->usermodified
);
920 $this->assertEquals('shortname1', $result->shortname
);
921 $this->assertEquals('idnumber1', $result->idnumber
);
922 $this->assertEquals('description1', $result->description
);
923 $this->assertEquals(FORMAT_HTML
, $result->descriptionformat
);
924 $this->assertEquals(0, $result->parentid
);
925 $this->assertEquals($framework->id
, $result->competencyframeworkid
);
929 * Test we can read a competency with read permissions.
931 public function test_read_competencies_with_read_permissions_in_category() {
932 $this->setUser($this->creator
);
933 $sysframework = $this->create_competency_framework(1, true);
934 $insystem = $this->create_competency(1, $sysframework->id
);
935 $catframework = $this->create_competency_framework(2, false);
936 $incat = $this->create_competency(2, $catframework->id
);
938 // Switch users to someone with less permissions.
939 $this->setUser($this->catuser
);
941 $result = external
::read_competency($id);
942 $result = (object) external_api
::clean_returnvalue(external
::read_competency_returns(), $result);
944 $this->assertGreaterThan(0, $result->timecreated
);
945 $this->assertGreaterThan(0, $result->timemodified
);
946 $this->assertEquals($this->creator
->id
, $result->usermodified
);
947 $this->assertEquals('shortname2', $result->shortname
);
948 $this->assertEquals('idnumber2', $result->idnumber
);
949 $this->assertEquals('description2', $result->description
);
950 $this->assertEquals(FORMAT_HTML
, $result->descriptionformat
);
951 $this->assertEquals(0, $result->parentid
);
952 $this->assertEquals($catframework->id
, $result->competencyframeworkid
);
955 external
::read_competency($insystem->id
);
956 $this->fail('User should not be able to read a competency in system context.');
957 } catch (required_capability_exception
$e) {
963 * Test we can delete a competency with manage permissions.
965 public function test_delete_competency_with_manage_permissions() {
966 $this->setUser($this->creator
);
967 $framework = $this->create_competency_framework(1, true);
968 $result = $this->create_competency(1, $framework->id
);
971 $result = external
::delete_competency($id);
972 $result = external_api
::clean_returnvalue(external
::delete_competency_returns(), $result);
974 $this->assertTrue($result);
978 * Test we can delete a competency with manage permissions.
980 public function test_delete_competency_with_manage_permissions_in_category() {
981 $this->setUser($this->creator
);
983 $sysframework = $this->create_competency_framework(1, true);
984 $insystem = $this->create_competency(1, $sysframework->id
);
985 $catframework = $this->create_competency_framework(2, false);
986 $incat = $this->create_competency(2, $catframework->id
);
988 $this->setUser($this->catcreator
);
990 $result = external
::delete_competency($id);
991 $result = external_api
::clean_returnvalue(external
::delete_competency_returns(), $result);
993 $this->assertTrue($result);
996 $result = external
::delete_competency($insystem->id
);
997 $this->fail('User should not be able to delete a competency in system context.');
998 } catch (required_capability_exception
$e) {
1004 * Test we can delete a competency with read permissions.
1006 * @expectedException required_capability_exception
1008 public function test_delete_competency_with_read_permissions() {
1009 $this->setUser($this->creator
);
1010 $framework = $this->create_competency_framework(1, true);
1011 $result = $this->create_competency(1, $framework->id
);
1014 // Switch users to someone with less permissions.
1015 $this->setUser($this->user
);
1016 $result = external
::delete_competency($id);
1020 * Test we can update a competency with manage permissions.
1022 public function test_update_competency_with_manage_permissions() {
1023 $this->setUser($this->creator
);
1024 $framework = $this->create_competency_framework(1, true);
1025 $result = $this->create_competency(1, $framework->id
);
1027 $result = $this->update_competency($result->id
, 2);
1029 $this->assertTrue($result);
1033 * Test we can update a competency with manage permissions.
1035 public function test_update_competency_with_manage_permissions_in_category() {
1036 $this->setUser($this->creator
);
1038 $sysframework = $this->create_competency_framework(1, true);
1039 $insystem = $this->create_competency(1, $sysframework->id
);
1040 $catframework = $this->create_competency_framework(2, false);
1041 $incat = $this->create_competency(2, $catframework->id
);
1043 $this->setUser($this->catcreator
);
1045 $result = $this->update_competency($incat->id
, 2);
1047 $this->assertTrue($result);
1050 $result = $this->update_competency($insystem->id
, 3);
1051 $this->fail('User should not be able to update a competency in system context.');
1052 } catch (required_capability_exception
$e) {
1058 * Test we can update a competency with read permissions.
1060 * @expectedException required_capability_exception
1062 public function test_update_competency_with_read_permissions() {
1063 $this->setUser($this->creator
);
1064 $framework = $this->create_competency_framework(1, true);
1065 $result = $this->create_competency(1, $framework->id
);
1067 $this->setUser($this->user
);
1068 $result = $this->update_competency($result->id
, 2);
1072 * Test count competencies with filters.
1074 public function test_count_competencies_with_filters() {
1075 $this->setUser($this->creator
);
1077 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1078 $f1 = $lpg->create_framework();
1079 $f2 = $lpg->create_framework();
1080 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
1081 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id'), 'shortname' => 'A'));
1082 $c3 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
1083 $c4 = $lpg->create_competency(array('competencyframeworkid' => $f2->get('id')));
1084 $c5 = $lpg->create_competency(array('competencyframeworkid' => $f2->get('id')));
1086 $result = external
::count_competencies(array(array('column' => 'competencyframeworkid', 'value' => $f2->get('id'))));
1087 $result = external_api
::clean_returnvalue(external
::count_competencies_returns(), $result);
1088 $this->assertEquals(2, $result);
1090 $result = external
::count_competencies(array(array('column' => 'competencyframeworkid', 'value' => $f1->get('id'))));
1091 $result = external_api
::clean_returnvalue(external
::count_competencies_returns(), $result);
1092 $this->assertEquals(3, $result);
1094 $result = external
::count_competencies(array(array('column' => 'shortname', 'value' => 'A')));
1095 $result = external_api
::clean_returnvalue(external
::count_competencies_returns(), $result);
1096 $this->assertEquals(1, $result);
1100 * Test we can list and count competencies with manage permissions.
1102 public function test_list_and_count_competencies_with_manage_permissions() {
1103 $this->setUser($this->creator
);
1104 $framework = $this->create_competency_framework(1, true);
1105 $result = $this->create_competency(1, $framework->id
);
1106 $result = $this->create_competency(2, $framework->id
);
1107 $result = $this->create_competency(3, $framework->id
);
1109 $result = external
::count_competencies(array());
1110 $result = external_api
::clean_returnvalue(external
::count_competencies_returns(), $result);
1112 $this->assertEquals($result, 3);
1114 array('id' => $result = external
::list_competencies(array(), 'shortname', 'ASC', 0, 10, context_system
::instance()->id
));
1115 $result = external_api
::clean_returnvalue(external
::list_competencies_returns(), $result);
1117 $this->assertEquals(count($result), 3);
1118 $result = (object) $result[0];
1120 $this->assertGreaterThan(0, $result->timecreated
);
1121 $this->assertGreaterThan(0, $result->timemodified
);
1122 $this->assertEquals($this->creator
->id
, $result->usermodified
);
1123 $this->assertEquals('shortname1', $result->shortname
);
1124 $this->assertEquals('idnumber1', $result->idnumber
);
1125 $this->assertEquals('description1', $result->description
);
1129 * Test we can list and count competencies with read permissions.
1131 public function test_list_and_count_competencies_with_read_permissions() {
1132 $this->setUser($this->creator
);
1133 $framework = $this->create_competency_framework(1, true);
1134 $result = $this->create_competency(1, $framework->id
);
1135 $result = $this->create_competency(2, $framework->id
);
1136 $result = $this->create_competency(3, $framework->id
);
1138 $this->setUser($this->user
);
1140 $result = external
::count_competencies(array());
1141 $result = external_api
::clean_returnvalue(external
::count_competencies_returns(), $result);
1143 $this->assertEquals($result, 3);
1145 array('id' => $result = external
::list_competencies(array(), 'shortname', 'ASC', 0, 10, context_system
::instance()->id
));
1146 $result = external_api
::clean_returnvalue(external
::list_competencies_returns(), $result);
1148 $this->assertEquals(count($result), 3);
1149 $result = (object) $result[0];
1151 $this->assertGreaterThan(0, $result->timecreated
);
1152 $this->assertGreaterThan(0, $result->timemodified
);
1153 $this->assertEquals($this->creator
->id
, $result->usermodified
);
1154 $this->assertEquals('shortname1', $result->shortname
);
1155 $this->assertEquals('idnumber1', $result->idnumber
);
1156 $this->assertEquals('description1', $result->description
);
1160 * Test we can search for competencies.
1162 public function test_search_competencies_with_read_permissions() {
1163 $this->setUser($this->creator
);
1164 $framework = $this->create_competency_framework(1, true);
1165 $result = $this->create_competency(1, $framework->id
);
1166 $result = $this->create_competency(2, $framework->id
);
1167 $result = $this->create_competency(3, $framework->id
);
1169 $this->setUser($this->user
);
1171 $result = external
::search_competencies('short', $framework->id
);
1172 $result = external_api
::clean_returnvalue(external
::search_competencies_returns(), $result);
1174 $this->assertEquals(count($result), 3);
1175 $result = (object) $result[0];
1177 $this->assertGreaterThan(0, $result->timecreated
);
1178 $this->assertGreaterThan(0, $result->timemodified
);
1179 $this->assertEquals($this->creator
->id
, $result->usermodified
);
1180 $this->assertEquals('shortname1', $result->shortname
);
1181 $this->assertEquals('idnumber1', $result->idnumber
);
1182 $this->assertEquals('description1', $result->description
);
1186 * Test plans creation and updates.
1188 public function test_create_and_update_plans() {
1189 $syscontext = context_system
::instance();
1191 $this->setUser($this->creator
);
1192 $plan0 = $this->create_plan(1, $this->creator
->id
, 0, plan
::STATUS_ACTIVE
, 0);
1194 $this->setUser($this->user
);
1197 $plan1 = $this->create_plan(2, $this->user
->id
, 0, plan
::STATUS_DRAFT
, 0);
1198 $this->fail('Exception expected due to not permissions to create draft plans');
1199 } catch (moodle_exception
$e) {
1200 $this->assertEquals('nopermissions', $e->errorcode
);
1203 assign_capability('moodle/competency:planmanageowndraft', CAP_ALLOW
, $this->userrole
, $syscontext->id
);
1204 accesslib_clear_all_caches_for_unit_testing();
1206 $this->setUser($this->user
);
1208 $plan2 = $this->create_plan(3, $this->user
->id
, 0, plan
::STATUS_DRAFT
, 0);
1210 // Basic update on the plan.
1211 $this->assertNotEquals('Updated plan 2 name', $plan2->name
);
1212 $plan2 = external
::update_plan(['id' => $plan2->id
, 'name' => 'Updated plan 2 name']);
1213 $this->assertEquals('Updated plan 2 name', $plan2->name
);
1216 $plan3 = $this->create_plan(4, $this->user
->id
, 0, plan
::STATUS_ACTIVE
, 0);
1217 $this->fail('Exception expected due to not permissions to create active plans');
1218 } catch (moodle_exception
$e) {
1219 $this->assertEquals('nopermissions', $e->errorcode
);
1222 $plan3 = $this->update_plan($plan2->id
, 4, $this->user
->id
, 0, plan
::STATUS_COMPLETE
, 0);
1223 $this->fail('We cannot complete a plan using api::update_plan().');
1224 } catch (coding_exception
$e) {
1225 $this->assertTrue(true);
1228 assign_capability('moodle/competency:planmanageown', CAP_ALLOW
, $this->userrole
, $syscontext->id
);
1229 accesslib_clear_all_caches_for_unit_testing();
1231 $plan3 = $this->create_plan(4, $this->user
->id
, 0, plan
::STATUS_ACTIVE
, 0);
1233 $plan4 = $this->create_plan(6, $this->creator
->id
, 0, plan
::STATUS_COMPLETE
, 0);
1234 $this->fail('Plans cannot be created as complete.');
1235 } catch (coding_exception
$e) {
1236 $this->assertRegexp('/A plan cannot be created as complete./', $e->getMessage());
1240 $plan0 = $this->update_plan($plan0->id
, 1, $this->user
->id
, 0, plan
::STATUS_ACTIVE
, 0);
1241 } catch (moodle_exception
$e) {
1242 $this->assertEquals('nopermissions', $e->errorcode
);
1245 unassign_capability('moodle/competency:planmanageown', $this->userrole
, $syscontext->id
);
1246 unassign_capability('moodle/competency:planmanageowndraft', $this->userrole
, $syscontext->id
);
1247 accesslib_clear_all_caches_for_unit_testing();
1250 // Cannot be updated even if they created it.
1251 $this->update_plan($plan2->id
, 1, $this->user
->id
, 0, plan
::STATUS_ACTIVE
, 0);
1252 $this->fail('The user can not update their own plan without permissions.');
1253 } catch (required_capability_exception
$e) {
1254 $this->assertRegexp('/Manage learning plans./', $e->getMessage());
1259 * Test complete plan.
1261 public function test_complete_plan() {
1262 $syscontext = context_system
::instance();
1264 $this->setUser($this->creator
);
1266 $this->setUser($this->user
);
1268 assign_capability('moodle/competency:planmanageowndraft', CAP_ALLOW
, $this->userrole
, $syscontext->id
);
1269 assign_capability('moodle/competency:planmanageown', CAP_ALLOW
, $this->userrole
, $syscontext->id
);
1270 accesslib_clear_all_caches_for_unit_testing();
1272 $this->setUser($this->user
);
1274 $plan = $this->create_plan(1, $this->user
->id
, 0, plan
::STATUS_ACTIVE
, 0);
1276 $result = external
::complete_plan($plan->id
);
1277 $this->assertTrue($result);
1283 public function test_reopen_plan() {
1284 $syscontext = context_system
::instance();
1286 $this->setUser($this->creator
);
1288 $this->setUser($this->user
);
1290 assign_capability('moodle/competency:planmanageowndraft', CAP_ALLOW
, $this->userrole
, $syscontext->id
);
1291 assign_capability('moodle/competency:planmanageown', CAP_ALLOW
, $this->userrole
, $syscontext->id
);
1292 accesslib_clear_all_caches_for_unit_testing();
1294 $this->setUser($this->user
);
1296 $plan = $this->create_plan(1, $this->user
->id
, 0, plan
::STATUS_ACTIVE
, 0);
1297 external
::complete_plan($plan->id
);
1299 $result = external
::reopen_plan($plan->id
);
1300 $this->assertTrue($result);
1304 * Test that we can read plans.
1306 public function test_read_plans() {
1308 $this->setUser($this->creator
);
1310 $syscontext = context_system
::instance();
1312 $plan1 = $this->create_plan(1, $this->user
->id
, 0, plan
::STATUS_DRAFT
, 0);
1313 $plan2 = $this->create_plan(2, $this->user
->id
, 0, plan
::STATUS_ACTIVE
, 0);
1314 $plan3 = $this->create_plan(3, $this->user
->id
, 0, plan
::STATUS_ACTIVE
, 0);
1315 external
::complete_plan($plan3->id
);
1316 $plan3 = (object) external
::read_plan($plan3->id
);
1318 $data = external
::read_plan($plan1->id
);
1319 $this->assertEquals((array)$plan1, external
::read_plan($plan1->id
));
1320 $data = external
::read_plan($plan2->id
);
1321 $this->assertEquals((array)$plan2, external
::read_plan($plan2->id
));
1322 $data = external
::read_plan($plan3->id
);
1323 $this->assertEquals((array)$plan3, external
::read_plan($plan3->id
));
1325 $this->setUser($this->user
);
1327 // The normal user can not edit these plans.
1328 $plan1->canmanage
= false;
1329 $plan2->canmanage
= false;
1330 $plan3->canmanage
= false;
1331 $plan1->canbeedited
= false;
1332 $plan2->canbeedited
= false;
1333 $plan3->canbeedited
= false;
1334 $plan1->canrequestreview
= true;
1335 $plan2->canrequestreview
= true;
1336 $plan3->canrequestreview
= true;
1337 $plan1->canreview
= false;
1338 $plan2->canreview
= false;
1339 $plan3->canreview
= false;
1340 $plan1->iscompleteallowed
= false;
1341 $plan2->iscompleteallowed
= false;
1342 $plan3->iscompleteallowed
= false;
1343 $plan1->isrequestreviewallowed
= true;
1344 $plan2->isrequestreviewallowed
= true;
1345 $plan3->isrequestreviewallowed
= true;
1346 $plan1->isapproveallowed
= false;
1347 $plan2->isapproveallowed
= false;
1348 $plan3->isapproveallowed
= false;
1349 $plan1->isunapproveallowed
= false;
1350 $plan2->isunapproveallowed
= false;
1351 $plan3->isunapproveallowed
= false;
1352 $plan3->isreopenallowed
= false;
1353 $plan1->commentarea
['canpost'] = false;
1354 $plan1->commentarea
['canview'] = true;
1356 // Prevent the user from seeing their own non-draft plans.
1357 assign_capability('moodle/competency:plancommentown', CAP_PROHIBIT
, $this->userrole
, $syscontext->id
, true);
1358 assign_capability('moodle/competency:planviewown', CAP_PROHIBIT
, $this->userrole
, $syscontext->id
, true);
1359 assign_capability('moodle/competency:planviewowndraft', CAP_ALLOW
, $this->userrole
, $syscontext->id
, true);
1360 accesslib_clear_all_caches_for_unit_testing();
1362 $this->assertEquals((array)$plan1, external
::read_plan($plan1->id
));
1365 external
::read_plan($plan2->id
);
1366 $this->fail('Exception expected due to not permissions to read plan');
1367 } catch (moodle_exception
$e) {
1368 $this->assertEquals('nopermissions', $e->errorcode
);
1371 external
::read_plan($plan3->id
);
1372 $this->fail('Exception expected due to not permissions to read plan');
1373 } catch (moodle_exception
$e) {
1374 $this->assertEquals('nopermissions', $e->errorcode
);
1377 // Allow user to see their plan.
1378 assign_capability('moodle/competency:plancommentown', CAP_ALLOW
, $this->userrole
, $syscontext->id
, true);
1379 assign_capability('moodle/competency:planviewown', CAP_ALLOW
, $this->userrole
, $syscontext->id
, true);
1380 assign_capability('moodle/competency:planmanageowndraft', CAP_PROHIBIT
, $this->userrole
, $syscontext->id
, true);
1381 accesslib_clear_all_caches_for_unit_testing();
1383 $plan1->commentarea
['canpost'] = true;
1384 $plan1->commentarea
['canview'] = true;
1385 $plan2->commentarea
['canpost'] = true;
1386 $plan2->isrequestreviewallowed
= false;
1387 $plan3->commentarea
['canpost'] = true;
1388 $plan3->isrequestreviewallowed
= false;
1389 $plan1->commentarea
['canpostorhascomments'] = true;
1390 $plan2->commentarea
['canpostorhascomments'] = true;
1391 $plan3->commentarea
['canpostorhascomments'] = true;
1393 $this->assertEquals((array)$plan1, external
::read_plan($plan1->id
));
1394 $this->assertEquals((array)$plan2, external
::read_plan($plan2->id
));
1395 $this->assertEquals((array)$plan3, external
::read_plan($plan3->id
));
1397 // Allow use to manage their own draft plan.
1398 assign_capability('moodle/competency:planviewown', CAP_PROHIBIT
, $this->userrole
, $syscontext->id
, true);
1399 assign_capability('moodle/competency:planmanageown', CAP_PROHIBIT
, $this->userrole
, $syscontext->id
, true);
1400 assign_capability('moodle/competency:planmanageowndraft', CAP_ALLOW
, $this->userrole
, $syscontext->id
, true);
1401 accesslib_clear_all_caches_for_unit_testing();
1403 $plan1->canmanage
= true;
1404 $plan1->canbeedited
= true;
1405 $plan1->canrequestreview
= true;
1406 $plan1->isrequestreviewallowed
= true;
1407 $this->assertEquals((array)$plan1, external
::read_plan($plan1->id
));
1409 external
::read_plan($plan2->id
);
1410 $this->fail('Exception expected due to not permissions to read plan');
1411 } catch (moodle_exception
$e) {
1412 $this->assertEquals('nopermissions', $e->errorcode
);
1415 external
::read_plan($plan3->id
);
1416 $this->fail('Exception expected due to not permissions to read plan');
1417 } catch (moodle_exception
$e) {
1418 $this->assertEquals('nopermissions', $e->errorcode
);
1421 // Allow use to manage their plan.
1422 assign_capability('moodle/competency:planviewown', CAP_PROHIBIT
, $this->userrole
, $syscontext->id
, true);
1423 assign_capability('moodle/competency:planmanageowndraft', CAP_PROHIBIT
, $this->userrole
, $syscontext->id
, true);
1424 assign_capability('moodle/competency:planmanageown', CAP_ALLOW
, $this->userrole
, $syscontext->id
, true);
1425 accesslib_clear_all_caches_for_unit_testing();
1427 $plan1->canmanage
= false;
1428 $plan1->canbeedited
= false;
1429 $plan1->canrequestreview
= true;
1430 $plan1->canreview
= true;
1431 $plan1->isrequestreviewallowed
= true;
1432 $plan1->isapproveallowed
= true;
1433 $plan1->iscompleteallowed
= false;
1435 $plan2->canmanage
= true;
1436 $plan2->canbeedited
= true;
1437 $plan2->canreview
= true;
1438 $plan2->iscompleteallowed
= true;
1439 $plan2->isunapproveallowed
= true;
1441 $plan3->canmanage
= true;
1442 $plan3->canreview
= true;
1443 $plan3->isreopenallowed
= true;
1445 $this->assertEquals((array)$plan1, external
::read_plan($plan1->id
));
1446 $this->assertEquals((array)$plan2, external
::read_plan($plan2->id
));
1447 $this->assertEquals((array)$plan3, external
::read_plan($plan3->id
));
1450 public function test_delete_plans() {
1451 $this->setUser($this->creator
);
1453 $syscontext = context_system
::instance();
1455 $plan1 = $this->create_plan(1, $this->user
->id
, 0, plan
::STATUS_ACTIVE
, 0);
1456 $plan2 = $this->create_plan(2, $this->user
->id
, 0, plan
::STATUS_ACTIVE
, 0);
1457 $plan3 = $this->create_plan(3, $this->creator
->id
, 0, plan
::STATUS_ACTIVE
, 0);
1459 $this->assertTrue(external
::delete_plan($plan1->id
));
1461 unassign_capability('moodle/competency:planmanage', $this->creatorrole
, $syscontext->id
);
1462 accesslib_clear_all_caches_for_unit_testing();
1465 external
::delete_plan($plan2->id
);
1466 $this->fail('Exception expected due to not permissions to manage plans');
1467 } catch (moodle_exception
$e) {
1468 $this->assertEquals('nopermissions', $e->errorcode
);
1471 $this->setUser($this->user
);
1473 // Can not delete plans created by other users.
1475 external
::delete_plan($plan2->id
);
1476 $this->fail('Exception expected due to not permissions to manage plans');
1477 } catch (moodle_exception
$e) {
1478 $this->assertEquals('nopermissions', $e->errorcode
);
1481 assign_capability('moodle/competency:planmanageown', CAP_ALLOW
, $this->userrole
, $syscontext->id
);
1482 accesslib_clear_all_caches_for_unit_testing();
1484 $this->assertTrue(external
::delete_plan($plan2->id
));
1486 // Can not delete plans created for other users.
1488 external
::delete_plan($plan3->id
);
1489 $this->fail('Exception expected due to not permissions to manage plans');
1490 } catch (moodle_exception
$e) {
1491 $this->assertEquals('nopermissions', $e->errorcode
);
1494 $plan4 = $this->create_plan(4, $this->user
->id
, 0, plan
::STATUS_ACTIVE
, 0);
1495 $this->assertTrue(external
::delete_plan($plan4->id
));
1498 public function test_delete_plan_removes_relations() {
1499 $this->setAdminUser();
1500 $dg = $this->getDataGenerator();
1501 $lpg = $dg->get_plugin_generator('core_competency');
1503 $user = $dg->create_user();
1504 $plan = $lpg->create_plan(array('userid' => $user->id
));
1505 $framework = $lpg->create_framework();
1506 $comp1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
1507 $comp2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
1508 $comp3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
1509 $pc1 = $lpg->create_plan_competency(array('planid' => $plan->get('id'), 'competencyid' => $comp1->get('id')));
1510 $pc2 = $lpg->create_plan_competency(array('planid' => $plan->get('id'), 'competencyid' => $comp2->get('id')));
1511 $pc3 = $lpg->create_plan_competency(array('planid' => $plan->get('id'), 'competencyid' => $comp3->get('id')));
1513 // Complete the plan to generate user_competency_plan entries.
1514 api
::complete_plan($plan);
1516 // Confirm the data we have.
1517 $this->assertEquals(3, plan_competency
::count_records(array('planid' => $plan->get('id'))));
1518 $this->assertEquals(3, user_competency_plan
::count_records(array('planid' => $plan->get('id'), 'userid' => $user->id
)));
1520 // Delete the plan now.
1521 api
::delete_plan($plan->get('id'));
1522 $this->assertEquals(0, plan_competency
::count_records(array('planid' => $plan->get('id'))));
1523 $this->assertEquals(0, user_competency_plan
::count_records(array('planid' => $plan->get('id'), 'userid' => $user->id
)));
1526 public function test_list_plan_competencies() {
1527 $this->setUser($this->creator
);
1529 $dg = $this->getDataGenerator();
1530 $lpg = $dg->get_plugin_generator('core_competency');
1532 $f1 = $lpg->create_framework();
1533 $f2 = $lpg->create_framework();
1535 $c1a = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
1536 $c1b = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
1537 $c1c = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
1538 $c2a = $lpg->create_competency(array('competencyframeworkid' => $f2->get('id')));
1539 $c2b = $lpg->create_competency(array('competencyframeworkid' => $f2->get('id')));
1541 $tpl = $lpg->create_template();
1542 $lpg->create_template_competency(array('templateid' => $tpl->get('id'), 'competencyid' => $c1a->get('id')));
1543 $lpg->create_template_competency(array('templateid' => $tpl->get('id'), 'competencyid' => $c1c->get('id')));
1544 $lpg->create_template_competency(array('templateid' => $tpl->get('id'), 'competencyid' => $c2b->get('id')));
1546 $plan = $lpg->create_plan(array('userid' => $this->user
->id
, 'templateid' => $tpl->get('id')));
1548 $uc1a = $lpg->create_user_competency(array('userid' => $this->user
->id
, 'competencyid' => $c1a->get('id'),
1549 'status' => user_competency
::STATUS_IN_REVIEW
, 'reviewerid' => $this->creator
->id
));
1550 $uc1b = $lpg->create_user_competency(array('userid' => $this->user
->id
, 'competencyid' => $c1b->get('id')));
1551 $uc2b = $lpg->create_user_competency(array('userid' => $this->user
->id
, 'competencyid' => $c2b->get('id'),
1552 'grade' => 2, 'proficiency' => 1));
1553 $ux1a = $lpg->create_user_competency(array('userid' => $this->creator
->id
, 'competencyid' => $c1a->get('id')));
1555 $result = external
::list_plan_competencies($plan->get('id'));
1556 $result = external
::clean_returnvalue(external
::list_plan_competencies_returns(), $result);
1558 $this->assertCount(3, $result);
1559 $this->assertEquals($c1a->get('id'), $result[0]['competency']['id']);
1560 $this->assertEquals($this->user
->id
, $result[0]['usercompetency']['userid']);
1561 $this->assertArrayNotHasKey('usercompetencyplan', $result[0]);
1562 $this->assertEquals($c1c->get('id'), $result[1]['competency']['id']);
1563 $this->assertEquals($this->user
->id
, $result[1]['usercompetency']['userid']);
1564 $this->assertArrayNotHasKey('usercompetencyplan', $result[1]);
1565 $this->assertEquals($c2b->get('id'), $result[2]['competency']['id']);
1566 $this->assertEquals($this->user
->id
, $result[2]['usercompetency']['userid']);
1567 $this->assertArrayNotHasKey('usercompetencyplan', $result[2]);
1568 $this->assertEquals(user_competency
::STATUS_IN_REVIEW
, $result[0]['usercompetency']['status']);
1569 $this->assertEquals(null, $result[1]['usercompetency']['grade']);
1570 $this->assertEquals(2, $result[2]['usercompetency']['grade']);
1571 $this->assertEquals(1, $result[2]['usercompetency']['proficiency']);
1573 // Check the return values when the plan status is complete.
1574 $completedplan = $lpg->create_plan(array('userid' => $this->user
->id
, 'templateid' => $tpl->get('id'),
1575 'status' => plan
::STATUS_COMPLETE
));
1577 $uc1a = $lpg->create_user_competency_plan(array('userid' => $this->user
->id
, 'competencyid' => $c1a->get('id'),
1578 'planid' => $completedplan->get('id')));
1579 $uc1b = $lpg->create_user_competency_plan(array('userid' => $this->user
->id
, 'competencyid' => $c1c->get('id'),
1580 'planid' => $completedplan->get('id')));
1581 $uc2b = $lpg->create_user_competency_plan(array('userid' => $this->user
->id
, 'competencyid' => $c2b->get('id'),
1582 'planid' => $completedplan->get('id'), 'grade' => 2, 'proficiency' => 1));
1583 $ux1a = $lpg->create_user_competency_plan(array('userid' => $this->creator
->id
, 'competencyid' => $c1a->get('id'),
1584 'planid' => $completedplan->get('id')));
1586 $result = external
::list_plan_competencies($completedplan->get('id'));
1587 $result = external
::clean_returnvalue(external
::list_plan_competencies_returns(), $result);
1589 $this->assertCount(3, $result);
1590 $this->assertEquals($c1a->get('id'), $result[0]['competency']['id']);
1591 $this->assertEquals($this->user
->id
, $result[0]['usercompetencyplan']['userid']);
1592 $this->assertArrayNotHasKey('usercompetency', $result[0]);
1593 $this->assertEquals($c1c->get('id'), $result[1]['competency']['id']);
1594 $this->assertEquals($this->user
->id
, $result[1]['usercompetencyplan']['userid']);
1595 $this->assertArrayNotHasKey('usercompetency', $result[1]);
1596 $this->assertEquals($c2b->get('id'), $result[2]['competency']['id']);
1597 $this->assertEquals($this->user
->id
, $result[2]['usercompetencyplan']['userid']);
1598 $this->assertArrayNotHasKey('usercompetency', $result[2]);
1599 $this->assertEquals(null, $result[1]['usercompetencyplan']['grade']);
1600 $this->assertEquals(2, $result[2]['usercompetencyplan']['grade']);
1601 $this->assertEquals(1, $result[2]['usercompetencyplan']['proficiency']);
1604 public function test_add_competency_to_template() {
1605 $this->setUser($this->creator
);
1607 $syscontext = context_system
::instance();
1609 // Create a template.
1610 $template = $this->create_template(1, true);
1612 // Create a competency.
1613 $framework = $this->create_competency_framework(1, true);
1614 $competency = $this->create_competency(1, $framework->id
);
1616 // Add the competency.
1617 external
::add_competency_to_template($template->id
, $competency->id
);
1619 // Check that it was added.
1620 $this->assertEquals(1, external
::count_competencies_in_template($template->id
));
1622 // Unassign capability.
1623 unassign_capability('moodle/competency:templatemanage', $this->creatorrole
, $syscontext->id
);
1624 accesslib_clear_all_caches_for_unit_testing();
1626 // Check we can not add the competency now.
1628 external
::add_competency_to_template($template->id
, $competency->id
);
1629 $this->fail('Exception expected due to not permissions to manage template competencies');
1630 } catch (moodle_exception
$e) {
1631 $this->assertEquals('nopermissions', $e->errorcode
);
1635 public function test_remove_competency_from_template() {
1636 $syscontext = context_system
::instance();
1637 $this->setUser($this->creator
);
1638 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
1640 // Create a template.
1641 $template = $this->create_template(1, true);
1643 // Create a competency.
1644 $framework = $lpg->create_framework();
1645 $competency = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
1647 // Add the competency.
1648 external
::add_competency_to_template($template->id
, $competency->get('id'));
1650 // Check that it was added.
1651 $this->assertEquals(1, external
::count_competencies_in_template($template->id
));
1653 // Check that we can remove the competency.
1654 external
::remove_competency_from_template($template->id
, $competency->get('id'));
1656 // Check that it was removed.
1657 $this->assertEquals(0, external
::count_competencies_in_template($template->id
));
1659 // Unassign capability.
1660 unassign_capability('moodle/competency:templatemanage', $this->creatorrole
, $syscontext->id
);
1661 accesslib_clear_all_caches_for_unit_testing();
1663 // Check we can not remove the competency now.
1665 external
::add_competency_to_template($template->id
, $competency->get('id'));
1666 $this->fail('Exception expected due to not permissions to manage template competencies');
1667 } catch (moodle_exception
$e) {
1668 $this->assertEquals('nopermissions', $e->errorcode
);
1673 * Test we can re-order competency frameworks.
1675 public function test_reorder_template_competencies() {
1676 $this->setUser($this->creator
);
1678 $syscontext = context_system
::instance();
1679 $onehour = time() +
60 * 60;
1681 // Create a template.
1682 $template = $this->create_template(1, true);
1684 // Create a competency framework.
1685 $framework = $this->create_competency_framework(1, true);
1687 // Create multiple competencies.
1688 $competency1 = $this->create_competency(1, $framework->id
);
1689 $competency2 = $this->create_competency(2, $framework->id
);
1690 $competency3 = $this->create_competency(3, $framework->id
);
1691 $competency4 = $this->create_competency(4, $framework->id
);
1693 // Add the competencies.
1694 external
::add_competency_to_template($template->id
, $competency1->id
);
1695 external
::add_competency_to_template($template->id
, $competency2->id
);
1696 external
::add_competency_to_template($template->id
, $competency3->id
);
1697 external
::add_competency_to_template($template->id
, $competency4->id
);
1699 // Test if removing competency from template don't create sortorder holes.
1700 external
::remove_competency_from_template($template->id
, $competency3->id
);
1701 $templcomp4 = template_competency
::get_record(array(
1702 'templateid' => $template->id
,
1703 'competencyid' => $competency4->id
1706 $this->assertEquals(2, $templcomp4->get('sortorder'));
1708 // This is a move up.
1709 external
::reorder_template_competency($template->id
, $competency4->id
, $competency2->id
);
1710 $result = external
::list_competencies_in_template($template->id
);
1711 $result = external_api
::clean_returnvalue(external
::list_competencies_in_template_returns(), $result);
1713 $r1 = (object) $result[0];
1714 $r2 = (object) $result[1];
1715 $r3 = (object) $result[2];
1717 $this->assertEquals($competency1->id
, $r1->id
);
1718 $this->assertEquals($competency4->id
, $r2->id
);
1719 $this->assertEquals($competency2->id
, $r3->id
);
1721 // This is a move down.
1722 external
::reorder_template_competency($template->id
, $competency1->id
, $competency4->id
);
1723 $result = external
::list_competencies_in_template($template->id
);
1724 $result = external_api
::clean_returnvalue(external
::list_competencies_in_template_returns(), $result);
1726 $r1 = (object) $result[0];
1727 $r2 = (object) $result[1];
1728 $r3 = (object) $result[2];
1730 $this->assertEquals($competency4->id
, $r1->id
);
1731 $this->assertEquals($competency1->id
, $r2->id
);
1732 $this->assertEquals($competency2->id
, $r3->id
);
1734 $this->expectException('required_capability_exception');
1735 $this->setUser($this->user
);
1736 external
::reorder_template_competency($template->id
, $competency1->id
, $competency2->id
);
1740 * Test we can duplicate learning plan template.
1742 public function test_duplicate_learning_plan_template() {
1743 $this->setUser($this->creator
);
1745 $syscontext = context_system
::instance();
1746 $onehour = time() +
60 * 60;
1748 // Create a template.
1749 $template = $this->create_template(1, true);
1751 // Create a competency framework.
1752 $framework = $this->create_competency_framework(1, true);
1754 // Create multiple competencies.
1755 $competency1 = $this->create_competency(1, $framework->id
);
1756 $competency2 = $this->create_competency(2, $framework->id
);
1757 $competency3 = $this->create_competency(3, $framework->id
);
1759 // Add the competencies.
1760 external
::add_competency_to_template($template->id
, $competency1->id
);
1761 external
::add_competency_to_template($template->id
, $competency2->id
);
1762 external
::add_competency_to_template($template->id
, $competency3->id
);
1764 // Duplicate the learning plan template.
1765 $duplicatedtemplate = external
::duplicate_template($template->id
);
1767 $result = external
::list_competencies_in_template($template->id
);
1768 $resultduplicated = external
::list_competencies_in_template($duplicatedtemplate->id
);
1770 $this->assertEquals(count($result), count($resultduplicated));
1771 $this->assertContains($template->shortname
, $duplicatedtemplate->shortname
);
1772 $this->assertEquals($duplicatedtemplate->description
, $template->description
);
1773 $this->assertEquals($duplicatedtemplate->descriptionformat
, $template->descriptionformat
);
1774 $this->assertEquals($duplicatedtemplate->visible
, $template->visible
);
1778 * Test that we can return scale values for a scale with the scale ID.
1780 public function test_get_scale_values() {
1783 $this->setUser($this->creator
);
1786 $record = new stdClass();
1787 $record->courseid
= 0;
1788 $record->userid
= $this->creator
->id
;
1789 $record->name
= 'Test scale';
1790 $record->scale
= 'Poor, Not good, Okay, Fine, Excellent';
1791 $record->description
= '<p>Test scale description.</p>';
1792 $record->descriptionformat
= 1;
1793 $record->timemodified
= time();
1794 $scaleid = $DB->insert_record('scale', $record);
1795 // Expected return value.
1796 $expected = array(array(
1801 'name' => 'Not good'
1810 'name' => 'Excellent'
1813 // Call the webservice.
1814 $result = external
::get_scale_values($scaleid);
1815 $this->assertEquals($expected, $result);
1819 * Create a template.
1821 public function test_create_template() {
1822 $syscontextid = context_system
::instance()->id
;
1823 $catcontextid = context_coursecat
::instance($this->category
->id
)->id
;
1825 // A user without permission.
1826 $this->setUser($this->user
);
1828 $result = $this->create_template(1, true);
1829 $this->fail('Invalid permissions');
1830 } catch (required_capability_exception
$e) {
1834 // A user without permission in a category.
1835 $this->setUser($this->catuser
);
1837 $result = $this->create_template(1, false);
1838 $this->fail('Invalid permissions');
1839 } catch (required_capability_exception
$e) {
1843 // A user with permissions in the system.
1844 $this->setUser($this->creator
);
1845 $result = $this->create_template(1, true);
1846 $this->assertEquals('shortname1', $result->shortname
);
1847 $this->assertEquals($syscontextid, $result->contextid
);
1848 $this->assertNotEmpty($result->id
);
1850 $result = $this->create_template(2, false);
1851 $this->assertEquals('shortname2', $result->shortname
);
1852 $this->assertEquals($catcontextid, $result->contextid
);
1853 $this->assertNotEmpty($result->id
);
1855 // A user with permissions in the category.
1856 $this->setUser($this->catcreator
);
1858 $result = $this->create_template(3, true);
1859 $this->fail('Invalid permissions');
1860 } catch (required_capability_exception
$e) {
1864 $result = $this->create_template(3, false);
1865 $this->assertEquals('shortname3', $result->shortname
);
1866 $this->assertEquals($catcontextid, $result->contextid
);
1867 $this->assertNotEmpty($result->id
);
1873 public function test_read_template() {
1874 $syscontextid = context_system
::instance()->id
;
1875 $catcontextid = context_coursecat
::instance($this->category
->id
)->id
;
1877 // Set a due date for the next year.
1878 $date = new DateTime('now');
1879 $date->modify('+1 year');
1880 $duedate = $date->getTimestamp();
1882 // Creating two templates.
1883 $this->setUser($this->creator
);
1884 $systemplate = $this->create_template(1, true);
1885 $cattemplate = $this->create_template(2, false);
1887 // User without permissions to read in system.
1888 assign_capability('moodle/competency:templateview', CAP_PROHIBIT
, $this->userrole
, $syscontextid, true);
1889 accesslib_clear_all_caches_for_unit_testing();
1890 $this->setUser($this->user
);
1891 $this->assertFalse(has_capability('moodle/competency:templateview', context_system
::instance()));
1893 external
::read_template($systemplate->id
);
1894 $this->fail('Invalid permissions');
1895 } catch (required_capability_exception
$e) {
1899 external
::read_template($cattemplate->id
);
1900 $this->fail('Invalid permissions');
1901 } catch (required_capability_exception
$e) {
1905 // User with permissions to read in a category.
1906 assign_capability('moodle/competency:templateview', CAP_PREVENT
, $this->userrole
, $syscontextid, true);
1907 assign_capability('moodle/competency:templateview', CAP_ALLOW
, $this->userrole
, $catcontextid, true);
1908 accesslib_clear_all_caches_for_unit_testing();
1909 $this->assertFalse(has_capability('moodle/competency:templateview', context_system
::instance()));
1910 $this->assertTrue(has_capability('moodle/competency:templateview', context_coursecat
::instance($this->category
->id
)));
1912 external
::read_template($systemplate->id
);
1913 $this->fail('Invalid permissions');
1914 } catch (required_capability_exception
$e) {
1918 $result = external
::read_template($cattemplate->id
);
1919 $result = external_api
::clean_returnvalue(external
::read_template_returns(), $result);
1920 $this->assertEquals($cattemplate->id
, $result['id']);
1921 $this->assertEquals('shortname2', $result['shortname']);
1922 $this->assertEquals('description2', $result['description']);
1923 $this->assertEquals(FORMAT_HTML
, $result['descriptionformat']);
1924 $this->assertEquals(1, $result['visible']);
1925 $this->assertEquals(0, $result['duedate']);
1926 $this->assertEquals(userdate(0), $result['duedateformatted']);
1928 // User with permissions to read in the system.
1929 assign_capability('moodle/competency:templateview', CAP_ALLOW
, $this->userrole
, $syscontextid, true);
1930 accesslib_clear_all_caches_for_unit_testing();
1931 $this->assertTrue(has_capability('moodle/competency:templateview', context_system
::instance()));
1932 $result = external
::read_template($systemplate->id
);
1933 $result = external_api
::clean_returnvalue(external
::read_template_returns(), $result);
1934 $this->assertEquals($systemplate->id
, $result['id']);
1935 $this->assertEquals('shortname1', $result['shortname']);
1936 $this->assertEquals('description1', $result['description']);
1937 $this->assertEquals(FORMAT_HTML
, $result['descriptionformat']);
1938 $this->assertEquals(true, $result['visible']);
1939 $this->assertEquals(0, $result['duedate']);
1940 $this->assertEquals(userdate(0), $result['duedateformatted']);
1942 $result = external
::read_template($cattemplate->id
);
1943 $result = external_api
::clean_returnvalue(external
::read_template_returns(), $result);
1944 $this->assertEquals($cattemplate->id
, $result['id']);
1945 $this->assertEquals('shortname2', $result['shortname']);
1946 $this->assertEquals('description2', $result['description']);
1947 $this->assertEquals(FORMAT_HTML
, $result['descriptionformat']);
1948 $this->assertEquals(true, $result['visible']);
1949 $this->assertEquals(0, $result['duedate']);
1950 $this->assertEquals(userdate(0), $result['duedateformatted']);
1954 * Update a template.
1956 public function test_update_template() {
1957 $syscontextid = context_system
::instance()->id
;
1958 $catcontextid = context_coursecat
::instance($this->category
->id
)->id
;
1960 // Set a due date for the next year.
1961 $date = new DateTime('now');
1962 $date->modify('+1 year');
1963 $duedate = $date->getTimestamp();
1965 // Creating two templates.
1966 $this->setUser($this->creator
);
1967 $systemplate = $this->create_template(1, true);
1968 $cattemplate = $this->create_template(2, false);
1970 // Trying to update in a without permissions.
1971 $this->setUser($this->user
);
1973 $this->update_template($systemplate->id
, 3);
1974 $this->fail('Invalid permissions');
1975 } catch (required_capability_exception
$e) {
1980 $this->update_template($cattemplate->id
, 3);
1981 $this->fail('Invalid permissions');
1982 } catch (required_capability_exception
$e) {
1986 // User with permissions to update in category.
1987 $this->setUser($this->catcreator
);
1989 $this->update_template($systemplate->id
, 3);
1990 $this->fail('Invalid permissions');
1991 } catch (required_capability_exception
$e) {
1995 $result = $this->update_template($cattemplate->id
, 3);
1996 $this->assertTrue($result);
1997 $result = external
::read_template($cattemplate->id
);
1998 $result = external_api
::clean_returnvalue(external
::read_template_returns(), $result);
1999 $this->assertEquals($cattemplate->id
, $result['id']);
2000 $this->assertEquals('shortname3', $result['shortname']);
2001 $this->assertEquals("description3", $result['description']);
2002 $this->assertEquals(FORMAT_HTML
, $result['descriptionformat']);
2003 $this->assertEquals(true, $result['visible']);
2004 $this->assertEquals(0, $result['duedate']);
2005 $this->assertEquals(userdate(0), $result['duedateformatted']);
2007 // User with permissions to update in the system.
2008 $this->setUser($this->creator
);
2009 $result = $this->update_template($systemplate->id
, 4);
2010 $this->assertTrue($result);
2011 $result = external
::read_template($systemplate->id
);
2012 $result = external_api
::clean_returnvalue(external
::read_template_returns(), $result);
2013 $this->assertEquals($systemplate->id
, $result['id']);
2014 $this->assertEquals('shortname4', $result['shortname']);
2015 $this->assertEquals('description4', $result['description']);
2016 $this->assertEquals(FORMAT_HTML
, $result['descriptionformat']);
2017 $this->assertEquals(true, $result['visible']);
2018 $this->assertEquals(0, $result['duedate']);
2019 $this->assertEquals(userdate(0), $result['duedateformatted']);
2021 $result = $this->update_template($cattemplate->id
, 5);
2022 $this->assertTrue($result);
2023 $result = external
::read_template($cattemplate->id
);
2024 $result = external_api
::clean_returnvalue(external
::read_template_returns(), $result);
2025 $this->assertEquals($cattemplate->id
, $result['id']);
2026 $this->assertEquals('shortname5', $result['shortname']);
2027 $this->assertEquals('description5', $result['description']);
2028 $this->assertEquals(FORMAT_HTML
, $result['descriptionformat']);
2029 $this->assertEquals(1, $result['visible']);
2030 $this->assertEquals(0, $result['duedate']);
2031 $this->assertEquals(userdate(0), $result['duedateformatted']);
2035 * Delete a template.
2037 public function test_delete_template() {
2039 $syscontextid = context_system
::instance()->id
;
2040 $catcontextid = context_coursecat
::instance($this->category
->id
)->id
;
2042 // Creating a few templates.
2043 $this->setUser($this->creator
);
2044 $sys1 = $this->create_template(1, true);
2045 $cat1 = $this->create_template(2, false);
2046 $cat2 = $this->create_template(3, false);
2047 $this->assertTrue($DB->record_exists(template
::TABLE
, array('id' => $sys1->id
)));
2048 $this->assertTrue($DB->record_exists(template
::TABLE
, array('id' => $cat1->id
)));
2049 $this->assertTrue($DB->record_exists(template
::TABLE
, array('id' => $cat2->id
)));
2051 // User without permissions.
2052 $this->setUser($this->user
);
2054 external
::delete_template($sys1->id
);
2055 $this->fail('Invalid permissions');
2056 } catch (required_capability_exception
$e) {
2060 external
::delete_template($cat1->id
);
2061 $this->fail('Invalid permissions');
2062 } catch (required_capability_exception
$e) {
2066 // User with category permissions.
2067 $this->setUser($this->catcreator
);
2069 external
::delete_template($sys1->id
);
2070 $this->fail('Invalid permissions');
2071 } catch (required_capability_exception
$e) {
2075 $result = external
::delete_template($cat1->id
);
2076 $result = external_api
::clean_returnvalue(external
::delete_template_returns(), $result);
2077 $this->assertTrue($result);
2078 $this->assertFalse($DB->record_exists(template
::TABLE
, array('id' => $cat1->id
)));
2080 // User with system permissions.
2081 $this->setUser($this->creator
);
2082 $result = external
::delete_template($sys1->id
);
2083 $result = external_api
::clean_returnvalue(external
::delete_template_returns(), $result);
2084 $this->assertTrue($result);
2085 $result = external
::delete_template($cat2->id
);
2086 $result = external_api
::clean_returnvalue(external
::delete_template_returns(), $result);
2087 $this->assertTrue($result);
2088 $this->assertFalse($DB->record_exists(template
::TABLE
, array('id' => $sys1->id
)));
2089 $this->assertFalse($DB->record_exists(template
::TABLE
, array('id' => $cat2->id
)));
2095 public function test_list_templates() {
2096 $syscontextid = context_system
::instance()->id
;
2097 $catcontextid = context_coursecat
::instance($this->category
->id
)->id
;
2099 // Creating a few templates.
2100 $this->setUser($this->creator
);
2101 $sys1 = $this->create_template(1, true);
2102 $sys2 = $this->create_template(2, true);
2103 $cat1 = $this->create_template(3, false);
2104 $cat2 = $this->create_template(4, false);
2106 // User without permission.
2107 $this->setUser($this->user
);
2108 assign_capability('moodle/competency:templateview', CAP_PROHIBIT
, $this->userrole
, $syscontextid, true);
2109 accesslib_clear_all_caches_for_unit_testing();
2111 external
::list_templates('id', 'ASC', 0, 10, array('contextid' => $syscontextid), 'children', false);
2112 $this->fail('Invalid permissions');
2113 } catch (required_capability_exception
$e) {
2117 // User with category permissions.
2118 assign_capability('moodle/competency:templateview', CAP_PREVENT
, $this->userrole
, $syscontextid, true);
2119 assign_capability('moodle/competency:templateview', CAP_ALLOW
, $this->userrole
, $catcontextid, true);
2120 accesslib_clear_all_caches_for_unit_testing();
2121 $result = external
::list_templates('id', 'ASC', 0, 10, array('contextid' => $syscontextid), 'children', false);
2122 $result = external_api
::clean_returnvalue(external
::list_templates_returns(), $result);
2123 $this->assertCount(2, $result);
2124 $this->assertEquals($cat1->id
, $result[0]['id']);
2125 $this->assertEquals($cat2->id
, $result[1]['id']);
2127 // User with system permissions.
2128 assign_capability('moodle/competency:templateview', CAP_ALLOW
, $this->userrole
, $syscontextid, true);
2129 accesslib_clear_all_caches_for_unit_testing();
2130 $result = external
::list_templates('id', 'DESC', 0, 3, array('contextid' => $catcontextid), 'parents', false);
2131 $result = external_api
::clean_returnvalue(external
::list_templates_returns(), $result);
2132 $this->assertCount(3, $result);
2133 $this->assertEquals($cat2->id
, $result[0]['id']);
2134 $this->assertEquals($cat1->id
, $result[1]['id']);
2135 $this->assertEquals($sys2->id
, $result[2]['id']);
2139 * List templates using competency.
2141 public function test_list_templates_using_competency() {
2142 $this->setUser($this->creator
);
2144 // Create a template.
2145 $template1 = $this->create_template(1, true);
2146 $template2 = $this->create_template(2, true);
2147 $template3 = $this->create_template(3, true);
2148 $template4 = $this->create_template(4, true);
2150 // Create a competency.
2151 $framework = $this->create_competency_framework(1, true);
2152 $competency1 = $this->create_competency(1, $framework->id
);
2153 $competency2 = $this->create_competency(2, $framework->id
);
2155 // Add the competency.
2156 external
::add_competency_to_template($template1->id
, $competency1->id
);
2157 external
::add_competency_to_template($template2->id
, $competency1->id
);
2158 external
::add_competency_to_template($template3->id
, $competency1->id
);
2160 external
::add_competency_to_template($template4->id
, $competency2->id
);
2162 $listcomp1 = external
::list_templates_using_competency($competency1->id
);
2163 $listcomp2 = external
::list_templates_using_competency($competency2->id
);
2165 // Test count_templates_using_competency.
2166 $counttempcomp1 = external
::count_templates_using_competency($competency1->id
);
2167 $counttempcomp2 = external
::count_templates_using_competency($competency2->id
);
2169 $comptemp1 = $listcomp1[0];
2170 $comptemp2 = $listcomp1[1];
2171 $comptemp3 = $listcomp1[2];
2173 $comptemp4 = $listcomp2[0];
2175 $this->assertCount(3, $listcomp1);
2176 $this->assertCount(1, $listcomp2);
2177 $this->assertEquals(3, $counttempcomp1);
2178 $this->assertEquals(1, $counttempcomp2);
2179 $this->assertEquals($template1->id
, $comptemp1->id
);
2180 $this->assertEquals($template2->id
, $comptemp2->id
);
2181 $this->assertEquals($template3->id
, $comptemp3->id
);
2182 $this->assertEquals($template4->id
, $comptemp4->id
);
2185 public function test_count_templates() {
2186 $syscontextid = context_system
::instance()->id
;
2187 $catcontextid = context_coursecat
::instance($this->category
->id
)->id
;
2189 // Creating a few templates.
2190 $this->setUser($this->creator
);
2191 $sys1 = $this->create_template(1, true);
2192 $sys2 = $this->create_template(2, true);
2193 $cat1 = $this->create_template(3, false);
2194 $cat2 = $this->create_template(4, false);
2195 $cat3 = $this->create_template(5, false);
2197 // User without permission.
2198 $this->setUser($this->user
);
2199 assign_capability('moodle/competency:templateview', CAP_PROHIBIT
, $this->userrole
, $syscontextid, true);
2200 accesslib_clear_all_caches_for_unit_testing();
2202 external
::count_templates(array('contextid' => $syscontextid), 'children');
2203 $this->fail('Invalid permissions');
2204 } catch (required_capability_exception
$e) {
2208 // User with category permissions.
2209 assign_capability('moodle/competency:templateview', CAP_PREVENT
, $this->userrole
, $syscontextid, true);
2210 assign_capability('moodle/competency:templateview', CAP_ALLOW
, $this->userrole
, $catcontextid, true);
2211 accesslib_clear_all_caches_for_unit_testing();
2212 $result = external
::count_templates(array('contextid' => $syscontextid), 'children');
2213 $result = external_api
::clean_returnvalue(external
::count_templates_returns(), $result);
2214 $this->assertEquals(3, $result);
2216 // User with system permissions.
2217 assign_capability('moodle/competency:templateview', CAP_ALLOW
, $this->userrole
, $syscontextid, true);
2218 accesslib_clear_all_caches_for_unit_testing();
2219 $result = external
::count_templates(array('contextid' => $catcontextid), 'parents');
2220 $result = external_api
::clean_returnvalue(external
::count_templates_returns(), $result);
2221 $this->assertEquals(5, $result);
2225 * Test that we can add related competencies.
2229 public function test_add_related_competency() {
2231 $this->setUser($this->creator
);
2233 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
2234 $framework = $lpg->create_framework();
2235 $framework2 = $lpg->create_framework();
2236 $competency1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2237 $competency2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2238 $competency3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2239 $competency4 = $lpg->create_competency(array('competencyframeworkid' => $framework2->get('id')));
2241 // The lower one always as competencyid.
2242 $result = external
::add_related_competency($competency1->get('id'), $competency2->get('id'));
2243 $result = external_api
::clean_returnvalue(external
::add_related_competency_returns(), $result);
2244 $this->assertTrue($result);
2245 $this->assertTrue($DB->record_exists_select(
2246 related_competency
::TABLE
, 'competencyid = :cid AND relatedcompetencyid = :rid',
2248 'cid' => $competency1->get('id'),
2249 'rid' => $competency2->get('id')
2252 $this->assertFalse($DB->record_exists_select(
2253 related_competency
::TABLE
, 'competencyid = :cid AND relatedcompetencyid = :rid',
2255 'cid' => $competency2->get('id'),
2256 'rid' => $competency1->get('id')
2260 $result = external
::add_related_competency($competency3->get('id'), $competency1->get('id'));
2261 $result = external_api
::clean_returnvalue(external
::add_related_competency_returns(), $result);
2262 $this->assertTrue($result);
2263 $this->assertTrue($DB->record_exists_select(
2264 related_competency
::TABLE
, 'competencyid = :cid AND relatedcompetencyid = :rid',
2266 'cid' => $competency1->get('id'),
2267 'rid' => $competency3->get('id')
2270 $this->assertFalse($DB->record_exists_select(
2271 related_competency
::TABLE
, 'competencyid = :cid AND relatedcompetencyid = :rid',
2273 'cid' => $competency3->get('id'),
2274 'rid' => $competency1->get('id')
2278 // We can not allow a duplicate relation, not even in the other direction.
2279 $this->assertEquals(1, $DB->count_records_select(related_competency
::TABLE
,
2280 'competencyid = :cid AND relatedcompetencyid = :rid',
2281 array('cid' => $competency1->get('id'), 'rid' => $competency2->get('id'))));
2282 $this->assertEquals(0, $DB->count_records_select(related_competency
::TABLE
,
2283 'competencyid = :cid AND relatedcompetencyid = :rid',
2284 array('rid' => $competency1->get('id'), 'cid' => $competency2->get('id'))));
2285 $result = external
::add_related_competency($competency2->get('id'), $competency1->get('id'));
2286 $result = external_api
::clean_returnvalue(external
::add_related_competency_returns(), $result);
2287 $this->assertTrue($result);
2288 $this->assertEquals(1, $DB->count_records_select(related_competency
::TABLE
,
2289 'competencyid = :cid AND relatedcompetencyid = :rid',
2290 array('cid' => $competency1->get('id'), 'rid' => $competency2->get('id'))));
2291 $this->assertEquals(0, $DB->count_records_select(related_competency
::TABLE
,
2292 'competencyid = :cid AND relatedcompetencyid = :rid',
2293 array('rid' => $competency1->get('id'), 'cid' => $competency2->get('id'))));
2295 // Check that we cannot create links across frameworks.
2297 external
::add_related_competency($competency1->get('id'), $competency4->get('id'));
2298 $this->fail('Exception expected due mis-use of shared competencies');
2299 } catch (invalid_persistent_exception
$e) {
2303 // User without permission.
2304 $this->setUser($this->user
);
2306 // Check we can not add the related competency now.
2308 external
::add_related_competency($competency1->get('id'), $competency3->get('id'));
2309 $this->fail('Exception expected due to not permissions to manage template competencies');
2310 } catch (moodle_exception
$e) {
2311 $this->assertEquals('nopermissions', $e->errorcode
);
2317 * Test that we can remove related competencies.
2321 public function test_remove_related_competency() {
2322 $this->setUser($this->creator
);
2324 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
2325 $framework = $lpg->create_framework();
2326 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2327 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2328 $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2329 $rc1 = $lpg->create_related_competency(array('competencyid' => $c1->get('id'), 'relatedcompetencyid' => $c2->get('id')));
2330 $rc2 = $lpg->create_related_competency(array('competencyid' => $c2->get('id'), 'relatedcompetencyid' => $c3->get('id')));
2332 $this->assertEquals(2, related_competency
::count_records());
2334 // Returns false when the relation does not exist.
2335 $result = external
::remove_related_competency($c1->get('id'), $c3->get('id'));
2336 $result = external_api
::clean_returnvalue(external
::remove_related_competency_returns(), $result);
2337 $this->assertFalse($result);
2339 // Returns true on success.
2340 $result = external
::remove_related_competency($c2->get('id'), $c3->get('id'));
2341 $result = external_api
::clean_returnvalue(external
::remove_related_competency_returns(), $result);
2342 $this->assertTrue($result);
2343 $this->assertEquals(1, related_competency
::count_records());
2345 // We don't need to specify competencyid and relatedcompetencyid in the right order.
2346 $result = external
::remove_related_competency($c2->get('id'), $c1->get('id'));
2347 $result = external_api
::clean_returnvalue(external
::remove_related_competency_returns(), $result);
2348 $this->assertTrue($result);
2349 $this->assertEquals(0, related_competency
::count_records());
2353 * Test that we can search and include related competencies.
2357 public function test_search_competencies_including_related() {
2358 $this->setUser($this->creator
);
2360 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
2361 $framework = $lpg->create_framework();
2362 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2363 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2364 $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2365 $c4 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2366 $c5 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2368 // We have 1-2, 1-3, 2-4, and no relation between 2-3 nor 1-4 nor 5.
2369 $rc12 = $lpg->create_related_competency(array('competencyid' => $c1->get('id'), 'relatedcompetencyid' => $c2->get('id')));
2370 $rc13 = $lpg->create_related_competency(array('competencyid' => $c1->get('id'), 'relatedcompetencyid' => $c3->get('id')));
2371 $rc24 = $lpg->create_related_competency(array('competencyid' => $c2->get('id'), 'relatedcompetencyid' => $c4->get('id')));
2373 $result = external
::search_competencies('comp', $framework->get('id'), true);
2374 $result = external_api
::clean_returnvalue(external
::search_competencies_returns(), $result);
2376 $this->assertCount(5, $result);
2381 * Test that we can add competency to plan if we have the right capability.
2385 public function test_add_competency_to_plan() {
2386 $this->resetAfterTest(true);
2387 $dg = $this->getDataGenerator();
2388 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
2389 $usermanage = $dg->create_user();
2390 $user = $dg->create_user();
2392 $syscontext = context_system
::instance();
2394 // Creating specific roles.
2395 $managerole = $dg->create_role(array(
2396 'name' => 'User manage',
2397 'shortname' => 'manage'
2400 assign_capability('moodle/competency:planmanage', CAP_ALLOW
, $managerole, $syscontext->id
);
2401 assign_capability('moodle/competency:planview', CAP_ALLOW
, $managerole, $syscontext->id
);
2403 $dg->role_assign($managerole, $usermanage->id
, $syscontext->id
);
2405 $this->setUser($usermanage);
2407 'userid' => $usermanage->id
,
2408 'status' => \core_competency\plan
::STATUS_ACTIVE
2410 $pl1 = $lpg->create_plan($plan);
2411 $framework = $lpg->create_framework();
2412 $competency = $lpg->create_competency(
2413 array('competencyframeworkid' => $framework->get('id'))
2415 $this->assertTrue(external
::add_competency_to_plan($pl1->get('id'), $competency->get('id')));
2417 // A competency cannot be added to plan based on template.
2418 $template = $lpg->create_template();
2420 'userid' => $usermanage->id
,
2421 'status' => \core_competency\plan
::STATUS_ACTIVE
,
2422 'templateid' => $template->get('id')
2424 $pl2 = $lpg->create_plan($plan);
2426 external
::add_competency_to_plan($pl2->get('id'), $competency->get('id'));
2427 $this->fail('A competency cannot be added to plan based on template');
2428 } catch (coding_exception
$ex) {
2429 $this->assertTrue(true);
2432 // User without capability cannot add competency to a plan.
2433 $this->setUser($user);
2435 external
::add_competency_to_plan($pl1->get('id'), $competency->get('id'));
2436 $this->fail('User without capability cannot add competency to a plan');
2437 } catch (required_capability_exception
$ex) {
2438 $this->assertTrue(true);
2443 * Test that we can add competency to plan if we have the right capability.
2447 public function test_remove_competency_from_plan() {
2448 $this->resetAfterTest(true);
2449 $dg = $this->getDataGenerator();
2450 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
2451 $usermanage = $dg->create_user();
2452 $user = $dg->create_user();
2454 $syscontext = context_system
::instance();
2456 // Creating specific roles.
2457 $managerole = $dg->create_role(array(
2458 'name' => 'User manage',
2459 'shortname' => 'manage'
2462 assign_capability('moodle/competency:planmanage', CAP_ALLOW
, $managerole, $syscontext->id
);
2463 assign_capability('moodle/competency:planview', CAP_ALLOW
, $managerole, $syscontext->id
);
2465 $dg->role_assign($managerole, $usermanage->id
, $syscontext->id
);
2467 $this->setUser($usermanage);
2469 'userid' => $usermanage->id
,
2470 'status' => \core_competency\plan
::STATUS_ACTIVE
2472 $pl1 = $lpg->create_plan($plan);
2473 $framework = $lpg->create_framework();
2474 $competency = $lpg->create_competency(
2475 array('competencyframeworkid' => $framework->get('id'))
2477 $lpg->create_plan_competency(
2479 'planid' => $pl1->get('id'),
2480 'competencyid' => $competency->get('id')
2483 $this->assertTrue(external
::remove_competency_from_plan($pl1->get('id'), $competency->get('id')));
2484 $this->assertCount(0, $pl1->get_competencies());
2488 * Test that we can add competency to plan if we have the right capability.
2492 public function test_reorder_plan_competency() {
2493 $this->resetAfterTest(true);
2494 $dg = $this->getDataGenerator();
2495 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
2496 $usermanage = $dg->create_user();
2497 $user = $dg->create_user();
2499 $syscontext = context_system
::instance();
2501 // Creating specific roles.
2502 $managerole = $dg->create_role(array(
2503 'name' => 'User manage',
2504 'shortname' => 'manage'
2507 assign_capability('moodle/competency:planmanage', CAP_ALLOW
, $managerole, $syscontext->id
);
2508 assign_capability('moodle/competency:planview', CAP_ALLOW
, $managerole, $syscontext->id
);
2510 $dg->role_assign($managerole, $usermanage->id
, $syscontext->id
);
2512 $this->setUser($usermanage);
2514 'userid' => $usermanage->id
,
2515 'status' => \core_competency\plan
::STATUS_ACTIVE
2517 $pl1 = $lpg->create_plan($plan);
2518 $framework = $lpg->create_framework();
2519 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2520 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2521 $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2522 $c4 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2523 $c5 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2525 $lpg->create_plan_competency(array('planid' => $pl1->get('id'), 'competencyid' => $c1->get('id'), 'sortorder' => 1));
2526 $lpg->create_plan_competency(array('planid' => $pl1->get('id'), 'competencyid' => $c2->get('id'), 'sortorder' => 2));
2527 $lpg->create_plan_competency(array('planid' => $pl1->get('id'), 'competencyid' => $c3->get('id'), 'sortorder' => 3));
2528 $lpg->create_plan_competency(array('planid' => $pl1->get('id'), 'competencyid' => $c4->get('id'), 'sortorder' => 4));
2529 $lpg->create_plan_competency(array('planid' => $pl1->get('id'), 'competencyid' => $c5->get('id'), 'sortorder' => 5));
2531 // Test if removing competency from plan don't create sortorder holes.
2532 external
::remove_competency_from_plan($pl1->get('id'), $c4->get('id'));
2533 $plancomp5 = plan_competency
::get_record(array(
2534 'planid' => $pl1->get('id'),
2535 'competencyid' => $c5->get('id')
2538 $this->assertEquals(3, $plancomp5->get('sortorder'));
2540 $this->assertTrue(external
::reorder_plan_competency($pl1->get('id'), $c2->get('id'), $c5->get('id')));
2541 $this->assertTrue(external
::reorder_plan_competency($pl1->get('id'), $c3->get('id'), $c1->get('id')));
2542 $plancompetencies = plan_competency
::get_records(array('planid' => $pl1->get('id')), 'sortorder', 'ASC');
2543 $plcmp1 = $plancompetencies[0];
2544 $plcmp2 = $plancompetencies[1];
2545 $plcmp3 = $plancompetencies[2];
2546 $plcmp4 = $plancompetencies[3];
2548 $this->assertEquals($plcmp1->get('competencyid'), $c3->get('id'));
2549 $this->assertEquals($plcmp2->get('competencyid'), $c1->get('id'));
2550 $this->assertEquals($plcmp3->get('competencyid'), $c5->get('id'));
2551 $this->assertEquals($plcmp4->get('competencyid'), $c2->get('id'));
2555 * Test resolving sortorder when we creating competency.
2557 public function test_fix_sortorder_when_creating_competency() {
2558 $this->resetAfterTest(true);
2559 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
2560 $framework = $lpg->create_framework();
2562 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2563 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'), 'sortorder' => 20));
2564 $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'), 'sortorder' => 1));
2566 $this->assertEquals(0, $c1->get('sortorder'));
2567 $this->assertEquals(1, $c2->get('sortorder'));
2568 $this->assertEquals(2, $c3->get('sortorder'));
2572 * Test resolving sortorder when we delete competency.
2574 public function test_fix_sortorder_when_delete_competency() {
2575 $this->resetAfterTest(true);
2576 $this->setUser($this->creator
);
2577 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
2579 $framework = $lpg->create_framework();
2581 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2582 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2583 $c2a = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'), 'parentid' => $c2->get('id')));
2584 $c2b = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'), 'parentid' => $c2->get('id')));
2585 $c2c = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'), 'parentid' => $c2->get('id')));
2586 $c2d = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'), 'parentid' => $c2->get('id')));
2588 $this->assertEquals(0, $c1->get('sortorder'));
2589 $this->assertEquals(1, $c2->get('sortorder'));
2590 $this->assertEquals(0, $c2a->get('sortorder'));
2591 $this->assertEquals(1, $c2b->get('sortorder'));
2592 $this->assertEquals(2, $c2c->get('sortorder'));
2593 $this->assertEquals(3, $c2d->get('sortorder'));
2595 $result = external
::delete_competency($c1->get('id'));
2596 $result = external_api
::clean_returnvalue(external
::delete_competency_returns(), $result);
2604 $this->assertEquals(0, $c2->get('sortorder'));
2605 $this->assertEquals(0, $c2a->get('sortorder'));
2606 $this->assertEquals(1, $c2b->get('sortorder'));
2607 $this->assertEquals(2, $c2c->get('sortorder'));
2608 $this->assertEquals(3, $c2d->get('sortorder'));
2610 $result = external
::delete_competency($c2b->get('id'));
2611 $result = external_api
::clean_returnvalue(external
::delete_competency_returns(), $result);
2618 $this->assertEquals(0, $c2->get('sortorder'));
2619 $this->assertEquals(0, $c2a->get('sortorder'));
2620 $this->assertEquals(1, $c2c->get('sortorder'));
2621 $this->assertEquals(2, $c2d->get('sortorder'));
2625 * Test resolving sortorder when moving a competency.
2627 public function test_fix_sortorder_when_moving_competency() {
2628 $this->resetAfterTest(true);
2629 $this->setUser($this->creator
);
2630 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
2632 $framework = $lpg->create_framework();
2634 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2635 $c1a = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'), 'parentid' => $c1->get('id')));
2636 $c1b = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'), 'parentid' => $c1->get('id')));
2637 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
2638 $c2a = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'), 'parentid' => $c2->get('id')));
2639 $c2b = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'), 'parentid' => $c2->get('id')));
2641 $this->assertEquals(0, $c1->get('sortorder'));
2642 $this->assertEquals(0, $c1a->get('sortorder'));
2643 $this->assertEquals(1, $c1b->get('sortorder'));
2644 $this->assertEquals(1, $c2->get('sortorder'));
2645 $this->assertEquals(0, $c2a->get('sortorder'));
2646 $this->assertEquals(1, $c2b->get('sortorder'));
2648 $result = external
::set_parent_competency($c2a->get('id'), $c1->get('id'));
2649 $result = external_api
::clean_returnvalue(external
::set_parent_competency_returns(), $result);
2658 $this->assertEquals(0, $c1->get('sortorder'));
2659 $this->assertEquals(0, $c1a->get('sortorder'));
2660 $this->assertEquals(1, $c1b->get('sortorder'));
2661 $this->assertEquals(2, $c2a->get('sortorder'));
2662 $this->assertEquals(1, $c2->get('sortorder'));
2663 $this->assertEquals(0, $c2b->get('sortorder'));
2665 // Move a root node.
2666 $result = external
::set_parent_competency($c2->get('id'), $c1b->get('id'));
2667 $result = external_api
::clean_returnvalue(external
::set_parent_competency_returns(), $result);
2676 $this->assertEquals(0, $c1->get('sortorder'));
2677 $this->assertEquals(0, $c1a->get('sortorder'));
2678 $this->assertEquals(1, $c1b->get('sortorder'));
2679 $this->assertEquals(0, $c2->get('sortorder'));
2680 $this->assertEquals(0, $c2b->get('sortorder'));
2681 $this->assertEquals(2, $c2a->get('sortorder'));
2684 public function test_grade_competency() {
2687 $this->setUser($this->creator
);
2688 $dg = $this->getDataGenerator();
2689 $lpg = $dg->get_plugin_generator('core_competency');
2691 $f1 = $lpg->create_framework();
2692 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
2693 $evidence = external
::grade_competency($this->user
->id
, $c1->get('id'), 1, 'Evil note');
2695 $this->assertEquals('The competency rating was manually set.', $evidence->description
);
2696 $this->assertEquals('A', $evidence->gradename
);
2697 $this->assertEquals('Evil note', $evidence->note
);
2699 $this->setUser($this->user
);
2701 $this->expectException('required_capability_exception');
2702 $evidence = external
::grade_competency($this->user
->id
, $c1->get('id'), 1);
2705 public function test_grade_competency_in_course() {
2708 $this->setUser($this->creator
);
2709 $dg = $this->getDataGenerator();
2710 $lpg = $dg->get_plugin_generator('core_competency');
2712 $course = $dg->create_course(['fullname' => 'Evil course']);
2713 $dg->enrol_user($this->creator
->id
, $course->id
, 'editingteacher');
2714 $dg->enrol_user($this->user
->id
, $course->id
, 'student');
2715 $f1 = $lpg->create_framework();
2716 $c1 = $lpg->create_competency(['competencyframeworkid' => $f1->get('id')]);
2717 $lpg->create_course_competency(['courseid' => $course->id
, 'competencyid' => $c1->get('id')]);
2719 $evidence = external
::grade_competency_in_course($course->id
, $this->user
->id
, $c1->get('id'), 1, 'Evil note');
2721 $this->assertEquals('The competency rating was manually set in the course \'Course: Evil course\'.', $evidence->description
);
2722 $this->assertEquals('A', $evidence->gradename
);
2723 $this->assertEquals('Evil note', $evidence->note
);
2725 $this->setUser($this->user
);
2727 $this->expectException('required_capability_exception');
2728 $evidence = external
::grade_competency_in_course($course->id
, $this->user
->id
, $c1->get('id'), 1);
2731 public function test_grade_competency_in_plan() {
2734 $this->setUser($this->creator
);
2736 $dg = $this->getDataGenerator();
2737 $lpg = $dg->get_plugin_generator('core_competency');
2739 $f1 = $lpg->create_framework();
2741 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id')));
2743 $tpl = $lpg->create_template();
2744 $lpg->create_template_competency(array('templateid' => $tpl->get('id'), 'competencyid' => $c1->get('id')));
2746 $plan = $lpg->create_plan(array('userid' => $this->user
->id
, 'templateid' => $tpl->get('id'), 'name' => 'Evil'));
2748 $uc = $lpg->create_user_competency(array('userid' => $this->user
->id
, 'competencyid' => $c1->get('id')));
2750 $evidence = external
::grade_competency_in_plan($plan->get('id'), $c1->get('id'), 1, 'Evil note');
2752 $this->assertEquals('The competency rating was manually set in the learning plan \'Evil\'.', $evidence->description
);
2753 $this->assertEquals('A', $evidence->gradename
);
2754 $this->assertEquals('Evil note', $evidence->note
);
2756 $this->setUser($this->user
);
2758 $this->expectException('required_capability_exception');
2759 $evidence = external
::grade_competency_in_plan($plan->get('id'), $c1->get('id'), 1);
2763 * Test update course competency settings.
2765 public function test_update_course_competency_settings() {
2766 $this->resetAfterTest(true);
2768 $dg = $this->getDataGenerator();
2770 $course = $dg->create_course();
2771 $roleid = $dg->create_role();
2772 $noobroleid = $dg->create_role();
2773 $context = context_course
::instance($course->id
);
2774 $compmanager = $this->getDataGenerator()->create_user();
2775 $compnoob = $this->getDataGenerator()->create_user();
2777 assign_capability('moodle/competency:coursecompetencyconfigure', CAP_ALLOW
, $roleid, $context->id
, true);
2778 assign_capability('moodle/competency:coursecompetencyview', CAP_ALLOW
, $roleid, $context->id
, true);
2779 assign_capability('moodle/competency:coursecompetencyview', CAP_ALLOW
, $noobroleid, $context->id
, true);
2781 role_assign($roleid, $compmanager->id
, $context->id
);
2782 role_assign($noobroleid, $compnoob->id
, $context->id
);
2783 $dg->enrol_user($compmanager->id
, $course->id
, $roleid);
2784 $dg->enrol_user($compnoob->id
, $course->id
, $noobroleid);
2786 $this->setUser($compmanager);
2789 $result = external
::update_course_competency_settings($course->id
, array('pushratingstouserplans' => true));
2791 $settings = course_competency_settings
::get_by_courseid($course->id
);
2793 $this->assertTrue((bool)$settings->get('pushratingstouserplans'));
2795 $result = external
::update_course_competency_settings($course->id
, array('pushratingstouserplans' => false));
2797 $settings = course_competency_settings
::get_by_courseid($course->id
);
2799 $this->assertFalse((bool)$settings->get('pushratingstouserplans'));
2800 $this->setUser($compnoob);
2802 $this->expectException('required_capability_exception');
2803 $result = external
::update_course_competency_settings($course->id
, array('pushratingstouserplans' => true));