Proyectos de Subversion Moodle

Rev

Rev 11 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 11 Rev 1441
Línea 14... Línea 14...
14
// You should have received a copy of the GNU General Public License
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/>.
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
Línea 16... Línea 16...
16
 
16
 
Línea 17... Línea -...
17
namespace core_cache;
-
 
18
 
-
 
19
use cache;
-
 
20
use cache_application;
-
 
21
use cache_config;
17
namespace core_cache;
22
use cache_config_disabled;
-
 
23
use cache_config_testing;
-
 
24
use cache_definition;
-
 
25
use cache_disabled;
-
 
26
use cache_factory;
-
 
27
use cache_factory_disabled;
-
 
28
use cache_helper;
18
 
29
use cache_loader;
19
use cache_config_testing;
30
use cache_phpunit_application;
20
use cache_phpunit_application;
31
use cache_phpunit_cache;
21
use cache_phpunit_cache;
32
use cache_phpunit_dummy_object;
22
use cache_phpunit_dummy_object;
33
use cache_phpunit_dummy_overrideclass;
23
use cache_phpunit_dummy_overrideclass;
34
use cache_phpunit_factory;
24
use cache_phpunit_factory;
35
use cache_phpunit_request;
25
use cache_phpunit_request;
36
use cache_phpunit_session;
26
use cache_phpunit_session;
37
use cache_request;
27
use cachestore_file;
-
 
28
use cachestore_session;
38
use cache_session;
29
use cachestore_static;
Línea 39... Línea 30...
39
use cache_store;
30
use core\exception\coding_exception;
40
use cacheable_object_array;
31
use core\exception\moodle_exception;
41
 
32
 
42
/**
33
/**
43
 * PHPunit tests for the cache API
34
 * PHPunit tests for the cache API
44
 *
35
 *
45
 * This file is part of Moodle's cache API, affectionately called MUC.
36
 * This file is part of Moodle's cache API, affectionately called MUC.
46
 * It contains the components that are requried in order to use caching.
37
 * It contains the components that are requried in order to use caching.
47
 *
38
 *
48
 * @package    core
39
 * @package    core_cache
49
 * @category   cache
40
 * @category   cache
50
 * @copyright  2012 Sam Hemelryk
-
 
51
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41
 * @copyright  2012 Sam Hemelryk
52
 * @coversDefaultClass \cache
42
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
53
 * @covers \cache
-
 
54
 */
43
 * @covers \core_cache\cache
