/**
  * Tests cache configuration
  */
 public function test_cache_config()
 {
     global $CFG;
     if (defined('TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH') && TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH && !empty($CFG->altcacheconfigpath)) {
         // We need to skip this test - it checks the default config structure, but very likely we arn't using the
         // default config structure here so theres no point in running the test.
         $this->markTestSkipped('Skipped testing default cache config structure as alt cache path is being used.');
     }
     if (defined('TEST_CACHE_USING_APPLICATION_STORE')) {
         // We need to skip this test - it checks the default config structure, but very likely we arn't using the
         // default config structure here because we are testing against an alternative application store.
         $this->markTestSkipped('Skipped testing default cache config structure as alt application store is being used.');
     }
     $instance = cache_config::instance();
     $this->assertInstanceOf('cache_config_phpunittest', $instance);
     $this->assertTrue(cache_config_phpunittest::config_file_exists());
     $stores = $instance->get_all_stores();
     $this->assertCount(3, $stores);
     foreach ($stores as $name => $store) {
         // Check its an array.
         $this->assertInternalType('array', $store);
         // Check the name is the key.
         $this->assertEquals($name, $store['name']);
         // Check that it has been declared default.
         $this->assertTrue($store['default']);
         // Required attributes = name + plugin + configuration + modes + features.
         $this->assertArrayHasKey('name', $store);
         $this->assertArrayHasKey('plugin', $store);
         $this->assertArrayHasKey('configuration', $store);
         $this->assertArrayHasKey('modes', $store);
         $this->assertArrayHasKey('features', $store);
     }
     $modemappings = $instance->get_mode_mappings();
     $this->assertCount(3, $modemappings);
     $modes = array(cache_store::MODE_APPLICATION => false, cache_store::MODE_SESSION => false, cache_store::MODE_REQUEST => false);
     foreach ($modemappings as $mapping) {
         // We expect 3 properties.
         $this->assertCount(3, $mapping);
         // Required attributes = mode + store.
         $this->assertArrayHasKey('mode', $mapping);
         $this->assertArrayHasKey('store', $mapping);
         // Record the mode.
         $modes[$mapping['mode']] = true;
     }
     // Must have the default 3 modes and no more.
     $this->assertCount(3, $mapping);
     foreach ($modes as $mode) {
         $this->assertTrue($mode);
     }
     $definitions = $instance->get_definitions();
     // The event invalidation definition is required for the cache API and must be there.
     $this->assertArrayHasKey('core/eventinvalidation', $definitions);
     $definitionmappings = $instance->get_definition_mappings();
     foreach ($definitionmappings as $mapping) {
         // Required attributes = definition + store.
         $this->assertArrayHasKey('definition', $mapping);
         $this->assertArrayHasKey('store', $mapping);
     }
 }
Example #2
0
 public function is_uninstall_allowed()
 {
     $instance = \cache_config::instance();
     foreach ($instance->get_all_stores() as $store) {
         if ($store['plugin'] == $this->name) {
             return false;
         }
     }
     return true;
 }
Example #3
0
 /**
  * Tests cache configuration
  */
 public function test_cache_config()
 {
     $instance = cache_config::instance();
     $this->assertInstanceOf('cache_config_phpunittest', $instance);
     $this->assertTrue(cache_config_phpunittest::config_file_exists());
     $stores = $instance->get_all_stores();
     $this->assertCount(3, $stores);
     foreach ($stores as $name => $store) {
         // Check its an array.
         $this->assertInternalType('array', $store);
         // Check the name is the key.
         $this->assertEquals($name, $store['name']);
         // Check that it has been declared default.
         $this->assertTrue($store['default']);
         // Required attributes = name + plugin + configuration + modes + features.
         $this->assertArrayHasKey('name', $store);
         $this->assertArrayHasKey('plugin', $store);
         $this->assertArrayHasKey('configuration', $store);
         $this->assertArrayHasKey('modes', $store);
         $this->assertArrayHasKey('features', $store);
     }
     $modemappings = $instance->get_mode_mappings();
     $this->assertCount(3, $modemappings);
     $modes = array(cache_store::MODE_APPLICATION => false, cache_store::MODE_SESSION => false, cache_store::MODE_REQUEST => false);
     foreach ($modemappings as $mapping) {
         // We expect 3 properties.
         $this->assertCount(3, $mapping);
         // Required attributes = mode + store.
         $this->assertArrayHasKey('mode', $mapping);
         $this->assertArrayHasKey('store', $mapping);
         // Record the mode.
         $modes[$mapping['mode']] = true;
     }
     // Must have the default 3 modes and no more.
     $this->assertCount(3, $mapping);
     foreach ($modes as $mode) {
         $this->assertTrue($mode);
     }
     $definitions = $instance->get_definitions();
     // The event invalidation definition is required for the cache API and must be there.
     $this->assertArrayHasKey('core/eventinvalidation', $definitions);
     $definitionmappings = $instance->get_definition_mappings();
     foreach ($definitionmappings as $mapping) {
         // Required attributes = definition + store.
         $this->assertArrayHasKey('definition', $mapping);
         $this->assertArrayHasKey('store', $mapping);
     }
 }
