[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/tool/lpmigrate/tests/ -> processor_test.php (source)

   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() {
  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          global $DB;
 400          if ($DB->get_dbfamily() === 'postgres' or $DB->get_dbfamily() === 'mssql') {
 401              $this->markTestSkipped('The processor is having issues with the transaction initialised in '
 402                  . 'advanced_testcase::runBare().');
 403              return;
 404          }
 405  
 406          $dg = $this->getDataGenerator();
 407          $u = $dg->create_user();
 408          $role = $dg->create_role();
 409          $sysctx = context_system::instance();
 410  
 411          $dg->enrol_user($u->id, $this->c1->id, 'editingteacher');
 412          $dg->enrol_user($u->id, $this->c2->id, 'editingteacher');
 413          assign_capability('moodle/competency:coursecompetencymanage', CAP_PROHIBIT, $role, $sysctx->id);
 414          role_assign($role, $u->id, context_course::instance($this->c1->id)->id);
 415          role_assign($role, $u->id, context_module::instance($this->cms[$this->c2->id]['F1']->cmid)->id);
 416  
 417          accesslib_clear_all_caches_for_unit_testing();
 418          $this->setUser($u);
 419  
 420          // Do C1 first.
 421          $mapper = new framework_mapper($this->f1->get_id(), $this->f2->get_id());
 422          $mapper->automap();
 423          $processor = new framework_processor($mapper);
 424          $processor->set_allowedcourses(array($this->c1->id));
 425          $processor->proceed();
 426  
 427          $this->assertEquals(1, $processor->get_courses_found_count());
 428          $this->assertEquals(3, $processor->get_expected_course_competency_migrations());
 429          $this->assertEquals(0, $processor->get_course_competency_migrations());
 430          $this->assertEquals(0, $processor->get_course_competency_removals());
 431  
 432          $this->assertEquals(2, $processor->get_cms_found_count());
 433          $this->assertEquals(3, $processor->get_expected_module_competency_migrations());
 434          $this->assertEquals(0, $processor->get_module_competency_migrations());
 435          $this->assertEquals(0, $processor->get_module_competency_removals());
 436  
 437          $this->assertEquals(array(), $processor->get_warnings());
 438          $errors = $processor->get_errors();
 439          $this->assertCount(2, $errors);
 440          $this->assertEquals($this->c1->id, $errors[0]['courseid']);
 441          $this->assertEquals($this->f1comps['A1']->get_id(), $errors[0]['competencyid']);
 442          $this->assertEquals(null, $errors[0]['cmid']);
 443          $this->assertRegexp('/Sorry, but you do not currently have permissions to do that/', $errors[0]['message']);
 444          $this->assertEquals($this->f1comps['A3']->get_id(), $errors[1]['competencyid']);
 445  
 446          $this->assertCourseCompetencyNotMigrated($this->c1, $this->f1comps['A1'], $this->f2comps['A1']);
 447          $this->assertCourseCompetencyNotMigrated($this->c1, $this->f1comps['A3'], $this->f2comps['A3']);
 448          $this->assertModuleCompetencyNotMigrated($this->cms[$this->c1->id]['F1'], $this->f1comps['A1'], $this->f2comps['A1']);
 449          $this->assertModuleCompetencyNotMigrated($this->cms[$this->c1->id]['P1'], $this->f1comps['A3'], $this->f2comps['A3']);
 450  
 451          // Do C2 now.
 452          $processor = new framework_processor($mapper);
 453          $processor->set_allowedcourses(array($this->c2->id));
 454          $processor->proceed();
 455  
 456          $this->assertEquals(1, $processor->get_courses_found_count());
 457          $this->assertEquals(2, $processor->get_expected_course_competency_migrations());
 458          $this->assertEquals(2, $processor->get_course_competency_migrations());
 459          $this->assertEquals(0, $processor->get_course_competency_removals());
 460  
 461          $this->assertEquals(1, $processor->get_cms_found_count());
 462          $this->assertEquals(2, $processor->get_expected_module_competency_migrations());
 463          $this->assertEquals(0, $processor->get_module_competency_migrations());
 464          $this->assertEquals(0, $processor->get_module_competency_removals());
 465  
 466          $this->assertEquals(array(), $processor->get_warnings());
 467          $errors = $processor->get_errors();
 468          $this->assertCount(2, $errors);
 469          $this->assertEquals($this->c2->id, $errors[0]['courseid']);
 470          $this->assertEquals($this->f1comps['A2']->get_id(), $errors[0]['competencyid']);
 471          $this->assertEquals($this->cms[$this->c2->id]['F1']->cmid, $errors[0]['cmid']);
 472          $this->assertRegexp('/Sorry, but you do not currently have permissions to do that/', $errors[0]['message']);
 473          $this->assertEquals($this->f1comps['A3']->get_id(), $errors[1]['competencyid']);
 474  
 475          // The new competencies were added to the course, but the old ones were not removed because they are still in modules.
 476          $this->assertCourseCompetencyExists($this->c2, $this->f1comps['A2']);
 477          $this->assertCourseCompetencyExists($this->c2, $this->f1comps['A3']);
 478          $this->assertCourseCompetencyExists($this->c2, $this->f2comps['A2']);
 479          $this->assertCourseCompetencyExists($this->c2, $this->f2comps['A3']);
 480  
 481          // Module competencies were not migrated because permissions are lacking.
 482          $this->assertModuleCompetencyNotMigrated($this->cms[$this->c2->id]['F1'], $this->f1comps['A2'], $this->f2comps['A2']);
 483          $this->assertModuleCompetencyNotMigrated($this->cms[$this->c2->id]['F1'], $this->f1comps['A3'], $this->f2comps['A2']);
 484      }
 485  
 486      /**
 487       * Assert that the course competency exists.
 488       *
 489       * @param stdClass $course The course.
 490       * @param competency $competency The competency.
 491       */
 492      protected function assertCourseCompetencyExists($course, $competency) {
 493          $this->assertTrue(course_competency::record_exists_select("courseid = :courseid AND competencyid = :competencyid",
 494              array('courseid' => $course->id, 'competencyid' => $competency->get_id())));
 495      }
 496  
 497      /**
 498       * Assert that the course competency does not exist.
 499       *
 500       * @param stdClass $course The course.
 501       * @param competency $competency The competency.
 502       */
 503      protected function assertCourseCompetencyNotExists($course, $competency) {
 504          $this->assertFalse(course_competency::record_exists_select("courseid = :courseid AND competencyid = :competencyid",
 505              array('courseid' => $course->id, 'competencyid' => $competency->get_id())));
 506      }
 507  
 508      /**
 509       * Assert that the course competency was migrated.
 510       *
 511       * @param stdClass $course The course.
 512       * @param competency $compfrom The competency from.
 513       * @param competency $compto The competency to.
 514       */
 515      protected function assertCourseCompetencyMigrated($course, $compfrom, $compto) {
 516          $ccs = $this->ccs[$course->id];
 517  
 518          $this->assertCourseCompetencyNotExists($course, $compfrom);
 519          $this->assertCourseCompetencyExists($course, $compto);
 520  
 521          $before = $ccs[$compfrom->get_id()];
 522          $after = course_competency::get_record(array(
 523              'courseid' => $course->id,
 524              'competencyid' => $compto->get_id()
 525          ));
 526  
 527          $this->assertNotEquals($before->get_id(), $after->get_id());
 528          $this->assertEquals($before->get_courseid(), $after->get_courseid());
 529          $this->assertEquals($before->get_sortorder(), $after->get_sortorder());
 530          $this->assertEquals($before->get_ruleoutcome(), $after->get_ruleoutcome());
 531      }
 532  
 533      /**
 534       * Assert that the course competency was not migrated.
 535       *
 536       * @param stdClass $course The course.
 537       * @param competency $compfrom The competency from.
 538       * @param competency $compto The competency to.
 539       */
 540      protected function assertCourseCompetencyNotMigrated($course, $compfrom, $compto) {
 541          $ccs = $this->ccs[$course->id];
 542  
 543          $this->assertCourseCompetencyExists($course, $compfrom);
 544          $this->assertCourseCompetencyNotExists($course, $compto);
 545  
 546          $before = $ccs[$compfrom->get_id()];
 547          $after = $ccs[$compfrom->get_id()];
 548  
 549          $this->assertEquals($before->get_id(), $after->get_id());
 550          $this->assertEquals($before->get_courseid(), $after->get_courseid());
 551          $this->assertEquals($before->get_sortorder(), $after->get_sortorder());
 552          $this->assertEquals($before->get_ruleoutcome(), $after->get_ruleoutcome());
 553      }
 554  
 555      /**
 556       * Assert that the course module competency exists.
 557       *
 558       * @param stdClass $cm The CM.
 559       * @param competency $competency The competency.
 560       */
 561      protected function assertModuleCompetencyExists($cm, $competency) {
 562          $this->assertTrue(course_module_competency::record_exists_select("cmid = :cmid AND competencyid = :competencyid",
 563              array('cmid' => $cm->cmid, 'competencyid' => $competency->get_id())));
 564      }
 565  
 566      /**
 567       * Assert that the course module competency does not exist.
 568       *
 569       * @param stdClass $cm The CM.
 570       * @param competency $competency The competency.
 571       */
 572      protected function assertModuleCompetencyNotExists($cm, $competency) {
 573          $this->assertFalse(course_module_competency::record_exists_select("cmid = :cmid AND competencyid = :competencyid",
 574              array('cmid' => $cm->cmid, 'competencyid' => $competency->get_id())));
 575      }
 576  
 577      /**
 578       * Assert that the course module competency was migrated.
 579       *
 580       * @param stdClass $cm The CM.
 581       * @param competency $compfrom The competency from.
 582       * @param competency $compto The competency to.
 583       */
 584      protected function assertModuleCompetencyMigrated($cm, $compfrom, $compto) {
 585          $cmcs = $this->cmcs[$cm->cmid];
 586  
 587          $this->assertModuleCompetencyNotExists($cm, $compfrom);
 588          $this->assertModuleCompetencyExists($cm, $compto);
 589  
 590          $before = $cmcs[$compfrom->get_id()];
 591          $after = course_module_competency::get_record(array(
 592              'cmid' => $cm->cmid,
 593              'competencyid' => $compto->get_id()
 594          ));
 595  
 596          $this->assertNotEquals($before->get_id(), $after->get_id());
 597          $this->assertEquals($before->get_cmid(), $after->get_cmid());
 598          $this->assertEquals($before->get_sortorder(), $after->get_sortorder());
 599          $this->assertEquals($before->get_ruleoutcome(), $after->get_ruleoutcome());
 600      }
 601  
 602      /**
 603       * Assert that the course module competency was not migrated.
 604       *
 605       * @param stdClass $cm The CM.
 606       * @param competency $compfrom The competency from.
 607       * @param competency $compto The competency to.
 608       */
 609      protected function assertModuleCompetencyNotMigrated($cm, $compfrom, $compto) {
 610          $cmcs = $this->cmcs[$cm->cmid];
 611  
 612          $this->assertModuleCompetencyExists($cm, $compfrom);
 613          $this->assertModuleCompetencyNotExists($cm, $compto);
 614  
 615          $before = $cmcs[$compfrom->get_id()];
 616          $after = $cmcs[$compfrom->get_id()];
 617  
 618          $this->assertEquals($before->get_id(), $after->get_id());
 619          $this->assertEquals($before->get_cmid(), $after->get_cmid());
 620          $this->assertEquals($before->get_sortorder(), $after->get_sortorder());
 621          $this->assertEquals($before->get_ruleoutcome(), $after->get_ruleoutcome());
 622      }
 623  
 624  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1