Skip to content

Commit

Permalink
MDL-25290 cache: Added unit tests for admin and fixed up identified i…
Browse files Browse the repository at this point in the history
…ssues
  • Loading branch information
Sam Hemelryk committed Oct 7, 2012
1 parent fd59389 commit 42f2c59
Show file tree
Hide file tree
Showing 3 changed files with 525 additions and 31 deletions.
96 changes: 65 additions & 31 deletions cache/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ public function add_plugin_instance($name, $plugin, array $configuration = array
'configuration' => $configuration,
'features' => $class::get_supported_features($configuration),
'modes' => $class::get_supported_modes($configuration),
'mappingsonly' => !empty($configuration['mappingsonly'])
'mappingsonly' => !empty($configuration['mappingsonly']),
'class' => $class,
'default' => false
);
if (array_key_exists('lock', $configuration)) {
$this->configstores[$name]['lock'] = $configuration['lock'];
Expand Down Expand Up @@ -207,7 +209,7 @@ public function set_mode_mappings(array $modemappings) {
/**
* Edits a give plugin instance.
*
* The plugin instance if determined by its name, hence you cannot rename plugins.
* The plugin instance is determined by its name, hence you cannot rename plugins.
* This function also calls save so you should redirect immediately, or at least very shortly after
* calling this method.
*
Expand All @@ -225,14 +227,14 @@ public function edit_plugin_instance($name, $plugin, $configuration) {
if (!array_key_exists($plugin, $plugins)) {
throw new cache_exception('Invalid plugin name specified. The plugin either does not exist or is not valid.');
}
$class = 'cachestore_.'.$plugin;
$class = 'cachestore_'.$plugin;
$file = $plugins[$plugin];
if (!class_exists($class)) {
if (file_exists($file)) {
require_once($file);
}
if (!class_exists($class)) {
throw new cache_exception('Invalid cache plugin specified. The plugin does not contain the require class.');
throw new cache_exception('Invalid cache plugin specified. The plugin does not contain the required class.'.$class);
}
}
$this->configstores[$name] = array(
Expand All @@ -241,7 +243,9 @@ public function edit_plugin_instance($name, $plugin, $configuration) {
'configuration' => $configuration,
'features' => $class::get_supported_features($configuration),
'modes' => $class::get_supported_modes($configuration),
'mappingsonly' => !empty($configuration['mappingsonly'])
'mappingsonly' => !empty($configuration['mappingsonly']),
'class' => $class,
'default' => $this->configstores[$name]['default'] // Can't change the default.
);
if (array_key_exists('lock', $configuration)) {
$this->configstores[$name]['lock'] = $configuration['lock'];
Expand Down Expand Up @@ -718,7 +722,11 @@ public static function get_plugin_actions($name, array $plugindetails) {
*/
public static function get_add_store_form($plugin) {
global $CFG; // Needed for includes.
$plugindir = get_plugin_directory('cachestore', $plugin);
$plugins = get_plugin_list('cachestore');
if (!array_key_exists($plugin, $plugins)) {
throw new coding_exception('Invalid cache plugin used when trying to create an edit form.');
}
$plugindir = $plugins[$plugin];
$class = 'cachestore_addinstance_form';
if (file_exists($plugindir.'/addinstanceform.php')) {
require_once($plugindir.'/addinstanceform.php');
Expand All @@ -730,29 +738,7 @@ public static function get_add_store_form($plugin) {
}
}

$supportsnativelocking = false;
if (file_exists($plugindir.'/lib.php')) {
require_once($plugindir.'/lib.php');
$pluginclass = 'cachestore_'.$plugin;
if (class_exists($pluginclass)) {
$supportsnativelocking = array_key_exists('cache_is_lockable', class_implements($pluginclass));
}
}

if (!$supportsnativelocking) {
$config = cache_config::instance();
$locks = array();
foreach ($config->get_locks() as $lock => $conf) {
if (!empty($conf['default'])) {
$name = get_string($lock, 'cache');
} else {
$name = $lock;
}
$locks[$lock] = $name;
}
} else {
$locks = false;
}
$locks = self::get_possible_locks_for_plugin($plugindir, $plugin);

$url = new moodle_url('/cache/admin.php', array('action' => 'addstore'));
return new $class($url, array('plugin' => $plugin, 'store' => null, 'locks' => $locks));
Expand All @@ -768,7 +754,17 @@ public static function get_add_store_form($plugin) {
*/
public static function get_edit_store_form($plugin, $store) {
global $CFG; // Needed for includes.
$plugindir = get_plugin_directory('cachestore', $plugin);
$plugins = get_plugin_list('cachestore');
if (!array_key_exists($plugin, $plugins)) {
throw new coding_exception('Invalid cache plugin used when trying to create an edit form.');
}
$factory = cache_factory::instance();
$config = $factory->create_config_instance();
$stores = $config->get_all_stores();
if (!array_key_exists($store, $stores)) {
throw new coding_exception('Invalid store name given when trying to create an edit form.');
}
$plugindir = $plugins[$plugin];
$class = 'cachestore_addinstance_form';
if (file_exists($plugindir.'/addinstanceform.php')) {
require_once($plugindir.'/addinstanceform.php');
Expand All @@ -780,8 +776,46 @@ public static function get_edit_store_form($plugin, $store) {
}
}

$locks = self::get_possible_locks_for_plugin($plugindir, $plugin);

$url = new moodle_url('/cache/admin.php', array('action' => 'editstore'));
return new $class($url, array('plugin' => $plugin, 'store' => $store));
return new $class($url, array('plugin' => $plugin, 'store' => $store, 'locks' => $locks));
}

/**
* Returns an array of suitable lock instances for use with this plugin, or false if the plugin handles locking itself.
*
* @param string $plugindir
* @param string $plugin
* @return array|false
*/
protected static function get_possible_locks_for_plugin($plugindir, $plugin) {
global $CFG; // Needed for includes.
$supportsnativelocking = false;
if (file_exists($plugindir.'/lib.php')) {
require_once($plugindir.'/lib.php');
$pluginclass = 'cachestore_'.$plugin;
if (class_exists($pluginclass)) {
$supportsnativelocking = array_key_exists('cache_is_lockable', class_implements($pluginclass));
}
}

if (!$supportsnativelocking) {
$config = cache_config::instance();
$locks = array();
foreach ($config->get_locks() as $lock => $conf) {
if (!empty($conf['default'])) {
$name = get_string($lock, 'cache');
} else {
$name = $lock;
}
$locks[$lock] = $name;
}
} else {
$locks = false;
}

return $locks;
}

/**
Expand Down
35 changes: 35 additions & 0 deletions cache/tests/cache_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,29 @@ public function test_definition_overridden_loader() {
$this->assertEquals('Test has no value really.', $cache->get('Test'));
}

public function test_definition_ttl() {
$instance = cache_config_phpunittest::instance(true);
$instance->phpunit_add_definition('phpunit/ttltest', array(
'mode' => cache_store::MODE_APPLICATION,
'component' => 'phpunit',
'area' => 'ttltest',
'ttl' => -10
));
$cache = cache::make('phpunit', 'ttltest');
$this->assertInstanceOf('cache_application', $cache);

// Purge it to be sure.
$this->assertTrue($cache->purge());
// It won't be there yet.
$this->assertFalse($cache->has('Test'));
// Set it now.
$this->assertTrue($cache->set('Test', 'Test'));
// Check its not there.
$this->assertFalse($cache->has('Test'));
// Double check by trying to get it.
$this->assertFalse($cache->get('Test'));
}

/**
* Tests manual locking operations on an application cache
*/
Expand Down Expand Up @@ -564,4 +587,16 @@ public function test_application_definition_purge() {
$this->assertFalse($cache->get('testkey1'));
$this->assertFalse($cache->get('testkey2'));
}

/**
* Test the use of an alt path.
* If we can generate a config instance we are done :)
*/
public function test_alt_cache_path() {
global $CFG;
$this->resetAfterTest();
$CFG->altcacheconfigpath = $CFG->dataroot.'/cache/altcacheconfigpath';
$instance = cache_config_phpunittest::instance();
$this->assertInstanceOf('cache_config', $instance);
}
}
Loading

0 comments on commit 42f2c59

Please sign in to comment.