Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
namespace tool_lpmigrate;
18
 
19
use core_competency\course_competency;
20
use core_competency\course_module_competency;
21
 
22
/**
23
 * Framework processor testcase.
24
 *
25
 * @package    tool_lpmigrate
26
 * @copyright  2016 Frédéric Massart - FMCorz.net
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
1441 ariadna 29
final class processor_test extends \advanced_testcase {
1 efrain 30
 
31
    /** @var \core_competency\competency_framework|null $f1  */
32
    protected $f1 = null;
33
 
34
    /** @var \core_competency\competency_framework|null $f2 */
35
    protected $f2 = null;
36
 
37
    /** @var object|null $c1 course instance. */
38
    protected ?object $c1 = null;
39
 
40
    /** @var object|null $c2 course instance. */
41
    protected ?object $c2 = null;
42
 
43
    /** @var array $f1comps */
44
    protected array $f1comps = [];
45
 
46
    /** @var array $f2comps */
47
    protected array $f2comps = [];
48
 
49
    /** @var array $cms */
50
    protected array $cms = [];
51
 
52
    /** @var array $ccs */
53
    protected array $ccs = [];
54
 
55
    /** @var array $cmcs */
56
    protected array $cmcs = [];
57
 
58
    /**
59
     * This sets up a few things, and assign class variables.
60
     *
61
     * We create 2 frameworks, each with 2 matching competencies and 1 foreign.
62
     * Then we create 2 courses, and in each 1 CM.
63
     * Then we attach some competencies from the first framework to courses and CM.
64
     */
