2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 use cache_config_testing
;
20 use cache_config_writer
;
25 defined('MOODLE_INTERNAL') ||
die();
27 // Include the necessary evils.
29 require_once($CFG->dirroot
.'/cache/locallib.php');
30 require_once($CFG->dirroot
.'/cache/tests/fixtures/lib.php');
34 * PHPunit tests for the cache API and in particular the core_cache\administration_helper
38 * @copyright 2012 Sam Hemelryk
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 class administration_helper_test
extends \advanced_testcase
{
44 * Set things back to the default before each test.
46 public function setUp(): void
{
48 cache_factory
::reset();
49 cache_config_testing
::create_default_configuration();
53 * Final task is to reset the cache system
55 public static function tearDownAfterClass(): void
{
56 parent
::tearDownAfterClass();
57 cache_factory
::reset();
61 * Test the numerous summaries the helper can produce.
63 public function test_get_summaries() {
64 // First the preparation.
65 $config = cache_config_writer
::instance();
66 $this->assertTrue($config->add_store_instance('summariesstore', 'file'));
67 $config->set_definition_mappings('core/eventinvalidation', array('summariesstore'));
68 $this->assertTrue($config->set_mode_mappings(array(
69 cache_store
::MODE_APPLICATION
=> array('summariesstore'),
70 cache_store
::MODE_SESSION
=> array('default_session'),
71 cache_store
::MODE_REQUEST
=> array('default_request'),
74 $storesummaries = administration_helper
::get_store_instance_summaries();
75 $this->assertIsArray($storesummaries);
76 $this->assertArrayHasKey('summariesstore', $storesummaries);
77 $summary = $storesummaries['summariesstore'];
79 $this->assertArrayHasKey('name', $summary);
80 $this->assertArrayHasKey('plugin', $summary);
81 $this->assertArrayHasKey('default', $summary);
82 $this->assertArrayHasKey('isready', $summary);
83 $this->assertArrayHasKey('requirementsmet', $summary);
84 $this->assertArrayHasKey('mappings', $summary);
85 $this->assertArrayHasKey('modes', $summary);
86 $this->assertArrayHasKey('supports', $summary);
87 // Check the important/known values
88 $this->assertEquals('summariesstore', $summary['name']);
89 $this->assertEquals('file', $summary['plugin']);
90 $this->assertEquals(0, $summary['default']);
91 $this->assertEquals(1, $summary['isready']);
92 $this->assertEquals(1, $summary['requirementsmet']);
94 // Find the number of mappings to sessionstore.
95 $mappingcount = count(array_filter($config->get_definitions(), function($element) {
96 return $element['mode'] === cache_store
::MODE_APPLICATION
;
98 $this->assertEquals($mappingcount, $summary['mappings']);
100 $definitionsummaries = administration_helper
::get_definition_summaries();
101 $this->assertIsArray($definitionsummaries);
102 $this->assertArrayHasKey('core/eventinvalidation', $definitionsummaries);
103 $summary = $definitionsummaries['core/eventinvalidation'];
105 $this->assertArrayHasKey('id', $summary);
106 $this->assertArrayHasKey('name', $summary);
107 $this->assertArrayHasKey('mode', $summary);
108 $this->assertArrayHasKey('component', $summary);
109 $this->assertArrayHasKey('area', $summary);
110 $this->assertArrayHasKey('mappings', $summary);
111 // Check the important/known values
112 $this->assertEquals('core/eventinvalidation', $summary['id']);
113 $this->assertInstanceOf('lang_string', $summary['name']);
114 $this->assertEquals(cache_store
::MODE_APPLICATION
, $summary['mode']);
115 $this->assertEquals('core', $summary['component']);
116 $this->assertEquals('eventinvalidation', $summary['area']);
117 $this->assertIsArray($summary['mappings']);
118 $this->assertContains('summariesstore', $summary['mappings']);
120 $pluginsummaries = administration_helper
::get_store_plugin_summaries();
121 $this->assertIsArray($pluginsummaries);
122 $this->assertArrayHasKey('file', $pluginsummaries);
123 $summary = $pluginsummaries['file'];
125 $this->assertArrayHasKey('name', $summary);
126 $this->assertArrayHasKey('requirementsmet', $summary);
127 $this->assertArrayHasKey('instances', $summary);
128 $this->assertArrayHasKey('modes', $summary);
129 $this->assertArrayHasKey('supports', $summary);
130 $this->assertArrayHasKey('canaddinstance', $summary);
132 $locksummaries = administration_helper
::get_lock_summaries();
133 $this->assertIsArray($locksummaries);
134 $this->assertTrue(count($locksummaries) > 0);
136 $mappings = administration_helper
::get_default_mode_stores();
137 $this->assertIsArray($mappings);
138 $this->assertCount(3, $mappings);
139 $this->assertArrayHasKey(cache_store
::MODE_APPLICATION
, $mappings);
140 $this->assertIsArray($mappings[cache_store
::MODE_APPLICATION
]);
141 $this->assertContains('summariesstore', $mappings[cache_store
::MODE_APPLICATION
]);
143 $potentials = administration_helper
::get_definition_store_options('core', 'eventinvalidation');
144 $this->assertIsArray($potentials); // Currently used, suitable, default
145 $this->assertCount(3, $potentials);
146 $this->assertArrayHasKey('summariesstore', $potentials[0]);
147 $this->assertArrayHasKey('summariesstore', $potentials[1]);
148 $this->assertArrayHasKey('default_application', $potentials[1]);
152 * Test instantiating an add store form.
154 public function test_get_add_store_form() {
155 $form = cache_factory
::get_administration_display_helper()->get_add_store_form('file');
156 $this->assertInstanceOf('moodleform', $form);
159 $form = cache_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.');
161 } catch (\moodle_exception
$e) {
162 $this->assertInstanceOf('coding_exception', $e, 'Needs to be: ' .get_class($e)." ::: ".$e->getMessage());
167 * Test instantiating a form to edit a store instance.
169 public function test_get_edit_store_form() {
170 // Always instantiate a new core display helper here.
171 $administrationhelper = new local\administration_display_helper
;
172 $config = cache_config_writer
::instance();
173 $this->assertTrue($config->add_store_instance('test_get_edit_store_form', 'file'));
175 $form = $administrationhelper->get_edit_store_form('file', 'test_get_edit_store_form');
176 $this->assertInstanceOf('moodleform', $form);
179 $form = $administrationhelper->get_edit_store_form('somethingstupid', 'moron');
180 $this->fail('You should not be able to create an edit form for a store plugin that does not exist.');
181 } catch (\moodle_exception
$e) {
182 $this->assertInstanceOf('coding_exception', $e);
186 $form = $administrationhelper->get_edit_store_form('file', 'blisters');
187 $this->fail('You should not be able to create an edit form for a store plugin that does not exist.');
188 } catch (\moodle_exception
$e) {
189 $this->assertInstanceOf('coding_exception', $e);
194 * Test the hash_key functionality.
196 public function test_hash_key() {
197 $this->resetAfterTest();
198 set_debugging(DEBUG_ALL
);
200 // First with simplekeys
201 $instance = cache_config_testing
::instance(true);
202 $instance->phpunit_add_definition('phpunit/hashtest', array(
203 'mode' => cache_store
::MODE_APPLICATION
,
204 'component' => 'phpunit',
205 'area' => 'hashtest',
208 $factory = cache_factory
::instance();
209 $definition = $factory->create_definition('phpunit', 'hashtest');
211 $result = cache_helper
::hash_key('test', $definition);
212 $this->assertEquals('test-'.$definition->generate_single_key_prefix(), $result);
215 cache_helper
::hash_key('test/test', $definition);
216 $this->fail('Invalid key was allowed, you should see this.');
217 } catch (\coding_exception
$e) {
218 $this->assertEquals('test/test', $e->debuginfo
);
221 // Second without simple keys
222 $instance->phpunit_add_definition('phpunit/hashtest2', array(
223 'mode' => cache_store
::MODE_APPLICATION
,
224 'component' => 'phpunit',
225 'area' => 'hashtest2',
226 'simplekeys' => false
228 $definition = $factory->create_definition('phpunit', 'hashtest2');
230 $result = cache_helper
::hash_key('test', $definition);
231 $this->assertEquals(sha1($definition->generate_single_key_prefix().'-test'), $result);
233 $result = cache_helper
::hash_key('test/test', $definition);
234 $this->assertEquals(sha1($definition->generate_single_key_prefix().'-test/test'), $result);
238 * Tests the get_usage function.
240 public function test_get_usage(): void
{
241 // Create a test cache definition and put items in it.
242 $instance = cache_config_testing
::instance(true);
243 $instance->phpunit_add_definition('phpunit/test', [
244 'mode' => cache_store
::MODE_APPLICATION
,
245 'component' => 'phpunit',
249 $cache = \cache
::make('phpunit', 'test');
250 for ($i = 0; $i < 100; $i++
) {
251 $cache->set('key' . $i, str_repeat('x', $i));
254 $factory = cache_factory
::instance();
255 $adminhelper = $factory->get_administration_display_helper();
257 $usage = $adminhelper->get_usage(10)['phpunit/test'];
258 $this->assertEquals('phpunit/test', $usage->cacheid
);
259 $this->assertCount(1, $usage->stores
);
260 $store = $usage->stores
[0];
261 $this->assertEquals('default_application', $store->name
);
262 $this->assertEquals('cachestore_file', $store->class);
263 $this->assertEquals(true, $store->supported
);
264 $this->assertEquals(100, $store->items
);
266 // As file store checks all items, the values should be exact.
267 $this->assertEqualsWithDelta(57.4, $store->mean
, 0.1);
268 $this->assertEqualsWithDelta(29.0, $store->sd
, 0.1);
269 $this->assertEquals(0, $store->margin
);