MDL-52220 tool_lp: Adjust permission checks related to plans/templates
[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);
58405003
IT
423 }
424
2388d46d
FM
425 public function test_create_plan_from_template() {
426 $this->resetAfterTest(true);
427 $this->setAdminUser();
428
429 $u1 = $this->getDataGenerator()->create_user();
430 $tpl = $this->getDataGenerator()->get_plugin_generator('tool_lp')->create_template();
431
432 // Creating a new plan.
433 $plan = api::create_plan_from_template($tpl, $u1->id);
434 $record = $plan->to_record();
435 $this->assertInstanceOf('\tool_lp\plan', $plan);
436 $this->assertTrue(\tool_lp\plan::record_exists($plan->get_id()));
437 $this->assertEquals($tpl->get_id(), $plan->get_templateid());
438 $this->assertEquals($u1->id, $plan->get_userid());
439 $this->assertTrue($plan->is_based_on_template());
440
441 // Creating a plan that already exists.
442 $plan = api::create_plan_from_template($tpl, $u1->id);
443 $this->assertFalse($plan);
444
445 // Check that api::create_plan cannot be used.
446 $this->setExpectedException('coding_exception');
447 unset($record->id);
448 $plan = api::create_plan($record);
449 }
450
4de877eb
FM
451 public function test_update_plan_based_on_template() {
452 $this->resetAfterTest(true);
453 $dg = $this->getDataGenerator();
454 $lpg = $dg->get_plugin_generator('tool_lp');
455 $u1 = $dg->create_user();
456 $u2 = $dg->create_user();
457
458 $this->setAdminUser();
459 $tpl1 = $lpg->create_template();
460 $tpl2 = $lpg->create_template();
461 $up1 = $lpg->create_plan(array('userid' => $u1->id, 'templateid' => $tpl1->get_id()));
462 $up2 = $lpg->create_plan(array('userid' => $u2->id, 'templateid' => null));
463
464 try {
465 // Trying to remove the template dependency.
466 $record = $up1->to_record();
467 $record->templateid = null;
468 api::update_plan($record);
469 $this->fail('A plan cannot be unlinked using api::update_plan()');
470 } catch (coding_exception $e) {
471 }
472
473 try {
474 // Trying to switch to another template.
475 $record = $up1->to_record();
476 $record->templateid = $tpl2->get_id();
477 api::update_plan($record);
478 $this->fail('A plan cannot be moved to another template.');
479 } catch (coding_exception $e) {
480 }
481
482 try {
483 // Trying to switch to using a template.
484 $record = $up2->to_record();
485 $record->templateid = $tpl1->get_id();
486 api::update_plan($record);
487 $this->fail('A plan cannot be update to use a template.');
488 } catch (coding_exception $e) {
489 }
490 }
491
6d2c2e86
FM
492 public function test_unlink_plan_from_template() {
493 $this->resetAfterTest(true);
494 $dg = $this->getDataGenerator();
495 $lpg = $dg->get_plugin_generator('tool_lp');
496 $u1 = $dg->create_user();
497 $u2 = $dg->create_user();
498
499 $this->setAdminUser();
500 $f1 = $lpg->create_framework();
501 $f2 = $lpg->create_framework();
502 $c1a = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
503 $c2a = $lpg->create_competency(array('competencyframeworkid' => $f2->get_id()));
504 $c1b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
505
506 $tpl1 = $lpg->create_template();
507 $tpl2 = $lpg->create_template();
508
509 $tplc1a = $lpg->create_template_competency(array('templateid' => $tpl1->get_id(), 'competencyid' => $c1a->get_id(),
510 'sortorder' => 9));
511 $tplc1b = $lpg->create_template_competency(array('templateid' => $tpl1->get_id(), 'competencyid' => $c1b->get_id(),
512 'sortorder' => 8));
513 $tplc2a = $lpg->create_template_competency(array('templateid' => $tpl2->get_id(), 'competencyid' => $c2a->get_id()));
514
515 $plan1 = $lpg->create_plan(array('userid' => $u1->id, 'templateid' => $tpl1->get_id()));
516 $plan2 = $lpg->create_plan(array('userid' => $u2->id, 'templateid' => $tpl2->get_id()));
517
518 // Check that we have what we expect at this stage.
519 $this->assertEquals(2, \tool_lp\template_competency::count_records(array('templateid' => $tpl1->get_id())));
520 $this->assertEquals(1, \tool_lp\template_competency::count_records(array('templateid' => $tpl2->get_id())));
521 $this->assertEquals(0, \tool_lp\plan_competency::count_records(array('planid' => $plan1->get_id())));
522 $this->assertEquals(0, \tool_lp\plan_competency::count_records(array('planid' => $plan2->get_id())));
523 $this->assertTrue($plan1->is_based_on_template());
524 $this->assertTrue($plan2->is_based_on_template());
525
526 // Let's do this!
527 $tpl1comps = \tool_lp\template_competency::list_competencies($tpl1->get_id(), true);
528 $tpl2comps = \tool_lp\template_competency::list_competencies($tpl2->get_id(), true);
529
530 api::unlink_plan_from_template($plan1);
531
532 $plan1->read();
533 $plan2->read();
534 $this->assertCount(2, $tpl1comps);
535 $this->assertCount(1, $tpl2comps);
536 $this->assertEquals(2, \tool_lp\template_competency::count_records(array('templateid' => $tpl1->get_id())));
537 $this->assertEquals(1, \tool_lp\template_competency::count_records(array('templateid' => $tpl2->get_id())));
538 $this->assertEquals(2, \tool_lp\plan_competency::count_records(array('planid' => $plan1->get_id())));
539 $this->assertEquals(0, \tool_lp\plan_competency::count_records(array('planid' => $plan2->get_id())));
540 $this->assertFalse($plan1->is_based_on_template());
541 $this->assertEquals($tpl1->get_id(), $plan1->get_origtemplateid());
542 $this->assertTrue($plan2->is_based_on_template());
543 $this->assertEquals(null, $plan2->get_origtemplateid());
544
545 // Even the order remains.
546 $plan1comps = \tool_lp\plan_competency::list_competencies($plan1->get_id());
547 $before = reset($tpl1comps);
548 $after = reset($plan1comps);
549 $this->assertEquals($before->get_id(), $after->get_id());
550 $this->assertEquals($before->get_sortorder(), $after->get_sortorder());
551 $before = next($tpl1comps);
552 $after = next($plan1comps);
553 $this->assertEquals($before->get_id(), $after->get_id());
554 $this->assertEquals($before->get_sortorder(), $after->get_sortorder());
555 }
556
c7999f6d
FM
557 public function test_update_template_updates_plans() {
558 $this->resetAfterTest(true);
559 $this->setAdminUser();
560
561 $dg = $this->getDataGenerator();
562 $u1 = $dg->create_user();
563 $u2 = $dg->create_user();
564 $lpg = $dg->get_plugin_generator('tool_lp');
565 $tpl1 = $lpg->create_template();
566 $tpl2 = $lpg->create_template();
567
568
569 // Create plans with data not matching templates.
570 $time = time();
571 $plan1 = $lpg->create_plan(array('templateid' => $tpl1->get_id(), 'userid' => $u1->id,
572 'name' => 'Not good name', 'duedate' => $time + 3600, 'description' => 'Ahah', 'descriptionformat' => FORMAT_MARKDOWN));
573 $plan2 = $lpg->create_plan(array('templateid' => $tpl1->get_id(), 'userid' => $u2->id,
574 'name' => 'Not right name', 'duedate' => $time + 3601, 'description' => 'Ahah', 'descriptionformat' => FORMAT_PLAIN));
575 $plan3 = $lpg->create_plan(array('templateid' => $tpl2->get_id(), 'userid' => $u1->id,
576 'name' => 'Not sweet name', 'duedate' => $time + 3602, 'description' => 'Ahah', 'descriptionformat' => FORMAT_PLAIN));
577
578 // Prepare our expectations.
579 $plan1->read();
580 $plan2->read();
581 $plan3->read();
582
583 $this->assertEquals($tpl1->get_id(), $plan1->get_templateid());
584 $this->assertEquals($tpl1->get_id(), $plan2->get_templateid());
585 $this->assertEquals($tpl2->get_id(), $plan3->get_templateid());
586 $this->assertNotEquals($tpl1->get_shortname(), $plan1->get_name());
587 $this->assertNotEquals($tpl1->get_shortname(), $plan2->get_name());
588 $this->assertNotEquals($tpl2->get_shortname(), $plan3->get_name());
589 $this->assertNotEquals($tpl1->get_description(), $plan1->get_description());
590 $this->assertNotEquals($tpl1->get_description(), $plan2->get_description());
591 $this->assertNotEquals($tpl2->get_description(), $plan3->get_description());
592 $this->assertNotEquals($tpl1->get_descriptionformat(), $plan1->get_descriptionformat());
593 $this->assertNotEquals($tpl1->get_descriptionformat(), $plan2->get_descriptionformat());
594 $this->assertNotEquals($tpl2->get_descriptionformat(), $plan3->get_descriptionformat());
595 $this->assertNotEquals($tpl1->get_duedate(), $plan1->get_duedate());
596 $this->assertNotEquals($tpl1->get_duedate(), $plan2->get_duedate());
597 $this->assertNotEquals($tpl2->get_duedate(), $plan3->get_duedate());
598
599 // Update the template without changing critical fields does not update the plans.
600 $data = $tpl1->to_record();
601 $data->visible = 0;
602 api::update_template($data);
603 $this->assertNotEquals($tpl1->get_shortname(), $plan1->get_name());
604 $this->assertNotEquals($tpl1->get_shortname(), $plan2->get_name());
605 $this->assertNotEquals($tpl2->get_shortname(), $plan3->get_name());
606 $this->assertNotEquals($tpl1->get_description(), $plan1->get_description());
607 $this->assertNotEquals($tpl1->get_description(), $plan2->get_description());
608 $this->assertNotEquals($tpl2->get_description(), $plan3->get_description());
609 $this->assertNotEquals($tpl1->get_descriptionformat(), $plan1->get_descriptionformat());
610 $this->assertNotEquals($tpl1->get_descriptionformat(), $plan2->get_descriptionformat());
611 $this->assertNotEquals($tpl2->get_descriptionformat(), $plan3->get_descriptionformat());
612 $this->assertNotEquals($tpl1->get_duedate(), $plan1->get_duedate());
613 $this->assertNotEquals($tpl1->get_duedate(), $plan2->get_duedate());
614 $this->assertNotEquals($tpl2->get_duedate(), $plan3->get_duedate());
615
616 // Now really update the template.
617 $data = $tpl1->to_record();
618 $data->shortname = 'Awesome!';
619 $data->description = 'This is too awesome!';
620 $data->descriptionformat = FORMAT_HTML;
621 $data->duedate = $time + 7200;
622 api::update_template($data);
623 $tpl1->read();
624
625 // Now confirm that the right plans were updated.
626 $plan1->read();
627 $plan2->read();
628 $plan3->read();
629
630 $this->assertEquals($tpl1->get_id(), $plan1->get_templateid());
631 $this->assertEquals($tpl1->get_id(), $plan2->get_templateid());
632 $this->assertEquals($tpl2->get_id(), $plan3->get_templateid());
633
634 $this->assertEquals($tpl1->get_shortname(), $plan1->get_name());
635 $this->assertEquals($tpl1->get_shortname(), $plan2->get_name());
636 $this->assertNotEquals($tpl2->get_shortname(), $plan3->get_name());
637 $this->assertEquals($tpl1->get_description(), $plan1->get_description());
638 $this->assertEquals($tpl1->get_description(), $plan2->get_description());
639 $this->assertNotEquals($tpl2->get_description(), $plan3->get_description());
640 $this->assertEquals($tpl1->get_descriptionformat(), $plan1->get_descriptionformat());
641 $this->assertEquals($tpl1->get_descriptionformat(), $plan2->get_descriptionformat());
642 $this->assertNotEquals($tpl2->get_descriptionformat(), $plan3->get_descriptionformat());
643 $this->assertEquals($tpl1->get_duedate(), $plan1->get_duedate());
644 $this->assertEquals($tpl1->get_duedate(), $plan2->get_duedate());
645 $this->assertNotEquals($tpl2->get_duedate(), $plan3->get_duedate());
646 }
647
5bfab685
FM
648 /**
649 * Test that the method to complete a plan.
650 */
651 public function test_complete_plan() {
652 global $DB;
653
654 $this->resetAfterTest(true);
655 $this->setAdminUser();
656 $dg = $this->getDataGenerator();
657 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
658 $user = $dg->create_user();
659
660 // Create a framework and assign competencies.
661 $framework = $lpg->create_framework();
662 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
663 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
664 $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
665
666 // Create two plans and assign competencies.
667 $plan = $lpg->create_plan(array('userid' => $user->id));
668 $otherplan = $lpg->create_plan(array('userid' => $user->id));
669
670 $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c1->get_id()));
671 $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c2->get_id()));
672 $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c3->get_id()));
673 $lpg->create_plan_competency(array('planid' => $otherplan->get_id(), 'competencyid' => $c1->get_id()));
674
675 $uclist = array(
676 $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c1->get_id(),
677 'proficiency' => true, 'grade' => 1 )),
678 $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c2->get_id(),
679 'proficiency' => false, 'grade' => 2 ))
680 );
681
682 $this->assertEquals(2, \tool_lp\user_competency::count_records());
683 $this->assertEquals(0, \tool_lp\user_competency_plan::count_records());
684
685 // Change status of the plan to complete.
686 api::complete_plan($plan);
687
688 // Check that user competencies are now in user_competency_plan objects and still in user_competency.
689 $this->assertEquals(2, \tool_lp\user_competency::count_records());
690 $this->assertEquals(3, \tool_lp\user_competency_plan::count_records());
691
692 $usercompetenciesplan = \tool_lp\user_competency_plan::get_records();
693
694 $this->assertEquals($uclist[0]->get_userid(), $usercompetenciesplan[0]->get_userid());
695 $this->assertEquals($uclist[0]->get_competencyid(), $usercompetenciesplan[0]->get_competencyid());
696 $this->assertEquals($uclist[0]->get_proficiency(), (bool) $usercompetenciesplan[0]->get_proficiency());
697 $this->assertEquals($uclist[0]->get_grade(), $usercompetenciesplan[0]->get_grade());
698 $this->assertEquals($plan->get_id(), $usercompetenciesplan[0]->get_planid());
699
700 $this->assertEquals($uclist[1]->get_userid(), $usercompetenciesplan[1]->get_userid());
701 $this->assertEquals($uclist[1]->get_competencyid(), $usercompetenciesplan[1]->get_competencyid());
702 $this->assertEquals($uclist[1]->get_proficiency(), (bool) $usercompetenciesplan[1]->get_proficiency());
703 $this->assertEquals($uclist[1]->get_grade(), $usercompetenciesplan[1]->get_grade());
704 $this->assertEquals($plan->get_id(), $usercompetenciesplan[1]->get_planid());
705
706 $this->assertEquals($user->id, $usercompetenciesplan[2]->get_userid());
707 $this->assertEquals($c3->get_id(), $usercompetenciesplan[2]->get_competencyid());
708 $this->assertNull($usercompetenciesplan[2]->get_proficiency());
709 $this->assertNull($usercompetenciesplan[2]->get_grade());
710 $this->assertEquals($plan->get_id(), $usercompetenciesplan[2]->get_planid());
711
712 // Completing a plan that is completed throws an exception.
713 $this->setExpectedException('coding_exception');
714 api::complete_plan($plan);
715 }
716
192569ed
JPG
717 /**
718 * Test update plan and the managing of archived user competencies.
719 */
720 public function test_update_plan_manage_archived_competencies() {
721 global $DB;
722
723 $this->resetAfterTest(true);
724 $dg = $this->getDataGenerator();
725 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
726
727 $syscontext = context_system::instance();
728
729 // Create users and roles for the test.
730 $user = $dg->create_user();
731 $manageownrole = $dg->create_role(array(
732 'name' => 'User manage own',
733 'shortname' => 'manageown'
734 ));
735 assign_capability('tool/lp:planmanageowndraft', CAP_ALLOW, $manageownrole, $syscontext->id);
736 assign_capability('tool/lp:planviewowndraft', CAP_ALLOW, $manageownrole, $syscontext->id);
737 assign_capability('tool/lp:planmanageown', CAP_ALLOW, $manageownrole, $syscontext->id);
738 assign_capability('tool/lp:planviewown', CAP_ALLOW, $manageownrole, $syscontext->id);
739 $dg->role_assign($manageownrole, $user->id, $syscontext->id);
740 $this->setUser($user);
741
742 // Create a framework and assign competencies.
743 $framework = $lpg->create_framework();
744 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
745 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
746 $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
747
748 // Create two plans and assign competencies.
749 $plan = $lpg->create_plan(array('userid' => $user->id));
750 $otherplan = $lpg->create_plan(array('userid' => $user->id));
751
752 $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c1->get_id()));
753 $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c2->get_id()));
754 $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c3->get_id()));
755 $lpg->create_plan_competency(array('planid' => $otherplan->get_id(), 'competencyid' => $c1->get_id()));
756
757 $uclist = array(
758 $lpg->create_user_competency(array(
759 'userid' => $user->id,
760 'competencyid' => $c1->get_id(),
761 'proficiency' => true,
762 'grade' => 1
763 )),
764 $lpg->create_user_competency(array(
765 'userid' => $user->id,
766 'competencyid' => $c2->get_id(),
767 'proficiency' => false,
768 'grade' => 2
769 ))
770 );
771
772 // Change status of the plan to complete.
773 $record = $plan->to_record();
774 $record->status = \tool_lp\plan::STATUS_COMPLETE;
775
5bfab685
FM
776 try {
777 $plan = api::update_plan($record);
778 $this->fail('We cannot complete a plan using api::update_plan().');
779 } catch (coding_exception $e) {
780 }
781 api::complete_plan($plan);
192569ed
JPG
782
783 // Check that user compretencies are now in user_competency_plan objects and still in user_competency.
784 $this->assertEquals(2, \tool_lp\user_competency::count_records());
785 $this->assertEquals(3, \tool_lp\user_competency_plan::count_records());
786
787 $usercompetenciesplan = \tool_lp\user_competency_plan::get_records();
788
789 $this->assertEquals($uclist[0]->get_userid(), $usercompetenciesplan[0]->get_userid());
790 $this->assertEquals($uclist[0]->get_competencyid(), $usercompetenciesplan[0]->get_competencyid());
791 $this->assertEquals($uclist[0]->get_proficiency(), (bool) $usercompetenciesplan[0]->get_proficiency());
792 $this->assertEquals($uclist[0]->get_grade(), $usercompetenciesplan[0]->get_grade());
793 $this->assertEquals($plan->get_id(), $usercompetenciesplan[0]->get_planid());
794
795 $this->assertEquals($uclist[1]->get_userid(), $usercompetenciesplan[1]->get_userid());
796 $this->assertEquals($uclist[1]->get_competencyid(), $usercompetenciesplan[1]->get_competencyid());
797 $this->assertEquals($uclist[1]->get_proficiency(), (bool) $usercompetenciesplan[1]->get_proficiency());
798 $this->assertEquals($uclist[1]->get_grade(), $usercompetenciesplan[1]->get_grade());
799 $this->assertEquals($plan->get_id(), $usercompetenciesplan[1]->get_planid());
800
801 $this->assertEquals($user->id, $usercompetenciesplan[2]->get_userid());
802 $this->assertEquals($c3->get_id(), $usercompetenciesplan[2]->get_competencyid());
803 $this->assertNull($usercompetenciesplan[2]->get_proficiency());
804 $this->assertNull($usercompetenciesplan[2]->get_grade());
805 $this->assertEquals($plan->get_id(), $usercompetenciesplan[2]->get_planid());
806
192569ed
JPG
807 // Change status of the plan to active.
808 $record = $plan->to_record();
809 $record->status = \tool_lp\plan::STATUS_ACTIVE;
810
811 $plan = api::update_plan($record);
812
813 // Check that user_competency_plan objects are deleted if the plan status is changed to another status.
814 $this->assertEquals(2, \tool_lp\user_competency::count_records());
815 $this->assertEquals(0, \tool_lp\user_competency_plan::count_records());
816 }
817
818 /**
819 * Test remove plan and the managing of archived user competencies.
820 */
821 public function test_delete_plan_manage_archived_competencies() {
822 $this->resetAfterTest(true);
823 $dg = $this->getDataGenerator();
824 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
825
826 $syscontext = context_system::instance();
827
828 // Create user and role for the test.
829 $user = $dg->create_user();
830 $managerole = $dg->create_role(array(
831 'name' => 'User manage own',
832 'shortname' => 'manageown'
833 ));
834 assign_capability('tool/lp:planmanageowndraft', CAP_ALLOW, $managerole, $syscontext->id);
835 assign_capability('tool/lp:planmanageown', CAP_ALLOW, $managerole, $syscontext->id);
836 $dg->role_assign($managerole, $user->id, $syscontext->id);
837 $this->setUser($user);
838
839 // Create a framework and assign competencies.
840 $framework = $lpg->create_framework();
841 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
842 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
843 $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
844
845 // Create completed plan with records in user_competency.
846 $completedplan = $lpg->create_plan(array('userid' => $user->id, 'status' => \tool_lp\plan::STATUS_COMPLETE));
847
848 $lpg->create_plan_competency(array('planid' => $completedplan->get_id(), 'competencyid' => $c1->get_id()));
849 $lpg->create_plan_competency(array('planid' => $completedplan->get_id(), 'competencyid' => $c2->get_id()));
850
851 $uc1 = $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c1->get_id()));
852 $uc2 = $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c2->get_id()));
853
854 $ucp1 = $lpg->create_user_competency_plan(array('userid' => $user->id, 'competencyid' => $c1->get_id(),
855 'planid' => $completedplan->get_id()));
856 $ucp2 = $lpg->create_user_competency_plan(array('userid' => $user->id, 'competencyid' => $c2->get_id(),
857 'planid' => $completedplan->get_id()));
858
859 api::delete_plan($completedplan->get_id());
860
861 // Check that achived user competencies are deleted.
862 $this->assertEquals(0, \tool_lp\plan::count_records());
863 $this->assertEquals(2, \tool_lp\user_competency::count_records());
864 $this->assertEquals(0, \tool_lp\user_competency_plan::count_records());
865 }
866
867 /**
868 * Test listing of plan competencies.
869 */
870 public function test_list_plan_competencies_manage_archived_competencies() {
871 $this->resetAfterTest(true);
872 $dg = $this->getDataGenerator();
873 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
874
875 $syscontext = context_system::instance();
876
877 // Create user and role for the test.
878 $user = $dg->create_user();
879 $viewrole = $dg->create_role(array(
880 'name' => 'User view',
881 'shortname' => 'view'
882 ));
883 assign_capability('tool/lp:planviewdraft', CAP_ALLOW, $viewrole, $syscontext->id);
884 assign_capability('tool/lp:planview', CAP_ALLOW, $viewrole, $syscontext->id);
885 $dg->role_assign($viewrole, $user->id, $syscontext->id);
886 $this->setUser($user);
887
888 // Create a framework and assign competencies.
889 $framework = $lpg->create_framework();
890 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
891 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
892 $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
893
894 // Create draft plan with records in user_competency.
895 $draftplan = $lpg->create_plan(array('userid' => $user->id));
896
897 $lpg->create_plan_competency(array('planid' => $draftplan->get_id(), 'competencyid' => $c1->get_id()));
898 $lpg->create_plan_competency(array('planid' => $draftplan->get_id(), 'competencyid' => $c2->get_id()));
899 $lpg->create_plan_competency(array('planid' => $draftplan->get_id(), 'competencyid' => $c3->get_id()));
900
901 $uc1 = $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c1->get_id()));
902 $uc2 = $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c2->get_id()));
903
904 // Check that user_competency objects are returned when plan status is not complete.
905 $plancompetencies = api::list_plan_competencies($draftplan);
906
907 $this->assertCount(3, $plancompetencies);
908 $this->assertInstanceOf('\tool_lp\user_competency', $plancompetencies[0]->usercompetency);
909 $this->assertEquals($uc1->get_id(), $plancompetencies[0]->usercompetency->get_id());
910 $this->assertNull($plancompetencies[0]->usercompetencyplan);
911
912 $this->assertInstanceOf('\tool_lp\user_competency', $plancompetencies[1]->usercompetency);
913 $this->assertEquals($uc2->get_id(), $plancompetencies[1]->usercompetency->get_id());
914 $this->assertNull($plancompetencies[1]->usercompetencyplan);
915
916 $this->assertInstanceOf('\tool_lp\user_competency', $plancompetencies[2]->usercompetency);
917 $this->assertEquals(0, $plancompetencies[2]->usercompetency->get_id());
918 $this->assertNull($plancompetencies[2]->usercompetencyplan);
919
920 // Create completed plan with records in user_competency_plan.
921 $completedplan = $lpg->create_plan(array('userid' => $user->id, 'status' => \tool_lp\plan::STATUS_COMPLETE));
922
923 $pc1 = $lpg->create_plan_competency(array('planid' => $completedplan->get_id(), 'competencyid' => $c1->get_id()));
924 $pc2 = $lpg->create_plan_competency(array('planid' => $completedplan->get_id(), 'competencyid' => $c2->get_id()));
925 $pc3 = $lpg->create_plan_competency(array('planid' => $completedplan->get_id(), 'competencyid' => $c3->get_id()));
926
927 $ucp1 = $lpg->create_user_competency_plan(array('userid' => $user->id, 'competencyid' => $c1->get_id(),
928 'planid' => $completedplan->get_id()));
929 $ucp2 = $lpg->create_user_competency_plan(array('userid' => $user->id, 'competencyid' => $c2->get_id(),
930 'planid' => $completedplan->get_id()));
931
932 // Check that an exception is thrown when a user competency plan is missing.
933 try {
934 $plancompetencies = api::list_plan_competencies($completedplan);
935 $this->fail('All competencies in the plan must be associated to a user competency plan');
936 } catch (coding_exception $e) {
937 $this->assertTrue(true);
938 }
939
940 $ucp3 = $lpg->create_user_competency_plan(array('userid' => $user->id, 'competencyid' => $c3->get_id(),
941 'planid' => $completedplan->get_id()));
942
943 // Check that user_competency_plan objects are returned when plan status is complete.
944 $plancompetencies = api::list_plan_competencies($completedplan);
945
946 $this->assertCount(3, $plancompetencies);
947 $this->assertInstanceOf('\tool_lp\user_competency_plan', $plancompetencies[0]->usercompetencyplan);
948 $this->assertEquals($ucp1->get_id(), $plancompetencies[0]->usercompetencyplan->get_id());
949 $this->assertNull($plancompetencies[0]->usercompetency);
950 $this->assertInstanceOf('\tool_lp\user_competency_plan', $plancompetencies[1]->usercompetencyplan);
951 $this->assertEquals($ucp2->get_id(), $plancompetencies[1]->usercompetencyplan->get_id());
952 $this->assertNull($plancompetencies[1]->usercompetency);
953 $this->assertInstanceOf('\tool_lp\user_competency_plan', $plancompetencies[2]->usercompetencyplan);
954 $this->assertEquals($ucp3->get_id(), $plancompetencies[2]->usercompetencyplan->get_id());
955 $this->assertNull($plancompetencies[2]->usercompetency);
956 }
957
bee480a4
FM
958 public function test_create_template_cohort() {
959 $this->resetAfterTest(true);
960 $this->setAdminUser();
961
962 $dg = $this->getDataGenerator();
963 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
964
965 $c1 = $dg->create_cohort();
966 $c2 = $dg->create_cohort();
967 $t1 = $lpg->create_template();
968 $t2 = $lpg->create_template();
969
970 $this->assertEquals(0, \tool_lp\template_cohort::count_records());
971
972 // Create two relations with mixed parameters.
973 $result = api::create_template_cohort($t1->get_id(), $c1->id);
974 $result = api::create_template_cohort($t1, $c2);
975
976 $this->assertEquals(2, \tool_lp\template_cohort::count_records());
977 $this->assertInstanceOf('tool_lp\template_cohort', $result);
978 $this->assertEquals($c2->id, $result->get_cohortid());
979 $this->assertEquals($t1->get_id(), $result->get_templateid());
980 $this->assertEquals(2, \tool_lp\template_cohort::count_records_select('templateid = :id', array('id' => $t1->get_id())));
981 $this->assertEquals(0, \tool_lp\template_cohort::count_records_select('templateid = :id', array('id' => $t2->get_id())));
982 }
983
984 public function test_delete_template_cohort() {
985 $this->resetAfterTest(true);
986 $this->setAdminUser();
987
988 $dg = $this->getDataGenerator();
989 $lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
990
991 $c1 = $dg->create_cohort();
992 $c2 = $dg->create_cohort();
993 $t1 = $lpg->create_template();
994 $t2 = $lpg->create_template();
995 $tc1 = $lpg->create_template_cohort(array('templateid' => $t1->get_id(), 'cohortid' => $c1->id));
996 $tc1 = $lpg->create_template_cohort(array('templateid' => $t2->get_id(), 'cohortid' => $c2->id));
997
998 $this->assertEquals(2, \tool_lp\template_cohort::count_records());
999 $this->assertEquals(1, \tool_lp\template_cohort::count_records_select('templateid = :id', array('id' => $t1->get_id())));
1000 $this->assertEquals(1, \tool_lp\template_cohort::count_records_select('templateid = :id', array('id' => $t2->get_id())));
1001
1002 // Delete existing.
1003 $result = api::delete_template_cohort($t1->get_id(), $c1->id);
1004 $this->assertTrue($result);
1005 $this->assertEquals(1, \tool_lp\template_cohort::count_records());
1006 $this->assertEquals(0, \tool_lp\template_cohort::count_records_select('templateid = :id', array('id' => $t1->get_id())));
1007 $this->assertEquals(1, \tool_lp\template_cohort::count_records_select('templateid = :id', array('id' => $t2->get_id())));
1008
1009 // Delete non-existant.
1010 $result = api::delete_template_cohort($t1->get_id(), $c1->id);
1011 $this->assertTrue($result);
1012 $this->assertEquals(1, \tool_lp\template_cohort::count_records());
1013 $this->assertEquals(0, \tool_lp\template_cohort::count_records_select('templateid = :id', array('id' => $t1->get_id())));
1014 $this->assertEquals(1, \tool_lp\template_cohort::count_records_select('templateid = :id', array('id' => $t2->get_id())));
1015 }
f610a957 1016}