a1b4118c2c47fe0a8b6143e1e05bca40efdf723b
[moodle.git] / admin / tool / lp / tests / externallib_test.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 defined('MOODLE_INTERNAL') || die();
19 global $CFG;
21 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
23 use tool_lp\external;
24 use tool_lp\plan;
26 /**
27  * External learning plans webservice API tests.
28  *
29  * @package tool_lp
30  * @copyright 2015 Damyon Wiese
31  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32  */
33 class tool_lp_external_testcase extends externallib_advanced_testcase {
35     /** @var stdClass $learningplancreator User with enough permissions to create */
36     protected $creator = null;
38     /** @var stdClass $learningplanuser User with enough permissions to view */
39     protected $user = null;
41     /** @var int Creator role id */
42     protected $creatorrole = null;
44     /** @var int User role id */
45     protected $userrole = null;
47     /**
48      * Setup function - we will create a course and add an assign instance to it.
49      */
50     protected function setUp() {
51         global $DB;
53         $this->resetAfterTest(true);
55         // Create some users.
56         $creator = $this->getDataGenerator()->create_user();
57         $user = $this->getDataGenerator()->create_user();
58         $syscontext = context_system::instance();
60         $this->creatorrole = create_role('Creator role', 'creatorrole', 'learning plan creator role description');
61         $this->userrole = create_role('User role', 'userrole', 'learning plan user role description');
63         assign_capability('tool/lp:competencymanage', CAP_ALLOW, $this->creatorrole, $syscontext->id);
64         assign_capability('tool/lp:competencyview', CAP_ALLOW, $this->userrole, $syscontext->id);
65         assign_capability('tool/lp:planmanage', CAP_ALLOW, $this->creatorrole, $syscontext->id);
66         assign_capability('tool/lp:planmanageown', CAP_ALLOW, $this->creatorrole, $syscontext->id);
67         assign_capability('tool/lp:planviewall', CAP_ALLOW, $this->creatorrole, $syscontext->id);
68         assign_capability('tool/lp:templatemanage', CAP_ALLOW, $this->creatorrole, $syscontext->id);
69         assign_capability('tool/lp:templatecompetencymanage', CAP_ALLOW, $this->creatorrole, $syscontext->id);
71         role_assign($this->creatorrole, $creator->id, $syscontext->id);
72         role_assign($this->userrole, $user->id, $syscontext->id);
74         $this->creator = $creator;
75         $this->user = $user;
76         accesslib_clear_all_caches_for_unit_testing();
77     }
79     /**
80      * Test we can't create a competency framework with only read permissions.
81      */
82     public function test_create_competency_frameworks_with_read_permissions() {
83         $this->setExpectedException('required_capability_exception');
84         $this->setUser($this->user);
85         $result = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, true);
86     }
88     /**
89      * Test we can create a competency framework with manage permissions.
90      */
91     public function test_create_competency_frameworks_with_manage_permissions() {
92         $this->setUser($this->creator);
93         $result = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, true);
94         $result = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result);
96         $this->assertGreaterThan(0, $result->timecreated);
97         $this->assertGreaterThan(0, $result->timemodified);
98         $this->assertEquals($this->creator->id, $result->usermodified);
99         $this->assertEquals('shortname', $result->shortname);
100         $this->assertEquals('idnumber', $result->idnumber);
101         $this->assertEquals('description', $result->description);
102         $this->assertEquals(FORMAT_HTML, $result->descriptionformat);
103         $this->assertEquals(true, $result->visible);
104     }
106     /**
107      * Test we cannot create a competency framework with nasty data.
108      */
109     public function test_create_competency_frameworks_with_nasty_data() {
110         $this->setUser($this->creator);
111         $this->setExpectedException('invalid_parameter_exception');
112         $result = external::create_competency_framework('short<a href="">', 'id;"number', 'de<>\\..scription', FORMAT_HTML, true);
113     }
115     /**
116      * Test we can read a competency framework with manage permissions.
117      */
118     public function test_read_competency_frameworks_with_manage_permissions() {
119         $this->setUser($this->creator);
120         $result = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, true);
121         $result = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result);
123         $id = $result->id;
124         $result = external::read_competency_framework($id);
125         $result = (object) external_api::clean_returnvalue(external::read_competency_framework_returns(), $result);
127         $this->assertGreaterThan(0, $result->timecreated);
128         $this->assertGreaterThan(0, $result->timemodified);
129         $this->assertEquals($this->creator->id, $result->usermodified);
130         $this->assertEquals('shortname', $result->shortname);
131         $this->assertEquals('idnumber', $result->idnumber);
132         $this->assertEquals('description', $result->description);
133         $this->assertEquals(FORMAT_HTML, $result->descriptionformat);
134         $this->assertEquals(true, $result->visible);
135     }
137     /**
138      * Test we can read a competency framework with read permissions.
139      */
140     public function test_read_competency_frameworks_with_read_permissions() {
141         $this->setUser($this->creator);
142         $result = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, true);
143         $result = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result);
145         // Switch users to someone with less permissions.
146         $this->setUser($this->user);
147         $id = $result->id;
148         $result = external::read_competency_framework($id);
149         $result = (object) external_api::clean_returnvalue(external::read_competency_framework_returns(), $result);
151         $this->assertGreaterThan(0, $result->timecreated);
152         $this->assertGreaterThan(0, $result->timemodified);
153         $this->assertEquals($this->creator->id, $result->usermodified);
154         $this->assertEquals('shortname', $result->shortname);
155         $this->assertEquals('idnumber', $result->idnumber);
156         $this->assertEquals('description', $result->description);
157         $this->assertEquals(FORMAT_HTML, $result->descriptionformat);
158         $this->assertEquals(true, $result->visible);
159     }
161     /**
162      * Test we can delete a competency framework with manage permissions.
163      */
164     public function test_delete_competency_frameworks_with_manage_permissions() {
165         $this->setUser($this->creator);
166         $result = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, true);
167         $result = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result);
169         $id = $result->id;
170         $result = external::delete_competency_framework($id);
171         $result = external_api::clean_returnvalue(external::delete_competency_framework_returns(), $result);
173         $this->assertTrue($result);
174     }
176     /**
177      * Test we can delete a competency framework with read permissions.
178      */
179     public function test_delete_competency_frameworks_with_read_permissions() {
180         $this->setExpectedException('required_capability_exception');
181         $this->setUser($this->creator);
182         $result = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, true);
183         $result = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result);
185         $id = $result->id;
186         // Switch users to someone with less permissions.
187         $this->setUser($this->user);
188         $result = external::delete_competency_framework($id);
189     }
191     /**
192      * Test we can update a competency framework with manage permissions.
193      */
194     public function test_update_competency_frameworks_with_manage_permissions() {
195         $this->setUser($this->creator);
196         $result = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, true);
197         $result = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result);
199         $result = external::update_competency_framework($result->id, 'shortname2', 'idnumber2', 'description2', FORMAT_PLAIN, false);
200         $result = external_api::clean_returnvalue(external::update_competency_framework_returns(), $result);
202         $this->assertTrue($result);
203     }
205     /**
206      * Test we can update a competency framework with read permissions.
207      */
208     public function test_update_competency_frameworks_with_read_permissions() {
209         $this->setExpectedException('required_capability_exception');
210         $this->setUser($this->creator);
211         $result = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, true);
212         $result = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $result);
214         $this->setUser($this->user);
215         $result = external::update_competency_framework($result->id, 'shortname2', 'idnumber2', 'description2', FORMAT_PLAIN, false);
216     }
218     /**
219      * Test we can list and count competency frameworks with manage permissions.
220      */
221     public function test_list_and_count_competency_frameworks_with_manage_permissions() {
222         $this->setUser($this->creator);
223         $result = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, true);
224         $result = external::create_competency_framework('shortname2', 'idnumber2', 'description', FORMAT_HTML, true);
225         $result = external::create_competency_framework('shortname3', 'idnumber3', 'description', FORMAT_HTML, true);
227         $result = external::count_competency_frameworks(array());
228         $result = external_api::clean_returnvalue(external::count_competency_frameworks_returns(), $result);
230         $this->assertEquals($result, 3);
232         $result = external::list_competency_frameworks(array(), 'shortname', 'ASC', 0, 10);
233         $result = external_api::clean_returnvalue(external::list_competency_frameworks_returns(), $result);
235         $this->assertEquals(count($result), 3);
236         $result = (object) $result[0];
238         $this->assertGreaterThan(0, $result->timecreated);
239         $this->assertGreaterThan(0, $result->timemodified);
240         $this->assertEquals($this->creator->id, $result->usermodified);
241         $this->assertEquals('shortname', $result->shortname);
242         $this->assertEquals('idnumber', $result->idnumber);
243         $this->assertEquals('description', $result->description);
244         $this->assertEquals(FORMAT_HTML, $result->descriptionformat);
245         $this->assertEquals(true, $result->visible);
246     }
248     /**
249      * Test we can list and count competency frameworks with read permissions.
250      */
251     public function test_list_and_count_competency_frameworks_with_read_permissions() {
252         $this->setUser($this->creator);
253         $result = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, true);
254         $result = external::create_competency_framework('shortname2', 'idnumber2', 'description', FORMAT_HTML, true);
255         $result = external::create_competency_framework('shortname3', 'idnumber3', 'description', FORMAT_HTML, true);
257         $this->setUser($this->user);
258         $result = external::count_competency_frameworks(array());
259         $result = external_api::clean_returnvalue(external::count_competency_frameworks_returns(), $result);
261         $this->assertEquals($result, 3);
263         $result = external::list_competency_frameworks(array(), 'shortname', 'ASC', 0, 10);
264         $result = external_api::clean_returnvalue(external::list_competency_frameworks_returns(), $result);
266         $this->assertEquals(count($result), 3);
267         $result = (object) $result[0];
269         $this->assertGreaterThan(0, $result->timecreated);
270         $this->assertGreaterThan(0, $result->timemodified);
271         $this->assertEquals($this->creator->id, $result->usermodified);
272         $this->assertEquals('shortname', $result->shortname);
273         $this->assertEquals('idnumber', $result->idnumber);
274         $this->assertEquals('description', $result->description);
275         $this->assertEquals(FORMAT_HTML, $result->descriptionformat);
276         $this->assertEquals(true, $result->visible);
277     }
279     /**
280      * Test we can re-order competency frameworks.
281      */
282     public function test_reorder_competency_framework() {
283         $this->setUser($this->creator);
284         $f1 = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, true);
285         $f2 = external::create_competency_framework('shortname2', 'idnumber2', 'description', FORMAT_HTML, true);
286         $f3 = external::create_competency_framework('shortname3', 'idnumber3', 'description', FORMAT_HTML, true);
287         $f4 = external::create_competency_framework('shortname4', 'idnumber4', 'description', FORMAT_HTML, true);
288         $f5 = external::create_competency_framework('shortname5', 'idnumber5', 'description', FORMAT_HTML, true);
289         $f6 = external::create_competency_framework('shortname6', 'idnumber6', 'description', FORMAT_HTML, true);
291         // This is a move up.
292         $result = external::reorder_competency_framework($f5->id, $f2->id);
293         $result = external::list_competency_frameworks(array(), 'sortorder', 'ASC', 0, 10);
294         $result = external_api::clean_returnvalue(external::list_competency_frameworks_returns(), $result);
296         $r1 = (object) $result[0];
297         $r2 = (object) $result[1];
298         $r3 = (object) $result[2];
299         $r4 = (object) $result[3];
300         $r5 = (object) $result[4];
301         $r6 = (object) $result[5];
303         $this->assertEquals($f1->id, $r1->id);
304         $this->assertEquals($f5->id, $r2->id);
305         $this->assertEquals($f2->id, $r3->id);
306         $this->assertEquals($f3->id, $r4->id);
307         $this->assertEquals($f4->id, $r5->id);
308         $this->assertEquals($f6->id, $r6->id);
310         // This is a move down.
311         $result = external::reorder_competency_framework($f5->id, $f4->id);
312         $result = external::list_competency_frameworks(array(), 'sortorder', 'ASC', 0, 10);
313         $result = external_api::clean_returnvalue(external::list_competency_frameworks_returns(), $result);
315         $r1 = (object) $result[0];
316         $r2 = (object) $result[1];
317         $r3 = (object) $result[2];
318         $r4 = (object) $result[3];
319         $r5 = (object) $result[4];
320         $r6 = (object) $result[5];
322         $this->assertEquals($f1->id, $r1->id);
323         $this->assertEquals($f2->id, $r2->id);
324         $this->assertEquals($f3->id, $r3->id);
325         $this->assertEquals($f4->id, $r4->id);
326         $this->assertEquals($f5->id, $r5->id);
327         $this->assertEquals($f6->id, $r6->id);
329         $this->setExpectedException('required_capability_exception');
330         $this->setUser($this->user);
331         $result = external::reorder_competency_framework($f5->id, $f4->id);
332     }
334     /**
335      * Test we can't create a competency with only read permissions.
336      */
337     public function test_create_competency_with_read_permissions() {
338         $this->setExpectedException('required_capability_exception');
339         $framework = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, true);
340         $framework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $framework);
341         $this->setUser($this->user);
342         $competency = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $framework->id, 0);
343     }
345     /**
346      * Test we can create a competency with manage permissions.
347      */
348     public function test_create_competency_with_manage_permissions() {
349         $this->setUser($this->creator);
350         $framework = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, true);
351         $framework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $framework);
353         $competency = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $framework->id, 0);
354         $competency = (object) external_api::clean_returnvalue(external::create_competency_returns(), $competency);
356         $this->assertGreaterThan(0, $competency->timecreated);
357         $this->assertGreaterThan(0, $competency->timemodified);
358         $this->assertEquals($this->creator->id, $competency->usermodified);
359         $this->assertEquals('shortname', $competency->shortname);
360         $this->assertEquals('idnumber', $competency->idnumber);
361         $this->assertEquals('description', $competency->description);
362         $this->assertEquals(FORMAT_HTML, $competency->descriptionformat);
363         $this->assertEquals(true, $competency->visible);
364         $this->assertEquals(0, $competency->parentid);
365         $this->assertEquals($framework->id, $competency->competencyframeworkid);
366     }
368     /**
369      * Test we cannot create a competency with nasty data.
370      */
371     public function test_create_competency_with_nasty_data() {
372         $this->setUser($this->creator);
373         $framework = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, true);
374         $framework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $framework);
375         $this->setExpectedException('invalid_parameter_exception');
376         $competency = external::create_competency('shortname<a href="">', 'id;"number', 'de<>\\..scription', FORMAT_HTML, true, $framework->id, 0);
377     }
379     /**
380      * Test we can read a competency with manage permissions.
381      */
382     public function test_read_competencies_with_manage_permissions() {
383         $this->setUser($this->creator);
384         $framework = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, true);
385         $framework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $framework);
386         $result = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $framework->id, 0);
387         $result = (object) external_api::clean_returnvalue(external::create_competency_returns(), $result);
389         $id = $result->id;
390         $result = external::read_competency($id);
391         $result = (object) external_api::clean_returnvalue(external::read_competency_returns(), $result);
393         $this->assertGreaterThan(0, $result->timecreated);
394         $this->assertGreaterThan(0, $result->timemodified);
395         $this->assertEquals($this->creator->id, $result->usermodified);
396         $this->assertEquals('shortname', $result->shortname);
397         $this->assertEquals('idnumber', $result->idnumber);
398         $this->assertEquals('description', $result->description);
399         $this->assertEquals(FORMAT_HTML, $result->descriptionformat);
400         $this->assertEquals(true, $result->visible);
401         $this->assertEquals(0, $result->parentid);
402         $this->assertEquals(0, $result->parentid);
403     }
405     /**
406      * Test we can read a competency with read permissions.
407      */
408     public function test_read_competencies_with_read_permissions() {
409         $this->setUser($this->creator);
410         $framework = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, true);
411         $framework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $framework);
412         $result = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $framework->id, 0);
413         $result = (object) external_api::clean_returnvalue(external::create_competency_returns(), $result);
415         // Switch users to someone with less permissions.
416         $this->setUser($this->user);
417         $id = $result->id;
418         $result = external::read_competency($id);
419         $result = (object) external_api::clean_returnvalue(external::read_competency_returns(), $result);
421         $this->assertGreaterThan(0, $result->timecreated);
422         $this->assertGreaterThan(0, $result->timemodified);
423         $this->assertEquals($this->creator->id, $result->usermodified);
424         $this->assertEquals('shortname', $result->shortname);
425         $this->assertEquals('idnumber', $result->idnumber);
426         $this->assertEquals('description', $result->description);
427         $this->assertEquals(FORMAT_HTML, $result->descriptionformat);
428         $this->assertEquals(true, $result->visible);
429         $this->assertEquals(0, $result->parentid);
430         $this->assertEquals(0, $result->parentid);
431     }
433     /**
434      * Test we can delete a competency with manage permissions.
435      */
436     public function test_delete_competency_with_manage_permissions() {
437         $this->setUser($this->creator);
438         $framework = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, true);
439         $framework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $framework);
440         $result = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $framework->id, 0);
441         $result = (object) external_api::clean_returnvalue(external::create_competency_returns(), $result);
443         $id = $result->id;
444         $result = external::delete_competency($id);
445         $result = external_api::clean_returnvalue(external::delete_competency_returns(), $result);
447         $this->assertTrue($result);
448     }
450     /**
451      * Test we can delete a competency with read permissions.
452      */
453     public function test_delete_competency_with_read_permissions() {
454         $this->setExpectedException('required_capability_exception');
455         $this->setUser($this->creator);
456         $framework = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, true);
457         $framework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $framework);
458         $result = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $framework->id, 0);
459         $result = (object) external_api::clean_returnvalue(external::create_competency_returns(), $result);
461         $id = $result->id;
462         // Switch users to someone with less permissions.
463         $this->setUser($this->user);
464         $result = external::delete_competency($id);
465     }
467     /**
468      * Test we can update a competency with manage permissions.
469      */
470     public function test_update_competency_with_manage_permissions() {
471         $this->setUser($this->creator);
472         $framework = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, true);
473         $framework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $framework);
474         $result = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $framework->id, 0);
475         $result = (object) external_api::clean_returnvalue(external::create_competency_returns(), $result);
477         $result = external::update_competency($result->id, 'shortname2', 'idnumber2', 'description2', FORMAT_HTML, false);
478         $result = external_api::clean_returnvalue(external::update_competency_returns(), $result);
480         $this->assertTrue($result);
481     }
483     /**
484      * Test we can update a competency with read permissions.
485      */
486     public function test_update_competency_with_read_permissions() {
487         $this->setExpectedException('required_capability_exception');
488         $this->setUser($this->creator);
489         $framework = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, true);
490         $framework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $framework);
491         $result = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $framework->id, 0);
492         $result = (object) external_api::clean_returnvalue(external::create_competency_returns(), $result);
494         $this->setUser($this->user);
495         $result = external::update_competency($result->id, 'shortname2', 'idnumber2', 'description2', FORMAT_HTML, false);
496     }
498     /**
499      * Test we can list and count competencies with manage permissions.
500      */
501     public function test_list_and_count_competencies_with_manage_permissions() {
502         $this->setUser($this->creator);
503         $framework = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, true);
504         $framework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $framework);
505         $result = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $framework->id, 0);
506         $result = external::create_competency('shortname2', 'idnumber2', 'description2', FORMAT_HTML, true, $framework->id, 0);
507         $result = external::create_competency('shortname3', 'idnumber3', 'description3', FORMAT_HTML, true, $framework->id, 0);
509         $result = external::count_competencies(array());
510         $result = external_api::clean_returnvalue(external::count_competencies_returns(), $result);
512         $this->assertEquals($result, 3);
514         $result = external::list_competencies(array(), 'shortname', 'ASC', 0, 10);
515         $result = external_api::clean_returnvalue(external::list_competencies_returns(), $result);
517         $this->assertEquals(count($result), 3);
518         $result = (object) $result[0];
520         $this->assertGreaterThan(0, $result->timecreated);
521         $this->assertGreaterThan(0, $result->timemodified);
522         $this->assertEquals($this->creator->id, $result->usermodified);
523         $this->assertEquals('shortname', $result->shortname);
524         $this->assertEquals('idnumber', $result->idnumber);
525         $this->assertEquals('description', $result->description);
526         $this->assertEquals(true, $result->visible);
527     }
529     /**
530      * Test we can list and count competencies with read permissions.
531      */
532     public function test_list_and_count_competencies_with_read_permissions() {
533         $this->setUser($this->creator);
534         $framework = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, true);
535         $framework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $framework);
536         $result = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $framework->id, 0);
537         $result = external::create_competency('shortname2', 'idnumber2', 'description2', FORMAT_HTML, true, $framework->id, 0);
538         $result = external::create_competency('shortname3', 'idnumber3', 'description3', FORMAT_HTML, true, $framework->id, 0);
540         $this->setUser($this->user);
542         $result = external::count_competencies(array());
543         $result = external_api::clean_returnvalue(external::count_competencies_returns(), $result);
545         $this->assertEquals($result, 3);
547         $result = external::list_competencies(array(), 'shortname', 'ASC', 0, 10);
548         $result = external_api::clean_returnvalue(external::list_competencies_returns(), $result);
550         $this->assertEquals(count($result), 3);
551         $result = (object) $result[0];
553         $this->assertGreaterThan(0, $result->timecreated);
554         $this->assertGreaterThan(0, $result->timemodified);
555         $this->assertEquals($this->creator->id, $result->usermodified);
556         $this->assertEquals('shortname', $result->shortname);
557         $this->assertEquals('idnumber', $result->idnumber);
558         $this->assertEquals('description', $result->description);
559         $this->assertEquals(true, $result->visible);
560     }
562     /**
563      * Test we can search for competencies.
564      */
565     public function test_search_competencies_with_read_permissions() {
566         $this->setUser($this->creator);
567         $framework = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, true);
568         $framework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $framework);
569         $result = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $framework->id, 0);
570         $result = external::create_competency('shortname2', 'idnumber2', 'description2', FORMAT_HTML, true, $framework->id, 0);
571         $result = external::create_competency('shortname3', 'idnumber3', 'description3', FORMAT_HTML, true, $framework->id, 0);
573         $this->setUser($this->user);
575         $result = external::search_competencies('short', $framework->id);
576         $result = external_api::clean_returnvalue(external::search_competencies_returns(), $result);
578         $this->assertEquals(count($result), 3);
579         $result = (object) $result[0];
581         $this->assertGreaterThan(0, $result->timecreated);
582         $this->assertGreaterThan(0, $result->timemodified);
583         $this->assertEquals($this->creator->id, $result->usermodified);
584         $this->assertEquals('shortname', $result->shortname);
585         $this->assertEquals('idnumber', $result->idnumber);
586         $this->assertEquals('description', $result->description);
587         $this->assertEquals(true, $result->visible);
588     }
590     /**
591      * Test plans creation and updates.
592      */
593     public function test_create_and_update_plans() {
594         $syscontext = context_system::instance();
596         $this->setUser($this->creator);
597         $plan0 = external::create_plan('Complete plan', 'A description', FORMAT_HTML, $this->creator->id, 0, plan::STATUS_COMPLETE, 0);
599         $this->setUser($this->user);
601         try {
602             $plan1 = external::create_plan('Draft plan (they can not with the default capabilities)', 'A description', FORMAT_HTML, $this->user->id, 0, plan::STATUS_DRAFT, 0);
603             $this->fail('Exception expected due to not permissions to create draft plans');
604         } catch (moodle_exception $e) {
605             $this->assertEquals('nopermissions', $e->errorcode);
606         }
608         assign_capability('tool/lp:plancreatedraft', CAP_ALLOW, $this->userrole, $syscontext->id);
609         accesslib_clear_all_caches_for_unit_testing();
611         $this->setUser($this->user);
613         $plan2 = external::create_plan('Draft plan', 'A description', FORMAT_HTML, $this->user->id, 0, plan::STATUS_DRAFT, 0);
615         try {
616             $plan3 = external::create_plan('Active plan (they can not)', 'A description', FORMAT_HTML, $this->user->id, 0, plan::STATUS_ACTIVE, 0);
617             $this->fail('Exception expected due to not permissions to create active plans');
618         } catch (moodle_exception $e) {
619             $this->assertEquals('nopermissions', $e->errorcode);
620         }
621         try {
622             $plan3 = external::update_plan($plan2['id'], 'Updated active plan', 'A description', FORMAT_HTML, $this->user->id, 0, plan::STATUS_COMPLETE, 0);
623             $this->fail('Exception expected due to not permissions to update plans to complete status');
624         } catch (moodle_exception $e) {
625             $this->assertEquals('nopermissions', $e->errorcode);
626         }
628         assign_capability('tool/lp:planmanageown', CAP_ALLOW, $this->userrole, $syscontext->id);
629         accesslib_clear_all_caches_for_unit_testing();
631         $plan3 = external::create_plan('Active plan', 'A description', FORMAT_HTML, $this->user->id, 0, plan::STATUS_ACTIVE, 0);
632         $plan4 = external::create_plan('Complete plan', 'A description', FORMAT_HTML, $this->user->id, 0, plan::STATUS_COMPLETE, 0);
633         try {
634             $plan4 = external::create_plan('Plan for another user', 'A description', FORMAT_HTML, $this->creator->id, 0, plan::STATUS_COMPLETE, 0);
635             $this->fail('Exception expected due to not permissions to manage other users plans');
636         } catch (moodle_exception $e) {
637             $this->assertEquals('nopermissions', $e->errorcode);
638         }
640         try {
641             $plan0 = external::update_plan($plan0['id'], 'Can not update other users plans', 'A description', FORMAT_HTML, $this->user->id, 0, plan::STATUS_ACTIVE, 0);
642         } catch (moodle_exception $e) {
643             $this->assertEquals('nopermissions', $e->errorcode);
644         }
646         unassign_capability('tool/lp:planmanageown', $this->userrole, $syscontext->id);
647         unassign_capability('tool/lp:plancreatedraft', $this->userrole, $syscontext->id);
648         accesslib_clear_all_caches_for_unit_testing();
650         try {
651             $plan1 = external::update_plan($plan2['id'], 'Can not be updated even if they created it', 'A description', FORMAT_HTML, $this->user->id, 0, plan::STATUS_COMPLETE, 0);
652             $this->fail('Exception expected due to not permissions to create draft plan');
653         } catch (moodle_exception $e) {
654             $this->assertEquals('nopermissions', $e->errorcode);
655         }
656     }
658     /**
659      * Test that we can read plans.
660      */
661     public function test_read_plans() {
662         $this->setUser($this->creator);
664         $syscontext = context_system::instance();
666         $plan1 = external::create_plan('Plan draft by creator', 'A description', FORMAT_HTML, $this->user->id, 0, plan::STATUS_DRAFT, 0);
667         $plan2 = external::create_plan('Plan active by creator', 'A description', FORMAT_HTML, $this->user->id, 0, plan::STATUS_ACTIVE, 0);
668         $plan3 = external::create_plan('Plan complete by creator', 'A description', FORMAT_HTML, $this->user->id, 0, plan::STATUS_COMPLETE, 0);
670         $this->assertEquals((Array)$plan1, external::read_plan($plan1['id']));
671         $this->assertEquals((Array)$plan2, external::read_plan($plan2['id']));
672         $this->assertEquals((Array)$plan3, external::read_plan($plan3['id']));
674         $this->setUser($this->user);
676         // The normal user can not edit these plans.
677         $plan1['usercanupdate'] = false;
678         $plan2['usercanupdate'] = false;
679         $plan3['usercanupdate'] = false;
681         // You need planmanage, planmanageown or plancreatedraft to see draft plans.
682         try {
683             external::read_plan($plan1['id']);
684             $this->fail('Exception expected due to not permissions to read draft plan');
685         } catch (moodle_exception $e) {
686             $this->assertEquals('nopermissions', $e->errorcode);
687         }
688         $this->assertEquals((Array)$plan2, external::read_plan($plan2['id']));
689         $this->assertEquals((Array)$plan3, external::read_plan($plan3['id']));
691         assign_capability('tool/lp:plancreatedraft', CAP_ALLOW, $this->userrole, $syscontext->id);
692         accesslib_clear_all_caches_for_unit_testing();
694         $this->assertEquals((Array)$plan1, external::read_plan($plan1['id']));
696         assign_capability('tool/lp:planviewown', CAP_PROHIBIT, $this->userrole, $syscontext->id);
697         unassign_capability('tool/lp:plancreatedraft', $this->userrole, $syscontext->id);
698         accesslib_clear_all_caches_for_unit_testing();
700         try {
701             $plan = external::read_plan($plan2['id']);
702             $this->fail('Exception expected due to not permissions to view own plans');
703         } catch (moodle_exception $e) {
704             $this->assertEquals('nopermissions', $e->errorcode);
705         }
706     }
708     public function test_delete_plans() {
709         $this->setUser($this->creator);
711         $syscontext = context_system::instance();
713         $plan1 = external::create_plan('1', 'A description', FORMAT_HTML, $this->user->id, 0, plan::STATUS_COMPLETE, 0);
714         $plan2 = external::create_plan('2', 'A description', FORMAT_HTML, $this->user->id, 0, plan::STATUS_COMPLETE, 0);
715         $plan3 = external::create_plan('3', 'A description', FORMAT_HTML, $this->creator->id, 0, plan::STATUS_COMPLETE, 0);
717         $this->assertTrue(external::delete_plan($plan1['id']));
719         unassign_capability('tool/lp:planmanage', $this->creatorrole, $syscontext->id);
720         accesslib_clear_all_caches_for_unit_testing();
722         try {
723             external::delete_plan($plan2['id']);
724             $this->fail('Exception expected due to not permissions to manage plans');
725         } catch (moodle_exception $e) {
726             $this->assertEquals('nopermissions', $e->errorcode);
727         }
729         $this->setUser($this->user);
731         // Can not delete plans created by other users.
732         try {
733             external::delete_plan($plan2['id']);
734             $this->fail('Exception expected due to not permissions to manage plans');
735         } catch (moodle_exception $e) {
736             $this->assertEquals('nopermissions', $e->errorcode);
737         }
739         assign_capability('tool/lp:planmanageown', CAP_ALLOW, $this->userrole, $syscontext->id);
740         accesslib_clear_all_caches_for_unit_testing();
742         $this->assertTrue(external::delete_plan($plan2['id']));
744         // Can not delete plans created for other users.
745         try {
746             external::delete_plan($plan3['id']);
747             $this->fail('Exception expected due to not permissions to manage plans');
748         } catch (moodle_exception $e) {
749             $this->assertEquals('nopermissions', $e->errorcode);
750         }
752         $plan4 = external::create_plan('4', 'A description', FORMAT_HTML, $this->user->id, 0, plan::STATUS_COMPLETE, 0);
753         $this->assertTrue(external::delete_plan($plan4['id']));
754     }
756     public function test_add_competency_to_template() {
757         $this->setUser($this->creator);
759         $syscontext = context_system::instance();
761         // Create a template.
762         $template = external::create_template('shortname', 'idnumber', time(), 'description', FORMAT_HTML, true);
763         $template = (object) external_api::clean_returnvalue(external::create_template_returns(), $template);
765         // Create a competency.
766         $framework = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, true);
767         $framework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $framework);
768         $competency = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $framework->id, 0);
769         $competency = (object) external_api::clean_returnvalue(external::create_competency_returns(), $competency);
771         // Add the competency.
772         external::add_competency_to_template($template->id, $competency->id);
774         // Check that it was added.
775         $this->assertEquals(1, external::count_competencies_in_template($template->id));
777         // Unassign capability.
778         unassign_capability('tool/lp:templatecompetencymanage', $this->creatorrole, $syscontext->id);
779         accesslib_clear_all_caches_for_unit_testing();
781         // Check we can not add the competency now.
782         try {
783             external::add_competency_to_template($template->id, $competency->id);
784             $this->fail('Exception expected due to not permissions to manage template competencies');
785         } catch (moodle_exception $e) {
786             $this->assertEquals('nopermissions', $e->errorcode);
787         }
788     }
790     public function test_remove_competency_from_template() {
791         $this->setUser($this->creator);
793         $syscontext = context_system::instance();
795         // Create a template.
796         $template = external::create_template('shortname', 'idnumber', time(), 'description', FORMAT_HTML, true);
797         $template = (object) external_api::clean_returnvalue(external::create_template_returns(), $template);
799         // Create a competency.
800         $framework = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, true);
801         $framework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $framework);
802         $competency = external::create_competency('shortname', 'idnumber', 'description', FORMAT_HTML, true, $framework->id, 0);
803         $competency = (object) external_api::clean_returnvalue(external::create_competency_returns(), $competency);
805         // Add the competency.
806         external::add_competency_to_template($template->id, $competency->id);
808         // Check that it was added.
809         $this->assertEquals(1, external::count_competencies_in_template($template->id));
811         // Check that we can remove the competency.
812         external::remove_competency_from_template($template->id, $competency->id);
814         // Check that it was removed.
815         $this->assertEquals(0, external::count_competencies_in_template($template->id));
817         // Unassign capability.
818         unassign_capability('tool/lp:templatecompetencymanage', $this->creatorrole, $syscontext->id);
819         accesslib_clear_all_caches_for_unit_testing();
821         // Check we can not remove the competency now.
822         try {
823             external::add_competency_to_template($template->id, $competency->id);
824             $this->fail('Exception expected due to not permissions to manage template competencies');
825         } catch (moodle_exception $e) {
826             $this->assertEquals('nopermissions', $e->errorcode);
827         }
828     }
830     /**
831      * Test we can re-order competency frameworks.
832      */
833     public function test_reorder_template_competencies() {
834         $this->setUser($this->creator);
836         $syscontext = context_system::instance();
838         // Create a template.
839         $template = external::create_template('shortname', 'idnumber', time(), 'description', FORMAT_HTML, true);
840         $template = (object) external_api::clean_returnvalue(external::create_template_returns(), $template);
842         // Create a competency framework.
843         $framework = external::create_competency_framework('shortname', 'idnumber', 'description', FORMAT_HTML, true);
844         $framework = (object) external_api::clean_returnvalue(external::create_competency_framework_returns(), $framework);
846         // Create multiple competencies.
847         $competency1 = external::create_competency('shortname1', 'idnumber', 'description', FORMAT_HTML, true, $framework->id, 0);
848         $competency1 = (object) external_api::clean_returnvalue(external::create_competency_returns(), $competency1);
849         $competency2 = external::create_competency('shortname2', 'idnumber', 'description', FORMAT_HTML, true, $framework->id, 0);
850         $competency2 = (object) external_api::clean_returnvalue(external::create_competency_returns(), $competency2);
851         $competency3 = external::create_competency('shortname3', 'idnumber', 'description', FORMAT_HTML, true, $framework->id, 0);
852         $competency3 = (object) external_api::clean_returnvalue(external::create_competency_returns(), $competency3);
854         // Add the competencies.
855         external::add_competency_to_template($template->id, $competency1->id);
856         external::add_competency_to_template($template->id, $competency2->id);
857         external::add_competency_to_template($template->id, $competency3->id);
859         // This is a move up.
860         external::reorder_template_competency($template->id, $competency3->id, $competency2->id);
861         $result = external::list_competencies_in_template($template->id);
862         $result = external_api::clean_returnvalue(external::list_competencies_in_template_returns(), $result);
864         $r1 = (object) $result[0];
865         $r2 = (object) $result[1];
866         $r3 = (object) $result[2];
868         $this->assertEquals($competency1->id, $r1->id);
869         $this->assertEquals($competency3->id, $r2->id);
870         $this->assertEquals($competency2->id, $r3->id);
872         // This is a move down.
873         external::reorder_template_competency($template->id, $competency1->id, $competency3->id);
874         $result = external::list_competencies_in_template($template->id);
875         $result = external_api::clean_returnvalue(external::list_competencies_in_template_returns(), $result);
877         $r1 = (object) $result[0];
878         $r2 = (object) $result[1];
879         $r3 = (object) $result[2];
881         $this->assertEquals($competency3->id, $r1->id);
882         $this->assertEquals($competency1->id, $r2->id);
883         $this->assertEquals($competency2->id, $r3->id);
885         $this->setExpectedException('required_capability_exception');
886         $this->setUser($this->user);
887         external::reorder_template_competency($template->id, $competency1->id, $competency2->id);
888     }
890     /**
891      * Test that we can return scale values for a scale with the scale ID.
892      */
893     public function test_get_scale_values() {
894         global $DB;
895         // Create a scale.
896         $record = new stdClass();
897         $record->courseid = 0;
898         $record->userid = $this->creator->id;
899         $record->name = 'Test scale';
900         $record->scale = 'Poor, Not good, Okay, Fine, Excellent';
901         $record->description = '<p>Test scale description.</p>';
902         $record->descriptionformat = 1;
903         $record->timemodified = time();
904         $scaleid = $DB->insert_record('scale', $record);
905         // Expected return value.
906         $expected = array(array(
907                 'id' => 1,
908                 'name' => 'Excellent'
909             ), array(
910                 'id' => 2,
911                 'name' => 'Fine'
912             ), array(
913                 'id' => 3,
914                 'name' => 'Okay'
915             ), array(
916                 'id' => 4,
917                 'name' => 'Not good'
918             ), array(
919                 'id' => 5,
920                 'name' => 'Poor'
921             )
922         );
923         // Call the webservice.
924         $result = external::get_scale_values($scaleid);
925         $this->assertEquals($expected, $result);
926     }