65
    public function setUp(): void {
1441 ariadna 66
        parent::setUp();
1 efrain 67
        $this->resetAfterTest(true);
68
        $dg = $this->getDataGenerator();
69
        $lpg = $dg->get_plugin_generator('core_competency');
70
 
71
        $f1 = $lpg->create_framework(array('idnumber' => 'BIO2015'));
72
        $f2 = $lpg->create_framework(array('idnumber' => 'BIO2016'));
73
 
74
        $f1comps = array();
75
        $f1comps['A1'] = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id'), 'idnumber' => 'A1'));
76
        $f1comps['A2'] = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id'), 'idnumber' => 'A2'));
77
        $f1comps['A3'] = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id'), 'idnumber' => 'A3'));
78
        $f1comps['X1'] = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id'), 'idnumber' => 'X1'));
79
 
80
        $f2comps = array();
81
        $f2comps['A1'] = $lpg->create_competency(array('competencyframeworkid' => $f2->get('id'), 'idnumber' => 'A1'));
82
        $f2comps['A2'] = $lpg->create_competency(array('competencyframeworkid' => $f2->get('id'), 'idnumber' => 'A2'));
83
        $f2comps['A3'] = $lpg->create_competency(array('competencyframeworkid' => $f2->get('id'), 'idnumber' => 'A3'));
84
        $f2comps['Y1'] = $lpg->create_competency(array('competencyframeworkid' => $f2->get('id'), 'idnumber' => 'Y1'));
85
 
86
        $c1 = $dg->create_course(array('startdate' => time() - 72000));
87
        $c2 = $dg->create_course(array('startdate' => time() + 72000));
88
        $cms = array(
89
            $c1->id => array(
90
                'F1' => $dg->create_module('forum', (object) array('course' => $c1->id)),
91
                'P1' => $dg->create_module('page', (object) array('course' => $c1->id)),
92
                'EmptyA' => $dg->create_module('page', (object) array('course' => $c1->id)),
93
            ),
94
            $c2->id => array(
95
                'F1' => $dg->create_module('forum', (object) array('course' => $c2->id)),
96
                'EmptyB' => $dg->create_module('page', (object) array('course' => $c2->id)),
97
            ),
98
        );
99
 
100
        // Course CompetencieS.
101
        $ccs = array(
102
            $c1->id => array(
103
                $f1comps['A1']->get('id') => $lpg->create_course_competency(array('courseid' => $c1->id,
104
                    'competencyid' => $f1comps['A1']->get('id'))),
105
                $f1comps['A3']->get('id') => $lpg->create_course_competency(array('courseid' => $c1->id,
106
                    'competencyid' => $f1comps['A3']->get('id'))),
107
                $f1comps['X1']->get('id') => $lpg->create_course_competency(array('courseid' => $c1->id,
108
                    'competencyid' => $f1comps['X1']->get('id'))),
109
            ),
110
            $c2->id => array(
111
                $f1comps['A2']->get('id') => $lpg->create_course_competency(array('courseid' => $c2->id,
112
                    'competencyid' => $f1comps['A2']->get('id'))),
113
                $f1comps['A3']->get('id') => $lpg->create_course_competency(array('courseid' => $c2->id,
114
                    'competencyid' => $f1comps['A3']->get('id'))),
115
            )
116
        );
117
 
118
        // Course Module CompetencieS.
119
        $cmcs = array(
120
            $cms[$c1->id]['F1']->cmid => array(
121
                $f1comps['A1']->get('id') => $lpg->create_course_module_competency(array(
122
                    'cmid' => $cms[$c1->id]['F1']->cmid,
123
                    'competencyid' => $f1comps['A1']->get('id')
124
                )),
125
                $f1comps['X1']->get('id') => $lpg->create_course_module_competency(array(
126
                    'cmid' => $cms[$c1->id]['F1']->cmid,
127
                    'competencyid' => $f1comps['X1']->get('id')
128
                )),
129
            ),
130
            $cms[$c1->id]['P1']->cmid => array(
131
                $f1comps['A3']->get('id') => $lpg->create_course_module_competency(array(
132
                    'cmid' => $cms[$c1->id]['P1']->cmid,
133
                    'competencyid' => $f1comps['A3']->get('id')
134
                )),
135
            ),
136
            $cms[$c2->id]['F1']->cmid => array(
137
                $f1comps['A2']->get('id') => $lpg->create_course_module_competency(array(
138
                    'cmid' => $cms[$c2->id]['F1']->cmid,
139
                    'competencyid' => $f1comps['A2']->get('id')
140
                )),
141
                $f1comps['A3']->get('id') => $lpg->create_course_module_competency(array(
142
                    'cmid' => $cms[$c2->id]['F1']->cmid,
143
                    'competencyid' => $f1comps['A3']->get('id')
144
                )),
145
            ),
146
        );
147
 
148
        $this->assertCourseCompetencyExists($c1, $f1comps['A1']);
149
        $this->assertCourseCompetencyExists($c1, $f1comps['A3']);
150
        $this->assertCourseCompetencyExists($c1, $f1comps['X1']);
151
        $this->assertCourseCompetencyExists($c2, $f1comps['A2']);
152
        $this->assertCourseCompetencyExists($c2, $f1comps['A3']);
153
        $this->assertModuleCompetencyExists($cms[$c1->id]['F1'], $f1comps['A1']);
154
        $this->assertModuleCompetencyExists($cms[$c1->id]['P1'], $f1comps['A3']);
155
        $this->assertModuleCompetencyExists($cms[$c1->id]['F1'], $f1comps['X1']);
156
        $this->assertModuleCompetencyExists($cms[$c2->id]['F1'], $f1comps['A2']);
157
        $this->assertModuleCompetencyExists($cms[$c2->id]['F1'], $f1comps['A3']);
158
 
159
        $this->f1 = $f1;
160
        $this->f1comps = $f1comps;
161
        $this->f2 = $f2;
162
        $this->f2comps = $f2comps;
163
        $this->c1 = $c1;
164
        $this->c2 = $c2;
165
        $this->cms = $cms;
166
        $this->ccs = $ccs;
167
        $this->cmcs = $cmcs;
168
    }
169
 
