MDL-52196 tool_lp: New persistent for template/cohort relationship
[moodle.git] / admin / tool / lp / tests / api_test.php
CommitLineData
f610a957
FM
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * API tests.
19 *
20 * @package tool_lp
21 * @copyright 2015 Frédéric Massart - FMCorz.net
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26global $CFG;
27
28use tool_lp\api;
29
30/**
31 * API tests.
32 *
33 * @package tool_lp
34 * @copyright 2015 Frédéric Massart - FMCorz.net
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class tool_lp_api_testcase extends advanced_testcase {
38
39 public function test_get_framework_related_contexts() {
40 $this->resetAfterTest(true);
41 $dg = $this->getDataGenerator();
42 $cat1 = $dg->create_category();
43 $cat2 = $dg->create_category(array('parent' => $cat1->id));
44 $cat3 = $dg->create_category(array('parent' => $cat2->id));
f0da26a4 45 $c1 = $dg->create_course(array('category' => $cat2->id)); // This context should not be returned.
f610a957
FM
46
47 $cat1ctx = context_coursecat::instance($cat1->id);
48 $cat2ctx = context_coursecat::instance($cat2->id);
49 $cat3ctx = context_coursecat::instance($cat3->id);
50 $sysctx = context_system::instance();
51
52 $expected = array($cat1ctx->id => $cat1ctx);
f0da26a4 53 $this->assertEquals($expected, api::get_related_contexts($cat1ctx, 'self'));
f610a957
FM
54
55 $expected = array($cat1ctx->id => $cat1ctx, $cat2ctx->id => $cat2ctx, $cat3ctx->id => $cat3ctx);
f0da26a4 56 $this->assertEquals($expected, api::get_related_contexts($cat1ctx, 'children'));
f610a957
FM
57
58 $expected = array($sysctx->id => $sysctx, $cat1ctx->id => $cat1ctx, $cat2ctx->id => $cat2ctx);
f0da26a4 59 $this->assertEquals($expected, api::get_related_contexts($cat2ctx, 'parents'));
f610a957
FM
60 }
61
62 public function test_get_framework_related_contexts_with_capabilities() {
63 $this->resetAfterTest(true);
64 $dg = $this->getDataGenerator();
65 $user = $dg->create_user();
66 $cat1 = $dg->create_category();
67 $cat2 = $dg->create_category(array('parent' => $cat1->id));
68 $cat3 = $dg->create_category(array('parent' => $cat2->id));
f0da26a4 69 $c1 = $dg->create_course(array('category' => $cat2->id)); // This context should not be returned.
f610a957
FM
70
71 $cat1ctx = context_coursecat::instance($cat1->id);
72 $cat2ctx = context_coursecat::instance($cat2->id);
73 $cat3ctx = context_coursecat::instance($cat3->id);
74 $sysctx = context_system::instance();
75
76 $roleallow = create_role('Allow', 'allow', 'Allow read');
77 assign_capability('tool/lp:competencyread', CAP_ALLOW, $roleallow, $sysctx->id);
78 role_assign($roleallow, $user->id, $sysctx->id);
79
80 $roleprevent = create_role('Prevent', 'prevent', 'Prevent read');
81 assign_capability('tool/lp:competencyread', CAP_PROHIBIT, $roleprevent, $sysctx->id);
82 role_assign($roleprevent, $user->id, $cat2ctx->id);
83
84 accesslib_clear_all_caches_for_unit_testing();
85 $this->setUser($user);
86 $this->assertFalse(has_capability('tool/lp:competencyread', $cat2ctx));
87
88 $requiredcap = array('tool/lp:competencyread');
89
90 $expected = array();
f0da26a4 91 $this->assertEquals($expected, api::get_related_contexts($cat2ctx, 'self', $requiredcap));
f610a957
FM
92
93 $expected = array($cat1ctx->id => $cat1ctx);
f0da26a4 94 $this->assertEquals($expected, api::get_related_contexts($cat1ctx, 'children', $requiredcap));
f610a957
FM
95
96 $expected = array($sysctx->id => $sysctx, $cat1ctx->id => $cat1ctx);
f0da26a4 97 $this->assertEquals($expected, api::get_related_contexts($cat2ctx, 'parents', $requiredcap));
f610a957 98 }
f0da26a4
FM
99
100 public function test_get_template_related_contexts() {
101 $this->resetAfterTest(true);
102 $dg = $this->getDataGenerator();
103 $cat1 = $dg->create_category();
104 $cat2 = $dg->create_category(array('parent' => $cat1->id));
105 $cat3 = $dg->create_category(array('parent' => $cat2->id));
106 $c1 = $dg->create_course(array('category' => $cat2->id)); // This context should not be returned.
107
108 $cat1ctx = context_coursecat::instance($cat1->id);
109 $cat2ctx = context_coursecat::instance($cat2->id);
110 $cat3ctx = context_coursecat::instance($cat3->id);
111 $sysctx = context_system::instance();
112
113 $expected = array($cat1ctx->id => $cat1ctx);
114 $this->assertEquals($expected, api::get_related_contexts($cat1ctx, 'self'));
115
116 $expected = array($cat1ctx->id => $cat1ctx, $cat2ctx->id => $cat2ctx, $cat3ctx->id => $cat3ctx);
117 $this->assertEquals($expected, api::get_related_contexts($cat1ctx, 'children'));
118
119 $expected = array($sysctx->id => $sysctx, $cat1ctx->id => $cat1ctx, $cat2ctx->id => $cat2ctx);
120 $this->assertEquals($expected, api::get_related_contexts($cat2ctx, 'parents'));
121 }
122
123 public function test_get_template_related_contexts_with_capabilities() {
124 $this->resetAfterTest(true);
125 $dg = $this->getDataGenerator();
126 $user = $dg->create_user();
127 $cat1 = $dg->create_category();
128 $cat2 = $dg->create_category(array('parent' => $cat1->id));
129 $cat3 = $dg->create_category(array('parent' => $cat2->id));
130 $c1 = $dg->create_course(array('category' => $cat2->id)); // This context should not be returned.
131
132 $cat1ctx = context_coursecat::instance($cat1->id);
133 $cat2ctx = context_coursecat::instance($cat2->id);
134 $cat3ctx = context_coursecat::instance($cat3->id);
135 $sysctx = context_system::instance();
136
137 $roleallow = create_role('Allow', 'allow', 'Allow read');
138 assign_capability('tool/lp:templateread', CAP_ALLOW, $roleallow, $sysctx->id);
139 role_assign($roleallow, $user->id, $sysctx->id);
140
141 $roleprevent = create_role('Prevent', 'prevent', 'Prevent read');
142 assign_capability('tool/lp:templateread', CAP_PROHIBIT, $roleprevent, $sysctx->id);
143 role_assign($roleprevent, $user->id, $cat2ctx->id);
144
145 accesslib_clear_all_caches_for_unit_testing();
146 $this->setUser($user);
147 $this->assertFalse(has_capability('tool/lp:templateread', $cat2ctx));
148
149 $requiredcap = array('tool/lp:templateread');
150
151 $expected = array();
152 $this->assertEquals($expected, api::get_related_contexts($cat2ctx, 'self', $requiredcap));
153
154 $expected = array($cat1ctx->id => $cat1ctx);
155 $this->assertEquals($expected, api::get_related_contexts($cat1ctx, 'children', $requiredcap));
156
157 $expected = array($sysctx->id => $sysctx, $cat1ctx->id => $cat1ctx);
158 $this->assertEquals($expected, api::get_related_contexts($cat2ctx, 'parents', $requiredcap));
159 }
160
ec324dc6
FM
161 /**
162 * Test updating a template.
163 */
164 public function test_update_template() {
165 $cat = $this->getDataGenerator()->create_category();
166 $this->resetAfterTest(true);
167 $this->setAdminUser();
168
169 $syscontext = context_system::instance();
170 $template = api::create_template((object) array('shortname' => 'testing', 'contextid' => $syscontext->id));
171
172 $this->assertEquals('testing', $template->get_shortname());
173 $this->assertEquals($syscontext->id, $template->get_contextid());
174
175 // Simple update.
176 api::update_template((object) array('id' => $template->get_id(), 'shortname' => 'success'));
177 $template = api::read_template($template->get_id());
178 $this->assertEquals('success', $template->get_shortname());
179
180 // Trying to change the context.
181 $this->setExpectedException('coding_exception');
182 api::update_template((object) array('id' => $template->get_id(), 'contextid' => context_coursecat::instance($cat->id)));
183 }
184
b3979696
IT
185 /**
186 * Test listing framework with order param.
187 */
188 public function test_list_frameworks() {
189 $this->resetAfterTest(true);
190 $this->setAdminUser();
bf61cd0c 191 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
b3979696
IT
192
193 // Create a list of frameworks.
bf61cd0c
FM
194 $framework1 = $lpg->create_framework(array(
195 'shortname' => 'shortname_a',
196 'idnumber' => 'idnumber_c',
197 'description' => 'description',
198 'descriptionformat' => FORMAT_HTML,
199 'visible' => true,
200 'contextid' => context_system::instance()->id
201 ));
202
203 $framework2 = $lpg->create_framework(array(
204 'shortname' => 'shortname_b',
205 'idnumber' => 'idnumber_a',
206 'description' => 'description',
207 'descriptionformat' => FORMAT_HTML,
208 'visible' => true,
209 'contextid' => context_system::instance()->id
210 ));
211
212 $framework3 = $lpg->create_framework(array(
213 'shortname' => 'shortname_c',
214 'idnumber' => 'idnumber_b',
215 'description' => 'description',
216 'descriptionformat' => FORMAT_HTML,
217 'visible' => true,
218 'contextid' => context_system::instance()->id
219 ));
b3979696
IT
220
221 // Get frameworks list order by shortname desc.
222 $result = api::list_frameworks('shortname', 'DESC', null, 3, context_system::instance());
223
1896274f
FM
224 $f = (object) array_shift($result);
225 $this->assertEquals($framework3->get_id(), $f->get_id());
226 $f = (object) array_shift($result);
227 $this->assertEquals($framework2->get_id(), $f->get_id());
228 $f = (object) array_shift($result);
229 $this->assertEquals($framework1->get_id(), $f->get_id());
b3979696
IT
230
231 // Get frameworks list order by idnumber asc.
232 $result = api::list_frameworks('idnumber', 'ASC', null, 3, context_system::instance());
233
1896274f
FM
234 $f = (object) array_shift($result);
235 $this->assertEquals($framework2->get_id(), $f->get_id());
236 $f = (object) array_shift($result);
237 $this->assertEquals($framework3->get_id(), $f->get_id());
238 $f = (object) array_shift($result);
239 $this->assertEquals($framework1->get_id(), $f->get_id());
b3979696
IT
240 }
241
c61db7bb
IT
242 /**
243 * Test duplicate a framework.
244 */
245 public function test_duplicate_framework() {
246 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
247 $this->resetAfterTest(true);
248 $this->setAdminUser();
249
250 $syscontext = context_system::instance();
251 $params = array(
252 'shortname' => 'shortname_a',
253 'idnumber' => 'idnumber_c',
254 'description' => 'description',
255 'descriptionformat' => FORMAT_HTML,
256 'visible' => true,
257 'contextid' => $syscontext->id
258 );
259 $framework = $lpg->create_framework($params);
260 $competency1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
261 $competency2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
262 $competency3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
263 $competency4 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
264 $competencyidnumbers = array($competency1->get_idnumber(),
265 $competency2->get_idnumber(),
266 $competency3->get_idnumber(),
267 $competency4->get_idnumber()
268 );
269
270 api::add_related_competency($competency1->get_id(), $competency2->get_id());
271 api::add_related_competency($competency3->get_id(), $competency4->get_id());
272
273 $frameworkduplicated1 = api::duplicate_framework($framework->get_id());
274 $frameworkduplicated2 = api::duplicate_framework($framework->get_id());
275
276 $this->assertEquals($framework->get_idnumber().'_1', $frameworkduplicated1->get_idnumber());
277 $this->assertEquals($framework->get_idnumber().'_2', $frameworkduplicated2->get_idnumber());
278
279 $competenciesfr1 = api::list_competencies(array('competencyframeworkid' => $frameworkduplicated1->get_id()));
280 $competenciesfr2 = api::list_competencies(array('competencyframeworkid' => $frameworkduplicated2->get_id()));
281
282 $competencyidsfr1 = array();
283 $competencyidsfr2 = array();
284
285 foreach ($competenciesfr1 as $cmp) {
286 $competencyidsfr1[] = $cmp->get_idnumber();
287 }
288 foreach ($competenciesfr2 as $cmp) {
289 $competencyidsfr2[] = $cmp->get_idnumber();
290 }
291
292 $this->assertEmpty(array_diff($competencyidsfr1, $competencyidnumbers));
293 $this->assertEmpty(array_diff($competencyidsfr2, $competencyidnumbers));
294 $this->assertCount(4, $competenciesfr1);
295 $this->assertCount(4, $competenciesfr2);
296
297 // Test the related competencies.
298 reset($competenciesfr1);
299 $compduplicated1 = current($competenciesfr1);
300 $relatedcompetencies = $compduplicated1->get_related_competencies();
301 $comprelated = current($relatedcompetencies);
302 $this->assertEquals($comprelated->get_idnumber(), $competency2->get_idnumber());
303 }
304
58405003
IT
305 /**
306 * Test update plan.
307 */
308 public function test_update_plan() {
309 $this->resetAfterTest(true);
310 $dg = $this->getDataGenerator();
311 $usermanageowndraft = $dg->create_user();
312 $usermanageown = $dg->create_user();
313 $usermanagedraft = $dg->create_user();
314 $usermanage = $dg->create_user();
315
316 $syscontext = context_system::instance();
317
318 // Creating specific roles.
319 $manageowndraftrole = $dg->create_role(array(
320 'name' => 'User manage own draft',
321 'shortname' => 'manage-own-draft'
322 ));
323 $manageownrole = $dg->create_role(array(
324 'name' => 'User manage own',
325 'shortname' => 'manage-own'
326 ));
327 $managedraftrole = $dg->create_role(array(
328 'name' => 'User manage draft',
329 'shortname' => 'manage-draft'
330 ));
331 $managerole = $dg->create_role(array(
332 'name' => 'User manage',
333 'shortname' => 'manage'
334 ));
335
336 assign_capability('tool/lp:planmanageowndraft', CAP_ALLOW, $manageowndraftrole, $syscontext->id);
337 assign_capability('tool/lp:planviewowndraft', CAP_ALLOW, $manageowndraftrole, $syscontext->id);
338
339 assign_capability('tool/lp:planmanageown', CAP_ALLOW, $manageownrole, $syscontext->id);
340 assign_capability('tool/lp:planviewown', CAP_ALLOW, $manageownrole, $syscontext->id);
341
342 assign_capability('tool/lp:planmanagedraft', CAP_ALLOW, $managedraftrole, $syscontext->id);
343 assign_capability('tool/lp:planviewdraft', CAP_ALLOW, $managedraftrole, $syscontext->id);
344
345 assign_capability('tool/lp:planmanage', CAP_ALLOW, $managerole, $syscontext->id);
346 assign_capability('tool/lp:planview', CAP_ALLOW, $managerole, $syscontext->id);
347
348 $dg->role_assign($manageowndraftrole, $usermanageowndraft->id, $syscontext->id);
349 $dg->role_assign($manageownrole, $usermanageown->id, $syscontext->id);
350 $dg->role_assign($managedraftrole, $usermanagedraft->id, $syscontext->id);
351 $dg->role_assign($managerole, $usermanage->id, $syscontext->id);
352
353 // Create first learning plan with user create draft.
354 $this->setUser($usermanageowndraft);
355 $plan = array (
356 'name' => 'plan own draft',
357 'description' => 'plan own draft',
358 'userid' => $usermanageowndraft->id
359 );
360 $plan = api::create_plan((object)$plan);
361 $record = $plan->to_record();
362 $record->name = 'plan own draft modified';
363
364 // Check if user create draft can edit the plan name.
365 $plan = api::update_plan($record);
366 $this->assertInstanceOf('\tool_lp\plan', $plan);
367
368 // Thrown exception when manageowndraft user try to change the status.
369 $record->status = \tool_lp\plan::STATUS_ACTIVE;
370 try {
371 $plan = api::update_plan($record);
372 $this->fail('User with manage own draft capability cannot edit the plan status.');
373 } catch (required_capability_exception $e) {
374 $this->assertTrue(true);
375 }
376
377 // Test when user with manage own plan capability try to edit other user plan.
378 $record->status = \tool_lp\plan::STATUS_DRAFT;
379 $record->name = 'plan create draft modified 2';
380 $this->setUser($usermanageown);
381 try {
382 $plan = api::update_plan($record);
383 $this->fail('User with manage own plan capability can only edit his own plan.');
384 } catch (required_capability_exception $e) {
385 $this->assertTrue(true);
386 }
387
388 // User with manage plan capability cannot edit the other user plans with status draft.
389 $this->setUser($usermanage);
390 $record->status = \tool_lp\plan::STATUS_COMPLETE;
391 try {
392 $plan = api::update_plan($record);
393 $this->fail('User with manage plan capability cannot edit the other user plans with status draft');
394 } catch (required_capability_exception $e) {
395 $this->assertTrue(true);
396 }
397
398 // User with manage draft capability can edit other user's learning plan if the status is draft.
399 $this->setUser($usermanagedraft);
400 $record->status = \tool_lp\plan::STATUS_DRAFT;
401 $record->name = 'plan manage draft modified 3';
402 $plan = api::update_plan($record);
403 $this->assertInstanceOf('\tool_lp\plan', $plan);
404
405 // User with manage plan capability can create/edit learning plan if status is active/complete.
406 $this->setUser($usermanage);
407 $plan = array (
408 'name' => 'plan create',
409 'description' => 'plan create',
410 'userid' => $usermanage->id,
411 'status' => \tool_lp\plan::STATUS_ACTIVE
412 );
413 $plan = api::create_plan((object)$plan);
5bfab685
FM
414
415 // Silently transition to complete status to avoid errors about transitioning to complete.
416 $plan->set_status(\tool_lp\plan::STATUS_COMPLETE);
417 $plan->update();
418
58405003
IT
419 $record = $plan->to_record();
420 $record->name = 'plan create own modified';
58405003
IT
421 $plan = api::update_plan($record);
422 $this->assertInstanceOf('\tool_lp\plan', $plan);
423
424 }
425
5bfab685
FM
426 /**
427 * Test that the method to complete a plan.
428 */
429 public function test_complete_plan() {
430 global $DB;
431
432 $this->resetAfterTest(true);
433 $this->setAdminUser();
434 $dg = $this->getDataGenerator();
435 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
436 $user = $dg->create_user();
437
438 // Create a framework and assign competencies.
439 $framework = $lpg->create_framework();
440 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
441 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
442 $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
443
444 // Create two plans and assign competencies.
445 $plan = $lpg->create_plan(array('userid' => $user->id));
446 $otherplan = $lpg->create_plan(array('userid' => $user->id));
447
448 $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c1->get_id()));
449 $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c2->get_id()));
450 $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c3->get_id()));
451 $lpg->create_plan_competency(array('planid' => $otherplan->get_id(), 'competencyid' => $c1->get_id()));
452
453 $uclist = array(
454 $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c1->get_id(),
455 'proficiency' => true, 'grade' => 1 )),
456 $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c2->get_id(),
457 'proficiency' => false, 'grade' => 2 ))
458 );
459
460 $this->assertEquals(2, \tool_lp\user_competency::count_records());
461 $this->assertEquals(0, \tool_lp\user_competency_plan::count_records());
462
463 // Change status of the plan to complete.
464 api::complete_plan($plan);
465
466 // Check that user competencies are now in user_competency_plan objects and still in user_competency.
467 $this->assertEquals(2, \tool_lp\user_competency::count_records());
468 $this->assertEquals(3, \tool_lp\user_competency_plan::count_records());
469
470 $usercompetenciesplan = \tool_lp\user_competency_plan::get_records();
471
472 $this->assertEquals($uclist[0]->get_userid(), $usercompetenciesplan[0]->get_userid());
473 $this->assertEquals($uclist[0]->get_competencyid(), $usercompetenciesplan[0]->get_competencyid());
474 $this->assertEquals($uclist[0]->get_proficiency(), (bool) $usercompetenciesplan[0]->get_proficiency());
475 $this->assertEquals($uclist[0]->get_grade(), $usercompetenciesplan[0]->get_grade());
476 $this->assertEquals($plan->get_id(), $usercompetenciesplan[0]->get_planid());
477
478 $this->assertEquals($uclist[1]->get_userid(), $usercompetenciesplan[1]->get_userid());
479 $this->assertEquals($uclist[1]->get_competencyid(), $usercompetenciesplan[1]->get_competencyid());
480 $this->assertEquals($uclist[1]->get_proficiency(), (bool) $usercompetenciesplan[1]->get_proficiency());
481 $this->assertEquals($uclist[1]->get_grade(), $usercompetenciesplan[1]->get_grade());
482 $this->assertEquals($plan->get_id(), $usercompetenciesplan[1]->get_planid());
483
484 $this->assertEquals($user->id, $usercompetenciesplan[2]->get_userid());
485 $this->assertEquals($c3->get_id(), $usercompetenciesplan[2]->get_competencyid());
486 $this->assertNull($usercompetenciesplan[2]->get_proficiency());
487 $this->assertNull($usercompetenciesplan[2]->get_grade());
488 $this->assertEquals($plan->get_id(), $usercompetenciesplan[2]->get_planid());
489
490 // Completing a plan that is completed throws an exception.
491 $this->setExpectedException('coding_exception');
492 api::complete_plan($plan);
493 }
494
192569ed
JPG
495 /**
496 * Test update plan and the managing of archived user competencies.
497 */
498 public function test_update_plan_manage_archived_competencies() {
499 global $DB;
500
501 $this->resetAfterTest(true);
502 $dg = $this->getDataGenerator();
503 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
504
505 $syscontext = context_system::instance();
506
507 // Create users and roles for the test.
508 $user = $dg->create_user();
509 $manageownrole = $dg->create_role(array(
510 'name' => 'User manage own',
511 'shortname' => 'manageown'
512 ));
513 assign_capability('tool/lp:planmanageowndraft', CAP_ALLOW, $manageownrole, $syscontext->id);
514 assign_capability('tool/lp:planviewowndraft', CAP_ALLOW, $manageownrole, $syscontext->id);
515 assign_capability('tool/lp:planmanageown', CAP_ALLOW, $manageownrole, $syscontext->id);
516 assign_capability('tool/lp:planviewown', CAP_ALLOW, $manageownrole, $syscontext->id);
517 $dg->role_assign($manageownrole, $user->id, $syscontext->id);
518 $this->setUser($user);
519
520 // Create a framework and assign competencies.
521 $framework = $lpg->create_framework();
522 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
523 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
524 $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
525
526 // Create two plans and assign competencies.
527 $plan = $lpg->create_plan(array('userid' => $user->id));
528 $otherplan = $lpg->create_plan(array('userid' => $user->id));
529
530 $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c1->get_id()));
531 $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c2->get_id()));
532 $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c3->get_id()));
533 $lpg->create_plan_competency(array('planid' => $otherplan->get_id(), 'competencyid' => $c1->get_id()));
534
535 $uclist = array(
536 $lpg->create_user_competency(array(
537 'userid' => $user->id,
538 'competencyid' => $c1->get_id(),
539 'proficiency' => true,
540 'grade' => 1
541 )),
542 $lpg->create_user_competency(array(
543 'userid' => $user->id,
544 'competencyid' => $c2->get_id(),
545 'proficiency' => false,
546 'grade' => 2
547 ))
548 );
549
550 // Change status of the plan to complete.
551 $record = $plan->to_record();
552 $record->status = \tool_lp\plan::STATUS_COMPLETE;
553
5bfab685
FM
554 try {
555 $plan = api::update_plan($record);
556 $this->fail('We cannot complete a plan using api::update_plan().');
557 } catch (coding_exception $e) {
558 }
559 api::complete_plan($plan);
192569ed
JPG
560
561 // Check that user compretencies are now in user_competency_plan objects and still in user_competency.
562 $this->assertEquals(2, \tool_lp\user_competency::count_records());
563 $this->assertEquals(3, \tool_lp\user_competency_plan::count_records());
564
565 $usercompetenciesplan = \tool_lp\user_competency_plan::get_records();
566
567 $this->assertEquals($uclist[0]->get_userid(), $usercompetenciesplan[0]->get_userid());
568 $this->assertEquals($uclist[0]->get_competencyid(), $usercompetenciesplan[0]->get_competencyid());
569 $this->assertEquals($uclist[0]->get_proficiency(), (bool) $usercompetenciesplan[0]->get_proficiency());
570 $this->assertEquals($uclist[0]->get_grade(), $usercompetenciesplan[0]->get_grade());
571 $this->assertEquals($plan->get_id(), $usercompetenciesplan[0]->get_planid());
572
573 $this->assertEquals($uclist[1]->get_userid(), $usercompetenciesplan[1]->get_userid());
574 $this->assertEquals($uclist[1]->get_competencyid(), $usercompetenciesplan[1]->get_competencyid());
575 $this->assertEquals($uclist[1]->get_proficiency(), (bool) $usercompetenciesplan[1]->get_proficiency());
576 $this->assertEquals($uclist[1]->get_grade(), $usercompetenciesplan[1]->get_grade());
577 $this->assertEquals($plan->get_id(), $usercompetenciesplan[1]->get_planid());
578
579 $this->assertEquals($user->id, $usercompetenciesplan[2]->get_userid());
580 $this->assertEquals($c3->get_id(), $usercompetenciesplan[2]->get_competencyid());
581 $this->assertNull($usercompetenciesplan[2]->get_proficiency());
582 $this->assertNull($usercompetenciesplan[2]->get_grade());
583 $this->assertEquals($plan->get_id(), $usercompetenciesplan[2]->get_planid());
584
192569ed
JPG
585 // Change status of the plan to active.
586 $record = $plan->to_record();
587 $record->status = \tool_lp\plan::STATUS_ACTIVE;
588
589 $plan = api::update_plan($record);
590
591 // Check that user_competency_plan objects are deleted if the plan status is changed to another status.
592 $this->assertEquals(2, \tool_lp\user_competency::count_records());
593 $this->assertEquals(0, \tool_lp\user_competency_plan::count_records());
594 }
595
596 /**
597 * Test remove plan and the managing of archived user competencies.
598 */
599 public function test_delete_plan_manage_archived_competencies() {
600 $this->resetAfterTest(true);
601 $dg = $this->getDataGenerator();
602 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
603
604 $syscontext = context_system::instance();
605
606 // Create user and role for the test.
607 $user = $dg->create_user();
608 $managerole = $dg->create_role(array(
609 'name' => 'User manage own',
610 'shortname' => 'manageown'
611 ));
612 assign_capability('tool/lp:planmanageowndraft', CAP_ALLOW, $managerole, $syscontext->id);
613 assign_capability('tool/lp:planmanageown', CAP_ALLOW, $managerole, $syscontext->id);
614 $dg->role_assign($managerole, $user->id, $syscontext->id);
615 $this->setUser($user);
616
617 // Create a framework and assign competencies.
618 $framework = $lpg->create_framework();
619 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
620 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
621 $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
622
623 // Create completed plan with records in user_competency.
624 $completedplan = $lpg->create_plan(array('userid' => $user->id, 'status' => \tool_lp\plan::STATUS_COMPLETE));
625
626 $lpg->create_plan_competency(array('planid' => $completedplan->get_id(), 'competencyid' => $c1->get_id()));
627 $lpg->create_plan_competency(array('planid' => $completedplan->get_id(), 'competencyid' => $c2->get_id()));
628
629 $uc1 = $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c1->get_id()));
630 $uc2 = $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c2->get_id()));
631
632 $ucp1 = $lpg->create_user_competency_plan(array('userid' => $user->id, 'competencyid' => $c1->get_id(),
633 'planid' => $completedplan->get_id()));
634 $ucp2 = $lpg->create_user_competency_plan(array('userid' => $user->id, 'competencyid' => $c2->get_id(),
635 'planid' => $completedplan->get_id()));
636
637 api::delete_plan($completedplan->get_id());
638
639 // Check that achived user competencies are deleted.
640 $this->assertEquals(0, \tool_lp\plan::count_records());
641 $this->assertEquals(2, \tool_lp\user_competency::count_records());
642 $this->assertEquals(0, \tool_lp\user_competency_plan::count_records());
643 }
644
645 /**
646 * Test listing of plan competencies.
647 */
648 public function test_list_plan_competencies_manage_archived_competencies() {
649 $this->resetAfterTest(true);
650 $dg = $this->getDataGenerator();
651 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
652
653 $syscontext = context_system::instance();
654
655 // Create user and role for the test.
656 $user = $dg->create_user();
657 $viewrole = $dg->create_role(array(
658 'name' => 'User view',
659 'shortname' => 'view'
660 ));
661 assign_capability('tool/lp:planviewdraft', CAP_ALLOW, $viewrole, $syscontext->id);
662 assign_capability('tool/lp:planview', CAP_ALLOW, $viewrole, $syscontext->id);
663 $dg->role_assign($viewrole, $user->id, $syscontext->id);
664 $this->setUser($user);
665
666 // Create a framework and assign competencies.
667 $framework = $lpg->create_framework();
668 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
669 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
670 $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
671
672 // Create draft plan with records in user_competency.
673 $draftplan = $lpg->create_plan(array('userid' => $user->id));
674
675 $lpg->create_plan_competency(array('planid' => $draftplan->get_id(), 'competencyid' => $c1->get_id()));
676 $lpg->create_plan_competency(array('planid' => $draftplan->get_id(), 'competencyid' => $c2->get_id()));
677 $lpg->create_plan_competency(array('planid' => $draftplan->get_id(), 'competencyid' => $c3->get_id()));
678
679 $uc1 = $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c1->get_id()));
680 $uc2 = $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c2->get_id()));
681
682 // Check that user_competency objects are returned when plan status is not complete.
683 $plancompetencies = api::list_plan_competencies($draftplan);
684
685 $this->assertCount(3, $plancompetencies);
686 $this->assertInstanceOf('\tool_lp\user_competency', $plancompetencies[0]->usercompetency);
687 $this->assertEquals($uc1->get_id(), $plancompetencies[0]->usercompetency->get_id());
688 $this->assertNull($plancompetencies[0]->usercompetencyplan);
689
690 $this->assertInstanceOf('\tool_lp\user_competency', $plancompetencies[1]->usercompetency);
691 $this->assertEquals($uc2->get_id(), $plancompetencies[1]->usercompetency->get_id());
692 $this->assertNull($plancompetencies[1]->usercompetencyplan);
693
694 $this->assertInstanceOf('\tool_lp\user_competency', $plancompetencies[2]->usercompetency);
695 $this->assertEquals(0, $plancompetencies[2]->usercompetency->get_id());
696 $this->assertNull($plancompetencies[2]->usercompetencyplan);
697
698 // Create completed plan with records in user_competency_plan.
699 $completedplan = $lpg->create_plan(array('userid' => $user->id, 'status' => \tool_lp\plan::STATUS_COMPLETE));
700
701 $pc1 = $lpg->create_plan_competency(array('planid' => $completedplan->get_id(), 'competencyid' => $c1->get_id()));
702 $pc2 = $lpg->create_plan_competency(array('planid' => $completedplan->get_id(), 'competencyid' => $c2->get_id()));
703 $pc3 = $lpg->create_plan_competency(array('planid' => $completedplan->get_id(), 'competencyid' => $c3->get_id()));
704
705 $ucp1 = $lpg->create_user_competency_plan(array('userid' => $user->id, 'competencyid' => $c1->get_id(),
706 'planid' => $completedplan->get_id()));
707 $ucp2 = $lpg->create_user_competency_plan(array('userid' => $user->id, 'competencyid' => $c2->get_id(),
708 'planid' => $completedplan->get_id()));
709
710 // Check that an exception is thrown when a user competency plan is missing.
711 try {
712 $plancompetencies = api::list_plan_competencies($completedplan);
713 $this->fail('All competencies in the plan must be associated to a user competency plan');
714 } catch (coding_exception $e) {
715 $this->assertTrue(true);
716 }
717
718 $ucp3 = $lpg->create_user_competency_plan(array('userid' => $user->id, 'competencyid' => $c3->get_id(),
719 'planid' => $completedplan->get_id()));
720
721 // Check that user_competency_plan objects are returned when plan status is complete.
722 $plancompetencies = api::list_plan_competencies($completedplan);
723
724 $this->assertCount(3, $plancompetencies);
725 $this->assertInstanceOf('\tool_lp\user_competency_plan', $plancompetencies[0]->usercompetencyplan);
726 $this->assertEquals($ucp1->get_id(), $plancompetencies[0]->usercompetencyplan->get_id());
727 $this->assertNull($plancompetencies[0]->usercompetency);
728 $this->assertInstanceOf('\tool_lp\user_competency_plan', $plancompetencies[1]->usercompetencyplan);
729 $this->assertEquals($ucp2->get_id(), $plancompetencies[1]->usercompetencyplan->get_id());
730 $this->assertNull($plancompetencies[1]->usercompetency);
731 $this->assertInstanceOf('\tool_lp\user_competency_plan', $plancompetencies[2]->usercompetencyplan);
732 $this->assertEquals($ucp3->get_id(), $plancompetencies[2]->usercompetencyplan->get_id());
733 $this->assertNull($plancompetencies[2]->usercompetency);
734 }
735
f610a957 736}