55
class cache_test extends \advanced_testcase {
44
 */
56
 
45
final class cache_test extends \advanced_testcase {
57
    /**
46
    /**
58
     * Load required libraries and fixtures.
47
     * Load required libraries and fixtures.
Línea 59... Línea -...
59
     */
-
 
60
    public static function setUpBeforeClass(): void {
48
     */
61
        global $CFG;
49
    public static function setUpBeforeClass(): void {
-
 
50
        global $CFG;
62
 
51
 
Línea 63... Línea 52...
63
        require_once($CFG->dirroot . '/cache/locallib.php');
52
        require_once($CFG->dirroot . '/cache/tests/fixtures/lib.php');
64
        require_once($CFG->dirroot . '/cache/tests/fixtures/lib.php');
53
        require_once($CFG->dirroot . '/cache/tests/fixtures/cache_phpunit_dummy_datasource_versionable.php');
65
        require_once($CFG->dirroot . '/cache/tests/fixtures/cache_phpunit_dummy_datasource_versionable.php');
54
        parent::setUpBeforeClass();
66
    }
55
    }
67
 
56
 
68
    /**
57
    /**
69
     * Set things back to the default before each test.
58
     * Set things back to the default before each test.
70
     */
59
     */
Línea 71... Línea 60...
71
    public function setUp(): void {
60
    public function setUp(): void {
72
        parent::setUp();
61
        parent::setUp();
73
        cache_factory::reset();
62
        factory::reset();
74
        cache_config_testing::create_default_configuration();
63
        cache_config_testing::create_default_configuration();
75
    }
64
    }
76
 
65
 
-
 
66
    /**
-
 
67
     * Final task is to reset the cache system
-
 
68
     */
-
 
69
    public static function tearDownAfterClass(): void {
-
 
70
        parent::tearDownAfterClass();
-
 
71
        factory::reset();
-
 
72
    }
-
 
73
 
-
 
74
    /**
-
 
75
     * Check if the $CFG->altcacheconfigpath tests can be run and skip the test if not.
-
 
76
     *
-
 
77
     * @throws \PHPUnit\Framework\SkippedTestError if the test is skipped
-
 
78
     */
-
 
79
    protected function skip_if_empty_alt_cache_path(): void {
-
 
80
        if ((defined('TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH') && TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH)) {
77
    /**
81
            if (!empty($CFG->altcacheconfigpath)) {
Línea 78... Línea 82...
78
     * Final task is to reset the cache system
82
                // We can't run this test as it requires us to delete the cache configuration script which we just
79
     */
83
                // cant do with a custom path in play.
-
 
84
                $this->markTestSkipped('Skipped testing cache disable functionality as alt cache path is being used.');
80
    public static function tearDownAfterClass(): void {
85
            }
81
        parent::tearDownAfterClass();
86
        }
82
        cache_factory::reset();
87
    }
83
    }
88
 
84
 
89
    /**
Línea 85... Línea 90...
85
    /**
90
     * Returns the expected application cache store.
86
     * Returns the expected application cache store.
91
     *
87
     * @return string
92
     * @return string
88
     */
93
     */
Línea 89... Línea 94...
89
    protected function get_expected_application_cache_store() {
94
    protected function get_expected_application_store(): string {
-
 
95
        global $CFG;
-
 
96
        $expected = cachestore_file::class;
-
 
97
 
90
        global $CFG;
98
        // Verify if we are using any of the available ways to use a different application store within tests.
91
        $expected = 'cachestore_file';
99
        if (!defined('TEST_CACHE_USING_APPLICATION_STORE') || empty(TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH)) {
92
 
100
            return $expected;
93
        // Verify if we are using any of the available ways to use a different application store within tests.
101
        }
94
        if (defined('TEST_CACHE_USING_APPLICATION_STORE') && preg_match('#[a-zA-Z][a-zA-Z0-9_]*#', TEST_CACHE_USING_APPLICATION_STORE)) {
102
 
95
            // 1st way. Using some of the testing servers.
103
        if (preg_match('#[a-zA-Z][a-zA-Z0-9_]*#', TEST_CACHE_USING_APPLICATION_STORE)) {
96
            $expected = 'cachestore_'.(string)TEST_CACHE_USING_APPLICATION_STORE;
104
            // 1st way. Using some of the testing servers.
97
 
105
            $expected = 'cachestore_' . (string) TEST_CACHE_USING_APPLICATION_STORE;
98
        } else if (defined('TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH') && TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH && !empty($CFG->altcacheconfigpath)) {
106
        } else if (!empty($CFG->altcacheconfigpath)) {
99
            // 2nd way. Using an alternative configuration.
107
            // 2nd way. Using an alternative configuration.
-
 
108
            $defaultstores = helper::get_stores_suitable_for_mode_default();
100
            $defaultstores = cache_helper::get_stores_suitable_for_mode_default();
109
            $instance = config::instance();
101
            $instance = cache_config::instance();
110
            // Iterate over defined mode mappings until we get an application one not being the default.
-
 
111
            foreach ($instance->get_mode_mappings() as $mapping) {
102
            // Iterate over defined mode mappings until we get an application one not being the default.
112
                // If the store is not for application mode, ignore.
103
            foreach ($instance->get_mode_mappings() as $mapping) {
113
                if ($mapping['mode'] !== store::MODE_APPLICATION) {
104
                // If the store is not for application mode, ignore.
114
                    continue;
105
                if ($mapping['mode'] !== cache_store::MODE_APPLICATION) {
115
                }
106
                    continue;
116
                // If the store matches some default mapping store name, ignore.
107
                }
117
                if (array_key_exists($mapping['store'], $defaultstores)) {
Línea 108... Línea 118...
108
                // If the store matches some default mapping store name, ignore.
118
                    if (!empty($defaultstores[$mapping['store']]['default'])) {
109
                if (array_key_exists($mapping['store'], $defaultstores) && !empty($defaultstores[$mapping['store']]['default'])) {
119
                        continue;
Línea 122... Línea 132...
122
     * Tests cache configuration
132
     * Tests cache configuration
123
     */
133
     */
124
    public function test_cache_config(): void {
134
    public function test_cache_config(): void {
125
        global $CFG;
135
        global $CFG;
Línea 126... Línea -...
126
 
-
 
127
        if (defined('TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH') && TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH &&
136
 
128
            !empty($CFG->altcacheconfigpath)) {
-
 
129
            // We need to skip this test - it checks the default config structure, but very likely we arn't using the
-
 
130
            // default config structure here so theres no point in running the test.
-
 
131
            $this->markTestSkipped('Skipped testing default cache config structure as alt cache path is being used.');
-
 
Línea 132... Línea -...
132
        }
-
 
133
 
-
 
134
        if (defined('TEST_CACHE_USING_APPLICATION_STORE')) {
-
 
135
            // We need to skip this test - it checks the default config structure, but very likely we arn't using the
-
 
136
            // default config structure here because we are testing against an alternative application store.
-
 
137
            $this->markTestSkipped('Skipped testing default cache config structure as alt application store is being used.');
-
 
138
        }
137
        $this->skip_if_empty_alt_cache_path();
139
 
138
 
Línea 140... Línea 139...
140
        $instance = cache_config::instance();
139
        $instance = config::instance();
Línea 141... Línea 140...
141
        $this->assertInstanceOf(cache_config_testing::class, $instance);
140
        $this->assertInstanceOf(cache_config_testing::class, $instance);
Línea 159... Línea 158...
159
            $this->assertArrayHasKey('features', $store);
158
            $this->assertArrayHasKey('features', $store);
160
        }
159
        }
Línea 161... Línea 160...
161
 
160
 
162
        $modemappings = $instance->get_mode_mappings();
161
        $modemappings = $instance->get_mode_mappings();
163
        $this->assertCount(3, $modemappings);
162
        $this->assertCount(3, $modemappings);
164
        $modes = array(
163
        $modes = [
165
            cache_store::MODE_APPLICATION => false,
164
            store::MODE_APPLICATION => false,
166
            cache_store::MODE_SESSION => false,
165
            store::MODE_SESSION => false,
167
            cache_store::MODE_REQUEST => false,
166
            store::MODE_REQUEST => false,
168
        );
167
        ];
169
        foreach ($modemappings as $mapping) {
168
        foreach ($modemappings as $mapping) {
170
            // We expect 3 properties.
169
            // We expect 3 properties.
171
            $this->assertCount(3, $mapping);
170
            $this->assertCount(3, $mapping);
172
            // Required attributes = mode + store.
171
            // Required attributes = mode + store.
Línea 197... Línea 196...
197
    /**
196
    /**
198
     * Tests for cache keys that would break on windows.
197
     * Tests for cache keys that would break on windows.
199
     */
198
     */
200
    public function test_windows_nasty_keys(): void {
199
    public function test_windows_nasty_keys(): void {
201
        $instance = cache_config_testing::instance();
200
        $instance = cache_config_testing::instance();
202
        $instance->phpunit_add_definition('phpunit/windowskeytest', array(
201
        $instance->phpunit_add_definition('phpunit/windowskeytest', [
203
            'mode' => cache_store::MODE_APPLICATION,
202
            'mode' => store::MODE_APPLICATION,
204
            'component' => 'phpunit',
203
            'component' => 'phpunit',
205
            'area' => 'windowskeytest',
204
            'area' => 'windowskeytest',
206
            'simplekeys' => true,
205
            'simplekeys' => true,
207
            'simpledata' => true
206
            'simpledata' => true,
208
        ));
207
        ]);
209
        $cache = cache::make('phpunit', 'windowskeytest');
208
        $cache = cache::make('phpunit', 'windowskeytest');
210
        $this->assertTrue($cache->set('contest', 'test data 1'));
209
        $this->assertTrue($cache->set('contest', 'test data 1'));
211
        $this->assertEquals('test data 1', $cache->get('contest'));
210
        $this->assertEquals('test data 1', $cache->get('contest'));
212
    }
211
    }
Línea 216... Línea 215...
216
     *
215
     *
217
     * set_identifiers cannot be called after initial cache instantiation, as you need to create a difference cache.
216
     * set_identifiers cannot be called after initial cache instantiation, as you need to create a difference cache.
218
     */
217
     */
219
    public function test_set_identifiers(): void {
218
    public function test_set_identifiers(): void {
220
        $instance = cache_config_testing::instance();
219
        $instance = cache_config_testing::instance();
221
        $instance->phpunit_add_definition('phpunit/identifier', array(
220
        $instance->phpunit_add_definition('phpunit/identifier', [
222
            'mode' => cache_store::MODE_APPLICATION,
221
            'mode' => store::MODE_APPLICATION,
223
            'component' => 'phpunit',
222
            'component' => 'phpunit',
224
            'area' => 'identifier',
223
            'area' => 'identifier',
225
            'simplekeys' => true,
224
            'simplekeys' => true,
226
            'simpledata' => true,
225
            'simpledata' => true,
227
            'staticacceleration' => true
226
            'staticacceleration' => true,
228
        ));
227
        ]);
229
        $cache = cache::make('phpunit', 'identifier', array('area'));
228
        $cache = cache::make('phpunit', 'identifier', ['area']);
230
        $this->assertTrue($cache->set('contest', 'test data 1'));
229
        $this->assertTrue($cache->set('contest', 'test data 1'));
231
        $this->assertEquals('test data 1', $cache->get('contest'));
230
        $this->assertEquals('test data 1', $cache->get('contest'));
Línea 232... Línea 231...
232
 
231
 
233
        $this->expectException('coding_exception');
232
        $this->expectException(coding_exception::class);
234
        $cache->set_identifiers(array());
233
        $cache->set_identifiers([]);
Línea 235... Línea 234...
235
    }
234
    }
236
 
235
 
237
    /**
236
    /**
238
     * Tests the default application cache
237
     * Tests the default application cache
239
     */
238
     */
240
    public function test_default_application_cache(): void {
239
    public function test_default_application_cache(): void {
241
        $cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'phpunit', 'applicationtest');
240
        $cache = cache::make_from_params(store::MODE_APPLICATION, 'phpunit', 'applicationtest');
Línea 242... Línea 241...
242
        $this->assertInstanceOf(cache_application::class, $cache);
241
        $this->assertInstanceOf(application_cache::class, $cache);
243
        $this->run_on_cache($cache);
242
        $this->run_on_cache($cache);
244
 
243
 
245
        $instance = cache_config_testing::instance(true);
244
        $instance = cache_config_testing::instance();
246
        $instance->phpunit_add_definition('phpunit/test_default_application_cache', array(
245
        $instance->phpunit_add_definition('phpunit/test_default_application_cache', [
247
            'mode' => cache_store::MODE_APPLICATION,
246
            'mode' => store::MODE_APPLICATION,
248
            'component' => 'phpunit',
247
            'component' => 'phpunit',
249
            'area' => 'test_default_application_cache',
248
            'area' => 'test_default_application_cache',
250
            'staticacceleration' => true,
249
            'staticacceleration' => true,
251
            'staticaccelerationsize' => 1
250
            'staticaccelerationsize' => 1,
252
        ));
251
        ]);
253
        $cache = cache::make('phpunit', 'test_default_application_cache');
252
        $cache = cache::make('phpunit', 'test_default_application_cache');
Línea 254... Línea 253...
254
        $this->assertInstanceOf(cache_application::class, $cache);
253
        $this->assertInstanceOf(application_cache::class, $cache);
255
        $this->run_on_cache($cache);
254
        $this->run_on_cache($cache);
256
    }
255
    }
257
 
256
 
258
    /**
257
    /**
259
     * Tests the default session cache
258
     * Tests the default session cache
260
     */
259
     */
261
    public function test_default_session_cache(): void {
260
    public function test_default_session_cache(): void {
Línea 262... Línea 261...
262
        $cache = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'applicationtest');
261
        $cache = cache::make_from_params(store::MODE_SESSION, 'phpunit', 'applicationtest');
263
        $this->assertInstanceOf(cache_session::class, $cache);
262
        $this->assertInstanceOf(session_cache::class, $cache);
264
        $this->run_on_cache($cache);
263
        $this->run_on_cache($cache);
265
    }
264
    }
266
 
265
 
267
    /**
266
    /**
268
     * Tests the default request cache
267
     * Tests the default request cache
269
     */
268
     */
Línea 270... Línea 269...
270
    public function test_default_request_cache(): void {
269
    public function test_default_request_cache(): void {
271
        $cache = cache::make_from_params(cache_store::MODE_REQUEST, 'phpunit', 'applicationtest');
270
        $cache = cache::make_from_params(store::MODE_REQUEST, 'phpunit', 'applicationtest');
272
        $this->assertInstanceOf(cache_request::class, $cache);
271
        $this->assertInstanceOf(request_cache::class, $cache);
273
        $this->run_on_cache($cache);
272
        $this->run_on_cache($cache);
274
    }
273
    }
275
 
274
 
276
    /**
275
    /**
277
     * Tests using a cache system when there are no stores available (who knows what the admin did to achieve this).
276
     * Tests using a cache system when there are no stores available (who knows what the admin did to achieve this).
278
     */
277
     */
279
    public function test_on_cache_without_store(): void {
278
    public function test_on_cache_without_store(): void {
280
        $instance = cache_config_testing::instance(true);
279
        $instance = cache_config_testing::instance();
281
        $instance->phpunit_add_definition('phpunit/nostoretest1', array(
280
        $instance->phpunit_add_definition('phpunit/nostoretest1', [
282
            'mode' => cache_store::MODE_APPLICATION,
281
            'mode' => store::MODE_APPLICATION,
283
            'component' => 'phpunit',
282
            'component' => 'phpunit',
284
            'area' => 'nostoretest1',
283
            'area' => 'nostoretest1',
285
        ));
284
        ]);
286
        $instance->phpunit_add_definition('phpunit/nostoretest2', array(
285
        $instance->phpunit_add_definition('phpunit/nostoretest2', [
Línea 287... Línea 286...
287
            'mode' => cache_store::MODE_APPLICATION,
286
            'mode' => store::MODE_APPLICATION,
288
            'component' => 'phpunit',
287
            'component' => 'phpunit',
Línea 301... Línea 300...
301
    /**
300
    /**
302
     * Runs a standard series of access and use tests on a cache instance.
301
     * Runs a standard series of access and use tests on a cache instance.
303
     *
302
     *
304
     * This function is great because we can use it to ensure all of the loaders perform exactly the same way.
303
     * This function is great because we can use it to ensure all of the loaders perform exactly the same way.
305
     *
304
     *
306
     * @param cache_loader $cache
305
     * @param loader_interface $cache
307
     */
306
     */
308
    protected function run_on_cache(cache_loader $cache) {
307
    protected function run_on_cache(loader_interface $cache) {
309
        $key = 'contestkey';
308
        $key = 'contestkey';
310
        $datascalars = array('test data', null);
309
        $datascalars = ['test data', null];
311
        $dataarray = array('contest' => 'data', 'part' => 'two');
310
        $dataarray = ['contest' => 'data', 'part' => 'two'];
312
        $dataobject = (object)$dataarray;
311
        $dataobject = (object)$dataarray;
Línea 313... Línea 312...
313
 
312
 
314
        foreach ($datascalars as $datascalar) {
313
        foreach ($datascalars as $datascalar) {
Línea 315... Línea 314...
315
            $this->assertTrue($cache->purge());
314
            $this->assertTrue($cache->purge());
316
 
315
 
317
            // Check all read methods.
316
            // Check all read methods.
318
            $this->assertFalse($cache->get($key));
317
            $this->assertFalse($cache->get($key));
319
            $this->assertFalse($cache->has($key));
318
            $this->assertFalse($cache->has($key));
320
            $result = $cache->get_many(array($key));
319
            $result = $cache->get_many([$key]);
321
            $this->assertCount(1, $result);
320
            $this->assertCount(1, $result);
322
            $this->assertFalse(reset($result));
321
            $this->assertFalse(reset($result));
Línea 323... Línea 322...
323
            $this->assertFalse($cache->has_any(array($key)));
322
            $this->assertFalse($cache->has_any([$key]));
324
            $this->assertFalse($cache->has_all(array($key)));
323
            $this->assertFalse($cache->has_all([$key]));
325
 
324
 
326
            // Set the data.
325
            // Set the data.
Línea 327... Línea 326...
327
            $this->assertTrue($cache->set($key, $datascalar));
326
            $this->assertTrue($cache->set($key, $datascalar));
328
            // Setting it more than once should be permitted.
327
            // Setting it more than once should be permitted.
329
            $this->assertTrue($cache->set($key, $datascalar));
328
            $this->assertTrue($cache->set($key, $datascalar));
330
 
329
 
331
            // Recheck the read methods.
330
            // Recheck the read methods.
332
            $this->assertEquals($datascalar, $cache->get($key));
331
            $this->assertEquals($datascalar, $cache->get($key));
333
            $this->assertTrue($cache->has($key));
332
            $this->assertTrue($cache->has($key));
334
            $result = $cache->get_many(array($key));
333
            $result = $cache->get_many([$key]);
Línea 335... Línea 334...
335
            $this->assertCount(1, $result);
334
            $this->assertCount(1, $result);
336
            $this->assertEquals($datascalar, reset($result));
335
            $this->assertEquals($datascalar, reset($result));
Línea 337... Línea 336...
337
            $this->assertTrue($cache->has_any(array($key)));
336
            $this->assertTrue($cache->has_any([$key]));
Línea 362... Línea 361...
362
        $this->assertEquals('blue_ptc_wfc', $result->property2);
361
        $this->assertEquals('blue_ptc_wfc', $result->property2);
363
        $this->assertGreaterThan($starttime, $result->propertytime);
362
        $this->assertGreaterThan($starttime, $result->propertytime);
Línea 364... Línea 363...
364
 
363
 
365
        // Test array of objects.
364
        // Test array of objects.
366
        $specobject = new cache_phpunit_dummy_object('red', 'blue', $starttime);
365
        $specobject = new cache_phpunit_dummy_object('red', 'blue', $starttime);
367
        $data = new cacheable_object_array(array(
366
        $data = new cacheable_object_array([
368
            clone($specobject),
367
            clone($specobject),
369
            clone($specobject),
368
            clone($specobject),
370
            clone($specobject))
-
 
371
        );
369
            clone($specobject), ]);
372
        $this->assertTrue($cache->set($key, $data));
370
        $this->assertTrue($cache->set($key, $data));
373
        $result = $cache->get($key);
371
        $result = $cache->get($key);
374
        $this->assertInstanceOf(cacheable_object_array::class, $result);
372
        $this->assertInstanceOf(cacheable_object_array::class, $result);
375
        $this->assertCount(3, $data);
373
        $this->assertCount(3, $data);
Línea 380... Línea 378...
380
            // Ensure that wake from cache is called in all cases.
378
            // Ensure that wake from cache is called in all cases.
381
            $this->assertGreaterThan($starttime, $item->propertytime);
379
            $this->assertGreaterThan($starttime, $item->propertytime);
382
        }
380
        }
Línea 383... Línea 381...
383
 
381
 
384
        // Test set many.
382
        // Test set many.
385
        $cache->set_many(array('key1' => 'data1', 'key2' => 'data2', 'key3' => null));
383
        $cache->set_many(['key1' => 'data1', 'key2' => 'data2', 'key3' => null]);
386
        $this->assertEquals('data1', $cache->get('key1'));
384
        $this->assertEquals('data1', $cache->get('key1'));
387
        $this->assertEquals('data2', $cache->get('key2'));
385
        $this->assertEquals('data2', $cache->get('key2'));
388
        $this->assertEquals(null, $cache->get('key3'));
386
        $this->assertEquals(null, $cache->get('key3'));
389
        $this->assertTrue($cache->delete('key1'));
387
        $this->assertTrue($cache->delete('key1'));
390
        $this->assertTrue($cache->delete('key2'));
388
        $this->assertTrue($cache->delete('key2'));
Línea 391... Línea 389...
391
        $this->assertTrue($cache->delete('key3'));
389
        $this->assertTrue($cache->delete('key3'));
392
 
390
 
393
        $cache->set_many(array(
391
        $cache->set_many([
394
            'key1' => array(1, 2, 3),
392
            'key1' => [1, 2, 3],
395
            'key2' => array(3, 2, 1),
393
            'key2' => [3, 2, 1],
396
        ));
394
        ]);
397
        $this->assertIsArray($cache->get('key1'));
395
        $this->assertIsArray($cache->get('key1'));
398
        $this->assertIsArray($cache->get('key2'));
396
        $this->assertIsArray($cache->get('key2'));
399
        $this->assertCount(3, $cache->get('key1'));
397
        $this->assertCount(3, $cache->get('key1'));
400
        $this->assertCount(3, $cache->get('key2'));
398
        $this->assertCount(3, $cache->get('key2'));
401
        $this->assertIsArray($cache->get_many(array('key1', 'key2')));
399
        $this->assertIsArray($cache->get_many(['key1', 'key2']));
Línea 402... Línea 400...
402
        $this->assertCount(2, $cache->get_many(array('key1', 'key2')));
400
        $this->assertCount(2, $cache->get_many(['key1', 'key2']));
403
        $this->assertEquals(2, $cache->delete_many(array('key1', 'key2')));
401
        $this->assertEquals(2, $cache->delete_many(['key1', 'key2']));
404
 
402
 
405
        // Test delete many.
403
        // Test delete many.
Línea 406... Línea 404...
406
        $this->assertTrue($cache->set('key1', 'data1'));
404
        $this->assertTrue($cache->set('key1', 'data1'));
407
        $this->assertTrue($cache->set('key2', 'data2'));
405
        $this->assertTrue($cache->set('key2', 'data2'));
408
        $this->assertTrue($cache->set('key3', null));
406
        $this->assertTrue($cache->set('key3', null));
Línea 409... Línea 407...
409
 
407
 
Línea 410... Línea 408...
410
        $this->assertEquals('data1', $cache->get('key1'));
408
        $this->assertEquals('data1', $cache->get('key1'));
411
        $this->assertEquals('data2', $cache->get('key2'));
409
        $this->assertEquals('data2', $cache->get('key2'));
412
        $this->assertEquals(null, $cache->get('key3'));
410
        $this->assertEquals(null, $cache->get('key3'));
Línea 413... Línea 411...
413
 
411
 
414
        $this->assertEquals(3, $cache->delete_many(array('key1', 'key2', 'key3')));
412
        $this->assertEquals(3, $cache->delete_many(['key1', 'key2', 'key3']));
415
 
413
 
416
        $this->assertFalse($cache->get('key1'));
414
        $this->assertFalse($cache->get('key1'));
417
        $this->assertFalse($cache->get('key2'));
415
        $this->assertFalse($cache->get('key2'));
Línea 418... Línea 416...
418
        $this->assertFalse($cache->get('key3'));
416
        $this->assertFalse($cache->get('key3'));
Línea 434... Línea 432...
434
        $this->assertEquals('value', $var->key);
432
        $this->assertEquals('value', $var->key);
Línea 435... Línea 433...
435
 
433
 
Línea 436... Línea 434...
436
        $this->assertTrue($cache->delete('obj'));
434
        $this->assertTrue($cache->delete('obj'));
437
 
435
 
438
        // Deep reference test.
436
        // Deep reference test.
439
        $obj1 = new \stdClass;
437
        $obj1 = new \stdClass();
440
        $obj1->key = 'value';
438
        $obj1->key = 'value';
441
        $obj2 = new \stdClass;
439
        $obj2 = new \stdClass();
442
        $obj2->key = 'test';
440
        $obj2->key = 'test';
443
        $obj3 = new \stdClass;
441
        $obj3 = new \stdClass();
444
        $obj3->key = 'pork';
442
        $obj3->key = 'pork';
445
        $obj1->subobj =& $obj2;
443
        $obj1->subobj =& $obj2;
Línea 457... Línea 455...
457
        $this->assertInstanceOf(\stdClass::class, $var->subobj->subobj);
455
        $this->assertInstanceOf(\stdClass::class, $var->subobj->subobj);
458
        $this->assertEquals('pork', $var->subobj->subobj->key);
456
        $this->assertEquals('pork', $var->subobj->subobj->key);
459
        $this->assertTrue($cache->delete('obj'));
457
        $this->assertTrue($cache->delete('obj'));
Línea 460... Línea 458...
460
 
458
 
461
        // Death reference test... basically we don't want this to die.
459
        // Death reference test... basically we don't want this to die.
462
        $obj = new \stdClass;
460
        $obj = new \stdClass();
463
        $obj->key = 'value';
461
        $obj->key = 'value';
464
        $obj->self =& $obj;
462
        $obj->self =& $obj;
465
        $this->assertTrue($cache->set('obj', $obj));
463
        $this->assertTrue($cache->set('obj', $obj));
466
        $var = $cache->get('obj');
464
        $var = $cache->get('obj');
467
        $this->assertInstanceOf(\stdClass::class, $var);
465
        $this->assertInstanceOf(\stdClass::class, $var);
Línea 468... Línea 466...
468
        $this->assertEquals('value', $var->key);
466
        $this->assertEquals('value', $var->key);
469
 
467
 
470
        // Reference test after retrieve.
468
        // Reference test after retrieve.
471
        $obj = new \stdClass;
469
        $obj = new \stdClass();
Línea 472... Línea 470...
472
        $obj->key = 'value';
470
        $obj->key = 'value';
473
        $this->assertTrue($cache->set('obj', $obj));
471
        $this->assertTrue($cache->set('obj', $obj));
Línea 483... Línea 481...
483
        $this->assertEquals('value', $var2->key);
481
        $this->assertEquals('value', $var2->key);
Línea 484... Línea 482...
484
 
482
 
Línea 485... Línea 483...
485
        $this->assertTrue($cache->delete('obj'));
483
        $this->assertTrue($cache->delete('obj'));
486
 
484
 
487
        // Death reference test on get_many... basically we don't want this to die.
485
        // Death reference test on get_many... basically we don't want this to die.
488
        $obj = new \stdClass;
486
        $obj = new \stdClass();
489
        $obj->key = 'value';
487
        $obj->key = 'value';
490
        $obj->self =& $obj;
488
        $obj->self =& $obj;
491
        $this->assertEquals(1, $cache->set_many(array('obj' => $obj)));
489
        $this->assertEquals(1, $cache->set_many(['obj' => $obj]));
492
        $var = $cache->get_many(array('obj'));
490
        $var = $cache->get_many(['obj']);
Línea 493... Línea 491...
493
        $this->assertInstanceOf(\stdClass::class, $var['obj']);
491
        $this->assertInstanceOf(\stdClass::class, $var['obj']);
494
        $this->assertEquals('value', $var['obj']->key);
492
        $this->assertEquals('value', $var['obj']->key);
495
 
493
 
496
        // Reference test after retrieve.
494
        // Reference test after retrieve.
Línea 497... Línea 495...
497
        $obj = new \stdClass;
495
        $obj = new \stdClass();
498
        $obj->key = 'value';
496
        $obj->key = 'value';
499
        $this->assertEquals(1, $cache->set_many(array('obj' => $obj)));
497
        $this->assertEquals(1, $cache->set_many(['obj' => $obj]));
500
 
498
 
501
        $var1 = $cache->get_many(array('obj'));
499
        $var1 = $cache->get_many(['obj']);
Línea 502... Línea 500...
502
        $this->assertInstanceOf(\stdClass::class, $var1['obj']);
500
        $this->assertInstanceOf(\stdClass::class, $var1['obj']);
503
        $this->assertEquals('value', $var1['obj']->key);
501
        $this->assertEquals('value', $var1['obj']->key);
504
        $var1['obj']->key = 'eulav';
502
        $var1['obj']->key = 'eulav';
Línea 505... Línea 503...
505
        $this->assertEquals('eulav', $var1['obj']->key);
503
        $this->assertEquals('eulav', $var1['obj']->key);
Línea 516... Línea 514...
516
            $this->fail('Exception expected from cache::get using MUST_EXIST');
514
            $this->fail('Exception expected from cache::get using MUST_EXIST');
517
        } catch (\Exception $e) {
515
        } catch (\Exception $e) {
518
            $this->assertTrue(true);
516
            $this->assertTrue(true);
519
        }
517
        }
520
        try {
518
        try {
521
            $cache->get_many(array('exception1', 'exception2'), MUST_EXIST);
519
            $cache->get_many(['exception1', 'exception2'], MUST_EXIST);
522
            $this->fail('Exception expected from cache::get_many using MUST_EXIST');
520
            $this->fail('Exception expected from cache::get_many using MUST_EXIST');
523
        } catch (\Exception $e) {
521
        } catch (\Exception $e) {
524
            $this->assertTrue(true);
522
            $this->assertTrue(true);
525
        }
523
        }
526
        $cache->set('test', 'test');
524
        $cache->set('test', 'test');
527
        try {
525
        try {
528
            $cache->get_many(array('test', 'exception'), MUST_EXIST);
526
            $cache->get_many(['test', 'exception'], MUST_EXIST);
529
            $this->fail('Exception expected from cache::get_many using MUST_EXIST');
527
            $this->fail('Exception expected from cache::get_many using MUST_EXIST');
530
        } catch (\Exception $e) {
528
        } catch (\Exception $e) {
531
            $this->assertTrue(true);
529
            $this->assertTrue(true);
532
        }
530
        }
533
    }
531
    }
Línea 534... Línea 532...
534
 
532
 
535
    /**
533
    /**
536
     * Tests a definition using a data loader
534
     * Tests a definition using a data loader
537
     */
535
     */
538
    public function test_definition_data_loader(): void {
536
    public function test_definition_data_loader(): void {
539
        $instance = cache_config_testing::instance(true);
537
        $instance = cache_config_testing::instance();
540
        $instance->phpunit_add_definition('phpunit/datasourcetest', array(
538
        $instance->phpunit_add_definition('phpunit/datasourcetest', [
541
            'mode' => cache_store::MODE_APPLICATION,
539
            'mode' => store::MODE_APPLICATION,
542
            'component' => 'phpunit',
540
            'component' => 'phpunit',
543
            'area' => 'datasourcetest',
541
            'area' => 'datasourcetest',
544
            'datasource' => 'cache_phpunit_dummy_datasource',
542
            'datasource' => 'cache_phpunit_dummy_datasource',
545
            'datasourcefile' => 'cache/tests/fixtures/lib.php'
543
            'datasourcefile' => 'cache/tests/fixtures/lib.php',
Línea 546... Línea 544...
546
        ));
544
        ]);
547
 
545
 
Línea 548... Línea 546...
548
        $cache = cache::make('phpunit', 'datasourcetest');
546
        $cache = cache::make('phpunit', 'datasourcetest');
549
        $this->assertInstanceOf(cache_application::class, $cache);
547
        $this->assertInstanceOf(application_cache::class, $cache);
550
 
548
 
551
        // Purge it to be sure.
549
        // Purge it to be sure.
Línea 560... Línea 558...
560
        $this->assertEquals('Test has no value really.', $cache->get('Test'));
558
        $this->assertEquals('Test has no value really.', $cache->get('Test'));
Línea 561... Línea 559...
561
 
559
 
562
        // Test multiple values.
560
        // Test multiple values.
563
        $this->assertTrue($cache->purge());
561
        $this->assertTrue($cache->purge());
564
        $this->assertTrue($cache->set('b', 'B'));
562
        $this->assertTrue($cache->set('b', 'B'));
565
        $result = $cache->get_many(array('a', 'b', 'c'));
563
        $result = $cache->get_many(['a', 'b', 'c']);
566
        $this->assertIsArray($result);
564
        $this->assertIsArray($result);
567
        $this->assertCount(3, $result);
565
        $this->assertCount(3, $result);
568
        $this->assertArrayHasKey('a', $result);
566
        $this->assertArrayHasKey('a', $result);
569
        $this->assertArrayHasKey('b', $result);
567
        $this->assertArrayHasKey('b', $result);
Línea 573... Línea 571...
573
        $this->assertEquals('c has no value really.', $result['c']);
571
        $this->assertEquals('c has no value really.', $result['c']);
574
    }
572
    }
Línea 575... Línea 573...
575
 
573
 
576
    /**
574
    /**
577
     * Tests a definition using a data loader with versioned keys.
-
 
578
     *
-
 
579
     * @covers ::get_versioned
-
 
580
     * @covers ::set_versioned
575
     * Tests a definition using a data loader with versioned keys.
581
     */
576
     */
582
    public function test_definition_data_loader_versioned(): void {
577
    public function test_definition_data_loader_versioned(): void {
583
        // Create two definitions, one using a non-versionable data source and the other using
578
        // Create two definitions, one using a non-versionable data source and the other using
584
        // a versionable one.
579
        // a versionable one.
585
        $instance = cache_config_testing::instance(true);
580
        $instance = cache_config_testing::instance();
586
        $instance->phpunit_add_definition('phpunit/datasourcetest1', array(
581
        $instance->phpunit_add_definition('phpunit/datasourcetest1', [
587
            'mode' => cache_store::MODE_APPLICATION,
582
            'mode' => store::MODE_APPLICATION,
588
            'component' => 'phpunit',
583
            'component' => 'phpunit',
589
            'area' => 'datasourcetest1',
584
            'area' => 'datasourcetest1',
590
            'datasource' => 'cache_phpunit_dummy_datasource',
585
            'datasource' => 'cache_phpunit_dummy_datasource',
591
            'datasourcefile' => 'cache/tests/fixtures/lib.php'
586
            'datasourcefile' => 'cache/tests/fixtures/lib.php',
592
        ));
587
        ]);
593
        $instance->phpunit_add_definition('phpunit/datasourcetest2', array(
588
        $instance->phpunit_add_definition('phpunit/datasourcetest2', [
594
            'mode' => cache_store::MODE_APPLICATION,
589
            'mode' => store::MODE_APPLICATION,
595
            'component' => 'phpunit',
590
            'component' => 'phpunit',
596
            'area' => 'datasourcetest2',
591
            'area' => 'datasourcetest2',
597
            'datasource' => 'cache_phpunit_dummy_datasource_versionable',
592
            'datasource' => \cache_phpunit_dummy_datasource_versionable::class,
598
            'datasourcefile' => 'cache/tests/fixtures/lib.php'
593
            'datasourcefile' => 'cache/tests/fixtures/lib.php',
Línea 599... Línea 594...
599
        ));
594
        ]);
600
 
595
 
601
        // The first data source works for normal 'get'.
596
        // The first data source works for normal 'get'.
Línea 602... Línea 597...
602
        $cache1 = cache::make('phpunit', 'datasourcetest1');
597
        $cache1 = cache::make('phpunit', 'datasourcetest1');
603
        $this->assertEquals('Frog has no value really.', $cache1->get('Frog'));
598
        $this->assertEquals('Frog has no value really.', $cache1->get('Frog'));
604
 
599
 
605
        // But it doesn't work for get_versioned.
600
        // But it doesn't work for get_versioned.
606
        try {
601
        try {
607
            $cache1->get_versioned('zombie', 1);
602
            $cache1->get_versioned('zombie', 1);
608
            $this->fail();
603
            $this->fail();
Línea 609... Línea 604...
609
        } catch (\coding_exception $e) {
604
        } catch (coding_exception $e) {
610
            $this->assertStringContainsString('Data source is not versionable', $e->getMessage());
605
            $this->assertStringContainsString('Data source is not versionable', $e->getMessage());
Línea 630... Línea 625...
630
 
625
 
631
    /**
626
    /**
632
     * Tests a definition using an overridden loader
627
     * Tests a definition using an overridden loader
633
     */
628
     */
634
    public function test_definition_overridden_loader(): void {
629
    public function test_definition_overridden_loader(): void {
635
        $instance = cache_config_testing::instance(true);
630
        $instance = cache_config_testing::instance();
636
        $instance->phpunit_add_definition('phpunit/overridetest', array(
631
        $instance->phpunit_add_definition('phpunit/overridetest', [
637
            'mode' => cache_store::MODE_APPLICATION,
632
            'mode' => store::MODE_APPLICATION,
638
            'component' => 'phpunit',
633
            'component' => 'phpunit',
639
            'area' => 'overridetest',
634
            'area' => 'overridetest',
640
            'overrideclass' => 'cache_phpunit_dummy_overrideclass',
635
            'overrideclass' => 'cache_phpunit_dummy_overrideclass',
641
            'overrideclassfile' => 'cache/tests/fixtures/lib.php'
636
            'overrideclassfile' => 'cache/tests/fixtures/lib.php',
642
        ));
637
        ]);
643
        $cache = cache::make('phpunit', 'overridetest');
638
        $cache = cache::make('phpunit', 'overridetest');
644
        $this->assertInstanceOf(cache_phpunit_dummy_overrideclass::class, $cache);
639
        $this->assertInstanceOf(cache_phpunit_dummy_overrideclass::class, $cache);
645
        $this->assertInstanceOf(cache_application::class, $cache);
640
        $this->assertInstanceOf(application_cache::class, $cache);
646
        // Purge it to be sure.
641
        // Purge it to be sure.
647
        $this->assertTrue($cache->purge());
642
        $this->assertTrue($cache->purge());
648
        // It won't be there yet.
643
        // It won't be there yet.
649
        $this->assertFalse($cache->has('Test'));
644
        $this->assertFalse($cache->has('Test'));
Línea 656... Línea 651...
656
    /**
651
    /**
657
     * Test the mappingsonly setting.
652
     * Test the mappingsonly setting.
658
     */
653
     */
659
    public function test_definition_mappings_only(): void {
654
    public function test_definition_mappings_only(): void {
660
        /** @var cache_config_testing $instance */
655
        /** @var cache_config_testing $instance */
661
        $instance = cache_config_testing::instance(true);
656
        $instance = cache_config_testing::instance();
662
        $instance->phpunit_add_definition('phpunit/mappingsonly', array(
657
        $instance->phpunit_add_definition('phpunit/mappingsonly', [
663
            'mode' => cache_store::MODE_APPLICATION,
658
            'mode' => store::MODE_APPLICATION,
664
            'component' => 'phpunit',
659
            'component' => 'phpunit',
665
            'area' => 'mappingsonly',
660
            'area' => 'mappingsonly',
666
            'mappingsonly' => true
661
            'mappingsonly' => true,
667
        ), false);
662
        ], false);
668
        $instance->phpunit_add_definition('phpunit/nonmappingsonly', array(
663
        $instance->phpunit_add_definition('phpunit/nonmappingsonly', [
669
            'mode' => cache_store::MODE_APPLICATION,
664
            'mode' => store::MODE_APPLICATION,
670
            'component' => 'phpunit',
665
            'component' => 'phpunit',
671
            'area' => 'nonmappingsonly',
666
            'area' => 'nonmappingsonly',
672
            'mappingsonly' => false
667
            'mappingsonly' => false,
673
        ), false);
668
        ], false);
Línea 674... Línea 669...
674
 
669
 
675
        $cacheonly = cache::make('phpunit', 'mappingsonly');
670
        $cacheonly = cache::make('phpunit', 'mappingsonly');
676
        $this->assertInstanceOf(cache_application::class, $cacheonly);
671
        $this->assertInstanceOf(application_cache::class, $cacheonly);
Línea 677... Línea 672...
677
        $this->assertEquals('cachestore_dummy', $cacheonly->phpunit_get_store_class());
672
        $this->assertInstanceOf(dummy_cachestore::class, $cacheonly->get_store());
678
 
673
 
679
        $expected = $this->get_expected_application_cache_store();
674
        $expected = $this->get_expected_application_store();
680
        $cachenon = cache::make('phpunit', 'nonmappingsonly');
675
        $cachenon = cache::make('phpunit', 'nonmappingsonly');
681
        $this->assertInstanceOf(cache_application::class, $cachenon);
676
        $this->assertInstanceOf(application_cache::class, $cachenon);
Línea 682... Línea 677...
682
        $this->assertEquals($expected, $cachenon->phpunit_get_store_class());
677
        $this->assertInstanceOf($expected, $cachenon->get_store());
683
    }
678
    }
684
 
679
 
685
    /**
680
    /**
686
     * Test a very basic definition.
681
     * Test a very basic definition.
687
     */
682
     */
688
    public function test_definition(): void {
683
    public function test_definition(): void {
689
        $instance = cache_config_testing::instance();
684
        $instance = cache_config_testing::instance();
690
        $instance->phpunit_add_definition('phpunit/test', array(
685
        $instance->phpunit_add_definition('phpunit/test', [
691
            'mode' => cache_store::MODE_APPLICATION,
686
            'mode' => store::MODE_APPLICATION,
692
            'component' => 'phpunit',
687
            'component' => 'phpunit',
Línea 693... Línea 688...
693
            'area' => 'test',
688
            'area' => 'test',
694
        ));
689
        ]);
695
        $cache = cache::make('phpunit', 'test');
690
        $cache = cache::make('phpunit', 'test');
Línea 703... Línea 698...
703
    /**
698
    /**
704
     * Test a definition using the simple keys.
699
     * Test a definition using the simple keys.
705
     */
700
     */
706
    public function test_definition_simplekeys(): void {
701
    public function test_definition_simplekeys(): void {
707
        $instance = cache_config_testing::instance();
702
        $instance = cache_config_testing::instance();
708
        $instance->phpunit_add_definition('phpunit/simplekeytest', array(
703
        $instance->phpunit_add_definition('phpunit/simplekeytest', [
709
            'mode' => cache_store::MODE_APPLICATION,
704
            'mode' => store::MODE_APPLICATION,
710
            'component' => 'phpunit',
705
            'component' => 'phpunit',
711
            'area' => 'simplekeytest',
706
            'area' => 'simplekeytest',
712
            'simplekeys' => true
707
            'simplekeys' => true,
713
        ));
708
        ]);
714
        $cache = cache::make('phpunit', 'simplekeytest');
709
        $cache = cache::make('phpunit', 'simplekeytest');
Línea 715... Línea 710...
715
 
710
 
716
        $this->assertTrue($cache->set('testkey1', 'test data 1'));
711
        $this->assertTrue($cache->set('testkey1', 'test data 1'));
717
        $this->assertEquals('test data 1', $cache->get('testkey1'));
712
        $this->assertEquals('test data 1', $cache->get('testkey1'));
Línea 728... Línea 723...
728
 
723
 
729
    /**
724
    /**
730
     * Test a negative TTL on an application cache.
725
     * Test a negative TTL on an application cache.
731
     */
726
     */
732
    public function test_application_ttl_negative(): void {
727
    public function test_application_ttl_negative(): void {
733
        $instance = cache_config_testing::instance(true);
728
        $instance = cache_config_testing::instance();
734
        $instance->phpunit_add_definition('phpunit/ttltest', array(
729
        $instance->phpunit_add_definition('phpunit/ttltest', [
735
            'mode' => cache_store::MODE_APPLICATION,
730
            'mode' => store::MODE_APPLICATION,
736
            'component' => 'phpunit',
731
            'component' => 'phpunit',
737
            'area' => 'ttltest',
732
            'area' => 'ttltest',
738
            'ttl' => -86400 // Set to a day in the past to be extra sure.
733
            'ttl' => -86400, // Set to a day in the past to be extra sure.
739
        ));
734
        ]);
740
        $cache = cache::make('phpunit', 'ttltest');
735
        $cache = cache::make('phpunit', 'ttltest');
Línea 741... Línea 736...
741
        $this->assertInstanceOf(cache_application::class, $cache);
736
        $this->assertInstanceOf(application_cache::class, $cache);
742
 
737
 
743
        // Purge it to be sure.
738
        // Purge it to be sure.
744
        $this->assertTrue($cache->purge());
739
        $this->assertTrue($cache->purge());
Línea 750... Línea 745...
750
        $this->assertFalse($cache->has('Test'));
745
        $this->assertFalse($cache->has('Test'));
751
        // Double check by trying to get it.
746
        // Double check by trying to get it.
752
        $this->assertFalse($cache->get('Test'));
747
        $this->assertFalse($cache->get('Test'));
Línea 753... Línea 748...
753
 
748
 
754
        // Test with multiple keys.
749
        // Test with multiple keys.
755
        $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
750
        $this->assertEquals(3, $cache->set_many(['a' => 'A', 'b' => 'B', 'c' => 'C']));
756
        $result = $cache->get_many(array('a', 'b', 'c'));
751
        $result = $cache->get_many(['a', 'b', 'c']);
757
        $this->assertIsArray($result);
752
        $this->assertIsArray($result);
758
        $this->assertCount(3, $result);
753
        $this->assertCount(3, $result);
759
        $this->assertArrayHasKey('a', $result);
754
        $this->assertArrayHasKey('a', $result);
760
        $this->assertArrayHasKey('b', $result);
755
        $this->assertArrayHasKey('b', $result);
761
        $this->assertArrayHasKey('c', $result);
756
        $this->assertArrayHasKey('c', $result);
762
        $this->assertFalse($result['a']);
757
        $this->assertFalse($result['a']);
763
        $this->assertFalse($result['b']);
758
        $this->assertFalse($result['b']);
Línea 764... Línea 759...
764
        $this->assertFalse($result['c']);
759
        $this->assertFalse($result['c']);
765
 
760
 
766
        // Test with multiple keys including missing ones.
761
        // Test with multiple keys including missing ones.
767
        $result = $cache->get_many(array('a', 'c', 'e'));
762
        $result = $cache->get_many(['a', 'c', 'e']);
768
        $this->assertIsArray($result);
763
        $this->assertIsArray($result);
769
        $this->assertCount(3, $result);
764
        $this->assertCount(3, $result);
770
        $this->assertArrayHasKey('a', $result);
765
        $this->assertArrayHasKey('a', $result);
Línea 777... Línea 772...
777
 
772
 
778
    /**
773
    /**
779
     * Test a positive TTL on an application cache.
774
     * Test a positive TTL on an application cache.
780
     */
775
     */
781
    public function test_application_ttl_positive(): void {
776
    public function test_application_ttl_positive(): void {
782
        $instance = cache_config_testing::instance(true);
777
        $instance = cache_config_testing::instance();
783
        $instance->phpunit_add_definition('phpunit/ttltest', array(
778
        $instance->phpunit_add_definition('phpunit/ttltest', [
784
            'mode' => cache_store::MODE_APPLICATION,
779
            'mode' => store::MODE_APPLICATION,
785
            'component' => 'phpunit',
780
            'component' => 'phpunit',
786
            'area' => 'ttltest',
781
            'area' => 'ttltest',
787
            'ttl' => 86400 // Set to a day in the future to be extra sure.
782
            'ttl' => 86400, // Set to a day in the future to be extra sure.
788
        ));
783
        ]);
789
        $cache = cache::make('phpunit', 'ttltest');
784
        $cache = cache::make('phpunit', 'ttltest');
Línea 790... Línea 785...
790
        $this->assertInstanceOf(cache_application::class, $cache);
785
        $this->assertInstanceOf(application_cache::class, $cache);
791
 
786
 
792
        // Purge it to be sure.
787
        // Purge it to be sure.
793
        $this->assertTrue($cache->purge());
788
        $this->assertTrue($cache->purge());
Línea 799... Línea 794...
799
        $this->assertTrue($cache->has('Test'));
794
        $this->assertTrue($cache->has('Test'));
800
        // Double check by trying to get it.
795
        // Double check by trying to get it.
801
        $this->assertEquals('Test', $cache->get('Test'));
796
        $this->assertEquals('Test', $cache->get('Test'));
Línea 802... Línea 797...
802
 
797
 
803
        // Test with multiple keys.
798
        // Test with multiple keys.
804
        $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
799
        $this->assertEquals(3, $cache->set_many(['a' => 'A', 'b' => 'B', 'c' => 'C']));
805
        $result = $cache->get_many(array('a', 'b', 'c'));
800
        $result = $cache->get_many(['a', 'b', 'c']);
806
        $this->assertIsArray($result);
801
        $this->assertIsArray($result);
807
        $this->assertCount(3, $result);
802
        $this->assertCount(3, $result);
808
        $this->assertArrayHasKey('a', $result);
803
        $this->assertArrayHasKey('a', $result);
809
        $this->assertArrayHasKey('b', $result);
804
        $this->assertArrayHasKey('b', $result);
810
        $this->assertArrayHasKey('c', $result);
805
        $this->assertArrayHasKey('c', $result);
811
        $this->assertEquals('A', $result['a']);
806
        $this->assertEquals('A', $result['a']);
812
        $this->assertEquals('B', $result['b']);
807
        $this->assertEquals('B', $result['b']);
Línea 813... Línea 808...
813
        $this->assertEquals('C', $result['c']);
808
        $this->assertEquals('C', $result['c']);
814
 
809
 
815
        // Test with multiple keys including missing ones.
810
        // Test with multiple keys including missing ones.
816
        $result = $cache->get_many(array('a', 'c', 'e'));
811
        $result = $cache->get_many(['a', 'c', 'e']);
817
        $this->assertIsArray($result);
812
        $this->assertIsArray($result);
818
        $this->assertCount(3, $result);
813
        $this->assertCount(3, $result);
819
        $this->assertArrayHasKey('a', $result);
814
        $this->assertArrayHasKey('a', $result);
Línea 826... Línea 821...
826
 
821
 
827
    /**
822
    /**
828
     * Test a negative TTL on an session cache.
823
     * Test a negative TTL on an session cache.
829
     */
824
     */
830
    public function test_session_ttl_positive(): void {
825
    public function test_session_ttl_positive(): void {
831
        $instance = cache_config_testing::instance(true);
826
        $instance = cache_config_testing::instance();
832
        $instance->phpunit_add_definition('phpunit/ttltest', array(
827
        $instance->phpunit_add_definition('phpunit/ttltest', [
833
            'mode' => cache_store::MODE_SESSION,
828
            'mode' => store::MODE_SESSION,
834
            'component' => 'phpunit',
829
            'component' => 'phpunit',
835
            'area' => 'ttltest',
830
            'area' => 'ttltest',
836
            'ttl' => 86400 // Set to a day in the future to be extra sure.
831
            'ttl' => 86400, // Set to a day in the future to be extra sure.
837
        ));
832
        ]);
838
        $cache = cache::make('phpunit', 'ttltest');
833
        $cache = cache::make('phpunit', 'ttltest');
Línea 839... Línea 834...
839
        $this->assertInstanceOf(cache_session::class, $cache);
834
        $this->assertInstanceOf(session_cache::class, $cache);
840
 
835
 
841
        // Purge it to be sure.
836
        // Purge it to be sure.
842
        $this->assertTrue($cache->purge());
837
        $this->assertTrue($cache->purge());
Línea 848... Línea 843...
848
        $this->assertTrue($cache->has('Test'));
843
        $this->assertTrue($cache->has('Test'));
849
        // Double check by trying to get it.
844
        // Double check by trying to get it.
850
        $this->assertEquals('Test', $cache->get('Test'));
845
        $this->assertEquals('Test', $cache->get('Test'));
Línea 851... Línea 846...
851
 
846
 
852
        // Test with multiple keys.
847
        // Test with multiple keys.
853
        $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
848
        $this->assertEquals(3, $cache->set_many(['a' => 'A', 'b' => 'B', 'c' => 'C']));
854
        $result = $cache->get_many(array('a', 'b', 'c'));
849
        $result = $cache->get_many(['a', 'b', 'c']);
855
        $this->assertIsArray($result);
850
        $this->assertIsArray($result);
856
        $this->assertCount(3, $result);
851
        $this->assertCount(3, $result);
857
        $this->assertArrayHasKey('a', $result);
852
        $this->assertArrayHasKey('a', $result);
858
        $this->assertArrayHasKey('b', $result);
853
        $this->assertArrayHasKey('b', $result);
859
        $this->assertArrayHasKey('c', $result);
854
        $this->assertArrayHasKey('c', $result);
860
        $this->assertEquals('A', $result['a']);
855
        $this->assertEquals('A', $result['a']);
861
        $this->assertEquals('B', $result['b']);
856
        $this->assertEquals('B', $result['b']);
Línea 862... Línea 857...
862
        $this->assertEquals('C', $result['c']);
857
        $this->assertEquals('C', $result['c']);
863
 
858
 
864
        // Test with multiple keys including missing ones.
859
        // Test with multiple keys including missing ones.
865
        $result = $cache->get_many(array('a', 'c', 'e'));
860
        $result = $cache->get_many(['a', 'c', 'e']);
866
        $this->assertIsArray($result);
861
        $this->assertIsArray($result);
867
        $this->assertCount(3, $result);
862
        $this->assertCount(3, $result);
868
        $this->assertArrayHasKey('a', $result);
863
        $this->assertArrayHasKey('a', $result);
Línea 876... Línea 871...
876
    /**
871
    /**
877
     * Tests manual locking operations on an application cache
872
     * Tests manual locking operations on an application cache
878
     */
873
     */
879
    public function test_application_manual_locking(): void {
874
    public function test_application_manual_locking(): void {
880
        $instance = cache_config_testing::instance();
875
        $instance = cache_config_testing::instance();
881
        $instance->phpunit_add_definition('phpunit/lockingtest', array(
876
        $instance->phpunit_add_definition('phpunit/lockingtest', [
882
            'mode' => cache_store::MODE_APPLICATION,
877
            'mode' => store::MODE_APPLICATION,
883
            'component' => 'phpunit',
878
            'component' => 'phpunit',
884
            'area' => 'lockingtest'
879
            'area' => 'lockingtest',
885
        ));
880
        ]);
886
        // Configure the lock timeout so the test doesn't take too long to run.
881
        // Configure the lock timeout so the test doesn't take too long to run.
887
        $instance->phpunit_edit_store_config('default_application', ['lockwait' => 2]);
882
        $instance->phpunit_edit_store_config('default_application', ['lockwait' => 2]);
888
        $cache1 = cache::make('phpunit', 'lockingtest');
883
        $cache1 = cache::make('phpunit', 'lockingtest');
889
        $cache2 = clone($cache1);
884
        $cache2 = clone($cache1);
Línea 893... Línea 888...
893
 
888
 
894
        $cache1->acquire_lock('testkey');
889
        $cache1->acquire_lock('testkey');
895
        try {
890
        try {
896
            $cache2->acquire_lock('testkey');
891
            $cache2->acquire_lock('testkey');
897
            $this->fail();
892
            $this->fail();
898
        } catch (\moodle_exception $e) {
893
        } catch (moodle_exception $e) {
-
 
894
            // Check the right exception message, and debug info mentions the store type.
899
            // Check the right exception message, and debug info mentions the store type.
895
            $this->assertMatchesRegularExpression(
900
            $this->assertMatchesRegularExpression('~Unable to acquire a lock.*cachestore_file.*~',
896
                '~Unable to acquire a lock.*cachestore_file.*~',
-
 
897
                $e->getMessage()
901
                    $e->getMessage());
898
            );
Línea 902... Línea 899...
902
        }
899
        }
903
 
900
 
Línea 914... Línea 911...
914
    /**
911
    /**
915
     * Tests application cache event invalidation
912
     * Tests application cache event invalidation
916
     */
913
     */
917
    public function test_application_event_invalidation(): void {
914
    public function test_application_event_invalidation(): void {
918
        $instance = cache_config_testing::instance();
915
        $instance = cache_config_testing::instance();
919
        $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
916
        $instance->phpunit_add_definition('phpunit/eventinvalidationtest', [
920
            'mode' => cache_store::MODE_APPLICATION,
917
            'mode' => store::MODE_APPLICATION,
921
            'component' => 'phpunit',
918
            'component' => 'phpunit',
922
            'area' => 'eventinvalidationtest',
919
            'area' => 'eventinvalidationtest',
923
            'invalidationevents' => array(
920
            'invalidationevents' => [
924
                'crazyevent'
921
                'crazyevent',
925
            )
922
            ],
926
        ));
923
        ]);
927
        $cache = cache::make('phpunit', 'eventinvalidationtest');
924
        $cache = cache::make('phpunit', 'eventinvalidationtest');
Línea 928... Línea 925...
928
 
925
 
929
        $this->assertTrue($cache->set('testkey1', 'test data 1'));
926
        $this->assertTrue($cache->set('testkey1', 'test data 1'));
930
        $this->assertEquals('test data 1', $cache->get('testkey1'));
927
        $this->assertEquals('test data 1', $cache->get('testkey1'));
931
        $this->assertTrue($cache->set('testkey2', 'test data 2'));
928
        $this->assertTrue($cache->set('testkey2', 'test data 2'));
Línea 932... Línea 929...
932
        $this->assertEquals('test data 2', $cache->get('testkey2'));
929
        $this->assertEquals('test data 2', $cache->get('testkey2'));
933
 
930
 
Línea 934... Línea 931...
934
        // Test invalidating a single entry.
931
        // Test invalidating a single entry.
935
        cache_helper::invalidate_by_event('crazyevent', array('testkey1'));
932
        helper::invalidate_by_event('crazyevent', ['testkey1']);
Línea 936... Línea 933...
936
 
933
 
Línea 937... Línea 934...
937
        $this->assertFalse($cache->get('testkey1'));
934
        $this->assertFalse($cache->get('testkey1'));
938
        $this->assertEquals('test data 2', $cache->get('testkey2'));
935
        $this->assertEquals('test data 2', $cache->get('testkey2'));
Línea 939... Línea 936...
939
 
936
 
940
        $this->assertTrue($cache->set('testkey1', 'test data 1'));
937
        $this->assertTrue($cache->set('testkey1', 'test data 1'));
941
 
938
 
Línea 942... Línea 939...
942
        // Test invalidating both entries.
939
        // Test invalidating both entries.
943
        cache_helper::invalidate_by_event('crazyevent', array('testkey1', 'testkey2'));
940
        helper::invalidate_by_event('crazyevent', ['testkey1', 'testkey2']);
944
 
941
 
945
        $this->assertFalse($cache->get('testkey1'));
942
        $this->assertFalse($cache->get('testkey1'));
946
        $this->assertFalse($cache->get('testkey2'));
943
        $this->assertFalse($cache->get('testkey2'));
947
    }
944
    }
948
 
945
 
949
    /**
946
    /**
950
     * Tests session cache event invalidation
947
     * Tests session cache event invalidation
951
     */
948
     */
952
    public function test_session_event_invalidation(): void {
949
    public function test_session_event_invalidation(): void {
953
        $instance = cache_config_testing::instance();
950
        $instance = cache_config_testing::instance();
954
        $instance->phpunit_add_definition('phpunit/test_session_event_invalidation', array(
951
        $instance->phpunit_add_definition('phpunit/test_session_event_invalidation', [
955
            'mode' => cache_store::MODE_SESSION,
952
            'mode' => store::MODE_SESSION,
956
            'component' => 'phpunit',
953
            'component' => 'phpunit',
Línea 957... Línea 954...
957
            'area' => 'test_session_event_invalidation',
954
            'area' => 'test_session_event_invalidation',
958
            'invalidationevents' => array(
955
            'invalidationevents' => [
959
                'crazyevent'
956
                'crazyevent',
960
            )
957
            ],
Línea 961... Línea 958...
961
        ));
958
        ]);
962
        $cache = cache::make('phpunit', 'test_session_event_invalidation');
959
        $cache = cache::make('phpunit', 'test_session_event_invalidation');
Línea 963... Línea 960...
963
        $this->assertInstanceOf(cache_session::class, $cache);
960
        $this->assertInstanceOf(session_cache::class, $cache);
964
 
961
 
Línea 965... Línea 962...
965
        $this->assertTrue($cache->set('testkey1', 'test data 1'));
962
        $this->assertTrue($cache->set('testkey1', 'test data 1'));
Línea 966... Línea 963...
966
        $this->assertEquals('test data 1', $cache->get('testkey1'));
963
        $this->assertEquals('test data 1', $cache->get('testkey1'));
967
        $this->assertTrue($cache->set('testkey2', 'test data 2'));
964
        $this->assertTrue($cache->set('testkey2', 'test data 2'));
Línea 968... Línea 965...
968
        $this->assertEquals('test data 2', $cache->get('testkey2'));
965
        $this->assertEquals('test data 2', $cache->get('testkey2'));
969
 
966
 
970
        // Test invalidating a single entry.
967
        // Test invalidating a single entry.
Línea 971... Línea 968...
971
        cache_helper::invalidate_by_event('crazyevent', array('testkey1'));
968
        helper::invalidate_by_event('crazyevent', ['testkey1']);
972
 
969
 
973
        $this->assertFalse($cache->get('testkey1'));
970
        $this->assertFalse($cache->get('testkey1'));
974
        $this->assertEquals('test data 2', $cache->get('testkey2'));
971
        $this->assertEquals('test data 2', $cache->get('testkey2'));
975
 
972
 
976
        $this->assertTrue($cache->set('testkey1', 'test data 1'));
973
        $this->assertTrue($cache->set('testkey1', 'test data 1'));
977
 
974
 
978
        // Test invalidating both entries.
975
        // Test invalidating both entries.
979
        cache_helper::invalidate_by_event('crazyevent', array('testkey1', 'testkey2'));
976
        helper::invalidate_by_event('crazyevent', ['testkey1', 'testkey2']);
980
 
977
 
981
        $this->assertFalse($cache->get('testkey1'));
978
        $this->assertFalse($cache->get('testkey1'));
982
        $this->assertFalse($cache->get('testkey2'));
979
        $this->assertFalse($cache->get('testkey2'));
983
    }
980
    }
984
 
981
 
985
    /**
982
    /**
Línea 986... Línea 983...
986
     * Tests application cache definition invalidation
983
     * Tests application cache definition invalidation
Línea 987... Línea 984...
987
     */
984
     */
988
    public function test_application_definition_invalidation(): void {
985
    public function test_application_definition_invalidation(): void {
Línea 989... Línea 986...
989
        $instance = cache_config_testing::instance();
986
        $instance = cache_config_testing::instance();
Línea 990... Línea 987...
990
        $instance->phpunit_add_definition('phpunit/definitioninvalidation', array(
987
        $instance->phpunit_add_definition('phpunit/definitioninvalidation', [
Línea 991... Línea 988...
991
            'mode' => cache_store::MODE_APPLICATION,
988
            'mode' => store::MODE_APPLICATION,
992
            'component' => 'phpunit',
989
            'component' => 'phpunit',
Línea 993... Línea 990...
993
            'area' => 'definitioninvalidation'
990
            'area' => 'definitioninvalidation',
Línea 994... Línea 991...
994
        ));
991
        ]);
Línea 995... Línea 992...
995
        $cache = cache::make('phpunit', 'definitioninvalidation');
992
        $cache = cache::make('phpunit', 'definitioninvalidation');
996
        $this->assertTrue($cache->set('testkey1', 'test data 1'));
993
        $this->assertTrue($cache->set('testkey1', 'test data 1'));
997
        $this->assertEquals('test data 1', $cache->get('testkey1'));
994
        $this->assertEquals('test data 1', $cache->get('testkey1'));
Línea 998... Línea 995...
998
        $this->assertTrue($cache->set('testkey2', 'test data 2'));
995
        $this->assertTrue($cache->set('testkey2', 'test data 2'));
999
        $this->assertEquals('test data 2', $cache->get('testkey2'));
996
        $this->assertEquals('test data 2', $cache->get('testkey2'));
1000
 
997
 
1001
        cache_helper::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), 'testkey1');
998
        helper::invalidate_by_definition('phpunit', 'definitioninvalidation', [], 'testkey1');
1002
 
999
 
1003
        $this->assertFalse($cache->get('testkey1'));
1000
        $this->assertFalse($cache->get('testkey1'));
1004
        $this->assertEquals('test data 2', $cache->get('testkey2'));
1001
        $this->assertEquals('test data 2', $cache->get('testkey2'));
1005
 
1002
 
1006
        $this->assertTrue($cache->set('testkey1', 'test data 1'));
1003
        $this->assertTrue($cache->set('testkey1', 'test data 1'));
1007
 
1004
 
1008
        cache_helper::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), array('testkey1'));
1005
        helper::invalidate_by_definition('phpunit', 'definitioninvalidation', [], ['testkey1']);
1009
 
1006
 
1010
        $this->assertFalse($cache->get('testkey1'));
1007
        $this->assertFalse($cache->get('testkey1'));
1011
        $this->assertEquals('test data 2', $cache->get('testkey2'));
1008
        $this->assertEquals('test data 2', $cache->get('testkey2'));
1012
 
1009
 
1013
        $this->assertTrue($cache->set('testkey1', 'test data 1'));
1010
        $this->assertTrue($cache->set('testkey1', 'test data 1'));
Línea 1014... Línea 1011...
1014
 
1011
 
Línea 1015... Línea 1012...
1015
        cache_helper::invalidate_by_definition('phpunit', 'definitioninvalidation', array(), array('testkey1', 'testkey2'));
1012
        helper::invalidate_by_definition('phpunit', 'definitioninvalidation', [], ['testkey1', 'testkey2']);
1016
 
1013
 
Línea 1017... Línea 1014...
1017
        $this->assertFalse($cache->get('testkey1'));
1014
        $this->assertFalse($cache->get('testkey1'));
Línea -... Línea 1015...
-
 
1015
        $this->assertFalse($cache->get('testkey2'));
-
 
1016
    }
1018
        $this->assertFalse($cache->get('testkey2'));
1017
 
-
 
1018
    /**
1019
    }
1019
     * Tests session cache definition invalidation
-
 
1020
     */
Línea 1020... Línea 1021...
1020
 
1021
    public function test_session_definition_invalidation(): void {
1021
    /**
1022
        $instance = cache_config_testing::instance();
Línea 1022... Línea 1023...
1022
     * Tests session cache definition invalidation
1023
        $instance->phpunit_add_definition('phpunit/test_session_definition_invalidation', [
Línea -... Línea 1024...
-
 
1024
            'mode' => store::MODE_SESSION,
-
 
1025
            'component' => 'phpunit',
1023
     */
1026
            'area' => 'test_session_definition_invalidation',
-
 
1027
        ]);
1024
    public function test_session_definition_invalidation(): void {
1028
        $cache = cache::make('phpunit', 'test_session_definition_invalidation');
-
 
1029
        $this->assertInstanceOf(session_cache::class, $cache);
Línea 1025... Línea 1030...
1025
        $instance = cache_config_testing::instance();
1030
        $this->assertTrue($cache->set('testkey1', 'test data 1'));
1026
        $instance->phpunit_add_definition('phpunit/test_session_definition_invalidation', array(
1031
        $this->assertEquals('test data 1', $cache->get('testkey1'));
1027
            'mode' => cache_store::MODE_SESSION,
1032
        $this->assertTrue($cache->set('testkey2', 'test data 2'));
Línea 1065... Línea 1070...
1065
        // This is going to be an intense wee test.
1070
        // This is going to be an intense wee test.
1066
        // We need to add data the to cache, invalidate it by event, manually force it back without MUC knowing to simulate a
1071
        // We need to add data the to cache, invalidate it by event, manually force it back without MUC knowing to simulate a
1067
        // disconnected/distributed setup (think load balanced server using local cache), instantiate the cache again and finally
1072
        // disconnected/distributed setup (think load balanced server using local cache), instantiate the cache again and finally
1068
        // check that it is not picked up.
1073
        // check that it is not picked up.
1069
        $instance = cache_config_testing::instance();
1074
        $instance = cache_config_testing::instance();
1070
        $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
1075
        $instance->phpunit_add_definition('phpunit/eventinvalidationtest', [
1071
            'mode' => cache_store::MODE_APPLICATION,
1076
            'mode' => store::MODE_APPLICATION,
1072
            'component' => 'phpunit',
1077
            'component' => 'phpunit',
1073
            'area' => 'eventinvalidationtest',
1078
            'area' => 'eventinvalidationtest',
1074
            'simplekeys' => true,
1079
            'simplekeys' => true,
1075
            'simpledata' => true,
1080
            'simpledata' => true,
1076
            'invalidationevents' => array(
1081
            'invalidationevents' => [
1077
                'crazyevent'
1082
                'crazyevent',
1078
            )
1083
            ],
1079
        ));
1084
        ]);
1080
        $cache = cache::make('phpunit', 'eventinvalidationtest');
1085
        $cache = cache::make('phpunit', 'eventinvalidationtest');
1081
        $this->assertTrue($cache->set('testkey1', 'test data 1'));
1086
        $this->assertTrue($cache->set('testkey1', 'test data 1'));
1082
        $this->assertEquals('test data 1', $cache->get('testkey1'));
1087
        $this->assertEquals('test data 1', $cache->get('testkey1'));
Línea 1083... Línea 1088...
1083
 
1088
 
Línea 1084... Línea 1089...
1084
        cache_helper::invalidate_by_event('crazyevent', array('testkey1'));
1089
        helper::invalidate_by_event('crazyevent', ['testkey1']);
Línea 1085... Línea 1090...
1085
 
1090
 
1086
        $this->assertFalse($cache->get('testkey1'));
1091
        $this->assertFalse($cache->get('testkey1'));
1087
 
1092
 
-
 
1093
        // OK data added, data invalidated, and invalidation time has been set.
1088
        // OK data added, data invalidated, and invalidation time has been set.
1094
        // Now we need to manually add back the data and adjust the invalidation time.
1089
        // Now we need to manually add back the data and adjust the invalidation time.
1095
        $hash = md5(store::MODE_APPLICATION . '/phpunit/eventinvalidationtest/' . $CFG->wwwroot . 'phpunit');
1090
        $hash = md5(cache_store::MODE_APPLICATION.'/phpunit/eventinvalidationtest/'.$CFG->wwwroot.'phpunit');
1096
        $timefile = $CFG->dataroot .
1091
        $timefile = $CFG->dataroot."/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/las-cache/lastinvalidation-$hash.cache";
1097
            "/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/las-cache/lastinvalidation-$hash.cache";
1092
        // Make sure the file is correct.
1098
        // Make sure the file is correct.
1093
        $this->assertTrue(file_exists($timefile));
1099
        $this->assertTrue(file_exists($timefile));
1094
        $timecont = serialize(cache::now(true) - 60); // Back 60sec in the past to force it to re-invalidate.
1100
        $timecont = serialize(cache::now(true) - 60); // Back 60sec in the past to force it to re-invalidate.
Línea -... Línea 1101...
-
 
1101
        make_writable_directory(dirname($timefile));
1095
        make_writable_directory(dirname($timefile));
1102
        file_put_contents($timefile, $timecont);
1096
        file_put_contents($timefile, $timecont);
1103
        $this->assertTrue(file_exists($timefile));
1097
        $this->assertTrue(file_exists($timefile));
1104
 
1098
 
1105
        $datafile = $CFG->dataroot .
1099
        $datafile = $CFG->dataroot."/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/tes-cache/testkey1-$hash.cache";
1106
            "/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/tes-cache/testkey1-$hash.cache";
Línea 1100... Línea 1107...
1100
        $datacont = serialize("test data 1");
1107
        $datacont = serialize("test data 1");
1101
        make_writable_directory(dirname($datafile));
1108
        make_writable_directory(dirname($datafile));
1102
        file_put_contents($datafile, $datacont);
1109
        file_put_contents($datafile, $datacont);
1103
        $this->assertTrue(file_exists($datafile));
1110
        $this->assertTrue(file_exists($datafile));
1104
 
1111
 
1105
        // Test 1: Rebuild without the event and test its there.
1112
        // Test 1: Rebuild without the event and test its there.
1106
        cache_factory::reset();
1113
        factory::reset();
1107
        $instance = cache_config_testing::instance();
1114
        $instance = cache_config_testing::instance();
1108
        $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
1115
        $instance->phpunit_add_definition('phpunit/eventinvalidationtest', [
1109
            'mode' => cache_store::MODE_APPLICATION,
1116
            'mode' => store::MODE_APPLICATION,
1110
            'component' => 'phpunit',
1117
            'component' => 'phpunit',
1111
            'area' => 'eventinvalidationtest',
1118
            'area' => 'eventinvalidationtest',
Línea 1112... Línea 1119...
1112
            'simplekeys' => true,
1119
            'simplekeys' => true,
1113
            'simpledata' => true,
1120
            'simpledata' => true,
Línea 1114... Línea 1121...
1114
        ));
1121
        ]);
1115
        $cache = cache::make('phpunit', 'eventinvalidationtest');
1122
        $cache = cache::make('phpunit', 'eventinvalidationtest');
1116
        $this->assertEquals('test data 1', $cache->get('testkey1'));
1123
        $this->assertEquals('test data 1', $cache->get('testkey1'));
1117
 
1124
 
1118
        // Test 2: Rebuild and test the invalidation of the event via the invalidation cache.
1125
        // Test 2: Rebuild and test the invalidation of the event via the invalidation cache.
1119
        cache_factory::reset();
1126
        factory::reset();
1120
 
1127
 
1121
        $instance = cache_config_testing::instance();
1128
        $instance = cache_config_testing::instance();
1122
        $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array(
1129
        $instance->phpunit_add_definition('phpunit/eventinvalidationtest', [
1123
            'mode' => cache_store::MODE_APPLICATION,
1130
            'mode' => store::MODE_APPLICATION,
1124
            'component' => 'phpunit',
1131
            'component' => 'phpunit',
Línea 1125... Línea 1132...
1125
            'area' => 'eventinvalidationtest',
1132
            'area' => 'eventinvalidationtest',
1126
            'simplekeys' => true,
1133
            'simplekeys' => true,
Línea 1127... Línea 1134...
1127
            'simpledata' => true,
1134
            'simpledata' => true,
Línea 1144... Línea 1151...
1144
        // Set testkey2 data.
1151
        // Set testkey2 data.
1145
        $cache->set('testkey2', 'test data 2');
1152
        $cache->set('testkey2', 'test data 2');
Línea 1146... Línea 1153...
1146
 
1153
 
1147
        // Backdate the event invalidation time by 30 seconds.
1154
        // Backdate the event invalidation time by 30 seconds.
1148
        $invalidationcache = cache::make('core', 'eventinvalidation');
1155
        $invalidationcache = cache::make('core', 'eventinvalidation');
Línea 1149... Línea 1156...
1149
        $invalidationcache->set('crazyevent', array('testkey2' => cache::now() - 30));
1156
        $invalidationcache->set('crazyevent', ['testkey2' => cache::now() - 30]);
1150
 
1157
 
Línea 1151... Línea 1158...
1151
        // Lastinvalidation should already be cache::now().
1158
        // Lastinvalidation should already be cache::now().
1152
        $this->assertEquals(cache::get_purge_token(), $cache->get('lastinvalidation'));
1159
        $this->assertEquals(cache::get_purge_token(), $cache->get('lastinvalidation'));
1153
 
1160
 
Línea 1154... Línea 1161...
1154
        // Set it to 15 seconds ago so that we know if it changes.
1161
        // Set it to 15 seconds ago so that we know if it changes.
1155
        $pasttime = cache::now(true) - 15;
1162
        $pasttime = cache::now(true) - 15;
1156
        $cache->set('lastinvalidation', $pasttime);
1163
        $cache->set('lastinvalidation', $pasttime);
Línea 1157... Línea 1164...
1157
 
1164
 
1158
        // Make a new cache class.  This should not invalidate anything.
1165
        // Make a new cache class.  This should not invalidate anything.
Línea 1159... Línea 1166...
1159
        cache_factory::instance()->reset_cache_instances();
1166
        factory::instance()->reset_cache_instances();
1160
        $cache = cache::make('phpunit', 'eventinvalidationtest');
1167
        $cache = cache::make('phpunit', 'eventinvalidationtest');
1161
 
1168
 
1162
        // Lastinvalidation shouldn't change since it was already newer than invalidation event.
1169
        // Lastinvalidation shouldn't change since it was already newer than invalidation event.
1163
        $this->assertEquals($pasttime, $cache->get('lastinvalidation'));
1170
        $this->assertEquals($pasttime, $cache->get('lastinvalidation'));
1164
 
1171
 
1165
        // Now set the event invalidation to newer than the lastinvalidation time.
1172
        // Now set the event invalidation to newer than the lastinvalidation time.
Línea 1166... Línea 1173...
1166
        $invalidationcache->set('crazyevent', array('testkey2' => cache::now() - 5));
1173
        $invalidationcache->set('crazyevent', ['testkey2' => cache::now() - 5]);
1167
        // Make a new cache class.  This should should invalidate testkey2.
1174
        // Make a new cache class.  This should should invalidate testkey2.
1168
        cache_factory::instance()->reset_cache_instances();
1175
        factory::instance()->reset_cache_instances();
1169
        $cache = cache::make('phpunit', 'eventinvalidationtest');
1176
        $cache = cache::make('phpunit', 'eventinvalidationtest');
1170
        // Lastinvalidation timestamp should have updated to cache::now().
1177
        // Lastinvalidation timestamp should have updated to cache::now().
1171
        $this->assertEquals(cache::get_purge_token(), $cache->get('lastinvalidation'));
1178
        $this->assertEquals(cache::get_purge_token(), $cache->get('lastinvalidation'));
1172
 
1179
 
1173
        // Now simulate a purge_by_event 5 seconds ago.
1180
        // Now simulate a purge_by_event 5 seconds ago.
1174
        $invalidationcache = cache::make('core', 'eventinvalidation');
1181
        $invalidationcache = cache::make('core', 'eventinvalidation');
1175
        $invalidationcache->set('crazyevent', array('purged' => cache::now(true) - 5));
1182
        $invalidationcache->set('crazyevent', ['purged' => cache::now(true) - 5]);
1176
        // Set our lastinvalidation timestamp to 15 seconds ago.
-
 
1177
        $cache->set('lastinvalidation', cache::now(true) - 15);
1183
        // Set our lastinvalidation timestamp to 15 seconds ago.
Línea 1178... Línea 1184...
1178
        // Make a new cache class.  This should invalidate the cache.
1184
        $cache->set('lastinvalidation', cache::now(true) - 15);
1179
        cache_factory::instance()->reset_cache_instances();
1185
        // Make a new cache class.  This should invalidate the cache.
1180
        $cache = cache::make('phpunit', 'eventinvalidationtest');
1186
        factory::instance()->reset_cache_instances();
1181
        // Lastinvalidation timestamp should have updated to cache::now().
1187
        $cache = cache::make('phpunit', 'eventinvalidationtest');
1182
        $this->assertEquals(cache::get_purge_token(), $cache->get('lastinvalidation'));
1188
        // Lastinvalidation timestamp should have updated to cache::now().
1183
 
1189
        $this->assertEquals(cache::get_purge_token(), $cache->get('lastinvalidation'));
1184
    }
1190
    }
1185
 
1191
 
1186
    /**
1192
    /**
1187
     * Tests application cache event purge
1193
     * Tests application cache event purge
1188
     */
1194
     */
1189
    public function test_application_event_purge(): void {
1195
    public function test_application_event_purge(): void {
1190
        $instance = cache_config_testing::instance();
1196
        $instance = cache_config_testing::instance();
1191
        $instance->phpunit_add_definition('phpunit/eventpurgetest', array(
1197
        $instance->phpunit_add_definition('phpunit/eventpurgetest', [
1192
            'mode' => cache_store::MODE_APPLICATION,
1198
            'mode' => store::MODE_APPLICATION,
1193
            'component' => 'phpunit',
1199
            'component' => 'phpunit',
1194
            'area' => 'eventpurgetest',
1200
            'area' => 'eventpurgetest',
1195
            'invalidationevents' => array(
1201
            'invalidationevents' => [
1196
                'crazyevent'
1202
                'crazyevent',
1197
            )
1203
            ],
1198
        ));
1204
        ]);
1199
        $instance->phpunit_add_definition('phpunit/eventpurgetestaccelerated', array(
1205
        $instance->phpunit_add_definition('phpunit/eventpurgetestaccelerated', [
1200
            'mode' => cache_store::MODE_APPLICATION,
1206
            'mode' => store::MODE_APPLICATION,
Línea 1201... Línea 1207...
1201
            'component' => 'phpunit',
1207
            'component' => 'phpunit',
1202
            'area' => 'eventpurgetestaccelerated',
1208
            'area' => 'eventpurgetestaccelerated',
1203
            'staticacceleration' => true,
1209
            'staticacceleration' => true,
1204
            'invalidationevents' => array(
1210
            'invalidationevents' => [
Línea 1205... Línea 1211...
1205
                'crazyevent'
1211
                'crazyevent',
1206
            )
1212
            ],
Línea 1207... Línea 1213...
1207
        ));
1213
        ]);
1208
        $cache = cache::make('phpunit', 'eventpurgetest');
1214
        $cache = cache::make('phpunit', 'eventpurgetest');
1209
 
1215
 
Línea 1225... Línea 1231...
1225
        $this->assertEquals('test data 1', $cache->get('testkey1'));
1231
        $this->assertEquals('test data 1', $cache->get('testkey1'));
1226
        $this->assertTrue($cache->set('testkey2', 'test data 2'));
1232
        $this->assertTrue($cache->set('testkey2', 'test data 2'));
1227
        $this->assertEquals('test data 2', $cache->get('testkey2'));
1233
        $this->assertEquals('test data 2', $cache->get('testkey2'));
Línea 1228... Línea 1234...
1228
 
1234
 
1229
        // Purge the event.
1235
        // Purge the event.
Línea 1230... Línea 1236...
1230
        cache_helper::purge_by_event('crazyevent');
1236
        helper::purge_by_event('crazyevent');
1231
 
1237
 
1232
        // Check things have been removed.
1238
        // Check things have been removed.
1233
        $this->assertFalse($cache->get('testkey1'));
1239
        $this->assertFalse($cache->get('testkey1'));
Línea 1237... Línea 1243...
1237
    /**
1243
    /**
1238
     * Tests session cache event purge
1244
     * Tests session cache event purge
1239
     */
1245
     */
1240
    public function test_session_event_purge(): void {
1246
    public function test_session_event_purge(): void {
1241
        $instance = cache_config_testing::instance();
1247
        $instance = cache_config_testing::instance();
1242
        $instance->phpunit_add_definition('phpunit/eventpurgetest', array(
1248
        $instance->phpunit_add_definition('phpunit/eventpurgetest', [
1243
            'mode' => cache_store::MODE_SESSION,
1249
            'mode' => store::MODE_SESSION,
1244
            'component' => 'phpunit',
1250
            'component' => 'phpunit',
1245
            'area' => 'eventpurgetest',
1251
            'area' => 'eventpurgetest',
1246
            'invalidationevents' => array(
1252
            'invalidationevents' => [
1247
                'crazyevent'
1253
                'crazyevent',
1248
            )
1254
            ],
1249
        ));
1255
        ]);
1250
        $instance->phpunit_add_definition('phpunit/eventpurgetestaccelerated', array(
1256
        $instance->phpunit_add_definition('phpunit/eventpurgetestaccelerated', [
1251
            'mode' => cache_store::MODE_SESSION,
1257
            'mode' => store::MODE_SESSION,
1252
            'component' => 'phpunit',
1258
            'component' => 'phpunit',
1253
            'area' => 'eventpurgetestaccelerated',
1259
            'area' => 'eventpurgetestaccelerated',
1254
            'staticacceleration' => true,
1260
            'staticacceleration' => true,
1255
            'invalidationevents' => array(
1261
            'invalidationevents' => [
1256
                'crazyevent'
1262
                'crazyevent',
1257
            )
1263
            ],
1258
        ));
1264
        ]);
1259
        $cache = cache::make('phpunit', 'eventpurgetest');
1265
        $cache = cache::make('phpunit', 'eventpurgetest');
Línea 1260... Línea 1266...
1260
 
1266
 
1261
        $this->assertTrue($cache->set('testkey1', 'test data 1'));
1267
        $this->assertTrue($cache->set('testkey1', 'test data 1'));
1262
        $this->assertEquals('test data 1', $cache->get('testkey1'));
1268
        $this->assertEquals('test data 1', $cache->get('testkey1'));
1263
        $this->assertTrue($cache->set('testkey2', 'test data 2'));
1269
        $this->assertTrue($cache->set('testkey2', 'test data 2'));
Línea 1264... Línea 1270...
1264
        $this->assertEquals('test data 2', $cache->get('testkey2'));
1270
        $this->assertEquals('test data 2', $cache->get('testkey2'));
1265
 
1271
 
Línea 1266... Línea 1272...
1266
        // Purge the event.
1272
        // Purge the event.
1267
        cache_helper::purge_by_event('crazyevent');
1273
        helper::purge_by_event('crazyevent');
1268
 
1274
 
Línea 1276... Línea 1282...
1276
        $this->assertEquals('test data 1', $cache->get('testkey1'));
1282
        $this->assertEquals('test data 1', $cache->get('testkey1'));
1277
        $this->assertTrue($cache->set('testkey2', 'test data 2'));
1283
        $this->assertTrue($cache->set('testkey2', 'test data 2'));
1278
        $this->assertEquals('test data 2', $cache->get('testkey2'));
1284
        $this->assertEquals('test data 2', $cache->get('testkey2'));
Línea 1279... Línea 1285...
1279
 
1285
 
1280
        // Purge the event.
1286
        // Purge the event.
Línea 1281... Línea 1287...
1281
        cache_helper::purge_by_event('crazyevent');
1287
        helper::purge_by_event('crazyevent');
1282
 
1288
 
1283
        // Check things have been removed.
1289
        // Check things have been removed.
1284
        $this->assertFalse($cache->get('testkey1'));
1290
        $this->assertFalse($cache->get('testkey1'));
Línea 1288... Línea 1294...
1288
    /**
1294
    /**
1289
     * Tests application cache definition purge
1295
     * Tests application cache definition purge
1290
     */
1296
     */
1291
    public function test_application_definition_purge(): void {
1297
    public function test_application_definition_purge(): void {
1292
        $instance = cache_config_testing::instance();
1298
        $instance = cache_config_testing::instance();
1293
        $instance->phpunit_add_definition('phpunit/definitionpurgetest', array(
1299
        $instance->phpunit_add_definition('phpunit/definitionpurgetest', [
1294
            'mode' => cache_store::MODE_APPLICATION,
1300
            'mode' => store::MODE_APPLICATION,
1295
            'component' => 'phpunit',
1301
            'component' => 'phpunit',
1296
            'area' => 'definitionpurgetest',
1302
            'area' => 'definitionpurgetest',
1297
            'invalidationevents' => array(
1303
            'invalidationevents' => [
1298
                'crazyevent'
1304
                'crazyevent',
1299
            )
1305
            ],
1300
        ));
1306
        ]);
1301
        $cache = cache::make('phpunit', 'definitionpurgetest');
1307
        $cache = cache::make('phpunit', 'definitionpurgetest');
Línea 1302... Línea 1308...
1302
 
1308
 
1303
        $this->assertTrue($cache->set('testkey1', 'test data 1'));
1309
        $this->assertTrue($cache->set('testkey1', 'test data 1'));
1304
        $this->assertEquals('test data 1', $cache->get('testkey1'));
1310
        $this->assertEquals('test data 1', $cache->get('testkey1'));
1305
        $this->assertTrue($cache->set('testkey2', 'test data 2'));
1311
        $this->assertTrue($cache->set('testkey2', 'test data 2'));
Línea 1306... Línea 1312...
1306
        $this->assertEquals('test data 2', $cache->get('testkey2'));
1312
        $this->assertEquals('test data 2', $cache->get('testkey2'));
1307
 
1313
 
Línea 1308... Línea 1314...
1308
        // Purge the event.
1314
        // Purge the event.
1309
        cache_helper::purge_by_definition('phpunit', 'definitionpurgetest');
1315
        helper::purge_by_definition('phpunit', 'definitionpurgetest');
1310
 
1316
 
1311
        // Check things have been removed.
1317
        // Check things have been removed.
Línea 1317... Línea 1323...
1317
     * Test the use of an alt path.
1323
     * Test the use of an alt path.
1318
     * If we can generate a config instance we are done :)
1324
     * If we can generate a config instance we are done :)
1319
     */
1325
     */
1320
    public function test_alt_cache_path(): void {
1326
    public function test_alt_cache_path(): void {
1321
        global $CFG;
1327
        global $CFG;
1322
        if ((defined('TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH') && TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH) || !empty($CFG->altcacheconfigpath)) {
-
 
-
 
1328
 
1323
            $this->markTestSkipped('Skipped testing alt cache path as it is already being used.');
1329
        if ($this->skip_if_empty_alt_cache_path()) {
-
 
1330
            return;
1324
        }
1331
        }
-
 
1332
 
1325
        $this->resetAfterTest();
1333
        $this->resetAfterTest();
1326
        $CFG->altcacheconfigpath = $CFG->dataroot.'/cache/altcacheconfigpath';
1334
        $CFG->altcacheconfigpath = $CFG->dataroot . '/cache/altcacheconfigpath';
1327
        $instance = cache_config_testing::instance();
1335
        $instance = cache_config_testing::instance();
1328
        $this->assertInstanceOf(cache_config::class, $instance);
1336
        $this->assertInstanceOf(config::class, $instance);
1329
    }
1337
    }
Línea 1330... Línea 1338...
1330
 
1338
 
1331
    /**
1339
    /**
1332
     * Test disabling the cache stores.
1340
     * Test disabling the cache stores.
1333
     */
1341
     */
1334
    public function test_disable_stores(): void {
1342
    public function test_disable_stores(): void {
1335
        $instance = cache_config_testing::instance();
1343
        $instance = cache_config_testing::instance();
1336
        $instance->phpunit_add_definition('phpunit/disabletest1', array(
1344
        $instance->phpunit_add_definition('phpunit/disabletest1', [
1337
            'mode' => cache_store::MODE_APPLICATION,
1345
            'mode' => store::MODE_APPLICATION,
1338
            'component' => 'phpunit',
1346
            'component' => 'phpunit',
1339
            'area' => 'disabletest1'
1347
            'area' => 'disabletest1',
1340
        ));
1348
        ]);
1341
        $instance->phpunit_add_definition('phpunit/disabletest2', array(
1349
        $instance->phpunit_add_definition('phpunit/disabletest2', [
1342
            'mode' => cache_store::MODE_SESSION,
-
 
1343
            'component' => 'phpunit',
-
 
1344
            'area' => 'disabletest2'
-
 
1345
        ));
-
 
1346
        $instance->phpunit_add_definition('phpunit/disabletest3', array(
-
 
1347
            'mode' => cache_store::MODE_REQUEST,
1350
            'mode' => store::MODE_SESSION,
-
 
1351
            'component' => 'phpunit',
-
 
1352
            'area' => 'disabletest2',
-
 
1353
        ]);
-
 
1354
        $instance->phpunit_add_definition('phpunit/disabletest3', [
-
 
1355
            'mode' => store::MODE_REQUEST,
1348
            'component' => 'phpunit',
1356
            'component' => 'phpunit',
1349
            'area' => 'disabletest3'
1357
            'area' => 'disabletest3',
Línea 1350... Línea 1358...
1350
        ));
1358
        ]);
1351
 
1359
 
1352
        $caches = array(
1360
        $caches = [
1353
            'disabletest1' => cache::make('phpunit', 'disabletest1'),
1361
            'disabletest1' => cache::make('phpunit', 'disabletest1'),
1354
            'disabletest2' => cache::make('phpunit', 'disabletest2'),
1362
            'disabletest2' => cache::make('phpunit', 'disabletest2'),
Línea 1355... Línea 1363...
1355
            'disabletest3' => cache::make('phpunit', 'disabletest3')
1363
            'disabletest3' => cache::make('phpunit', 'disabletest3'),
1356
        );
1364
        ];
1357
 
1365
 
Línea 1358... Línea 1366...
1358
        $this->assertInstanceOf(cache_phpunit_application::class, $caches['disabletest1']);
1366
        $this->assertInstanceOf(cache_phpunit_application::class, $caches['disabletest1']);
1359
        $this->assertInstanceOf(cache_phpunit_session::class, $caches['disabletest2']);
1367
        $this->assertInstanceOf(cache_phpunit_session::class, $caches['disabletest2']);
1360
        $this->assertInstanceOf(cache_phpunit_request::class, $caches['disabletest3']);
1368
        $this->assertInstanceOf(cache_phpunit_request::class, $caches['disabletest3']);
Línea 1361... Línea 1369...
1361
 
1369
 
1362
        $this->assertEquals('cachestore_file', $caches['disabletest1']->phpunit_get_store_class());
1370
        $this->assertInstanceOf(cachestore_file::class, $caches['disabletest1']->get_store());
1363
        $this->assertEquals('cachestore_session', $caches['disabletest2']->phpunit_get_store_class());
1371
        $this->assertInstanceOf(cachestore_session::class, $caches['disabletest2']->get_store());
1364
        $this->assertEquals('cachestore_static', $caches['disabletest3']->phpunit_get_store_class());
1372
        $this->assertInstanceOf(cachestore_static::class, $caches['disabletest3']->get_store());
1365
 
1373
 
Línea 1366... Línea 1374...
1366
        foreach ($caches as $cache) {
1374
        foreach ($caches as $cache) {
Línea 1367... Línea 1375...
1367
            $this->assertFalse($cache->get('test'));
1375
            $this->assertFalse($cache->get('test'));
1368
            $this->assertTrue($cache->set('test', 'test'));
1376
            $this->assertTrue($cache->set('test', 'test'));
1369
            $this->assertEquals('test', $cache->get('test'));
1377
            $this->assertEquals('test', $cache->get('test'));
1370
        }
1378
        }
1371
 
1379
 
Línea 1372... Línea 1380...
1372
        cache_factory::disable_stores();
1380
        factory::disable_stores();
1373
 
1381
 
1374
        $caches = array(
1382
        $caches = [
Línea 1375... Línea 1383...
1375
            'disabletest1' => cache::make('phpunit', 'disabletest1'),
1383
            'disabletest1' => cache::make('phpunit', 'disabletest1'),
1376
            'disabletest2' => cache::make('phpunit', 'disabletest2'),
1384
            'disabletest2' => cache::make('phpunit', 'disabletest2'),
1377
            'disabletest3' => cache::make('phpunit', 'disabletest3')
1385
            'disabletest3' => cache::make('phpunit', 'disabletest3'),
Línea 1378... Línea 1386...
1378
        );
1386
        ];
1379
 
1387
 
1380
        $this->assertInstanceOf(cache_phpunit_application::class, $caches['disabletest1']);
1388
        $this->assertInstanceOf(cache_phpunit_application::class, $caches['disabletest1']);
1381
        $this->assertInstanceOf(cache_phpunit_session::class, $caches['disabletest2']);
1389
        $this->assertInstanceOf(cache_phpunit_session::class, $caches['disabletest2']);
Línea 1396... Línea 1404...
1396
     * Test disabling the cache.
1404
     * Test disabling the cache.
1397
     */
1405
     */
1398
    public function test_disable(): void {
1406
    public function test_disable(): void {
1399
        global $CFG;
1407
        global $CFG;
Línea 1400... Línea -...
1400
 
-
 
1401
        if ((defined('TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH') && TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH) || !empty($CFG->altcacheconfigpath)) {
-
 
1402
            // We can't run this test as it requires us to delete the cache configuration script which we just
1408
 
1403
            // cant do with a custom path in play.
-
 
1404
            $this->markTestSkipped('Skipped testing cache disable functionality as alt cache path is being used.');
-
 
Línea 1405... Línea 1409...
1405
        }
1409
        $this->skip_if_empty_alt_cache_path();
Línea 1406... Línea 1410...
1406
 
1410
 
1407
        $configfile = $CFG->dataroot.'/muc/config.php';
1411
        $configfile = $CFG->dataroot . '/muc/config.php';
1408
 
1412
 
1409
        // The config file will not exist yet as we've not done anything with the cache.
1413
        // The config file will not exist yet as we've not done anything with the cache.
Línea 1410... Línea 1414...
1410
        // reset_all_data removes the file and without a call to create a configuration it doesn't exist
1414
        // reset_all_data removes the file and without a call to create a configuration it doesn't exist
1411
        // as yet.
1415
        // as yet.
Línea 1412... Línea 1416...
1412
        $this->assertFileDoesNotExist($configfile);
1416
        $this->assertFileDoesNotExist($configfile);
1413
 
1417
 
1414
        // Disable the cache
1418
        // Disable the cache.
Línea 1415... Línea 1419...
1415
        cache_phpunit_factory::phpunit_disable();
1419
        cache_phpunit_factory::phpunit_disable();
1416
 
1420
 
1417
        // Check we get the expected disabled factory.
1421
        // Check we get the expected disabled factory.
Línea 1418... Línea 1422...
1418
        $factory = cache_factory::instance();
1422
        $factory = factory::instance();
1419
        $this->assertInstanceOf(cache_factory_disabled::class, $factory);
1423
        $this->assertInstanceOf(disabled_factory::class, $factory);
1420
 
1424
 
Línea 1421... Línea 1425...
1421
        // Check we get the expected disabled config.
1425
        // Check we get the expected disabled config.
1422
        $config = $factory->create_config_instance();
1426
        $config = $factory->create_config_instance();
1423
        $this->assertInstanceOf(cache_config_disabled::class, $config);
1427
        $this->assertInstanceOf(disabled_config::class, $config);
Línea 1424... Línea 1428...
1424
 
1428
 
1425
        // Check we get the expected disabled caches.
1429
        // Check we get the expected disabled caches.
1426
        $cache = cache::make('core', 'string');
1430
        $cache = cache::make('core', 'string');
1427
        $this->assertInstanceOf(cache_disabled::class, $cache);
1431
        $this->assertInstanceOf(disabled_cache::class, $cache);
Línea 1445... Línea 1449...
1445
        $this->assertTrue($cache->release_lock('test'));
1449
        $this->assertTrue($cache->release_lock('test'));
1446
        $this->assertTrue($cache->release_lock('test'));
1450
        $this->assertTrue($cache->release_lock('test'));
1447
        $this->assertTrue($cache->check_lock_state('test'));
1451
        $this->assertTrue($cache->check_lock_state('test'));
Línea 1448... Línea 1452...
1448
 
1452
 
1449
        // Test a session cache.
1453
        // Test a session cache.
1450
        $cache = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'disable');
1454
        $cache = cache::make_from_params(store::MODE_SESSION, 'phpunit', 'disable');
Línea 1451... Línea 1455...
1451
        $this->assertInstanceOf(cache_disabled::class, $cache);
1455
        $this->assertInstanceOf(disabled_cache::class, $cache);
1452
 
1456
 
1453
        $this->assertFalse($cache->get('test'));
1457
        $this->assertFalse($cache->get('test'));
1454
        $this->assertFalse($cache->get_versioned('v', 1));
1458
        $this->assertFalse($cache->get_versioned('v', 1));
1455
        $this->assertFalse($cache->set('test', 'test'));
1459
        $this->assertFalse($cache->set('test', 'test'));
1456
        $this->assertFalse($cache->set_versioned('v', 1, 'data'));
1460
        $this->assertFalse($cache->set_versioned('v', 1, 'data'));
Línea 1457... Línea 1461...
1457
        $this->assertFalse($cache->delete('test'));
1461
        $this->assertFalse($cache->delete('test'));
1458
        $this->assertTrue($cache->purge());
1462
        $this->assertTrue($cache->purge());
1459
 
1463
 
Línea 1460... Línea 1464...
1460
        // Finally test a request cache.
1464
        // Finally test a request cache.
1461
        $cache = cache::make_from_params(cache_store::MODE_REQUEST, 'phpunit', 'disable');
1465
        $cache = cache::make_from_params(store::MODE_REQUEST, 'phpunit', 'disable');
1462
        $this->assertInstanceOf(cache_disabled::class, $cache);
1466
        $this->assertInstanceOf(disabled_cache::class, $cache);
1463
 
1467
 
1464
        $this->assertFalse($cache->get('test'));
1468
        $this->assertFalse($cache->get('test'));
1465
        $this->assertFalse($cache->get_versioned('v', 1));
1469
        $this->assertFalse($cache->get_versioned('v', 1));
Línea 1466... Línea 1470...
1466
        $this->assertFalse($cache->set('test', 'test'));
1470
        $this->assertFalse($cache->set('test', 'test'));
Línea 1467... Línea 1471...
1467
        $this->assertFalse($cache->set_versioned('v', 1, 'data'));
1471
        $this->assertFalse($cache->set_versioned('v', 1, 'data'));
1468
        $this->assertFalse($cache->delete('test'));
1472
        $this->assertFalse($cache->delete('test'));
1469
        $this->assertTrue($cache->purge());
1473
        $this->assertTrue($cache->purge());
1470
 
1474
 
Línea 1471... Línea 1475...
1471
        cache_factory::reset();
1475
        factory::reset();
1472
 
1476
 
1473
        $factory = cache_factory::instance(true);
1477
        $factory = factory::instance(true);
1474
        $config = $factory->create_config_instance();
1478
        $config = $factory->create_config_instance();
1475
        $this->assertEquals('cache_config_testing', get_class($config));
1479
        $this->assertInstanceOf(cache_config_testing::class, $config);
1476
    }
1480
    }
1477
 
1481
 
1478
    /**
1482
    /**
1479
     * Test that multiple application loaders work ok.
1483
     * Test that multiple application loaders work ok.
1480
     */
1484
     */
1481
    public function test_multiple_application_loaders(): void {
1485
    public function test_multiple_application_loaders(): void {
1482
        $instance = cache_config_testing::instance(true);
1486
        $instance = cache_config_testing::instance();
1483
        $instance->phpunit_add_file_store('phpunittest1');
1487
        $instance->phpunit_add_file_store('phpunittest1');
1484
        $instance->phpunit_add_file_store('phpunittest2');
1488
        $instance->phpunit_add_file_store('phpunittest2');
Línea 1485... Línea 1489...
1485
        $instance->phpunit_add_definition('phpunit/multi_loader', array(
1489
        $instance->phpunit_add_definition('phpunit/multi_loader', [
1486
            'mode' => cache_store::MODE_APPLICATION,
1490
            'mode' => store::MODE_APPLICATION,
1487
            'component' => 'phpunit',
1491
            'component' => 'phpunit',
1488
            'area' => 'multi_loader'
1492
            'area' => 'multi_loader',
1489
        ));
1493
        ]);
1490
        $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest1', 3);
1494
        $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest1', 3);
1491
        $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest2', 2);
1495
        $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest2', 2);
1492
 
1496
 
1493
        $cache = cache::make('phpunit', 'multi_loader');
1497
        $cache = cache::make('phpunit', 'multi_loader');
1494
        $this->assertInstanceOf(cache_application::class, $cache);
1498
        $this->assertInstanceOf(application_cache::class, $cache);
Línea 1495... Línea 1499...
1495
        $this->assertFalse($cache->get('test'));
1499
        $this->assertFalse($cache->get('test'));
1496
        $this->assertTrue($cache->set('test', 'test'));
1500
        $this->assertTrue($cache->set('test', 'test'));
1497
        $this->assertEquals('test', $cache->get('test'));
1501
        $this->assertEquals('test', $cache->get('test'));
1498
        $this->assertTrue($cache->delete('test'));
1502
        $this->assertTrue($cache->delete('test'));
1499
        $this->assertFalse($cache->get('test'));
1503
        $this->assertFalse($cache->get('test'));
1500
        $this->assertTrue($cache->set('test', 'test'));
1504
        $this->assertTrue($cache->set('test', 'test'));
1501
        $this->assertTrue($cache->purge());
1505
        $this->assertTrue($cache->purge());
1502
        $this->assertFalse($cache->get('test'));
1506
        $this->assertFalse($cache->get('test'));
1503
 
1507
 
1504
        // Test the many commands.
1508
        // Test the many commands.
1505
        $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
1509
        $this->assertEquals(3, $cache->set_many(['a' => 'A', 'b' => 'B', 'c' => 'C']));
1506
        $result = $cache->get_many(array('a', 'b', 'c'));
1510
        $result = $cache->get_many(['a', 'b', 'c']);
1507
        $this->assertIsArray($result);
1511
        $this->assertIsArray($result);
1508
        $this->assertCount(3, $result);
1512
        $this->assertCount(3, $result);
1509
        $this->assertArrayHasKey('a', $result);
1513
        $this->assertArrayHasKey('a', $result);
1510
        $this->assertArrayHasKey('b', $result);
1514
        $this->assertArrayHasKey('b', $result);
1511
        $this->assertArrayHasKey('c', $result);
1515
        $this->assertArrayHasKey('c', $result);
1512
        $this->assertEquals('A', $result['a']);
1516
        $this->assertEquals('A', $result['a']);
1513
        $this->assertEquals('B', $result['b']);
1517
        $this->assertEquals('B', $result['b']);
Línea 1529... Línea 1533...
1529
        $this->assertSame('test', $cache->get('test'));
1533
        $this->assertSame('test', $cache->get('test'));
1530
        $this->assertTrue($cache->delete('test', false));
1534
        $this->assertTrue($cache->delete('test', false));
1531
        // We should still have it on a deeper loader.
1535
        // We should still have it on a deeper loader.
1532
        $this->assertSame('test', $cache->get('test'));
1536
        $this->assertSame('test', $cache->get('test'));
1533
        // Test non-recusive with many functions.
1537
        // Test non-recusive with many functions.
1534
        $this->assertSame(3, $cache->set_many(array(
1538
        $this->assertSame(3, $cache->set_many([
1535
            'one' => 'one',
1539
            'one' => 'one',
1536
            'two' => 'two',
1540
            'two' => 'two',
1537
            'three' => 'three'
1541
            'three' => 'three',
1538
        )));
1542
        ]));
1539
        $this->assertSame('one', $cache->get('one'));
1543
        $this->assertSame('one', $cache->get('one'));
1540
        $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
1544
        $this->assertSame(['two' => 'two', 'three' => 'three'], $cache->get_many(['two', 'three']));
1541
        $this->assertSame(3, $cache->delete_many(array('one', 'two', 'three'), false));
1545
        $this->assertSame(3, $cache->delete_many(['one', 'two', 'three'], false));
1542
        $this->assertSame('one', $cache->get('one'));
1546
        $this->assertSame('one', $cache->get('one'));
1543
        $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
1547
        $this->assertSame(['two' => 'two', 'three' => 'three'], $cache->get_many(['two', 'three']));
1544
    }
1548
    }
Línea 1545... Línea 1549...
1545
 
1549
 
1546
    /**
1550
    /**
1547
     * Data provider to try using a TTL or non-TTL cache.
1551
     * Data provider to try using a TTL or non-TTL cache.
1548
     *
1552
     *
1549
     * @return array
1553
     * @return array
1550
     */
1554
     */
1551
    public function ttl_or_not(): array {
1555
    public static function ttl_or_not(): array {
1552
        return [[false], [true]];
1556
        return [[false], [true]];
Línea 1553... Línea 1557...
1553
    }
1557
    }
1554
 
1558
 
1555
    /**
1559
    /**
1556
     * Data provider to try using a TTL or non-TTL cache, and static acceleration or not.
1560
     * Data provider to try using a TTL or non-TTL cache, and static acceleration or not.
1557
     *
1561
     *
1558
     * @return array
1562
     * @return array
1559
     */
1563
     */
1560
    public function ttl_and_static_acceleration_or_not(): array {
1564
    public static function ttl_and_static_acceleration_or_not(): array {
Línea 1561... Línea 1565...
1561
        return [[false, false], [false, true], [true, false], [true, true]];
1565
        return [[false, false], [false, true], [true, false], [true, true]];
1562
    }
1566
    }
1563
 
1567
 
1564
    /**
1568
    /**
1565
     * Data provider to try using a TTL or non-TTL cache, and simple data on or off.
1569
     * Data provider to try using a TTL or non-TTL cache, and simple data on or off.
1566
     *
1570
     *
1567
     * @return array
1571
     * @return array
1568
     */
1572
     */
1569
    public function ttl_and_simple_data_or_not(): array {
1573
    public static function ttl_and_simple_data_or_not(): array {
Línea 1570... Línea 1574...
1570
        // Same values as for ttl and static acceleration (two booleans).
1574
        // Same values as for ttl and static acceleration (two booleans).
1571
        return $this->ttl_and_static_acceleration_or_not();
1575
        return self::ttl_and_static_acceleration_or_not();
1572
    }
1576
    }
1573
 
1577
 
1574
    /**
1578
    /**
1575
     * Shared code to set up a two or three-layer versioned cache for testing.
1579
     * Shared code to set up a two or three-layer versioned cache for testing.
1576
     *
1580
     *
1577
     * @param bool $ttl If true, sets TTL in the definition
1581
     * @param bool $ttl If true, sets TTL in the definition
1578
     * @param bool $threelayer If true, uses a 3-layer instead of 2-layer cache
1582
     * @param bool $threelayer If true, uses a 3-layer instead of 2-layer cache
1579
     * @param bool $staticacceleration If true, enables static acceleration
1583
     * @param bool $staticacceleration If true, enables static acceleration
-
 
1584
     * @param bool $simpledata If true, enables simple data
-
 
1585
     * @return application_cache Cache
1580
     * @param bool $simpledata If true, enables simple data
1586
     */
-
 
1587
    protected function create_versioned_cache(
-
 
1588
        bool $ttl,
1581
     * @return \cache_application Cache
1589
        bool $threelayer = false,
1582
     */
1590
        bool $staticacceleration = false,
1583
    protected function create_versioned_cache(bool $ttl, bool $threelayer = false,
1591
        bool $simpledata = false
1584
            bool $staticacceleration = false, bool $simpledata = false): \cache_application {
1592
    ): application_cache {
1585
        $instance = cache_config_testing::instance(true);
1593
        $instance = cache_config_testing::instance();
1586
        $instance->phpunit_add_file_store('a', false);
1594
        $instance->phpunit_add_file_store('a', false);
1587
        $instance->phpunit_add_file_store('b', false);
1595
        $instance->phpunit_add_file_store('b', false);
1588
        if ($threelayer) {
1596
        if ($threelayer) {
1589
            $instance->phpunit_add_file_store('c', false);
1597
            $instance->phpunit_add_file_store('c', false);
1590
        }
1598
        }
1591
        $defarray = [
1599
        $defarray = [
1592
            'mode' => cache_store::MODE_APPLICATION,
1600
            'mode' => store::MODE_APPLICATION,
1593
            'component' => 'phpunit',
1601
            'component' => 'phpunit',
1594
            'area' => 'multi_loader'
1602
            'area' => 'multi_loader',
1595
        ];
1603
        ];
Línea 1618... Línea 1626...
1618
     * Tests basic use of versioned cache.
1626
     * Tests basic use of versioned cache.
1619
     *
1627
     *
1620
     * @dataProvider ttl_and_simple_data_or_not
1628
     * @dataProvider ttl_and_simple_data_or_not
1621
     * @param bool $ttl If true, uses a TTL cache.
1629
     * @param bool $ttl If true, uses a TTL cache.
1622
     * @param bool $simpledata If true, turns on simple data flag
1630
     * @param bool $simpledata If true, turns on simple data flag
1623
     * @covers ::set_versioned
-
 
1624
     * @covers ::get_versioned
-
 
1625
     */
1631
     */
1626
    public function test_versioned_cache_basic(bool $ttl, bool $simpledata): void {
1632
    public function test_versioned_cache_basic(bool $ttl, bool $simpledata): void {
1627
        $multicache = $this->create_versioned_cache($ttl, false, false, $simpledata);
1633
        $multicache = $this->create_versioned_cache($ttl, false, false, $simpledata);
Línea 1628... Línea 1634...
1628
 
1634
 
Línea 1637... Línea 1643...
1637
     * Tests versioned cache with objects.
1643
     * Tests versioned cache with objects.
1638
     *
1644
     *
1639
     * @dataProvider ttl_and_static_acceleration_or_not
1645
     * @dataProvider ttl_and_static_acceleration_or_not
1640
     * @param bool $ttl If true, uses a TTL cache.
1646
     * @param bool $ttl If true, uses a TTL cache.
1641
     * @param bool $staticacceleration If true, enables static acceleration
1647
     * @param bool $staticacceleration If true, enables static acceleration
1642
     * @covers ::set_versioned
-
 
1643
     * @covers ::get_versioned
-
 
1644
     */
1648
     */
1645
    public function test_versioned_cache_objects(bool $ttl, bool $staticacceleration): void {
1649
    public function test_versioned_cache_objects(bool $ttl, bool $staticacceleration): void {
1646
        $multicache = $this->create_versioned_cache($ttl, false, $staticacceleration);
1650
        $multicache = $this->create_versioned_cache($ttl, false, $staticacceleration);
Línea 1647... Línea 1651...
1647
 
1651
 
Línea 1664... Línea 1668...
1664
    /**
1668
    /**
1665
     * Tests requesting a version that doesn't exist.
1669
     * Tests requesting a version that doesn't exist.
1666
     *
1670
     *
1667
     * @dataProvider ttl_or_not
1671
     * @dataProvider ttl_or_not
1668
     * @param bool $ttl If true, uses a TTL cache.
1672
     * @param bool $ttl If true, uses a TTL cache.
1669
     * @covers ::set_versioned
-
 
1670
     * @covers ::get_versioned
-
 
1671
     */
1673
     */
1672
    public function test_versioned_cache_not_exist(bool $ttl): void {
1674
    public function test_versioned_cache_not_exist(bool $ttl): void {
1673
        $multicache = $this->create_versioned_cache($ttl);
1675
        $multicache = $this->create_versioned_cache($ttl);
Línea 1674... Línea 1676...
1674
 
1676
 
Línea 1684... Línea 1686...
1684
    /**
1686
    /**
1685
     * Tests attempts to use get after set_version or get_version after set.
1687
     * Tests attempts to use get after set_version or get_version after set.
1686
     *
1688
     *
1687
     * @dataProvider ttl_or_not
1689
     * @dataProvider ttl_or_not
1688
     * @param bool $ttl If true, uses a TTL cache.
1690
     * @param bool $ttl If true, uses a TTL cache.
1689
     * @covers ::set_versioned
-
 
1690
     * @covers ::get_versioned
-
 
1691
     */
1691
     */
1692
    public function test_versioned_cache_incompatible_versioning(bool $ttl): void {
1692
    public function test_versioned_cache_incompatible_versioning(bool $ttl): void {
1693
        $multicache = $this->create_versioned_cache($ttl);
1693
        $multicache = $this->create_versioned_cache($ttl);
Línea 1694... Línea 1694...
1694
 
1694
 
1695
        // What if you use get on a get_version cache?
1695
        // What if you use get on a get_version cache?
1696
        $multicache->set_versioned('game', 1, 'Pooh-sticks');
1696
        $multicache->set_versioned('game', 1, 'Pooh-sticks');
1697
        try {
1697
        try {
1698
            $multicache->get('game');
1698
            $multicache->get('game');
1699
            $this->fail();
1699
            $this->fail();
1700
        } catch (\coding_exception $e) {
1700
        } catch (coding_exception $e) {
1701
            $this->assertStringContainsString('Unexpectedly found versioned cache entry', $e->getMessage());
1701
            $this->assertStringContainsString('Unexpectedly found versioned cache entry', $e->getMessage());
Línea 1702... Línea 1702...
1702
        }
1702
        }
1703
 
1703
 
1704
        // Or get_version on a get cache?
1704
        // Or get_version on a get cache?
1705
        $multicache->set('toy', 'Train set');
1705
        $multicache->set('toy', 'Train set');
1706
        try {
1706
        try {
1707
            $multicache->get_versioned('toy', 1);
1707
            $multicache->get_versioned('toy', 1);
1708
            $this->fail();
1708
            $this->fail();
1709
        } catch (\coding_exception $e) {
1709
        } catch (coding_exception $e) {
1710
            $this->assertStringContainsString('Unexpectedly found non-versioned cache entry', $e->getMessage());
1710
            $this->assertStringContainsString('Unexpectedly found non-versioned cache entry', $e->getMessage());
Línea 1711... Línea 1711...
1711
        }
1711
        }
1712
    }
1712
    }
1713
 
1713
 
1714
    /**
1714
    /**
1715
     * Versions are only stored once, so if you set a newer version you will always get it even
1715
     * Versions are only stored once, so if you set a newer version you will always get it even
1716
     * if you ask for the lower version number.
1716
     * if you ask for the lower version number.
1717
     *
-
 
1718
     * @dataProvider ttl_or_not
-
 
1719
     * @param bool $ttl If true, uses a TTL cache.
1717
     *
1720
     * @covers ::set_versioned
1718
     * @dataProvider ttl_or_not
1721
     * @covers ::get_versioned
1719
     * @param bool $ttl If true, uses a TTL cache.
Línea 1722... Línea 1720...
1722
     */
1720
     */
Línea 1735... Línea 1733...
1735
     * If the first (local) store has an outdated copy but the second (shared) store has a newer
1733
     * If the first (local) store has an outdated copy but the second (shared) store has a newer
1736
     * one, then it should automatically be retrieved.
1734
     * one, then it should automatically be retrieved.
1737
     *
1735
     *
1738
     * @dataProvider ttl_or_not
1736
     * @dataProvider ttl_or_not
1739
     * @param bool $ttl If true, uses a TTL cache.
1737
     * @param bool $ttl If true, uses a TTL cache.
1740
     * @covers ::set_versioned
-
 
1741
     * @covers ::get_versioned
-
 
1742
     */
1738
     */
1743
    public function test_versioned_cache_outdated_local(bool $ttl): void {
1739
    public function test_versioned_cache_outdated_local(bool $ttl): void {
1744
        $multicache = $this->create_versioned_cache($ttl);
1740
        $multicache = $this->create_versioned_cache($ttl);
Línea 1745... Línea 1741...
1745
 
1741
 
1746
        // Set initial value to version 2, 'Tag', in both stores.
1742
        // Set initial value to version 2, 'Tag', in both stores.
Línea 1747... Línea 1743...
1747
        $multicache->set_versioned('game', 2, 'Tag');
1743
        $multicache->set_versioned('game', 2, 'Tag');
1748
 
1744
 
1749
        // Get the two separate cache stores for the multi-level cache.
1745
        // Get the two separate cache stores for the multi-level cache.
1750
        $factory = cache_factory::instance();
1746
        $factory = factory::instance();
Línea 1751... Línea 1747...
1751
        $definition = $factory->create_definition('phpunit', 'multi_loader');
1747
        $definition = $factory->create_definition('phpunit', 'multi_loader');
1752
        [0 => $storea, 1 => $storeb] = $factory->get_store_instances_in_use($definition);
1748
        [0 => $storea, 1 => $storeb] = $factory->get_store_instances_in_use($definition);
1753
 
1749
 
1754
        // Simulate what happens if the shared cache is updated with a new version but the
1750
        // Simulate what happens if the shared cache is updated with a new version but the
1755
        // local one still has an old version.
1751
        // local one still has an old version.
1756
        $hashgame = cache_helper::hash_key('game', $definition);
1752
        $hashgame = helper::hash_key('game', $definition);
1757
        $data = 'British Bulldog';
1753
        $data = 'British Bulldog';
1758
        if ($ttl) {
1754
        if ($ttl) {
Línea 1771... Línea 1767...
1771
        if ($ttl) {
1767
        if ($ttl) {
1772
            // In case the time has changed slightly since the first set, we can't do an exact
1768
            // In case the time has changed slightly since the first set, we can't do an exact
1773
            // compare, so check it ignoring the time field.
1769
            // compare, so check it ignoring the time field.
1774
            $this->assertEquals(3, $localvalue->version);
1770
            $this->assertEquals(3, $localvalue->version);
1775
            $ttldata = $localvalue->data;
1771
            $ttldata = $localvalue->data;
1776
            $this->assertInstanceOf('cache_ttl_wrapper', $ttldata);
1772
            $this->assertInstanceOf(ttl_wrapper::class, $ttldata);
1777
            $this->assertEquals('British Bulldog', $ttldata->data);
1773
            $this->assertEquals('British Bulldog', $ttldata->data);
1778
        } else {
1774
        } else {
1779
            $this->assertEquals(new \core_cache\version_wrapper('British Bulldog', 3), $localvalue);
1775
            $this->assertEquals(new \core_cache\version_wrapper('British Bulldog', 3), $localvalue);
1780
        }
1776
        }
1781
    }
1777
    }
Línea 1785... Línea 1781...
1785
     * cache (to save I/O if there are multiple requests, as if there is another request it will
1781
     * cache (to save I/O if there are multiple requests, as if there is another request it will
1786
     * not have to retrieve the values to find out that they're old).
1782
     * not have to retrieve the values to find out that they're old).
1787
     *
1783
     *
1788
     * @dataProvider ttl_or_not
1784
     * @dataProvider ttl_or_not
1789
     * @param bool $ttl If true, uses a TTL cache.
1785
     * @param bool $ttl If true, uses a TTL cache.
1790
     * @covers ::set_versioned
-
 
1791
     * @covers ::get_versioned
-
 
1792
     */
1786
     */
1793
    public function test_versioned_cache_deleting_outdated(bool $ttl): void {
1787
    public function test_versioned_cache_deleting_outdated(bool $ttl): void {
1794
        $multicache = $this->create_versioned_cache($ttl);
1788
        $multicache = $this->create_versioned_cache($ttl);
Línea 1795... Línea 1789...
1795
 
1789
 
1796
        // Set initial value to version 2, 'Tag', in both stores.
1790
        // Set initial value to version 2, 'Tag', in both stores.
Línea 1797... Línea 1791...
1797
        $multicache->set_versioned('game', 2, 'Tag');
1791
        $multicache->set_versioned('game', 2, 'Tag');
1798
 
1792
 
1799
        // Get the two separate cache stores for the multi-level cache.
1793
        // Get the two separate cache stores for the multi-level cache.
1800
        $factory = cache_factory::instance();
1794
        $factory = factory::instance();
Línea 1801... Línea 1795...
1801
        $definition = $factory->create_definition('phpunit', 'multi_loader');
1795
        $definition = $factory->create_definition('phpunit', 'multi_loader');
1802
        [0 => $storea, 1 => $storeb] = $factory->get_store_instances_in_use($definition);
1796
        [0 => $storea, 1 => $storeb] = $factory->get_store_instances_in_use($definition);
1803
 
1797
 
1804
        // If we request a newer version, then any older version should be deleted in each
1798
        // If we request a newer version, then any older version should be deleted in each
1805
        // cache level.
1799
        // cache level.
1806
        $this->assertFalse($multicache->get_versioned('game', 4));
1800
        $this->assertFalse($multicache->get_versioned('game', 4));
1807
        $hashgame = cache_helper::hash_key('game', $definition);
1801
        $hashgame = helper::hash_key('game', $definition);
Línea 1808... Línea 1802...
1808
        $this->assertFalse($storea->get($hashgame));
1802
        $this->assertFalse($storea->get($hashgame));
1809
        $this->assertFalse($storeb->get($hashgame));
1803
        $this->assertFalse($storeb->get($hashgame));
1810
    }
-
 
1811
 
-
 
1812
    /**
-
 
1813
     * Tests a versioned cache when using static cache.
1804
    }
1814
     *
1805
 
1815
     * @covers ::set_versioned
1806
    /**
Línea 1816... Línea 1807...
1816
     * @covers ::get_versioned
1807
     * Tests a versioned cache when using static cache.
1817
     */
1808
     */
Línea 1818... Línea 1809...
1818
    public function test_versioned_cache_static(): void {
1809
    public function test_versioned_cache_static(): void {
1819
        $staticcache = $this->create_versioned_cache(false, false, true);
1810
        $staticcache = $this->create_versioned_cache(false, false, true);
1820
 
1811
 
1821
        // Set a value in the cache, version 1. This will store it in static acceleration.
1812
        // Set a value in the cache, version 1. This will store it in static acceleration.
Línea 1822... Línea 1813...
1822
        $staticcache->set_versioned('game', 1, 'Pooh-sticks');
1813
        $staticcache->set_versioned('game', 1, 'Pooh-sticks');
1823
 
1814
 
1824
        // Get the first cache store (we don't need the second one for this test).
1815
        // Get the first cache store (we don't need the second one for this test).
1825
        $factory = cache_factory::instance();
1816
        $factory = factory::instance();
Línea 1826... Línea 1817...
1826
        $definition = $factory->create_definition('phpunit', 'multi_loader');
1817
        $definition = $factory->create_definition('phpunit', 'multi_loader');
1827
        [0 => $storea] = $factory->get_store_instances_in_use($definition);
1818
        [0 => $storea] = $factory->get_store_instances_in_use($definition);
Línea 1849... Línea 1840...
1849
        $this->assertFalse($staticcache->get_versioned('game', 4));
1840
        $this->assertFalse($staticcache->get_versioned('game', 4));
1850
    }
1841
    }
Línea 1851... Línea 1842...
1851
 
1842
 
1852
    /**
1843
    /**
1853
     * Tests basic use of 3-layer versioned caches.
-
 
1854
     *
-
 
1855
     * @covers ::set_versioned
-
 
1856
     * @covers ::get_versioned
1844
     * Tests basic use of 3-layer versioned caches.
1857
     */
1845
     */
1858
    public function test_versioned_cache_3_layers_basic(): void {
1846
    public function test_versioned_cache_3_layers_basic(): void {
Línea 1859... Línea 1847...
1859
        $multicache = $this->create_versioned_cache(false, true);
1847
        $multicache = $this->create_versioned_cache(false, true);
Línea 1870... Línea 1858...
1870
        $this->assertEquals('Tag', $multicache->get_versioned('game', 1));
1858
        $this->assertEquals('Tag', $multicache->get_versioned('game', 1));
1871
    }
1859
    }
Línea 1872... Línea 1860...
1872
 
1860
 
1873
    /**
1861
    /**
1874
     * Tests use of 3-layer versioned caches where the 3 layers currently have different versions.
-
 
1875
     *
-
 
1876
     * @covers ::set_versioned
-
 
1877
     * @covers ::get_versioned
1862
     * Tests use of 3-layer versioned caches where the 3 layers currently have different versions.
1878
     */
1863
     */
1879
    public function test_versioned_cache_3_layers_different_data(): void {
1864
    public function test_versioned_cache_3_layers_different_data(): void {
1880
        // Set version 2 using normal method.
1865
        // Set version 2 using normal method.
1881
        $multicache = $this->create_versioned_cache(false, true);
1866
        $multicache = $this->create_versioned_cache(false, true);
Línea 1882... Línea 1867...
1882
        $multicache->set_versioned('game', 2, 'Tag');
1867
        $multicache->set_versioned('game', 2, 'Tag');
1883
 
1868
 
1884
        // Get the three separate cache stores for the multi-level cache.
1869
        // Get the three separate cache stores for the multi-level cache.
1885
        $factory = cache_factory::instance();
1870
        $factory = factory::instance();
Línea 1886... Línea 1871...
1886
        $definition = $factory->create_definition('phpunit', 'multi_loader');
1871
        $definition = $factory->create_definition('phpunit', 'multi_loader');
1887
        [0 => $storea, 1 => $storeb, 2 => $storec] = $factory->get_store_instances_in_use($definition);
1872
        [0 => $storea, 1 => $storeb, 2 => $storec] = $factory->get_store_instances_in_use($definition);
1888
 
1873
 
1889
        // Set up two other versions so every level has a different version.
1874
        // Set up two other versions so every level has a different version.
Línea 1890... Línea 1875...
1890
        $hashgame = cache_helper::hash_key('game', $definition);
1875
        $hashgame = helper::hash_key('game', $definition);
1891
        $storeb->set($hashgame, new \core_cache\version_wrapper('British Bulldog', 3));
1876
        $storeb->set($hashgame, new \core_cache\version_wrapper('British Bulldog', 3));
Línea 1908... Línea 1893...
1908
 
1893
 
1909
    /**
1894
    /**
1910
     * Test that multiple application loaders work ok.
1895
     * Test that multiple application loaders work ok.
1911
     */
1896
     */
1912
    public function test_multiple_session_loaders(): void {
1897
    public function test_multiple_session_loaders(): void {
1913
        /* @var cache_config_testing $instance */
1898
        /* @var \cache_config_testing $instance */
1914
        $instance = cache_config_testing::instance(true);
1899
        $instance = cache_config_testing::instance();
1915
        $instance->phpunit_add_session_store('phpunittest1');
1900
        $instance->phpunit_add_session_store('phpunittest1');
1916
        $instance->phpunit_add_session_store('phpunittest2');
1901
        $instance->phpunit_add_session_store('phpunittest2');
1917
        $instance->phpunit_add_definition('phpunit/multi_loader', array(
1902
        $instance->phpunit_add_definition('phpunit/multi_loader', [
1918
            'mode' => cache_store::MODE_SESSION,
1903
            'mode' => store::MODE_SESSION,
1919
            'component' => 'phpunit',
1904
            'component' => 'phpunit',
1920
            'area' => 'multi_loader'
1905
            'area' => 'multi_loader',
1921
        ));
1906
        ]);
1922
        $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest1', 3);
1907
        $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest1', 3);
Línea 1923... Línea 1908...
1923
        $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest2', 2);
1908
        $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest2', 2);
1924
 
1909
 
1925
        $cache = cache::make('phpunit', 'multi_loader');
1910
        $cache = cache::make('phpunit', 'multi_loader');
1926
        $this->assertInstanceOf(cache_session::class, $cache);
1911
        $this->assertInstanceOf(session_cache::class, $cache);
1927
        $this->assertFalse($cache->get('test'));
1912
        $this->assertFalse($cache->get('test'));
1928
        $this->assertTrue($cache->set('test', 'test'));
1913
        $this->assertTrue($cache->set('test', 'test'));
1929
        $this->assertEquals('test', $cache->get('test'));
1914
        $this->assertEquals('test', $cache->get('test'));
1930
        $this->assertTrue($cache->delete('test'));
1915
        $this->assertTrue($cache->delete('test'));
1931
        $this->assertFalse($cache->get('test'));
1916
        $this->assertFalse($cache->get('test'));
1932
        $this->assertTrue($cache->set('test', 'test'));
1917
        $this->assertTrue($cache->set('test', 'test'));
Línea 1933... Línea 1918...
1933
        $this->assertTrue($cache->purge());
1918
        $this->assertTrue($cache->purge());
1934
        $this->assertFalse($cache->get('test'));
1919
        $this->assertFalse($cache->get('test'));
1935
 
1920
 
1936
        // Test the many commands.
1921
        // Test the many commands.
1937
        $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
1922
        $this->assertEquals(3, $cache->set_many(['a' => 'A', 'b' => 'B', 'c' => 'C']));
1938
        $result = $cache->get_many(array('a', 'b', 'c'));
1923
        $result = $cache->get_many(['a', 'b', 'c']);
1939
        $this->assertIsArray($result);
1924
        $this->assertIsArray($result);
1940
        $this->assertCount(3, $result);
1925
        $this->assertCount(3, $result);
1941
        $this->assertArrayHasKey('a', $result);
1926
        $this->assertArrayHasKey('a', $result);
1942
        $this->assertArrayHasKey('b', $result);
1927
        $this->assertArrayHasKey('b', $result);
1943
        $this->assertArrayHasKey('c', $result);
1928
        $this->assertArrayHasKey('c', $result);
1944
        $this->assertEquals('A', $result['a']);
1929
        $this->assertEquals('A', $result['a']);
1945
        $this->assertEquals('B', $result['b']);
1930
        $this->assertEquals('B', $result['b']);
1946
        $this->assertEquals('C', $result['c']);
1931
        $this->assertEquals('C', $result['c']);
1947
        $this->assertEquals($result, $cache->get_many(array('a', 'b', 'c')));
1932
        $this->assertEquals($result, $cache->get_many(['a', 'b', 'c']));
1948
        $this->assertEquals(2, $cache->delete_many(array('a', 'c')));
1933
        $this->assertEquals(2, $cache->delete_many(['a', 'c']));
1949
        $result = $cache->get_many(array('a', 'b', 'c'));
1934
        $result = $cache->get_many(['a', 'b', 'c']);
1950
        $this->assertIsArray($result);
1935
        $this->assertIsArray($result);
1951
        $this->assertCount(3, $result);
1936
        $this->assertCount(3, $result);
Línea 1961... Línea 1946...
1961
        $this->assertSame('test', $cache->get('test'));
1946
        $this->assertSame('test', $cache->get('test'));
1962
        $this->assertTrue($cache->delete('test', false));
1947
        $this->assertTrue($cache->delete('test', false));
1963
        // We should still have it on a deeper loader.
1948
        // We should still have it on a deeper loader.
1964
        $this->assertSame('test', $cache->get('test'));
1949
        $this->assertSame('test', $cache->get('test'));
1965
        // Test non-recusive with many functions.
1950
        // Test non-recusive with many functions.
1966
        $this->assertSame(3, $cache->set_many(array(
1951
        $this->assertSame(3, $cache->set_many([
1967
            'one' => 'one',
1952
            'one' => 'one',
1968
            'two' => 'two',
1953
            'two' => 'two',
1969
            'three' => 'three'
1954
            'three' => 'three',
1970
        )));
1955
        ]));
1971
        $this->assertSame('one', $cache->get('one'));
1956
        $this->assertSame('one', $cache->get('one'));
1972
        $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
1957
        $this->assertSame(['two' => 'two', 'three' => 'three'], $cache->get_many(['two', 'three']));
1973
        $this->assertSame(3, $cache->delete_many(array('one', 'two', 'three'), false));
1958
        $this->assertSame(3, $cache->delete_many(['one', 'two', 'three'], false));
1974
        $this->assertSame('one', $cache->get('one'));
1959
        $this->assertSame('one', $cache->get('one'));
1975
        $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
1960
        $this->assertSame(['two' => 'two', 'three' => 'three'], $cache->get_many(['two', 'three']));
1976
    }
1961
    }
Línea 1977... Línea 1962...
1977
 
1962
 
1978
    /**
1963
    /**
1979
     * Test switching users with session caches.
1964
     * Test switching users with session caches.
1980
     */
1965
     */
1981
    public function test_session_cache_switch_user(): void {
1966
    public function test_session_cache_switch_user(): void {
1982
        $this->resetAfterTest(true);
1967
        $this->resetAfterTest(true);
1983
        $cache = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'sessioncache');
1968
        $cache = cache::make_from_params(store::MODE_SESSION, 'phpunit', 'sessioncache');
1984
        $user1 = $this->getDataGenerator()->create_user();
1969
        $user1 = $this->getDataGenerator()->create_user();
Línea 1985... Línea 1970...
1985
        $user2 = $this->getDataGenerator()->create_user();
1970
        $user2 = $this->getDataGenerator()->create_user();
1986
 
1971
 
Línea 2006... Línea 1991...
2006
    /**
1991
    /**
2007
     * Test switching users with session caches.
1992
     * Test switching users with session caches.
2008
     */
1993
     */
2009
    public function test_session_cache_switch_user_application_mapping(): void {
1994
    public function test_session_cache_switch_user_application_mapping(): void {
2010
        $this->resetAfterTest(true);
1995
        $this->resetAfterTest(true);
2011
        $instance = cache_config_testing::instance(true);
1996
        $instance = cache_config_testing::instance();
2012
        $instance->phpunit_add_file_store('testfilestore');
1997
        $instance->phpunit_add_file_store('testfilestore');
2013
        $instance->phpunit_add_definition('phpunit/testappsession', array(
1998
        $instance->phpunit_add_definition('phpunit/testappsession', [
2014
            'mode' => cache_store::MODE_SESSION,
1999
            'mode' => store::MODE_SESSION,
2015
            'component' => 'phpunit',
2000
            'component' => 'phpunit',
2016
            'area' => 'testappsession'
2001
            'area' => 'testappsession',
2017
        ));
2002
        ]);
2018
        $instance->phpunit_add_definition_mapping('phpunit/testappsession', 'testfilestore', 3);
2003
        $instance->phpunit_add_definition_mapping('phpunit/testappsession', 'testfilestore', 3);
2019
        $cache = cache::make('phpunit', 'testappsession');
2004
        $cache = cache::make('phpunit', 'testappsession');
2020
        $user1 = $this->getDataGenerator()->create_user();
2005
        $user1 = $this->getDataGenerator()->create_user();
2021
        $user2 = $this->getDataGenerator()->create_user();
2006
        $user2 = $this->getDataGenerator()->create_user();
Línea 2041... Línea 2026...
2041
 
2026
 
2042
    /**
2027
    /**
2043
     * Test two session caches being used at once to confirm collisions don't occur.
2028
     * Test two session caches being used at once to confirm collisions don't occur.
2044
     */
2029
     */
2045
    public function test_dual_session_caches(): void {
2030
    public function test_dual_session_caches(): void {
2046
        $instance = cache_config_testing::instance(true);
2031
        $instance = cache_config_testing::instance();
2047
        $instance->phpunit_add_definition('phpunit/testsess1', array(
2032
        $instance->phpunit_add_definition('phpunit/testsess1', [
2048
            'mode' => cache_store::MODE_SESSION,
2033
            'mode' => store::MODE_SESSION,
2049
            'component' => 'phpunit',
2034
            'component' => 'phpunit',
2050
            'area' => 'testsess1'
2035
            'area' => 'testsess1',
2051
        ));
2036
        ]);
2052
        $instance->phpunit_add_definition('phpunit/testsess2', array(
2037
        $instance->phpunit_add_definition('phpunit/testsess2', [
2053
            'mode' => cache_store::MODE_SESSION,
2038
            'mode' => store::MODE_SESSION,
2054
            'component' => 'phpunit',
2039
            'component' => 'phpunit',
2055
            'area' => 'testsess2'
2040
            'area' => 'testsess2',
2056
        ));
2041
        ]);
2057
        $cache1 = cache::make('phpunit', 'testsess1');
2042
        $cache1 = cache::make('phpunit', 'testsess1');
Línea 2058... Línea 2043...
2058
        $cache2 = cache::make('phpunit', 'testsess2');
2043
        $cache2 = cache::make('phpunit', 'testsess2');
2059
 
2044
 
Línea 2076... Línea 2061...
2076
    /**
2061
    /**
2077
     * Test multiple session caches when switching user.
2062
     * Test multiple session caches when switching user.
2078
     */
2063
     */
2079
    public function test_session_cache_switch_user_multiple(): void {
2064
    public function test_session_cache_switch_user_multiple(): void {
2080
        $this->resetAfterTest(true);
2065
        $this->resetAfterTest(true);
2081
        $cache1 = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'sessioncache1');
2066
        $cache1 = cache::make_from_params(store::MODE_SESSION, 'phpunit', 'sessioncache1');
2082
        $cache2 = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'sessioncache2');
2067
        $cache2 = cache::make_from_params(store::MODE_SESSION, 'phpunit', 'sessioncache2');
2083
        $user1 = $this->getDataGenerator()->create_user();
2068
        $user1 = $this->getDataGenerator()->create_user();
2084
        $user2 = $this->getDataGenerator()->create_user();
2069
        $user2 = $this->getDataGenerator()->create_user();
Línea 2085... Línea 2070...
2085
 
2070
 
2086
        // Log in as the first user.
2071
        // Log in as the first user.
Línea 2105... Línea 2090...
2105
    }
2090
    }
Línea 2106... Línea 2091...
2106
 
2091
 
2107
    /**
2092
    /**
2108
     * The application locking feature should work with caches that support multiple identifiers
2093
     * The application locking feature should work with caches that support multiple identifiers
2109
     * (static cache and MongoDB with a specific setting).
-
 
2110
     *
-
 
2111
     * @covers \cache_application
2094
     * (static cache and MongoDB with a specific setting).
2112
     */
2095
     */
2113
    public function test_application_locking_multiple_identifier_cache(): void {
2096
    public function test_application_locking_multiple_identifier_cache(): void {
2114
        // Get an arbitrary definition (modinfo).
2097
        // Get an arbitrary definition (modinfo).
2115
        $instance = cache_config_testing::instance(true);
2098
        $instance = cache_config_testing::instance();
2116
        $definitions = $instance->get_definitions();
2099
        $definitions = $instance->get_definitions();
Línea 2117... Línea 2100...
2117
        $definition = \cache_definition::load('phpunit', $definitions['core/coursemodinfo']);
2100
        $definition = definition::load('phpunit', $definitions['core/coursemodinfo']);
2118
 
2101
 
2119
        // Set up a static cache using that definition, wrapped in cache_application so we can do
2102
        // Set up a static cache using that definition, wrapped in application_cache so we can do
2120
        // locking.
2103
        // locking.
2121
        $store = new \cachestore_static('test');
2104
        $store = new \cachestore_static('test');
Línea 2122... Línea 2105...
2122
        $store->initialise($definition);
2105
        $store->initialise($definition);
2123
        $cache = new cache_application($definition, $store);
2106
        $cache = new application_cache($definition, $store);
2124
 
2107
 
2125
        // Test the three locking functions.
2108
        // Test the three locking functions.
2126
        $cache->acquire_lock('frog');
2109
        $cache->acquire_lock('frog');
Línea 2127... Línea 2110...
2127
        $this->assertTrue($cache->check_lock_state('frog'));
2110
        $this->assertTrue($cache->check_lock_state('frog'));
2128
        $cache->release_lock('frog');
2111
        $cache->release_lock('frog');
2129
    }
-
 
2130
 
-
 
2131
    /**
2112
    }
2132
     * Test requiring a lock before attempting to set a key.
2113
 
2133
     *
2114
    /**
2134
     * @covers ::set_implementation
2115
     * Test requiring a lock before attempting to set a key.
2135
     */
2116
     */
2136
    public function test_application_locking_before_write(): void {
2117
    public function test_application_locking_before_write(): void {
2137
        $instance = cache_config_testing::instance(true);
2118
        $instance = cache_config_testing::instance();
2138
        $instance->phpunit_add_definition('phpunit/test_application_locking', array(
2119
        $instance->phpunit_add_definition('phpunit/test_application_locking', [
2139
            'mode' => cache_store::MODE_APPLICATION,
2120
            'mode' => store::MODE_APPLICATION,
2140
            'component' => 'phpunit',
2121
            'component' => 'phpunit',
2141
            'area' => 'test_application_locking',
2122
            'area' => 'test_application_locking',
2142
            'staticacceleration' => true,
2123
            'staticacceleration' => true,
2143
            'staticaccelerationsize' => 1,
2124
            'staticaccelerationsize' => 1,
Línea 2144... Línea 2125...
2144
            'requirelockingbeforewrite' => true
2125
            'requirelockingbeforewrite' => true,
2145
        ));
2126
        ]);
2146
        $cache = cache::make('phpunit', 'test_application_locking');
2127
        $cache = cache::make('phpunit', 'test_application_locking');
2147
        $this->assertInstanceOf(cache_application::class, $cache);
2128
        $this->assertInstanceOf(application_cache::class, $cache);
Línea 2148... Línea 2129...
2148
 
2129
 
2149
        $cache->acquire_lock('a');
2130
        $cache->acquire_lock('a');
2150
        try {
2131
        try {
2151
            // Set with lock.
2132
            // Set with lock.
2152
            $this->assertTrue($cache->set('a', 'A'));
2133
            $this->assertTrue($cache->set('a', 'A'));
2153
 
2134
 
2154
            // Set without lock.
2135
            // Set without lock.
2155
            try {
2136
            try {
2156
                $cache->set('b', 'B');
2137
                $cache->set('b', 'B');
-
 
2138
                $this->fail();
2157
                $this->fail();
2139
            } catch (coding_exception $e) {
Línea 2158... Línea 2140...
2158
            } catch (\coding_exception $e) {
2140
                $this->assertStringContainsString(
2159
                $this->assertStringContainsString(
2141
                    'Attempted to set cache key "b" without a lock. ' .
2160
                        'Attempted to set cache key "b" without a lock. ' .
2142
                        'Locking before writes is required for phpunit/test_application_locking',
2161
                        'Locking before writes is required for phpunit/test_application_locking',
2143
                    $e->getMessage()
2162
                        $e->getMessage());
2144
                );
2163
            }
2145
            }
2164
 
2146
 
2165
            // Set many without full lock.
2147
            // Set many without full lock.
-
 
2148
            try {
2166
            try {
2149
                $cache->set_many(['a' => 'AA', 'b' => 'BB']);
Línea 2167... Línea 2150...
2167
                $cache->set_many(['a' => 'AA', 'b' => 'BB']);
2150
                $this->fail();
2168
                $this->fail();
2151
            } catch (coding_exception $e) {
Línea 2191... Línea 2174...
2191
 
2174
 
2192
            // Delete key without lock.
2175
            // Delete key without lock.
2193
            try {
2176
            try {
2194
                $cache->delete('b');
2177
                $cache->delete('b');
2195
                $this->fail();
2178
                $this->fail();
2196
            } catch (\coding_exception $e) {
2179
            } catch (coding_exception $e) {
2197
                $this->assertStringContainsString(
2180
                $this->assertStringContainsString(
2198
                        'Attempted to delete cache key "b" without a lock.',
2181
                    'Attempted to delete cache key "b" without a lock.',
-
 
2182
                    $e->getMessage()
2199
                        $e->getMessage());
2183
                );
Línea 2200... Línea 2184...
2200
            }
2184
            }
2201
 
2185
 
2202
            // Delete many without full lock.
2186
            // Delete many without full lock.
2203
            $cache->set('a', 'AAA');
2187
            $cache->set('a', 'AAA');
2204
            try {
2188
            try {
2205
                $cache->delete_many(['a', 'b']);
2189
                $cache->delete_many(['a', 'b']);
2206
                $this->fail();
2190
                $this->fail();
2207
            } catch (\coding_exception $e) {
2191
            } catch (coding_exception $e) {
2208
                $this->assertStringContainsString(
2192
                $this->assertStringContainsString(
-
 
2193
                    'Attempted to delete cache key "b" without a lock.',
2209
                        'Attempted to delete cache key "b" without a lock.',
2194
                    $e->getMessage()
2210
                        $e->getMessage());
2195
                );
2211
            }
2196
            }
Línea 2212... Línea 2197...
2212
            // Nothing was deleted.
2197
            // Nothing was deleted.
Línea 2227... Línea 2212...
2227
    }
2212
    }
Línea 2228... Línea 2213...
2228
 
2213
 
2229
    /**
2214
    /**
2230
     * Test that locking before write works when writing across multiple layers.
2215
     * Test that locking before write works when writing across multiple layers.
2231
     *
2216
     *
2232
     * @covers \cache_loader
-
 
2233
     * @return void
2217
     * @covers \core_cache\application_cache
2234
     */
2218
     */
Línea 2235... Línea 2219...
2235
    public function test_application_locking_multiple_layers(): void {
2219
    public function test_application_locking_multiple_layers(): void {
2236
 
2220
 
2237
        $instance = cache_config_testing::instance(true);
2221
        $instance = cache_config_testing::instance();
2238
        $instance->phpunit_add_definition('phpunit/test_application_locking', array(
2222
        $instance->phpunit_add_definition('phpunit/test_application_locking', [
2239
            'mode' => cache_store::MODE_APPLICATION,
2223
            'mode' => store::MODE_APPLICATION,
2240
            'component' => 'phpunit',
2224
            'component' => 'phpunit',
2241
            'area' => 'test_application_locking',
2225
            'area' => 'test_application_locking',
2242
            'staticacceleration' => true,
2226
            'staticacceleration' => true,
2243
            'staticaccelerationsize' => 1,
2227
            'staticaccelerationsize' => 1,
2244
            'requirelockingbeforewrite' => true
2228
            'requirelockingbeforewrite' => true,
2245
        ), false);
2229
        ], false);
2246
        $instance->phpunit_add_file_store('phpunittest1');
2230
        $instance->phpunit_add_file_store('phpunittest1');
2247
        $instance->phpunit_add_file_store('phpunittest2');
2231
        $instance->phpunit_add_file_store('phpunittest2');
Línea 2248... Línea 2232...
2248
        $instance->phpunit_add_definition_mapping('phpunit/test_application_locking', 'phpunittest1', 1);
2232
        $instance->phpunit_add_definition_mapping('phpunit/test_application_locking', 'phpunittest1', 1);
2249
        $instance->phpunit_add_definition_mapping('phpunit/test_application_locking', 'phpunittest2', 2);
2233
        $instance->phpunit_add_definition_mapping('phpunit/test_application_locking', 'phpunittest2', 2);
Línea 2250... Línea 2234...
2250
 
2234
 
2251
        $cache = cache::make('phpunit', 'test_application_locking');
2235
        $cache = cache::make('phpunit', 'test_application_locking');
2252
        $this->assertInstanceOf(cache_application::class, $cache);
2236
        $this->assertInstanceOf(application_cache::class, $cache);
Línea 2278... Línea 2262...
2278
 
2262
 
2279
        // Try the tests again with a third layer.
2263
        // Try the tests again with a third layer.
2280
        $instance->phpunit_add_file_store('phpunittest3');
2264
        $instance->phpunit_add_file_store('phpunittest3');
2281
        $instance->phpunit_add_definition_mapping('phpunit/test_application_locking', 'phpunittest3', 3);
2265
        $instance->phpunit_add_definition_mapping('phpunit/test_application_locking', 'phpunittest3', 3);
2282
        $cache = cache::make('phpunit', 'test_application_locking');
2266
        $cache = cache::make('phpunit', 'test_application_locking');
Línea 2283... Línea 2267...
2283
        $this->assertInstanceOf(cache_application::class, $cache);
2267
        $this->assertInstanceOf(application_cache::class, $cache);
2284
 
2268
 
2285
        // Check that we can set a key across multiple layers.
2269
        // Check that we can set a key across multiple layers.
Línea 2307... Línea 2291...
2307
    }
2291
    }
Línea 2308... Línea 2292...
2308
 
2292
 
2309
    /**
2293
    /**
2310
     * Tests that locking fails correctly when either layer of a 2-layer cache has a lock already.
2294
     * Tests that locking fails correctly when either layer of a 2-layer cache has a lock already.
2311
     *
2295
     *
2312
     * @covers \cache_loader
2296
     * @covers \core_cache\application_cache
2313
     */
2297
     */
Línea 2314... Línea 2298...
2314
    public function test_application_locking_multiple_layers_failures(): void {
2298
    public function test_application_locking_multiple_layers_failures(): void {
2315
 
2299
 
2316
        $instance = cache_config_testing::instance(true);
2300
        $instance = cache_config_testing::instance();
2317
        $instance->phpunit_add_definition('phpunit/test_application_locking', array(
2301
        $instance->phpunit_add_definition('phpunit/test_application_locking', [
2318
            'mode' => cache_store::MODE_APPLICATION,
2302
            'mode' => store::MODE_APPLICATION,
2319
            'component' => 'phpunit',
2303
            'component' => 'phpunit',
2320
            'area' => 'test_application_locking',
2304
            'area' => 'test_application_locking',
2321
            'staticacceleration' => true,
2305
            'staticacceleration' => true,
2322
            'staticaccelerationsize' => 1,
2306
            'staticaccelerationsize' => 1,
2323
            'requirelockingbeforewrite' => true
2307
            'requirelockingbeforewrite' => true,
2324
        ), false);
2308
        ], false);
2325
        $instance->phpunit_add_file_store('phpunittest1');
2309
        $instance->phpunit_add_file_store('phpunittest1');
2326
        $instance->phpunit_add_file_store('phpunittest2');
2310
        $instance->phpunit_add_file_store('phpunittest2');
Línea 2346... Línea 2330...
2346
 
2330
 
2347
        $lockwaitprop->setValue($localstore, 1);
2331
        $lockwaitprop->setValue($localstore, 1);
Línea 2348... Línea 2332...
2348
        $lockwaitprop->setValue($sharedstore, 1);
2332
        $lockwaitprop->setValue($sharedstore, 1);
2349
 
2333
 
2350
        // Get key details and the cache identifier.
2334
        // Get key details and the cache identifier.
2351
        $hashedkey = cache_helper::hash_key('apple', $definition);
2335
        $hashedkey = helper::hash_key('apple', $definition);
Línea 2352... Línea 2336...
2352
        $localidentifier = $cache->get_identifier();
2336
        $localidentifier = $cache->get_identifier();
2353
        $sharedidentifier = $sharedcache->get_identifier();
2337
        $sharedidentifier = $sharedcache->get_identifier();
2354
 
2338
 
2355
        // 1. Local cache is not locked but parent cache is locked.
2339
        // 1. Local cache is not locked but parent cache is locked.
2356
        $sharedstore->acquire_lock($hashedkey, 'somebodyelse');
2340
        $sharedstore->acquire_lock($hashedkey, 'somebodyelse');
2357
        try {
2341
        try {
2358
            try {
2342
            try {
2359
                $cache->acquire_lock('apple');
2343
                $cache->acquire_lock('apple');
Línea 2360... Línea 2344...
2360
                $this->fail();
2344
                $this->fail();
2361
            } catch (\moodle_exception $e) {
2345
            } catch (moodle_exception $e) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
2362
            }
2346
            }
Línea 2373... Línea 2357...
2373
        $localstore->acquire_lock($hashedkey, 'somebodyelse');
2357
        $localstore->acquire_lock($hashedkey, 'somebodyelse');
2374
        try {
2358
        try {
2375
            try {
2359
            try {
2376
                $cache->acquire_lock('apple');
2360
                $cache->acquire_lock('apple');
2377
                $this->fail();
2361
                $this->fail();
2378
            } catch (\moodle_exception $e) {
2362
            } catch (moodle_exception $e) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
2379
 
-
 
2380
            }
2363
            }
Línea 2381... Línea 2364...
2381
 
2364
 
2382
            // Neither store is locked by us, local store still locked.
2365
            // Neither store is locked by us, local store still locked.
2383
            $this->assertFalse((bool)$localstore->check_lock_state($hashedkey, $localidentifier));
2366
            $this->assertFalse((bool)$localstore->check_lock_state($hashedkey, $localidentifier));
Línea 2396... Línea 2379...
2396
            $cache->release_lock('apple');
2379
            $cache->release_lock('apple');
2397
        }
2380
        }
2398
    }
2381
    }
Línea 2399... Línea 2382...
2399
 
2382
 
2400
    /**
2383
    /**
2401
     * Test the static cache_helper method purge_stores_used_by_definition.
2384
     * Test the static helper method purge_stores_used_by_definition.
2402
     */
2385
     */
2403
    public function test_purge_stores_used_by_definition(): void {
2386
    public function test_purge_stores_used_by_definition(): void {
2404
        $instance = cache_config_testing::instance(true);
2387
        $instance = cache_config_testing::instance();
2405
        $instance->phpunit_add_definition('phpunit/test_purge_stores_used_by_definition', array(
2388
        $instance->phpunit_add_definition('phpunit/test_purge_stores_used_by_definition', [
2406
            'mode' => cache_store::MODE_APPLICATION,
2389
            'mode' => store::MODE_APPLICATION,
2407
            'component' => 'phpunit',
2390
            'component' => 'phpunit',
2408
            'area' => 'test_purge_stores_used_by_definition'
2391
            'area' => 'test_purge_stores_used_by_definition',
2409
        ));
2392
        ]);
2410
        $cache = cache::make('phpunit', 'test_purge_stores_used_by_definition');
2393
        $cache = cache::make('phpunit', 'test_purge_stores_used_by_definition');
2411
        $this->assertInstanceOf(cache_application::class, $cache);
2394
        $this->assertInstanceOf(application_cache::class, $cache);
2412
        $this->assertTrue($cache->set('test', 'test'));
2395
        $this->assertTrue($cache->set('test', 'test'));
Línea 2413... Línea 2396...
2413
        unset($cache);
2396
        unset($cache);
Línea 2414... Línea 2397...
2414
 
2397
 
2415
        cache_helper::purge_stores_used_by_definition('phpunit', 'test_purge_stores_used_by_definition');
2398
        helper::purge_stores_used_by_definition('phpunit', 'test_purge_stores_used_by_definition');
2416
 
2399
 
2417
        $cache = cache::make('phpunit', 'test_purge_stores_used_by_definition');
2400
        $cache = cache::make('phpunit', 'test_purge_stores_used_by_definition');
Línea 2418... Línea 2401...
2418
        $this->assertInstanceOf(cache_application::class, $cache);
2401
        $this->assertInstanceOf(application_cache::class, $cache);
2419
        $this->assertFalse($cache->get('test'));
2402
        $this->assertFalse($cache->get('test'));
2420
    }
2403
    }
2421
 
2404
 
2422
    /**
2405
    /**
2423
     * Test purge routines.
2406
     * Test purge routines.
2424
     */
2407
     */
2425
    public function test_purge_routines(): void {
2408
    public function test_purge_routines(): void {
2426
        $instance = cache_config_testing::instance(true);
2409
        $instance = cache_config_testing::instance();
2427
        $instance->phpunit_add_definition('phpunit/purge1', array(
2410
        $instance->phpunit_add_definition('phpunit/purge1', [
2428
            'mode' => cache_store::MODE_APPLICATION,
2411
            'mode' => store::MODE_APPLICATION,
2429
            'component' => 'phpunit',
2412
            'component' => 'phpunit',
2430
            'area' => 'purge1'
2413
            'area' => 'purge1',
2431
        ));
2414
        ]);
2432
        $instance->phpunit_add_definition('phpunit/purge2', array(
2415
        $instance->phpunit_add_definition('phpunit/purge2', [
2433
            'mode' => cache_store::MODE_APPLICATION,
2416
            'mode' => store::MODE_APPLICATION,
2434
            'component' => 'phpunit',
2417
            'component' => 'phpunit',
2435
            'area' => 'purge2',
2418
            'area' => 'purge2',
Línea 2436... Línea 2419...
2436
            'requireidentifiers' => array(
2419
            'requireidentifiers' => [
2437
                'id'
2420
                'id',
2438
            )
2421
            ],
2439
        ));
2422
        ]);
2440
 
2423
 
2441
        $factory = cache_factory::instance();
2424
        $factory = factory::instance();
2442
        $definition = $factory->create_definition('phpunit', 'purge1');
2425
        $definition = $factory->create_definition('phpunit', 'purge1');
2443
        $this->assertFalse($definition->has_required_identifiers());
2426
        $this->assertFalse($definition->has_required_identifiers());
2444
        $cache = $factory->create_cache($definition);
2427
        $cache = $factory->create_cache($definition);
Línea 2445... Línea 2428...
2445
        $this->assertInstanceOf(cache_application::class, $cache);
2428
        $this->assertInstanceOf(application_cache::class, $cache);
2446
        $this->assertTrue($cache->set('test', 'test'));
2429
        $this->assertTrue($cache->set('test', 'test'));
2447
        $this->assertTrue($cache->has('test'));
2430
        $this->assertTrue($cache->has('test'));
2448
        cache_helper::purge_by_definition('phpunit', 'purge1');
2431
        helper::purge_by_definition('phpunit', 'purge1');
2449
        $this->assertFalse($cache->has('test'));
2432
        $this->assertFalse($cache->has('test'));
2450
 
2433
 
2451
        $factory = cache_factory::instance();
2434
        $factory = factory::instance();
2452
        $definition = $factory->create_definition('phpunit', 'purge2');
2435
        $definition = $factory->create_definition('phpunit', 'purge2');
2453
        $this->assertTrue($definition->has_required_identifiers());
2436
        $this->assertTrue($definition->has_required_identifiers());
Línea 2454... Línea 2437...
2454
        $cache = $factory->create_cache($definition);
2437
        $cache = $factory->create_cache($definition);
2455
        $this->assertInstanceOf(cache_application::class, $cache);
2438
        $this->assertInstanceOf(application_cache::class, $cache);
2456
        $this->assertTrue($cache->set('test', 'test'));
2439
        $this->assertTrue($cache->set('test', 'test'));
2457
        $this->assertTrue($cache->has('test'));
2440
        $this->assertTrue($cache->has('test'));
2458
        cache_helper::purge_stores_used_by_definition('phpunit', 'purge2');
2441
        helper::purge_stores_used_by_definition('phpunit', 'purge2');
2459
        $this->assertFalse($cache->has('test'));
2442
        $this->assertFalse($cache->has('test'));
2460
 
2443
 
Línea 2461... Línea 2444...
2461
        try {
2444
        try {
2462
            cache_helper::purge_by_definition('phpunit', 'purge2');
2445
            helper::purge_by_definition('phpunit', 'purge2');
2463
            $this->fail('Should not be able to purge a definition required identifiers without providing them.');
2446
            $this->fail('Should not be able to purge a definition required identifiers without providing them.');
2464
        } catch (\coding_exception $ex) {
2447
        } catch (coding_exception $ex) {
2465
            $this->assertStringContainsString('Identifier required for cache has not been provided', $ex->getMessage());
2448
            $this->assertStringContainsString('Identifier required for cache has not been provided', $ex->getMessage());
2466
        }
2449
        }
2467
    }
2450
    }
2468
 
2451
 
2469
    /**
2452
    /**
Línea 2470... Línea 2453...
2470
     * Tests that ad-hoc caches are correctly purged with a purge_all call.
2453
     * Tests that ad-hoc caches are correctly purged with a purge_all call.
2471
     */
2454
     */
2472
    public function test_purge_all_with_adhoc_caches(): void {
2455
    public function test_purge_all_with_adhoc_caches(): void {
2473
        $cache = cache::make_from_params(cache_store::MODE_REQUEST, 'core_cache', 'test');
2456
        $cache = cache::make_from_params(store::MODE_REQUEST, 'core_cache', 'test');
2474
        $cache->set('test', 123);
2457
        $cache->set('test', 123);
2475
        cache_helper::purge_all();
2458
        helper::purge_all();
2476
        $this->assertFalse($cache->get('test'));
2459
        $this->assertFalse($cache->get('test'));
2477
    }
2460
    }
2478
 
2461
 
2479
    /**
2462
    /**
2480
     * Test that the default stores all support searching.
2463
     * Test that the default stores all support searching.
2481
     */
2464
     */
2482
    public function test_defaults_support_searching(): void {
2465
    public function test_defaults_support_searching(): void {
2483
        $instance = cache_config_testing::instance(true);
2466
        $instance = cache_config_testing::instance();
2484
        $instance->phpunit_add_definition('phpunit/search1', array(
2467
        $instance->phpunit_add_definition('phpunit/search1', [
2485
            'mode' => cache_store::MODE_APPLICATION,
2468
            'mode' => store::MODE_APPLICATION,
2486
            'component' => 'phpunit',
2469
            'component' => 'phpunit',
2487
            'area' => 'search1',
2470
            'area' => 'search1',
2488
            'requiresearchable' => true
2471
            'requiresearchable' => true,
2489
        ));
2472
        ]);
2490
        $instance->phpunit_add_definition('phpunit/search2', array(
2473
        $instance->phpunit_add_definition('phpunit/search2', [
2491
            'mode' => cache_store::MODE_SESSION,
2474
            'mode' => store::MODE_SESSION,
2492
            'component' => 'phpunit',
2475
            'component' => 'phpunit',
2493
            'area' => 'search2',
2476
            'area' => 'search2',
Línea 2494... Línea 2477...
2494
            'requiresearchable' => true
2477
            'requiresearchable' => true,
2495
        ));
2478
        ]);
2496
        $instance->phpunit_add_definition('phpunit/search3', array(
2479
        $instance->phpunit_add_definition('phpunit/search3', [
2497
            'mode' => cache_store::MODE_REQUEST,
2480
            'mode' => store::MODE_REQUEST,
2498
            'component' => 'phpunit',
2481
            'component' => 'phpunit',
2499
            'area' => 'search3',
2482
            'area' => 'search3',
2500
            'requiresearchable' => true
2483
            'requiresearchable' => true,
Línea 2501... Línea 2484...
2501
        ));
2484
        ]);
2502
        $factory = cache_factory::instance();
2485
        $factory = factory::instance();
2503
 
2486
 
2504
        // Test application cache is searchable.
2487
        // Test application cache is searchable.
2505
        $definition = $factory->create_definition('phpunit', 'search1');
2488
        $definition = $factory->create_definition('phpunit', 'search1');
2506
        $this->assertInstanceOf(cache_definition::class, $definition);
2489
        $this->assertInstanceOf(definition::class, $definition);
2507
        $this->assertEquals(cache_store::IS_SEARCHABLE, $definition->get_requirements_bin() & cache_store::IS_SEARCHABLE);
2490
        $this->assertEquals(store::IS_SEARCHABLE, $definition->get_requirements_bin() & store::IS_SEARCHABLE);
Línea 2508... Línea 2491...
2508
        $cache = $factory->create_cache($definition);
2491
        $cache = $factory->create_cache($definition);
2509
        $this->assertInstanceOf(cache_application::class, $cache);
2492
        $this->assertInstanceOf(application_cache::class, $cache);
2510
        $this->assertArrayHasKey('cache_is_searchable', $cache->phpunit_get_store_implements());
2493
        $this->assertInstanceOf(searchable_cache_interface::class, $cache->get_store());
2511
 
2494
 
2512
        // Test session cache is searchable.
2495
        // Test session cache is searchable.
2513
        $definition = $factory->create_definition('phpunit', 'search2');
2496
        $definition = $factory->create_definition('phpunit', 'search2');
2514
        $this->assertInstanceOf(cache_definition::class, $definition);
2497
        $this->assertInstanceOf(definition::class, $definition);
2515
        $this->assertEquals(cache_store::IS_SEARCHABLE, $definition->get_requirements_bin() & cache_store::IS_SEARCHABLE);
2498
        $this->assertEquals(store::IS_SEARCHABLE, $definition->get_requirements_bin() & store::IS_SEARCHABLE);
Línea 2516... Línea 2499...
2516
        $cache = $factory->create_cache($definition);
2499
        $cache = $factory->create_cache($definition);
2517
        $this->assertInstanceOf(cache_session::class, $cache);
2500
        $this->assertInstanceOf(session_cache::class, $cache);
2518
        $this->assertArrayHasKey('cache_is_searchable', $cache->phpunit_get_store_implements());
2501
        $this->assertInstanceOf(searchable_cache_interface::class, $cache->get_store());
2519
 
2502
 
2520
        // Test request cache is searchable.
2503
        // Test request cache is searchable.
2521
        $definition = $factory->create_definition('phpunit', 'search3');
2504
        $definition = $factory->create_definition('phpunit', 'search3');
2522
        $this->assertInstanceOf(cache_definition::class, $definition);
2505
        $this->assertInstanceOf(definition::class, $definition);
2523
        $this->assertEquals(cache_store::IS_SEARCHABLE, $definition->get_requirements_bin() & cache_store::IS_SEARCHABLE);
2506
        $this->assertEquals(store::IS_SEARCHABLE, $definition->get_requirements_bin() & store::IS_SEARCHABLE);
2524
        $cache = $factory->create_cache($definition);
2507
        $cache = $factory->create_cache($definition);
2525
        $this->assertInstanceOf(cache_request::class, $cache);
2508
        $this->assertInstanceOf(request_cache::class, $cache);
2526
        $this->assertArrayHasKey('cache_is_searchable', $cache->phpunit_get_store_implements());
2509
        $this->assertInstanceOf(searchable_cache_interface::class, $cache->get_store());
2527
    }
2510
    }
2528
 
2511
 
2529
    /**
2512
    /**
2530
     * Test static acceleration
2513
     * Test static acceleration
2531
     *
2514
     *
2532
     * Note: All the assertGreaterThanOrEqual() in this test should be assertGreaterThan() be because of some microtime()
2515
     * Note: All the assertGreaterThanOrEqual() in this test should be assertGreaterThan() be because of some microtime()
2533
     * resolution problems under some OSs / PHP versions, we are accepting equal as valid outcome. For more info see MDL-57147.
2516
     * resolution problems under some OSs / PHP versions, we are accepting equal as valid outcome. For more info see MDL-57147.
2534
     */
2517
     */
2535
    public function test_static_acceleration(): void {
2518
    public function test_static_acceleration(): void {
2536
        $instance = cache_config_testing::instance();
2519
        $instance = cache_config_testing::instance();
2537
        $instance->phpunit_add_definition('phpunit/accelerated', array(
2520
        $instance->phpunit_add_definition('phpunit/accelerated', [
2538
            'mode' => cache_store::MODE_APPLICATION,
2521
            'mode' => store::MODE_APPLICATION,
2539
            'component' => 'phpunit',
2522
            'component' => 'phpunit',
2540
            'area' => 'accelerated',
2523
            'area' => 'accelerated',
2541
            'staticacceleration' => true,
2524
            'staticacceleration' => true,
2542
            'staticaccelerationsize' => 3,
2525
            'staticaccelerationsize' => 3,
2543
        ));
2526
        ]);
2544
        $instance->phpunit_add_definition('phpunit/accelerated2', array(
2527
        $instance->phpunit_add_definition('phpunit/accelerated2', [
2545
            'mode' => cache_store::MODE_APPLICATION,
2528
            'mode' => store::MODE_APPLICATION,
2546
            'component' => 'phpunit',
2529
            'component' => 'phpunit',
2547
            'area' => 'accelerated2',
2530
            'area' => 'accelerated2',
2548
            'staticacceleration' => true,
2531
            'staticacceleration' => true,
2549
            'staticaccelerationsize' => 3,
2532
            'staticaccelerationsize' => 3,
2550
        ));
2533
        ]);
2551
        $instance->phpunit_add_definition('phpunit/accelerated3', array(
2534
        $instance->phpunit_add_definition('phpunit/accelerated3', [
2552
            'mode' => cache_store::MODE_APPLICATION,
2535
            'mode' => store::MODE_APPLICATION,
2553
            'component' => 'phpunit',
2536
            'component' => 'phpunit',
2554
            'area' => 'accelerated3',
2537
            'area' => 'accelerated3',
2555
            'staticacceleration' => true,
2538
            'staticacceleration' => true,
2556
            'staticaccelerationsize' => 3,
2539
            'staticaccelerationsize' => 3,
2557
        ));
2540
        ]);
2558
        $instance->phpunit_add_definition('phpunit/accelerated4', array(
2541
        $instance->phpunit_add_definition('phpunit/accelerated4', [
2559
            'mode' => cache_store::MODE_APPLICATION,
2542
            'mode' => store::MODE_APPLICATION,
2560
            'component' => 'phpunit',
2543
            'component' => 'phpunit',
2561
            'area' => 'accelerated4',
2544
            'area' => 'accelerated4',
2562
            'staticacceleration' => true,
2545
            'staticacceleration' => true,
2563
            'staticaccelerationsize' => 4,
2546
            'staticaccelerationsize' => 4,
2564
        ));
2547
        ]);
2565
        $instance->phpunit_add_definition('phpunit/simpledataarea1', array(
2548
        $instance->phpunit_add_definition('phpunit/simpledataarea1', [
Línea 2566... Línea 2549...
2566
            'mode' => cache_store::MODE_APPLICATION,
2549
            'mode' => store::MODE_APPLICATION,
2567
            'component' => 'phpunit',
2550
            'component' => 'phpunit',
Línea 2568... Línea 2551...
2568
            'area' => 'simpledataarea1',
2551
            'area' => 'simpledataarea1',
2569
            'staticacceleration' => true,
2552
            'staticacceleration' => true,
2570
            'simpledata' => false
2553
            'simpledata' => false,
2571
        ));
2554
        ]);
2572
        $instance->phpunit_add_definition('phpunit/simpledataarea2', array(
2555
        $instance->phpunit_add_definition('phpunit/simpledataarea2', [
2573
            'mode' => cache_store::MODE_APPLICATION,
2556
            'mode' => store::MODE_APPLICATION,
Línea 2574... Línea 2557...
2574
            'component' => 'phpunit',
2557
            'component' => 'phpunit',
2575
            'area' => 'simpledataarea2',
2558
            'area' => 'simpledataarea2',
2576
            'staticacceleration' => true,
2559
            'staticacceleration' => true,
2577
            'simpledata' => true
2560
            'simpledata' => true,
Línea 2626... Línea 2609...
2626
        $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
2609
        $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
2627
        $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
2610
        $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
2628
        $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
2611
        $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
2629
        $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
2612
        $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
Línea 2630... Línea 2613...
2630
 
2613
 
2631
        // Store a cacheable_object, get many times and ensure each time wake_for_cache is used.
2614
        // Store a cacheable_object_interface, get many times and ensure each time wake_for_cache is used.
2632
        // Both get and get_many are tested.  Two cache entries are used to ensure the times aren't
2615
        // Both get and get_many are tested.  Two cache entries are used to ensure the times aren't
2633
        // confused with multiple calls to get()/get_many().
2616
        // confused with multiple calls to get()/get_many().
2634
        $startmicrotime = microtime(true);
2617
        $startmicrotime = microtime(true);
2635
        $cacheableobject = new cache_phpunit_dummy_object(1, 1, $startmicrotime);
2618
        $cacheableobject = new cache_phpunit_dummy_object(1, 1, $startmicrotime);
Línea 2647... Línea 2630...
2647
        $cachevalue = $cache->get('a');
2630
        $cachevalue = $cache->get('a');
2648
        $this->assertInstanceOf(cache_phpunit_dummy_object::class, $cachevalue);
2631
        $this->assertInstanceOf(cache_phpunit_dummy_object::class, $cachevalue);
2649
        $this->assertGreaterThanOrEqual($staticaccelerationreturntime, $cachevalue->propertytime);
2632
        $this->assertGreaterThanOrEqual($staticaccelerationreturntime, $cachevalue->propertytime);
2650
        $backingstorereturntime = $cachevalue->propertytime;
2633
        $backingstorereturntime = $cachevalue->propertytime;
Línea 2651... Línea 2634...
2651
 
2634
 
2652
        $results = $cache->get_many(array('b'));
2635
        $results = $cache->get_many(['b']);
2653
        $this->assertInstanceOf(cache_phpunit_dummy_object::class, $results['b']);
2636
        $this->assertInstanceOf(cache_phpunit_dummy_object::class, $results['b']);
2654
        $this->assertGreaterThanOrEqual($staticaccelerationreturntimeb, $results['b']->propertytime);
2637
        $this->assertGreaterThanOrEqual($staticaccelerationreturntimeb, $results['b']->propertytime);
Línea 2655... Línea 2638...
2655
        $backingstorereturntimeb = $results['b']->propertytime;
2638
        $backingstorereturntimeb = $results['b']->propertytime;
Línea 2659... Línea 2642...
2659
        // value is stored serialized in the static acceleration cache.
2642
        // value is stored serialized in the static acceleration cache.
2660
        $cachevalue = $cache->phpunit_static_acceleration_get('a');
2643
        $cachevalue = $cache->phpunit_static_acceleration_get('a');
2661
        $this->assertInstanceOf(cache_phpunit_dummy_object::class, $cachevalue);
2644
        $this->assertInstanceOf(cache_phpunit_dummy_object::class, $cachevalue);
2662
        $this->assertGreaterThanOrEqual($backingstorereturntime, $cachevalue->propertytime);
2645
        $this->assertGreaterThanOrEqual($backingstorereturntime, $cachevalue->propertytime);
Línea 2663... Línea 2646...
2663
 
2646
 
2664
        $results = $cache->get_many(array('b'));
2647
        $results = $cache->get_many(['b']);
2665
        $this->assertInstanceOf(cache_phpunit_dummy_object::class, $results['b']);
2648
        $this->assertInstanceOf(cache_phpunit_dummy_object::class, $results['b']);
Línea 2666... Línea 2649...
2666
        $this->assertGreaterThanOrEqual($backingstorereturntimeb, $results['b']->propertytime);
2649
        $this->assertGreaterThanOrEqual($backingstorereturntimeb, $results['b']->propertytime);
2667
 
2650
 
Línea 2689... Línea 2672...
2689
        $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a'));
2672
        $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a'));
2690
        $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
2673
        $this->assertFalse($cache->phpunit_static_acceleration_get('b'));
2691
        $this->assertFalse($cache->phpunit_static_acceleration_get('c'));
2674
        $this->assertFalse($cache->phpunit_static_acceleration_get('c'));
Línea 2692... Línea 2675...
2692
 
2675
 
2693
        // Current keys in the array: d, e, a.
2676
        // Current keys in the array: d, e, a.
2694
        $this->assertEquals(array('c' => 'C'), $cache->get_many(array('c')));
2677
        $this->assertEquals(['c' => 'C'], $cache->get_many(['c']));
2695
        // Current keys in the array: e, a, c.
2678
        // Current keys in the array: e, a, c.
2696
        $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
2679
        $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
2697
        $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a'));
2680
        $this->assertEquals('A', $cache->phpunit_static_acceleration_get('a'));
2698
        $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
2681
        $this->assertEquals('C', $cache->phpunit_static_acceleration_get('c'));
Línea 2720... Línea 2703...
2720
        $this->assertEquals('B2', $cache->phpunit_static_acceleration_get('b'));
2703
        $this->assertEquals('B2', $cache->phpunit_static_acceleration_get('b'));
2721
        $this->assertFalse($cache->phpunit_static_acceleration_get('c'));
2704
        $this->assertFalse($cache->phpunit_static_acceleration_get('c'));
2722
        $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
2705
        $this->assertEquals('D', $cache->phpunit_static_acceleration_get('d'));
2723
        $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
2706
        $this->assertEquals('E', $cache->phpunit_static_acceleration_get('e'));
Línea 2724... Línea 2707...
2724
 
2707
 
2725
        $this->assertEquals(2, $cache->set_many(array('b' => 'B3', 'c' => 'C3')));
2708
        $this->assertEquals(2, $cache->set_many(['b' => 'B3', 'c' => 'C3']));
2726
        $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
2709
        $this->assertFalse($cache->phpunit_static_acceleration_get('a'));
2727
        $this->assertEquals('B3', $cache->phpunit_static_acceleration_get('b'));
2710
        $this->assertEquals('B3', $cache->phpunit_static_acceleration_get('b'));
2728
        $this->assertEquals('C3', $cache->phpunit_static_acceleration_get('c'));
2711
        $this->assertEquals('C3', $cache->phpunit_static_acceleration_get('c'));
2729
        $this->assertFalse($cache->phpunit_static_acceleration_get('d'));
2712
        $this->assertFalse($cache->phpunit_static_acceleration_get('d'));
Línea 2761... Línea 2744...
2761
        $returnedinstance1->name = 'b';
2744
        $returnedinstance1->name = 'b';
2762
        $this->assertEquals('b', $returnedinstance2->name);
2745
        $this->assertEquals('b', $returnedinstance2->name);
2763
    }
2746
    }
Línea 2764... Línea 2747...
2764
 
2747
 
2765
    public function test_identifiers_have_separate_caches(): void {
2748
    public function test_identifiers_have_separate_caches(): void {
2766
        $cachepg = cache::make('core', 'databasemeta', array('dbfamily' => 'pgsql'));
2749
        $cachepg = cache::make('core', 'databasemeta', ['dbfamily' => 'pgsql']);
2767
        $cachepg->set(1, 'here');
2750
        $cachepg->set(1, 'here');
2768
        $cachemy = cache::make('core', 'databasemeta', array('dbfamily' => 'mysql'));
2751
        $cachemy = cache::make('core', 'databasemeta', ['dbfamily' => 'mysql']);
2769
        $cachemy->set(2, 'there');
2752
        $cachemy->set(2, 'there');
2770
        $this->assertEquals('here', $cachepg->get(1));
2753
        $this->assertEquals('here', $cachepg->get(1));
2771
        $this->assertEquals('there', $cachemy->get(2));
2754
        $this->assertEquals('there', $cachemy->get(2));
2772
        $this->assertFalse($cachemy->get(1));
2755
        $this->assertFalse($cachemy->get(1));
Línea 2777... Línea 2760...
2777
        $this->resetAfterTest(true);
2760
        $this->resetAfterTest(true);
2778
        $CFG->perfdebug = 15;
2761
        $CFG->perfdebug = 15;
Línea 2779... Línea 2762...
2779
 
2762
 
2780
        $instance = cache_config_testing::instance();
2763
        $instance = cache_config_testing::instance();
2781
        $applicationid = 'phpunit/applicationperf';
2764
        $applicationid = 'phpunit/applicationperf';
2782
        $instance->phpunit_add_definition($applicationid, array(
2765
        $instance->phpunit_add_definition($applicationid, [
2783
            'mode' => cache_store::MODE_APPLICATION,
2766
            'mode' => store::MODE_APPLICATION,
2784
            'component' => 'phpunit',
2767
            'component' => 'phpunit',
2785
            'area' => 'applicationperf'
2768
            'area' => 'applicationperf',
2786
        ));
2769
        ]);
2787
        $sessionid = 'phpunit/sessionperf';
2770
        $sessionid = 'phpunit/sessionperf';
2788
        $instance->phpunit_add_definition($sessionid, array(
2771
        $instance->phpunit_add_definition($sessionid, [
2789
            'mode' => cache_store::MODE_SESSION,
2772
            'mode' => store::MODE_SESSION,
2790
            'component' => 'phpunit',
2773
            'component' => 'phpunit',
2791
            'area' => 'sessionperf'
2774
            'area' => 'sessionperf',
2792
        ));
2775
        ]);
2793
        $requestid = 'phpunit/requestperf';
2776
        $requestid = 'phpunit/requestperf';
2794
        $instance->phpunit_add_definition($requestid, array(
2777
        $instance->phpunit_add_definition($requestid, [
2795
            'mode' => cache_store::MODE_REQUEST,
2778
            'mode' => store::MODE_REQUEST,
2796
            'component' => 'phpunit',
2779
            'component' => 'phpunit',
2797
            'area' => 'requestperf'
2780
            'area' => 'requestperf',
Línea 2798... Línea 2781...
2798
        ));
2781
        ]);
2799
 
2782
 
2800
        $application = cache::make('phpunit', 'applicationperf');
2783
        $application = cache::make('phpunit', 'applicationperf');
Línea 2801... Línea 2784...
2801
        $session = cache::make('phpunit', 'sessionperf');
2784
        $session = cache::make('phpunit', 'sessionperf');
2802
        $request = cache::make('phpunit', 'requestperf');
2785
        $request = cache::make('phpunit', 'requestperf');
2803
 
2786
 
2804
        // Check that no stats are recorded for these definitions yet.
2787
        // Check that no stats are recorded for these definitions yet.
2805
        $stats = cache_helper::get_stats();
2788
        $stats = helper::get_stats();
Línea 2806... Línea 2789...
2806
        $this->assertArrayNotHasKey($applicationid, $stats);
2789
        $this->assertArrayNotHasKey($applicationid, $stats);
Línea 2816... Línea 2799...
2816
        $this->assertFalse($request->get('missMe'));
2799
        $this->assertFalse($request->get('missMe'));
2817
        $this->assertFalse($request->get('missMe'));
2800
        $this->assertFalse($request->get('missMe'));
2818
        $this->assertFalse($request->get('missMe'));
2801
        $this->assertFalse($request->get('missMe'));
2819
        $this->assertFalse($request->get('missMe'));
2802
        $this->assertFalse($request->get('missMe'));
Línea 2820... Línea 2803...
2820
 
2803
 
2821
        $endstats = cache_helper::get_stats();
2804
        $endstats = helper::get_stats();
2822
        $this->assertEquals(2, $endstats[$applicationid]['stores']['default_application']['misses']);
2805
        $this->assertEquals(2, $endstats[$applicationid]['stores']['default_application']['misses']);
2823
        $this->assertEquals(0, $endstats[$applicationid]['stores']['default_application']['hits']);
2806
        $this->assertEquals(0, $endstats[$applicationid]['stores']['default_application']['hits']);
2824
        $this->assertEquals(0, $endstats[$applicationid]['stores']['default_application']['sets']);
2807
        $this->assertEquals(0, $endstats[$applicationid]['stores']['default_application']['sets']);
2825
        $this->assertEquals(3, $endstats[$sessionid]['stores']['default_session']['misses']);
2808
        $this->assertEquals(3, $endstats[$sessionid]['stores']['default_session']['misses']);
2826
        $this->assertEquals(0, $endstats[$sessionid]['stores']['default_session']['hits']);
2809
        $this->assertEquals(0, $endstats[$sessionid]['stores']['default_session']['hits']);
2827
        $this->assertEquals(1, $endstats[$sessionid]['stores']['default_session']['sets']);
2810
        $this->assertEquals(1, $endstats[$sessionid]['stores']['default_session']['sets']);
2828
        $this->assertEquals(4, $endstats[$requestid]['stores']['default_request']['misses']);
2811
        $this->assertEquals(4, $endstats[$requestid]['stores']['default_request']['misses']);
2829
        $this->assertEquals(0, $endstats[$requestid]['stores']['default_request']['hits']);
2812
        $this->assertEquals(0, $endstats[$requestid]['stores']['default_request']['hits']);
Línea 2830... Línea 2813...
2830
        $this->assertEquals(0, $endstats[$requestid]['stores']['default_request']['sets']);
2813
        $this->assertEquals(0, $endstats[$requestid]['stores']['default_request']['sets']);
Línea 2831... Línea 2814...
2831
 
2814
 
2832
        $startstats = cache_helper::get_stats();
2815
        $startstats = helper::get_stats();
2833
 
2816
 
2834
        // Check that stores register sets.
2817
        // Check that stores register sets.
Línea 2840... Línea 2823...
2840
        $this->assertTrue($request->set('setMe1', 1));
2823
        $this->assertTrue($request->set('setMe1', 1));
2841
        $this->assertTrue($request->set('setMe2', 2));
2824
        $this->assertTrue($request->set('setMe2', 2));
2842
        $this->assertTrue($request->set('setMe3', 3));
2825
        $this->assertTrue($request->set('setMe3', 3));
2843
        $this->assertTrue($request->set('setMe4', 4));
2826
        $this->assertTrue($request->set('setMe4', 4));
Línea 2844... Línea 2827...
2844
 
2827
 
2845
        $endstats = cache_helper::get_stats();
2828
        $endstats = helper::get_stats();
2846
        $this->assertEquals(0, $endstats[$applicationid]['stores']['default_application']['misses'] -
2829
        $this->assertEquals(0, $endstats[$applicationid]['stores']['default_application']['misses'] -
2847
                             $startstats[$applicationid]['stores']['default_application']['misses']);
2830
                             $startstats[$applicationid]['stores']['default_application']['misses']);
2848
        $this->assertEquals(0, $endstats[$applicationid]['stores']['default_application']['hits'] -
2831
        $this->assertEquals(0, $endstats[$applicationid]['stores']['default_application']['hits'] -
2849
                             $startstats[$applicationid]['stores']['default_application']['hits']);
2832
                             $startstats[$applicationid]['stores']['default_application']['hits']);
Línea 2860... Línea 2843...
2860
        $this->assertEquals(0, $endstats[$requestid]['stores']['default_request']['hits'] -
2843
        $this->assertEquals(0, $endstats[$requestid]['stores']['default_request']['hits'] -
2861
                             $startstats[$requestid]['stores']['default_request']['hits']);
2844
                             $startstats[$requestid]['stores']['default_request']['hits']);
2862
        $this->assertEquals(4, $endstats[$requestid]['stores']['default_request']['sets'] -
2845
        $this->assertEquals(4, $endstats[$requestid]['stores']['default_request']['sets'] -
2863
                             $startstats[$requestid]['stores']['default_request']['sets']);
2846
                             $startstats[$requestid]['stores']['default_request']['sets']);
Línea 2864... Línea 2847...
2864
 
2847
 
Línea 2865... Línea 2848...
2865
        $startstats = cache_helper::get_stats();
2848
        $startstats = helper::get_stats();
2866
 
2849
 
2867
        // Check that stores register hits.
2850
        // Check that stores register hits.
2868
        $this->assertEquals($application->get('setMe1'), 1);
2851
        $this->assertEquals($application->get('setMe1'), 1);
Línea 2873... Línea 2856...
2873
        $this->assertEquals($request->get('setMe1'), 1);
2856
        $this->assertEquals($request->get('setMe1'), 1);
2874
        $this->assertEquals($request->get('setMe2'), 2);
2857
        $this->assertEquals($request->get('setMe2'), 2);
2875
        $this->assertEquals($request->get('setMe3'), 3);
2858
        $this->assertEquals($request->get('setMe3'), 3);
2876
        $this->assertEquals($request->get('setMe4'), 4);
2859
        $this->assertEquals($request->get('setMe4'), 4);
Línea 2877... Línea 2860...
2877
 
2860
 
2878
        $endstats = cache_helper::get_stats();
2861
        $endstats = helper::get_stats();
2879
        $this->assertEquals(0, $endstats[$applicationid]['stores']['default_application']['misses'] -
2862
        $this->assertEquals(0, $endstats[$applicationid]['stores']['default_application']['misses'] -
2880
                             $startstats[$applicationid]['stores']['default_application']['misses']);
2863
                             $startstats[$applicationid]['stores']['default_application']['misses']);
2881
        $this->assertEquals(2, $endstats[$applicationid]['stores']['default_application']['hits'] -
2864
        $this->assertEquals(2, $endstats[$applicationid]['stores']['default_application']['hits'] -
2882
                             $startstats[$applicationid]['stores']['default_application']['hits']);
2865
                             $startstats[$applicationid]['stores']['default_application']['hits']);
Línea 2893... Línea 2876...
2893
        $this->assertEquals(4, $endstats[$requestid]['stores']['default_request']['hits'] -
2876
        $this->assertEquals(4, $endstats[$requestid]['stores']['default_request']['hits'] -
2894
                             $startstats[$requestid]['stores']['default_request']['hits']);
2877
                             $startstats[$requestid]['stores']['default_request']['hits']);
2895
        $this->assertEquals(0, $endstats[$requestid]['stores']['default_request']['sets'] -
2878
        $this->assertEquals(0, $endstats[$requestid]['stores']['default_request']['sets'] -
2896
                             $startstats[$requestid]['stores']['default_request']['sets']);
2879
                             $startstats[$requestid]['stores']['default_request']['sets']);
Línea 2897... Línea 2880...
2897
 
2880
 
Línea 2898... Línea 2881...
2898
        $startstats = cache_helper::get_stats();
2881
        $startstats = helper::get_stats();
2899
 
2882
 
2900
        // Check that stores register through get_many.
2883
        // Check that stores register through get_many.
2901
        $application->get_many(array('setMe1', 'setMe2'));
2884
        $application->get_many(['setMe1', 'setMe2']);
Línea 2902... Línea 2885...
2902
        $session->get_many(array('setMe1', 'setMe2', 'setMe3'));
2885
        $session->get_many(['setMe1', 'setMe2', 'setMe3']);
2903
        $request->get_many(array('setMe1', 'setMe2', 'setMe3', 'setMe4'));
2886
        $request->get_many(['setMe1', 'setMe2', 'setMe3', 'setMe4']);
2904
 
2887
 
2905
        $endstats = cache_helper::get_stats();
2888
        $endstats = helper::get_stats();
2906
        $this->assertEquals(0, $endstats[$applicationid]['stores']['default_application']['misses'] -
2889
        $this->assertEquals(0, $endstats[$applicationid]['stores']['default_application']['misses'] -
2907
                             $startstats[$applicationid]['stores']['default_application']['misses']);
2890
                             $startstats[$applicationid]['stores']['default_application']['misses']);
Línea 2920... Línea 2903...
2920
        $this->assertEquals(4, $endstats[$requestid]['stores']['default_request']['hits'] -
2903
        $this->assertEquals(4, $endstats[$requestid]['stores']['default_request']['hits'] -
2921
                             $startstats[$requestid]['stores']['default_request']['hits']);
2904
                             $startstats[$requestid]['stores']['default_request']['hits']);
2922
        $this->assertEquals(0, $endstats[$requestid]['stores']['default_request']['sets'] -
2905
        $this->assertEquals(0, $endstats[$requestid]['stores']['default_request']['sets'] -
2923
                             $startstats[$requestid]['stores']['default_request']['sets']);
2906
                             $startstats[$requestid]['stores']['default_request']['sets']);
Línea 2924... Línea 2907...
2924
 
2907
 
Línea 2925... Línea 2908...
2925
        $startstats = cache_helper::get_stats();
2908
        $startstats = helper::get_stats();
2926
 
2909
 
2927
        // Check that stores register through set_many.
2910
        // Check that stores register through set_many.
2928
        $this->assertEquals(2, $application->set_many(['setMe1' => 1, 'setMe2' => 2]));
2911
        $this->assertEquals(2, $application->set_many(['setMe1' => 1, 'setMe2' => 2]));
Línea 2929... Línea 2912...
2929
        $this->assertEquals(3, $session->set_many(['setMe1' => 1, 'setMe2' => 2, 'setMe3' => 3]));
2912
        $this->assertEquals(3, $session->set_many(['setMe1' => 1, 'setMe2' => 2, 'setMe3' => 3]));
Línea 2930... Línea 2913...
2930
        $this->assertEquals(4, $request->set_many(['setMe1' => 1, 'setMe2' => 2, 'setMe3' => 3, 'setMe4' => 4]));
2913
        $this->assertEquals(4, $request->set_many(['setMe1' => 1, 'setMe2' => 2, 'setMe3' => 3, 'setMe4' => 4]));
2931
 
2914
 
2932
        $endstats = cache_helper::get_stats();
2915
        $endstats = helper::get_stats();
2933
 
2916
 
Línea 2954... Línea 2937...
2954
    /**
2937
    /**
2955
     * Data provider for static acceleration performance tests.
2938
     * Data provider for static acceleration performance tests.
2956
     *
2939
     *
2957
     * @return array
2940
     * @return array
2958
     */
2941
     */
2959
    public function static_acceleration_performance_provider(): array {
2942
    public static function static_acceleration_performance_provider(): array {
2960
        // Note: These are the delta values, not the absolute values.
2943
        // Note: These are the delta values, not the absolute values.
2961
        // Also note that the set will actually store the valuein the static cache immediately.
2944
        // Also note that the set will actually store the valuein the static cache immediately.
2962
        $validfirst = [
2945
        $validfirst = [
2963
            'default_application' => [
2946
            'default_application' => [
2964
                'hits' => 1,
2947
                'hits' => 1,
2965
                'misses' => 0,
2948
                'misses' => 0,
2966
            ],
2949
            ],
2967
            cache_store::STATIC_ACCEL => [
2950
            store::STATIC_ACCEL => [
2968
                'hits' => 0,
2951
                'hits' => 0,
2969
                'misses' => 1,
2952
                'misses' => 1,
2970
            ],
2953
            ],
2971
        ];
2954
        ];
Línea 2972... Línea 2955...
2972
 
2955
 
2973
        $validsecond = [
2956
        $validsecond = [
2974
            'default_application' => [
2957
            'default_application' => [
2975
                'hits' => 0,
2958
                'hits' => 0,
2976
                'misses' => 0,
2959
                'misses' => 0,
2977
            ],
2960
            ],
2978
            cache_store::STATIC_ACCEL => [
2961
            store::STATIC_ACCEL => [
2979
                'hits' => 1,
2962
                'hits' => 1,
2980
                'misses' => 0,
2963
                'misses' => 0,
2981
            ],
2964
            ],
Línea 2982... Línea 2965...
2982
        ];
2965
        ];
2983
 
2966
 
2984
        $invalidfirst = [
2967
        $invalidfirst = [
2985
            'default_application' => [
2968
            'default_application' => [
2986
                'hits' => 0,
2969
                'hits' => 0,
2987
                'misses' => 1,
2970
                'misses' => 1,
2988
            ],
2971
            ],
2989
            cache_store::STATIC_ACCEL => [
2972
            store::STATIC_ACCEL => [
2990
                'hits' => 0,
2973
                'hits' => 0,
2991
                'misses' => 1,
2974
                'misses' => 1,
2992
            ],
2975
            ],
2993
        ];
2976
        ];
2994
        $invalidsecond = [
2977
        $invalidsecond = [
2995
            'default_application' => [
2978
            'default_application' => [
2996
                'hits' => 0,
2979
                'hits' => 0,
2997
                'misses' => 1,
2980
                'misses' => 1,
2998
            ],
2981
            ],
2999
            cache_store::STATIC_ACCEL => [
2982
            store::STATIC_ACCEL => [
3000
                'hits' => 0,
2983
                'hits' => 0,
3001
                'misses' => 1,
2984
                'misses' => 1,
-
 
2985
            ],
Línea 3002... Línea 2986...
3002
            ],
2986
        ];
3003
        ];;
2987
        ;
3004
 
2988
 
3005
        return [
2989
        return [
Línea 3049... Línea 3033...
3049
        $this->resetAfterTest(true);
3033
        $this->resetAfterTest(true);
3050
        $CFG->perfdebug = 15;
3034
        $CFG->perfdebug = 15;
Línea 3051... Línea 3035...
3051
 
3035
 
3052
        $instance = cache_config_testing::instance();
3036
        $instance = cache_config_testing::instance();
3053
        $instance->phpunit_add_definition('phpunit/accelerated', [
3037
        $instance->phpunit_add_definition('phpunit/accelerated', [
3054
            'mode' => cache_store::MODE_APPLICATION,
3038
            'mode' => store::MODE_APPLICATION,
3055
            'component' => 'phpunit',
3039
            'component' => 'phpunit',
3056
            'area' => 'accelerated',
3040
            'area' => 'accelerated',
3057
            'staticacceleration' => true,
3041
            'staticacceleration' => true,
3058
            'staticaccelerationsize' => 1,
3042
            'staticaccelerationsize' => 1,
Línea 3061... Línea 3045...
3061
        $cache = cache::make('phpunit', 'accelerated');
3045
        $cache = cache::make('phpunit', 'accelerated');
3062
        $this->assertInstanceOf(cache_phpunit_application::class, $cache);
3046
        $this->assertInstanceOf(cache_phpunit_application::class, $cache);
Línea 3063... Línea 3047...
3063
 
3047
 
Línea 3064... Línea 3048...
3064
        $this->assertTrue($cache->set('value', $value));
3048
        $this->assertTrue($cache->set('value', $value));
3065
 
3049
 
3066
        $checkstats = function(
3050
        $checkstats = function (
3067
            array $start,
3051
            array $start,
3068
            array $expectedstats,
3052
            array $expectedstats,
3069
        ): array {
3053
        ): array {
Línea 3070... Línea 3054...
3070
            $applicationid = 'phpunit/accelerated';
3054
            $applicationid = 'phpunit/accelerated';
3071
            $endstats = cache_helper::get_stats();
3055
            $endstats = helper::get_stats();
Línea 3072... Línea 3056...
3072
 
3056
 
Línea 3088... Línea 3072...
3088
 
3072
 
3089
            return $endstats;
3073
            return $endstats;
Línea 3090... Línea 3074...
3090
        };
3074
        };
3091
 
3075
 
3092
        // Reset the cache factory so that we can get the stats from a fresh instance.
3076
        // Reset the cache factory so that we can get the stats from a fresh instance.
3093
        $factory = cache_factory::instance();
3077
        $factory = factory::instance();
Línea 3094... Línea 3078...
3094
        $factory->reset_cache_instances();
3078
        $factory->reset_cache_instances();
3095
        $cache = cache::make('phpunit', 'accelerated');
3079
        $cache = cache::make('phpunit', 'accelerated');
Línea 3096... Línea 3080...
3096
 
3080
 
3097
        // Get the initial stats.
3081
        // Get the initial stats.
3098
        $startstats = cache_helper::get_stats();
3082
        $startstats = helper::get_stats();
Línea 3113... Línea 3097...
3113
        $CFG->perfdebug = 15;
3097
        $CFG->perfdebug = 15;
Línea 3114... Línea 3098...
3114
 
3098
 
3115
        // Create cache store with static acceleration.
3099
        // Create cache store with static acceleration.
3116
        $instance = cache_config_testing::instance();
3100
        $instance = cache_config_testing::instance();
3117
        $applicationid = 'phpunit/applicationperf';
3101
        $applicationid = 'phpunit/applicationperf';
3118
        $instance->phpunit_add_definition($applicationid, array(
3102
        $instance->phpunit_add_definition($applicationid, [
3119
            'mode' => cache_store::MODE_APPLICATION,
3103
            'mode' => store::MODE_APPLICATION,
3120
            'component' => 'phpunit',
3104
            'component' => 'phpunit',
3121
            'area' => 'applicationperf',
3105
            'area' => 'applicationperf',
3122
            'simplekeys' => true,
3106
            'simplekeys' => true,
3123
            'staticacceleration' => true,
3107
            'staticacceleration' => true,
3124
            'staticaccelerationsize' => 3
3108
            'staticaccelerationsize' => 3,
Línea 3125... Línea 3109...
3125
        ));
3109
        ]);
Línea 3126... Línea 3110...
3126
 
3110
 
3127
        $application = cache::make('phpunit', 'applicationperf');
3111
        $application = cache::make('phpunit', 'applicationperf');
3128
 
3112
 
3129
        // Check that stores register sets.
3113
        // Check that stores register sets.
3130
        $this->assertTrue($application->set('setMe1', 1));
3114
        $this->assertTrue($application->set('setMe1', 1));
3131
        $this->assertTrue($application->set('setMe2', 0));
3115
        $this->assertTrue($application->set('setMe2', 0));
3132
        $this->assertTrue($application->set('setMe3', array()));
3116
        $this->assertTrue($application->set('setMe3', []));
Línea 3133... Línea 3117...
3133
        $this->assertTrue($application->get('setMe1') !== false);
3117
        $this->assertTrue($application->get('setMe1') !== false);
3134
        $this->assertTrue($application->get('setMe2') !== false);
3118
        $this->assertTrue($application->get('setMe2') !== false);
3135
        $this->assertTrue($application->get('setMe3') !== false);
3119
        $this->assertTrue($application->get('setMe3') !== false);
3136
 
3120
 
3137
        // Check that the static acceleration worked, even on empty arrays and the number 0.
3121
        // Check that the static acceleration worked, even on empty arrays and the number 0.
Línea 3138... Línea 3122...
3138
        $endstats = cache_helper::get_stats();
3122
        $endstats = helper::get_stats();
Línea 3145... Línea 3129...
3145
        $this->resetAfterTest(true);
3129
        $this->resetAfterTest(true);
3146
        $CFG->perfdebug = 7;
3130
        $CFG->perfdebug = 7;
Línea 3147... Línea 3131...
3147
 
3131
 
3148
        $instance = cache_config_testing::instance();
3132
        $instance = cache_config_testing::instance();
3149
        $applicationid = 'phpunit/applicationperfoff';
3133
        $applicationid = 'phpunit/applicationperfoff';
3150
        $instance->phpunit_add_definition($applicationid, array(
3134
        $instance->phpunit_add_definition($applicationid, [
3151
            'mode' => cache_store::MODE_APPLICATION,
3135
            'mode' => store::MODE_APPLICATION,
3152
            'component' => 'phpunit',
3136
            'component' => 'phpunit',
3153
            'area' => 'applicationperfoff'
3137
            'area' => 'applicationperfoff',
3154
        ));
3138
        ]);
3155
        $sessionid = 'phpunit/sessionperfoff';
3139
        $sessionid = 'phpunit/sessionperfoff';
3156
        $instance->phpunit_add_definition($sessionid, array(
3140
        $instance->phpunit_add_definition($sessionid, [
3157
            'mode' => cache_store::MODE_SESSION,
3141
            'mode' => store::MODE_SESSION,
3158
            'component' => 'phpunit',
3142
            'component' => 'phpunit',
3159
            'area' => 'sessionperfoff'
3143
            'area' => 'sessionperfoff',
3160
        ));
3144
        ]);
3161
        $requestid = 'phpunit/requestperfoff';
3145
        $requestid = 'phpunit/requestperfoff';
3162
        $instance->phpunit_add_definition($requestid, array(
3146
        $instance->phpunit_add_definition($requestid, [
3163
            'mode' => cache_store::MODE_REQUEST,
3147
            'mode' => store::MODE_REQUEST,
3164
            'component' => 'phpunit',
3148
            'component' => 'phpunit',
3165
            'area' => 'requestperfoff'
3149
            'area' => 'requestperfoff',
Línea 3166... Línea 3150...
3166
        ));
3150
        ]);
3167
 
3151
 
3168
        $application = cache::make('phpunit', 'applicationperfoff');
3152
        $application = cache::make('phpunit', 'applicationperfoff');
Línea 3169... Línea 3153...
3169
        $session = cache::make('phpunit', 'sessionperfoff');
3153
        $session = cache::make('phpunit', 'sessionperfoff');
3170
        $request = cache::make('phpunit', 'requestperfoff');
3154
        $request = cache::make('phpunit', 'requestperfoff');
3171
 
3155
 
3172
        // Check that no stats are recorded for these definitions yet.
3156
        // Check that no stats are recorded for these definitions yet.
3173
        $stats = cache_helper::get_stats();
3157
        $stats = helper::get_stats();
Línea 3174... Línea 3158...
3174
        $this->assertArrayNotHasKey($applicationid, $stats);
3158
        $this->assertArrayNotHasKey($applicationid, $stats);
Línea 3185... Línea 3169...
3185
        $this->assertFalse($request->get('missMe'));
3169
        $this->assertFalse($request->get('missMe'));
3186
        $this->assertTrue($request->set('setMe', 4));
3170
        $this->assertTrue($request->set('setMe', 4));
3187
        $this->assertEquals(4, $request->get('setMe'));
3171
        $this->assertEquals(4, $request->get('setMe'));
Línea 3188... Línea 3172...
3188
 
3172
 
3189
        // Check that no stats are being recorded for these definitions.
3173
        // Check that no stats are being recorded for these definitions.
3190
        $endstats = cache_helper::get_stats();
3174
        $endstats = helper::get_stats();
3191
        $this->assertArrayNotHasKey($applicationid, $endstats);
3175
        $this->assertArrayNotHasKey($applicationid, $endstats);
3192
        $this->assertArrayNotHasKey($sessionid, $endstats);
3176
        $this->assertArrayNotHasKey($sessionid, $endstats);
3193
        $this->assertArrayNotHasKey($requestid, $endstats);
3177
        $this->assertArrayNotHasKey($requestid, $endstats);
Línea 3201... Línea 3185...
3201
     * A new request is started a short time later.
3185
     * A new request is started a short time later.
3202
     * The cache should be filled.
3186
     * The cache should be filled.
3203
     */
3187
     */
3204
    public function test_session_event_purge_same_second(): void {
3188
    public function test_session_event_purge_same_second(): void {
3205
        $instance = cache_config_testing::instance();
3189
        $instance = cache_config_testing::instance();
3206
        $instance->phpunit_add_definition('phpunit/eventpurgetest', array(
3190
        $instance->phpunit_add_definition('phpunit/eventpurgetest', [
3207
            'mode' => cache_store::MODE_SESSION,
3191
            'mode' => store::MODE_SESSION,
3208
            'component' => 'phpunit',
3192
            'component' => 'phpunit',
3209
            'area' => 'eventpurgetest',
3193
            'area' => 'eventpurgetest',
3210
            'invalidationevents' => array(
3194
            'invalidationevents' => [
3211
                'crazyevent',
3195
                'crazyevent',
3212
            )
3196
            ],
3213
        ));
3197
        ]);
Línea 3214... Línea 3198...
3214
 
3198
 
3215
        // Create the cache, set a value, and immediately purge it by event.
3199
        // Create the cache, set a value, and immediately purge it by event.
3216
        $cache = cache::make('phpunit', 'eventpurgetest');
3200
        $cache = cache::make('phpunit', 'eventpurgetest');
3217
        $cache->set('testkey1', 'test data 1');
3201
        $cache->set('testkey1', 'test data 1');
3218
        $this->assertEquals('test data 1', $cache->get('testkey1'));
3202
        $this->assertEquals('test data 1', $cache->get('testkey1'));
3219
        cache_helper::purge_by_event('crazyevent');
3203
        helper::purge_by_event('crazyevent');
Línea 3220... Línea 3204...
3220
        $this->assertFalse($cache->get('testkey1'));
3204
        $this->assertFalse($cache->get('testkey1'));
3221
 
3205
 
3222
        // Set up the cache again in the same request and add a new value back in.
3206
        // Set up the cache again in the same request and add a new value back in.
3223
        $factory = cache_factory::instance();
3207
        $factory = factory::instance();
3224
        $factory->reset_cache_instances();
3208
        $factory->reset_cache_instances();
3225
        $cache = cache::make('phpunit', 'eventpurgetest');
3209
        $cache = cache::make('phpunit', 'eventpurgetest');
Línea 3226... Línea 3210...
3226
        $cache->set('testkey1', 'test data 2');
3210
        $cache->set('testkey1', 'test data 2');
3227
        $this->assertEquals('test data 2', $cache->get('testkey1'));
3211
        $this->assertEquals('test data 2', $cache->get('testkey1'));
3228
 
3212
 
3229
        // Trick the cache into thinking that this is a new request.
3213
        // Trick the cache into thinking that this is a new request.
Línea 3230... Línea 3214...
3230
        cache_phpunit_cache::simulate_new_request();
3214
        cache_phpunit_cache::simulate_new_request();
3231
        $factory = cache_factory::instance();
3215
        $factory = factory::instance();
3232
        $factory->reset_cache_instances();
3216
        $factory->reset_cache_instances();
Línea 3244... Línea 3228...
3244
    public function test_session_distinct_storage_key(): void {
3228
    public function test_session_distinct_storage_key(): void {
3245
        $this->resetAfterTest();
3229
        $this->resetAfterTest();
Línea 3246... Línea 3230...
3246
 
3230
 
3247
        // Prepare a dummy session cache configuration.
3231
        // Prepare a dummy session cache configuration.
3248
        $config = cache_config_testing::instance();
3232
        $config = cache_config_testing::instance();
3249
        $config->phpunit_add_definition('phpunit/test_session_distinct_storage_key', array(
3233
        $config->phpunit_add_definition('phpunit/test_session_distinct_storage_key', [
3250
            'mode' => cache_store::MODE_SESSION,
3234
            'mode' => store::MODE_SESSION,
3251
            'component' => 'phpunit',
3235
            'component' => 'phpunit',
3252
            'area' => 'test_session_distinct_storage_key'
3236
            'area' => 'test_session_distinct_storage_key',
Línea 3253... Línea 3237...
3253
        ));
3237
        ]);
3254
 
3238
 
3255
        // First anonymous user's session cache.
3239
        // First anonymous user's session cache.
3256
        cache_phpunit_session::phpunit_mockup_session_id('foo');
3240
        cache_phpunit_session::phpunit_mockup_session_id('foo');
Línea 3257... Línea 3241...
3257
        $this->setUser(0);
3241
        $this->setUser(0);
3258
        $cache1 = cache::make('phpunit', 'test_session_distinct_storage_key');
3242
        $cache1 = cache::make('phpunit', 'test_session_distinct_storage_key');
Línea 3259... Línea 3243...
3259
 
3243
 
3260
        // Reset cache instances to emulate a new request.
3244
        // Reset cache instances to emulate a new request.
3261
        cache_factory::instance()->reset_cache_instances();
3245
        factory::instance()->reset_cache_instances();
3262
 
3246
 
Línea 3263... Línea 3247...
3263
        // Another anonymous user's session cache.
3247
        // Another anonymous user's session cache.
Línea 3264... Línea 3248...
3264
        cache_phpunit_session::phpunit_mockup_session_id('bar');
3248
        cache_phpunit_session::phpunit_mockup_session_id('bar');
3265
        $this->setUser(0);
3249
        $this->setUser(0);
3266
        $cache2 = cache::make('phpunit', 'test_session_distinct_storage_key');
3250
        $cache2 = cache::make('phpunit', 'test_session_distinct_storage_key');
3267
 
3251
 
Línea 3268... Línea 3252...
3268
        cache_factory::instance()->reset_cache_instances();
3252
        factory::instance()->reset_cache_instances();
Línea 3269... Línea 3253...
3269
 
3253
 
3270
        // Guest user's session cache.
3254
        // Guest user's session cache.
3271
        cache_phpunit_session::phpunit_mockup_session_id('baz');
3255
        cache_phpunit_session::phpunit_mockup_session_id('baz');
3272
        $this->setGuestUser();
3256
        $this->setGuestUser();