MDL-68440 cache: Added counting of default mappings
[moodle.git] / cache / tests / administration_helper_test.php
CommitLineData
8252b7c2
PS
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/**
18 * PHPunit tests for the cache API and in particular things in locallib.php
19 *
20 * This file is part of Moodle's cache API, affectionately called MUC.
21 * It contains the components that are requried in order to use caching.
22 *
23 * @package core
24 * @category cache
25 * @copyright 2012 Sam Hemelryk
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 */
28
29defined('MOODLE_INTERNAL') || die();
30
31// Include the necessary evils.
32global $CFG;
33require_once($CFG->dirroot.'/cache/locallib.php');
34require_once($CFG->dirroot.'/cache/tests/fixtures/lib.php');
35
36
37/**
40e41c19 38 * PHPunit tests for the cache API and in particular the core_cache\administration_helper
8252b7c2
PS
39 *
40 * @copyright 2012 Sam Hemelryk
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 */
43class core_cache_administration_helper_testcase extends advanced_testcase {
44
45 /**
46 * Set things back to the default before each test.
47 */
48 public function setUp() {
49 parent::setUp();
50 cache_factory::reset();
427c41ef 51 cache_config_testing::create_default_configuration();
8252b7c2
PS
52 }
53
54 /**
55 * Final task is to reset the cache system
56 */
57 public static function tearDownAfterClass() {
58 parent::tearDownAfterClass();
59 cache_factory::reset();
60 }
61
62 /**
63 * Test the numerous summaries the helper can produce.
64 */
65 public function test_get_summaries() {
66 // First the preparation.
67 $config = cache_config_writer::instance();
68 $this->assertTrue($config->add_store_instance('summariesstore', 'file'));
69 $config->set_definition_mappings('core/eventinvalidation', array('summariesstore'));
70 $this->assertTrue($config->set_mode_mappings(array(
71 cache_store::MODE_APPLICATION => array('summariesstore'),
72 cache_store::MODE_SESSION => array('default_session'),
73 cache_store::MODE_REQUEST => array('default_request'),
74 )));
75
40e41c19 76 $storesummaries = core_cache\administration_helper::get_store_instance_summaries();
8252b7c2
PS
77 $this->assertInternalType('array', $storesummaries);
78 $this->assertArrayHasKey('summariesstore', $storesummaries);
79 $summary = $storesummaries['summariesstore'];
80 // Check the keys
81 $this->assertArrayHasKey('name', $summary);
82 $this->assertArrayHasKey('plugin', $summary);
83 $this->assertArrayHasKey('default', $summary);
84 $this->assertArrayHasKey('isready', $summary);
85 $this->assertArrayHasKey('requirementsmet', $summary);
86 $this->assertArrayHasKey('mappings', $summary);
87 $this->assertArrayHasKey('modes', $summary);
88 $this->assertArrayHasKey('supports', $summary);
89 // Check the important/known values
90 $this->assertEquals('summariesstore', $summary['name']);
91 $this->assertEquals('file', $summary['plugin']);
92 $this->assertEquals(0, $summary['default']);
93 $this->assertEquals(1, $summary['isready']);
94 $this->assertEquals(1, $summary['requirementsmet']);
0628d113
PB
95
96 // Find the number of mappings to sessionstore.
97 $mappingcount = count(array_filter($config->get_definitions(), function($element) {
98 return $element['mode'] === cache_store::MODE_APPLICATION;
99 }));
100 $this->assertEquals($mappingcount, $summary['mappings']);
8252b7c2 101
40e41c19 102 $definitionsummaries = core_cache\administration_helper::get_definition_summaries();
8252b7c2
PS
103 $this->assertInternalType('array', $definitionsummaries);
104 $this->assertArrayHasKey('core/eventinvalidation', $definitionsummaries);
105 $summary = $definitionsummaries['core/eventinvalidation'];
106 // Check the keys
107 $this->assertArrayHasKey('id', $summary);
108 $this->assertArrayHasKey('name', $summary);
109 $this->assertArrayHasKey('mode', $summary);
110 $this->assertArrayHasKey('component', $summary);
111 $this->assertArrayHasKey('area', $summary);
112 $this->assertArrayHasKey('mappings', $summary);
113 // Check the important/known values
114 $this->assertEquals('core/eventinvalidation', $summary['id']);
115 $this->assertInstanceOf('lang_string', $summary['name']);
116 $this->assertEquals(cache_store::MODE_APPLICATION, $summary['mode']);
117 $this->assertEquals('core', $summary['component']);
118 $this->assertEquals('eventinvalidation', $summary['area']);
119 $this->assertInternalType('array', $summary['mappings']);
120 $this->assertContains('summariesstore', $summary['mappings']);
121
40e41c19 122 $pluginsummaries = core_cache\administration_helper::get_store_plugin_summaries();
8252b7c2
PS
123 $this->assertInternalType('array', $pluginsummaries);
124 $this->assertArrayHasKey('file', $pluginsummaries);
125 $summary = $pluginsummaries['file'];
126 // Check the keys
127 $this->assertArrayHasKey('name', $summary);
128 $this->assertArrayHasKey('requirementsmet', $summary);
129 $this->assertArrayHasKey('instances', $summary);
130 $this->assertArrayHasKey('modes', $summary);
131 $this->assertArrayHasKey('supports', $summary);
132 $this->assertArrayHasKey('canaddinstance', $summary);
133
40e41c19 134 $locksummaries = core_cache\administration_helper::get_lock_summaries();
8252b7c2
PS
135 $this->assertInternalType('array', $locksummaries);
136 $this->assertTrue(count($locksummaries) > 0);
137
40e41c19 138 $mappings = core_cache\administration_helper::get_default_mode_stores();
8252b7c2
PS
139 $this->assertInternalType('array', $mappings);
140 $this->assertCount(3, $mappings);
141 $this->assertArrayHasKey(cache_store::MODE_APPLICATION, $mappings);
142 $this->assertInternalType('array', $mappings[cache_store::MODE_APPLICATION]);
143 $this->assertContains('summariesstore', $mappings[cache_store::MODE_APPLICATION]);
144
40e41c19 145 $potentials = core_cache\administration_helper::get_definition_store_options('core', 'eventinvalidation');
8252b7c2
PS
146 $this->assertInternalType('array', $potentials); // Currently used, suitable, default
147 $this->assertCount(3, $potentials);
148 $this->assertArrayHasKey('summariesstore', $potentials[0]);
149 $this->assertArrayHasKey('summariesstore', $potentials[1]);
150 $this->assertArrayHasKey('default_application', $potentials[1]);
151 }
152
153 /**
154 * Test instantiating an add store form.
155 */
156 public function test_get_add_store_form() {
40e41c19 157 $form = cache_factory::get_administration_display_helper()->get_add_store_form('file');
8252b7c2
PS
158 $this->assertInstanceOf('moodleform', $form);
159
160 try {
40e41c19 161 $form = cache_factory::get_administration_display_helper()->get_add_store_form('somethingstupid');
8252b7c2
PS
162 $this->fail('You should not be able to create an add form for a store plugin that does not exist.');
163 } catch (moodle_exception $e) {
164 $this->assertInstanceOf('coding_exception', $e, 'Needs to be: ' .get_class($e)." ::: ".$e->getMessage());
165 }
166 }
167
168 /**
169 * Test instantiating a form to edit a store instance.
170 */
171 public function test_get_edit_store_form() {
40e41c19
PB
172 // Always instantiate a new core display helper here.
173 $administrationhelper = new core_cache\local\administration_display_helper;
8252b7c2 174 $config = cache_config_writer::instance();
79a8ea65 175 $this->assertTrue($config->add_store_instance('test_get_edit_store_form', 'file'));
8252b7c2 176
40e41c19 177 $form = $administrationhelper->get_edit_store_form('file', 'test_get_edit_store_form');
8252b7c2
PS
178 $this->assertInstanceOf('moodleform', $form);
179
180 try {
40e41c19 181 $form = $administrationhelper->get_edit_store_form('somethingstupid', 'moron');
8252b7c2
PS
182 $this->fail('You should not be able to create an edit form for a store plugin that does not exist.');
183 } catch (moodle_exception $e) {
184 $this->assertInstanceOf('coding_exception', $e);
185 }
186
187 try {
40e41c19 188 $form = $administrationhelper->get_edit_store_form('file', 'blisters');
8252b7c2
PS
189 $this->fail('You should not be able to create an edit form for a store plugin that does not exist.');
190 } catch (moodle_exception $e) {
191 $this->assertInstanceOf('coding_exception', $e);
192 }
193 }
194
195 /**
196 * Test the hash_key functionality.
197 */
198 public function test_hash_key() {
96f81ea3
PS
199 $this->resetAfterTest();
200 set_debugging(DEBUG_ALL);
8252b7c2
PS
201
202 // First with simplekeys
427c41ef 203 $instance = cache_config_testing::instance(true);
8252b7c2
PS
204 $instance->phpunit_add_definition('phpunit/hashtest', array(
205 'mode' => cache_store::MODE_APPLICATION,
206 'component' => 'phpunit',
207 'area' => 'hashtest',
208 'simplekeys' => true
209 ));
210 $factory = cache_factory::instance();
211 $definition = $factory->create_definition('phpunit', 'hashtest');
212
213 $result = cache_helper::hash_key('test', $definition);
214 $this->assertEquals('test-'.$definition->generate_single_key_prefix(), $result);
215
216 try {
217 cache_helper::hash_key('test/test', $definition);
218 $this->fail('Invalid key was allowed, you should see this.');
219 } catch (coding_exception $e) {
220 $this->assertEquals('test/test', $e->debuginfo);
221 }
222
223 // Second without simple keys
224 $instance->phpunit_add_definition('phpunit/hashtest2', array(
225 'mode' => cache_store::MODE_APPLICATION,
226 'component' => 'phpunit',
227 'area' => 'hashtest2',
228 'simplekeys' => false
229 ));
230 $definition = $factory->create_definition('phpunit', 'hashtest2');
231
232 $result = cache_helper::hash_key('test', $definition);
233 $this->assertEquals(sha1($definition->generate_single_key_prefix().'-test'), $result);
234
235 $result = cache_helper::hash_key('test/test', $definition);
236 $this->assertEquals(sha1($definition->generate_single_key_prefix().'-test/test'), $result);
8252b7c2
PS
237 }
238}