MDL-59595 admin: unit test covering the adminroot cache reset scenario

The adminroot cache should be reset when the current user is changed,
which can occur during cron or during unit tests.
This commit is contained in:
Jake Dallimore
2018-07-24 08:33:04 +08:00
parent 2d6cb43e1e
commit 3bbb669729
+24
View File
@@ -414,4 +414,28 @@ class core_admintree_testcase extends advanced_testcase {
$this->assertEquals('These entries are invalid: nonvalid site name', $adminsetting->write_setting('nonvalid site name'));
$this->assertEquals('Empty lines are not valid', $adminsetting->write_setting("localhost\n"));
}
/**
* Verifies the $ADMIN global (adminroot cache) is properly reset when changing users, which might occur naturally during cron.
*/
public function test_adminroot_cache_reset() {
$this->resetAfterTest();
global $DB;
// Current user is a manager at site context, which won't have access to the 'debugging' section of the admin tree.
$manageruser = $this->getDataGenerator()->create_user();
$context = context_system::instance();
$managerrole = $DB->get_record('role', array('shortname' => 'manager'));
role_assign($managerrole->id, $manageruser->id, $context->id);
$this->setUser($manageruser);
$adminroot = admin_get_root();
$section = $adminroot->locate('debugging');
$this->assertEmpty($section);
// Now, change the user to an admin user and confirm we get a new copy of the admin tree when next we ask for it.
$adminuser = get_admin();
$this->setUser($adminuser);
$adminroot = admin_get_root();
$section = $adminroot->locate('debugging');
$this->assertInstanceOf('\admin_settingpage', $section);
}
}