95c70de7515c5f3ad4310048f9786d99b1a1dd0e
[moodle.git] / cache / tests / administration_helper_test.php
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/>.
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  */
29 defined('MOODLE_INTERNAL') || die();
31 // Include the necessary evils.
32 global $CFG;
33 require_once($CFG->dirroot.'/cache/locallib.php');
34 require_once($CFG->dirroot.'/cache/tests/fixtures/lib.php');
37 /**
38  * PHPunit tests for the cache API and in particular the cache_administration_helper
39  *
40  * @copyright  2012 Sam Hemelryk
41  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42  */
43 class core_cache_administration_helper_testcase extends advanced_testcase {
45     /**
46      * Set things back to the default before each test.
47      */
48     public function setUp() {
49         parent::setUp();
50         cache_factory::reset();
51         cache_config_testing::create_default_configuration();
52     }
54     /**
55      * Final task is to reset the cache system
56      */
57     public static function tearDownAfterClass() {
58         parent::tearDownAfterClass();
59         cache_factory::reset();
60     }
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         )));
76         $storesummaries = cache_administration_helper::get_store_instance_summaries();
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']);
95         $this->assertEquals(1, $summary['mappings']);
97         $definitionsummaries = cache_administration_helper::get_definition_summaries();
98         $this->assertInternalType('array', $definitionsummaries);
99         $this->assertArrayHasKey('core/eventinvalidation', $definitionsummaries);
100         $summary = $definitionsummaries['core/eventinvalidation'];
101         // Check the keys
102         $this->assertArrayHasKey('id', $summary);
103         $this->assertArrayHasKey('name', $summary);
104         $this->assertArrayHasKey('mode', $summary);
105         $this->assertArrayHasKey('component', $summary);
106         $this->assertArrayHasKey('area', $summary);
107         $this->assertArrayHasKey('mappings', $summary);
108         // Check the important/known values
109         $this->assertEquals('core/eventinvalidation', $summary['id']);
110         $this->assertInstanceOf('lang_string', $summary['name']);
111         $this->assertEquals(cache_store::MODE_APPLICATION, $summary['mode']);
112         $this->assertEquals('core', $summary['component']);
113         $this->assertEquals('eventinvalidation', $summary['area']);
114         $this->assertInternalType('array', $summary['mappings']);
115         $this->assertContains('summariesstore', $summary['mappings']);
117         $pluginsummaries = cache_administration_helper::get_store_plugin_summaries();
118         $this->assertInternalType('array', $pluginsummaries);
119         $this->assertArrayHasKey('file', $pluginsummaries);
120         $summary = $pluginsummaries['file'];
121         // Check the keys
122         $this->assertArrayHasKey('name', $summary);
123         $this->assertArrayHasKey('requirementsmet', $summary);
124         $this->assertArrayHasKey('instances', $summary);
125         $this->assertArrayHasKey('modes', $summary);
126         $this->assertArrayHasKey('supports', $summary);
127         $this->assertArrayHasKey('canaddinstance', $summary);
129         $locksummaries = cache_administration_helper::get_lock_summaries();
130         $this->assertInternalType('array', $locksummaries);
131         $this->assertTrue(count($locksummaries) > 0);
133         $mappings = cache_administration_helper::get_default_mode_stores();
134         $this->assertInternalType('array', $mappings);
135         $this->assertCount(3, $mappings);
136         $this->assertArrayHasKey(cache_store::MODE_APPLICATION, $mappings);
137         $this->assertInternalType('array', $mappings[cache_store::MODE_APPLICATION]);
138         $this->assertContains('summariesstore', $mappings[cache_store::MODE_APPLICATION]);
140         $potentials = cache_administration_helper::get_definition_store_options('core', 'eventinvalidation');
141         $this->assertInternalType('array', $potentials); // Currently used, suitable, default
142         $this->assertCount(3, $potentials);
143         $this->assertArrayHasKey('summariesstore', $potentials[0]);
144         $this->assertArrayHasKey('summariesstore', $potentials[1]);
145         $this->assertArrayHasKey('default_application', $potentials[1]);
146     }
148     /**
149      * Test instantiating an add store form.
150      */
151     public function test_get_add_store_form() {
152         $form = cache_administration_helper::get_add_store_form('file');
153         $this->assertInstanceOf('moodleform', $form);
155         try {
156             $form = cache_administration_helper::get_add_store_form('somethingstupid');
157             $this->fail('You should not be able to create an add form for a store plugin that does not exist.');
158         } catch (moodle_exception $e) {
159             $this->assertInstanceOf('coding_exception', $e, 'Needs to be: ' .get_class($e)." ::: ".$e->getMessage());
160         }
161     }
163     /**
164      * Test instantiating a form to edit a store instance.
165      */
166     public function test_get_edit_store_form() {
167         $config = cache_config_writer::instance();
168         $this->assertTrue($config->add_store_instance('test_get_edit_store_form', 'file'));
170         $form = cache_administration_helper::get_edit_store_form('file', 'test_get_edit_store_form');
171         $this->assertInstanceOf('moodleform', $form);
173         try {
174             $form = cache_administration_helper::get_edit_store_form('somethingstupid', 'moron');
175             $this->fail('You should not be able to create an edit form for a store plugin that does not exist.');
176         } catch (moodle_exception $e) {
177             $this->assertInstanceOf('coding_exception', $e);
178         }
180         try {
181             $form = cache_administration_helper::get_edit_store_form('file', 'blisters');
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     }
188     /**
189      * Test the hash_key functionality.
190      */
191     public function test_hash_key() {
192         $this->resetAfterTest();
193         set_debugging(DEBUG_ALL);
195         // First with simplekeys
196         $instance = cache_config_testing::instance(true);
197         $instance->phpunit_add_definition('phpunit/hashtest', array(
198             'mode' => cache_store::MODE_APPLICATION,
199             'component' => 'phpunit',
200             'area' => 'hashtest',
201             'simplekeys' => true
202         ));
203         $factory = cache_factory::instance();
204         $definition = $factory->create_definition('phpunit', 'hashtest');
206         $result = cache_helper::hash_key('test', $definition);
207         $this->assertEquals('test-'.$definition->generate_single_key_prefix(), $result);
209         try {
210             cache_helper::hash_key('test/test', $definition);
211             $this->fail('Invalid key was allowed, you should see this.');
212         } catch (coding_exception $e) {
213             $this->assertEquals('test/test', $e->debuginfo);
214         }
216         // Second without simple keys
217         $instance->phpunit_add_definition('phpunit/hashtest2', array(
218             'mode' => cache_store::MODE_APPLICATION,
219             'component' => 'phpunit',
220             'area' => 'hashtest2',
221             'simplekeys' => false
222         ));
223         $definition = $factory->create_definition('phpunit', 'hashtest2');
225         $result = cache_helper::hash_key('test', $definition);
226         $this->assertEquals(sha1($definition->generate_single_key_prefix().'-test'), $result);
228         $result = cache_helper::hash_key('test/test', $definition);
229         $this->assertEquals(sha1($definition->generate_single_key_prefix().'-test/test'), $result);
230     }