Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace core_cache;
18
 
19
use cache_config_testing;
20
 
21
/**
22
 * PHPunit tests for the cache API and in particular the core_cache\administration_helper
23
 *
24
 * @package    core_cache
25
 * @category   test
26
 * @copyright  2012 Sam Hemelryk
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1441 ariadna 28
 * @covers \core_cache\administration_helper
1 efrain 29
 */
1441 ariadna 30
final class administration_helper_test extends \advanced_testcase {
1 efrain 31
    /**
32
     * Set things back to the default before each test.
33
     */
34
    public function setUp(): void {
35
        parent::setUp();
1441 ariadna 36
        factory::reset();
1 efrain 37
        cache_config_testing::create_default_configuration();
38
    }
39
 
40
    /**
1441 ariadna 41
     * Require all test dependencies not auto-loadable.
42
     */
43
    public static function setUpBeforeClass(): void {
44
        global $CFG;
45
 
46
        parent::setUpBeforeClass();
47
        require_once($CFG->dirroot . '/cache/tests/fixtures/lib.php');
48
    }
49
 
50
    /**
1 efrain 51
     * Final task is to reset the cache system
52
     */
53
    public static function tearDownAfterClass(): void {
54
        parent::tearDownAfterClass();
1441 ariadna 55
        factory::reset();
1 efrain 56
    }
57
 
58
    /**
59
     * Test the numerous summaries the helper can produce.
60
     */
11 efrain 61
    public function test_get_summaries(): void {
1 efrain 62
        // First the preparation.
1441 ariadna 63
        $config = config_writer::instance();
1 efrain 64
        $this->assertTrue($config->add_store_instance('summariesstore', 'file'));
1441 ariadna 65
        $config->set_definition_mappings('core/eventinvalidation', ['summariesstore']);
66
        $this->assertTrue($config->set_mode_mappings([
67
            store::MODE_APPLICATION => ['summariesstore'],
68
            store::MODE_SESSION => ['default_session'],
69
            store::MODE_REQUEST => ['default_request'],
70
        ]));
1 efrain 71
 
72
        $storesummaries = administration_helper::get_store_instance_summaries();
73
        $this->assertIsArray($storesummaries);
74
        $this->assertArrayHasKey('summariesstore', $storesummaries);
75
        $summary = $storesummaries['summariesstore'];
1441 ariadna 76
        // Check the keys.
1 efrain 77
        $this->assertArrayHasKey('name', $summary);
78
        $this->assertArrayHasKey('plugin', $summary);
79
        $this->assertArrayHasKey('default', $summary);
80
        $this->assertArrayHasKey('isready', $summary);
81
        $this->assertArrayHasKey('requirementsmet', $summary);
82
        $this->assertArrayHasKey('mappings', $summary);
83
        $this->assertArrayHasKey('modes', $summary);
84
        $this->assertArrayHasKey('supports', $summary);
1441 ariadna 85
        // Check the important/known values.
1 efrain 86
        $this->assertEquals('summariesstore', $summary['name']);
87
        $this->assertEquals('file', $summary['plugin']);
88
        $this->assertEquals(0, $summary['default']);
89
        $this->assertEquals(1, $summary['isready']);
90
        $this->assertEquals(1, $summary['requirementsmet']);
91
 
92
        // Find the number of mappings to sessionstore.
1441 ariadna 93
        $mappingcount = count(array_filter($config->get_definitions(), function ($element) {
94
            return $element['mode'] === store::MODE_APPLICATION;
1 efrain 95
        }));
96
        $this->assertEquals($mappingcount, $summary['mappings']);
97
 
98
        $definitionsummaries = administration_helper::get_definition_summaries();
99
        $this->assertIsArray($definitionsummaries);
100
        $this->assertArrayHasKey('core/eventinvalidation', $definitionsummaries);
101
        $summary = $definitionsummaries['core/eventinvalidation'];
1441 ariadna 102
        // Check the keys.
1 efrain 103
        $this->assertArrayHasKey('id', $summary);
104
        $this->assertArrayHasKey('name', $summary);
105
        $this->assertArrayHasKey('mode', $summary);
106
        $this->assertArrayHasKey('component', $summary);
107
        $this->assertArrayHasKey('area', $summary);
108
        $this->assertArrayHasKey('mappings', $summary);
1441 ariadna 109
        // Check the important/known values.
1 efrain 110
        $this->assertEquals('core/eventinvalidation', $summary['id']);
111
        $this->assertInstanceOf('lang_string', $summary['name']);
1441 ariadna 112
        $this->assertEquals(store::MODE_APPLICATION, $summary['mode']);
1 efrain 113
        $this->assertEquals('core', $summary['component']);
114
        $this->assertEquals('eventinvalidation', $summary['area']);
115
        $this->assertIsArray($summary['mappings']);
116
        $this->assertContains('summariesstore', $summary['mappings']);
117
 
118
        $pluginsummaries = administration_helper::get_store_plugin_summaries();
119
        $this->assertIsArray($pluginsummaries);
120
        $this->assertArrayHasKey('file', $pluginsummaries);
121
        $summary = $pluginsummaries['file'];
1441 ariadna 122
        // Check the keys.
1 efrain 123
        $this->assertArrayHasKey('name', $summary);
124
        $this->assertArrayHasKey('requirementsmet', $summary);
125
        $this->assertArrayHasKey('instances', $summary);
126
        $this->assertArrayHasKey('modes', $summary);
127
        $this->assertArrayHasKey('supports', $summary);
128
        $this->assertArrayHasKey('canaddinstance', $summary);
129
 
130
        $locksummaries = administration_helper::get_lock_summaries();
131
        $this->assertIsArray($locksummaries);
132
        $this->assertTrue(count($locksummaries) > 0);
133
 
134
        $mappings = administration_helper::get_default_mode_stores();
135
        $this->assertIsArray($mappings);
136
        $this->assertCount(3, $mappings);
1441 ariadna 137
        $this->assertArrayHasKey(store::MODE_APPLICATION, $mappings);
138
        $this->assertIsArray($mappings[store::MODE_APPLICATION]);
139
        $this->assertContains('summariesstore', $mappings[store::MODE_APPLICATION]);
1 efrain 140
 
141
        $potentials = administration_helper::get_definition_store_options('core', 'eventinvalidation');
1441 ariadna 142
        $this->assertIsArray($potentials); // Currently used, suitable, default.
1 efrain 143
        $this->assertCount(3, $potentials);
144
        $this->assertArrayHasKey('summariesstore', $potentials[0]);
145
        $this->assertArrayHasKey('summariesstore', $potentials[1]);
146
        $this->assertArrayHasKey('default_application', $potentials[1]);
147
    }
148
 
149
    /**
150
     * Test instantiating an add store form.
151
     */
11 efrain 152
    public function test_get_add_store_form(): void {
1441 ariadna 153
        $form = factory::get_administration_display_helper()->get_add_store_form('file');
1 efrain 154
        $this->assertInstanceOf('moodleform', $form);
155
 
156
        try {
1441 ariadna 157
            $form = factory::get_administration_display_helper()->get_add_store_form('somethingstupid');
1 efrain 158
            $this->fail('You should not be able to create an add form for a store plugin that does not exist.');
159
        } catch (\moodle_exception $e) {
1441 ariadna 160
            $this->assertInstanceOf('coding_exception', $e, 'Needs to be: ' . get_class($e) . " ::: " . $e->getMessage());
1 efrain 161
        }
162
    }
163
 
164
    /**
165
     * Test instantiating a form to edit a store instance.
166
     */
11 efrain 167
    public function test_get_edit_store_form(): void {
1 efrain 168
        // Always instantiate a new core display helper here.
1441 ariadna 169
        $administrationhelper = new local\administration_display_helper();
170
        $config = config_writer::instance();
1 efrain 171
        $this->assertTrue($config->add_store_instance('test_get_edit_store_form', 'file'));
172
 
173
        $form = $administrationhelper->get_edit_store_form('file', 'test_get_edit_store_form');
174
        $this->assertInstanceOf('moodleform', $form);
175
 
176
        try {
177
            $form = $administrationhelper->get_edit_store_form('somethingstupid', 'moron');
178
            $this->fail('You should not be able to create an edit form for a store plugin that does not exist.');
179
        } catch (\moodle_exception $e) {
180
            $this->assertInstanceOf('coding_exception', $e);
181
        }
182
 
183
        try {
184
            $form = $administrationhelper->get_edit_store_form('file', 'blisters');
185
            $this->fail('You should not be able to create an edit form for a store plugin that does not exist.');
186
        } catch (\moodle_exception $e) {
187
            $this->assertInstanceOf('coding_exception', $e);
188
        }
189
    }
190
 
191
    /**
192
     * Test the hash_key functionality.
193
     */
11 efrain 194
    public function test_hash_key(): void {
1 efrain 195
        $this->resetAfterTest();
196
        set_debugging(DEBUG_ALL);
197
 
1441 ariadna 198
        // First with simplekeys.
1 efrain 199
        $instance = cache_config_testing::instance(true);
1441 ariadna 200
        $instance->phpunit_add_definition('phpunit/hashtest', [
201
            'mode' => store::MODE_APPLICATION,
1 efrain 202
            'component' => 'phpunit',
203
            'area' => 'hashtest',
1441 ariadna 204
            'simplekeys' => true,
205
        ]);
206
        $factory = factory::instance();
1 efrain 207
        $definition = $factory->create_definition('phpunit', 'hashtest');
208
 
1441 ariadna 209
        $result = helper::hash_key('test', $definition);
210
        $this->assertEquals('test-' . $definition->generate_single_key_prefix(), $result);
1 efrain 211
 
212
        try {
1441 ariadna 213
            helper::hash_key('test/test', $definition);
1 efrain 214
            $this->fail('Invalid key was allowed, you should see this.');
215
        } catch (\coding_exception $e) {
216
            $this->assertEquals('test/test', $e->debuginfo);
217
        }
218
 
1441 ariadna 219
        // Second without simple keys.
220
        $instance->phpunit_add_definition('phpunit/hashtest2', [
221
            'mode' => store::MODE_APPLICATION,
1 efrain 222
            'component' => 'phpunit',
223
            'area' => 'hashtest2',
1441 ariadna 224
            'simplekeys' => false,
225
        ]);
1 efrain 226
        $definition = $factory->create_definition('phpunit', 'hashtest2');
227
 
1441 ariadna 228
        $result = helper::hash_key('test', $definition);
229
        $this->assertEquals(sha1($definition->generate_single_key_prefix() . '-test'), $result);
1 efrain 230
 
1441 ariadna 231
        $result = helper::hash_key('test/test', $definition);
232
        $this->assertEquals(sha1($definition->generate_single_key_prefix() . '-test/test'), $result);
1 efrain 233
    }
234
 
235
    /**
236
     * Tests the get_usage function.
237
     */
238
    public function test_get_usage(): void {
239
        // Create a test cache definition and put items in it.
240
        $instance = cache_config_testing::instance(true);
241
        $instance->phpunit_add_definition('phpunit/test', [
1441 ariadna 242
                'mode' => store::MODE_APPLICATION,
1 efrain 243
                'component' => 'phpunit',
244
                'area' => 'test',
1441 ariadna 245
                'simplekeys' => true,
1 efrain 246
        ]);
247
        $cache = \cache::make('phpunit', 'test');
248
        for ($i = 0; $i < 100; $i++) {
249
            $cache->set('key' . $i, str_repeat('x', $i));
250
        }
251
 
1441 ariadna 252
        $factory = factory::instance();
1 efrain 253
        $adminhelper = $factory->get_administration_display_helper();
254
 
255
        $usage = $adminhelper->get_usage(10)['phpunit/test'];
256
        $this->assertEquals('phpunit/test', $usage->cacheid);
257
        $this->assertCount(1, $usage->stores);
258
        $store = $usage->stores[0];
259
        $this->assertEquals('default_application', $store->name);
260
        $this->assertEquals('cachestore_file', $store->class);
261
        $this->assertEquals(true, $store->supported);
262
        $this->assertEquals(100, $store->items);
263
 
264
        // As file store checks all items, the values should be exact.
265
        $this->assertEqualsWithDelta(57.4, $store->mean, 0.1);
266
        $this->assertEqualsWithDelta(29.0, $store->sd, 0.1);
267
        $this->assertEquals(0, $store->margin);
268
    }
269
}