MDL-51036 tool_lp: Method to find plans from user and competency
[moodle.git] / admin / tool / lp / tests / plan_persistent_test.php
CommitLineData
5d2758bc
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 * Plan persistent class 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;
29use tool_lp\plan;
30
31/**
32 * Plan persistent testcase.
33 *
34 * @package tool_lp
35 * @copyright 2015 Frédéric Massart - FMCorz.net
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38class tool_lp_plan_persistent_testcase extends advanced_testcase {
39
40 public function test_get_by_user_and_competency() {
41 $this->resetAfterTest();
42 $this->setAdminUser();
43
44 $dg = $this->getDataGenerator();
45 $lpg = $dg->get_plugin_generator('tool_lp');
46
47 $u1 = $dg->create_user();
48 $u2 = $dg->create_user();
49 $u3 = $dg->create_user();
50 $u4 = $dg->create_user();
51
52 $f1 = $lpg->create_framework();
53 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
54 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
55
56 $tpl1 = $lpg->create_template();
57 $lpg->create_template_competency(array('competencyid' => $c1->get_id(), 'templateid' => $tpl1->get_id()));
58
59 $p1 = $lpg->create_plan(array('userid' => $u1->id));
60 $lpg->create_plan_competency(array('planid' => $p1->get_id(), 'competencyid' => $c1->get_id()));
61 $p2 = $lpg->create_plan(array('userid' => $u2->id));
62 $lpg->create_plan_competency(array('planid' => $p2->get_id(), 'competencyid' => $c1->get_id()));
63 $p3 = $lpg->create_plan(array('userid' => $u3->id, 'templateid' => $tpl1->get_id()));
64 $p4 = $lpg->create_plan(array('userid' => $u4->id, 'templateid' => $tpl1->get_id()));
65 api::complete_plan($p2);
66 api::complete_plan($p4);
67
68 // Finding a plan, not completed.
69 $plans = plan::get_by_user_and_competency($u1->id, $c1->get_id());
70 $this->assertCount(1, $plans);
71 $plan = array_shift($plans);
72 $this->assertEquals($p1->get_id(), $plan->get_id());
73 $this->assertNotEquals(plan::STATUS_COMPLETE, $plan->get_status());
74
75 // Finding a completed plan.
76 $plans = plan::get_by_user_and_competency($u2->id, $c1->get_id());
77 $this->assertCount(1, $plans);
78 $plan = array_shift($plans);
79 $this->assertEquals($p2->get_id(), $plan->get_id());
80 $this->assertEquals(plan::STATUS_COMPLETE, $plan->get_status());
81
82 // Finding a plan based on a template, not completed.
83 $plans = plan::get_by_user_and_competency($u3->id, $c1->get_id());
84 $this->assertCount(1, $plans);
85 $plan = array_shift($plans);
86 $this->assertEquals($p3->get_id(), $plan->get_id());
87 $this->assertTrue($plan->is_based_on_template());
88 $this->assertNotEquals(plan::STATUS_COMPLETE, $plan->get_status());
89
90 // Finding a plan based on a template.
91 $plans = plan::get_by_user_and_competency($u4->id, $c1->get_id());
92 $this->assertCount(1, $plans);
93 $plan = array_shift($plans);
94 $this->assertEquals($p4->get_id(), $plan->get_id());
95 $this->assertTrue($plan->is_based_on_template());
96 $this->assertEquals(plan::STATUS_COMPLETE, $plan->get_status());
97
98 // Finding more than one plan, no template.
99 $p5 = $lpg->create_plan(array('userid' => $u1->id));
100 $lpg->create_plan_competency(array('planid' => $p5->get_id(), 'competencyid' => $c1->get_id()));
101 $plans = plan::get_by_user_and_competency($u1->id, $c1->get_id());
102 $this->assertCount(2, $plans);
103 $plan = array_shift($plans);
104 $this->assertEquals($p1->get_id(), $plan->get_id());
105 $plan = array_shift($plans);
106 $this->assertEquals($p5->get_id(), $plan->get_id());
107
108 // Finding more than one plan, with template.
109 $p6 = $lpg->create_plan(array('userid' => $u1->id, 'templateid' => $tpl1->get_id()));
110 $plans = plan::get_by_user_and_competency($u1->id, $c1->get_id());
111 $this->assertCount(3, $plans);
112 $plan = array_shift($plans);
113 $this->assertEquals($p1->get_id(), $plan->get_id());
114 $plan = array_shift($plans);
115 $this->assertEquals($p5->get_id(), $plan->get_id());
116 $plan = array_shift($plans);
117 $this->assertEquals($p6->get_id(), $plan->get_id());
118
119 // Finding no plans.
120 $plans = plan::get_by_user_and_competency($u1->id, $c2->get_id());
121 $this->assertCount(0, $plans);
122 }
123
124}