MDL-57015 tests: Use preventResetByRollback() if test uses transactions
[moodle.git] / admin / tool / lpmigrate / tests / processor_test.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * Framework processor tests.
19  *
20  * @package    tool_lpmigrate
21  * @copyright  2016 Frédéric Massart - FMCorz.net
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 defined('MOODLE_INTERNAL') || die();
26 global $CFG;
28 use core_competency\course_competency;
29 use core_competency\course_module_competency;
30 use tool_lpmigrate\framework_mapper;
31 use tool_lpmigrate\framework_processor;
33 /**
34  * Framework processor testcase.
35  *
36  * @package    tool_lpmigrate
37  * @copyright  2016 Frédéric Massart - FMCorz.net
38  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39  */
40 class tool_lpmigrate_framework_processor_testcase extends advanced_testcase {
42     /**
43      * This sets up a few things, and assign class variables.
44      *
45      * We create 2 frameworks, each with 2 matching competencies and 1 foreign.
46      * Then we create 2 courses, and in each 1 CM.
47      * Then we attach some competencies from the first framework to courses and CM.
48      */
49     public function setUp() {
50         $this->resetAfterTest(true);
51         $dg = $this->getDataGenerator();
52         $lpg = $dg->get_plugin_generator('core_competency');
54         $f1 = $lpg->create_framework(array('idnumber' => 'BIO2015'));
55         $f2 = $lpg->create_framework(array('idnumber' => 'BIO2016'));
57         $f1comps = array();
58         $f1comps['A1'] = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'idnumber' => 'A1'));
59         $f1comps['A2'] = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'idnumber' => 'A2'));
60         $f1comps['A3'] = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'idnumber' => 'A3'));
61         $f1comps['X1'] = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id(), 'idnumber' => 'X1'));
63         $f2comps = array();
64         $f2comps['A1'] = $lpg->create_competency(array('competencyframeworkid' => $f2->get_id(), 'idnumber' => 'A1'));
65         $f2comps['A2'] = $lpg->create_competency(array('competencyframeworkid' => $f2->get_id(), 'idnumber' => 'A2'));
66         $f2comps['A3'] = $lpg->create_competency(array('competencyframeworkid' => $f2->get_id(), 'idnumber' => 'A3'));
67         $f2comps['Y1'] = $lpg->create_competency(array('competencyframeworkid' => $f2->get_id(), 'idnumber' => 'Y1'));
69         $c1 = $dg->create_course(array('startdate' => time() - 72000));
70         $c2 = $dg->create_course(array('startdate' => time() + 72000));
71         $cms = array(
72             $c1->id => array(
73                 'F1' => $dg->create_module('forum', (object) array('course' => $c1->id)),
74                 'P1' => $dg->create_module('page', (object) array('course' => $c1->id)),
75                 'EmptyA' => $dg->create_module('page', (object) array('course' => $c1->id)),
76             ),
77             $c2->id => array(
78                 'F1' => $dg->create_module('forum', (object) array('course' => $c2->id)),
79                 'EmptyB' => $dg->create_module('page', (object) array('course' => $c2->id)),
80             ),
81         );
83         // Course CompetencieS.
84         $ccs = array(
85             $c1->id => array(
86                 $f1comps['A1']->get_id() => $lpg->create_course_competency(array('courseid' => $c1->id,
87                     'competencyid' => $f1comps['A1']->get_id())),
88                 $f1comps['A3']->get_id() => $lpg->create_course_competency(array('courseid' => $c1->id,
89                     'competencyid' => $f1comps['A3']->get_id())),
90                 $f1comps['X1']->get_id() => $lpg->create_course_competency(array('courseid' => $c1->id,
91                     'competencyid' => $f1comps['X1']->get_id())),
92             ),
93             $c2->id => array(
94                 $f1comps['A2']->get_id() => $lpg->create_course_competency(array('courseid' => $c2->id,
95                     'competencyid' => $f1comps['A2']->get_id())),
96                 $f1comps['A3']->get_id() => $lpg->create_course_competency(array('courseid' => $c2->id,
97                     'competencyid' => $f1comps['A3']->get_id())),
98             )
99         );
101         // Course Module CompetencieS.
102         $cmcs = array(
103             $cms[$c1->id]['F1']->cmid => array(
104                 $f1comps['A1']->get_id() => $lpg->create_course_module_competency(array(
105                     'cmid' => $cms[$c1->id]['F1']->cmid,
106                     'competencyid' => $f1comps['A1']->get_id()
107                 )),
108                 $f1comps['X1']->get_id() => $lpg->create_course_module_competency(array(
109                     'cmid' => $cms[$c1->id]['F1']->cmid,
110                     'competencyid' => $f1comps['X1']->get_id()
111                 )),
112             ),
113             $cms[$c1->id]['P1']->cmid => array(
114                 $f1comps['A3']->get_id() => $lpg->create_course_module_competency(array(
115                     'cmid' => $cms[$c1->id]['P1']->cmid,
116                     'competencyid' => $f1comps['A3']->get_id()
117                 )),
118             ),
119             $cms[$c2->id]['F1']->cmid => array(
120                 $f1comps['A2']->get_id() => $lpg->create_course_module_competency(array(
121                     'cmid' => $cms[$c2->id]['F1']->cmid,
122                     'competencyid' => $f1comps['A2']->get_id()
123                 )),
124                 $f1comps['A3']->get_id() => $lpg->create_course_module_competency(array(
125                     'cmid' => $cms[$c2->id]['F1']->cmid,
126                     'competencyid' => $f1comps['A3']->get_id()
127                 )),
128             ),
129         );
131         $this->assertCourseCompetencyExists($c1, $f1comps['A1']);
132         $this->assertCourseCompetencyExists($c1, $f1comps['A3']);
133         $this->assertCourseCompetencyExists($c1, $f1comps['X1']);
134         $this->assertCourseCompetencyExists($c2, $f1comps['A2']);
135         $this->assertCourseCompetencyExists($c2, $f1comps['A3']);
136         $this->assertModuleCompetencyExists($cms[$c1->id]['F1'], $f1comps['A1']);
137         $this->assertModuleCompetencyExists($cms[$c1->id]['P1'], $f1comps['A3']);
138         $this->assertModuleCompetencyExists($cms[$c1->id]['F1'], $f1comps['X1']);
139         $this->assertModuleCompetencyExists($cms[$c2->id]['F1'], $f1comps['A2']);
140         $this->assertModuleCompetencyExists($cms[$c2->id]['F1'], $f1comps['A3']);
142         $this->f1 = $f1;
143         $this->f1comps = $f1comps;
144         $this->f2 = $f2;
145         $this->f2comps = $f2comps;
146         $this->c1 = $c1;
147         $this->c2 = $c2;
148         $this->cms = $cms;
149         $this->ccs = $ccs;
150         $this->cmcs = $cmcs;
151     }
153     public function test_simple_migration() {
154         $this->setAdminUser();
156         $mapper = new framework_mapper($this->f1->get_id(), $this->f2->get_id());
157         $mapper->automap();
158         $processor = new framework_processor($mapper);
159         $processor->proceed();
161         $this->assertEquals(2, $processor->get_courses_found_count());
162         $this->assertEquals(5, $processor->get_expected_course_competency_migrations());
163         $this->assertEquals(4, $processor->get_course_competency_migrations());
164         $this->assertEquals(4, $processor->get_course_competency_removals());
166         $this->assertEquals(3, $processor->get_cms_found_count());
167         $this->assertEquals(5, $processor->get_expected_module_competency_migrations());
168         $this->assertEquals(4, $processor->get_module_competency_migrations());
169         $this->assertEquals(4, $processor->get_module_competency_removals());
171         $this->assertEquals(array(), $processor->get_warnings());
172         $this->assertEquals(array(), $processor->get_errors());
173         $this->assertEquals(array($this->f1comps['X1']->get_id() => true), $processor->get_missing_mappings());
175         $this->assertCourseCompetencyMigrated($this->c1, $this->f1comps['A1'], $this->f2comps['A1']);
176         $this->assertCourseCompetencyMigrated($this->c1, $this->f1comps['A3'], $this->f2comps['A3']);
177         $this->assertCourseCompetencyMigrated($this->c2, $this->f1comps['A2'], $this->f2comps['A2']);
178         $this->assertCourseCompetencyMigrated($this->c2, $this->f1comps['A3'], $this->f2comps['A3']);
180         $this->assertModuleCompetencyMigrated($this->cms[$this->c1->id]['F1'], $this->f1comps['A1'], $this->f2comps['A1']);
181         $this->assertModuleCompetencyMigrated($this->cms[$this->c1->id]['P1'], $this->f1comps['A3'], $this->f2comps['A3']);
182         $this->assertModuleCompetencyMigrated($this->cms[$this->c2->id]['F1'], $this->f1comps['A2'], $this->f2comps['A2']);
183         $this->assertModuleCompetencyMigrated($this->cms[$this->c2->id]['F1'], $this->f1comps['A3'], $this->f2comps['A3']);
185         $this->assertCourseCompetencyExists($this->c1, $this->f1comps['X1']);
186         $this->assertModuleCompetencyExists($this->cms[$this->c1->id]['F1'], $this->f1comps['X1']);
187     }
189     public function test_remove_when_missing() {
190         $this->setAdminUser();
192         $mapper = new framework_mapper($this->f1->get_id(), $this->f2->get_id());
193         $mapper->automap();
194         $processor = new framework_processor($mapper);
195         $processor->set_remove_when_mapping_is_missing(true);
196         $processor->proceed();
198         $this->assertEquals(2, $processor->get_courses_found_count());
199         $this->assertEquals(5, $processor->get_expected_course_competency_migrations());
200         $this->assertEquals(4, $processor->get_course_competency_migrations());
201         $this->assertEquals(5, $processor->get_course_competency_removals());
203         $this->assertEquals(3, $processor->get_cms_found_count());
204         $this->assertEquals(5, $processor->get_expected_module_competency_migrations());
205         $this->assertEquals(4, $processor->get_module_competency_migrations());
206         $this->assertEquals(5, $processor->get_module_competency_removals());
208         $this->assertCount(0, $processor->get_errors());
209         $this->assertCount(0, $processor->get_warnings());
211         $this->assertCourseCompetencyNotExists($this->c1, $this->f1comps['X1']);
212         $this->assertModuleCompetencyNotExists($this->cms[$this->c1->id]['F1'], $this->f1comps['X1']);
213     }
215     public function test_allowed_courses() {
216         $this->setAdminUser();
218         $mapper = new framework_mapper($this->f1->get_id(), $this->f2->get_id());
219         $mapper->automap();
220         $processor = new framework_processor($mapper);
221         $processor->set_allowedcourses(array($this->c1->id));
222         $processor->proceed();
224         $this->assertEquals(1, $processor->get_courses_found_count());
225         $this->assertEquals(3, $processor->get_expected_course_competency_migrations());
226         $this->assertEquals(2, $processor->get_course_competency_migrations());
227         $this->assertEquals(2, $processor->get_course_competency_removals());
229         $this->assertEquals(2, $processor->get_cms_found_count());
230         $this->assertEquals(3, $processor->get_expected_module_competency_migrations());
231         $this->assertEquals(2, $processor->get_module_competency_migrations());
232         $this->assertEquals(2, $processor->get_module_competency_removals());
234         $this->assertCount(0, $processor->get_errors());
235         $this->assertCount(0, $processor->get_warnings());
237         $this->assertCourseCompetencyMigrated($this->c1, $this->f1comps['A1'], $this->f2comps['A1']);
238         $this->assertCourseCompetencyMigrated($this->c1, $this->f1comps['A3'], $this->f2comps['A3']);
239         $this->assertModuleCompetencyMigrated($this->cms[$this->c1->id]['F1'], $this->f1comps['A1'], $this->f2comps['A1']);
240         $this->assertModuleCompetencyMigrated($this->cms[$this->c1->id]['P1'], $this->f1comps['A3'], $this->f2comps['A3']);
242         $this->assertCourseCompetencyNotMigrated($this->c2, $this->f1comps['A2'], $this->f2comps['A2']);
243         $this->assertCourseCompetencyNotMigrated($this->c2, $this->f1comps['A3'], $this->f2comps['A3']);
244         $this->assertModuleCompetencyNotMigrated($this->cms[$this->c2->id]['F1'], $this->f1comps['A2'], $this->f2comps['A2']);
245         $this->assertModuleCompetencyNotMigrated($this->cms[$this->c2->id]['F1'], $this->f1comps['A3'], $this->f2comps['A3']);
246     }
248     public function test_disallowed_courses() {
249         $this->setAdminUser();
251         $mapper = new framework_mapper($this->f1->get_id(), $this->f2->get_id());
252         $mapper->automap();
253         $processor = new framework_processor($mapper);
254         $processor->set_disallowedcourses(array($this->c2->id));
255         $processor->proceed();
257         $this->assertEquals(1, $processor->get_courses_found_count());
258         $this->assertEquals(3, $processor->get_expected_course_competency_migrations());
259         $this->assertEquals(2, $processor->get_course_competency_migrations());
260         $this->assertEquals(2, $processor->get_course_competency_removals());
262         $this->assertEquals(2, $processor->get_cms_found_count());
263         $this->assertEquals(3, $processor->get_expected_module_competency_migrations());
264         $this->assertEquals(2, $processor->get_module_competency_migrations());
265         $this->assertEquals(2, $processor->get_module_competency_removals());
267         $this->assertCount(0, $processor->get_errors());
268         $this->assertCount(0, $processor->get_warnings());
270         $this->assertCourseCompetencyMigrated($this->c1, $this->f1comps['A1'], $this->f2comps['A1']);
271         $this->assertCourseCompetencyMigrated($this->c1, $this->f1comps['A3'], $this->f2comps['A3']);
272         $this->assertModuleCompetencyMigrated($this->cms[$this->c1->id]['F1'], $this->f1comps['A1'], $this->f2comps['A1']);
273         $this->assertModuleCompetencyMigrated($this->cms[$this->c1->id]['P1'], $this->f1comps['A3'], $this->f2comps['A3']);
275         $this->assertCourseCompetencyNotMigrated($this->c2, $this->f1comps['A2'], $this->f2comps['A2']);
276         $this->assertCourseCompetencyNotMigrated($this->c2, $this->f1comps['A3'], $this->f2comps['A3']);
277         $this->assertModuleCompetencyNotMigrated($this->cms[$this->c2->id]['F1'], $this->f1comps['A2'], $this->f2comps['A2']);
278         $this->assertModuleCompetencyNotMigrated($this->cms[$this->c2->id]['F1'], $this->f1comps['A3'], $this->f2comps['A3']);
279     }
281     public function test_course_start_date_from() {
282         $this->setAdminUser();
284         $mapper = new framework_mapper($this->f1->get_id(), $this->f2->get_id());
285         $mapper->automap();
286         $processor = new framework_processor($mapper);
287         $processor->set_course_start_date_from(time());
288         $processor->proceed();
290         $this->assertEquals(1, $processor->get_courses_found_count());
291         $this->assertEquals(2, $processor->get_expected_course_competency_migrations());
292         $this->assertEquals(2, $processor->get_course_competency_migrations());
293         $this->assertEquals(2, $processor->get_course_competency_removals());
295         $this->assertEquals(1, $processor->get_cms_found_count());
296         $this->assertEquals(2, $processor->get_expected_module_competency_migrations());
297         $this->assertEquals(2, $processor->get_module_competency_migrations());
298         $this->assertEquals(2, $processor->get_module_competency_removals());
300         $this->assertCount(0, $processor->get_errors());
301         $this->assertCount(0, $processor->get_warnings());
303         $this->assertCourseCompetencyNotMigrated($this->c1, $this->f1comps['A1'], $this->f2comps['A1']);
304         $this->assertCourseCompetencyNotMigrated($this->c1, $this->f1comps['A3'], $this->f2comps['A3']);
305         $this->assertModuleCompetencyNotMigrated($this->cms[$this->c1->id]['F1'], $this->f1comps['A1'], $this->f2comps['A1']);
306         $this->assertModuleCompetencyNotMigrated($this->cms[$this->c1->id]['P1'], $this->f1comps['A3'], $this->f2comps['A3']);
308         $this->assertCourseCompetencyMigrated($this->c2, $this->f1comps['A2'], $this->f2comps['A2']);
309         $this->assertCourseCompetencyMigrated($this->c2, $this->f1comps['A3'], $this->f2comps['A3']);
310         $this->assertModuleCompetencyMigrated($this->cms[$this->c2->id]['F1'], $this->f1comps['A2'], $this->f2comps['A2']);
311         $this->assertModuleCompetencyMigrated($this->cms[$this->c2->id]['F1'], $this->f1comps['A3'], $this->f2comps['A3']);
312     }
314     public function test_destination_competency_exists() {
315         $this->setAdminUser();
316         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
318         // Pre-add the new competency to course 1.
319         $lpg->create_course_competency(array('courseid' => $this->c1->id, 'competencyid' => $this->f2comps['A1']->get_id()));
321         // Pre-add the new competency to module in course 2.
322         $lpg->create_course_module_competency(array(
323             'cmid' => $this->cms[$this->c2->id]['F1']->cmid,
324             'competencyid' => $this->f2comps['A2']->get_id()
325         ));
327         $mapper = new framework_mapper($this->f1->get_id(), $this->f2->get_id());
328         $mapper->automap();
329         $processor = new framework_processor($mapper);
330         $processor->proceed();
332         $this->assertEquals(2, $processor->get_courses_found_count());
333         $this->assertEquals(5, $processor->get_expected_course_competency_migrations());
334         $this->assertEquals(3, $processor->get_course_competency_migrations());
335         $this->assertEquals(2, $processor->get_course_competency_removals());
337         $this->assertEquals(3, $processor->get_cms_found_count());
338         $this->assertEquals(5, $processor->get_expected_module_competency_migrations());
339         $this->assertEquals(3, $processor->get_module_competency_migrations());
340         $this->assertEquals(3, $processor->get_module_competency_removals());
342         $this->assertEquals(array(), $processor->get_errors());
343         $warnings = $processor->get_warnings();
344         $this->assertCount(2, $warnings);
346         $warning = array_shift($warnings);
347         $this->assertEquals($this->c1->id, $warning['courseid']);
348         $this->assertEquals($this->f1comps['A1']->get_id(), $warning['competencyid']);
349         $this->assertEquals(null, $warning['cmid']);
350         $this->assertRegexp('/competency already exists/', $warning['message']);
352         $warning = array_shift($warnings);
353         $this->assertEquals($this->c2->id, $warning['courseid']);
354         $this->assertEquals($this->f1comps['A2']->get_id(), $warning['competencyid']);
355         $this->assertEquals($this->cms[$this->c2->id]['F1']->cmid, $warning['cmid']);
356         $this->assertRegexp('/competency already exists/', $warning['message']);
358         $this->assertCourseCompetencyExists($this->c1, $this->f1comps['A1']);
359         $this->assertModuleCompetencyExists($this->cms[$this->c2->id]['F1'], $this->f1comps['A2']);
360     }
362     public function test_destination_competency_exists_remove_original() {
363         $this->setAdminUser();
364         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
366         // Pre-add the new competency to course 1.
367         $lpg->create_course_competency(array('courseid' => $this->c1->id, 'competencyid' => $this->f2comps['A1']->get_id()));
369         // Pre-add the new competency to module in course 2.
370         $lpg->create_course_module_competency(array(
371             'cmid' => $this->cms[$this->c2->id]['F1']->cmid,
372             'competencyid' => $this->f2comps['A2']->get_id()
373         ));
375         $mapper = new framework_mapper($this->f1->get_id(), $this->f2->get_id());
376         $mapper->automap();
377         $processor = new framework_processor($mapper);
378         $processor->set_remove_original_when_destination_already_present(true);
379         $processor->proceed();
381         $this->assertEquals(2, $processor->get_courses_found_count());
382         $this->assertEquals(5, $processor->get_expected_course_competency_migrations());
383         $this->assertEquals(3, $processor->get_course_competency_migrations());
384         $this->assertEquals(4, $processor->get_course_competency_removals());
386         $this->assertEquals(3, $processor->get_cms_found_count());
387         $this->assertEquals(5, $processor->get_expected_module_competency_migrations());
388         $this->assertEquals(3, $processor->get_module_competency_migrations());
389         $this->assertEquals(4, $processor->get_module_competency_removals());
391         $this->assertEquals(array(), $processor->get_errors());
392         $this->assertEquals(array(), $processor->get_warnings());
394         $this->assertCourseCompetencyNotExists($this->c1, $this->f1comps['A1']);
395         $this->assertModuleCompetencyNotExists($this->cms[$this->c2->id]['F1'], $this->f1comps['A2']);
396     }
398     public function test_permission_exception() {
400         $this->preventResetByRollback(); // Test uses transactions, so we cannot use them for speedy reset.
402         $dg = $this->getDataGenerator();
403         $u = $dg->create_user();
404         $role = $dg->create_role();
405         $sysctx = context_system::instance();
407         $dg->enrol_user($u->id, $this->c1->id, 'editingteacher');
408         $dg->enrol_user($u->id, $this->c2->id, 'editingteacher');
409         assign_capability('moodle/competency:coursecompetencymanage', CAP_PROHIBIT, $role, $sysctx->id);
410         role_assign($role, $u->id, context_course::instance($this->c1->id)->id);
411         role_assign($role, $u->id, context_module::instance($this->cms[$this->c2->id]['F1']->cmid)->id);
413         accesslib_clear_all_caches_for_unit_testing();
414         $this->setUser($u);
416         // Do C1 first.
417         $mapper = new framework_mapper($this->f1->get_id(), $this->f2->get_id());
418         $mapper->automap();
419         $processor = new framework_processor($mapper);
420         $processor->set_allowedcourses(array($this->c1->id));
421         $processor->proceed();
423         $this->assertEquals(1, $processor->get_courses_found_count());
424         $this->assertEquals(3, $processor->get_expected_course_competency_migrations());
425         $this->assertEquals(0, $processor->get_course_competency_migrations());
426         $this->assertEquals(0, $processor->get_course_competency_removals());
428         $this->assertEquals(2, $processor->get_cms_found_count());
429         $this->assertEquals(3, $processor->get_expected_module_competency_migrations());
430         $this->assertEquals(0, $processor->get_module_competency_migrations());
431         $this->assertEquals(0, $processor->get_module_competency_removals());
433         $this->assertEquals(array(), $processor->get_warnings());
434         $errors = $processor->get_errors();
435         $this->assertCount(2, $errors);
436         $this->assertEquals($this->c1->id, $errors[0]['courseid']);
437         $this->assertEquals($this->f1comps['A1']->get_id(), $errors[0]['competencyid']);
438         $this->assertEquals(null, $errors[0]['cmid']);
439         $this->assertRegexp('/Sorry, but you do not currently have permissions to do that/', $errors[0]['message']);
440         $this->assertEquals($this->f1comps['A3']->get_id(), $errors[1]['competencyid']);
442         $this->assertCourseCompetencyNotMigrated($this->c1, $this->f1comps['A1'], $this->f2comps['A1']);
443         $this->assertCourseCompetencyNotMigrated($this->c1, $this->f1comps['A3'], $this->f2comps['A3']);
444         $this->assertModuleCompetencyNotMigrated($this->cms[$this->c1->id]['F1'], $this->f1comps['A1'], $this->f2comps['A1']);
445         $this->assertModuleCompetencyNotMigrated($this->cms[$this->c1->id]['P1'], $this->f1comps['A3'], $this->f2comps['A3']);
447         // Do C2 now.
448         $processor = new framework_processor($mapper);
449         $processor->set_allowedcourses(array($this->c2->id));
450         $processor->proceed();
452         $this->assertEquals(1, $processor->get_courses_found_count());
453         $this->assertEquals(2, $processor->get_expected_course_competency_migrations());
454         $this->assertEquals(2, $processor->get_course_competency_migrations());
455         $this->assertEquals(0, $processor->get_course_competency_removals());
457         $this->assertEquals(1, $processor->get_cms_found_count());
458         $this->assertEquals(2, $processor->get_expected_module_competency_migrations());
459         $this->assertEquals(0, $processor->get_module_competency_migrations());
460         $this->assertEquals(0, $processor->get_module_competency_removals());
462         $this->assertEquals(array(), $processor->get_warnings());
463         $errors = $processor->get_errors();
464         $this->assertCount(2, $errors);
465         $this->assertEquals($this->c2->id, $errors[0]['courseid']);
466         $this->assertEquals($this->f1comps['A2']->get_id(), $errors[0]['competencyid']);
467         $this->assertEquals($this->cms[$this->c2->id]['F1']->cmid, $errors[0]['cmid']);
468         $this->assertRegexp('/Sorry, but you do not currently have permissions to do that/', $errors[0]['message']);
469         $this->assertEquals($this->f1comps['A3']->get_id(), $errors[1]['competencyid']);
471         // The new competencies were added to the course, but the old ones were not removed because they are still in modules.
472         $this->assertCourseCompetencyExists($this->c2, $this->f1comps['A2']);
473         $this->assertCourseCompetencyExists($this->c2, $this->f1comps['A3']);
474         $this->assertCourseCompetencyExists($this->c2, $this->f2comps['A2']);
475         $this->assertCourseCompetencyExists($this->c2, $this->f2comps['A3']);
477         // Module competencies were not migrated because permissions are lacking.
478         $this->assertModuleCompetencyNotMigrated($this->cms[$this->c2->id]['F1'], $this->f1comps['A2'], $this->f2comps['A2']);
479         $this->assertModuleCompetencyNotMigrated($this->cms[$this->c2->id]['F1'], $this->f1comps['A3'], $this->f2comps['A2']);
480     }
482     /**
483      * Assert that the course competency exists.
484      *
485      * @param stdClass $course The course.
486      * @param competency $competency The competency.
487      */
488     protected function assertCourseCompetencyExists($course, $competency) {
489         $this->assertTrue(course_competency::record_exists_select("courseid = :courseid AND competencyid = :competencyid",
490             array('courseid' => $course->id, 'competencyid' => $competency->get_id())));
491     }
493     /**
494      * Assert that the course competency does not exist.
495      *
496      * @param stdClass $course The course.
497      * @param competency $competency The competency.
498      */
499     protected function assertCourseCompetencyNotExists($course, $competency) {
500         $this->assertFalse(course_competency::record_exists_select("courseid = :courseid AND competencyid = :competencyid",
501             array('courseid' => $course->id, 'competencyid' => $competency->get_id())));
502     }
504     /**
505      * Assert that the course competency was migrated.
506      *
507      * @param stdClass $course The course.
508      * @param competency $compfrom The competency from.
509      * @param competency $compto The competency to.
510      */
511     protected function assertCourseCompetencyMigrated($course, $compfrom, $compto) {
512         $ccs = $this->ccs[$course->id];
514         $this->assertCourseCompetencyNotExists($course, $compfrom);
515         $this->assertCourseCompetencyExists($course, $compto);
517         $before = $ccs[$compfrom->get_id()];
518         $after = course_competency::get_record(array(
519             'courseid' => $course->id,
520             'competencyid' => $compto->get_id()
521         ));
523         $this->assertNotEquals($before->get_id(), $after->get_id());
524         $this->assertEquals($before->get_courseid(), $after->get_courseid());
525         $this->assertEquals($before->get_sortorder(), $after->get_sortorder());
526         $this->assertEquals($before->get_ruleoutcome(), $after->get_ruleoutcome());
527     }
529     /**
530      * Assert that the course competency was not migrated.
531      *
532      * @param stdClass $course The course.
533      * @param competency $compfrom The competency from.
534      * @param competency $compto The competency to.
535      */
536     protected function assertCourseCompetencyNotMigrated($course, $compfrom, $compto) {
537         $ccs = $this->ccs[$course->id];
539         $this->assertCourseCompetencyExists($course, $compfrom);
540         $this->assertCourseCompetencyNotExists($course, $compto);
542         $before = $ccs[$compfrom->get_id()];
543         $after = $ccs[$compfrom->get_id()];
545         $this->assertEquals($before->get_id(), $after->get_id());
546         $this->assertEquals($before->get_courseid(), $after->get_courseid());
547         $this->assertEquals($before->get_sortorder(), $after->get_sortorder());
548         $this->assertEquals($before->get_ruleoutcome(), $after->get_ruleoutcome());
549     }
551     /**
552      * Assert that the course module competency exists.
553      *
554      * @param stdClass $cm The CM.
555      * @param competency $competency The competency.
556      */
557     protected function assertModuleCompetencyExists($cm, $competency) {
558         $this->assertTrue(course_module_competency::record_exists_select("cmid = :cmid AND competencyid = :competencyid",
559             array('cmid' => $cm->cmid, 'competencyid' => $competency->get_id())));
560     }
562     /**
563      * Assert that the course module competency does not exist.
564      *
565      * @param stdClass $cm The CM.
566      * @param competency $competency The competency.
567      */
568     protected function assertModuleCompetencyNotExists($cm, $competency) {
569         $this->assertFalse(course_module_competency::record_exists_select("cmid = :cmid AND competencyid = :competencyid",
570             array('cmid' => $cm->cmid, 'competencyid' => $competency->get_id())));
571     }
573     /**
574      * Assert that the course module competency was migrated.
575      *
576      * @param stdClass $cm The CM.
577      * @param competency $compfrom The competency from.
578      * @param competency $compto The competency to.
579      */
580     protected function assertModuleCompetencyMigrated($cm, $compfrom, $compto) {
581         $cmcs = $this->cmcs[$cm->cmid];
583         $this->assertModuleCompetencyNotExists($cm, $compfrom);
584         $this->assertModuleCompetencyExists($cm, $compto);
586         $before = $cmcs[$compfrom->get_id()];
587         $after = course_module_competency::get_record(array(
588             'cmid' => $cm->cmid,
589             'competencyid' => $compto->get_id()
590         ));
592         $this->assertNotEquals($before->get_id(), $after->get_id());
593         $this->assertEquals($before->get_cmid(), $after->get_cmid());
594         $this->assertEquals($before->get_sortorder(), $after->get_sortorder());
595         $this->assertEquals($before->get_ruleoutcome(), $after->get_ruleoutcome());
596     }
598     /**
599      * Assert that the course module competency was not migrated.
600      *
601      * @param stdClass $cm The CM.
602      * @param competency $compfrom The competency from.
603      * @param competency $compto The competency to.
604      */
605     protected function assertModuleCompetencyNotMigrated($cm, $compfrom, $compto) {
606         $cmcs = $this->cmcs[$cm->cmid];
608         $this->assertModuleCompetencyExists($cm, $compfrom);
609         $this->assertModuleCompetencyNotExists($cm, $compto);
611         $before = $cmcs[$compfrom->get_id()];
612         $after = $cmcs[$compfrom->get_id()];
614         $this->assertEquals($before->get_id(), $after->get_id());
615         $this->assertEquals($before->get_cmid(), $after->get_cmid());
616         $this->assertEquals($before->get_sortorder(), $after->get_sortorder());
617         $this->assertEquals($before->get_ruleoutcome(), $after->get_ruleoutcome());
618     }