11 efrain 170
    public function test_simple_migration(): void {
1 efrain 171
        $this->setAdminUser();
172
 
173
        $mapper = new framework_mapper($this->f1->get('id'), $this->f2->get('id'));
174
        $mapper->automap();
175
        $processor = new framework_processor($mapper);
176
        $processor->proceed();
177
 
178
        $this->assertEquals(2, $processor->get_courses_found_count());
179
        $this->assertEquals(5, $processor->get_expected_course_competency_migrations());
180
        $this->assertEquals(4, $processor->get_course_competency_migrations());
181
        $this->assertEquals(4, $processor->get_course_competency_removals());
182
 
183
        $this->assertEquals(3, $processor->get_cms_found_count());
184
        $this->assertEquals(5, $processor->get_expected_module_competency_migrations());
185
        $this->assertEquals(4, $processor->get_module_competency_migrations());
186
        $this->assertEquals(4, $processor->get_module_competency_removals());
187
 
188
        $this->assertEquals(array(), $processor->get_warnings());
189
        $this->assertEquals(array(), $processor->get_errors());
190
        $this->assertEquals(array($this->f1comps['X1']->get('id') => true), $processor->get_missing_mappings());
191
 
192
        $this->assertCourseCompetencyMigrated($this->c1, $this->f1comps['A1'], $this->f2comps['A1']);
193
        $this->assertCourseCompetencyMigrated($this->c1, $this->f1comps['A3'], $this->f2comps['A3']);
194
        $this->assertCourseCompetencyMigrated($this->c2, $this->f1comps['A2'], $this->f2comps['A2']);
195
        $this->assertCourseCompetencyMigrated($this->c2, $this->f1comps['A3'], $this->f2comps['A3']);
196
 
197
        $this->assertModuleCompetencyMigrated($this->cms[$this->c1->id]['F1'], $this->f1comps['A1'], $this->f2comps['A1']);
198
        $this->assertModuleCompetencyMigrated($this->cms[$this->c1->id]['P1'], $this->f1comps['A3'], $this->f2comps['A3']);
199
        $this->assertModuleCompetencyMigrated($this->cms[$this->c2->id]['F1'], $this->f1comps['A2'], $this->f2comps['A2']);
200
        $this->assertModuleCompetencyMigrated($this->cms[$this->c2->id]['F1'], $this->f1comps['A3'], $this->f2comps['A3']);
201
 
202
        $this->assertCourseCompetencyExists($this->c1, $this->f1comps['X1']);
203
        $this->assertModuleCompetencyExists($this->cms[$this->c1->id]['F1'], $this->f1comps['X1']);
204
    }
205
 
11 efrain 206
    public function test_remove_when_missing(): void {
1 efrain 207
        $this->setAdminUser();
208
 
209
        $mapper = new framework_mapper($this->f1->get('id'), $this->f2->get('id'));
210
        $mapper->automap();
211
        $processor = new framework_processor($mapper);
212
        $processor->set_remove_when_mapping_is_missing(true);
213
        $processor->proceed();
214
 
215
        $this->assertEquals(2, $processor->get_courses_found_count());
216
        $this->assertEquals(5, $processor->get_expected_course_competency_migrations());
217
        $this->assertEquals(4, $processor->get_course_competency_migrations());
218
        $this->assertEquals(5, $processor->get_course_competency_removals());
219
 
220
        $this->assertEquals(3, $processor->get_cms_found_count());
221
        $this->assertEquals(5, $processor->get_expected_module_competency_migrations());
222
        $this->assertEquals(4, $processor->get_module_competency_migrations());
223
        $this->assertEquals(5, $processor->get_module_competency_removals());
224
 
225
        $this->assertCount(0, $processor->get_errors());
226
        $this->assertCount(0, $processor->get_warnings());
227
 
228
        $this->assertCourseCompetencyNotExists($this->c1, $this->f1comps['X1']);
229
        $this->assertModuleCompetencyNotExists($this->cms[$this->c1->id]['F1'], $this->f1comps['X1']);
230
    }
231
 
11 efrain 232
    public function test_allowed_courses(): void {
1 efrain 233
        $this->setAdminUser();
234
 
235
        $mapper = new framework_mapper($this->f1->get('id'), $this->f2->get('id'));
236
        $mapper->automap();
237
        $processor = new framework_processor($mapper);
238
        $processor->set_allowedcourses(array($this->c1->id));
239
        $processor->proceed();
240
 
241
        $this->assertEquals(1, $processor->get_courses_found_count());
242
        $this->assertEquals(3, $processor->get_expected_course_competency_migrations());
243
        $this->assertEquals(2, $processor->get_course_competency_migrations());
244
        $this->assertEquals(2, $processor->get_course_competency_removals());
245
 
246
        $this->assertEquals(2, $processor->get_cms_found_count());
247
        $this->assertEquals(3, $processor->get_expected_module_competency_migrations());
248
        $this->assertEquals(2, $processor->get_module_competency_migrations());
249
        $this->assertEquals(2, $processor->get_module_competency_removals());
250
 
251
        $this->assertCount(0, $processor->get_errors());
252
        $this->assertCount(0, $processor->get_warnings());
253
 
254
        $this->assertCourseCompetencyMigrated($this->c1, $this->f1comps['A1'], $this->f2comps['A1']);
255
        $this->assertCourseCompetencyMigrated($this->c1, $this->f1comps['A3'], $this->f2comps['A3']);
256
        $this->assertModuleCompetencyMigrated($this->cms[$this->c1->id]['F1'], $this->f1comps['A1'], $this->f2comps['A1']);
257
        $this->assertModuleCompetencyMigrated($this->cms[$this->c1->id]['P1'], $this->f1comps['A3'], $this->f2comps['A3']);
258
 
259
        $this->assertCourseCompetencyNotMigrated($this->c2, $this->f1comps['A2'], $this->f2comps['A2']);
260
        $this->assertCourseCompetencyNotMigrated($this->c2, $this->f1comps['A3'], $this->f2comps['A3']);
261
        $this->assertModuleCompetencyNotMigrated($this->cms[$this->c2->id]['F1'], $this->f1comps['A2'], $this->f2comps['A2']);
262
        $this->assertModuleCompetencyNotMigrated($this->cms[$this->c2->id]['F1'], $this->f1comps['A3'], $this->f2comps['A3']);
263
    }
