| Línea 15... |
Línea 15... |
| 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... |
| 17 |
namespace core_cache;
|
17 |
namespace core_cache;
|
| 18 |
|
- |
|
| 19 |
use cache_config_testing;
|
- |
|
| 20 |
use cache_config_writer;
|
- |
|
| 21 |
use cache_factory;
|
- |
|
| 22 |
use cache_store;
|
- |
|
| 23 |
|
- |
|
| 24 |
defined('MOODLE_INTERNAL') || die();
|
- |
|
| 25 |
|
- |
|
| 26 |
// Include the necessary evils.
|
- |
|
| 27 |
global $CFG;
|
- |
|
| Línea 28... |
Línea 18... |
| 28 |
require_once($CFG->dirroot.'/cache/locallib.php');
|
18 |
|
| 29 |
require_once($CFG->dirroot.'/cache/tests/fixtures/lib.php');
|
19 |
use cache_config_testing;
|
| 30 |
|
20 |
|
| 31 |
/**
|
21 |
/**
|
| 32 |
* PHPunit tests for the cache API and in particular the cache config writer.
|
22 |
* PHPunit tests for the cache API and in particular the cache config writer.
|
| 33 |
*
|
23 |
*
|
| 34 |
* @package core_cache
|
24 |
* @package core_cache
|
| - |
|
25 |
* @category test
|
| 35 |
* @category test
|
26 |
* @copyright 2012 Sam Hemelryk
|
| 36 |
* @copyright 2012 Sam Hemelryk
|
27 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
| - |
|
28 |
* @covers \core_cache\config_writer
|
| - |
|
29 |
*/
|
| - |
|
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;
|
| - |
|
36 |
|
| Línea 37... |
Línea 37... |
| 37 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
37 |
require_once($CFG->dirroot . '/cache/tests/fixtures/lib.php');
|
| 38 |
*/
|
38 |
parent::setUpBeforeClass();
|
| 39 |
class config_writer_test extends \advanced_testcase {
|
39 |
}
|
| 40 |
|
40 |
|
| 41 |
/**
|
41 |
/**
|
| 42 |
* Set things back to the default before each test.
|
42 |
* Set things back to the default before each test.
|
| 43 |
*/
|
43 |
*/
|
| 44 |
public function setUp(): void {
|
44 |
public function setUp(): void {
|
| Línea 45... |
Línea 45... |
| 45 |
parent::setUp();
|
45 |
parent::setUp();
|
| 46 |
cache_factory::reset();
|
46 |
factory::reset();
|
| 47 |
cache_config_testing::create_default_configuration();
|
47 |
cache_config_testing::create_default_configuration();
|
| 48 |
}
|
48 |
}
|
| 49 |
|
49 |
|
| 50 |
/**
|
50 |
/**
|
| 51 |
* Final task is to reset the cache system
|
51 |
* Final task is to reset the cache system
|
| Línea 52... |
Línea 52... |
| 52 |
*/
|
52 |
*/
|
| 53 |
public static function tearDownAfterClass(): void {
|
53 |
public static function tearDownAfterClass(): void {
|
| 54 |
parent::tearDownAfterClass();
|
54 |
parent::tearDownAfterClass();
|
| 55 |
cache_factory::reset();
|
55 |
factory::reset();
|
| 56 |
}
|
56 |
}
|
| 57 |
|
57 |
|
| 58 |
/**
|
58 |
/**
|
| Línea 59... |
Línea 59... |
| 59 |
* Test getting an instance. Pretty basic.
|
59 |
* Test getting an instance. Pretty basic.
|
| 60 |
*/
|
60 |
*/
|
| 61 |
public function test_instance(): void {
|
61 |
public function test_instance(): void {
|
| 62 |
$config = cache_config_writer::instance();
|
62 |
$config = config_writer::instance();
|
| 63 |
$this->assertInstanceOf('cache_config_writer', $config);
|
63 |
$this->assertInstanceOf(config_writer::class, $config);
|
| Línea 64... |
Línea 64... |
| 64 |
}
|
64 |
}
|
| 65 |
|
65 |
|
| 66 |
/**
|
66 |
/**
|
| 67 |
* Test the default configuration.
|
67 |
* Test the default configuration.
|
| Línea 80... |
Línea 80... |
| 80 |
$this->assertArrayHasKey('plugin', $store);
|
80 |
$this->assertArrayHasKey('plugin', $store);
|
| 81 |
$this->assertArrayHasKey('modes', $store);
|
81 |
$this->assertArrayHasKey('modes', $store);
|
| 82 |
$this->assertArrayHasKey('default', $store);
|
82 |
$this->assertArrayHasKey('default', $store);
|
| 83 |
// Check the mode, we need at least one default store of each mode.
|
83 |
// Check the mode, we need at least one default store of each mode.
|
| 84 |
if (!empty($store['default'])) {
|
84 |
if (!empty($store['default'])) {
|
| 85 |
if ($store['modes'] & cache_store::MODE_APPLICATION) {
|
85 |
if ($store['modes'] & store::MODE_APPLICATION) {
|
| 86 |
$hasapplication = true;
|
86 |
$hasapplication = true;
|
| 87 |
}
|
87 |
}
|
| 88 |
if ($store['modes'] & cache_store::MODE_SESSION) {
|
88 |
if ($store['modes'] & store::MODE_SESSION) {
|
| 89 |
$hassession = true;
|
89 |
$hassession = true;
|
| 90 |
}
|
90 |
}
|
| 91 |
if ($store['modes'] & cache_store::MODE_REQUEST) {
|
91 |
if ($store['modes'] & store::MODE_REQUEST) {
|
| 92 |
$hasrequest = true;
|
92 |
$hasrequest = true;
|
| 93 |
}
|
93 |
}
|
| 94 |
}
|
94 |
}
|
| 95 |
}
|
95 |
}
|
| 96 |
$this->assertTrue($hasapplication, 'There is no default application cache store.');
|
96 |
$this->assertTrue($hasapplication, 'There is no default application cache store.');
|
| Línea 109... |
Línea 109... |
| 109 |
$eventinvalidation = true;
|
109 |
$eventinvalidation = true;
|
| 110 |
}
|
110 |
}
|
| 111 |
}
|
111 |
}
|
| 112 |
$this->assertTrue($eventinvalidation, 'Missing the event invalidation definition.');
|
112 |
$this->assertTrue($eventinvalidation, 'Missing the event invalidation definition.');
|
| Línea 113... |
Línea 113... |
| 113 |
|
113 |
|
| 114 |
// Next mode mappings
|
114 |
// Next mode mappings.
|
| 115 |
$mappings = $config->get_mode_mappings();
|
115 |
$mappings = $config->get_mode_mappings();
|
| 116 |
$hasapplication = false;
|
116 |
$hasapplication = false;
|
| 117 |
$hassession = false;
|
117 |
$hassession = false;
|
| 118 |
$hasrequest = false;
|
118 |
$hasrequest = false;
|
| 119 |
foreach ($mappings as $mode) {
|
119 |
foreach ($mappings as $mode) {
|
| 120 |
// Check the required keys.
|
120 |
// Check the required keys.
|
| 121 |
$this->assertArrayHasKey('mode', $mode);
|
121 |
$this->assertArrayHasKey('mode', $mode);
|
| Línea 122... |
Línea 122... |
| 122 |
$this->assertArrayHasKey('store', $mode);
|
122 |
$this->assertArrayHasKey('store', $mode);
|
| 123 |
|
123 |
|
| 124 |
if ($mode['mode'] === cache_store::MODE_APPLICATION) {
|
124 |
if ($mode['mode'] === store::MODE_APPLICATION) {
|
| 125 |
$hasapplication = true;
|
125 |
$hasapplication = true;
|
| 126 |
}
|
126 |
}
|
| 127 |
if ($mode['mode'] === cache_store::MODE_SESSION) {
|
127 |
if ($mode['mode'] === store::MODE_SESSION) {
|
| 128 |
$hassession = true;
|
128 |
$hassession = true;
|
| 129 |
}
|
129 |
}
|
| 130 |
if ($mode['mode'] === cache_store::MODE_REQUEST) {
|
130 |
if ($mode['mode'] === store::MODE_REQUEST) {
|
| 131 |
$hasrequest = true;
|
131 |
$hasrequest = true;
|
| 132 |
}
|
132 |
}
|
| 133 |
}
|
133 |
}
|
| 134 |
$this->assertTrue($hasapplication, 'There is no mapping for the application mode.');
|
134 |
$this->assertTrue($hasapplication, 'There is no mapping for the application mode.');
|
| Línea 135... |
Línea 135... |
| 135 |
$this->assertTrue($hassession, 'There is no mapping for the session 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.');
|
136 |
$this->assertTrue($hasrequest, 'There is no mapping for the request mode.');
|
| 137 |
|
137 |
|
| 138 |
// Finally check config locks
|
138 |
// Finally check config locks.
|
| 139 |
$locks = $config->get_locks();
|
139 |
$locks = $config->get_locks();
|
| 140 |
foreach ($locks as $lock) {
|
140 |
foreach ($locks as $lock) {
|
| Línea 148... |
Línea 148... |
| 148 |
|
148 |
|
| 149 |
/**
|
149 |
/**
|
| 150 |
* Test updating the definitions.
|
150 |
* Test updating the definitions.
|
| 151 |
*/
|
151 |
*/
|
| 152 |
public function test_update_definitions(): void {
|
152 |
public function test_update_definitions(): void {
|
| 153 |
$config = cache_config_writer::instance();
|
153 |
$config = config_writer::instance();
|
| 154 |
// Remove the definition.
|
154 |
// Remove the definition.
|
| 155 |
$config->phpunit_remove_definition('core/string');
|
155 |
$config->phpunit_remove_definition('core/string');
|
| 156 |
$definitions = $config->get_definitions();
|
156 |
$definitions = $config->get_definitions();
|
| 157 |
// Check it is gone.
|
157 |
// Check it is gone.
|
| 158 |
$this->assertFalse(array_key_exists('core/string', $definitions));
|
158 |
$this->assertFalse(array_key_exists('core/string', $definitions));
|
| 159 |
// Update definitions. This should re-add it.
|
159 |
// Update definitions. This should re-add it.
|
| 160 |
cache_config_writer::update_definitions();
|
160 |
config_writer::update_definitions();
|
| 161 |
$definitions = $config->get_definitions();
|
161 |
$definitions = $config->get_definitions();
|
| 162 |
// Check it is back again.
|
162 |
// Check it is back again.
|
| 163 |
$this->assertTrue(array_key_exists('core/string', $definitions));
|
163 |
$this->assertTrue(array_key_exists('core/string', $definitions));
|
| Línea 164... |
Línea 164... |
| 164 |
}
|
164 |
}
|
| 165 |
|
165 |
|
| 166 |
/**
|
166 |
/**
|
| 167 |
* Test adding/editing/deleting store instances.
|
167 |
* Test adding/editing/deleting store instances.
|
| 168 |
*/
|
168 |
*/
|
| 169 |
public function test_add_edit_delete_plugin_instance(): void {
|
169 |
public function test_add_edit_delete_plugin_instance(): void {
|
| 170 |
$config = cache_config_writer::instance();
|
170 |
$config = config_writer::instance();
|
| 171 |
$this->assertArrayNotHasKey('addplugintest', $config->get_all_stores());
|
171 |
$this->assertArrayNotHasKey('addplugintest', $config->get_all_stores());
|
| 172 |
$this->assertArrayNotHasKey('addplugintestwlock', $config->get_all_stores());
|
172 |
$this->assertArrayNotHasKey('addplugintestwlock', $config->get_all_stores());
|
| Línea 173... |
Línea 173... |
| 173 |
// Add a default file instance.
|
173 |
// Add a default file instance.
|
| 174 |
$config->add_store_instance('addplugintest', 'file');
|
174 |
$config->add_store_instance('addplugintest', 'file');
|
| 175 |
|
175 |
|
| Línea 176... |
Línea 176... |
| 176 |
cache_factory::reset();
|
176 |
factory::reset();
|
| 177 |
$config = cache_config_writer::instance();
|
177 |
$config = config_writer::instance();
|
| 178 |
$this->assertArrayHasKey('addplugintest', $config->get_all_stores());
|
178 |
$this->assertArrayHasKey('addplugintest', $config->get_all_stores());
|
| Línea 179... |
Línea 179... |
| 179 |
|
179 |
|
| 180 |
// Add a store with a lock described.
|
180 |
// Add a store with a lock described.
|
| 181 |
$config->add_store_instance('addplugintestwlock', 'file', array('lock' => 'default_file_lock'));
|
181 |
$config->add_store_instance('addplugintestwlock', 'file', ['lock' => 'default_file_lock']);
|
| Línea 188... |
Línea 188... |
| 188 |
$config->delete_store_instance('addplugintestwlock');
|
188 |
$config->delete_store_instance('addplugintestwlock');
|
| 189 |
$this->assertArrayNotHasKey('addplugintest', $config->get_all_stores());
|
189 |
$this->assertArrayNotHasKey('addplugintest', $config->get_all_stores());
|
| 190 |
$this->assertArrayNotHasKey('addplugintestwlock', $config->get_all_stores());
|
190 |
$this->assertArrayNotHasKey('addplugintestwlock', $config->get_all_stores());
|
| Línea 191... |
Línea 191... |
| 191 |
|
191 |
|
| 192 |
// Add a default file instance.
|
192 |
// Add a default file instance.
|
| 193 |
$config->add_store_instance('storeconfigtest', 'file', array('test' => 'a', 'one' => 'two'));
|
193 |
$config->add_store_instance('storeconfigtest', 'file', ['test' => 'a', 'one' => 'two']);
|
| 194 |
$stores = $config->get_all_stores();
|
194 |
$stores = $config->get_all_stores();
|
| 195 |
$this->assertArrayHasKey('storeconfigtest', $stores);
|
195 |
$this->assertArrayHasKey('storeconfigtest', $stores);
|
| 196 |
$this->assertArrayHasKey('configuration', $stores['storeconfigtest']);
|
196 |
$this->assertArrayHasKey('configuration', $stores['storeconfigtest']);
|
| 197 |
$this->assertArrayHasKey('test', $stores['storeconfigtest']['configuration']);
|
197 |
$this->assertArrayHasKey('test', $stores['storeconfigtest']['configuration']);
|
| 198 |
$this->assertArrayHasKey('one', $stores['storeconfigtest']['configuration']);
|
198 |
$this->assertArrayHasKey('one', $stores['storeconfigtest']['configuration']);
|
| 199 |
$this->assertEquals('a', $stores['storeconfigtest']['configuration']['test']);
|
199 |
$this->assertEquals('a', $stores['storeconfigtest']['configuration']['test']);
|
| Línea 200... |
Línea 200... |
| 200 |
$this->assertEquals('two', $stores['storeconfigtest']['configuration']['one']);
|
200 |
$this->assertEquals('two', $stores['storeconfigtest']['configuration']['one']);
|
| 201 |
|
201 |
|
| 202 |
$config->edit_store_instance('storeconfigtest', 'file', array('test' => 'b', 'one' => 'three'));
|
202 |
$config->edit_store_instance('storeconfigtest', 'file', ['test' => 'b', 'one' => 'three']);
|
| 203 |
$stores = $config->get_all_stores();
|
203 |
$stores = $config->get_all_stores();
|
| 204 |
$this->assertArrayHasKey('storeconfigtest', $stores);
|
204 |
$this->assertArrayHasKey('storeconfigtest', $stores);
|
| 205 |
$this->assertArrayHasKey('configuration', $stores['storeconfigtest']);
|
205 |
$this->assertArrayHasKey('configuration', $stores['storeconfigtest']);
|
| Línea 212... |
Línea 212... |
| 212 |
|
212 |
|
| 213 |
try {
|
213 |
try {
|
| 214 |
$config->delete_store_instance('default_application');
|
214 |
$config->delete_store_instance('default_application');
|
| 215 |
$this->fail('Default store deleted. This should not be possible!');
|
215 |
$this->fail('Default store deleted. This should not be possible!');
|
| 216 |
} catch (\Exception $e) {
|
216 |
} catch (\Exception $e) {
|
| 217 |
$this->assertInstanceOf('cache_exception', $e);
|
217 |
$this->assertInstanceOf(\core_cache\exception\cache_exception::class, $e);
|
| Línea 218... |
Línea 218... |
| 218 |
}
|
218 |
}
|
| 219 |
|
219 |
|
| 220 |
try {
|
220 |
try {
|
| 221 |
$config->delete_store_instance('some_crazy_store');
|
221 |
$config->delete_store_instance('some_crazy_store');
|
| 222 |
$this->fail('You should not be able to delete a store that does not exist.');
|
222 |
$this->fail('You should not be able to delete a store that does not exist.');
|
| 223 |
} catch (\Exception $e) {
|
223 |
} catch (\Exception $e) {
|
| Línea 224... |
Línea 224... |
| 224 |
$this->assertInstanceOf('cache_exception', $e);
|
224 |
$this->assertInstanceOf(\core_cache\exception\cache_exception::class, $e);
|
| 225 |
}
|
225 |
}
|
| 226 |
|
226 |
|
| 227 |
try {
|
227 |
try {
|
| 228 |
// Try with a plugin that does not exist.
|
228 |
// Try with a plugin that does not exist.
|
| 229 |
$config->add_store_instance('storeconfigtest', 'shallowfail', array('test' => 'a', 'one' => 'two'));
|
229 |
$config->add_store_instance('storeconfigtest', 'shallowfail', ['test' => 'a', 'one' => 'two']);
|
| 230 |
$this->fail('You should not be able to add an instance of a store that does not exist.');
|
230 |
$this->fail('You should not be able to add an instance of a store that does not exist.');
|
| 231 |
} catch (\Exception $e) {
|
231 |
} catch (\Exception $e) {
|
| Línea 232... |
Línea 232... |
| 232 |
$this->assertInstanceOf('cache_exception', $e);
|
232 |
$this->assertInstanceOf(\core_cache\exception\cache_exception::class, $e);
|
| 233 |
}
|
233 |
}
|
| 234 |
}
|
234 |
}
|
| 235 |
|
235 |
|
| 236 |
/**
|
236 |
/**
|
| 237 |
* Test setting some mode mappings.
|
237 |
* Test setting some mode mappings.
|
| 238 |
*/
|
238 |
*/
|
| 239 |
public function test_set_mode_mappings(): void {
|
239 |
public function test_set_mode_mappings(): void {
|
| 240 |
$config = cache_config_writer::instance();
|
240 |
$config = config_writer::instance();
|
| 241 |
$this->assertTrue($config->add_store_instance('setmodetest', 'file'));
|
241 |
$this->assertTrue($config->add_store_instance('setmodetest', 'file'));
|
| 242 |
$this->assertTrue($config->set_mode_mappings(array(
|
242 |
$this->assertTrue($config->set_mode_mappings([
|
| 243 |
cache_store::MODE_APPLICATION => array('setmodetest', 'default_application'),
|
243 |
store::MODE_APPLICATION => ['setmodetest', 'default_application'],
|
| 244 |
cache_store::MODE_SESSION => array('default_session'),
|
244 |
store::MODE_SESSION => ['default_session'],
|
| 245 |
cache_store::MODE_REQUEST => array('default_request'),
|
245 |
store::MODE_REQUEST => ['default_request'],
|
| 246 |
)));
|
246 |
]));
|
| 247 |
$mappings = $config->get_mode_mappings();
|
247 |
$mappings = $config->get_mode_mappings();
|
| 248 |
$setmodetestfound = false;
|
248 |
$setmodetestfound = false;
|
| 249 |
foreach ($mappings as $mapping) {
|
249 |
foreach ($mappings as $mapping) {
|
| 250 |
if ($mapping['store'] == 'setmodetest' && $mapping['mode'] == cache_store::MODE_APPLICATION) {
|
250 |
if ($mapping['store'] == 'setmodetest' && $mapping['mode'] == store::MODE_APPLICATION) {
|
| 251 |
$setmodetestfound = true;
|
251 |
$setmodetestfound = true;
|
| Línea 252... |
Línea 252... |
| 252 |
}
|
252 |
}
|
| 253 |
}
|
253 |
}
|
| 254 |
$this->assertTrue($setmodetestfound, 'Set mapping did not work as expected.');
|
254 |
$this->assertTrue($setmodetestfound, 'Set mapping did not work as expected.');
|
| 255 |
}
|
255 |
}
|
| 256 |
|
256 |
|
| 257 |
/**
|
257 |
/**
|
| 258 |
* Test setting some definition mappings.
|
258 |
* Test setting some definition mappings.
|
| 259 |
*/
|
259 |
*/
|
| 260 |
public function test_set_definition_mappings(): void {
|
260 |
public function test_set_definition_mappings(): void {
|
| 261 |
$config = cache_config_testing::instance(true);
|
261 |
$config = cache_config_testing::instance();
|
| Línea 262... |
Línea 262... |
| 262 |
$config->phpunit_add_definition('phpunit/testdefinition', array(
|
262 |
$config->phpunit_add_definition('phpunit/testdefinition', [
|
| 263 |
'mode' => cache_store::MODE_APPLICATION,
|
263 |
'mode' => store::MODE_APPLICATION,
|
| 264 |
'component' => 'phpunit',
|
264 |
'component' => 'phpunit',
|
| 265 |
'area' => 'testdefinition'
|
265 |
'area' => 'testdefinition',
|
| Línea 266... |
Línea 266... |
| 266 |
));
|
266 |
]);
|
| 267 |
|
267 |
|
| 268 |
$config = cache_config_writer::instance();
|
268 |
$config = config_writer::instance();
|
| 269 |
$this->assertTrue($config->add_store_instance('setdefinitiontest', 'file'));
|
269 |
$this->assertTrue($config->add_store_instance('setdefinitiontest', 'file'));
|
| 270 |
$this->assertIsArray($config->get_definition_by_id('phpunit/testdefinition'));
|
270 |
$this->assertIsArray($config->get_definition_by_id('phpunit/testdefinition'));
|
| 271 |
$config->set_definition_mappings('phpunit/testdefinition', array('setdefinitiontest', 'default_application'));
|
271 |
$config->set_definition_mappings('phpunit/testdefinition', ['setdefinitiontest', 'default_application']);
|
| Línea 272... |
Línea 272... |
| 272 |
|
272 |
|
| 273 |
try {
|
273 |
try {
|
| 274 |
$config->set_definition_mappings('phpunit/testdefinition', array('something that does not exist'));
|
274 |
$config->set_definition_mappings('phpunit/testdefinition', ['something that does not exist']);
|
| 275 |
$this->fail('You should not be able to set a mapping for a store that does not exist.');
|
275 |
$this->fail('You should not be able to set a mapping for a store that does not exist.');
|
| 276 |
} catch (\Exception $e) {
|
276 |
} catch (\Exception $e) {
|
| 277 |
$this->assertInstanceOf('coding_exception', $e);
|
277 |
$this->assertInstanceOf(\core\exception\coding_exception::class, $e);
|
| 278 |
}
|
278 |
}
|
| 279 |
|
279 |
|