Example #4
0
 /**
  * Purges a store given its name.
  *
  * @param string $storename
  * @return bool
  */
 public static function purge_store($storename)
 {
     $config = cache_config::instance();
     $stores = $config->get_all_stores();
     if (!array_key_exists($storename, $stores)) {
         // The store does not exist.
         return false;
     }
     $store = $stores[$storename];
     $class = $store['class'];
     // Found the store: is it ready?
     $instance = new $class($store['name'], $store['configuration']);
     if (!$instance->is_ready()) {
         unset($instance);
         return false;
     }
     foreach ($config->get_definitions_by_store($storename) as $id => $definition) {
         $definition = cache_definition::load($id, $definition);
         $instance = new $class($store['name'], $store['configuration']);
         $instance->initialise($definition);
         $instance->purge();
         unset($instance);
     }
     return true;
 }
Example #5
0
 /**
  * Creates a cache config instance with the ability to write if required.
  *
  * @param bool $writer If set to true an instance that can update the configuration will be returned.
  * @return cache_config|cache_config_writer
  */
 public function create_config_instance($writer = false)
 {
     global $CFG;
     // Check if we need to create a config file with defaults.
     $needtocreate = !cache_config::config_file_exists();
     // The class to use.
     $class = 'cache_config';
     if ($writer || $needtocreate) {
         require_once $CFG->dirroot . '/cache/locallib.php';
         $class .= '_writer';
     }
     // Check if this is a PHPUnit test and redirect to the phpunit config classes if it is.
     if (defined('PHPUNIT_TEST') && PHPUNIT_TEST) {
         require_once $CFG->dirroot . '/cache/locallib.php';
         require_once $CFG->dirroot . '/cache/tests/fixtures/lib.php';
         // We have just a single class for PHP unit tests. We don't care enough about its
         // performance to do otherwise and having a single method allows us to inject things into it
         // while testing.
         $class = 'cache_config_phpunittest';
     }
     $error = false;
     if ($needtocreate) {
         // Create the default configuration.
         // Update the state, we are now initialising the cache.
         self::set_state(self::STATE_INITIALISING);
         $configuration = $class::create_default_configuration();
         if ($configuration !== true) {
             // Failed to create the default configuration. Disable the cache stores and update the state.
             self::set_state(self::STATE_ERROR_INITIALISING);
             $this->configs[$class] = new $class();
             $this->configs[$class]->load($configuration);
             $error = true;
         }
     }
     if (!array_key_exists($class, $this->configs)) {
         // Create a new instance and call it to load it.
         $this->configs[$class] = new $class();
         $this->configs[$class]->load();
     }
     if (!$error) {
         // The cache is now ready to use. Update the state.
         self::set_state(self::STATE_READY);
     }
     // Return the instance.
     return $this->configs[$class];
 }