264
 
11 efrain 265
    public function test_disallowed_courses(): void {
1 efrain 266
        $this->setAdminUser();
267
 
268
        $mapper = new framework_mapper($this->f1->get('id'), $this->f2->get('id'));
269
        $mapper->automap();
270
        $processor = new framework_processor($mapper);
271
        $processor->set_disallowedcourses(array($this->c2->id));
272
        $processor->proceed();
273
 
274
        $this->assertEquals(1, $processor->get_courses_found_count());
275
        $this->assertEquals(3, $processor->get_expected_course_competency_migrations());
276
        $this->assertEquals(2, $processor->get_course_competency_migrations());
277
        $this->assertEquals(2, $processor->get_course_competency_removals());
278
 
279
        $this->assertEquals(2, $processor->get_cms_found_count());
280
        $this->assertEquals(3, $processor->get_expected_module_competency_migrations());
281
        $this->assertEquals(2, $processor->get_module_competency_migrations());
282
        $this->assertEquals(2, $processor->get_module_competency_removals());
283
 
284
        $this->assertCount(0, $processor->get_errors());
285
        $this->assertCount(0, $processor->get_warnings());
286
 
287
        $this->assertCourseCompetencyMigrated($this->c1, $this->f1comps['A1'], $this->f2comps['A1']);
288
        $this->assertCourseCompetencyMigrated($this->c1, $this->f1comps['A3'], $this->f2comps['A3']);
289
        $this->assertModuleCompetencyMigrated($this->cms[$this->c1->id]['F1'], $this->f1comps['A1'], $this->f2comps['A1']);
290
        $this->assertModuleCompetencyMigrated($this->cms[$this->c1->id]['P1'], $this->f1comps['A3'], $this->f2comps['A3']);
291
 
292
        $this->assertCourseCompetencyNotMigrated($this->c2, $this->f1comps['A2'], $this->f2comps['A2']);
293
        $this->assertCourseCompetencyNotMigrated($this->c2, $this->f1comps['A3'], $this->f2comps['A3']);
294
        $this->assertModuleCompetencyNotMigrated($this->cms[$this->c2->id]['F1'], $this->f1comps['A2'], $this->f2comps['A2']);
295
        $this->assertModuleCompetencyNotMigrated($this->cms[$this->c2->id]['F1'], $this->f1comps['A3'], $this->f2comps['A3']);
296
    }
297
 
