/** * 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); } }
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; }
/** * 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); } }
/** * 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; }
/** * 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; }
/** * 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; }
/** * 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; }
/** * 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; }
/** * 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; }