From 1f8860f189d26b21bf289bab7f5db54c57481bd1 Mon Sep 17 00:00:00 2001 From: Dmitrii Metelkin Date: Sun, 17 Nov 2019 00:19:48 +1100 Subject: [PATCH] Add more tests --- tests/auth_plugin_test.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/auth_plugin_test.php b/tests/auth_plugin_test.php index 0658b87..bb6017f 100644 --- a/tests/auth_plugin_test.php +++ b/tests/auth_plugin_test.php @@ -1053,4 +1053,35 @@ class auth_plugin_userkey_testcase extends advanced_testcase { } } + /** + * Test if a user is logged in and tries to log in again it stays logged in. + */ + public function test_that_already_logged_in_user_stays_logged_in() { + global $DB, $USER, $SESSION; + + $this->setUser($this->user); + $this->assertEquals($USER->id, $this->user->id); + + $key = new stdClass(); + $key->value = 'UserLogin'; + $key->script = 'auth/userkey'; + $key->userid = $this->user->id; + $key->instance = $this->user->id; + $key->iprestriction = null; + $key->validuntil = time() + 300; + $key->timecreated = time(); + $DB->insert_record('user_private_key', $key); + + $_POST['key'] = 'UserLogin'; + + try { + // Using @ is the only way to test this. Thanks moodle! + @$this->auth->user_login_userkey(); + } catch (moodle_exception $e) { + $this->assertEquals($this->user->id, $USER->id); + $this->assertSame(sesskey(), $USER->sesskey); + $this->assertObjectHasAttribute('userkey', $SESSION); + } + } + }