Merge branch 'MDL-81449-main' of https://github.com/lucaboesch/moodle
[moodle.git] / cache / tests / allow_temporary_caches_test.php
blobcd1879830f61d0f7a5dbb70b0f92e7abd9e2aaa1
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 namespace core_cache;
19 /**
20 * Unit tests for {@see allow_temporary_caches}.
22 * @package core_cache
23 * @category test
24 * @copyright 2022 The Open University
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 * @covers \core_cache\allow_temporary_caches
28 class allow_temporary_caches_test extends \advanced_testcase {
30 /**
31 * Tests whether temporary caches are allowed.
33 public function test_is_allowed(): void {
34 // Not allowed by default.
35 $this->assertFalse(allow_temporary_caches::is_allowed());
37 // Allowed if we have an instance.
38 $frog = new allow_temporary_caches();
39 $this->assertTrue(allow_temporary_caches::is_allowed());
41 // Or two instances.
42 $toad = new allow_temporary_caches();
43 $this->assertTrue(allow_temporary_caches::is_allowed());
45 // Get rid of the instances.
46 unset($frog);
47 $this->assertTrue(allow_temporary_caches::is_allowed());
49 // Not allowed when we get back to no instances.
50 unset($toad);
51 $this->assertFalse(allow_temporary_caches::is_allowed());
53 // Check it works to automatically free up the instance when variable goes out of scope.
54 $this->inner_is_allowed();
55 $this->assertFalse(allow_temporary_caches::is_allowed());
58 /**
59 * Function call to demonstrate that you don't need to manually unset the variable.
61 protected function inner_is_allowed(): void {
62 $gecko = new allow_temporary_caches();
63 $this->assertTrue(allow_temporary_caches::is_allowed());
66 /**
67 * Tests that the temporary caches actually work, including normal and versioned get and set.
69 public function test_temporary_cache(): void {
70 $this->resetAfterTest();
72 // Disable the cache.
73 \cache_phpunit_factory::phpunit_disable();
74 try {
75 // Try using the cache now - it returns false/null for everything.
76 $cache = \cache::make('core', 'coursemodinfo');
77 $cache->set('frog', 'ribbit');
78 $this->assertFalse($cache->get('frog'));
79 $cache->set_versioned('toad', 2, 'croak');
80 $this->assertFalse($cache->get_versioned('toad', 2));
82 // But when we allow temporary caches, it should work as normal.
83 $allow = new allow_temporary_caches();
84 $cache = \cache::make('core', 'coursemodinfo');
85 $cache->set('frog', 'ribbit');
86 $this->assertEquals('ribbit', $cache->get('frog'));
87 $cache->set_versioned('toad', 2, 'croak');
88 $this->assertEquals('croak', $cache->get_versioned('toad', 2));
90 // Let's actually use modinfo, to check it works with locking too.
91 $course = $this->getDataGenerator()->create_course();
92 get_fast_modinfo($course);
93 } finally {
94 // You have to do this after phpunit_disable or it breaks later tests.
95 \cache_factory::reset();
96 \cache_factory::instance(true);