From fff9ab1087665fac3deca0a3256d9fb7c7111e3c Mon Sep 17 00:00:00 2001 From: djarrancotleanu Date: Fri, 11 Oct 2024 08:03:03 +1000 Subject: [PATCH] Fix #72: Add unit test for save_settings function --- locallib.php | 5 ++ tests/locallib_test.php | 109 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 tests/locallib_test.php diff --git a/locallib.php b/locallib.php index 788c2d2..67aa420 100644 --- a/locallib.php +++ b/locallib.php @@ -237,6 +237,11 @@ public function save_settings(stdClass $data) { // Test Mahara connection. try { + // Skip webservice call if running unit tests. + if ((defined('PHPUNIT_TEST') || PHPUNIT_TEST)) { + return true; + } + $data = $this->webservice_call("mahara_user_get_extended_context", array()); $funcs = array(); diff --git a/tests/locallib_test.php b/tests/locallib_test.php new file mode 100644 index 0000000..6675cbe --- /dev/null +++ b/tests/locallib_test.php @@ -0,0 +1,109 @@ +. + +/** + * Tests for mod/assign/submission/ltisubmissions/locallib.php + * + * @package assignsubmission_maharaws + * @copyright 2024 Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace assignsubmission_maharaws; + +use mod_assign_test_generator; + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once($CFG->dirroot . '/mod/assign/tests/generator.php'); + +/** + * Unit tests for mod/assign/submission/maharaws/locallib.php + * + * @package assignsubmission_maharaws + * @copyright 2024 Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class locallib_test extends \advanced_testcase { + + // Use the generator helper. + use mod_assign_test_generator; + + /** + * Test save_settings. + * + * Ensure that all required config is set if force_global_credentials is enabled + * when assign is created with Mahara submissions by user with and without + * assignsubmission/maharaws:configure permission. + * + * @covers ::save_settings + */ + public function test_save_settings() { + global $DB; + + $this->resetAfterTest(); + + // Set plugin configuration, enable global credentials. + $pluginname = 'assignsubmission_maharaws'; + set_config('lock', '0', $pluginname); + set_config('force_global_credentials', '1', $pluginname); + set_config('url', 'https://url.com', $pluginname); + set_config('key', 'key', $pluginname); + set_config('secret', 'secret', $pluginname); + + // Course setup. + $course = $this->getDataGenerator()->create_course(); + $teacherWithPermission = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher'); + $teacherWithoutPermission = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); + $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); + + // Assign configure role to editingteacher role. + $roleid = $DB->get_field('role', 'id', ['shortname' => 'editingteacher'], MUST_EXIST); + assign_capability('assignsubmission/maharaws:configure', CAP_ALLOW, $roleid, SYSCONTEXTID, true); + + $this->setUser($teacherWithPermission); + $assignWithPermission = $this->create_instance($course, [ + 'assignsubmission_maharaws_enabled' => 1, + // Must pass same data expected during config form submission by user with permissions. + 'assignsubmission_maharaws_lockpages' => 0, + 'assignsubmission_maharaws_archiveonrelease' => 0, + ]); + $this->setUser($teacherWithoutPermission); + $assignWithoutPermission = $this->create_instance($course, [ + 'assignsubmission_maharaws_enabled' => 1, + // Don't set lockpages as this cannot be changed by user without permissions. + 'assignsubmission_maharaws_archiveonrelease' => 0, + ]); + + // Assert that username_attribute has been set. + $plugin = $assignWithPermission->get_submission_plugin_by_type('maharaws'); + $this->assertEquals('email', $plugin->get_config('username_attribute')); + $plugin = $assignWithoutPermission->get_submission_plugin_by_type('maharaws'); + $this->assertEquals('email', $plugin->get_config('username_attribute')); + + // Assert that all submission config for assign has been set. + // Assumes that global credentials are forced. + $dbparams = array('assignment'=> $assignWithPermission->get_instance()->id, + 'subtype'=> 'assignsubmission', + 'plugin'=> 'maharaws'); + $this->assertCount(6, $DB->get_records('assign_plugin_config', $dbparams)); + $dbparams = array('assignment'=> $assignWithoutPermission->get_instance()->id, + 'subtype'=> 'assignsubmission', + 'plugin'=> 'maharaws'); + $this->assertCount(6, $DB->get_records('assign_plugin_config', $dbparams)); + } +}