| 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_helper;
|
- |
|
| 23 |
use cache_store;
|
- |
|
| 24 |
|
- |
|
| 25 |
defined('MOODLE_INTERNAL') || die();
|
- |
|
| 26 |
|
- |
|
| 27 |
// Include the necessary evils.
|
- |
|
| 28 |
global $CFG;
|
- |
|
| 29 |
require_once($CFG->dirroot.'/cache/locallib.php');
|
- |
|
| Línea 30... |
Línea 18... |
| 30 |
require_once($CFG->dirroot.'/cache/tests/fixtures/lib.php');
|
18 |
|
| 31 |
|
19 |
use cache_config_testing;
|
| 32 |
|
20 |
|
| 33 |
/**
|
21 |
/**
|
| 34 |
* PHPunit tests for the cache API and in particular the core_cache\administration_helper
|
22 |
* PHPunit tests for the cache API and in particular the core_cache\administration_helper
|
| 35 |
*
|
23 |
*
|
| 36 |
* @package core_cache
|
24 |
* @package core_cache
|
| - |
|
25 |
* @category test
|
| 37 |
* @category test
|
26 |
* @copyright 2012 Sam Hemelryk
|
| 38 |
* @copyright 2012 Sam Hemelryk
|
27 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
| 39 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
- |
|
| 40 |
*/
|
28 |
* @covers \core_cache\administration_helper
|
| 41 |
class administration_helper_test extends \advanced_testcase {
|
29 |
*/
|
| 42 |
|
30 |
final class administration_helper_test extends \advanced_testcase {
|
| 43 |
/**
|
31 |
/**
|
| 44 |
* Set things back to the default before each test.
|
32 |
* Set things back to the default before each test.
|
| 45 |
*/
|
33 |
*/
|
| 46 |
public function setUp(): void {
|
34 |
public function setUp(): void {
|
| 47 |
parent::setUp();
|
35 |
parent::setUp();
|
| Línea 48... |
Línea 36... |
| 48 |
cache_factory::reset();
|
36 |
factory::reset();
|
| - |
|
37 |
cache_config_testing::create_default_configuration();
|
| - |
|
38 |
}
|
| - |
|
39 |
|
| - |
|
40 |
/**
|
| - |
|
41 |
* Require all test dependencies not auto-loadable.
|
| - |
|
42 |
*/
|
| - |
|
43 |
public static function setUpBeforeClass(): void {
|
| - |
|
44 |
global $CFG;
|
| - |
|
45 |
|
| - |
|
46 |
parent::setUpBeforeClass();
|
| 49 |
cache_config_testing::create_default_configuration();
|
47 |
require_once($CFG->dirroot . '/cache/tests/fixtures/lib.php');
|
| 50 |
}
|
48 |
}
|
| 51 |
|
49 |
|
| 52 |
/**
|
50 |
/**
|
| 53 |
* Final task is to reset the cache system
|
51 |
* Final task is to reset the cache system
|
| 54 |
*/
|
52 |
*/
|
| Línea 55... |
Línea 53... |
| 55 |
public static function tearDownAfterClass(): void {
|
53 |
public static function tearDownAfterClass(): void {
|
| 56 |
parent::tearDownAfterClass();
|
54 |
parent::tearDownAfterClass();
|
| 57 |
cache_factory::reset();
|
55 |
factory::reset();
|
| 58 |
}
|
56 |
}
|
| 59 |
|
57 |
|
| 60 |
/**
|
58 |
/**
|
| 61 |
* Test the numerous summaries the helper can produce.
|
59 |
* Test the numerous summaries the helper can produce.
|
| 62 |
*/
|
60 |
*/
|
| 63 |
public function test_get_summaries(): void {
|
61 |
public function test_get_summaries(): void {
|
| 64 |
// First the preparation.
|
62 |
// First the preparation.
|
| 65 |
$config = cache_config_writer::instance();
|
63 |
$config = config_writer::instance();
|
| 66 |
$this->assertTrue($config->add_store_instance('summariesstore', 'file'));
|
64 |
$this->assertTrue($config->add_store_instance('summariesstore', 'file'));
|
| 67 |
$config->set_definition_mappings('core/eventinvalidation', array('summariesstore'));
|
65 |
$config->set_definition_mappings('core/eventinvalidation', ['summariesstore']);
|
| Línea 68... |
Línea 66... |
| 68 |
$this->assertTrue($config->set_mode_mappings(array(
|
66 |
$this->assertTrue($config->set_mode_mappings([
|
| 69 |
cache_store::MODE_APPLICATION => array('summariesstore'),
|
67 |
store::MODE_APPLICATION => ['summariesstore'],
|
| 70 |
cache_store::MODE_SESSION => array('default_session'),
|
68 |
store::MODE_SESSION => ['default_session'],
|
| 71 |
cache_store::MODE_REQUEST => array('default_request'),
|
69 |
store::MODE_REQUEST => ['default_request'],
|
| 72 |
)));
|
70 |
]));
|
| 73 |
|
71 |
|
| 74 |
$storesummaries = administration_helper::get_store_instance_summaries();
|
72 |
$storesummaries = administration_helper::get_store_instance_summaries();
|
| 75 |
$this->assertIsArray($storesummaries);
|
73 |
$this->assertIsArray($storesummaries);
|
| 76 |
$this->assertArrayHasKey('summariesstore', $storesummaries);
|
74 |
$this->assertArrayHasKey('summariesstore', $storesummaries);
|
| 77 |
$summary = $storesummaries['summariesstore'];
|
75 |
$summary = $storesummaries['summariesstore'];
|
| 78 |
// Check the keys
|
76 |
// Check the keys.
|
| 79 |
$this->assertArrayHasKey('name', $summary);
|
77 |
$this->assertArrayHasKey('name', $summary);
|
| 80 |
$this->assertArrayHasKey('plugin', $summary);
|
78 |
$this->assertArrayHasKey('plugin', $summary);
|
| 81 |
$this->assertArrayHasKey('default', $summary);
|
79 |
$this->assertArrayHasKey('default', $summary);
|
| 82 |
$this->assertArrayHasKey('isready', $summary);
|
80 |
$this->assertArrayHasKey('isready', $summary);
|
| 83 |
$this->assertArrayHasKey('requirementsmet', $summary);
|
81 |
$this->assertArrayHasKey('requirementsmet', $summary);
|
| 84 |
$this->assertArrayHasKey('mappings', $summary);
|
82 |
$this->assertArrayHasKey('mappings', $summary);
|
| 85 |
$this->assertArrayHasKey('modes', $summary);
|
83 |
$this->assertArrayHasKey('modes', $summary);
|
| 86 |
$this->assertArrayHasKey('supports', $summary);
|
84 |
$this->assertArrayHasKey('supports', $summary);
|
| Línea 87... |
Línea 85... |
| 87 |
// Check the important/known values
|
85 |
// Check the important/known values.
|
| 88 |
$this->assertEquals('summariesstore', $summary['name']);
|
86 |
$this->assertEquals('summariesstore', $summary['name']);
|
| 89 |
$this->assertEquals('file', $summary['plugin']);
|
87 |
$this->assertEquals('file', $summary['plugin']);
|
| 90 |
$this->assertEquals(0, $summary['default']);
|
88 |
$this->assertEquals(0, $summary['default']);
|
| 91 |
$this->assertEquals(1, $summary['isready']);
|
89 |
$this->assertEquals(1, $summary['isready']);
|
| Línea 92... |
Línea 90... |
| 92 |
$this->assertEquals(1, $summary['requirementsmet']);
|
90 |
$this->assertEquals(1, $summary['requirementsmet']);
|
| 93 |
|
91 |
|
| 94 |
// Find the number of mappings to sessionstore.
|
92 |
// Find the number of mappings to sessionstore.
|
| 95 |
$mappingcount = count(array_filter($config->get_definitions(), function($element) {
|
93 |
$mappingcount = count(array_filter($config->get_definitions(), function ($element) {
|
| 96 |
return $element['mode'] === cache_store::MODE_APPLICATION;
|
94 |
return $element['mode'] === store::MODE_APPLICATION;
|
| 97 |
}));
|
95 |
}));
|
| 98 |
$this->assertEquals($mappingcount, $summary['mappings']);
|
96 |
$this->assertEquals($mappingcount, $summary['mappings']);
|
| 99 |
|
97 |
|
| 100 |
$definitionsummaries = administration_helper::get_definition_summaries();
|
98 |
$definitionsummaries = administration_helper::get_definition_summaries();
|
| 101 |
$this->assertIsArray($definitionsummaries);
|
99 |
$this->assertIsArray($definitionsummaries);
|
| 102 |
$this->assertArrayHasKey('core/eventinvalidation', $definitionsummaries);
|
100 |
$this->assertArrayHasKey('core/eventinvalidation', $definitionsummaries);
|
| 103 |
$summary = $definitionsummaries['core/eventinvalidation'];
|
101 |
$summary = $definitionsummaries['core/eventinvalidation'];
|
| 104 |
// Check the keys
|
102 |
// Check the keys.
|
| 105 |
$this->assertArrayHasKey('id', $summary);
|
103 |
$this->assertArrayHasKey('id', $summary);
|
| 106 |
$this->assertArrayHasKey('name', $summary);
|
104 |
$this->assertArrayHasKey('name', $summary);
|
| 107 |
$this->assertArrayHasKey('mode', $summary);
|
105 |
$this->assertArrayHasKey('mode', $summary);
|
| 108 |
$this->assertArrayHasKey('component', $summary);
|
106 |
$this->assertArrayHasKey('component', $summary);
|
| 109 |
$this->assertArrayHasKey('area', $summary);
|
107 |
$this->assertArrayHasKey('area', $summary);
|
| 110 |
$this->assertArrayHasKey('mappings', $summary);
|
108 |
$this->assertArrayHasKey('mappings', $summary);
|
| Línea 111... |
Línea 109... |
| 111 |
// Check the important/known values
|
109 |
// Check the important/known values.
|
| 112 |
$this->assertEquals('core/eventinvalidation', $summary['id']);
|
110 |
$this->assertEquals('core/eventinvalidation', $summary['id']);
|
| 113 |
$this->assertInstanceOf('lang_string', $summary['name']);
|
111 |
$this->assertInstanceOf('lang_string', $summary['name']);
|
| 114 |
$this->assertEquals(cache_store::MODE_APPLICATION, $summary['mode']);
|
112 |
$this->assertEquals(store::MODE_APPLICATION, $summary['mode']);
|
| 115 |
$this->assertEquals('core', $summary['component']);
|
113 |
$this->assertEquals('core', $summary['component']);
|
| 116 |
$this->assertEquals('eventinvalidation', $summary['area']);
|
114 |
$this->assertEquals('eventinvalidation', $summary['area']);
|
| 117 |
$this->assertIsArray($summary['mappings']);
|
115 |
$this->assertIsArray($summary['mappings']);
|
| 118 |
$this->assertContains('summariesstore', $summary['mappings']);
|
116 |
$this->assertContains('summariesstore', $summary['mappings']);
|
| 119 |
|
117 |
|
| 120 |
$pluginsummaries = administration_helper::get_store_plugin_summaries();
|
118 |
$pluginsummaries = administration_helper::get_store_plugin_summaries();
|
| Línea 134... |
Línea 132... |
| 134 |
$this->assertTrue(count($locksummaries) > 0);
|
132 |
$this->assertTrue(count($locksummaries) > 0);
|
| Línea 135... |
Línea 133... |
| 135 |
|
133 |
|
| 136 |
$mappings = administration_helper::get_default_mode_stores();
|
134 |
$mappings = administration_helper::get_default_mode_stores();
|
| 137 |
$this->assertIsArray($mappings);
|
135 |
$this->assertIsArray($mappings);
|
| 138 |
$this->assertCount(3, $mappings);
|
136 |
$this->assertCount(3, $mappings);
|
| 139 |
$this->assertArrayHasKey(cache_store::MODE_APPLICATION, $mappings);
|
137 |
$this->assertArrayHasKey(store::MODE_APPLICATION, $mappings);
|
| 140 |
$this->assertIsArray($mappings[cache_store::MODE_APPLICATION]);
|
138 |
$this->assertIsArray($mappings[store::MODE_APPLICATION]);
|
| Línea 141... |
Línea 139... |
| 141 |
$this->assertContains('summariesstore', $mappings[cache_store::MODE_APPLICATION]);
|
139 |
$this->assertContains('summariesstore', $mappings[store::MODE_APPLICATION]);
|
| 142 |
|
140 |
|
| 143 |
$potentials = administration_helper::get_definition_store_options('core', 'eventinvalidation');
|
141 |
$potentials = administration_helper::get_definition_store_options('core', 'eventinvalidation');
|
| 144 |
$this->assertIsArray($potentials); // Currently used, suitable, default
|
142 |
$this->assertIsArray($potentials); // Currently used, suitable, default.
|
| 145 |
$this->assertCount(3, $potentials);
|
143 |
$this->assertCount(3, $potentials);
|
| 146 |
$this->assertArrayHasKey('summariesstore', $potentials[0]);
|
144 |
$this->assertArrayHasKey('summariesstore', $potentials[0]);
|
| 147 |
$this->assertArrayHasKey('summariesstore', $potentials[1]);
|
145 |
$this->assertArrayHasKey('summariesstore', $potentials[1]);
|
| Línea 148... |
Línea 146... |
| 148 |
$this->assertArrayHasKey('default_application', $potentials[1]);
|
146 |
$this->assertArrayHasKey('default_application', $potentials[1]);
|
| 149 |
}
|
147 |
}
|
| 150 |
|
148 |
|
| 151 |
/**
|
149 |
/**
|
| 152 |
* Test instantiating an add store form.
|
150 |
* Test instantiating an add store form.
|
| 153 |
*/
|
151 |
*/
|
| Línea 154... |
Línea 152... |
| 154 |
public function test_get_add_store_form(): void {
|
152 |
public function test_get_add_store_form(): void {
|
| 155 |
$form = cache_factory::get_administration_display_helper()->get_add_store_form('file');
|
153 |
$form = factory::get_administration_display_helper()->get_add_store_form('file');
|
| 156 |
$this->assertInstanceOf('moodleform', $form);
|
154 |
$this->assertInstanceOf('moodleform', $form);
|
| 157 |
|
155 |
|
| 158 |
try {
|
156 |
try {
|
| 159 |
$form = cache_factory::get_administration_display_helper()->get_add_store_form('somethingstupid');
|
157 |
$form = factory::get_administration_display_helper()->get_add_store_form('somethingstupid');
|
| 160 |
$this->fail('You should not be able to create an add form for a store plugin that does not exist.');
|
158 |
$this->fail('You should not be able to create an add form for a store plugin that does not exist.');
|
| Línea 161... |
Línea 159... |
| 161 |
} catch (\moodle_exception $e) {
|
159 |
} catch (\moodle_exception $e) {
|
| 162 |
$this->assertInstanceOf('coding_exception', $e, 'Needs to be: ' .get_class($e)." ::: ".$e->getMessage());
|
160 |
$this->assertInstanceOf('coding_exception', $e, 'Needs to be: ' . get_class($e) . " ::: " . $e->getMessage());
|
| 163 |
}
|
161 |
}
|
| 164 |
}
|
162 |
}
|
| 165 |
|
163 |
|
| 166 |
/**
|
164 |
/**
|
| 167 |
* Test instantiating a form to edit a store instance.
|
165 |
* Test instantiating a form to edit a store instance.
|
| 168 |
*/
|
166 |
*/
|
| Línea 169... |
Línea 167... |
| 169 |
public function test_get_edit_store_form(): void {
|
167 |
public function test_get_edit_store_form(): void {
|
| 170 |
// Always instantiate a new core display helper here.
|
168 |
// Always instantiate a new core display helper here.
|
| Línea 195... |
Línea 193... |
| 195 |
*/
|
193 |
*/
|
| 196 |
public function test_hash_key(): void {
|
194 |
public function test_hash_key(): void {
|
| 197 |
$this->resetAfterTest();
|
195 |
$this->resetAfterTest();
|
| 198 |
set_debugging(DEBUG_ALL);
|
196 |
set_debugging(DEBUG_ALL);
|
| Línea 199... |
Línea 197... |
| 199 |
|
197 |
|
| 200 |
// First with simplekeys
|
198 |
// First with simplekeys.
|
| 201 |
$instance = cache_config_testing::instance(true);
|
199 |
$instance = cache_config_testing::instance(true);
|
| 202 |
$instance->phpunit_add_definition('phpunit/hashtest', array(
|
200 |
$instance->phpunit_add_definition('phpunit/hashtest', [
|
| 203 |
'mode' => cache_store::MODE_APPLICATION,
|
201 |
'mode' => store::MODE_APPLICATION,
|
| 204 |
'component' => 'phpunit',
|
202 |
'component' => 'phpunit',
|
| 205 |
'area' => 'hashtest',
|
203 |
'area' => 'hashtest',
|
| 206 |
'simplekeys' => true
|
204 |
'simplekeys' => true,
|
| 207 |
));
|
205 |
]);
|
| 208 |
$factory = cache_factory::instance();
|
206 |
$factory = factory::instance();
|
| Línea 209... |
Línea 207... |
| 209 |
$definition = $factory->create_definition('phpunit', 'hashtest');
|
207 |
$definition = $factory->create_definition('phpunit', 'hashtest');
|
| 210 |
|
208 |
|
| Línea 211... |
Línea 209... |
| 211 |
$result = cache_helper::hash_key('test', $definition);
|
209 |
$result = helper::hash_key('test', $definition);
|
| 212 |
$this->assertEquals('test-'.$definition->generate_single_key_prefix(), $result);
|
210 |
$this->assertEquals('test-' . $definition->generate_single_key_prefix(), $result);
|
| 213 |
|
211 |
|
| 214 |
try {
|
212 |
try {
|
| 215 |
cache_helper::hash_key('test/test', $definition);
|
213 |
helper::hash_key('test/test', $definition);
|
| 216 |
$this->fail('Invalid key was allowed, you should see this.');
|
214 |
$this->fail('Invalid key was allowed, you should see this.');
|
| Línea 217... |
Línea 215... |
| 217 |
} catch (\coding_exception $e) {
|
215 |
} catch (\coding_exception $e) {
|
| 218 |
$this->assertEquals('test/test', $e->debuginfo);
|
216 |
$this->assertEquals('test/test', $e->debuginfo);
|
| 219 |
}
|
217 |
}
|
| 220 |
|
218 |
|
| 221 |
// Second without simple keys
|
219 |
// Second without simple keys.
|
| 222 |
$instance->phpunit_add_definition('phpunit/hashtest2', array(
|
220 |
$instance->phpunit_add_definition('phpunit/hashtest2', [
|
| 223 |
'mode' => cache_store::MODE_APPLICATION,
|
221 |
'mode' => store::MODE_APPLICATION,
|
| 224 |
'component' => 'phpunit',
|
222 |
'component' => 'phpunit',
|
| Línea 225... |
Línea 223... |
| 225 |
'area' => 'hashtest2',
|
223 |
'area' => 'hashtest2',
|
| 226 |
'simplekeys' => false
|
224 |
'simplekeys' => false,
|
| Línea 227... |
Línea 225... |
| 227 |
));
|
225 |
]);
|
| 228 |
$definition = $factory->create_definition('phpunit', 'hashtest2');
|
226 |
$definition = $factory->create_definition('phpunit', 'hashtest2');
|
| 229 |
|
227 |
|
| Línea 230... |
Línea 228... |
| 230 |
$result = cache_helper::hash_key('test', $definition);
|
228 |
$result = helper::hash_key('test', $definition);
|
| 231 |
$this->assertEquals(sha1($definition->generate_single_key_prefix().'-test'), $result);
|
229 |
$this->assertEquals(sha1($definition->generate_single_key_prefix() . '-test'), $result);
|
| 232 |
|
230 |
|
| 233 |
$result = cache_helper::hash_key('test/test', $definition);
|
231 |
$result = helper::hash_key('test/test', $definition);
|
| 234 |
$this->assertEquals(sha1($definition->generate_single_key_prefix().'-test/test'), $result);
|
232 |
$this->assertEquals(sha1($definition->generate_single_key_prefix() . '-test/test'), $result);
|
| 235 |
}
|
233 |
}
|
| 236 |
|
234 |
|
| 237 |
/**
|
235 |
/**
|
| 238 |
* Tests the get_usage function.
|
236 |
* Tests the get_usage function.
|
| 239 |
*/
|
237 |
*/
|
| 240 |
public function test_get_usage(): void {
|
238 |
public function test_get_usage(): void {
|
| 241 |
// Create a test cache definition and put items in it.
|
239 |
// Create a test cache definition and put items in it.
|
| 242 |
$instance = cache_config_testing::instance(true);
|
240 |
$instance = cache_config_testing::instance(true);
|
| 243 |
$instance->phpunit_add_definition('phpunit/test', [
|
241 |
$instance->phpunit_add_definition('phpunit/test', [
|
| 244 |
'mode' => cache_store::MODE_APPLICATION,
|
242 |
'mode' => store::MODE_APPLICATION,
|
| 245 |
'component' => 'phpunit',
|
243 |
'component' => 'phpunit',
|
| Línea 246... |
Línea 244... |
| 246 |
'area' => 'test',
|
244 |
'area' => 'test',
|
| 247 |
'simplekeys' => true
|
245 |
'simplekeys' => true,
|
| Línea 248... |
Línea 246... |
| 248 |
]);
|
246 |
]);
|
| 249 |
$cache = \cache::make('phpunit', 'test');
|
247 |
$cache = \cache::make('phpunit', 'test');
|
| 250 |
for ($i = 0; $i < 100; $i++) {
|
248 |
for ($i = 0; $i < 100; $i++) {
|