From ef2a1934a0501b95608ac6da8b3bccc8a6e7d831 Mon Sep 17 00:00:00 2001 From: Huong Nguyen Date: Thu, 9 Mar 2023 11:34:05 +0700 Subject: [PATCH] MDL-75316 core_moodlenet: Add setting to control sharing Originally implemented as MDL-75319 --- admin/settings/moodlenet.php | 55 +++++++++++++++++++ admin/tests/behat/moodlenet_outbound.feature | 52 ++++++++++++++++++ admin/tool/moodlenet/settings.php | 4 +- lang/en/moodle.php | 2 + lib/tests/behat/behat_moodlenet.php | 56 ++++++++++++++++++++ 5 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 admin/settings/moodlenet.php create mode 100644 admin/tests/behat/moodlenet_outbound.feature create mode 100644 lib/tests/behat/behat_moodlenet.php diff --git a/admin/settings/moodlenet.php b/admin/settings/moodlenet.php new file mode 100644 index 00000000000..22a69a4f6f6 --- /dev/null +++ b/admin/settings/moodlenet.php @@ -0,0 +1,55 @@ +. + +/** + * This file gives information about MoodleNet. + * + * @package core + * @copyright 2023 Huong Nguyen + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die; + +if ($hassiteconfig) { + if (!empty($CFG->enablesharingtomoodlenet)) { + if (!$ADMIN->locate('moodlenet')) { + $ADMIN->add('root', new admin_category('moodlenet', get_string('pluginname', 'tool_moodlenet'))); + } + + // Outbound settings page. + $settings = new admin_settingpage('moodlenetoutbound', new lang_string('moodlenet:outboundsettings', 'moodle')); + $ADMIN->add('moodlenet', $settings); + + // Get all the issuers. + $issuers = \core\oauth2\api::get_all_issuers(); + $oauth2services = [ + '' => new lang_string('none', 'admin'), + ]; + foreach ($issuers as $issuer) { + // Get the enabled issuer with the service type is MoodleNet only. + if ($issuer->get('servicetype') == 'moodlenet' && $issuer->get('enabled')) { + $oauth2services[$issuer->get('id')] = s($issuer->get('name')); + } + } + + $url = new \moodle_url('/admin/tool/oauth2/issuers.php'); + + $settings->add(new admin_setting_configselect('moodlenet/oauthservice', new lang_string('issuer', 'auth_oauth2'), + new lang_string('moodlenet:configoauthservice', 'moodle', $url->out()), '', $oauth2services)); + + } +} diff --git a/admin/tests/behat/moodlenet_outbound.feature b/admin/tests/behat/moodlenet_outbound.feature new file mode 100644 index 00000000000..6ce291317e0 --- /dev/null +++ b/admin/tests/behat/moodlenet_outbound.feature @@ -0,0 +1,52 @@ +@core @core_admin +Feature: MoodleNet outbound configuration + In order to send activity/resource to MoodleNet + As a Moodle administrator + I need to set outbound configuration + + Background: + Given I log in as "admin" + + Scenario: Share to MoodleNet experimental flag + Given I navigate to "Development > Experimental" in site administration + Then "Enable sharing to MoodleNet" "field" should exist + And the field "Enable sharing to MoodleNet" matches value "0" + + Scenario: Outbound configuration without experimental flag enable yet + Given I navigate to "MoodleNet" in site administration + Then I should not see "MoodleNet outbound settings" + + Scenario: Outbound configuration without OAuth 2 service setup yet + Given the following config values are set as admin: + | enablesharingtomoodlenet | 1 | + When I navigate to "MoodleNet" in site administration + Then I should see "MoodleNet outbound settings" + And I click on "MoodleNet outbound settings" "link" + And the field "OAuth 2 service" matches value "None" + And I should see "Select the OAuth 2 service that is configured to talk to the MoodleNet server. If the service doesn't exist yet, you will need to create it." + And I click on "create" "link" + And I should see "OAuth 2 services" + + Scenario: Outbound configuration with OAuth 2 service setup + Given a MoodleNet mock server is configured + And the following config values are set as admin: + | enablesharingtomoodlenet | 1 | + And I navigate to "Server > OAuth 2 services" in site administration + And I press "Custom" + And I should see "Create new service: Custom" + And I set the following fields to these values: + | Name | Testing custom service | + | Client ID | thisistheclientid | + | Client secret | supersecret | + And I press "Save changes" + When I navigate to "MoodleNet > MoodleNet outbound settings" in site administration + Then the field "OAuth 2 service" matches value "None" + And I navigate to "Server > OAuth 2 services" in site administration + And I press "MoodleNet" + And I should see "Create new service: MoodleNet" + And I change the MoodleNet field "Service base URL" to mock server + And I press "Save changes" + And I navigate to "MoodleNet > MoodleNet outbound settings" in site administration + And the "OAuth 2 service" "field" should be enabled + And I should see "MoodleNet" in the "OAuth 2 service" "select" + And I should not see "Testing custom service" in the "OAuth 2 service" "select" diff --git a/admin/tool/moodlenet/settings.php b/admin/tool/moodlenet/settings.php index 2c48f3b7135..35d09c77ed9 100644 --- a/admin/tool/moodlenet/settings.php +++ b/admin/tool/moodlenet/settings.php @@ -35,7 +35,9 @@ if ($hassiteconfig) { // Create a MoodleNet category. if (get_config('tool_moodlenet', 'enablemoodlenet')) { - $ADMIN->add('root', new admin_category('moodlenet', get_string('pluginname', 'tool_moodlenet'))); + if (!$ADMIN->locate('moodlenet')) { + $ADMIN->add('root', new admin_category('moodlenet', get_string('pluginname', 'tool_moodlenet'))); + } // Our settings page. $settings = new admin_settingpage('tool_moodlenet', get_string('moodlenetsettings', 'tool_moodlenet')); $ADMIN->add('moodlenet', $settings); diff --git a/lang/en/moodle.php b/lang/en/moodle.php index 18449e927d7..a76415717e9 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -1365,6 +1365,8 @@ $string['moodledocslink'] = 'Help and documentation'; $string['moodleversion'] = 'Moodle version'; $string['moodlerelease'] = 'Moodle release'; $string['moodleservicesandsupport'] = 'Services and support'; +$string['moodlenet:configoauthservice'] = 'Select the OAuth 2 service that is configured to talk to the MoodleNet server. If the service doesn\'t exist yet, you will need to create it.'; +$string['moodlenet:outboundsettings'] = 'MoodleNet outbound settings'; $string['more'] = 'more'; $string['morehelp'] = 'More help'; $string['morehelpaboutmodule'] = 'More help about the {$a} activity'; diff --git a/lib/tests/behat/behat_moodlenet.php b/lib/tests/behat/behat_moodlenet.php new file mode 100644 index 00000000000..c849d955f58 --- /dev/null +++ b/lib/tests/behat/behat_moodlenet.php @@ -0,0 +1,56 @@ +. + +// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php. + +use Moodle\BehatExtension\Exception\SkippedException; + +require_once(__DIR__ . '/../../behat/behat_base.php'); + +/** + * Steps definitions related to MoodleNet. + * + * @package core + * @category test + * @copyright 2023 Huong Nguyen + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class behat_moodlenet extends behat_base { + + /** + * Check that the TEST_MOODLENET_MOCK_SERVER is defined, so we can connect to the mock server. + * + * @Given /^a MoodleNet mock server is configured$/ + */ + public function mock_is_configured(): void { + if (!defined('TEST_MOODLENET_MOCK_SERVER')) { + throw new SkippedException( + 'The TEST_MOODLENET_MOCK_SERVER constant must be defined to run MoodleNet tests' + ); + } + } + + /** + * Change the service base url to the TEST_MOODLENET_MOCK_SERVER url. + * + * @Given /^I change the MoodleNet field "(?P(?:[^"]|\\")*)" to mock server$/ + * @param string $field Field name + */ + public function change_service_base_url_to_mock_url(string $field): void { + $field = behat_field_manager::get_form_field_from_label($field, $this); + $field->set_value(TEST_MOODLENET_MOCK_SERVER); + } +}