Files
moodle/cache/tests/allow_temporary_caches_test.php
T
sam marshall 0b2c2a1f95 MDL-76129 upgrade: Allow caching in specific functions
During install/upgrade, caching is disabled. This change provides a
way to temporarily enable caching (using in-memory cache storage only)
within a specific function; caches are deleted afterwards.

Adding this to two locations improves install performance quite a lot.

Caching is not enabled during the parts of plugin installation that
can vary for individual plugins (install.php/upgrade.php) as these
might be relying on its absence, for example by making direct database
changes.
2022-12-22 11:05:45 +00:00

66 lines
2.2 KiB
PHP

<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_cache;
/**
* Unit tests for {@see allow_temporary_caches}.
*
* @package core_cache
* @category test
* @copyright 2022 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \core_cache\allow_temporary_caches
*/
class allow_temporary_caches_test extends \advanced_testcase {
/**
* Tests whether temporary caches are allowed.
*/
public function test_is_allowed(): void {
// Not allowed by default.
$this->assertFalse(allow_temporary_caches::is_allowed());
// Allowed if we have an instance.
$frog = new allow_temporary_caches();
$this->assertTrue(allow_temporary_caches::is_allowed());
// Or two instances.
$toad = new allow_temporary_caches();
$this->assertTrue(allow_temporary_caches::is_allowed());
// Get rid of the instances.
unset($frog);
$this->assertTrue(allow_temporary_caches::is_allowed());
// Not allowed when we get back to no instances.
unset($toad);
$this->assertFalse(allow_temporary_caches::is_allowed());
// Check it works to automatically free up the instance when variable goes out of scope.
$this->inner_is_allowed();
$this->assertFalse(allow_temporary_caches::is_allowed());
}
/**
* Function call to demonstrate that you don't need to manually unset the variable.
*/
protected function inner_is_allowed(): void {
$gecko = new allow_temporary_caches();
$this->assertTrue(allow_temporary_caches::is_allowed());
}
}