From c6c7b1dadc81cc362ea9bcb0b1a6d73c219137da Mon Sep 17 00:00:00 2001 From: Jake Dallimore Date: Mon, 24 Jan 2022 18:10:27 +0800 Subject: [PATCH] MDL-69542 enrol_lti: add generators for use in LTI Advantage behat --- .../tests/generator/behat_enrol_lti_generator.php | 47 ++++++++++++ enrol/lti/tests/generator/lib.php | 88 ++++++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 enrol/lti/tests/generator/behat_enrol_lti_generator.php create mode 100644 enrol/lti/tests/generator/lib.php diff --git a/enrol/lti/tests/generator/behat_enrol_lti_generator.php b/enrol/lti/tests/generator/behat_enrol_lti_generator.php new file mode 100644 index 00000000000..ef41ba4e5e4 --- /dev/null +++ b/enrol/lti/tests/generator/behat_enrol_lti_generator.php @@ -0,0 +1,47 @@ +. + +/** + * Behat data generator for enrol_lti. + * + * @package enrol_lti + * @category test + * @copyright 2021 Jake Dallimore + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class behat_enrol_lti_generator extends behat_generator_base { + + /** + * Get a list of the entities that Behat can create using the generator step. + * + * @return array the list of creatable entities. + */ + protected function get_creatable_entities(): array { + return [ + 'application registrations' => [ + 'singular' => 'application registration', + 'datagenerator' => 'application_registration', + 'required' => ['name', 'platformid', 'clientid', 'authrequesturl', 'jwksurl', 'accesstokenurl'] + ], + 'published resources' => [ + 'singular' => 'published resource', + 'datagenerator' => 'published_resource', + 'required' => ['name', 'activity', 'course'], + 'switchids' => ['activity' => 'activityid', 'course' => 'courseid'] + ] + ]; + } +} diff --git a/enrol/lti/tests/generator/lib.php b/enrol/lti/tests/generator/lib.php new file mode 100644 index 00000000000..e757ab51bc0 --- /dev/null +++ b/enrol/lti/tests/generator/lib.php @@ -0,0 +1,88 @@ +. + +use enrol_lti\local\ltiadvantage\entity\application_registration; +use enrol_lti\local\ltiadvantage\repository\application_registration_repository; +use enrol_lti\local\ltiadvantage\repository\deployment_repository; + +/** + * LTI Enrolment test data generator class. + * + * @package enrol_lti + * @category test + * @copyright 2021 Jake Dallimore + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class enrol_lti_generator extends component_generator_base { + + /** + * Test method to generate an application registration (and optionally a deployment) for a platform. + * + * @param array $data the application registration data, with optional deployment data. + * @return application_registration + */ + public function create_application_registration(array $data): application_registration { + $registration = application_registration::create( + $data['name'], + new moodle_url($data['platformid']), + $data['clientid'], + new moodle_url($data['authrequesturl']), + new moodle_url($data['jwksurl']), + new moodle_url($data['accesstokenurl']) + ); + + $appregrepo = new application_registration_repository(); + $createdregistration = $appregrepo->save($registration); + + if (isset($data['deploymentname']) && isset($data['deploymentid'])) { + $deployment = $createdregistration->add_tool_deployment($data['deploymentname'], $data['deploymentid']); + $deploymentrepo = new deployment_repository(); + $deploymentrepo->save($deployment); + } + + return $createdregistration; + } + + /** + * Test method to generate a published resource for a course. + * + * @param array $data the data required to publish the resource. + * @return stdClass the enrol_lti_tools record, representing the published resource. + */ + public function create_published_resource(array $data): stdClass { + + if (!empty($data['ltiversion']) && !in_array($data['ltiversion'], ['LTI-1p3', 'LTI-1p0/LTI-2p0'])) { + throw new coding_exception("The field 'ltiversion' must be either 'LTI-1p3' or 'LTI-1p0/LTI-2p0'."); + } + + $instancedata = (object) [ + 'name' => $data['name'], + 'courseid' => $data['courseid'], + 'cmid' => $data['activityid'], + 'ltiversion' => $data['ltiversion'] ?? 'LTI-1p3' + ]; + $tool = $this->datagenerator->create_lti_tool($instancedata); + + if (empty($data['uuid'])) { + return $tool; + } + + // Allow tests to create predictable uuids. + global $DB; + $DB->set_field('enrol_lti_tools', 'uuid', $data['uuid']); + return enrol_lti\helper::get_lti_tool($tool->id); + } +} -- 2.11.4.GIT