11 efrain 298
    public function test_course_start_date_from(): void {
1 efrain 299
        $this->setAdminUser();
300
 
301
        $mapper = new framework_mapper($this->f1->get('id'), $this->f2->get('id'));
302
        $mapper->automap();
303
        $processor = new framework_processor($mapper);
304
        $processor->set_course_start_date_from(time());
305
        $processor->proceed();
306
 
307
        $this->assertEquals(1, $processor->get_courses_found_count());
308
        $this->assertEquals(2, $processor->get_expected_course_competency_migrations());
309
        $this->assertEquals(2, $processor->get_course_competency_migrations());
310
        $this->assertEquals(2, $processor->get_course_competency_removals());
311
 
312
        $this->assertEquals(1, $processor->get_cms_found_count());
313
        $this->assertEquals(2, $processor->get_expected_module_competency_migrations());
314
        $this->assertEquals(2, $processor->get_module_competency_migrations());
315
        $this->assertEquals(2, $processor->get_module_competency_removals());
316
 
317
        $this->assertCount(0, $processor->get_errors());
318
        $this->assertCount(0, $processor->get_warnings());
319
 
320
        $this->assertCourseCompetencyNotMigrated($this->c1, $this->f1comps['A1'], $this->f2comps['A1']);
321
        $this->assertCourseCompetencyNotMigrated($this->c1, $this->f1comps['A3'], $this->f2comps['A3']);
322
        $this->assertModuleCompetencyNotMigrated($this->cms[$this->c1->id]['F1'], $this->f1comps['A1'], $this->f2comps['A1']);
323
        $this->assertModuleCompetencyNotMigrated($this->cms[$this->c1->id]['P1'], $this->f1comps['A3'], $this->f2comps['A3']);
324
 
325
        $this->assertCourseCompetencyMigrated($this->c2, $this->f1comps['A2'], $this->f2comps['A2']);
326
        $this->assertCourseCompetencyMigrated($this->c2, $this->f1comps['A3'], $this->f2comps['A3']);
327
        $this->assertModuleCompetencyMigrated($this->cms[$this->c2->id]['F1'], $this->f1comps['A2'], $this->f2comps['A2']);
328
        $this->assertModuleCompetencyMigrated($this->cms[$this->c2->id]['F1'], $this->f1comps['A3'], $this->f2comps['A3']);
329
    }
330
 
11 efrain 331
    public function test_destination_competency_exists(): void {
1 efrain 332
        $this->setAdminUser();
333
        $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
334
 
335
        // Pre-add the new competency to course 1.
336
        $lpg->create_course_competency(array('courseid' => $this->c1->id, 'competencyid' => $this->f2comps['A1']->get('id')));
337
 
338
        // Pre-add the new competency to module in course 2.
339
        $lpg->create_course_module_competency(array(
340
            'cmid' => $this->cms[$this->c2->id]['F1']->cmid,
341
            'competencyid' => $this->f2comps['A2']->get('id')
342
        ));
343
 
344
        $mapper = new framework_mapper($this->f1->get('id'), $this->f2->get('id'));
345
        $mapper->automap();
346
        $processor = new framework_processor($mapper);
347
        $processor->proceed();
348
 
349
        $this->assertEquals(2, $processor->get_courses_found_count());
350
        $this->assertEquals(5, $processor->get_expected_course_competency_migrations());
351
        $this->assertEquals(3, $processor->get_course_competency_migrations());
352
        $this->assertEquals(2, $processor->get_course_competency_removals());
353
 
354
        $this->assertEquals(3, $processor->get_cms_found_count());
355
        $this->assertEquals(5, $processor->get_expected_module_competency_migrations());
356
        $this->assertEquals(3, $processor->get_module_competency_migrations());
357
        $this->assertEquals(3, $processor->get_module_competency_removals());
358
 
359
        $this->assertEquals(array(), $processor->get_errors());
360
        $warnings = $processor->get_warnings();
361
        $this->assertCount(2, $warnings);
362
 
363
        $warning = array_shift($warnings);
364
        $this->assertEquals($this->c1->id, $warning['courseid']);
365
        $this->assertEquals($this->f1comps['A1']->get('id'), $warning['competencyid']);
366
        $this->assertEquals(null, $warning['cmid']);
367
        $this->assertMatchesRegularExpression('/competency already exists/', $warning['message']);
368
 
369
        $warning = array_shift($warnings);
370
        $this->assertEquals($this->c2->id, $warning['courseid']);
371
        $this->assertEquals($this->f1comps['A2']->get('id'), $warning['competencyid']);
372
        $this->assertEquals($this->cms[$this->c2->id]['F1']->cmid, $warning['cmid']);
373
        $this->assertMatchesRegularExpression('/competency already exists/', $warning['message']);
374
 
375
        $this->assertCourseCompetencyExists($this->c1, $this->f1comps['A1']);
376
        $this->assertModuleCompetencyExists($this->cms[$this->c2->id]['F1'], $this->f1comps['A2']);
377
    }
378
 
