Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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
 */
1441 ariadna 28
final class allow_temporary_caches_test extends \advanced_testcase {
1 efrain 29
    /**
30
     * Tests whether temporary caches are allowed.
31
     */
32
    public function test_is_allowed(): void {
33
        // Not allowed by default.
34
        $this->assertFalse(allow_temporary_caches::is_allowed());
35
 
36
        // Allowed if we have an instance.
37
        $frog = new allow_temporary_caches();
38
        $this->assertTrue(allow_temporary_caches::is_allowed());
39
 
40
        // Or two instances.
41
        $toad = new allow_temporary_caches();
42
        $this->assertTrue(allow_temporary_caches::is_allowed());
43
 
44
        // Get rid of the instances.
45
        unset($frog);
46
        $this->assertTrue(allow_temporary_caches::is_allowed());
47
 
48
        // Not allowed when we get back to no instances.
49
        unset($toad);
50
        $this->assertFalse(allow_temporary_caches::is_allowed());
51
 
52
        // Check it works to automatically free up the instance when variable goes out of scope.
53
        $this->inner_is_allowed();
54
        $this->assertFalse(allow_temporary_caches::is_allowed());
55
    }
56
 
57
    /**
58
     * Function call to demonstrate that you don't need to manually unset the variable.
59
     */
60
    protected function inner_is_allowed(): void {
61
        $gecko = new allow_temporary_caches();
62
        $this->assertTrue(allow_temporary_caches::is_allowed());
63
    }
64
 
65
    /**
66
     * Tests that the temporary caches actually work, including normal and versioned get and set.
67
     */
68
    public function test_temporary_cache(): void {
69
        $this->resetAfterTest();
70
 
71
        // Disable the cache.
72
        \cache_phpunit_factory::phpunit_disable();
73
        try {
74
            // Try using the cache now - it returns false/null for everything.
75
            $cache = \cache::make('core', 'coursemodinfo');
76
            $cache->set('frog', 'ribbit');
77
            $this->assertFalse($cache->get('frog'));
78
            $cache->set_versioned('toad', 2, 'croak');
79
            $this->assertFalse($cache->get_versioned('toad', 2));
80
 
81
            // But when we allow temporary caches, it should work as normal.
82
            $allow = new allow_temporary_caches();
83
            $cache = \cache::make('core', 'coursemodinfo');
84
            $cache->set('frog', 'ribbit');
85
            $this->assertEquals('ribbit', $cache->get('frog'));
86
            $cache->set_versioned('toad', 2, 'croak');
87
            $this->assertEquals('croak', $cache->get_versioned('toad', 2));
88
 
89
            // Let's actually use modinfo, to check it works with locking too.
90
            $course = $this->getDataGenerator()->create_course();
91
            get_fast_modinfo($course);
92
        } finally {
93
            // You have to do this after phpunit_disable or it breaks later tests.
1441 ariadna 94
            factory::reset();
95
            factory::instance(true);
1 efrain 96
        }
97
    }
98
}