Example #6
0
 /**
  * Returns the definitions mapped into the given store name.
  *
  * @param string $storename
  * @return array Associative array of definitions, id=>definition
  */
 public static function get_definitions_by_store($storename)
 {
     $definitions = array();
     $config = cache_config::instance();
     $stores = $config->get_all_stores();
     if (!array_key_exists($storename, $stores)) {
         // The store does not exist.
         return false;
     }
     $defmappings = $config->get_definition_mappings();
     // Create an associative array for the definition mappings.
     $thedefmappings = array();
     foreach ($defmappings as $defmapping) {
         $thedefmappings[$defmapping['definition']] = $defmapping;
     }
     // Search for matches in default mappings.
     $defs = $config->get_definitions();
     foreach ($config->get_mode_mappings() as $modemapping) {
         if ($modemapping['store'] !== $storename) {
             continue;
         }
         foreach ($defs as $id => $definition) {
             if ($definition['mode'] !== $modemapping['mode']) {
                 continue;
             }
             // Exclude custom definitions mapping: they will be managed few lines below.
             if (array_key_exists($id, $thedefmappings)) {
                 continue;
             }
             $definitions[$id] = $definition;
         }
     }
     // Search for matches in the custom definitions mapping
     foreach ($defmappings as $defmapping) {
         if ($defmapping['store'] !== $storename) {
             continue;
         }
         $definition = $config->get_definition_by_id($defmapping['definition']);
         if ($definition) {
             $definitions[$defmapping['definition']] = $definition;
         }
     }
     return $definitions;
 }
 /**
  * Returns an array summarising the locks available in the system
  */
 public static function get_lock_summaries()
 {
     $locks = array();
     $instance = cache_config::instance();
     $stores = $instance->get_all_stores();
     foreach ($instance->get_locks() as $lock) {
         $default = !empty($lock['default']);
         if ($default) {
             $name = new lang_string($lock['name'], 'cache');
         } else {
             $name = $lock['name'];
         }
         $uses = 0;
         foreach ($stores as $store) {
             if (!empty($store['lock']) && $store['lock'] === $lock['name']) {
                 $uses++;
             }
         }
         $lockdata = array('name' => $name, 'default' => $default, 'uses' => $uses, 'type' => get_string('pluginname', $lock['type']));
         $locks[$lock['name']] = $lockdata;
     }
     return $locks;
 }
Example #8
0
 /**
  * Purges a store given its name.
  *
  * @param string $storename
  * @param cache_config $config
  * @return bool
  */
 public static function purge_store($storename, cache_config $config = null)
 {
     if ($config === null) {
         $config = cache_config::instance();
     }
     $stores = $config->get_all_stores();
     if (!array_key_exists($storename, $stores)) {
         // The store does not exist.
         return false;
     }
     $store = $stores[$storename];
     $class = $store['class'];
     // Found the store: is it ready?
     /* @var cache_store $instance */
     $instance = new $class($store['name'], $store['configuration']);
     // We check are_requirements_met although we expect is_ready is going to check as well.
     if (!$instance::are_requirements_met() || !$instance->is_ready()) {
         unset($instance);
         return false;
     }
     foreach ($config->get_definitions_by_store($storename) as $id => $definition) {
         $definition = cache_definition::load($id, $definition);
         $definitioninstance = clone $instance;
         $definitioninstance->initialise($definition);
         $definitioninstance->purge();
         unset($definitioninstance);
     }
     return true;
 }
Example #9
0
 /**
  * Validates this form.
  *
  * @param array $data
  * @param array $files
  * @return array
  */
 public final function validation($data, $files)
 {
     $errors = parent::validation($data, $files);
     if (!isset($errors['name'])) {
         $config = cache_config::instance();
         if (in_array($data['name'], array_keys($config->get_locks()))) {
             $errors['name'] = get_string('locknamenotunique', 'cache');
         }
     }
     $errors = $this->plugin_validation($data, $files, $errors);
     return $errors;
 }
Example #10
0
 /**
  * Purges a store given its name.
  *
  * @param string $storename
  * @return bool
  */
 public static function purge_store($storename)
 {
     $config = cache_config::instance();
     foreach ($config->get_all_stores() as $store) {
         if ($store['name'] !== $storename) {
             continue;
         }
         $class = $store['class'];
         $instance = new $class($store['name'], $store['configuration']);
         if (!$instance->is_ready()) {
             continue;
         }
         $definition = cache_definition::load_adhoc(cache_store::MODE_REQUEST, 'core', 'cache_purge');
         $instance->initialise($definition);
         $instance->purge();
         return true;
     }
     return false;
 }
