[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/enrol/imsenterprise/tests/ -> imsenterprise_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   * IMS Enterprise enrolment tests.
  19   *
  20   * @package    enrol_imsenterprise
  21   * @category   test
  22   * @copyright  2012 David Monllaó
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once($CFG->dirroot . '/enrol/imsenterprise/locallib.php');
  30  require_once($CFG->dirroot . '/enrol/imsenterprise/lib.php');
  31  
  32  /**
  33   * IMS Enterprise test case
  34   *
  35   * @package    enrol_imsenterprise
  36   * @category   test
  37   * @copyright  2012 David Monllaó
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class enrol_imsenterprise_testcase extends advanced_testcase {
  41  
  42      /**
  43       * @var $imsplugin enrol_imsenterprise_plugin IMS plugin instance.
  44       */
  45      public $imsplugin;
  46  
  47      /**
  48       * Setup required for all tests.
  49       */
  50      protected function setUp() {
  51          $this->resetAfterTest(true);
  52          $this->imsplugin = enrol_get_plugin('imsenterprise');
  53          $this->set_test_config();
  54      }
  55  
  56      /**
  57       * With an empty IMS enterprise file
  58       */
  59      public function test_emptyfile() {
  60          global $DB;
  61  
  62          $prevncourses = $DB->count_records('course');
  63          $prevnusers = $DB->count_records('user');
  64  
  65          $this->set_xml_file(false, false);
  66          $this->imsplugin->cron();
  67  
  68          $this->assertEquals($prevncourses, $DB->count_records('course'));
  69          $this->assertEquals($prevnusers, $DB->count_records('user'));
  70      }
  71  
  72      /**
  73       * Existing users are not created again
  74       */
  75      public function test_users_existing() {
  76          global $DB;
  77  
  78          $user1 = $this->getDataGenerator()->create_user();
  79          $user2 = $this->getDataGenerator()->create_user();
  80  
  81          $prevnusers = $DB->count_records('user');
  82  
  83          $users = array($user1, $user2);
  84          $this->set_xml_file($users);
  85          $this->imsplugin->cron();
  86  
  87          $this->assertEquals($prevnusers, $DB->count_records('user'));
  88      }
  89  
  90      /**
  91       * Add new users
  92       */
  93      public function test_users_add() {
  94          global $DB;
  95  
  96          $prevnusers = $DB->count_records('user');
  97  
  98          $user1 = new StdClass();
  99          $user1->username = 'u1';
 100          $user1->email = '[email protected]';
 101          $user1->firstname = 'U';
 102          $user1->lastname = '1';
 103  
 104          $users = array($user1);
 105          $this->set_xml_file($users);
 106          $this->imsplugin->cron();
 107  
 108          $this->assertEquals(($prevnusers + 1), $DB->count_records('user'));
 109      }
 110  
 111      /**
 112       * Existing courses are not created again
 113       */
 114      public function test_courses_existing() {
 115          global $DB;
 116  
 117          $course1 = $this->getDataGenerator()->create_course(array('idnumber' => 'id1'));
 118          $course2 = $this->getDataGenerator()->create_course(array('idnumber' => 'id2'));
 119  
 120          // Default mapping according to default course attributes - IMS description tags mapping.
 121          $course1->imsshort = $course1->fullname;
 122          $course2->imsshort = $course2->fullname;
 123  
 124          $prevncourses = $DB->count_records('course');
 125  
 126          $courses = array($course1, $course2);
 127          $this->set_xml_file(false, $courses);
 128          $this->imsplugin->cron();
 129  
 130          $this->assertEquals($prevncourses, $DB->count_records('course'));
 131      }
 132  
 133      /**
 134       * Add new courses
 135       */
 136      public function test_courses_add() {
 137          global $DB;
 138  
 139          $prevncourses = $DB->count_records('course');
 140  
 141          $course1 = new StdClass();
 142          $course1->idnumber = 'id1';
 143          $course1->imsshort = 'id1';
 144          $course1->category = 'DEFAULT CATNAME';
 145  
 146          $course2 = new StdClass();
 147          $course2->idnumber = 'id2';
 148          $course2->imsshort = 'id2';
 149          $course2->category = 'DEFAULT CATNAME';
 150  
 151          $courses = array($course1, $course2);
 152          $this->set_xml_file(false, $courses);
 153          $this->imsplugin->cron();
 154  
 155          $this->assertEquals(($prevncourses + 2), $DB->count_records('course'));
 156      }
 157  
 158      /**
 159       * Add new course without a category.
 160       */
 161      public function test_course_add_default_category() {
 162          global $DB, $CFG;
 163          require_once($CFG->libdir.'/coursecatlib.php');
 164  
 165          $this->imsplugin->set_config('createnewcategories', false);
 166  
 167          // Delete the default category, to ensure the plugin handles this gracefully.
 168          $defaultcat = coursecat::get_default();
 169          $defaultcat->delete_full(false);
 170  
 171          // Create an course with the IMS plugin without a category.
 172          $course1 = new stdClass();
 173          $course1->idnumber = 'id1';
 174          $course1->imsshort = 'id1';
 175          $course1->category = '';
 176          $this->set_xml_file(false, array($course1));
 177          $this->imsplugin->cron();
 178  
 179          // Check the course has been created.
 180          $dbcourse = $DB->get_record('course', array('idnumber' => $course1->idnumber), '*', MUST_EXIST);
 181          // Check that it belongs to a category which exists.
 182          $this->assertTrue($DB->record_exists('course_categories', array('id' => $dbcourse->category)));
 183      }
 184  
 185      /**
 186       * Course attributes mapping to IMS enterprise group description tags
 187       */
 188      public function test_courses_attrmapping() {
 189          global $DB;
 190  
 191          // Setting a all = coursecode (idnumber) mapping.
 192          $this->imsplugin->set_config('imscoursemapshortname', 'coursecode');
 193          $this->imsplugin->set_config('imscoursemapfullname', 'coursecode');
 194          $this->imsplugin->set_config('imscoursemapsummary', 'coursecode');
 195  
 196          $course1 = new StdClass();
 197          $course1->idnumber = 'id1';
 198          $course1->imsshort = 'description_short1';
 199          $course1->imslong = 'description_long';
 200          $course1->imsfull = 'description_full';
 201          $course1->category = 'DEFAULT CATNAME';
 202  
 203          $this->set_xml_file(false, array($course1));
 204          $this->imsplugin->cron();
 205  
 206          $dbcourse = $DB->get_record('course', array('idnumber' => $course1->idnumber));
 207          $this->assertFalse(!$dbcourse);
 208          $this->assertEquals($dbcourse->shortname, $course1->idnumber);
 209          $this->assertEquals($dbcourse->fullname, $course1->idnumber);
 210          $this->assertEquals($dbcourse->summary, $course1->idnumber);
 211  
 212          // Setting a mapping using all the description tags.
 213          $this->imsplugin->set_config('imscoursemapshortname', 'short');
 214          $this->imsplugin->set_config('imscoursemapfullname', 'long');
 215          $this->imsplugin->set_config('imscoursemapsummary', 'full');
 216  
 217          $course2 = new StdClass();
 218          $course2->idnumber = 'id2';
 219          $course2->imsshort = 'description_short2';
 220          $course2->imslong = 'description_long';
 221          $course2->imsfull = 'description_full';
 222          $course2->category = 'DEFAULT CATNAME';
 223  
 224          $this->set_xml_file(false, array($course2));
 225          $this->imsplugin->cron();
 226  
 227          $dbcourse = $DB->get_record('course', array('idnumber' => $course2->idnumber));
 228          $this->assertFalse(!$dbcourse);
 229          $this->assertEquals($dbcourse->shortname, $course2->imsshort);
 230          $this->assertEquals($dbcourse->fullname, $course2->imslong);
 231          $this->assertEquals($dbcourse->summary, $course2->imsfull);
 232  
 233          // Setting a mapping where the specified description tags doesn't exist in the XML file (must delegate into idnumber).
 234          $this->imsplugin->set_config('imscoursemapshortname', 'short');
 235          $this->imsplugin->set_config('imscoursemapfullname', 'long');
 236          $this->imsplugin->set_config('imscoursemapsummary', 'full');
 237  
 238          $course3 = new StdClass();
 239          $course3->idnumber = 'id3';
 240          $course3->imsshort = 'description_short3';
 241          $course3->category = 'DEFAULT CATNAME';
 242  
 243          $this->set_xml_file(false, array($course3));
 244          $this->imsplugin->cron();
 245  
 246          $dbcourse = $DB->get_record('course', array('idnumber' => $course3->idnumber));
 247          $this->assertFalse(!$dbcourse);
 248          $this->assertEquals($dbcourse->shortname, $course3->imsshort);
 249          $this->assertEquals($dbcourse->fullname, $course3->idnumber);
 250          $this->assertEquals($dbcourse->summary, $course3->idnumber);
 251  
 252      }
 253  
 254      /**
 255       * Sets the plugin configuration for testing
 256       */
 257      public function set_test_config() {
 258          $this->imsplugin->set_config('mailadmins', false);
 259          $this->imsplugin->set_config('prev_path', '');
 260          $this->imsplugin->set_config('createnewusers', true);
 261          $this->imsplugin->set_config('createnewcourses', true);
 262          $this->imsplugin->set_config('createnewcategories', true);
 263      }
 264  
 265      /**
 266       * Creates an IMS enterprise XML file and adds it's path to config settings.
 267       *
 268       * @param bool|array $users false or array of users StdClass
 269       * @param bool|array $courses false or of courses StdClass
 270       */
 271      public function set_xml_file($users = false, $courses = false) {
 272  
 273          $xmlcontent = '<enterprise>';
 274  
 275          // Users.
 276          if (!empty($users)) {
 277              foreach ($users as $user) {
 278                  $xmlcontent .= '
 279    <person>
 280      <sourcedid>
 281        <source>TestSource</source>
 282        <id>'.$user->username.'</id>
 283      </sourcedid>
 284      <userid>'.$user->username.'</userid>
 285      <name>
 286        <fn>'.$user->firstname.' '.$user->lastname.'</fn>
 287        <n>
 288          <family>'.$user->lastname.'</family>
 289          <given>'.$user->firstname.'</given>
 290        </n>
 291      </name>
 292      <email>'.$user->email.'</email>
 293    </person>';
 294              }
 295          }
 296  
 297          // Courses.
 298          // Mapping based on default course attributes - IMS group tags mapping.
 299          if (!empty($courses)) {
 300              foreach ($courses as $course) {
 301  
 302                  $xmlcontent .= '
 303    <group>
 304      <sourcedid>
 305        <source>TestSource</source>
 306        <id>'.$course->idnumber.'</id>
 307      </sourcedid>
 308      <description>';
 309  
 310                  // Optional to test course attributes mappings.
 311                  if (!empty($course->imsshort)) {
 312                      $xmlcontent .= '
 313        <short>'.$course->imsshort.'</short>';
 314                  }
 315  
 316                  // Optional to test course attributes mappings.
 317                  if (!empty($course->imslong)) {
 318                      $xmlcontent .= '
 319        <long>'.$course->imslong.'</long>';
 320                  }
 321  
 322                  // Optional to test course attributes mappings.
 323                  if (!empty($course->imsfull)) {
 324                      $xmlcontent .= '
 325        <full>'.$course->imsfull.'</full>';
 326                  }
 327  
 328                  // The orgunit tag value is used by moodle as category name.
 329                  $xmlcontent .= '
 330      </description>
 331      <org>
 332        <orgunit>'.$course->category.'</orgunit>
 333      </org>
 334    </group>';
 335              }
 336          }
 337  
 338          $xmlcontent .= '
 339  </enterprise>';
 340  
 341          // Creating the XML file.
 342          $filename = 'ims_' . rand(1000, 9999) . '.xml';
 343          $tmpdir = make_temp_directory('enrol_imsenterprise');
 344          $xmlfilepath = $tmpdir . '/' . $filename;
 345          file_put_contents($xmlfilepath, $xmlcontent);
 346  
 347          // Setting the file path in CFG.
 348          $this->imsplugin->set_config('imsfilelocation', $xmlfilepath);
 349      }
 350  
 351      /**
 352       * IMS Enterprise enrolment task test.
 353       */
 354      public function test_imsenterprise_cron_task() {
 355          global $DB;
 356          $prevnusers = $DB->count_records('user');
 357  
 358          $user1 = new StdClass();
 359          $user1->username = 'u1';
 360          $user1->email = '[email protected]';
 361          $user1->firstname = 'U';
 362          $user1->lastname = '1';
 363  
 364          $users = array($user1);
 365          $this->set_xml_file($users);
 366  
 367          $task = new enrol_imsenterprise\task\cron_task();
 368          $task->execute();
 369  
 370          $this->assertEquals(($prevnusers + 1), $DB->count_records('user'));
 371      }
 372  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1