From 5a97f26be9b21c2dee3b75666acd2dceb060e51f Mon Sep 17 00:00:00 2001 From: Pat Kira Date: Wed, 20 Sep 2017 11:20:21 +1000 Subject: [PATCH] MDL-60342 test: Reset user agent after tests - Add a test that proves this useragent is not being reset after a test - Reset user agent --- lib/phpunit/classes/util.php | 3 +++ lib/phpunit/tests/advanced_test.php | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/lib/phpunit/classes/util.php b/lib/phpunit/classes/util.php index 716ab3f629a..c257f63eac9 100644 --- a/lib/phpunit/classes/util.php +++ b/lib/phpunit/classes/util.php @@ -278,6 +278,9 @@ class phpunit_util extends testing_util { // Reset the log manager cache. get_log_manager(true); + // Reset user agent. + core_useragent::instance(true, null); + // verify db writes just in case something goes wrong in reset if (self::$lastdbwrites != $DB->perf_get_writes()) { error_log('Unexpected DB writes in phpunit_util::reset_all_data()'); diff --git a/lib/phpunit/tests/advanced_test.php b/lib/phpunit/tests/advanced_test.php index e3dc965879a..a69477edaf9 100644 --- a/lib/phpunit/tests/advanced_test.php +++ b/lib/phpunit/tests/advanced_test.php @@ -662,4 +662,23 @@ class core_phpunit_advanced_testcase extends advanced_testcase { $this->assertSame('en_AU.UTF-8', setlocale(LC_TIME, 0)); } } + + /** + * This test sets a user agent and makes sure that it is cleared when the test is reset. + */ + public function test_it_resets_useragent_after_test() { + $this->resetAfterTest(); + $fakeagent = 'New user agent set.'; + + // Sanity check: it should not be set when test begins. + self::assertFalse(core_useragent::get_user_agent_string(), 'It should not be set at first.'); + + // Set a fake useragent and check it was set properly. + core_useragent::instance(true, $fakeagent); + self::assertSame($fakeagent, core_useragent::get_user_agent_string(), 'It should be the forced agent.'); + + // Reset test data and ansure the useragent was cleaned. + self::resetAllData(false); + self::assertFalse(core_useragent::get_user_agent_string(), 'It should not be set again, data was reset.'); + } }