diff --git a/ltix/classes/local/lticore/models/resource_link.php b/ltix/classes/local/lticore/models/resource_link.php new file mode 100644 index 0000000000000..13e31a45c22e3 --- /dev/null +++ b/ltix/classes/local/lticore/models/resource_link.php @@ -0,0 +1,125 @@ +. +// +// This file is part of BasicLTI4Moodle +// +// BasicLTI4Moodle is an IMS BasicLTI (Basic Learning Tools for Interoperability) +// consumer for Moodle 1.9 and Moodle 2.0. BasicLTI is a IMS Standard that allows web +// based learning tools to be easily integrated in LMS as native ones. The IMS BasicLTI +// specification is part of the IMS standard Common Cartridge 1.1 Sakai and other main LMS +// are already supporting or going to support BasicLTI. This project Implements the consumer +// for Moodle. Moodle is a Free Open source Learning Management System by Martin Dougiamas. +// BasicLTI4Moodle is a project iniciated and leaded by Ludo(Marc Alier) and Jordi Piguillem +// at the GESSI research group at UPC. +// SimpleLTI consumer for Moodle is an implementation of the early specification of LTI +// by Charles Severance (Dr Chuck) htp://dr-chuck.com , developed by Jordi Piguillem in a +// Google Summer of Code 2008 project co-mentored by Charles Severance and Marc Alier. +// +// BasicLTI4Moodle is copyright 2009 by Marc Alier Forment, Jordi Piguillem and Nikolas Galanis +// of the Universitat Politecnica de Catalunya http://www.upc.edu +// Contact info: Marc Alier Forment granludo @ gmail.com or marc.alier @ upc.edu. + +namespace core_ltix\local\lticore\models; + +use core\persistent; +use core\uuid; + +/** + * Resource link persistent. + * + * @package core_ltix + * @copyright 2024 Jake Dallimore + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class resource_link extends persistent { + + /** @var string The table name. */ + public const TABLE = 'lti_resource_link'; + + protected static function define_properties(): array { + global $CFG; + require_once($CFG->dirroot . '/ltix/constants.php'); + + return [ + 'typeid' => [ + 'type' => PARAM_INT, + ], + 'contextid' => [ + 'type' => PARAM_INT, + ], + 'legacyid' => [ + 'type' => PARAM_INT, + 'default' => null, + 'null' => NULL_ALLOWED, + ], + 'uuid' => [ + 'type' => PARAM_ALPHANUMEXT, + 'default' => static function(): string { + return uuid::generate(); + }, + ], + 'url' => [ + 'type' => PARAM_URL, + ], + 'title' => [ + 'type' => PARAM_TEXT, + ], + 'text' => [ + 'type' => PARAM_CLEANHTML, + 'default' => null, + 'null' => NULL_ALLOWED, + ], + 'textformat' => [ + 'type' => PARAM_INT, + 'default' => FORMAT_MOODLE, + 'null' => NULL_ALLOWED, + 'choices' => [ + FORMAT_MOODLE, + FORMAT_HTML, + FORMAT_PLAIN, + FORMAT_MARKDOWN, + ], + ], + 'launchcontainer' => [ + 'type' => PARAM_INT, + 'default' => LTI_LAUNCH_CONTAINER_DEFAULT, + 'null' => NULL_NOT_ALLOWED, + 'choices' => [ + LTI_LAUNCH_CONTAINER_DEFAULT, + LTI_LAUNCH_CONTAINER_EMBED, + LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS, + LTI_LAUNCH_CONTAINER_WINDOW, + LTI_LAUNCH_CONTAINER_REPLACE_MOODLE_WINDOW, + ], + ], + 'customparams' => [ + 'type' => PARAM_TEXT, + 'default' => null, + 'null' => NULL_ALLOWED, + ], + 'icon' => [ + 'type' => PARAM_URL, + 'default' => null, + 'null' => NULL_ALLOWED, + ], + 'servicesalt' => [ + 'type' => PARAM_TEXT, + 'default' => null, + 'null' => NULL_ALLOWED, + ], + ]; + } +} diff --git a/ltix/tests/local/lticore/models/resource_link_test.php b/ltix/tests/local/lticore/models/resource_link_test.php new file mode 100644 index 0000000000000..25f68084fa17a --- /dev/null +++ b/ltix/tests/local/lticore/models/resource_link_test.php @@ -0,0 +1,158 @@ +. +// +// This file is part of BasicLTI4Moodle +// +// BasicLTI4Moodle is an IMS BasicLTI (Basic Learning Tools for Interoperability) +// consumer for Moodle 1.9 and Moodle 2.0. BasicLTI is a IMS Standard that allows web +// based learning tools to be easily integrated in LMS as native ones. The IMS BasicLTI +// specification is part of the IMS standard Common Cartridge 1.1 Sakai and other main LMS +// are already supporting or going to support BasicLTI. This project Implements the consumer +// for Moodle. Moodle is a Free Open source Learning Management System by Martin Dougiamas. +// BasicLTI4Moodle is a project iniciated and leaded by Ludo(Marc Alier) and Jordi Piguillem +// at the GESSI research group at UPC. +// SimpleLTI consumer for Moodle is an implementation of the early specification of LTI +// by Charles Severance (Dr Chuck) htp://dr-chuck.com , developed by Jordi Piguillem in a +// Google Summer of Code 2008 project co-mentored by Charles Severance and Marc Alier. +// +// BasicLTI4Moodle is copyright 2009 by Marc Alier Forment, Jordi Piguillem and Nikolas Galanis +// of the Universitat Politecnica de Catalunya http://www.upc.edu +// Contact info: Marc Alier Forment granludo @ gmail.com or marc.alier @ upc.edu. + +namespace core_ltix\local\lticore\models; + +/** + * Tests covering resource_link. + * + * @covers \core_ltix\local\lticore\models\resource_link + * @package core_ltix + * @copyright 2024 Jake Dallimore + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +final class resource_link_test extends \advanced_testcase { + + /** + * Test creation using the bare minimum number of fields (those required). + * + * @dataProvider create_resource_link_provider + * @param array $setdata the data to pass to set(). + * @param array $expecteddata the expected dataset, to compare with calls to get(). + * @param null|string $expectedexception the exception type to expect, if any, else null. + * @return void + */ + public function test_resource_link_creation(array $setdata, array $expecteddata, ?string$expectedexception = null): void { + $this->resetAfterTest(); + + $rl = new resource_link(); + + if (!is_null($expectedexception)) { + $this->expectException($expectedexception); + } + foreach ($setdata as $name => $value) { + $rl->set($name, $value); + } + $rl->save(); + + foreach ($expecteddata as $name => $value) { + $this->assertEquals($value, $rl->get($name)); + } + // In cases where this is generated, just confirm it's a string. + $this->assertIsString($rl->get('uuid')); + $this->assertNotEmpty($rl->get('uuid')); + } + + public function create_resource_link_provider(): array { + global $CFG; + require_once($CFG->dirroot . '/ltix/constants.php'); + + return [ + 'minimal set, required fields only' => [ + 'setdata' => [ + 'typeid' => 4, + 'contextid' => 33, + 'url' => (new \moodle_url('http://tool.example.com/my/resource'))->out(false), + 'title' => 'My resource', + ], + 'expecteddata' => [ + 'typeid' => 4, + 'contextid' => 33, + 'legacyid' => null, + // Note: can't check UUID in this case since it's a randomly generated default, so it's omitted. + 'url' => 'http://tool.example.com/my/resource', + 'title' => 'My resource', + 'text' => null, + 'textformat' => FORMAT_MOODLE, + 'launchcontainer' => LTI_LAUNCH_CONTAINER_DEFAULT, + 'customparams' => null, + 'icon' => null, + 'servicesalt' => null, + ], + ], + 'full set, all fields specified' => [ + 'setdata' => [ + 'typeid' => 4, + 'contextid' => 33, + 'legacyid' => 56001, + 'uuid' => '123', + 'url' => (new \moodle_url('http://tool.example.com/my/resource'))->out(false), + 'title' => 'My resource', + 'text' => '
This is a larger description of the resource link
', + 'textformat' => FORMAT_HTML, + 'launchcontainer' => LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS, + 'customparams' => 'id=abc-123-fff', + 'icon' => (new \moodle_url('http://tool.example.com/my/resource/icon.png'))->out(false), + 'servicesalt' => '664c4261e47d85.53212526', + ], + 'expecteddata' => [ + 'typeid' => 4, + 'contextid' => 33, + 'legacyid' => 56001, + 'uuid' => '123', + 'url' => 'http://tool.example.com/my/resource', + 'title' => 'My resource', + 'text' => '
This is a larger description of the resource link
', + 'textformat' => FORMAT_HTML, + 'launchcontainer' => LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS, + 'customparams' => 'id=abc-123-fff', + 'icon' => 'http://tool.example.com/my/resource/icon.png', + 'servicesalt' => '664c4261e47d85.53212526', + ] + ], + 'Exceptional case, invalid launchcontainer value' => [ + 'setdata' => [ + 'typeid' => 4, + 'contextid' => 33, + 'url' => (new \moodle_url('http://tool.example.com/my/resource'))->out(false), + 'title' => 'My resource', + 'launchcontainer' => 999999, // Invalid launch container. + ], + 'expecteddata' => [], + 'expectedexception' => \core\invalid_persistent_exception::class, + ], + 'Exceptional case, invalid textformat value' => [ + 'setdata' => [ + 'typeid' => 4, + 'contextid' => 33, + 'url' => (new \moodle_url('http://tool.example.com/my/resource'))->out(false), + 'title' => 'My resource', + 'textformat' => 999999, // Invalid text format. + ], + 'expecteddata' => [], + 'expectedexception' => \core\invalid_persistent_exception::class, + ], + ]; + } +}