Proyectos de Subversion Moodle

Rev

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

Rev 1 Rev 1441
Línea 18... Línea 18...
18
 * Support library for the cache PHPUnit tests.
18
 * Support library for the cache PHPUnit tests.
19
 *
19
 *
20
 * This file is part of Moodle's cache API, affectionately called MUC.
20
 * This file is part of Moodle's cache API, affectionately called MUC.
21
 * It contains the components that are requried in order to use caching.
21
 * It contains the components that are requried in order to use caching.
22
 *
22
 *
23
 * @package    core
23
 * @package    core_cache
24
 * @category   cache
24
 * @category   cache
25
 * @copyright  2012 Sam Hemelryk
25
 * @copyright  2012 Sam Hemelryk
26
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 */
27
 */
Línea 28... Línea 28...
28
 
28
 
Línea 29... Línea 29...
29
defined('MOODLE_INTERNAL') || die();
29
defined('MOODLE_INTERNAL') || die();
Línea 30... Línea 30...
30
 
30
 
31
require_once($CFG->dirroot.'/cache/locallib.php');
31
use core_cache\store;
32
 
32
 
33
/**
33
/**
Línea 39... Línea 39...
39
 * @since 2.9
39
 * @since 2.9
40
 * @copyright 2012 Sam Hemelryk
40
 * @copyright 2012 Sam Hemelryk
41
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42
 */
42
 */
43
class cache_config_testing extends cache_config_writer {
43
class cache_config_testing extends cache_config_writer {
44
 
-
 
45
    /**
44
    /**
46
     * Creates the default configuration and saves it.
45
     * Creates the default configuration and saves it.
47
     *
46
     *
48
     * This function calls config_save, however it is safe to continue using it afterwards as this function should only ever
47
     * This function calls config_save, however it is safe to continue using it afterwards as this function should only ever
49
     * be called when there is no configuration file already.
48
     * be called when there is no configuration file already.
Línea 55... Línea 54...
55
    public static function create_default_configuration($forcesave = false) {
54
    public static function create_default_configuration($forcesave = false) {
56
        global $CFG;
55
        global $CFG;
57
        // HACK ALERT.
56
        // HACK ALERT.
58
        // We probably need to come up with a better way to create the default stores, or at least ensure 100% that the
57
        // We probably need to come up with a better way to create the default stores, or at least ensure 100% that the
59
        // default store plugins are protected from deletion.
58
        // default store plugins are protected from deletion.
60
        $writer = new self;
59
        $writer = new self();
61
        $writer->configstores = self::get_default_stores();
60
        $writer->configstores = self::get_default_stores();
62
        $writer->configdefinitions = self::locate_definitions();
61
        $writer->configdefinitions = self::locate_definitions();
63
        $defaultapplication = 'default_application';
62
        $defaultapplication = 'default_application';
Línea 64... Línea 63...
64
 
63
 
65
        $appdefine = defined('TEST_CACHE_USING_APPLICATION_STORE') ? TEST_CACHE_USING_APPLICATION_STORE : false;
64
        $appdefine = defined('TEST_CACHE_USING_APPLICATION_STORE') ? TEST_CACHE_USING_APPLICATION_STORE : false;
66
        if ($appdefine !== false && preg_match('/^[a-zA-Z][a-zA-Z0-9_]+$/', $appdefine)) {
65
        if ($appdefine !== false && preg_match('/^[a-zA-Z][a-zA-Z0-9_]+$/', $appdefine)) {
67
            $expectedstore = $appdefine;
66
            $expectedstore = $appdefine;
68
            $file = $CFG->dirroot.'/cache/stores/'.$appdefine.'/lib.php';
67
            $file = $CFG->dirroot . '/cache/stores/' . $appdefine . '/lib.php';
69
            $class = 'cachestore_'.$appdefine;
68
            $class = 'cachestore_' . $appdefine;
70
            if (file_exists($file)) {
69
            if (file_exists($file)) {
71
                require_once($file);
70
                require_once($file);
72
            }
71
            }
73
            if (class_exists($class) && $class::ready_to_be_used_for_testing()) {
72
            if (class_exists($class) && $class::ready_to_be_used_for_testing()) {
74
                /* @var cache_store $class */
73
                /* @var store $class */
75
                $writer->configstores['test_application'] = array(
74
                $writer->configstores['test_application'] = [
76
                    'name' => 'test_application',
75
                    'name' => 'test_application',
77
                    'plugin' => $expectedstore,
76
                    'plugin' => $expectedstore,
78
                    'modes' => $class::get_supported_modes(),
77
                    'modes' => $class::get_supported_modes(),
79
                    'features' => $class::get_supported_features(),
78
                    'features' => $class::get_supported_features(),
80
                    'configuration' => $class::unit_test_configuration()
79
                    'configuration' => $class::unit_test_configuration(),
Línea 81... Línea 80...
81
                );
80
                ];
82
 
81
 
83
                $defaultapplication = 'test_application';
82
                $defaultapplication = 'test_application';
Línea 84... Línea 83...
84
            }
83
            }
