mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2025-07-30 19:00:10 +02:00
[1.7.0] Add versioning to serializer cache
- Make some AttrDef member-variables lazy-loading to save serialization space, clean up others - Refactor get*Definition() methods git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1116 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
@@ -53,7 +53,12 @@ class HTMLPurifier_DefinitionCache_SerializerTest extends HTMLPurifier_Definitio
|
||||
$cache = new HTMLPurifier_DefinitionCache_Serializer('Test');
|
||||
|
||||
$config_array = array('Foo' => 'Bar');
|
||||
$config_md5 = md5(serialize($config_array));
|
||||
|
||||
$config = $this->generateConfigMock($config_array);
|
||||
$config->version = '1.0.0';
|
||||
$config->revision = 2;
|
||||
|
||||
$config_md5 = '1.0.0-' . $config->revision . '-' . md5(serialize($config_array));
|
||||
|
||||
$file = realpath(
|
||||
$rel_file = dirname(__FILE__) .
|
||||
@@ -62,7 +67,6 @@ class HTMLPurifier_DefinitionCache_SerializerTest extends HTMLPurifier_Definitio
|
||||
);
|
||||
if($file && file_exists($file)) unlink($file); // prevent previous failures from causing problems
|
||||
|
||||
$config = $this->generateConfigMock($config_array);
|
||||
$this->assertIdentical($config_md5, $cache->generateKey($config));
|
||||
|
||||
$def_original = $this->generateDefinition();
|
||||
@@ -150,6 +154,34 @@ class HTMLPurifier_DefinitionCache_SerializerTest extends HTMLPurifier_Definitio
|
||||
|
||||
}
|
||||
|
||||
function testCleanup() {
|
||||
|
||||
$cache = new HTMLPurifier_DefinitionCache_Serializer('Test');
|
||||
|
||||
// in order of age, oldest first
|
||||
// note that configurations are all identical, but version/revision
|
||||
// are different
|
||||
|
||||
$config1 = $this->generateConfigMock();
|
||||
$config1->version = '0.9.0';
|
||||
$config1->revision = 574;
|
||||
$def1 = $this->generateDefinition(array('info' => 1));
|
||||
|
||||
$config2 = $this->generateConfigMock();
|
||||
$config2->version = '1.0.0beta';
|
||||
$config2->revision = 1;
|
||||
$def2 = $this->generateDefinition(array('info' => 3));
|
||||
|
||||
$cache->set($def1, $config1);
|
||||
$cache->cleanup($config1);
|
||||
$this->assertEqual($def1, $cache->get($config1)); // no change
|
||||
|
||||
$cache->cleanup($config2);
|
||||
$this->assertFalse($cache->get($config1));
|
||||
$this->assertFalse($cache->get($config2));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that a file exists, ignoring the stat cache
|
||||
*/
|
||||
|
@@ -8,10 +8,12 @@ class HTMLPurifier_DefinitionCacheHarness extends UnitTestCase
|
||||
* to a getBatch() call
|
||||
* @param $values Values to return when getBatch is invoked
|
||||
*/
|
||||
function generateConfigMock($values) {
|
||||
function generateConfigMock($values = array()) {
|
||||
generate_mock_once('HTMLPurifier_Config');
|
||||
$config = new HTMLPurifier_ConfigMock($this);
|
||||
$config->setReturnValue('getBatch', $values, array('Test'));
|
||||
$config->version = '1.0.0';
|
||||
$config->revision = 1;
|
||||
return $config;
|
||||
}
|
||||
|
||||
|
@@ -9,6 +9,22 @@ class HTMLPurifier_DefinitionCacheTest extends UnitTestCase
|
||||
$cache = HTMLPurifier_DefinitionCache::create('Test', $config);
|
||||
$this->assertEqual($cache, new HTMLPurifier_DefinitionCache_Serializer('Test'));
|
||||
}
|
||||
|
||||
function test_isOld() {
|
||||
$cache = new HTMLPurifier_DefinitionCache('Test'); // non-functional
|
||||
|
||||
$config = HTMLPurifier_Config::createDefault();
|
||||
$config->version = '1.0.0';
|
||||
$config->revision = 10;
|
||||
|
||||
$this->assertIdentical($cache->isOld('1.0.0-10-hashstuffhere', $config), false);
|
||||
$this->assertIdentical($cache->isOld('1.5.0-1-hashstuffhere', $config), false);
|
||||
|
||||
$this->assertIdentical($cache->isOld('0.9.0-1-hashstuffhere', $config), true);
|
||||
$this->assertIdentical($cache->isOld('1.0.0-1-hashstuffhere', $config), true);
|
||||
$this->assertIdentical($cache->isOld('1.0.0beta-11-hashstuffhere', $config), true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
Reference in New Issue
Block a user