Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

< /** < * Unit tests for mod_lti gradebookservices < * @package ltiservice_gradebookservices < * @category external < * @copyright 2020 Claude Vervoort <claude.vervoort@cengage.com> < * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later < */ < use ltiservice_gradebookservices\local\service\gradebookservices;
> namespace ltiservice_gradebookservices;
< defined('MOODLE_INTERNAL') || die();
> use ltiservice_gradebookservices\local\service\gradebookservices;
/** * Unit tests for lti gradebookservices.
> * */ > * @package ltiservice_gradebookservices class mod_lti_gradebookservices_testcase extends advanced_testcase { > * @category test > * @copyright 2020 Claude Vervoort <claude.vervoort@cengage.com> /** > * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * Test saving a graded LTI with resource and tag info (as a result of > * @coversDefaultClass \mod_lti\service\gradebookservices\local\gradebookservices
< class mod_lti_gradebookservices_testcase extends advanced_testcase {
> class gradebookservices_test extends \advanced_testcase {
* that can be retrieved using the gradebook service API.
> * @covers ::instance_added */ > *
public function test_lti_add_coupled_lineitem() { global $CFG; require_once($CFG->dirroot . '/mod/lti/locallib.php'); $this->resetAfterTest(); $this->setAdminUser(); // Create a tool type, associated with that proxy. $typeid = $this->create_type(); $course = $this->getDataGenerator()->create_course(); $resourceid = 'test-resource-id'; $tag = 'tag';
> $subreviewurl = 'https://subreview.example.com'; > $subreviewparams = 'a=2';
< $ltiinstance = $this->create_graded_lti($typeid, $course, $resourceid, $tag);
> $ltiinstance = $this->create_graded_lti($typeid, $course, $resourceid, $tag, $subreviewurl, $subreviewparams);
$this->assertNotNull($ltiinstance); $gbs = gradebookservices::find_ltiservice_gradebookservice_for_lti($ltiinstance->id); $this->assertNotNull($gbs); $this->assertEquals($resourceid, $gbs->resourceid); $this->assertEquals($tag, $gbs->tag);
> $this->assertEquals($subreviewurl, $gbs->subreviewurl); > $this->assertEquals($subreviewparams, $gbs->subreviewparams); $this->assert_lineitems($course, $typeid, $ltiinstance->name, $ltiinstance, $resourceid, $tag); > $this->assert_lineitems($course, $typeid, $ltiinstance->name, } > $ltiinstance, $resourceid, $tag, $subreviewurl, $subreviewparams); > } /** > * Test saving a standalone LTI lineitem with resource and tag info > /** * that can be retrieved using the gradebook service API. > * @covers ::instance_added */ > * public function test_lti_add_standalone_lineitem() { > * Test saving a graded LTI with resource and tag info (as a result of $this->resetAfterTest(); > * content item selection) creates a gradebookservices record $this->setAdminUser(); > * that can be retrieved using the gradebook service API. > */ $course = $this->getDataGenerator()->create_course(); > public function test_lti_add_coupled_lineitem_default_subreview() { $resourceid = "test-resource-standalone"; > global $CFG; $tag = "test-tag-standalone"; > require_once($CFG->dirroot . '/mod/lti/locallib.php');
< $this->assert_lineitems($course, $typeid, $ltiinstance->name, $ltiinstance, $resourceid, $tag);
> $this->resetAfterTest(); > $this->setAdminUser(); > > // Create a tool type, associated with that proxy. > > $typeid = $this->create_type(); > $course = $this->getDataGenerator()->create_course(); > $resourceid = 'test-resource-id'; > $tag = 'tag'; > > $ltiinstance = $this->create_graded_lti($typeid, $course, $resourceid, $tag, 'DEFAULT'); > > $this->assertNotNull($ltiinstance); > > $gbs = gradebookservices::find_ltiservice_gradebookservice_for_lti($ltiinstance->id); > > $this->assertNotNull($gbs); > $this->assertEquals('DEFAULT', $gbs->subreviewurl); > $this->assert_lineitems($course, $typeid, $ltiinstance->name, $ltiinstance, $resourceid, $tag, 'DEFAULT');
> * @covers ::add_standalone_lineitem $this->create_standalone_lineitem($course->id, $typeid, $resourceid, $tag); > *
$this->assert_lineitems($course, $typeid, "manualtest", null, $resourceid, $tag); } /**
> * @covers ::find_ltiservice_gradebookservice_for_lti * Test line item URL is populated for coupled line item only > *
* if there is not another line item bound to the lti instance, * since in that case there would be no rule to define which of * the line items should be actually passed. */ public function test_get_launch_parameters_coupled() { global $CFG; require_once($CFG->dirroot . '/mod/lti/locallib.php'); $this->resetAfterTest(); $this->setAdminUser(); // Create a tool type, associated with that proxy. $typeid = $this->create_type(); $course = $this->getDataGenerator()->create_course();
< $ltiinstance = $this->create_graded_lti($typeid, $course, 'resource-id', 'tag');
> $ltiinstance = $this->create_graded_lti($typeid, $course, 'resource-id', 'tag', 'https://subreview.url', 'sub=review');
$this->assertNotNull($ltiinstance); $gbservice = new gradebookservices(); $params = $gbservice->get_launch_parameters('basic-lti-launch-request', $course->id, 111, $typeid, $ltiinstance->id); $this->assertEquals('$LineItem.url', $params['lineitem_url']); $this->assertEquals('$LineItem.url', $params['lineitem_url']); $this->create_standalone_lineitem($course->id, $typeid, 'resource-id', 'tag', $ltiinstance->id); $params = $gbservice->get_launch_parameters('basic-lti-launch-request', $course->id, 111, $typeid, $ltiinstance->id); $this->assertEquals('$LineItems.url', $params['lineitems_url']); // 2 line items for a single link, we cannot return a single line item url. $this->assertFalse(array_key_exists('$LineItem.url', $params)); } /**
> * @covers ::override_endpoint * Test line item URL is populated for not coupled line item only > * * if there is a single line item attached to that lti instance. > * Test Submission Review URL and custom parameter is applied when the */ > * launch is submission review. public function test_get_launch_parameters_decoupled() { > */ global $CFG; > public function test_get_launch_parameters_coupled_subreview_override() { require_once($CFG->dirroot . '/mod/lti/locallib.php'); > global $CFG; > require_once($CFG->dirroot . '/mod/lti/locallib.php'); $this->resetAfterTest(); > $this->setAdminUser(); > $this->resetAfterTest(); > $this->setAdminUser(); // Create a tool type, associated with that proxy. > > // Create a tool type, associated with that proxy. $typeid = $this->create_type(); > > $typeid = $this->create_type(); $course = $this->getDataGenerator()->create_course(); > $course = $this->getDataGenerator()->create_course(); > $ltiinstance = $this->create_notgraded_lti($typeid, $course); > $ltiinstance = $this->create_graded_lti($typeid, $course, 'resource-id', 'tag', > 'https://example.com/subreview', 'action=review'); $this->assertNotNull($ltiinstance); > > $this->assertNotNull($ltiinstance); $gbservice = new gradebookservices(); > $params = $gbservice->get_launch_parameters('basic-lti-launch-request', $course->id, 111, $typeid, $ltiinstance->id); > $gbservice = new gradebookservices(); $this->assertEquals('$LineItems.url', $params['lineitems_url']); > $overrides = $gbservice->override_endpoint('LtiSubmissionReviewRequest', 'https://example.com/lti', $this->assertFalse(array_key_exists('$LineItem.url', $params)); > "color=blue", $course->id, $ltiinstance); > $this->create_standalone_lineitem($course->id, $typeid, 'resource-id', 'tag', $ltiinstance->id); > $this->assertEquals('https://example.com/subreview', $overrides[0]); $params = $gbservice->get_launch_parameters('basic-lti-launch-request', $course->id, 111, $typeid, $ltiinstance->id); > $this->assertEquals("color=blue\naction=review", $overrides[1]); $this->assertEquals('$LineItems.url', $params['lineitems_url']); > } $this->assertEquals('$LineItem.url', $params['lineitem_url']); > > /** // 2 line items for a single link, we cannot return a single line item url. > * @covers ::override_endpoint $this->create_standalone_lineitem($course->id, $typeid, 'resource-id', 'tag-2', $ltiinstance->id); > * $this->assertFalse(array_key_exists('$LineItem.url', $params)); > * Test Submission Review URL and custom parameter is applied when the } > * launch is submission review. > */ /** > public function test_get_launch_parameters_coupled_subreview_override_default() { * Asserts a matching gradebookservices record exist with the matching tag and resourceid. > global $CFG; * > require_once($CFG->dirroot . '/mod/lti/locallib.php'); * @param object $course current course > * @param int $typeid Type id of the tool > $this->resetAfterTest(); * @param string $label Label of the line item > $this->setAdminUser(); * @param object|null $ltiinstance lti instance related to that line item > * @param string|null $resourceid resourceid the line item should have > // Create a tool type, associated with that proxy. * @param string|null $tag tag the line item should have > */ > $typeid = $this->create_type(); private function assert_lineitems(object $course, int $typeid, > $course = $this->getDataGenerator()->create_course(); string $label, ?object $ltiinstance, ?string $resourceid, ?string $tag) : void { > $gbservice = new gradebookservices(); > $ltiinstance = $this->create_graded_lti($typeid, $course, 'resource-id', 'tag', $gradeitems = $gbservice->get_lineitems($course->id, null, null, null, null, null, $typeid); > 'DEFAULT', ''); > // The 1st item in the array is the items count. > $this->assertNotNull($ltiinstance); $this->assertEquals(1, $gradeitems[0]); > > $gbservice = new gradebookservices(); $lineitem = gradebookservices::item_for_json($gradeitems[1][0], '', $typeid); > $overrides = $gbservice->override_endpoint('LtiSubmissionReviewRequest', 'https://example.com/lti', $this->assertEquals(10, $lineitem->scoreMaximum); > "color=blue", $course->id, $ltiinstance); $this->assertEquals($resourceid, $lineitem->resourceId); > $this->assertEquals($tag, $lineitem->tag); > $this->assertEquals('https://example.com/lti', $overrides[0]); $this->assertEquals($label, $lineitem->label); > $this->assertEquals("color=blue", $overrides[1]); > } $gradeitems = $gbservice->get_lineitems($course->id, $resourceid, null, null, null, null, $typeid); > $this->assertEquals(1, $gradeitems[0]); > /** > * @covers ::get_launch_parameters if (isset($ltiinstance)) { > *
$gradeitems = $gbservice->get_lineitems($course->id, null, $ltiinstance->id, null, null, null, $typeid);
> * @covers ::is_user_gradable_in_course $this->assertEquals(1, $gradeitems[0]); > * $gradeitems = $gbservice->get_lineitems($course->id, null, $ltiinstance->id + 1, null, null, null, $typeid); > * Test if a user can be graded in a course. $this->assertEquals(0, $gradeitems[0]); > */ } > public function test_is_user_gradable_in_course() { > $this->resetAfterTest(); $gradeitems = $gbservice->get_lineitems($course->id, null, null, $tag, null, null, $typeid); > $this->assertEquals(1, $gradeitems[0]); > $generator = $this->getDataGenerator(); > $course = $generator->create_course(); $gradeitems = $gbservice->get_lineitems($course->id, 'an unknown resource id', null, null, null, null, $typeid); > $user1 = $generator->create_user(); $this->assertEquals(0, $gradeitems[0]); > $user2 = $generator->create_user(); > $generator->enrol_user($user1->id, $course->id, 'student'); $gradeitems = $gbservice->get_lineitems($course->id, null, null, 'an unknown tag', null, null, $typeid); > $generator->enrol_user($user2->id, $course->id, 'editingteacher'); $this->assertEquals(0, $gradeitems[0]); > } > $this->assertTrue(gradebookservices::is_user_gradable_in_course($course->id, $user1->id)); > $this->assertFalse(gradebookservices::is_user_gradable_in_course($course->id, $user2->id)); /** > } * Inserts a graded lti instance, which should create a grade_item and gradebookservices record. > * > /**
* @param int $typeid Type ID of the LTI Tool.
> * @param string|null $subreviewurl submission review url * @param object $course course where to add the lti instance. > * @param string|null $subreviewparams submission review custom params
< string $label, ?object $ltiinstance, ?string $resourceid, ?string $tag) : void {
> string $label, ?object $ltiinstance, ?string $resourceid, ?string $tag, > ?string $subreviewurl = null, ?string $subreviewparams = null) : void {
<
*
> $this->assertEquals(!empty($subreviewurl), isset($lineitem->submissionReview)); * @return object lti instance created > if ($subreviewurl) { */ > if ($subreviewurl == 'DEFAULT') { private function create_graded_lti(int $typeid, object $course, ?string $resourceid, ?string $tag) : object { > $this->assertFalse(isset($lineitem->submissionReview->url)); > } else { $lti = ['course' => $course->id, > $this->assertEquals($subreviewurl, $lineitem->submissionReview->url); 'typeid' => $typeid, > } 'instructorchoiceacceptgrades' => LTI_SETTING_ALWAYS, > if ($subreviewparams) { 'grade' => 10, > $custom = $lineitem->submissionReview->custom; 'lineitemresourceid' => $resourceid, > $this->assertEquals($subreviewparams, join("\n", array_map(fn($k) => $k.'='.$custom[$k], array_keys($custom)))); 'lineitemtag' => $tag]; > } else { > $this->assertFalse(isset($lineitem->submissionReview->custom)); return $this->getDataGenerator()->create_module('lti', $lti, array()); > } } > }
> * @param string|null $subreviewurl submission review url /** > * @param string|null $subreviewparams submission review custom params
< private function create_graded_lti(int $typeid, object $course, ?string $resourceid, ?string $tag) : object {
> private function create_graded_lti(int $typeid, object $course, ?string $resourceid, ?string $tag, > ?string $subreviewurl = null, ?string $subreviewparams = null) : object {
< 'lineitemtag' => $tag];
> 'lineitemtag' => $tag, > 'lineitemsubreviewurl' => $subreviewurl, > 'lineitemsubreviewparams' => $subreviewparams];
* @param int $typeid Type Id of the LTI Tool. * @param object $course course where to add the lti instance. * * @return object lti instance created */ private function create_notgraded_lti(int $typeid, object $course) : object { $lti = ['course' => $course->id, 'typeid' => $typeid, 'instructorchoiceacceptgrades' => LTI_SETTING_NEVER]; return $this->getDataGenerator()->create_module('lti', $lti, array()); } /** * Inserts a standalone lineitem (gradeitem, gradebookservices entries). * * @param int $courseid Id of the course where the standalone line item will be added. * @param int $typeid of the LTI Tool * @param string|null $resourceid resource id * @param string|null $tag tag * @param int|null $ltiinstanceid Id of the LTI instance the standalone line item will be related to. * */ private function create_standalone_lineitem(int $courseid, int $typeid, ?string $resourceid, ?string $tag, int $ltiinstanceid = null) : void { $gbservice = new gradebookservices(); $gbservice->add_standalone_lineitem($courseid, "manualtest", 10, "https://test.phpunit", $ltiinstanceid, $resourceid, $tag, $typeid, null /*toolproxyid*/); } /** * Creates a new LTI Tool Type. */ private function create_type() {
< $type = new stdClass();
> $type = new \stdClass();
$type->state = LTI_TOOL_STATE_CONFIGURED; $type->name = "Test tool"; $type->description = "Example description"; $type->clientid = "Test client ID"; $type->baseurl = $this->getExternalTestFileUrl('/test.html');
< $config = new stdClass();
> $config = new \stdClass();
$config->ltiservice_gradesynchronization = 2; return lti_add_type($type, $config); } }