Example #11
0
 /**
  * Creates a cache config instance with the ability to write if required.
  *
  * @param bool $writer Unused.
  * @return cache_config_disabled|cache_config_writer
  */
 public function create_config_instance($writer = false)
 {
     // We are always going to use the cache_config_disabled class for all regular request.
     // However if the code has requested the writer then likely something is changing and
     // we're going to need to interact with the config.php file.
     // In this case we will still use the cache_config_writer.
     $class = 'cache_config_disabled';
     if ($writer) {
         // If the writer was requested then something is changing.
         $class = 'cache_config_writer';
     }
     if (!array_key_exists($class, $this->configs)) {
         self::set_state(self::STATE_INITIALISING);
         if ($class === 'cache_config_disabled') {
             $configuration = $class::create_default_configuration();
         } else {
             $configuration = false;
             if (!cache_config::config_file_exists()) {
                 cache_config_writer::create_default_configuration(true);
             }
         }
         $this->configs[$class] = new $class();
         $this->configs[$class]->load($configuration);
     }
     self::set_state(self::STATE_READY);
     // Return the instance.
     return $this->configs[$class];
 }
Example #12
0
 /**
  * Returns the definitions mapped into the given store name.
  *
  * @param string $storename
  * @return array Associative array of definitions, id=>definition
  */
 public function get_definitions_by_store($storename)
 {
     $definitions = array();
     // This function was accidentally made static at some stage in the past.
     // It was converted to an instance method but to be backwards compatible
     // we must step around this in code.
     if (!isset($this)) {
         $config = cache_config::instance();
     } else {
         $config = $this;
     }
     $stores = $config->get_all_stores();
     if (!array_key_exists($storename, $stores)) {
         // The store does not exist.
         return false;
     }
     $defmappings = $config->get_definition_mappings();
     // Create an associative array for the definition mappings.
     $thedefmappings = array();
     foreach ($defmappings as $defmapping) {
         $thedefmappings[$defmapping['definition']] = $defmapping;
     }
     // Search for matches in default mappings.
     $defs = $config->get_definitions();
     foreach ($config->get_mode_mappings() as $modemapping) {
         if ($modemapping['store'] !== $storename) {
             continue;
         }
         foreach ($defs as $id => $definition) {
             if ($definition['mode'] !== $modemapping['mode']) {
                 continue;
             }
             // Exclude custom definitions mapping: they will be managed few lines below.
             if (array_key_exists($id, $thedefmappings)) {
                 continue;
             }
             $definitions[$id] = $definition;
         }
     }
     // Search for matches in the custom definitions mapping
     foreach ($defmappings as $defmapping) {
         if ($defmapping['store'] !== $storename) {
             continue;
         }
         $definition = $config->get_definition_by_id($defmapping['definition']);
         if ($definition) {
             $definitions[$defmapping['definition']] = $definition;
         }
     }
     return $definitions;
 }
Example #13
0
 /**
  * Creates a cache config instance with the ability to write if required.
  *
  * @param bool $writer If set to true an instance that can update the configuration will be returned.
  * @return cache_config|cache_config_writer
  */
 public function create_config_instance($writer = false)
 {
     global $CFG;
     // Check if we need to create a config file with defaults.
     $needtocreate = !cache_config::config_file_exists();
     // The class to use.
     $class = 'cache_config';
     if ($writer || $needtocreate) {
         require_once $CFG->dirroot . '/cache/locallib.php';
         $class .= '_writer';
     }
     // Check if this is a PHPUnit test and redirect to the phpunit config classes if it is.
     if (defined('PHPUNIT_TEST') && PHPUNIT_TEST) {
         require_once $CFG->dirroot . '/cache/locallib.php';
         require_once $CFG->dirroot . '/cache/tests/fixtures/lib.php';
         // We have just a single class for PHP unit tests. We don't care enough about its
         // performance to do otherwise and having a single method allows us to inject things into it
         // while testing.
         $class = 'cache_config_phpunittest';
     }
     if ($needtocreate) {
         // Create the default configuration.
         $class::create_default_configuration();
     }
     if (!array_key_exists($class, $this->configs)) {
         // Create a new instance and call it to load it.
         $this->configs[$class] = new $class();
         $this->configs[$class]->load();
     }
     // Return the instance.
     return $this->configs[$class];
 }