See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]
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 * Asserts a matching gradebookservices record exist with the matching tag and resourceid. 155 * 156 * @param object $course current course 157 * @param int $typeid Type id of the tool 158 * @param string $label Label of the line item 159 * @param object|null $ltiinstance lti instance related to that line item 160 * @param string|null $resourceid resourceid the line item should have 161 * @param string|null $tag tag the line item should have 162 */ 163 private function assert_lineitems(object $course, int $typeid, 164 string $label, ?object $ltiinstance, ?string $resourceid, ?string $tag) : void { 165 $gbservice = new gradebookservices(); 166 $gradeitems = $gbservice->get_lineitems($course->id, null, null, null, null, null, $typeid); 167 168 // The 1st item in the array is the items count. 169 $this->assertEquals(1, $gradeitems[0]); 170 171 $lineitem = gradebookservices::item_for_json($gradeitems[1][0], '', $typeid); 172 $this->assertEquals(10, $lineitem->scoreMaximum); 173 $this->assertEquals($resourceid, $lineitem->resourceId); 174 $this->assertEquals($tag, $lineitem->tag); 175 $this->assertEquals($label, $lineitem->label); 176 177 $gradeitems = $gbservice->get_lineitems($course->id, $resourceid, null, null, null, null, $typeid); 178 $this->assertEquals(1, $gradeitems[0]); 179 180 if (isset($ltiinstance)) { 181 $gradeitems = $gbservice->get_lineitems($course->id, null, $ltiinstance->id, null, null, null, $typeid); 182 $this->assertEquals(1, $gradeitems[0]); 183 $gradeitems = $gbservice->get_lineitems($course->id, null, $ltiinstance->id + 1, null, null, null, $typeid); 184 $this->assertEquals(0, $gradeitems[0]); 185 } 186 187 $gradeitems = $gbservice->get_lineitems($course->id, null, null, $tag, null, null, $typeid); 188 $this->assertEquals(1, $gradeitems[0]); 189 190 $gradeitems = $gbservice->get_lineitems($course->id, 'an unknown resource id', null, null, null, null, $typeid); 191 $this->assertEquals(0, $gradeitems[0]); 192 193 $gradeitems = $gbservice->get_lineitems($course->id, null, null, 'an unknown tag', null, null, $typeid); 194 $this->assertEquals(0, $gradeitems[0]); 195 } 196 197 /** 198 * Inserts a graded lti instance, which should create a grade_item and gradebookservices record. 199 * 200 * @param int $typeid Type ID of the LTI Tool. 201 * @param object $course course where to add the lti instance. 202 * @param string|null $resourceid resource id 203 * @param string|null $tag tag 204 * 205 * @return object lti instance created 206 */ 207 private function create_graded_lti(int $typeid, object $course, ?string $resourceid, ?string $tag) : object { 208 209 $lti = ['course' => $course->id, 210 'typeid' => $typeid, 211 'instructorchoiceacceptgrades' => LTI_SETTING_ALWAYS, 212 'grade' => 10, 213 'lineitemresourceid' => $resourceid, 214 'lineitemtag' => $tag]; 215 216 return $this->getDataGenerator()->create_module('lti', $lti, array()); 217 } 218 219 /** 220 * Inserts an lti instance that is not graded. 221 * 222 * @param int $typeid Type Id of the LTI Tool. 223 * @param object $course course where to add the lti instance. 224 * 225 * @return object lti instance created 226 */ 227 private function create_notgraded_lti(int $typeid, object $course) : object { 228 229 $lti = ['course' => $course->id, 230 'typeid' => $typeid, 231 'instructorchoiceacceptgrades' => LTI_SETTING_NEVER]; 232 233 return $this->getDataGenerator()->create_module('lti', $lti, array()); 234 } 235 236 /** 237 * Inserts a standalone lineitem (gradeitem, gradebookservices entries). 238 * 239 * @param int $courseid Id of the course where the standalone line item will be added. 240 * @param int $typeid of the LTI Tool 241 * @param string|null $resourceid resource id 242 * @param string|null $tag tag 243 * @param int|null $ltiinstanceid Id of the LTI instance the standalone line item will be related to. 244 * 245 */ 246 private function create_standalone_lineitem(int $courseid, int $typeid, ?string $resourceid, 247 ?string $tag, int $ltiinstanceid = null) : void { 248 $gbservice = new gradebookservices(); 249 $gbservice->add_standalone_lineitem($courseid, 250 "manualtest", 251 10, 252 "https://test.phpunit", 253 $ltiinstanceid, 254 $resourceid, 255 $tag, 256 $typeid, 257 null /*toolproxyid*/); 258 } 259 260 /** 261 * Creates a new LTI Tool Type. 262 */ 263 private function create_type() { 264 $type = new stdClass(); 265 $type->state = LTI_TOOL_STATE_CONFIGURED; 266 $type->name = "Test tool"; 267 $type->description = "Example description"; 268 $type->clientid = "Test client ID"; 269 $type->baseurl = $this->getExternalTestFileUrl('/test.html'); 270 271 $config = new stdClass(); 272 $config->ltiservice_gradesynchronization = 2; 273 return lti_add_type($type, $config); 274 } 275 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body