Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

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