From 045fe95c6310e9aee3e6840c66756f4a6c7f400c Mon Sep 17 00:00:00 2001 From: Sam Hemelryk Date: Tue, 7 May 2013 15:11:33 +1200 Subject: [PATCH] MDL-39525 cache: fixed purging definitions requiring identifiers --- cache/admin.php | 10 +++++++- cache/classes/definition.php | 9 +++++++ cache/tests/cache_test.php | 47 ++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/cache/admin.php b/cache/admin.php index c395c2f642c..38aea5799ed 100644 --- a/cache/admin.php +++ b/cache/admin.php @@ -210,7 +210,15 @@ if (!empty($action) && confirm_sesskey()) { case 'purgedefinition': // Purge a specific definition. $definition = required_param('definition', PARAM_SAFEPATH); list($component, $area) = explode('/', $definition, 2); - cache_helper::purge_by_definition($component, $area); + $factory = cache_factory::instance(); + $definition = $factory->create_definition($component, $area); + if ($definition->has_required_identifiers()) { + // We will have to purge the stores used by this definition. + cache_helper::purge_stores_used_by_definition($component, $area); + } else { + // Alrighty we can purge just the data belonging to this definition. + cache_helper::purge_by_definition($component, $area); + } redirect($PAGE->url, get_string('purgedefinitionsuccess', 'cache'), 5); break; diff --git a/cache/classes/definition.php b/cache/classes/definition.php index d4b3dc5d82a..1b19fb2f2ed 100644 --- a/cache/classes/definition.php +++ b/cache/classes/definition.php @@ -907,4 +907,13 @@ class cache_definition { } return join('/', $identifiers); } + + /** + * Returns true if this definition requires identifiers. + * + * @param bool + */ + public function has_required_identifiers() { + return (count($this->requireidentifiers) > 0); + } } \ No newline at end of file diff --git a/cache/tests/cache_test.php b/cache/tests/cache_test.php index cff510dde1f..5293eeccc73 100644 --- a/cache/tests/cache_test.php +++ b/cache/tests/cache_test.php @@ -1327,4 +1327,51 @@ class cache_phpunit_tests extends advanced_testcase { $this->assertInstanceOf('cache_application', $cache); $this->assertFalse($cache->get('test')); } + + /** + * Test purge routines. + */ + public function test_purge_routines() { + $instance = cache_config_phpunittest::instance(true); + $instance->phpunit_add_definition('phpunit/purge1', array( + 'mode' => cache_store::MODE_APPLICATION, + 'component' => 'phpunit', + 'area' => 'purge1' + )); + $instance->phpunit_add_definition('phpunit/purge2', array( + 'mode' => cache_store::MODE_APPLICATION, + 'component' => 'phpunit', + 'area' => 'purge2', + 'requireidentifiers' => array( + 'id' + ) + )); + + $factory = cache_factory::instance(); + $definition = $factory->create_definition('phpunit', 'purge1'); + $this->assertFalse($definition->has_required_identifiers()); + $cache = $factory->create_cache($definition); + $this->assertInstanceOf('cache_application', $cache); + $this->assertTrue($cache->set('test', 'test')); + $this->assertTrue($cache->has('test')); + cache_helper::purge_by_definition('phpunit', 'purge1'); + $this->assertFalse($cache->has('test')); + + $factory = cache_factory::instance(); + $definition = $factory->create_definition('phpunit', 'purge2'); + $this->assertTrue($definition->has_required_identifiers()); + $cache = $factory->create_cache($definition); + $this->assertInstanceOf('cache_application', $cache); + $this->assertTrue($cache->set('test', 'test')); + $this->assertTrue($cache->has('test')); + cache_helper::purge_stores_used_by_definition('phpunit', 'purge2'); + $this->assertFalse($cache->has('test')); + + try { + cache_helper::purge_by_definition('phpunit', 'purge2'); + $this->fail('Should not be able to purge a definition required identifiers without providing them.'); + } catch (coding_exception $ex) { + $this->assertContains('Identifier required for cache has not been provided', $ex->getMessage()); + } + } }