85
        }
84
        }
86
 
85
 
87
        $writer->configmodemappings = array(
86
        $writer->configmodemappings = [
88
            array(
87
            [
89
                'mode' => cache_store::MODE_APPLICATION,
88
                'mode' => store::MODE_APPLICATION,
90
                'store' => $defaultapplication,
89
                'store' => $defaultapplication,
91
                'sort' => -1
90
                'sort' => -1,
92
            ),
91
            ],
93
            array(
92
            [
94
                'mode' => cache_store::MODE_SESSION,
93
                'mode' => store::MODE_SESSION,
95
                'store' => 'default_session',
94
                'store' => 'default_session',
96
                'sort' => -1
95
                'sort' => -1,
97
            ),
96
            ],
98
            array(
97
            [
99
                'mode' => cache_store::MODE_REQUEST,
98
                'mode' => store::MODE_REQUEST,
100
                'store' => 'default_request',
99
                'store' => 'default_request',
101
                'sort' => -1
100
                'sort' => -1,
102
            )
101
            ],
103
        );
102
        ];
104
        $writer->configlocks = array(
103
        $writer->configlocks = [
105
            'default_file_lock' => array(
104
            'default_file_lock' => [
106
                'name' => 'cachelock_file_default',
105
                'name' => 'cachelock_file_default',
107
                'type' => 'cachelock_file',
106
                'type' => 'cachelock_file',
108
                'dir' => 'filelocks',
107
                'dir' => 'filelocks',
Línea 109... Línea 108...
109
                'default' => true
108
                'default' => true,
110
            )
109
            ],
111
        );
110
        ];
112
 
111
 
Línea 133... Línea 132...
133
     * @return string The absolute path
132
     * @return string The absolute path
134
     */
133
     */
