MDL-53700 competency: Migrating tests to core
[moodle.git] / competency / tests / generator_test.php
CommitLineData
72a1e0de
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 * Tool LP data generator tests.
19 *
922634d3 20 * @package core_competency
72a1e0de
FM
21 * @category test
22 * @copyright 2015 Frédéric Massart - FMCorz.net
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
67bc0eaf
FM
26use core_competency\competency;
27use core_competency\competency_framework;
28use core_competency\course_competency;
29use core_competency\course_module_competency;
30use core_competency\plan;
31use core_competency\related_competency;
32use core_competency\template;
33use core_competency\template_cohort;
34use core_competency\template_competency;
35use core_competency\user_competency;
36use core_competency\user_competency_plan;
37use core_competency\plan_competency;
38use core_competency\evidence;
6d57a987 39
72a1e0de
FM
40defined('MOODLE_INTERNAL') || die();
41
42/**
43 * Tool LP data generator testcase.
44 *
922634d3 45 * @package core_competency
72a1e0de
FM
46 * @category test
47 * @copyright 2015 Frédéric Massart - FMCorz.net
48 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
49 */
922634d3 50class core_competency_generator_testcase extends advanced_testcase {
72a1e0de
FM
51
52 public function test_create_framework() {
53 $this->resetAfterTest(true);
54
922634d3 55 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
72a1e0de
FM
56 $this->assertEquals(0, competency_framework::count_records());
57 $framework = $lpg->create_framework();
58 $framework = $lpg->create_framework();
59 $this->assertEquals(2, competency_framework::count_records());
67bc0eaf 60 $this->assertInstanceOf('\core_competency\competency_framework', $framework);
72a1e0de
FM
61 }
62
63 public function test_create_competency() {
64 $this->resetAfterTest(true);
65
922634d3 66 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
72a1e0de
FM
67 $framework = $lpg->create_framework();
68 $this->assertEquals(0, competency::count_records());
69 $competency = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
70 $competency = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
71 $this->assertEquals(2, competency::count_records());
67bc0eaf 72 $this->assertInstanceOf('\core_competency\competency', $competency);
72a1e0de
FM
73 }
74
75 public function test_create_related_competency() {
76 $this->resetAfterTest(true);
77
922634d3 78 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
72a1e0de
FM
79 $framework = $lpg->create_framework();
80 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
81 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
82 $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
83 $this->assertEquals(0, related_competency::count_records());
84 $rc = $lpg->create_related_competency(array('competencyid' => $c1->get_id(), 'relatedcompetencyid' => $c2->get_id()));
85 $rc = $lpg->create_related_competency(array('competencyid' => $c2->get_id(), 'relatedcompetencyid' => $c3->get_id()));
86 $this->assertEquals(2, related_competency::count_records());
67bc0eaf 87 $this->assertInstanceOf('\core_competency\related_competency', $rc);
72a1e0de
FM
88 }
89
8a9030c1
SG
90 public function test_create_plan() {
91 $this->resetAfterTest(true);
92
93 $user = $this->getDataGenerator()->create_user();
922634d3 94 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
8a9030c1
SG
95 $this->assertEquals(0, plan::count_records());
96 $plan = $lpg->create_plan(array('userid' => $user->id));
97 $this->assertEquals(1, plan::count_records());
67bc0eaf 98 $this->assertInstanceOf('\core_competency\plan', $plan);
8a9030c1
SG
99 }
100
6d57a987
SG
101 public function test_create_user_competency() {
102 $this->resetAfterTest(true);
103
104 $user = $this->getDataGenerator()->create_user();
922634d3 105 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
6d57a987
SG
106 $framework = $lpg->create_framework();
107 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
108 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
109 $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
110 $this->assertEquals(0, user_competency::count_records());
111 $rc = $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c1->get_id()));
112 $rc = $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c2->get_id()));
113 $this->assertEquals(2, user_competency::count_records());
67bc0eaf 114 $this->assertInstanceOf('\core_competency\user_competency', $rc);
6d57a987
SG
115 }
116
8a9030c1
SG
117 public function test_create_user_competency_plan() {
118 $this->resetAfterTest(true);
119
120 $user = $this->getDataGenerator()->create_user();
922634d3 121 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
8a9030c1
SG
122 $framework = $lpg->create_framework();
123 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
124 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
125 $plan = $lpg->create_plan(array('userid' => $user->id));
126 $this->assertEquals(0, user_competency_plan::count_records());
127 $ucp = $lpg->create_user_competency_plan(array(
128 'userid' => $user->id,
129 'competencyid' => $c1->get_id(),
130 'planid' => $plan->get_id()
131 ));
132 $ucp = $lpg->create_user_competency_plan(array(
133 'userid' => $user->id,
134 'competencyid' => $c2->get_id(),
135 'planid' => $plan->get_id()
136 ));
137 $this->assertEquals(2, user_competency_plan::count_records());
67bc0eaf 138 $this->assertInstanceOf('\core_competency\user_competency_plan', $ucp);
8a9030c1
SG
139 }
140
7f386a2e
FM
141 public function test_create_template() {
142 $this->resetAfterTest(true);
143
922634d3 144 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
7f386a2e
FM
145 $this->assertEquals(0, template::count_records());
146 $template = $lpg->create_template();
147 $template = $lpg->create_template();
148 $this->assertEquals(2, template::count_records());
67bc0eaf 149 $this->assertInstanceOf('\core_competency\template', $template);
7f386a2e
FM
150 }
151
152 public function test_create_template_competency() {
153 $this->resetAfterTest(true);
922634d3 154 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
7f386a2e
FM
155
156 $this->assertEquals(0, template_competency::count_records());
157 $framework = $lpg->create_framework();
158 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
159 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
160 $template = $lpg->create_template();
161 $relation = $lpg->create_template_competency(array('competencyid' => $c1->get_id(), 'templateid' => $template->get_id()));
162 $relation = $lpg->create_template_competency(array('competencyid' => $c2->get_id(), 'templateid' => $template->get_id()));
163 $this->assertEquals(2, template_competency::count_records());
67bc0eaf 164 $this->assertInstanceOf('\core_competency\template_competency', $relation);
7f386a2e
FM
165 }
166
4ad3ac34
IT
167 public function test_create_plan_competency() {
168 $this->resetAfterTest(true);
169 $user = $this->getDataGenerator()->create_user();
922634d3 170 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
4ad3ac34
IT
171
172 $framework = $lpg->create_framework();
173 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
174 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
175
176 $plan = $lpg->create_plan(array('userid' => $user->id));
177
178 $pc1 = $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c1->get_id()));
179 $pc2 = $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c2->get_id()));
180
181 $this->assertEquals(2, plan_competency::count_records());
67bc0eaf
FM
182 $this->assertInstanceOf('\core_competency\plan_competency', $pc1);
183 $this->assertInstanceOf('\core_competency\plan_competency', $pc2);
4ad3ac34
IT
184 $this->assertEquals($plan->get_id(), $pc1->get_planid());
185 }
186
bee480a4
FM
187 public function test_create_template_cohort() {
188 $this->resetAfterTest(true);
189
922634d3 190 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
bee480a4
FM
191 $c1 = $this->getDataGenerator()->create_cohort();
192 $c2 = $this->getDataGenerator()->create_cohort();
193 $t1 = $lpg->create_template();
194 $this->assertEquals(0, template_cohort::count_records());
195 $tc = $lpg->create_template_cohort(array('templateid' => $t1->get_id(), 'cohortid' => $c1->id));
196 $this->assertEquals(1, template_cohort::count_records());
197 $tc = $lpg->create_template_cohort(array('templateid' => $t1->get_id(), 'cohortid' => $c2->id));
198 $this->assertEquals(2, template_cohort::count_records());
67bc0eaf 199 $this->assertInstanceOf('\core_competency\template_cohort', $tc);
bee480a4
FM
200 }
201
4c0e8167
SG
202 public function test_create_evidence() {
203 $this->resetAfterTest(true);
204
205 $user = $this->getDataGenerator()->create_user();
922634d3 206 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
4c0e8167
SG
207 $framework = $lpg->create_framework();
208 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
209 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
210 $rc1 = $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c1->get_id()));
211 $rc2 = $lpg->create_user_competency(array('userid' => $user->id, 'competencyid' => $c2->get_id()));
212 $e = $lpg->create_evidence(array('usercompetencyid' => $rc1->get_id()));
213 $e = $lpg->create_evidence(array('usercompetencyid' => $rc2->get_id()));
214 $this->assertEquals(2, evidence::count_records());
67bc0eaf 215 $this->assertInstanceOf('\core_competency\evidence', $e);
4c0e8167
SG
216 }
217
40e5ad0e
FM
218 public function test_create_course_competency() {
219 $this->resetAfterTest(true);
220
922634d3 221 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
40e5ad0e
FM
222 $course1 = $this->getDataGenerator()->create_course();
223 $course2 = $this->getDataGenerator()->create_course();
224 $framework = $lpg->create_framework();
225 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
226 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
227 $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
228 $this->assertEquals(0, course_competency::count_records());
229 $rc = $lpg->create_course_competency(array('competencyid' => $c1->get_id(), 'courseid' => $course1->id));
230 $rc = $lpg->create_course_competency(array('competencyid' => $c2->get_id(), 'courseid' => $course1->id));
231 $this->assertEquals(2, course_competency::count_records(array('courseid' => $course1->id)));
232 $this->assertEquals(0, course_competency::count_records(array('courseid' => $course2->id)));
233 $rc = $lpg->create_course_competency(array('competencyid' => $c3->get_id(), 'courseid' => $course2->id));
234 $this->assertEquals(1, course_competency::count_records(array('courseid' => $course2->id)));
67bc0eaf 235 $this->assertInstanceOf('\core_competency\course_competency', $rc);
40e5ad0e
FM
236 }
237
c5e89aa1
FM
238 public function test_create_course_module_competency() {
239 $this->resetAfterTest(true);
240
922634d3 241 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
c5e89aa1
FM
242 $course1 = $this->getDataGenerator()->create_course();
243 $cm1 = $this->getDataGenerator()->create_module('forum', array('course' => $course1->id));
244 $cm2 = $this->getDataGenerator()->create_module('forum', array('course' => $course1->id));
245 $framework = $lpg->create_framework();
246 $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
247 $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
248 $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
249 $this->assertEquals(0, course_module_competency::count_records());
250 $rc = $lpg->create_course_module_competency(array('competencyid' => $c1->get_id(), 'cmid' => $cm1->cmid));
251 $rc = $lpg->create_course_module_competency(array('competencyid' => $c2->get_id(), 'cmid' => $cm1->cmid));
252 $this->assertEquals(2, course_module_competency::count_records(array('cmid' => $cm1->cmid)));
253 $this->assertEquals(0, course_module_competency::count_records(array('cmid' => $cm2->cmid)));
254 $rc = $lpg->create_course_module_competency(array('competencyid' => $c3->get_id(), 'cmid' => $cm2->cmid));
255 $this->assertEquals(1, course_module_competency::count_records(array('cmid' => $cm2->cmid)));
67bc0eaf 256 $this->assertInstanceOf('\core_competency\course_module_competency', $rc);
c5e89aa1
FM
257 }
258
72a1e0de
FM
259}
260