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 cache config writer.
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\config_writer
1 efrain 29
 */
1441 ariadna 30
final class config_writer_test extends \advanced_testcase {
31
    /**
32
     * Load required libraries and fixtures.
33
     */
34
    public static function setUpBeforeClass(): void {
35
        global $CFG;
1 efrain 36
 
1441 ariadna 37
        require_once($CFG->dirroot . '/cache/tests/fixtures/lib.php');
38
        parent::setUpBeforeClass();
39
    }
40
 
1 efrain 41
    /**
42
     * Set things back to the default before each test.
43
     */
44
    public function setUp(): void {
45
        parent::setUp();
1441 ariadna 46
        factory::reset();
1 efrain 47
        cache_config_testing::create_default_configuration();
48
    }
49
 
50
    /**
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 getting an instance. Pretty basic.
60
     */
11 efrain 61
    public function test_instance(): void {
1441 ariadna 62
        $config = config_writer::instance();
63
        $this->assertInstanceOf(config_writer::class, $config);
1 efrain 64
    }
65
 
66
    /**
67
     * Test the default configuration.
68
     */
11 efrain 69
    public function test_default_configuration(): void {
1441 ariadna 70
        $config = config_writer::instance();
1 efrain 71
 
72
        // First check stores.
73
        $stores = $config->get_all_stores();
74
        $hasapplication = false;
75
        $hassession = false;
76
        $hasrequest = false;
77
        foreach ($stores as $store) {
78
            // Check the required keys.
79
            $this->assertArrayHasKey('name', $store);
80
            $this->assertArrayHasKey('plugin', $store);
81
            $this->assertArrayHasKey('modes', $store);
82
            $this->assertArrayHasKey('default', $store);
83
            // Check the mode, we need at least one default store of each mode.
84
            if (!empty($store['default'])) {
1441 ariadna 85
                if ($store['modes'] & store::MODE_APPLICATION) {
1 efrain 86
                    $hasapplication = true;
87
                }
1441 ariadna 88
                if ($store['modes'] & store::MODE_SESSION) {
1 efrain 89
                    $hassession = true;
90
                }
1441 ariadna 91
                if ($store['modes'] & store::MODE_REQUEST) {
1 efrain 92
                    $hasrequest = true;
93
                }
94
            }
95
        }
96
        $this->assertTrue($hasapplication, 'There is no default application cache store.');
97
        $this->assertTrue($hassession, 'There is no default session cache store.');
98
        $this->assertTrue($hasrequest, 'There is no default request cache store.');
99
 
100
        // Next check the definitions.
101
        $definitions = $config->get_definitions();
102
        $eventinvalidation = false;
103
        foreach ($definitions as $definition) {
104
            // Check the required keys.
105
            $this->assertArrayHasKey('mode', $definition);
106
            $this->assertArrayHasKey('component', $definition);
107
            $this->assertArrayHasKey('area', $definition);
108
            if ($definition['component'] === 'core' && $definition['area'] === 'eventinvalidation') {
109
                $eventinvalidation = true;
110
            }
111
        }
112
        $this->assertTrue($eventinvalidation, 'Missing the event invalidation definition.');
113
 
1441 ariadna 114
        // Next mode mappings.
1 efrain 115
        $mappings = $config->get_mode_mappings();
116
        $hasapplication = false;
117
        $hassession = false;
118
        $hasrequest = false;
119
        foreach ($mappings as $mode) {
120
            // Check the required keys.
121
            $this->assertArrayHasKey('mode', $mode);
122
            $this->assertArrayHasKey('store', $mode);
123
 
1441 ariadna 124
            if ($mode['mode'] === store::MODE_APPLICATION) {
1 efrain 125
                $hasapplication = true;
126
            }
1441 ariadna 127
            if ($mode['mode'] === store::MODE_SESSION) {
1 efrain 128
                $hassession = true;
129
            }
1441 ariadna 130
            if ($mode['mode'] === store::MODE_REQUEST) {
1 efrain 131
                $hasrequest = true;
132
            }
133
        }
134
        $this->assertTrue($hasapplication, 'There is no mapping for the application mode.');
135
        $this->assertTrue($hassession, 'There is no mapping for the session mode.');
136
        $this->assertTrue($hasrequest, 'There is no mapping for the request mode.');
137
 
1441 ariadna 138
        // Finally check config locks.
1 efrain 139
        $locks = $config->get_locks();
140
        foreach ($locks as $lock) {
141
            $this->assertArrayHasKey('name', $lock);
142
            $this->assertArrayHasKey('type', $lock);
143
            $this->assertArrayHasKey('default', $lock);
144
        }
145
        // There has to be at least the default lock.
146
        $this->assertTrue(count($locks) > 0);
147
    }
148
 
149
    /**
150
     * Test updating the definitions.
151
     */
11 efrain 152
    public function test_update_definitions(): void {
1441 ariadna 153
        $config = config_writer::instance();
1 efrain 154
        // Remove the definition.
155
        $config->phpunit_remove_definition('core/string');
156
        $definitions = $config->get_definitions();
157
        // Check it is gone.
158
        $this->assertFalse(array_key_exists('core/string', $definitions));
159
        // Update definitions. This should re-add it.
1441 ariadna 160
        config_writer::update_definitions();
1 efrain 161
        $definitions = $config->get_definitions();
162
        // Check it is back again.
163
        $this->assertTrue(array_key_exists('core/string', $definitions));
164
    }
165
 
166
    /**
167
     * Test adding/editing/deleting store instances.
168
     */
11 efrain 169
    public function test_add_edit_delete_plugin_instance(): void {
1441 ariadna 170
        $config = config_writer::instance();
1 efrain 171
        $this->assertArrayNotHasKey('addplugintest', $config->get_all_stores());
172
        $this->assertArrayNotHasKey('addplugintestwlock', $config->get_all_stores());
173
        // Add a default file instance.
174
        $config->add_store_instance('addplugintest', 'file');
175
 
1441 ariadna 176
        factory::reset();
177
        $config = config_writer::instance();
1 efrain 178
        $this->assertArrayHasKey('addplugintest', $config->get_all_stores());
179
 
180
        // Add a store with a lock described.
1441 ariadna 181
        $config->add_store_instance('addplugintestwlock', 'file', ['lock' => 'default_file_lock']);
1 efrain 182
        $this->assertArrayHasKey('addplugintestwlock', $config->get_all_stores());
183
 
184
        $config->delete_store_instance('addplugintest');
185
        $this->assertArrayNotHasKey('addplugintest', $config->get_all_stores());
186
        $this->assertArrayHasKey('addplugintestwlock', $config->get_all_stores());
187
 
188
        $config->delete_store_instance('addplugintestwlock');
189
        $this->assertArrayNotHasKey('addplugintest', $config->get_all_stores());
190
        $this->assertArrayNotHasKey('addplugintestwlock', $config->get_all_stores());
191
 
192
        // Add a default file instance.
1441 ariadna 193
        $config->add_store_instance('storeconfigtest', 'file', ['test' => 'a', 'one' => 'two']);
1 efrain 194
        $stores = $config->get_all_stores();
195
        $this->assertArrayHasKey('storeconfigtest', $stores);
196
        $this->assertArrayHasKey('configuration', $stores['storeconfigtest']);
197
        $this->assertArrayHasKey('test', $stores['storeconfigtest']['configuration']);
198
        $this->assertArrayHasKey('one', $stores['storeconfigtest']['configuration']);
199
        $this->assertEquals('a', $stores['storeconfigtest']['configuration']['test']);
200
        $this->assertEquals('two', $stores['storeconfigtest']['configuration']['one']);
201
 
1441 ariadna 202
        $config->edit_store_instance('storeconfigtest', 'file', ['test' => 'b', 'one' => 'three']);
1 efrain 203
        $stores = $config->get_all_stores();
204
        $this->assertArrayHasKey('storeconfigtest', $stores);
205
        $this->assertArrayHasKey('configuration', $stores['storeconfigtest']);
206
        $this->assertArrayHasKey('test', $stores['storeconfigtest']['configuration']);
207
        $this->assertArrayHasKey('one', $stores['storeconfigtest']['configuration']);
208
        $this->assertEquals('b', $stores['storeconfigtest']['configuration']['test']);
209
        $this->assertEquals('three', $stores['storeconfigtest']['configuration']['one']);
210
 
211
        $config->delete_store_instance('storeconfigtest');
212
 
213
        try {
214
            $config->delete_store_instance('default_application');
215
            $this->fail('Default store deleted. This should not be possible!');
216
        } catch (\Exception $e) {
1441 ariadna 217
            $this->assertInstanceOf(\core_cache\exception\cache_exception::class, $e);
1 efrain 218
        }
219
 
220
        try {
221
            $config->delete_store_instance('some_crazy_store');
222
            $this->fail('You should not be able to delete a store that does not exist.');
223
        } catch (\Exception $e) {
1441 ariadna 224
            $this->assertInstanceOf(\core_cache\exception\cache_exception::class, $e);
1 efrain 225
        }
226
 
227
        try {
228
            // Try with a plugin that does not exist.
1441 ariadna 229
            $config->add_store_instance('storeconfigtest', 'shallowfail', ['test' => 'a', 'one' => 'two']);
1 efrain 230
            $this->fail('You should not be able to add an instance of a store that does not exist.');
231
        } catch (\Exception $e) {
1441 ariadna 232
            $this->assertInstanceOf(\core_cache\exception\cache_exception::class, $e);
1 efrain 233
        }
234
    }
235
 
236
    /**
237
     * Test setting some mode mappings.
238
     */
11 efrain 239
    public function test_set_mode_mappings(): void {
1441 ariadna 240
        $config = config_writer::instance();
1 efrain 241
        $this->assertTrue($config->add_store_instance('setmodetest', 'file'));
1441 ariadna 242
        $this->assertTrue($config->set_mode_mappings([
243
            store::MODE_APPLICATION => ['setmodetest', 'default_application'],
244
            store::MODE_SESSION => ['default_session'],
245
            store::MODE_REQUEST => ['default_request'],
246
        ]));
1 efrain 247
        $mappings = $config->get_mode_mappings();
248
        $setmodetestfound = false;
249
        foreach ($mappings as $mapping) {
1441 ariadna 250
            if ($mapping['store'] == 'setmodetest' && $mapping['mode'] == store::MODE_APPLICATION) {
1 efrain 251
                $setmodetestfound = true;
252
            }
253
        }
254
        $this->assertTrue($setmodetestfound, 'Set mapping did not work as expected.');
255
    }
256
 
257
    /**
258
     * Test setting some definition mappings.
259
     */
11 efrain 260
    public function test_set_definition_mappings(): void {
1441 ariadna 261
        $config = cache_config_testing::instance();
262
        $config->phpunit_add_definition('phpunit/testdefinition', [
263
            'mode' => store::MODE_APPLICATION,
1 efrain 264
            'component' => 'phpunit',
1441 ariadna 265
            'area' => 'testdefinition',
266
        ]);
1 efrain 267
 
1441 ariadna 268
        $config = config_writer::instance();
1 efrain 269
        $this->assertTrue($config->add_store_instance('setdefinitiontest', 'file'));
270
        $this->assertIsArray($config->get_definition_by_id('phpunit/testdefinition'));
1441 ariadna 271
        $config->set_definition_mappings('phpunit/testdefinition', ['setdefinitiontest', 'default_application']);
1 efrain 272
 
273
        try {
1441 ariadna 274
            $config->set_definition_mappings('phpunit/testdefinition', ['something that does not exist']);
1 efrain 275
            $this->fail('You should not be able to set a mapping for a store that does not exist.');
276
        } catch (\Exception $e) {
1441 ariadna 277
            $this->assertInstanceOf(\core\exception\coding_exception::class, $e);
1 efrain 278
        }
279
 
280
        try {
1441 ariadna 281
            $config->set_definition_mappings('something/crazy', ['setdefinitiontest']);
1 efrain 282
            $this->fail('You should not be able to set a mapping for a definition that does not exist.');
283
        } catch (\Exception $e) {
1441 ariadna 284
            $this->assertInstanceOf(\core\exception\coding_exception::class, $e);
1 efrain 285
        }
286
    }
287
}