11 efrain 379
    public function test_destination_competency_exists_remove_original(): void {
1 efrain 380
        $this->setAdminUser();
381
        $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
382
 
383
        // Pre-add the new competency to course 1.
384
        $lpg->create_course_competency(array('courseid' => $this->c1->id, 'competencyid' => $this->f2comps['A1']->get('id')));
385
 
386
        // Pre-add the new competency to module in course 2.
387
        $lpg->create_course_module_competency(array(
388
            'cmid' => $this->cms[$this->c2->id]['F1']->cmid,
389
            'competencyid' => $this->f2comps['A2']->get('id')
390
        ));
391
 
392
        $mapper = new framework_mapper($this->f1->get('id'), $this->f2->get('id'));
393
        $mapper->automap();
394
        $processor = new framework_processor($mapper);
395
        $processor->set_remove_original_when_destination_already_present(true);
396
        $processor->proceed();
397
 
398
        $this->assertEquals(2, $processor->get_courses_found_count());
399
        $this->assertEquals(5, $processor->get_expected_course_competency_migrations());
400
        $this->assertEquals(3, $processor->get_course_competency_migrations());
401
        $this->assertEquals(4, $processor->get_course_competency_removals());
402
 
403
        $this->assertEquals(3, $processor->get_cms_found_count());
404
        $this->assertEquals(5, $processor->get_expected_module_competency_migrations());
405
        $this->assertEquals(3, $processor->get_module_competency_migrations());
406
        $this->assertEquals(4, $processor->get_module_competency_removals());
407
 
408
        $this->assertEquals(array(), $processor->get_errors());
409
        $this->assertEquals(array(), $processor->get_warnings());
410
 
411
        $this->assertCourseCompetencyNotExists($this->c1, $this->f1comps['A1']);
412
        $this->assertModuleCompetencyNotExists($this->cms[$this->c2->id]['F1'], $this->f1comps['A2']);
413
    }
414
 
11 efrain 415
    public function test_permission_exception(): void {
1 efrain 416
 
417
        $this->preventResetByRollback(); // Test uses transactions, so we cannot use them for speedy reset.
418
 
419
        $dg = $this->getDataGenerator();
420
        $u = $dg->create_user();
421
        $role = $dg->create_role();
422
        $sysctx = \context_system::instance();
423
 
424
        $dg->enrol_user($u->id, $this->c1->id, 'editingteacher');
425
        $dg->enrol_user($u->id, $this->c2->id, 'editingteacher');
426
        assign_capability('moodle/competency:coursecompetencymanage', CAP_PROHIBIT, $role, $sysctx->id);
427
        role_assign($role, $u->id, \context_course::instance($this->c1->id)->id);
428
        role_assign($role, $u->id, \context_module::instance($this->cms[$this->c2->id]['F1']->cmid)->id);
429
 
430
        accesslib_clear_all_caches_for_unit_testing();
431
        $this->setUser($u);
432
 
433
        // Do C1 first.
434
        $mapper = new framework_mapper($this->f1->get('id'), $this->f2->get('id'));
435
        $mapper->automap();
436
        $processor = new framework_processor($mapper);
437
        $processor->set_allowedcourses(array($this->c1->id));
438
        $processor->proceed();
439
 
440
        $this->assertEquals(1, $processor->get_courses_found_count());
441
        $this->assertEquals(3, $processor->get_expected_course_competency_migrations());
442
        $this->assertEquals(0, $processor->get_course_competency_migrations());
443
        $this->assertEquals(0, $processor->get_course_competency_removals());
444
 
445
        $this->assertEquals(2, $processor->get_cms_found_count());
446
        $this->assertEquals(3, $processor->get_expected_module_competency_migrations());
447
        $this->assertEquals(0, $processor->get_module_competency_migrations());
448
        $this->assertEquals(0, $processor->get_module_competency_removals());
449
 
450
        $this->assertEquals(array(), $processor->get_warnings());
451
        $errors = $processor->get_errors();
452
        $this->assertCount(2, $errors);
453
        $this->assertEquals($this->c1->id, $errors[0]['courseid']);
454
        $this->assertEquals($this->f1comps['A1']->get('id'), $errors[0]['competencyid']);
455
        $this->assertEquals(null, $errors[0]['cmid']);
456
        $this->assertMatchesRegularExpression('/Sorry, but you do not currently have permissions to do that/',
457
            $errors[0]['message']);
458
        $this->assertEquals($this->f1comps['A3']->get('id'), $errors[1]['competencyid']);
459
 
460
        $this->assertCourseCompetencyNotMigrated($this->c1, $this->f1comps['A1'], $this->f2comps['A1']);
461
        $this->assertCourseCompetencyNotMigrated($this->c1, $this->f1comps['A3'], $this->f2comps['A3']);
462
        $this->assertModuleCompetencyNotMigrated($this->cms[$this->c1->id]['F1'], $this->f1comps['A1'], $this->f2comps['A1']);
463
        $this->assertModuleCompetencyNotMigrated($this->cms[$this->c1->id]['P1'], $this->f1comps['A3'], $this->f2comps['A3']);
464
 
465
        // Do C2 now.
466
        $processor = new framework_processor($mapper);
467
        $processor->set_allowedcourses(array($this->c2->id));
468
        $processor->proceed();
469
 
470
        $this->assertEquals(1, $processor->get_courses_found_count());
471
        $this->assertEquals(2, $processor->get_expected_course_competency_migrations());
472
        $this->assertEquals(2, $processor->get_course_competency_migrations());
473
        $this->assertEquals(0, $processor->get_course_competency_removals());
474
 
475
        $this->assertEquals(1, $processor->get_cms_found_count());
476
        $this->assertEquals(2, $processor->get_expected_module_competency_migrations());
477
        $this->assertEquals(0, $processor->get_module_competency_migrations());
478
        $this->assertEquals(0, $processor->get_module_competency_removals());
479
 
480
        $this->assertEquals(array(), $processor->get_warnings());
481
        $errors = $processor->get_errors();
482
        $this->assertCount(2, $errors);
483
        $this->assertEquals($this->c2->id, $errors[0]['courseid']);
484
        $this->assertEquals($this->f1comps['A2']->get('id'), $errors[0]['competencyid']);
485
        $this->assertEquals($this->cms[$this->c2->id]['F1']->cmid, $errors[0]['cmid']);
486
        $this->assertMatchesRegularExpression('/Sorry, but you do not currently have permissions to do that/',
487
            $errors[0]['message']);
488
        $this->assertEquals($this->f1comps['A3']->get('id'), $errors[1]['competencyid']);
489
 
490
        // The new competencies were added to the course, but the old ones were not removed because they are still in modules.
491
        $this->assertCourseCompetencyExists($this->c2, $this->f1comps['A2']);
492
        $this->assertCourseCompetencyExists($this->c2, $this->f1comps['A3']);
493
        $this->assertCourseCompetencyExists($this->c2, $this->f2comps['A2']);
494
        $this->assertCourseCompetencyExists($this->c2, $this->f2comps['A3']);
495
 
496
        // Module competencies were not migrated because permissions are lacking.
497
        $this->assertModuleCompetencyNotMigrated($this->cms[$this->c2->id]['F1'], $this->f1comps['A2'], $this->f2comps['A2']);
498
        $this->assertModuleCompetencyNotMigrated($this->cms[$this->c2->id]['F1'], $this->f1comps['A3'], $this->f2comps['A2']);
499
    }
