From e2a951420f392a6b72f59af38f46396e7efa57c9 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Tue, 29 May 2007 20:49:33 +0000 Subject: [PATCH] [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 --- TODO | 1 - .../DefinitionCache/Decorator.php | 1 + .../DefinitionCache/Decorator/Cleanup.php | 45 ++++++++++++++ .../DefinitionCache/Decorator/Memory.php | 1 + .../DefinitionCache/Decorator/Template.php.in | 3 +- .../HTMLPurifier/DefinitionCacheFactory.php | 10 +++- .../DefinitionCache/Decorator/CleanupTest.php | 59 +++++++++++++++++++ .../DefinitionCache/Decorator/MemoryTest.php | 40 ++++++------- .../DefinitionCache/DecoratorHarness.php | 26 ++++++++ tests/test_files.php | 1 + 10 files changed, 161 insertions(+), 26 deletions(-) create mode 100644 library/HTMLPurifier/DefinitionCache/Decorator/Cleanup.php create mode 100644 tests/HTMLPurifier/DefinitionCache/Decorator/CleanupTest.php create mode 100644 tests/HTMLPurifier/DefinitionCache/DecoratorHarness.php diff --git a/TODO b/TODO index fbc18bf9..5b6f6f42 100644 --- a/TODO +++ b/TODO @@ -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!] diff --git a/library/HTMLPurifier/DefinitionCache/Decorator.php b/library/HTMLPurifier/DefinitionCache/Decorator.php index ca2e53aa..1061ed6d 100644 --- a/library/HTMLPurifier/DefinitionCache/Decorator.php +++ b/library/HTMLPurifier/DefinitionCache/Decorator.php @@ -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 { diff --git a/library/HTMLPurifier/DefinitionCache/Decorator/Cleanup.php b/library/HTMLPurifier/DefinitionCache/Decorator/Cleanup.php new file mode 100644 index 00000000..f67a0c6a --- /dev/null +++ b/library/HTMLPurifier/DefinitionCache/Decorator/Cleanup.php @@ -0,0 +1,45 @@ + \ No newline at end of file diff --git a/library/HTMLPurifier/DefinitionCache/Decorator/Memory.php b/library/HTMLPurifier/DefinitionCache/Decorator/Memory.php index 44908293..91817f9a 100644 --- a/library/HTMLPurifier/DefinitionCache/Decorator/Memory.php +++ b/library/HTMLPurifier/DefinitionCache/Decorator/Memory.php @@ -12,6 +12,7 @@ class HTMLPurifier_DefinitionCache_Decorator_Memory extends { var $definitions; + var $name = 'Memory'; function copy() { return new HTMLPurifier_DefinitionCache_Decorator_Memory(); diff --git a/library/HTMLPurifier/DefinitionCache/Decorator/Template.php.in b/library/HTMLPurifier/DefinitionCache/Decorator/Template.php.in index 6bf4a63d..71acf0c9 100644 --- a/library/HTMLPurifier/DefinitionCache/Decorator/Template.php.in +++ b/library/HTMLPurifier/DefinitionCache/Decorator/Template.php.in @@ -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(); } diff --git a/library/HTMLPurifier/DefinitionCacheFactory.php b/library/HTMLPurifier/DefinitionCacheFactory.php index c68f3135..97707a98 100644 --- a/library/HTMLPurifier/DefinitionCacheFactory.php +++ b/library/HTMLPurifier/DefinitionCacheFactory.php @@ -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; } } diff --git a/tests/HTMLPurifier/DefinitionCache/Decorator/CleanupTest.php b/tests/HTMLPurifier/DefinitionCache/Decorator/CleanupTest.php new file mode 100644 index 00000000..758a4cb8 --- /dev/null +++ b/tests/HTMLPurifier/DefinitionCache/Decorator/CleanupTest.php @@ -0,0 +1,59 @@ +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); + } + +} + +?> \ No newline at end of file diff --git a/tests/HTMLPurifier/DefinitionCache/Decorator/MemoryTest.php b/tests/HTMLPurifier/DefinitionCache/Decorator/MemoryTest.php index e715df4e..b7f89c56 100644 --- a/tests/HTMLPurifier/DefinitionCache/Decorator/MemoryTest.php +++ b/tests/HTMLPurifier/DefinitionCache/Decorator/MemoryTest.php @@ -1,29 +1,16 @@ 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); } diff --git a/tests/HTMLPurifier/DefinitionCache/DecoratorHarness.php b/tests/HTMLPurifier/DefinitionCache/DecoratorHarness.php new file mode 100644 index 00000000..f5411d75 --- /dev/null +++ b/tests/HTMLPurifier/DefinitionCache/DecoratorHarness.php @@ -0,0 +1,26 @@ +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); + } + +} + +?> \ No newline at end of file diff --git a/tests/test_files.php b/tests/test_files.php index 6ecdb3c7..a6549d9f 100644 --- a/tests/test_files.php +++ b/tests/test_files.php @@ -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';