[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/enrol/lti/tests/ -> helper_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   * Test the helper functionality.
  19   *
  20   * @package enrol_lti
  21   * @copyright 2016 Mark Nelson <markn@moodle.com>
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Test the helper functionality.
  29   *
  30   * @package enrol_lti
  31   * @copyright 2016 Mark Nelson <markn@moodle.com>
  32   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class enrol_lti_helper_testcase extends advanced_testcase {
  35  
  36      /**
  37       * @var stdClass $user1 A user.
  38       */
  39      public $user1;
  40  
  41      /**
  42       * @var stdClass $user2 A user.
  43       */
  44      public $user2;
  45  
  46      /**
  47       * Test set up.
  48       *
  49       * This is executed before running any test in this file.
  50       */
  51      public function setUp() {
  52          $this->resetAfterTest();
  53  
  54          // Set this user as the admin.
  55          $this->setAdminUser();
  56  
  57          // Get some of the information we need.
  58          $this->user1 = self::getDataGenerator()->create_user();
  59          $this->user2 = self::getDataGenerator()->create_user();
  60      }
  61  
  62      /**
  63       * Test the update user profile image function.
  64       */
  65      public function test_update_user_profile_image() {
  66          global $DB, $CFG;
  67  
  68          // Set the profile image.
  69          \enrol_lti\helper::update_user_profile_image($this->user1->id, $this->getExternalTestFileUrl('/test.jpg'));
  70  
  71          // Get the new user record.
  72          $this->user1 = $DB->get_record('user', array('id' => $this->user1->id));
  73  
  74          // Set the page details.
  75          $page = new moodle_page();
  76          $page->set_url('/user/profile.php');
  77          $page->set_context(context_system::instance());
  78          $renderer = $page->get_renderer('core');
  79          $usercontext = context_user::instance($this->user1->id);
  80  
  81          // Get the user's profile picture and make sure it is correct.
  82          $userpicture = new user_picture($this->user1);
  83          $this->assertSame($CFG->wwwroot . '/pluginfile.php/' . $usercontext->id . '/user/icon/clean/f2?rev=' .$this->user1->picture,
  84              $userpicture->get_url($page, $renderer)->out(false));
  85      }
  86  
  87      /**
  88       * Test that we can not enrol past the maximum number of users allowed.
  89       */
  90      public function test_enrol_user_max_enrolled() {
  91          global $DB;
  92  
  93          // Set up the LTI enrolment tool.
  94          $data = new stdClass();
  95          $data->maxenrolled = 1;
  96          $tool = $this->create_tool($data);
  97  
  98          // Now get all the information we need.
  99          $tool = \enrol_lti\helper::get_lti_tool($tool->id);
 100  
 101          // Enrol a user.
 102          $result = \enrol_lti\helper::enrol_user($tool, $this->user1->id);
 103  
 104          // Check that the user was enrolled.
 105          $this->assertEquals(true, $result);
 106          $this->assertEquals(1, $DB->count_records('user_enrolments', array('enrolid' => $tool->enrolid)));
 107  
 108          // Try and enrol another user - should not happen.
 109          $result = \enrol_lti\helper::enrol_user($tool, $this->user2->id);
 110  
 111          // Check that this user was not enrolled and we are told why.
 112          $this->assertEquals(\enrol_lti\helper::ENROLMENT_MAX_ENROLLED, $result);
 113          $this->assertEquals(1, $DB->count_records('user_enrolments', array('enrolid' => $tool->enrolid)));
 114      }
 115  
 116      /**
 117       * Test that we can not enrol when the enrolment has not started.
 118       */
 119      public function test_enrol_user_enrolment_not_started() {
 120          global $DB;
 121  
 122          // Set up the LTI enrolment tool.
 123          $data = new stdClass();
 124          $data->enrolstartdate = time() + DAYSECS; // Make sure it is in the future.
 125          $tool = $this->create_tool($data);
 126  
 127          // Now get all the information we need.
 128          $tool = \enrol_lti\helper::get_lti_tool($tool->id);
 129  
 130          // Try and enrol a user - should not happen.
 131          $result = \enrol_lti\helper::enrol_user($tool, $this->user1->id);
 132  
 133          // Check that this user was not enrolled and we are told why.
 134          $this->assertEquals(\enrol_lti\helper::ENROLMENT_NOT_STARTED, $result);
 135          $this->assertEquals(0, $DB->count_records('user_enrolments', array('enrolid' => $tool->enrolid)));
 136      }
 137  
 138      /**
 139       * Test that we can not enrol when the enrolment has finished.
 140       */
 141      public function test_enrol_user_enrolment_finished() {
 142          global $DB;
 143  
 144          // Set up the LTI enrolment tool.
 145          $data = new stdClass();
 146          $data->enrolenddate = time() - DAYSECS; // Make sure it is in the past.
 147          $tool = $this->create_tool($data);
 148  
 149          // Now get all the information we need.
 150          $tool = \enrol_lti\helper::get_lti_tool($tool->id);
 151  
 152          // Try and enrol a user - should not happen.
 153          $result = \enrol_lti\helper::enrol_user($tool, $this->user1->id);
 154  
 155          // Check that this user was not enrolled and we are told why.
 156          $this->assertEquals(\enrol_lti\helper::ENROLMENT_FINISHED, $result);
 157          $this->assertEquals(0, $DB->count_records('user_enrolments', array('enrolid' => $tool->enrolid)));
 158      }
 159  
 160      /**
 161       * Test returning the number of available tools.
 162       */
 163      public function test_count_lti_tools() {
 164          // Create two tools belonging to the same course.
 165          $course1 = $this->getDataGenerator()->create_course();
 166          $data = new stdClass();
 167          $data->courseid = $course1->id;
 168          $this->create_tool($data);
 169          $this->create_tool($data);
 170  
 171          // Create two more tools in a separate course.
 172          $course2 = $this->getDataGenerator()->create_course();
 173          $data = new stdClass();
 174          $data->courseid = $course2->id;
 175          $this->create_tool($data);
 176  
 177          // Set the next tool to disabled.
 178          $data->status = ENROL_INSTANCE_DISABLED;
 179          $this->create_tool($data);
 180  
 181          // Count all the tools.
 182          $count = \enrol_lti\helper::count_lti_tools();
 183          $this->assertEquals(4, $count);
 184  
 185          // Count all the tools in course 1.
 186          $count = \enrol_lti\helper::count_lti_tools(array('courseid' => $course1->id));
 187          $this->assertEquals(2, $count);
 188  
 189          // Count all the tools in course 2 that are disabled.
 190          $count = \enrol_lti\helper::count_lti_tools(array('courseid' => $course2->id, 'status' => ENROL_INSTANCE_DISABLED));
 191          $this->assertEquals(1, $count);
 192  
 193          // Count all the tools that are enabled.
 194          $count = \enrol_lti\helper::count_lti_tools(array('status' => ENROL_INSTANCE_ENABLED));
 195          $this->assertEquals(3, $count);
 196      }
 197  
 198      /**
 199       * Test returning the list of available tools.
 200       */
 201      public function test_get_lti_tools() {
 202          // Create two tools belonging to the same course.
 203          $course1 = $this->getDataGenerator()->create_course();
 204          $data = new stdClass();
 205          $data->courseid = $course1->id;
 206          $tool1 = $this->create_tool($data);
 207          $tool2 = $this->create_tool($data);
 208  
 209          // Create two more tools in a separate course.
 210          $course2 = $this->getDataGenerator()->create_course();
 211          $data = new stdClass();
 212          $data->courseid = $course2->id;
 213          $tool3 = $this->create_tool($data);
 214  
 215          // Set the next tool to disabled.
 216          $data->status = ENROL_INSTANCE_DISABLED;
 217          $tool4 = $this->create_tool($data);
 218  
 219          // Get all the tools.
 220          $tools = \enrol_lti\helper::get_lti_tools();
 221  
 222          // Check that we got all the tools.
 223          $this->assertEquals(4, count($tools));
 224  
 225          // Get all the tools in course 1.
 226          $tools = \enrol_lti\helper::get_lti_tools(array('courseid' => $course1->id));
 227  
 228          // Check that we got all the tools in course 1.
 229          $this->assertEquals(2, count($tools));
 230          $this->assertTrue(isset($tools[$tool1->id]));
 231          $this->assertTrue(isset($tools[$tool2->id]));
 232  
 233          // Get all the tools in course 2 that are disabled.
 234          $tools = \enrol_lti\helper::get_lti_tools(array('courseid' => $course2->id, 'status' => ENROL_INSTANCE_DISABLED));
 235  
 236          // Check that we got all the tools in course 2 that are disabled.
 237          $this->assertEquals(1, count($tools));
 238          $this->assertTrue(isset($tools[$tool4->id]));
 239  
 240          // Get all the tools that are enabled.
 241          $tools = \enrol_lti\helper::get_lti_tools(array('status' => ENROL_INSTANCE_ENABLED));
 242  
 243          // Check that we got all the tools that are enabled.
 244          $this->assertEquals(3, count($tools));
 245          $this->assertTrue(isset($tools[$tool1->id]));
 246          $this->assertTrue(isset($tools[$tool2->id]));
 247          $this->assertTrue(isset($tools[$tool3->id]));
 248      }
 249  
 250      /**
 251       * Helper function used to create a tool.
 252       *
 253       * @param array $data
 254       * @return stdClass the tool
 255       */
 256      protected function create_tool($data = array()) {
 257          global $DB;
 258  
 259          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
 260          $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
 261  
 262          // Create a course if no course id was specified.
 263          if (empty($data->courseid)) {
 264              $course = $this->getDataGenerator()->create_course();
 265              $data->courseid = $course->id;
 266          } else {
 267              $course = get_course($data->courseid);
 268          }
 269  
 270          // Set it to enabled if no status was specified.
 271          if (!isset($data->status)) {
 272              $data->status = ENROL_INSTANCE_ENABLED;
 273          }
 274  
 275          // Add some extra necessary fields to the data.
 276          $data->name = 'Test LTI';
 277          $data->contextid = context_course::instance($data->courseid)->id;
 278          $data->roleinstructor = $studentrole->id;
 279          $data->rolelearner = $teacherrole->id;
 280  
 281          // Get the enrol LTI plugin.
 282          $enrolplugin = enrol_get_plugin('lti');
 283          $instanceid = $enrolplugin->add_instance($course, (array) $data);
 284  
 285          // Get the tool associated with this instance.
 286          return $DB->get_record('enrol_lti_tools', array('enrolid' => $instanceid));
 287      }
 288  }


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