135
    protected static function get_config_file_path() {
134
    protected static function get_config_file_path() {
136
        global $CFG;
135
        global $CFG;
137
        // We always use this path.
136
        // We always use this path.
138
        $configpath = $CFG->dataroot.'/muc/config.php';
137
        $configpath = $CFG->dataroot . '/muc/config.php';
Línea 139... Línea 138...
139
 
138
 
140
        if (!empty($CFG->altcacheconfigpath)) {
-
 
141
 
139
        if (!empty($CFG->altcacheconfigpath)) {
142
            // No need to check we are within a test here, this is the cache config class that gets used
140
            // No need to check we are within a test here, this is the cache config class that gets used
143
            // only when one of those is true.
141
            // only when one of those is true.
144
            if  (!defined('TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH') || !TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH) {
142
            if (!defined('TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH') || !TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH) {
145
                // TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH has not being defined or is false, we want to use the default.
143
                // TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH has not being defined or is false, we want to use the default.
146
                return $configpath;
144
                return $configpath;
Línea 147... Línea 145...
147
            }
145
            }
148
 
146
 
149
            $path = $CFG->altcacheconfigpath;
147
            $path = $CFG->altcacheconfigpath;
150
            if (is_dir($path) && is_writable($path)) {
148
            if (is_dir($path) && is_writable($path)) {
151
                // Its a writable directory, thats fine. Convert it to a file.
149
                // Its a writable directory, thats fine. Convert it to a file.
152
                $path = $CFG->altcacheconfigpath.'/cacheconfig.php';
150
                $path = $CFG->altcacheconfigpath . '/cacheconfig.php';
153
            }
151
            }
154
            if (is_readable($path)) {
152
            if (is_readable($path)) {
155
                $directory = dirname($configpath);
153
                $directory = dirname($configpath);
Línea 178... Línea 176...
178
     *    however set this to false if you only want it to add the definition and not the mapping.
176
     *    however set this to false if you only want it to add the definition and not the mapping.
179
     */
177
     */
180
    public function phpunit_add_definition($area, array $properties, $addmapping = true) {
178
    public function phpunit_add_definition($area, array $properties, $addmapping = true) {
181
        if (!array_key_exists('overrideclass', $properties)) {
179
        if (!array_key_exists('overrideclass', $properties)) {
182
            switch ($properties['mode']) {
180
            switch ($properties['mode']) {
183
                case cache_store::MODE_APPLICATION:
181
                case store::MODE_APPLICATION:
184
                    $properties['overrideclass'] = 'cache_phpunit_application';
182
                    $properties['overrideclass'] = 'cache_phpunit_application';
185
                    break;
183
                    break;
186
                case cache_store::MODE_SESSION:
184
                case store::MODE_SESSION:
187
                    $properties['overrideclass'] = 'cache_phpunit_session';
185
                    $properties['overrideclass'] = 'cache_phpunit_session';
188
                    break;
186
                    break;
189
                case cache_store::MODE_REQUEST:
187
                case store::MODE_REQUEST:
190
                    $properties['overrideclass'] = 'cache_phpunit_request';
188
                    $properties['overrideclass'] = 'cache_phpunit_request';
191
                    break;
189
                    break;
192
            }
190
            }
193
        }
191
        }
194
        $this->configdefinitions[$area] = $properties;
192
        $this->configdefinitions[$area] = $properties;
195
        if ($addmapping) {
193
        if ($addmapping) {
196
            switch ($properties['mode']) {
194
            switch ($properties['mode']) {
197
                case cache_store::MODE_APPLICATION:
195
                case store::MODE_APPLICATION:
198
                    $this->phpunit_add_definition_mapping($area, 'default_application', 0);
196
                    $this->phpunit_add_definition_mapping($area, 'default_application', 0);
199
                    break;
197
                    break;
200
                case cache_store::MODE_SESSION:
198
                case store::MODE_SESSION:
201
                    $this->phpunit_add_definition_mapping($area, 'default_session', 0);
199
                    $this->phpunit_add_definition_mapping($area, 'default_session', 0);
202
                    break;
200
                    break;
203
                case cache_store::MODE_REQUEST:
201
                case store::MODE_REQUEST:
204
                    $this->phpunit_add_definition_mapping($area, 'default_request', 0);
202
                    $this->phpunit_add_definition_mapping($area, 'default_request', 0);
205
                    break;
203
                    break;
206
            }
204
            }
207
        }
205
        }
208
    }
206
    }
Línea 217... Línea 215...
217
 
215
 
218
    /**
216
    /**
219
     * Removes the configured stores so that there are none available.
217
     * Removes the configured stores so that there are none available.
220
     */
218
     */
221
    public function phpunit_remove_stores() {
219
    public function phpunit_remove_stores() {
222
        $this->configstores = array();
220
        $this->configstores = [];
Línea 223... Línea 221...
223
    }
221
    }
224
 
222
 
225
    /**
223
    /**
Línea 232... Línea 230...
232
     */
230
     */
233
    public function phpunit_add_file_store(string $name, bool $nativettl = true): void {
231
    public function phpunit_add_file_store(string $name, bool $nativettl = true): void {
234
        if (!$nativettl) {
232
        if (!$nativettl) {
235
            require_once(__DIR__ . '/cachestore_file_with_ttl_wrappers.php');
233
            require_once(__DIR__ . '/cachestore_file_with_ttl_wrappers.php');
236
        }
234
        }
237
        $this->configstores[$name] = array(
235
        $this->configstores[$name] = [
238
            'name' => $name,
236
            'name' => $name,
239
            'plugin' => 'file',
237
            'plugin' => 'file',
240
            'configuration' => array(
238
            'configuration' => [
241
                'path' => ''
239
                'path' => '',
242
            ),
240
            ],
243
            'features' => 6,
241
            'features' => 6,
244
            'modes' => 3,
242
            'modes' => 3,
245
            'mappingsonly' => false,
243
            'mappingsonly' => false,
246
            'class' => $nativettl ? 'cachestore_file' : 'cachestore_file_with_ttl_wrappers',
244
            'class' => $nativettl ? 'cachestore_file' : 'cachestore_file_with_ttl_wrappers',
247
            'default' => false,
245
            'default' => false,
248
            'lock' => 'cachelock_file_default'
246
            'lock' => 'cachelock_file_default',
249
        );
247
        ];
250
    }
248
    }
Línea 251... Línea 249...
251
 
249
 
252
    /**
250
    /**
253
     * Hacks the in-memory configuration for a store.
251
     * Hacks the in-memory configuration for a store.
Línea 265... Línea 263...
265
     * Forcefully adds a session store.
263
     * Forcefully adds a session store.
266
     *
264
     *
267
     * @param string $name
265
     * @param string $name
268
     */
266
     */
269
    public function phpunit_add_session_store($name) {
267
    public function phpunit_add_session_store($name) {
270
        $this->configstores[$name] = array(
268
        $this->configstores[$name] = [
271
            'name' => $name,
269
            'name' => $name,
272
            'plugin' => 'session',
270
            'plugin' => 'session',
273
            'configuration' => array(),
271
            'configuration' => [],
274
            'features' => 14,
272
            'features' => 14,
275
            'modes' => 2,
273
            'modes' => 2,
276
            'default' => true,
274
            'default' => true,
277
            'class' => 'cachestore_session',
275
            'class' => 'cachestore_session',
278
            'lock' => 'cachelock_file_default',
276
            'lock' => 'cachelock_file_default',
279
        );
277
        ];
280
    }
278
    }
Línea 281... Línea 279...
281
 
279
 
282
    /**
280
    /**
283
     * Forcefully injects a definition => store mapping.
281
     * Forcefully injects a definition => store mapping.
Línea 288... Línea 286...
288
     * @param string $definition
286
     * @param string $definition
289
     * @param string $store
287
     * @param string $store
290
     * @param int $sort
288
     * @param int $sort
291
     */
289
     */
292
    public function phpunit_add_definition_mapping($definition, $store, $sort) {
290
    public function phpunit_add_definition_mapping($definition, $store, $sort) {
293
        $this->configdefinitionmappings[] = array(
291
        $this->configdefinitionmappings[] = [
294
            'store' => $store,
292
            'store' => $store,
295
            'definition' => $definition,
293
            'definition' => $definition,
296
            'sort' => (int)$sort
294
            'sort' => (int)$sort,
297
        );
295
        ];
298
    }
296
    }
Línea 299... Línea 297...
299
 
297
 
300
    /**
298
    /**
301
     * Overrides the default site identifier used by the Cache API so that we can be sure of what it is.
299
     * Overrides the default site identifier used by the Cache API so that we can be sure of what it is.
302
     *
300
     *
303
     * @return string
301
     * @return string
304
     */
302
     */
305
    public function get_site_identifier() {
303
    public function get_site_identifier() {
306
        global $CFG;
304
        global $CFG;
307
        return $CFG->wwwroot.'phpunit';
305
        return $CFG->wwwroot . 'phpunit';
Línea 308... Línea 306...
308
    }
306
    }
309
 
307
 
310
    /**
308
    /**
Línea 361... Línea 359...
361
    /**
359
    /**
362
     * Prepares this object for caching
360
     * Prepares this object for caching
363
     * @return array
361
     * @return array
364
     */
362
     */
365
    public function prepare_to_cache() {
363
    public function prepare_to_cache() {
366
        return array($this->property1.'_ptc', $this->property2.'_ptc', $this->propertytime);
364
        return [$this->property1 . '_ptc', $this->property2 . '_ptc', $this->propertytime];
367
    }
365
    }
368
    /**
366
    /**
369
     * Returns this object from the cache
367
     * Returns this object from the cache
370
     * @param array $data
368
     * @param array $data
371
     * @return cache_phpunit_dummy_object
369
     * @return cache_phpunit_dummy_object
Línea 375... Línea 373...
375
        if (!is_null($data[2])) {
373
        if (!is_null($data[2])) {
376
            // Windows 32bit microtime() resolution is 15ms, we ensure the time has moved forward.
374
            // Windows 32bit microtime() resolution is 15ms, we ensure the time has moved forward.
377
            do {
375
            do {
378
                $time = microtime(true);
376
                $time = microtime(true);
379
            } while ($time == $data[2]);
377
            } while ($time == $data[2]);
380
 
-
 
381
        }
378
        }
382
        return new cache_phpunit_dummy_object(array_shift($data).'_wfc', array_shift($data).'_wfc', $time);
379
        return new cache_phpunit_dummy_object(array_shift($data) . '_wfc', array_shift($data) . '_wfc', $time);
383
    }
380
    }
384
}
381
}
Línea 385... Línea 382...
385
 
382
 
386
/**
383
/**
Línea 405... Línea 402...
405
     *
402
     *
406
     * @param string $key
403
     * @param string $key
407
     * @return string
404
     * @return string
408
     */
405
     */
409
    public function load_for_cache($key) {
406
    public function load_for_cache($key) {
410
        return $key.' has no value really.';
407
        return $key . ' has no value really.';
411
    }
408
    }
Línea 412... Línea 409...
412
 
409
 
413
    /**
410
    /**
414
     * Loads many keys for the cache
411
     * Loads many keys for the cache
415
     *
412
     *
416
     * @param array $keys
413
     * @param array $keys
417
     * @return array
414
     * @return array
418
     */
415
     */
419
    public function load_many_for_cache(array $keys) {
416
    public function load_many_for_cache(array $keys) {
420
        $return = array();
417
        $return = [];
421
        foreach ($keys as $key) {
418
        foreach ($keys as $key) {
422
            $return[$key] = $key.' has no value really.';
419
            $return[$key] = $key . ' has no value really.';
423
        }
420
        }
424
        return $return;
421
        return $return;
425
    }
422
    }
Línea 432... Línea 429...
432
 *
429
 *
433
 * @copyright  2012 Sam Hemelryk
430
 * @copyright  2012 Sam Hemelryk
434
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
431
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
435
 */
432
 */
436
class cache_phpunit_application extends cache_application {
433
class cache_phpunit_application extends cache_application {
-
 
434
    #[\Override]
-
 
435
    public function get_store() {
-
 
436
        return parent::get_store();
-
 
437
    }
Línea 437... Línea 438...
437
 
438
 
438
    /**
439
    /**
439
     * Returns the class of the store immediately associated with this cache.
440
     * Returns the class of the store immediately associated with this cache.
440
     * @return string
441
     * @return string
Línea 480... Línea 481...
480
 *
481
 *
481
 * @copyright  2012 Sam Hemelryk
482
 * @copyright  2012 Sam Hemelryk
482
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
483
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
483
 */
484
 */
484
class cache_phpunit_session extends cache_session {
485
class cache_phpunit_session extends cache_session {
485
 
-
 
486
    /** @var Static member used for emulating the behaviour of session_id() during the tests. */
486
    /** @var Static member used for emulating the behaviour of session_id() during the tests. */
487
    protected static $sessionidmockup = 'phpunitmockupsessionid';
487
    protected static $sessionidmockup = 'phpunitmockupsessionid';
Línea -... Línea 488...
-
 
488
 
-
 
489
    #[\Override]
-
 
490
    public function get_store() {
-
 
491
        return parent::get_store();
-
 
492
    }
488
 
493
 
489
    /**
494
    /**
490
     * Returns the class of the store immediately associated with this cache.
495
     * Returns the class of the store immediately associated with this cache.
491
     * @return string
496
     * @return string
492
     */
497
     */
Línea 535... Línea 540...
535
 *
540
 *
536
 * @copyright  2012 Sam Hemelryk
541
 * @copyright  2012 Sam Hemelryk
537
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
542
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
538
 */
543
 */
539
class cache_phpunit_request extends cache_request {
544
class cache_phpunit_request extends cache_request {
-
 
545
    #[\Override]
-
 
546
    public function get_store() {
-
 
547
        return parent::get_store();
-
 
548
    }
Línea 540... Línea 549...
540
 
549
 
541
    /**
550
    /**
542
     * Returns the class of the store immediately associated with this cache.
551
     * Returns the class of the store immediately associated with this cache.
543
     * @return string
552
     * @return string