Skip to content

Commit

Permalink
MDL-60342 test: Reset user agent after tests
Browse files Browse the repository at this point in the history
- Add a test that proves this useragent is not being reset after a test
- Reset user agent
  • Loading branch information
patkira committed Oct 18, 2017
1 parent 298c136 commit 74ee81e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/phpunit/classes/util.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ public static function reset_all_data($detectchanges = false) {
// 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()');
Expand Down
19 changes: 19 additions & 0 deletions lib/phpunit/tests/advanced_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -662,4 +662,23 @@ public function test_locale_reset() {
$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.');
}
}

0 comments on commit 74ee81e

Please sign in to comment.