500
 
501
    /**
502
     * Assert that the course competency exists.
503
     *
504
     * @param \stdClass $course The course.
505
     * @param competency $competency The competency.
506
     */
507
    protected function assertCourseCompetencyExists($course, $competency) {
508
        $this->assertTrue(course_competency::record_exists_select("courseid = :courseid AND competencyid = :competencyid",
509
            array('courseid' => $course->id, 'competencyid' => $competency->get('id'))));
510
    }
511
 
512
    /**
513
     * Assert that the course competency does not exist.
514
     *
515
     * @param \stdClass $course The course.
516
     * @param competency $competency The competency.
517
     */
518
    protected function assertCourseCompetencyNotExists($course, $competency) {
519
        $this->assertFalse(course_competency::record_exists_select("courseid = :courseid AND competencyid = :competencyid",
520
            array('courseid' => $course->id, 'competencyid' => $competency->get('id'))));
521
    }
522
 
523
    /**
524
     * Assert that the course competency was migrated.
525
     *
526
     * @param \stdClass $course The course.
527
     * @param competency $compfrom The competency from.
528
     * @param competency $compto The competency to.
529
     */
530
    protected function assertCourseCompetencyMigrated($course, $compfrom, $compto) {
531
        $ccs = $this->ccs[$course->id];
532
 
533
        $this->assertCourseCompetencyNotExists($course, $compfrom);
534
        $this->assertCourseCompetencyExists($course, $compto);
535
 
536
        $before = $ccs[$compfrom->get('id')];
537
        $after = course_competency::get_record(array(
538
            'courseid' => $course->id,
539
            'competencyid' => $compto->get('id')
540
        ));
541
 
542
        $this->assertNotEquals($before->get('id'), $after->get('id'));
543
        $this->assertEquals($before->get('courseid'), $after->get('courseid'));
544
        $this->assertEquals($before->get('sortorder'), $after->get('sortorder'));
545
        $this->assertEquals($before->get('ruleoutcome'), $after->get('ruleoutcome'));
546
    }
