Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * 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   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  global $CFG;
  27  
  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;
  32  
  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 {
  41  
  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(): void {
  50          $this->resetAfterTest(true);
  51          $dg = $this->getDataGenerator();
  52          $lpg = $dg->get_plugin_generator('core_competency');
  53  
  54          $f1 = $lpg->create_framework(array('idnumber' => 'BIO2015'));
  55          $f2 = $lpg->create_framework(array('idnumber' => 'BIO2016'));
  56  
  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'));
  62  
  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'));
  68  
  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          );
  82  
  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          );
 100  
 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          );
 130  
 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']);
 141  
 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      }
 152  
 153      public function test_simple_migration() {
 154          $this->setAdminUser();
 155  
 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();
 160  
 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());
 165  
 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());
 170  
 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());
 174  
 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']);
 179  
 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']);
 184  
 185          $this->assertCourseCompetencyExists($this->c1, $this->f1comps['X1']);
 186          $this->assertModuleCompetencyExists($this->cms[$this->c1->id]['F1'], $this->f1comps['X1']);
 187      }
 188  
 189      public function test_remove_when_missing() {
 190          $this->setAdminUser();
 191  
 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();
 197  
 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());
 202  
 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());
 207  
 208          $this->assertCount(0, $processor->get_errors());
 209          $this->assertCount(0, $processor->get_warnings());
 210  
 211          $this->assertCourseCompetencyNotExists($this->c1, $this->f1comps['X1']);
 212          $this->assertModuleCompetencyNotExists($this->cms[$this->c1->id]['F1'], $this->f1comps['X1']);
 213      }
 214  
 215      public function test_allowed_courses() {
 216          $this->setAdminUser();
 217  
 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();
 223  
 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());
 228  
 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());
 233  
 234          $this->assertCount(0, $processor->get_errors());
 235          $this->assertCount(0, $processor->get_warnings());
 236  
 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']);
 241  
 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      }
 247  
 248      public function test_disallowed_courses() {
 249          $this->setAdminUser();
 250  
 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();
 256  
 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());
 261  
 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());
 266  
 267          $this->assertCount(0, $processor->get_errors());
 268          $this->assertCount(0, $processor->get_warnings());
 269  
 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']);
 274  
 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      }
 280  
 281      public function test_course_start_date_from() {
 282          $this->setAdminUser();
 283  
 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();
 289  
 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());
 294  
 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());
 299  
 300          $this->assertCount(0, $processor->get_errors());
 301          $this->assertCount(0, $processor->get_warnings());
 302  
 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']);
 307  
 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      }
 313  
 314      public function test_destination_competency_exists() {
 315          $this->setAdminUser();
 316          $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
 317  
 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')));
 320  
 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          ));
 326  
 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();
 331  
 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());
 336  
 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());
 341  
 342          $this->assertEquals(array(), $processor->get_errors());
 343          $warnings = $processor->get_warnings();
 344          $this->assertCount(2, $warnings);
 345  
 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']);
 351  
 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']);
 357  
 358          $this->assertCourseCompetencyExists($this->c1, $this->f1comps['A1']);
 359          $this->assertModuleCompetencyExists($this->cms[$this->c2->id]['F1'], $this->f1comps['A2']);
 360      }
 361  
 362      public function test_destination_competency_exists_remove_original() {
 363          $this->setAdminUser();
 364          $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
 365  
 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')));
 368  
 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          ));
 374  
 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();
 380  
 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());
 385  
 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());
 390  
 391          $this->assertEquals(array(), $processor->get_errors());
 392          $this->assertEquals(array(), $processor->get_warnings());
 393  
 394          $this->assertCourseCompetencyNotExists($this->c1, $this->f1comps['A1']);
 395          $this->assertModuleCompetencyNotExists($this->cms[$this->c2->id]['F1'], $this->f1comps['A2']);
 396      }
 397  
 398      public function test_permission_exception() {
 399  
 400          $this->preventResetByRollback(); // Test uses transactions, so we cannot use them for speedy reset.
 401  
 402          $dg = $this->getDataGenerator();
 403          $u = $dg->create_user();
 404          $role = $dg->create_role();
 405          $sysctx = context_system::instance();
 406  
 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);
 412  
 413          accesslib_clear_all_caches_for_unit_testing();
 414          $this->setUser($u);
 415  
 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();
 422  
 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());
 427  
 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());
 432  
 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']);
 441  
 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']);
 446  
 447          // Do C2 now.
 448          $processor = new framework_processor($mapper);
 449          $processor->set_allowedcourses(array($this->c2->id));
 450          $processor->proceed();
 451  
 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());
 456  
 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());
 461  
 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']);
 470  
 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']);
 476  
 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      }
 481  
 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      }
 492  
 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      }
 503  
 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];
 513  
 514          $this->assertCourseCompetencyNotExists($course, $compfrom);
 515          $this->assertCourseCompetencyExists($course, $compto);
 516  
 517          $before = $ccs[$compfrom->get('id')];
 518          $after = course_competency::get_record(array(
 519              'courseid' => $course->id,
 520              'competencyid' => $compto->get('id')
 521          ));
 522  
 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      }
 528  
 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];
 538  
 539          $this->assertCourseCompetencyExists($course, $compfrom);
 540          $this->assertCourseCompetencyNotExists($course, $compto);
 541  
 542          $before = $ccs[$compfrom->get('id')];
 543          $after = $ccs[$compfrom->get('id')];
 544  
 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      }
 550  
 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      }
 561  
 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      }
 572  
 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];
 582  
 583          $this->assertModuleCompetencyNotExists($cm, $compfrom);
 584          $this->assertModuleCompetencyExists($cm, $compto);
 585  
 586          $before = $cmcs[$compfrom->get('id')];
 587          $after = course_module_competency::get_record(array(
 588              'cmid' => $cm->cmid,
 589              'competencyid' => $compto->get('id')
 590          ));
 591  
 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      }
 597  
 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];
 607  
 608          $this->assertModuleCompetencyExists($cm, $compfrom);
 609          $this->assertModuleCompetencyNotExists($cm, $compto);
 610  
 611          $before = $cmcs[$compfrom->get('id')];
 612          $after = $cmcs[$compfrom->get('id')];
 613  
 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      }
 619  
 620  }