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   * Unit tests for mod_lti gradebookservices
  19   * @package    ltiservice_gradebookservices
  20   * @category   external
  21   * @copyright  2020 Claude Vervoort <claude.vervoort@cengage.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  use ltiservice_gradebookservices\local\service\gradebookservices;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * Unit tests for lti gradebookservices.
  30   */
  31  class mod_lti_gradebookservices_testcase extends advanced_testcase {
  32  
  33      /**
  34       * Test saving a graded LTI with resource and tag info (as a result of
  35       * content item selection) creates a gradebookservices record
  36       * that can be retrieved using the gradebook service API.
  37       */
  38      public function test_lti_add_coupled_lineitem() {
  39          global $CFG;
  40          require_once($CFG->dirroot . '/mod/lti/locallib.php');
  41  
  42          $this->resetAfterTest();
  43          $this->setAdminUser();
  44  
  45          // Create a tool type, associated with that proxy.
  46  
  47          $typeid = $this->create_type();
  48          $course = $this->getDataGenerator()->create_course();
  49          $resourceid = 'test-resource-id';
  50          $tag = 'tag';
  51  
  52          $ltiinstance = $this->create_graded_lti($typeid, $course, $resourceid, $tag);
  53  
  54          $this->assertNotNull($ltiinstance);
  55  
  56          $gbs = gradebookservices::find_ltiservice_gradebookservice_for_lti($ltiinstance->id);
  57  
  58          $this->assertNotNull($gbs);
  59          $this->assertEquals($resourceid, $gbs->resourceid);
  60          $this->assertEquals($tag, $gbs->tag);
  61  
  62          $this->assert_lineitems($course, $typeid, $ltiinstance->name, $ltiinstance, $resourceid, $tag);
  63      }
  64  
  65      /**
  66       * Test saving a standalone LTI lineitem with resource and tag info
  67       * that can be retrieved using the gradebook service API.
  68       */
  69      public function test_lti_add_standalone_lineitem() {
  70          $this->resetAfterTest();
  71          $this->setAdminUser();
  72  
  73          $course = $this->getDataGenerator()->create_course();
  74          $resourceid = "test-resource-standalone";
  75          $tag = "test-tag-standalone";
  76          $typeid = $this->create_type();
  77  
  78          $this->create_standalone_lineitem($course->id, $typeid, $resourceid, $tag);
  79  
  80          $this->assert_lineitems($course, $typeid, "manualtest", null, $resourceid, $tag);
  81      }
  82  
  83      /**
  84       * Test line item URL is populated for coupled line item only
  85       * if there is not another line item bound to the lti instance,
  86       * since in that case there would be no rule to define which of
  87       * the line items should be actually passed.
  88       */
  89      public function test_get_launch_parameters_coupled() {
  90          global $CFG;
  91          require_once($CFG->dirroot . '/mod/lti/locallib.php');
  92  
  93          $this->resetAfterTest();
  94          $this->setAdminUser();
  95  
  96          // Create a tool type, associated with that proxy.
  97  
  98          $typeid = $this->create_type();
  99          $course = $this->getDataGenerator()->create_course();
 100  
 101          $ltiinstance = $this->create_graded_lti($typeid, $course, 'resource-id', 'tag');
 102  
 103          $this->assertNotNull($ltiinstance);
 104  
 105          $gbservice = new gradebookservices();
 106          $params = $gbservice->get_launch_parameters('basic-lti-launch-request', $course->id, 111, $typeid, $ltiinstance->id);
 107          $this->assertEquals('$LineItem.url', $params['lineitem_url']);
 108          $this->assertEquals('$LineItem.url', $params['lineitem_url']);
 109  
 110          $this->create_standalone_lineitem($course->id, $typeid, 'resource-id', 'tag', $ltiinstance->id);
 111          $params = $gbservice->get_launch_parameters('basic-lti-launch-request', $course->id, 111, $typeid, $ltiinstance->id);
 112          $this->assertEquals('$LineItems.url', $params['lineitems_url']);
 113          // 2 line items for a single link, we cannot return a single line item url.
 114          $this->assertFalse(array_key_exists('$LineItem.url', $params));
 115      }
 116  
 117      /**
 118       * Test line item URL is populated for not coupled line item only
 119       * if there is a single line item attached to that lti instance.
 120       */
 121      public function test_get_launch_parameters_decoupled() {
 122          global $CFG;
 123          require_once($CFG->dirroot . '/mod/lti/locallib.php');
 124  
 125          $this->resetAfterTest();
 126          $this->setAdminUser();
 127  
 128          // Create a tool type, associated with that proxy.
 129  
 130          $typeid = $this->create_type();
 131  
 132          $course = $this->getDataGenerator()->create_course();
 133  
 134          $ltiinstance = $this->create_notgraded_lti($typeid, $course);
 135  
 136          $this->assertNotNull($ltiinstance);
 137  
 138          $gbservice = new gradebookservices();
 139          $params = $gbservice->get_launch_parameters('basic-lti-launch-request', $course->id, 111, $typeid, $ltiinstance->id);
 140          $this->assertEquals('$LineItems.url', $params['lineitems_url']);
 141          $this->assertFalse(array_key_exists('$LineItem.url', $params));
 142  
 143          $this->create_standalone_lineitem($course->id, $typeid, 'resource-id', 'tag', $ltiinstance->id);
 144          $params = $gbservice->get_launch_parameters('basic-lti-launch-request', $course->id, 111, $typeid, $ltiinstance->id);
 145          $this->assertEquals('$LineItems.url', $params['lineitems_url']);
 146          $this->assertEquals('$LineItem.url', $params['lineitem_url']);
 147  
 148          // 2 line items for a single link, we cannot return a single line item url.
 149          $this->create_standalone_lineitem($course->id, $typeid, 'resource-id', 'tag-2', $ltiinstance->id);
 150          $this->assertFalse(array_key_exists('$LineItem.url', $params));
 151      }
 152  
 153      /**
 154       * Test if a user can be graded in a course.
 155       */
 156      public function test_is_user_gradable_in_course() {
 157          $this->resetAfterTest();
 158  
 159          $generator = $this->getDataGenerator();
 160          $course = $generator->create_course();
 161          $user1 = $generator->create_user();
 162          $user2 = $generator->create_user();
 163          $generator->enrol_user($user1->id, $course->id, 'student');
 164          $generator->enrol_user($user2->id, $course->id, 'editingteacher');
 165  
 166          $this->assertTrue(gradebookservices::is_user_gradable_in_course($course->id, $user1->id));
 167          $this->assertFalse(gradebookservices::is_user_gradable_in_course($course->id, $user2->id));
 168      }
 169  
 170      /**
 171       * Asserts a matching gradebookservices record exist with the matching tag and resourceid.
 172       *
 173       * @param object $course current course
 174       * @param int $typeid Type id of the tool
 175       * @param string $label Label of the line item
 176       * @param object|null $ltiinstance lti instance related to that line item
 177       * @param string|null $resourceid resourceid the line item should have
 178       * @param string|null $tag tag the line item should have
 179       */
 180      private function assert_lineitems(object $course, int $typeid,
 181              string $label, ?object $ltiinstance, ?string $resourceid, ?string $tag) : void {
 182          $gbservice = new gradebookservices();
 183          $gradeitems = $gbservice->get_lineitems($course->id, null, null, null, null, null, $typeid);
 184  
 185          // The 1st item in the array is the items count.
 186          $this->assertEquals(1, $gradeitems[0]);
 187  
 188          $lineitem = gradebookservices::item_for_json($gradeitems[1][0], '', $typeid);
 189          $this->assertEquals(10, $lineitem->scoreMaximum);
 190          $this->assertEquals($resourceid, $lineitem->resourceId);
 191          $this->assertEquals($tag, $lineitem->tag);
 192          $this->assertEquals($label, $lineitem->label);
 193  
 194          $gradeitems = $gbservice->get_lineitems($course->id, $resourceid, null, null, null, null, $typeid);
 195          $this->assertEquals(1, $gradeitems[0]);
 196  
 197          if (isset($ltiinstance)) {
 198              $gradeitems = $gbservice->get_lineitems($course->id, null, $ltiinstance->id, null, null, null, $typeid);
 199              $this->assertEquals(1, $gradeitems[0]);
 200              $gradeitems = $gbservice->get_lineitems($course->id, null, $ltiinstance->id + 1, null, null, null, $typeid);
 201              $this->assertEquals(0, $gradeitems[0]);
 202          }
 203  
 204          $gradeitems = $gbservice->get_lineitems($course->id, null, null, $tag, null, null, $typeid);
 205          $this->assertEquals(1, $gradeitems[0]);
 206  
 207          $gradeitems = $gbservice->get_lineitems($course->id, 'an unknown resource id', null, null, null, null, $typeid);
 208          $this->assertEquals(0, $gradeitems[0]);
 209  
 210          $gradeitems = $gbservice->get_lineitems($course->id, null, null, 'an unknown tag', null, null, $typeid);
 211          $this->assertEquals(0, $gradeitems[0]);
 212      }
 213  
 214      /**
 215       * Inserts a graded lti instance, which should create a grade_item and gradebookservices record.
 216       *
 217       * @param int $typeid Type ID of the LTI Tool.
 218       * @param object $course course where to add the lti instance.
 219       * @param string|null $resourceid resource id
 220       * @param string|null $tag tag
 221       *
 222       * @return object lti instance created
 223       */
 224      private function create_graded_lti(int $typeid, object $course, ?string $resourceid, ?string $tag) : object {
 225  
 226          $lti = ['course' => $course->id,
 227              'typeid' => $typeid,
 228              'instructorchoiceacceptgrades' => LTI_SETTING_ALWAYS,
 229              'grade' => 10,
 230              'lineitemresourceid' => $resourceid,
 231              'lineitemtag' => $tag];
 232  
 233          return $this->getDataGenerator()->create_module('lti', $lti, array());
 234      }
 235  
 236       /**
 237        * Inserts an lti instance that is not graded.
 238        *
 239        * @param int $typeid Type Id of the LTI Tool.
 240        * @param object $course course where to add the lti instance.
 241        *
 242        * @return object lti instance created
 243        */
 244      private function create_notgraded_lti(int $typeid, object $course) : object {
 245  
 246          $lti = ['course' => $course->id,
 247              'typeid' => $typeid,
 248              'instructorchoiceacceptgrades' => LTI_SETTING_NEVER];
 249  
 250          return $this->getDataGenerator()->create_module('lti', $lti, array());
 251      }
 252  
 253      /**
 254       * Inserts a standalone lineitem (gradeitem, gradebookservices entries).
 255       *
 256       * @param int $courseid Id of the course where the standalone line item will be added.
 257       * @param int $typeid of the LTI Tool
 258       * @param string|null $resourceid resource id
 259       * @param string|null $tag tag
 260       * @param int|null $ltiinstanceid Id of the LTI instance the standalone line item will be related to.
 261       *
 262       */
 263      private function create_standalone_lineitem(int $courseid, int $typeid, ?string $resourceid,
 264              ?string $tag, int $ltiinstanceid = null) : void {
 265          $gbservice = new gradebookservices();
 266          $gbservice->add_standalone_lineitem($courseid,
 267              "manualtest",
 268              10,
 269              "https://test.phpunit",
 270              $ltiinstanceid,
 271              $resourceid,
 272              $tag,
 273              $typeid,
 274              null /*toolproxyid*/);
 275      }
 276  
 277      /**
 278       * Creates a new LTI Tool Type.
 279       */
 280      private function create_type() {
 281          $type = new stdClass();
 282          $type->state = LTI_TOOL_STATE_CONFIGURED;
 283          $type->name = "Test tool";
 284          $type->description = "Example description";
 285          $type->clientid = "Test client ID";
 286          $type->baseurl = $this->getExternalTestFileUrl('/test.html');
 287  
 288          $config = new stdClass();
 289          $config->ltiservice_gradesynchronization = 2;
 290          return lti_add_type($type, $config);
 291      }
 292  }