547
 
548
    /**
549
     * Assert that the course competency was not migrated.
550
     *
551
     * @param \stdClass $course The course.
552
     * @param competency $compfrom The competency from.
553
     * @param competency $compto The competency to.
554
     */
555
    protected function assertCourseCompetencyNotMigrated($course, $compfrom, $compto) {
556
        $ccs = $this->ccs[$course->id];
557
 
558
        $this->assertCourseCompetencyExists($course, $compfrom);
559
        $this->assertCourseCompetencyNotExists($course, $compto);
560
 
561
        $before = $ccs[$compfrom->get('id')];
562
        $after = $ccs[$compfrom->get('id')];
563
 
564
        $this->assertEquals($before->get('id'), $after->get('id'));
565
        $this->assertEquals($before->get('courseid'), $after->get('courseid'));
566
        $this->assertEquals($before->get('sortorder'), $after->get('sortorder'));
567
        $this->assertEquals($before->get('ruleoutcome'), $after->get('ruleoutcome'));
568
    }
569
 
570
    /**
571
     * Assert that the course module competency exists.
572
     *
573
     * @param \stdClass $cm The CM.
574
     * @param competency $competency The competency.
575
     */
576
    protected function assertModuleCompetencyExists($cm, $competency) {
577
        $this->assertTrue(course_module_competency::record_exists_select("cmid = :cmid AND competencyid = :competencyid",
578
            array('cmid' => $cm->cmid, 'competencyid' => $competency->get('id'))));
579
    }
580
 
581
    /**
582
     * Assert that the course module competency does not exist.
583
     *
584
     * @param \stdClass $cm The CM.
585
     * @param competency $competency The competency.
586
     */
587
    protected function assertModuleCompetencyNotExists($cm, $competency) {
588
        $this->assertFalse(course_module_competency::record_exists_select("cmid = :cmid AND competencyid = :competencyid",
589
            array('cmid' => $cm->cmid, 'competencyid' => $competency->get('id'))));
590
    }
591
 
592
    /**
593
     * Assert that the course module competency was migrated.
594
     *
595
     * @param \stdClass $cm The CM.
596
     * @param competency $compfrom The competency from.
597
     * @param competency $compto The competency to.
598
     */
599
    protected function assertModuleCompetencyMigrated($cm, $compfrom, $compto) {
600
        $cmcs = $this->cmcs[$cm->cmid];
601
 
602
        $this->assertModuleCompetencyNotExists($cm, $compfrom);
603
        $this->assertModuleCompetencyExists($cm, $compto);
604
 
605
        $before = $cmcs[$compfrom->get('id')];
606
        $after = course_module_competency::get_record(array(
607
            'cmid' => $cm->cmid,
608
            'competencyid' => $compto->get('id')
609
        ));
610
 
611
        $this->assertNotEquals($before->get('id'), $after->get('id'));
612
        $this->assertEquals($before->get('cmid'), $after->get('cmid'));
613
        $this->assertEquals($before->get('sortorder'), $after->get('sortorder'));
614
        $this->assertEquals($before->get('ruleoutcome'), $after->get('ruleoutcome'));
615
    }
616
 
617
    /**
618
     * Assert that the course module competency was not migrated.
619
     *
620
     * @param \stdClass $cm The CM.
621
     * @param competency $compfrom The competency from.
622
     * @param competency $compto The competency to.
623
     */
624
    protected function assertModuleCompetencyNotMigrated($cm, $compfrom, $compto) {
625
        $cmcs = $this->cmcs[$cm->cmid];
626
 
627
        $this->assertModuleCompetencyExists($cm, $compfrom);
628
        $this->assertModuleCompetencyNotExists($cm, $compto);
629
 
630
        $before = $cmcs[$compfrom->get('id')];
631
        $after = $cmcs[$compfrom->get('id')];
632
 
633
        $this->assertEquals($before->get('id'), $after->get('id'));
634
        $this->assertEquals($before->get('cmid'), $after->get('cmid'));
635
        $this->assertEquals($before->get('sortorder'), $after->get('sortorder'));
636
        $this->assertEquals($before->get('ruleoutcome'), $after->get('ruleoutcome'));
637
    }
638
}