Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  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');
  35  
  36  
  37  /**
  38   * PHPunit tests for the cache API and in particular the core_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 {
  44  
  45      /**
  46       * Set things back to the default before each test.
  47       */
  48      public function setUp(): void {
  49          parent::setUp();
  50          cache_factory::reset();
  51          cache_config_testing::create_default_configuration();
  52      }
  53  
  54      /**
  55       * Final task is to reset the cache system
  56       */
  57      public static function tearDownAfterClass(): void {
  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  
  76          $storesummaries = core_cache\administration_helper::get_store_instance_summaries();
  77          $this->assertIsArray($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  
  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']);
 101  
 102          $definitionsummaries = core_cache\administration_helper::get_definition_summaries();
 103          $this->assertIsArray($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->assertIsArray($summary['mappings']);
 120          $this->assertContains('summariesstore', $summary['mappings']);
 121  
 122          $pluginsummaries = core_cache\administration_helper::get_store_plugin_summaries();
 123          $this->assertIsArray($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  
 134          $locksummaries = core_cache\administration_helper::get_lock_summaries();
 135          $this->assertIsArray($locksummaries);
 136          $this->assertTrue(count($locksummaries) > 0);
 137  
 138          $mappings = core_cache\administration_helper::get_default_mode_stores();
 139          $this->assertIsArray($mappings);
 140          $this->assertCount(3, $mappings);
 141          $this->assertArrayHasKey(cache_store::MODE_APPLICATION, $mappings);
 142          $this->assertIsArray($mappings[cache_store::MODE_APPLICATION]);
 143          $this->assertContains('summariesstore', $mappings[cache_store::MODE_APPLICATION]);
 144  
 145          $potentials = core_cache\administration_helper::get_definition_store_options('core', 'eventinvalidation');
 146          $this->assertIsArray($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() {
 157          $form = cache_factory::get_administration_display_helper()->get_add_store_form('file');
 158          $this->assertInstanceOf('moodleform', $form);
 159  
 160          try {
 161              $form = cache_factory::get_administration_display_helper()->get_add_store_form('somethingstupid');
 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() {
 172          // Always instantiate a new core display helper here.
 173          $administrationhelper = new core_cache\local\administration_display_helper;
 174          $config = cache_config_writer::instance();
 175          $this->assertTrue($config->add_store_instance('test_get_edit_store_form', 'file'));
 176  
 177          $form = $administrationhelper->get_edit_store_form('file', 'test_get_edit_store_form');
 178          $this->assertInstanceOf('moodleform', $form);
 179  
 180          try {
 181              $form = $administrationhelper->get_edit_store_form('somethingstupid', 'moron');
 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 {
 188              $form = $administrationhelper->get_edit_store_form('file', 'blisters');
 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() {
 199          $this->resetAfterTest();
 200          set_debugging(DEBUG_ALL);
 201  
 202          // First with simplekeys
 203          $instance = cache_config_testing::instance(true);
 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);
 237      }
 238  }