0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-11-09 15:28:40 +00:00

[1.7.0] Implement Cleanup decorator

- Create generic DecoratorHarness
- Name decorators, so that they can be overridden or removed
- Add setup function to definition cache factory

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1118 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2007-05-29 20:49:33 +00:00
parent 002395de09
commit e2a951420f
10 changed files with 161 additions and 26 deletions

1
TODO
View File

@ -13,7 +13,6 @@ TODO List
- Reorganize configuration directives
- Set up anonymous module management by HTMLDefinition (Advanced API)
- Get all AttrTypes into string form
# Clean up HTMLDefinition caching, need easy cache invalidation.
- Parse TinyMCE-style whitelist into our %HTML.Allow* whitelists
1.8 release [Refactor, refactor!]

View File

@ -3,6 +3,7 @@
require_once 'HTMLPurifier/DefinitionCache.php';
require_once 'HTMLPurifier/DefinitionCache/Decorator/Memory.php';
require_once 'HTMLPurifier/DefinitionCache/Decorator/Cleanup.php';
class HTMLPurifier_DefinitionCache_Decorator extends HTMLPurifier_DefinitionCache
{

View File

@ -0,0 +1,45 @@
<?php
require_once 'HTMLPurifier/DefinitionCache/Decorator.php';
/**
* Definition cache decorator class that cleans up the cache
* whenever there is a cache miss.
*/
class HTMLPurifier_DefinitionCache_Decorator_Cleanup extends
HTMLPurifier_DefinitionCache_Decorator
{
var $name = 'Cleanup';
function copy() {
return new HTMLPurifier_DefinitionCache_Decorator_Cleanup();
}
function add($def, $config) {
$status = parent::add($def, $config);
if (!$status) parent::cleanup($config);
return $status;
}
function set($def, $config) {
$status = parent::set($def, $config);
if (!$status) parent::cleanup($config);
return $status;
}
function replace($def, $config) {
$status = parent::replace($def, $config);
if (!$status) parent::cleanup($config);
return $status;
}
function get($config) {
$ret = parent::get($config);
if (!$ret) parent::cleanup($config);
return $ret;
}
}
?>

View File

@ -12,6 +12,7 @@ class HTMLPurifier_DefinitionCache_Decorator_Memory extends
{
var $definitions;
var $name = 'Memory';
function copy() {
return new HTMLPurifier_DefinitionCache_Decorator_Memory();

View File

@ -9,9 +9,10 @@ class HTMLPurifier_DefinitionCache_Decorator_Template extends
HTMLPurifier_DefinitionCache_Decorator
{
// must be defined!!!
var $name = 'Template'; // replace this
function copy() {
// replace class name with yours
return new HTMLPurifier_DefinitionCache_Decorator_Template();
}

View File

@ -24,6 +24,13 @@ class HTMLPurifier_DefinitionCacheFactory
var $caches = array('Serializer' => array());
var $decorators = array();
/**
* Initialize default decorators
*/
function setup() {
$this->addDecorator('Cleanup');
}
/**
* Retrieves an instance of global definition cache factory.
* @static
@ -34,6 +41,7 @@ class HTMLPurifier_DefinitionCacheFactory
$instance = $prototype;
} elseif ($instance === null || $prototype === true) {
$instance = new HTMLPurifier_DefinitionCacheFactory();
$instance->setup();
}
return $instance;
}
@ -74,7 +82,7 @@ class HTMLPurifier_DefinitionCacheFactory
$class = "HTMLPurifier_DefinitionCache_Decorator_$decorator";
$decorator = new $class;
}
$this->decorators[] = $decorator;
$this->decorators[$decorator->name] = $decorator;
}
}

View File

@ -0,0 +1,59 @@
<?php
require_once 'HTMLPurifier/DefinitionCache/DecoratorHarness.php';
require_once 'HTMLPurifier/DefinitionCache/Decorator/Cleanup.php';
generate_mock_once('HTMLPurifier_DefinitionCache');
class HTMLPurifier_DefinitionCache_Decorator_CleanupTest extends HTMLPurifier_DefinitionCache_DecoratorHarness
{
function setup() {
$this->cache = new HTMLPurifier_DefinitionCache_Decorator_Cleanup();
parent::setup();
}
function setupMockForSuccess($op) {
$this->mock->expectOnce($op, array($this->def, $this->config));
$this->mock->setReturnValue($op, true, array($this->def, $this->config));
$this->mock->expectNever('cleanup');
}
function setupMockForFailure($op) {
$this->mock->expectOnce($op, array($this->def, $this->config));
$this->mock->setReturnValue($op, false, array($this->def, $this->config));
$this->mock->expectOnce('cleanup', array($this->config));
}
function test_get() {
$this->mock->expectOnce('get', array($this->config));
$this->mock->setReturnValue('get', true, array($this->config));
$this->mock->expectNever('cleanup');
$this->assertEqual($this->cache->get($this->config), $this->def);
}
function test_get_failure() {
$this->mock->expectOnce('get', array($this->config));
$this->mock->setReturnValue('get', false, array($this->config));
$this->mock->expectOnce('cleanup', array($this->config));
$this->assertEqual($this->cache->get($this->config), false);
}
function test_set() {
$this->setupMockForSuccess('set');
$this->assertEqual($this->cache->set($this->def, $this->config), true);
}
function test_replace() {
$this->setupMockForSuccess('replace');
$this->assertEqual($this->cache->replace($this->def, $this->config), true);
}
function test_add() {
$this->setupMockForSuccess('add');
$this->assertEqual($this->cache->add($this->def, $this->config), true);
}
}
?>

View File

@ -1,29 +1,16 @@
<?php
require_once 'HTMLPurifier/DefinitionCacheHarness.php';
require_once 'HTMLPurifier/DefinitionCache/DecoratorHarness.php';
require_once 'HTMLPurifier/DefinitionCache/Decorator/Memory.php';
generate_mock_once('HTMLPurifier_DefinitionCache');
class HTMLPurifier_DefinitionCache_Decorator_MemoryTest extends HTMLPurifier_DefinitionCacheHarness
class HTMLPurifier_DefinitionCache_Decorator_MemoryTest extends HTMLPurifier_DefinitionCache_DecoratorHarness
{
function setup() {
unset($this->mock);
unset($this->cache);
$this->mock =& new HTMLPurifier_DefinitionCacheMock($this);
$this->mock->type = 'Test';
$this->cache = new HTMLPurifier_DefinitionCache_Decorator_Memory();
$this->cache = $this->cache->decorate($this->mock);
$this->def = $this->generateDefinition();
$this->config = $this->generateConfigMock();
}
function test_get() {
$this->mock->expectOnce('get', array($this->config)); // only ONE call!
$this->mock->setReturnValue('get', $this->def, array($this->config));
$this->assertEqual($this->cache->get($this->config), $this->def);
$this->assertEqual($this->cache->get($this->config), $this->def);
$this->cache = new HTMLPurifier_DefinitionCache_Decorator_Memory();
parent::setup();
}
function setupMockForSuccess($op) {
@ -38,38 +25,45 @@ class HTMLPurifier_DefinitionCache_Decorator_MemoryTest extends HTMLPurifier_Def
$this->mock->expectOnce('get', array($this->config));
}
function test_get() {
$this->mock->expectOnce('get', array($this->config)); // only ONE call!
$this->mock->setReturnValue('get', $this->def, array($this->config));
$this->assertEqual($this->cache->get($this->config), $this->def);
$this->assertEqual($this->cache->get($this->config), $this->def);
}
function test_set() {
$this->setupMockForSuccess('set');
$this->setupMockForSuccess('set', 'get');
$this->assertEqual($this->cache->set($this->def, $this->config), true);
$this->assertEqual($this->cache->get($this->config), $this->def);
}
function test_set_failure() {
$this->setupMockForFailure('set');
$this->setupMockForFailure('set', 'get');
$this->assertEqual($this->cache->set($this->def, $this->config), false);
$this->cache->get($this->config);
}
function test_replace() {
$this->setupMockForSuccess('replace');
$this->setupMockForSuccess('replace', 'get');
$this->assertEqual($this->cache->replace($this->def, $this->config), true);
$this->assertEqual($this->cache->get($this->config), $this->def);
}
function test_replace_failure() {
$this->setupMockForFailure('replace');
$this->setupMockForFailure('replace', 'get');
$this->assertEqual($this->cache->replace($this->def, $this->config), false);
$this->cache->get($this->config);
}
function test_add() {
$this->setupMockForSuccess('add');
$this->setupMockForSuccess('add', 'get');
$this->assertEqual($this->cache->add($this->def, $this->config), true);
$this->assertEqual($this->cache->get($this->config), $this->def);
}
function test_add_failure() {
$this->setupMockForFailure('add');
$this->setupMockForFailure('add', 'get');
$this->assertEqual($this->cache->add($this->def, $this->config), false);
$this->cache->get($this->config);
}

View File

@ -0,0 +1,26 @@
<?php
require_once 'HTMLPurifier/DefinitionCacheHarness.php';
require_once 'HTMLPurifier/DefinitionCache/Decorator/Memory.php';
generate_mock_once('HTMLPurifier_DefinitionCache');
class HTMLPurifier_DefinitionCache_DecoratorHarness extends HTMLPurifier_DefinitionCacheHarness
{
function setup() {
$this->mock =& new HTMLPurifier_DefinitionCacheMock($this);
$this->mock->type = 'Test';
$this->cache = $this->cache->decorate($this->mock);
$this->def = $this->generateDefinition();
$this->config = $this->generateConfigMock();
}
function teardown() {
unset($this->mock);
unset($this->cache);
}
}
?>

View File

@ -63,6 +63,7 @@ $test_files[] = 'HTMLPurifier/ConfigTest.php';
$test_files[] = 'HTMLPurifier/ContextTest.php';
$test_files[] = 'HTMLPurifier/DefinitionCacheFactoryTest.php';
$test_files[] = 'HTMLPurifier/DefinitionCacheTest.php';
$test_files[] = 'HTMLPurifier/DefinitionCache/Decorator/CleanupTest.php';
$test_files[] = 'HTMLPurifier/DefinitionCache/Decorator/MemoryTest.php';
$test_files[] = 'HTMLPurifier/DefinitionCache/DecoratorTest.php';
$test_files[] = 'HTMLPurifier/DefinitionCache/SerializerTest.php';