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