Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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.
13
//
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/>.
16
 
17
namespace core_cache;
18
 
19
/**
20
 * Unit tests for {@see allow_temporary_caches}.
21
 *
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
27
 */
28
class allow_temporary_caches_test extends \advanced_testcase {
29
 
30
    /**
31
     * Tests whether temporary caches are allowed.
32
     */
33
    public function test_is_allowed(): void {
34
        // Not allowed by default.
35
        $this->assertFalse(allow_temporary_caches::is_allowed());
36
 
37
        // Allowed if we have an instance.
38
        $frog = new allow_temporary_caches();
39
        $this->assertTrue(allow_temporary_caches::is_allowed());
40
 
41
        // Or two instances.
42
        $toad = new allow_temporary_caches();
43
        $this->assertTrue(allow_temporary_caches::is_allowed());
44
 
45
        // Get rid of the instances.
46
        unset($frog);
47
        $this->assertTrue(allow_temporary_caches::is_allowed());
48
 
49
        // Not allowed when we get back to no instances.
50
        unset($toad);
51
        $this->assertFalse(allow_temporary_caches::is_allowed());
52
 
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());
56
    }
57
 
58
    /**
59
     * Function call to demonstrate that you don't need to manually unset the variable.
60
     */
61
    protected function inner_is_allowed(): void {
62
        $gecko = new allow_temporary_caches();
63
        $this->assertTrue(allow_temporary_caches::is_allowed());
64
    }
65
 
66
    /**
67
     * Tests that the temporary caches actually work, including normal and versioned get and set.
68
     */
69
    public function test_temporary_cache(): void {
70
        $this->resetAfterTest();
71
 
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));
81
 
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));
89
 
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);
97
        }
98
    }
99
}