diff --git a/auth.php b/auth.php index 73fa83b..e05f7f5 100644 --- a/auth.php +++ b/auth.php @@ -107,7 +107,7 @@ class auth_plugin_userkey extends auth_plugin_base { /** * Redirects the user to provided URL. * - * @param $url URL to redirect to. + * @param string $url URL to redirect to. * * @throws \moodle_exception If gets running via CLI or AJAX call. */ @@ -134,8 +134,6 @@ class auth_plugin_userkey extends auth_plugin_base { /** * Logs a user in using userkey and redirects after. * - * @TODO: refactor this method to make it easy to read. - * * @throws \moodle_exception If something went wrong. */ public function user_login_userkey() { @@ -157,7 +155,7 @@ class auth_plugin_userkey extends auth_plugin_base { if (isloggedin()) { require_logout(); } - print_error($exception->errorcode); + throw $exception; } if (isloggedin()) { @@ -653,7 +651,7 @@ class auth_plugin_userkey extends auth_plugin_base { $this->redirect($redirect); } else { // If logged in with different auth type, then display an error. - print_error('incorrectlogout', 'auth_userkey', $CFG->wwwroot); + throw new moodle_exception('incorrectlogout', 'auth_userkey', $CFG->wwwroot); } } } diff --git a/classes/core_userkey_manager.php b/classes/core_userkey_manager.php index d15ebc8..83e7c0b 100644 --- a/classes/core_userkey_manager.php +++ b/classes/core_userkey_manager.php @@ -14,6 +14,8 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +namespace auth_userkey; + /** * Key manager class. * @@ -21,9 +23,6 @@ * @copyright 2016 Dmitrii Metelkin (dmitriim@catalyst-au.net) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ - -namespace auth_userkey; - class core_userkey_manager implements userkey_manager_interface { /** @@ -115,17 +114,17 @@ class core_userkey_manager implements userkey_manager_interface { ); if (!$key = $DB->get_record('user_private_key', $options)) { - print_error('invalidkey'); + throw new \moodle_exception('invalidkey'); } if (!empty($key->validuntil) and $key->validuntil < time()) { - print_error('expiredkey'); + throw new \moodle_exception('expiredkey'); } $this->validate_ip_address($key); if (!$user = $DB->get_record('user', array('id' => $key->userid))) { - print_error('invaliduserid'); + throw new \moodle_exception('invaliduserid'); } return $key; } @@ -145,7 +144,7 @@ class core_userkey_manager implements userkey_manager_interface { $remoteaddr = getremoteaddr(null); if (empty($remoteaddr)) { - print_error('noip', 'auth_userkey'); + throw new \moodle_exception('noip', 'auth_userkey'); } if (address_in_subnet($remoteaddr, $key->iprestriction)) { @@ -161,6 +160,6 @@ class core_userkey_manager implements userkey_manager_interface { } } - print_error('ipmismatch', 'error', '', null, "Remote address: $remoteaddr\nKey IP: $key->iprestriction"); + throw new \moodle_exception('ipmismatch', 'error', '', null, "Remote address: $remoteaddr\nKey IP: $key->iprestriction"); } } diff --git a/db/upgrade.php b/db/upgrade.php index efb5a5b..70c844b 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -22,6 +22,12 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +/** + * Upgrade hook. + * + * @param string $oldversion Old version of the plugin. + * @return bool + */ function xmldb_auth_userkey_upgrade($oldversion) { global $DB; diff --git a/externallib.php b/externallib.php index f264197..a25bb94 100644 --- a/externallib.php +++ b/externallib.php @@ -28,6 +28,13 @@ require_once($CFG->libdir . "/externallib.php"); require_once($CFG->dirroot . "/webservice/lib.php"); require_once($CFG->dirroot . "/auth/userkey/auth.php"); +/** + * Webservices for auth_userkey. + * + * @package auth_userkey + * @copyright 2016 Dmitrii Metelkin (dmitriim@catalyst-au.net) + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ class auth_userkey_external extends external_api { /** diff --git a/login.php b/login.php index b6e7733..883b154 100644 --- a/login.php +++ b/login.php @@ -25,7 +25,7 @@ require_once(dirname(__FILE__) . '/../../config.php'); if (!is_enabled_auth('userkey')) { - print_error(get_string('pluginisdisabled', 'auth_userkey')); + throw new moodle_exception(get_string('pluginisdisabled', 'auth_userkey')); } get_auth_plugin('userkey')->user_login_userkey(); diff --git a/logout.php b/logout.php index 2e5cb0e..19e5f9a 100644 --- a/logout.php +++ b/logout.php @@ -25,7 +25,7 @@ require_once(dirname(__FILE__) . '/../../config.php'); if (!is_enabled_auth('userkey')) { - print_error(get_string('pluginisdisabled', 'auth_userkey')); + throw new moodle_exception(get_string('pluginisdisabled', 'auth_userkey')); } get_auth_plugin('userkey')->user_logout_userkey(); diff --git a/tests/phpunit/auth_plugin_test.php b/tests/phpunit/auth_plugin_test.php index 37c588e..28a15b9 100644 --- a/tests/phpunit/auth_plugin_test.php +++ b/tests/phpunit/auth_plugin_test.php @@ -185,62 +185,58 @@ class auth_plugin_userkey_testcase extends advanced_testcase { /** * Test that auth plugin throws correct exception if default mapping field is not provided. - * - * @expectedException \invalid_parameter_exception - * @expectedExceptionMessage Invalid parameter value detected (Required field "email" is not set or empty.) */ public function test_throwing_exception_if_default_mapping_field_is_not_provided() { $user = array(); + $this->expectException(invalid_parameter_exception::class); + $this->expectExceptionMessage('Invalid parameter value detected (Required field "email" is not set or empty.)'); + $actual = $this->auth->get_login_url($user); } /** * Test that auth plugin throws correct exception if username mapping field is not provided, but set in configs. - * - * @expectedException \invalid_parameter_exception - * @expectedExceptionMessage Invalid parameter value detected (Required field "username" is not set or empty.) */ public function test_throwing_exception_if_mapping_field_username_is_not_provided() { $user = array(); set_config('mappingfield', 'username', 'auth_userkey'); $this->auth = new auth_plugin_userkey(); + $this->expectException(invalid_parameter_exception::class); + $this->expectExceptionMessage('Invalid parameter value detected (Required field "username" is not set or empty.)'); + $actual = $this->auth->get_login_url($user); } /** * Test that auth plugin throws correct exception if idnumber mapping field is not provided, but set in configs. - * - * @expectedException \invalid_parameter_exception - * @expectedExceptionMessage Invalid parameter value detected (Required field "idnumber" is not set or empty.) */ public function test_throwing_exception_if_mapping_field_idnumber_is_not_provided() { $user = array(); set_config('mappingfield', 'idnumber', 'auth_userkey'); $this->auth = new auth_plugin_userkey(); + $this->expectException(invalid_parameter_exception::class); + $this->expectExceptionMessage('Invalid parameter value detected (Required field "idnumber" is not set or empty.)'); + $actual = $this->auth->get_login_url($user); } /** * Test that auth plugin throws correct exception if we trying to request not existing user. - * - * @expectedException \invalid_parameter_exception - * @expectedExceptionMessage Invalid parameter value detected (User is not exist) */ public function test_throwing_exception_if_user_is_not_exist() { $user = array(); $user['email'] = 'notexists@test.com'; + $this->expectException(invalid_parameter_exception::class); + $this->expectExceptionMessage('Invalid parameter value detected (User is not exist)'); $actual = $this->auth->get_login_url($user); } /** * Test that auth plugin throws correct exception if we trying to request user, * but ip field is not set and iprestriction is enabled. - * - * @expectedException \invalid_parameter_exception - * @expectedExceptionMessage Invalid parameter value detected (Required parameter "ip" is not set.) */ public function test_throwing_exception_if_iprestriction_is_enabled_but_ip_is_missing_in_data() { $user = array(); @@ -248,6 +244,9 @@ class auth_plugin_userkey_testcase extends advanced_testcase { set_config('iprestriction', true, 'auth_userkey'); $this->auth = new auth_plugin_userkey(); + $this->expectException(invalid_parameter_exception::class); + $this->expectExceptionMessage('Invalid parameter value detected (Required parameter "ip" is not set.)'); + $actual = $this->auth->get_login_url($user); } @@ -349,11 +348,7 @@ class auth_plugin_userkey_testcase extends advanced_testcase { /** * Test that we can request a key for a new user. - * - * @expectedException \invalid_parameter_exception - * @expectedExceptionMessage Unable to create user, missing value(s): username,firstname,lastname */ - public function test_missing_data_to_create_user() { global $CFG, $DB; @@ -367,13 +362,14 @@ class auth_plugin_userkey_testcase extends advanced_testcase { $user->email = 'username@test.com'; $user->ip = '192.168.1.1'; + $this->expectException(invalid_parameter_exception::class); + $this->expectExceptionMessage('Unable to create user, missing value(s): username,firstname,lastname'); + $this->auth->get_login_url($user); } /** * Test that when we attempt to create a new user duplicate usernames are caught. - * @expectedException \invalid_parameter_exception - * @expectedExceptionMessage Username already exists: username */ public function test_create_refuse_duplicate_username() { set_config('createuser', true, 'auth_userkey'); @@ -395,14 +391,14 @@ class auth_plugin_userkey_testcase extends advanced_testcase { $duplicateuser = clone($originaluser); $duplicateuser->email = 'duplicateuser@test.com'; + $this->expectException(invalid_parameter_exception::class); + $this->expectExceptionMessage('Username already exists: username'); + $this->auth->get_login_url($duplicateuser); } /** * Test that when we attempt to create a new user duplicate emails are caught. - * - * @expectedException \invalid_parameter_exception - * @expectedExceptionMessage Email address already exists: username@test.com */ public function test_create_refuse_duplicate_email() { set_config('createuser', true, 'auth_userkey'); @@ -425,6 +421,9 @@ class auth_plugin_userkey_testcase extends advanced_testcase { $duplicateuser = clone($originaluser); $duplicateuser->username = 'duplicateuser'; + $this->expectException(invalid_parameter_exception::class); + $this->expectExceptionMessage('Email address already exists: username@test.com'); + $this->auth->get_login_url($duplicateuser); } @@ -472,9 +471,6 @@ class auth_plugin_userkey_testcase extends advanced_testcase { /** * Test that when we attempt to update a user duplicate emails are caught. - * - * @expectedException \invalid_parameter_exception - * @expectedExceptionMessage Email address already exists: trytoduplicate@test.com */ public function test_update_refuse_duplicate_email() { set_config('updateuser', true, 'auth_userkey'); @@ -495,14 +491,14 @@ class auth_plugin_userkey_testcase extends advanced_testcase { $originaluser->city = 'brighton'; $originaluser->ip = '192.168.1.1'; + $this->expectException(invalid_parameter_exception::class); + $this->expectExceptionMessage('Email address already exists: trytoduplicate@test.com'); + $this->auth->get_login_url($originaluser); } /** * Test that when we attempt to update a user duplicate usernames are caught. - * - * @expectedException \invalid_parameter_exception - * @expectedExceptionMessage Username already exists: trytoduplicate */ public function test_update_refuse_duplicate_username() { set_config('updateuser', true, 'auth_userkey'); @@ -522,6 +518,9 @@ class auth_plugin_userkey_testcase extends advanced_testcase { $originaluser->city = 'brighton'; $originaluser->ip = '192.168.1.1'; + $this->expectException(invalid_parameter_exception::class); + $this->expectExceptionMessage('Username already exists: trytoduplicate'); + $this->auth->get_login_url($originaluser); } @@ -693,57 +692,55 @@ class auth_plugin_userkey_testcase extends advanced_testcase { /** * Test required parameter exception gets thrown id try to login, but key is not set. - * - * @expectedException moodle_exception - * @expectedExceptionMessage A required parameter (key) was missing */ public function test_required_parameter_exception_thrown_if_key_not_set() { + $this->expectException(moodle_exception::class); + $this->expectExceptionMessage('A required parameter (key) was missing'); + $this->auth->user_login_userkey(); } /** * Test that incorrect key exception gets thrown if a key is incorrect. - * - * @expectedException moodle_exception - * @expectedExceptionMessage Incorrect key */ public function test_invalid_key_exception_thrown_if_invalid_key() { + $this->expectException(moodle_exception::class); + $this->expectExceptionMessage('Incorrect key'); + $_POST['key'] = 'InvalidKey'; $this->auth->user_login_userkey(); } /** * Test that expired key exception gets thrown if a key is expired. - * - * @expectedException moodle_exception - * @expectedExceptionMessage Expired key */ public function test_expired_key_exception_thrown_if_expired_key() { $this->create_user_private_key(['validuntil' => time() - 3000]); + $this->expectException(moodle_exception::class); + $this->expectExceptionMessage('Expired key'); + $_POST['key'] = 'TestKey'; $this->auth->user_login_userkey(); } /** * Test that IP address mismatch exception gets thrown if incorrect IP. - * - * @expectedException moodle_exception - * @expectedExceptionMessage Client IP address mismatch */ public function test_ipmismatch_exception_thrown_if_ip_is_incorrect() { $this->create_user_private_key(['iprestriction' => '192.168.1.1']); $_POST['key'] = 'TestKey'; $_SERVER['HTTP_CLIENT_IP'] = '192.168.1.2'; + + $this->expectException(moodle_exception::class); + $this->expectExceptionMessage('Client IP address mismatch'); + $this->auth->user_login_userkey(); } /** * Test that IP address mismatch exception gets thrown if incorrect IP and outside whitelist. - * - * @expectedException moodle_exception - * @expectedExceptionMessage Client IP address mismatch */ public function test_ipmismatch_exception_thrown_if_ip_is_outside_whitelist() { set_config('ipwhitelist', '10.0.0.0/8;172.16.0.0/12;192.168.0.0/16', 'auth_userkey'); @@ -751,14 +748,15 @@ class auth_plugin_userkey_testcase extends advanced_testcase { $_POST['key'] = 'TestKey'; $_SERVER['HTTP_CLIENT_IP'] = '192.161.1.2'; + + $this->expectException(moodle_exception::class); + $this->expectExceptionMessage('Client IP address mismatch'); + $this->auth->user_login_userkey(); } /** * Test that IP address mismatch exception gets thrown if user id is incorrect. - * - * @expectedException moodle_exception - * @expectedExceptionMessageRegExp /Invalid user id/i */ public function test_invalid_user_exception_thrown_if_user_is_invalid() { $this->create_user_private_key([ @@ -769,6 +767,10 @@ class auth_plugin_userkey_testcase extends advanced_testcase { $_POST['key'] = 'TestKey'; $_SERVER['HTTP_CLIENT_IP'] = '192.168.1.1'; + + $this->expectException(moodle_exception::class); + $this->expectExceptionMessage('Invalid user'); + $this->auth->user_login_userkey(); } @@ -797,9 +799,6 @@ class auth_plugin_userkey_testcase extends advanced_testcase { /** * Test that a user logs in and gets redirected correctly. - * - * @expectedException moodle_exception - * @expectedExceptionMessage Unsupported redirect to http://www.example.com/moodle detected, execution terminated. */ public function test_that_user_logged_in_and_redirected() { global $CFG; @@ -807,6 +806,10 @@ class auth_plugin_userkey_testcase extends advanced_testcase { $this->create_user_private_key(); $CFG->wwwroot = 'http://www.example.com/moodle'; $_POST['key'] = 'TestKey'; + + $this->expectException(moodle_exception::class); + $this->expectExceptionMessage('Unsupported redirect to http://www.example.com/moodle detected, execution terminated'); + @$this->auth->user_login_userkey(); } @@ -832,25 +835,21 @@ class auth_plugin_userkey_testcase extends advanced_testcase { /** * Test that a user gets redirected to internal wantsurl URL successful log in. - * - * @expectedException moodle_exception - * @expectedExceptionMessage Unsupported redirect to /course/index.php?id=12&key=134 detected, execution terminated. */ public function test_that_user_gets_redirected_to_internal_wantsurl() { $this->create_user_private_key(); $_POST['key'] = 'TestKey'; $_POST['wantsurl'] = '/course/index.php?id=12&key=134'; + $this->expectException(moodle_exception::class); + $this->expectExceptionMessage('Unsupported redirect to /course/index.php?id=12&key=134 detected, execution terminated'); + // Using @ is the only way to test this. Thanks moodle! @$this->auth->user_login_userkey(); } /** * Test that a user gets redirected to external wantsurl URL successful log in. - * - * @expectedException moodle_exception - * @expectedExceptionMessage Unsupported redirect to http://test.com/course/index.php?id=12&key=134 detected, - * execution terminated. */ public function test_that_user_gets_redirected_to_external_wantsurl() { $this->create_user_private_key(); @@ -858,15 +857,15 @@ class auth_plugin_userkey_testcase extends advanced_testcase { $_POST['key'] = 'TestKey'; $_POST['wantsurl'] = 'http://test.com/course/index.php?id=12&key=134'; + $this->expectException(moodle_exception::class); + $this->expectExceptionMessage('Unsupported redirect to http://test.com/course/index.php?id=12&key=134 detected, execution terminated'); + // Using @ is the only way to test this. Thanks moodle! @$this->auth->user_login_userkey(); } /** * Test that login hook redirects a user if skipsso not set and ssourl is set. - * - * @expectedException moodle_exception - * @expectedExceptionMessage Unsupported redirect to http://google.com detected, execution terminated. */ public function test_loginpage_hook_redirects_if_skipsso_not_set_and_ssourl_set() { global $SESSION; @@ -875,6 +874,9 @@ class auth_plugin_userkey_testcase extends advanced_testcase { set_config('ssourl', 'http://google.com', 'auth_userkey'); $this->auth = new auth_plugin_userkey(); + $this->expectException(moodle_exception::class); + $this->expectExceptionMessage('Unsupported redirect to http://google.com detected, execution terminated.'); + $this->auth->loginpage_hook(); } @@ -906,9 +908,6 @@ class auth_plugin_userkey_testcase extends advanced_testcase { /** * Test that pre login hook redirects a user if skipsso not set and ssourl is set. - * - * @expectedException moodle_exception - * @expectedExceptionMessage Unsupported redirect to http://google.com detected, execution terminated. */ public function test_pre_loginpage_hook_redirects_if_skipsso_not_set_and_ssourl_set() { global $SESSION; @@ -917,6 +916,9 @@ class auth_plugin_userkey_testcase extends advanced_testcase { set_config('ssourl', 'http://google.com', 'auth_userkey'); $this->auth = new auth_plugin_userkey(); + $this->expectException(moodle_exception::class); + $this->expectExceptionMessage('Unsupported redirect to http://google.com detected, execution terminated.'); + $this->auth->pre_loginpage_hook(); } @@ -1021,23 +1023,23 @@ class auth_plugin_userkey_testcase extends advanced_testcase { /** * Test when try to logout, but required return is not set. - * - * @expectedException moodle_exception - * @expectedExceptionMessage A required parameter (return) was missing */ public function test_user_logout_userkey_when_required_return_not_set() { + $this->expectException(moodle_exception::class); + $this->expectExceptionMessage('A required parameter (return) was missing'); + $this->auth->user_logout_userkey(); } /** * Test when try to logout, but user is not logged in. - * - * @expectedException moodle_exception - * @expectedExceptionMessage Unsupported redirect to http://google.com detected, execution terminated. */ public function test_user_logout_userkey_when_user_is_not_logged_in() { $_POST['return'] = 'http://google.com'; + $this->expectException(moodle_exception::class); + $this->expectExceptionMessage('Unsupported redirect to http://google.com detected, execution terminated.'); + $this->auth->user_logout_userkey(); } @@ -1064,12 +1066,13 @@ class auth_plugin_userkey_testcase extends advanced_testcase { /** * Test when try to logout, but user logged in with different auth type. - * - * @expectedException moodle_exception - * @expectedExceptionMessage A required parameter (return) was missing */ public function test_user_logout_userkey_when_user_logged_in_but_return_not_set() { $this->setUser($this->user); + + $this->expectException(moodle_exception::class); + $this->expectExceptionMessage('A required parameter (return) was missing'); + $this->auth->user_logout_userkey(); } diff --git a/tests/phpunit/core_userkey_manager_test.php b/tests/phpunit/core_userkey_manager_test.php index bf79ae5..cadc3f0 100644 --- a/tests/phpunit/core_userkey_manager_test.php +++ b/tests/phpunit/core_userkey_manager_test.php @@ -154,9 +154,6 @@ class core_userkey_manager_testcase extends advanced_testcase { /** * Test that IP address mismatch exception gets thrown if incorrect IP and outside whitelist. - * - * @expectedException moodle_exception - * @expectedExceptionMessage Client IP address mismatch */ public function test_exception_if_ip_is_outside_whitelist() { global $DB; @@ -169,6 +166,9 @@ class core_userkey_manager_testcase extends advanced_testcase { $_SERVER['HTTP_CLIENT_IP'] = '193.168.1.2'; + $this->expectException(moodle_exception::class); + $this->expectExceptionMessage('Client IP address mismatch'); + $manager->validate_key($value); } diff --git a/tests/phpunit/externallib_test.php b/tests/phpunit/externallib_test.php index 79d6e3b..28aa256 100644 --- a/tests/phpunit/externallib_test.php +++ b/tests/phpunit/externallib_test.php @@ -21,7 +21,6 @@ * @copyright 2016 Dmitrii Metelkin (dmitriim@catalyst-au.net) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ - class auth_userkey_externallib_testcase extends advanced_testcase { /** * User object. @@ -50,9 +49,6 @@ class auth_userkey_externallib_testcase extends advanced_testcase { /** * Test call with incorrect required parameter. - * - * @expectedException webservice_access_exception - * @expectedExceptionMessage Access control exception (The userkey authentication plugin is disabled.) */ public function test_throwing_plugin_disabled_exception() { $this->setAdminUser(); @@ -60,6 +56,10 @@ class auth_userkey_externallib_testcase extends advanced_testcase { $params = array( 'bla' => 'exists@test.com', ); + + $this->expectException(webservice_access_exception::class); + $this->expectExceptionMessage('Access control exception (The userkey authentication plugin is disabled.)'); + // Simulate the web service server. $result = auth_userkey_external::request_login_url($params); $result = external_api::clean_returnvalue(auth_userkey_external::request_login_url_returns(), $result); @@ -146,9 +146,6 @@ class auth_userkey_externallib_testcase extends advanced_testcase { /** * Test call with missing email required parameter. - * - * @expectedException invalid_parameter_exception - * @expectedExceptionMessage Invalid parameter value detected (Required field "email" is not set or empty.) */ public function test_exception_thrown_if_required_parameter_email_is_not_set() { global $CFG; @@ -160,14 +157,14 @@ class auth_userkey_externallib_testcase extends advanced_testcase { 'bla' => 'exists@test.com', ); + $this->expectException(invalid_parameter_exception::class); + $this->expectExceptionMessage('Invalid parameter value detected (Required field "email" is not set or empty.)'); + auth_userkey_external::request_login_url($params); } /** * Test call with missing ip required parameter. - * - * @expectedException invalid_parameter_exception - * @expectedExceptionMessage Invalid parameter value detected (Required parameter "ip" is not set.) */ public function test_exception_thrown_if_required_parameter_op_is_not_set() { global $CFG; @@ -181,14 +178,14 @@ class auth_userkey_externallib_testcase extends advanced_testcase { 'email' => 'exists@test.com', ); + $this->expectException(invalid_parameter_exception::class); + $this->expectExceptionMessage('Invalid parameter value detected (Required parameter "ip" is not set.)'); + auth_userkey_external::request_login_url($params); } /** * Test request for a user who is not exist. - * - * @expectedException invalid_parameter_exception - * @expectedExceptionMessage Invalid parameter value detected (User is not exist) */ public function test_request_not_existing_user() { global $CFG; @@ -200,6 +197,9 @@ class auth_userkey_externallib_testcase extends advanced_testcase { 'email' => 'notexists@test.com', ); + $this->expectException(invalid_parameter_exception::class); + $this->expectExceptionMessage('Invalid parameter value detected (User is not exist)'); + // Simulate the web service server. $result = auth_userkey_external::request_login_url($params); $result = external_api::clean_returnvalue(auth_userkey_external::request_login_url_returns(), $result); @@ -207,9 +207,6 @@ class auth_userkey_externallib_testcase extends advanced_testcase { /** * Test that permission exception gets thrown if user doesn't have required permissions. - * - * @expectedException required_capability_exception - * @expectedExceptionMessage Sorry, but you do not currently have permissions to do that (Generate login user key) */ public function test_throwing_of_permission_exception() { global $CFG; @@ -221,6 +218,9 @@ class auth_userkey_externallib_testcase extends advanced_testcase { 'email' => 'notexists@test.com', ); + $this->expectException(required_capability_exception::class); + $this->expectExceptionMessage('Sorry, but you do not currently have permissions to do that (Generate login user key)'); + // Simulate the web service server. $result = auth_userkey_external::request_login_url($params); $result = external_api::clean_returnvalue(auth_userkey_external::request_login_url_returns(), $result); diff --git a/tests/phpunit/fake_userkey_manager.php b/tests/phpunit/fake_userkey_manager.php index 3120be7..5c554c2 100644 --- a/tests/phpunit/fake_userkey_manager.php +++ b/tests/phpunit/fake_userkey_manager.php @@ -14,6 +14,8 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +namespace auth_userkey; + /** * Fake userkey manager for testing. * @@ -21,20 +23,35 @@ * @copyright 2016 Dmitrii Metelkin (dmitriim@catalyst-au.net) * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ - -namespace auth_userkey; - class fake_userkey_manager implements userkey_manager_interface { + /** + * Create key. + * + * @param int $userid user ID. + * @param null $allowedips Allowed IPs. + * + * @return string + */ public function create_key($userid, $allowedips = null) { return 'FaKeKeyFoRtEsTiNg'; } + /** + * Delete keys for a user. + * + * @param int $userid User ID to delete keys for. + */ public function delete_keys($userid) { - // TODO: Implement delete_keys() method. } + /** + * Validate provided key. + * + * @param string $keyvalue Key to validate. + * + * @return object|void + */ public function validate_key($keyvalue) { - // TODO: Implement validate_key() method. } }