Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401]

   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  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');
  30  require_once($CFG->dirroot.'/cache/tests/fixtures/lib.php');
  31  
  32  
  33  /**
  34   * PHPunit tests for the cache API and in particular the core_cache\administration_helper
  35   *
  36   * @package    core_cache
  37   * @category   test
  38   * @copyright  2012 Sam Hemelryk
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class administration_helper_test extends \advanced_testcase {
  42  
  43      /**
  44       * Set things back to the default before each test.
  45       */
  46      public function setUp(): void {
  47          parent::setUp();
  48          cache_factory::reset();
  49          cache_config_testing::create_default_configuration();
  50      }
  51  
  52      /**
  53       * Final task is to reset the cache system
  54       */
  55      public static function tearDownAfterClass(): void {
  56          parent::tearDownAfterClass();
  57          cache_factory::reset();
  58      }
  59  
  60      /**
  61       * Test the numerous summaries the helper can produce.
  62       */
  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'),
  72          )));
  73  
  74          $storesummaries = administration_helper::get_store_instance_summaries();
  75          $this->assertIsArray($storesummaries);
  76          $this->assertArrayHasKey('summariesstore', $storesummaries);
  77          $summary = $storesummaries['summariesstore'];
  78          // Check the keys
  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']);
  93  
  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;
  97          }));
  98          $this->assertEquals($mappingcount, $summary['mappings']);
  99  
 100          $definitionsummaries = administration_helper::get_definition_summaries();
 101          $this->assertIsArray($definitionsummaries);
 102          $this->assertArrayHasKey('core/eventinvalidation', $definitionsummaries);
 103          $summary = $definitionsummaries['core/eventinvalidation'];
 104          // Check the keys
 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']);
 119  
 120          $pluginsummaries = administration_helper::get_store_plugin_summaries();
 121          $this->assertIsArray($pluginsummaries);
 122          $this->assertArrayHasKey('file', $pluginsummaries);
 123          $summary = $pluginsummaries['file'];
 124          // Check the keys
 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);
 131  
 132          $locksummaries = administration_helper::get_lock_summaries();
 133          $this->assertIsArray($locksummaries);
 134          $this->assertTrue(count($locksummaries) > 0);
 135  
 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]);
 142  
 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]);
 149      }
 150  
 151      /**
 152       * Test instantiating an add store form.
 153       */
 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);
 157  
 158          try {
 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());
 163          }
 164      }
 165  
 166      /**
 167       * Test instantiating a form to edit a store instance.
 168       */
 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'));
 174  
 175          $form = $administrationhelper->get_edit_store_form('file', 'test_get_edit_store_form');
 176          $this->assertInstanceOf('moodleform', $form);
 177  
 178          try {
 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);
 183          }
 184  
 185          try {
 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);
 190          }
 191      }
 192  
 193      /**
 194       * Test the hash_key functionality.
 195       */
 196      public function test_hash_key() {
 197          $this->resetAfterTest();
 198          set_debugging(DEBUG_ALL);
 199  
 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',
 206              'simplekeys' => true
 207          ));
 208          $factory = cache_factory::instance();
 209          $definition = $factory->create_definition('phpunit', 'hashtest');
 210  
 211          $result = cache_helper::hash_key('test', $definition);
 212          $this->assertEquals('test-'.$definition->generate_single_key_prefix(), $result);
 213  
 214          try {
 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);
 219          }
 220  
 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
 227          ));
 228          $definition = $factory->create_definition('phpunit', 'hashtest2');
 229  
 230          $result = cache_helper::hash_key('test', $definition);
 231          $this->assertEquals(sha1($definition->generate_single_key_prefix().'-test'), $result);
 232  
 233          $result = cache_helper::hash_key('test/test', $definition);
 234          $this->assertEquals(sha1($definition->generate_single_key_prefix().'-test/test'), $result);
 235      }
 236  
 237      /**
 238       * Tests the get_usage function.
 239       */
 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',
 246                  'area' => 'test',
 247                  'simplekeys' => true
 248          ]);
 249          $cache = \cache::make('phpunit', 'test');
 250          for ($i = 0; $i < 100; $i++) {
 251              $cache->set('key' . $i, str_repeat('x', $i));
 252          }
 253  
 254          $factory = cache_factory::instance();
 255          $adminhelper = $factory->get_administration_display_helper();
 256  
 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);
 265  
 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);
 270      }
 271  }