mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2025-01-03 05:11:52 +00:00
[1.7.0] Add DefinitionCache decorators, implement Memory decorator
- Move serialization responsibility to Config - Create DefinitionCacheFactory - Implement Null definition cache git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1117 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
parent
d1187ed331
commit
002395de09
3
NEWS
3
NEWS
@ -21,7 +21,8 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier
|
|||||||
# New compact syntax for AttrDef objects that can be used to instantiate
|
# New compact syntax for AttrDef objects that can be used to instantiate
|
||||||
new objects via make()
|
new objects via make()
|
||||||
# Definitions (esp. HTMLDefinition) are now cached for a significant
|
# Definitions (esp. HTMLDefinition) are now cached for a significant
|
||||||
performance boost
|
performance boost. You can disable caching by setting %Core.DefinitionCache
|
||||||
|
to null.
|
||||||
! HTML Purifier now works in PHP 4.3.2.
|
! HTML Purifier now works in PHP 4.3.2.
|
||||||
! Configuration form-editing API makes tweaking HTMLPurifier_Config a
|
! Configuration form-editing API makes tweaking HTMLPurifier_Config a
|
||||||
breeze!
|
breeze!
|
||||||
|
@ -6,7 +6,7 @@ require_once 'HTMLPurifier/ConfigSchema.php';
|
|||||||
require_once 'HTMLPurifier/HTMLDefinition.php';
|
require_once 'HTMLPurifier/HTMLDefinition.php';
|
||||||
require_once 'HTMLPurifier/CSSDefinition.php';
|
require_once 'HTMLPurifier/CSSDefinition.php';
|
||||||
require_once 'HTMLPurifier/Doctype.php';
|
require_once 'HTMLPurifier/Doctype.php';
|
||||||
require_once 'HTMLPurifier/DefinitionCache.php';
|
require_once 'HTMLPurifier/DefinitionCacheFactory.php';
|
||||||
|
|
||||||
// accomodations for versions earlier than 4.3.10 and 5.0.2
|
// accomodations for versions earlier than 4.3.10 and 5.0.2
|
||||||
// borrowed from PHP_Compat, LGPL licensed, by Aidan Lister <aidan@php.net>
|
// borrowed from PHP_Compat, LGPL licensed, by Aidan Lister <aidan@php.net>
|
||||||
@ -76,6 +76,12 @@ class HTMLPurifier_Config
|
|||||||
*/
|
*/
|
||||||
var $autoFinalize = true;
|
var $autoFinalize = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Namespace indexed array of serials for specific namespaces (see
|
||||||
|
* getSerial for more info).
|
||||||
|
*/
|
||||||
|
var $serials = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $definition HTMLPurifier_ConfigSchema that defines what directives
|
* @param $definition HTMLPurifier_ConfigSchema that defines what directives
|
||||||
* are allowed.
|
* are allowed.
|
||||||
@ -149,6 +155,18 @@ class HTMLPurifier_Config
|
|||||||
return $this->conf[$namespace];
|
return $this->conf[$namespace];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a md5 signature of a segment of the configuration object
|
||||||
|
* that uniquely identifies that particular configuration
|
||||||
|
* @param $namespace Namespace to get serial for
|
||||||
|
*/
|
||||||
|
function getBatchSerial($namespace) {
|
||||||
|
if (empty($this->serials[$namespace])) {
|
||||||
|
$this->serials[$namespace] = md5(serialize($this->getBatch($namespace)));
|
||||||
|
}
|
||||||
|
return $this->serials[$namespace];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves all directives, organized by namespace
|
* Retrieves all directives, organized by namespace
|
||||||
*/
|
*/
|
||||||
@ -210,6 +228,8 @@ class HTMLPurifier_Config
|
|||||||
if ($namespace == 'HTML' || $namespace == 'CSS') {
|
if ($namespace == 'HTML' || $namespace == 'CSS') {
|
||||||
$this->definitions[$namespace] = null;
|
$this->definitions[$namespace] = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->serials[$namespace] = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -235,7 +255,8 @@ class HTMLPurifier_Config
|
|||||||
*/
|
*/
|
||||||
function &getDefinition($type, $raw = false) {
|
function &getDefinition($type, $raw = false) {
|
||||||
if (!$this->finalized && $this->autoFinalize) $this->finalize();
|
if (!$this->finalized && $this->autoFinalize) $this->finalize();
|
||||||
$cache = HTMLPurifier_DefinitionCache::create($type, $this);
|
$factory = HTMLPurifier_DefinitionCacheFactory::instance();
|
||||||
|
$cache = $factory->create($type, $this);
|
||||||
if (!$raw) {
|
if (!$raw) {
|
||||||
// see if we can quickly supply a definition
|
// see if we can quickly supply a definition
|
||||||
if (!empty($this->definitions[$type])) {
|
if (!empty($this->definitions[$type])) {
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
require_once 'HTMLPurifier/DefinitionCache/Serializer.php';
|
require_once 'HTMLPurifier/DefinitionCache/Serializer.php';
|
||||||
|
require_once 'HTMLPurifier/DefinitionCache/Null.php';
|
||||||
|
|
||||||
|
require_once 'HTMLPurifier/DefinitionCache/Decorator.php';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract class representing Definition cache managers that implements
|
* Abstract class representing Definition cache managers that implements
|
||||||
* useful common methods and is a factory.
|
* useful common methods and is a factory.
|
||||||
* @note The configuration object is transformed into the key used by the cache
|
|
||||||
* @todo Get some sort of versioning variable so the library can easily
|
* @todo Get some sort of versioning variable so the library can easily
|
||||||
* invalidate the cache with a new version
|
* invalidate the cache with a new version
|
||||||
* @todo Make the test runner cache aware and allow the user to easily
|
* @todo Make the test runner cache aware and allow the user to easily
|
||||||
@ -33,9 +35,9 @@ class HTMLPurifier_DefinitionCache
|
|||||||
* @param Instance of HTMLPurifier_Config
|
* @param Instance of HTMLPurifier_Config
|
||||||
*/
|
*/
|
||||||
function generateKey($config) {
|
function generateKey($config) {
|
||||||
$version = $config->version;
|
return $config->version . '-' . // possibly replace with function calls
|
||||||
$revision = $config->revision;
|
$config->revision . '-' .
|
||||||
return $version . '-' . $revision . '-' . md5(serialize($config->getBatch($this->type)));
|
$config->getBatchSerial($this->type);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -52,17 +54,6 @@ class HTMLPurifier_DefinitionCache
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Factory method that creates a cache object based on configuration
|
|
||||||
* @param $name Name of definitions handled by cache
|
|
||||||
* @param $config Instance of HTMLPurifier_Config
|
|
||||||
*/
|
|
||||||
function create($name, $config) {
|
|
||||||
// only one implementation as for right now, $config will
|
|
||||||
// be used to determine implementation
|
|
||||||
return new HTMLPurifier_DefinitionCache_Serializer($name);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if a definition's type jives with the cache's type
|
* Checks if a definition's type jives with the cache's type
|
||||||
* @note Throws an error on failure
|
* @note Throws an error on failure
|
||||||
|
62
library/HTMLPurifier/DefinitionCache/Decorator.php
Normal file
62
library/HTMLPurifier/DefinitionCache/Decorator.php
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once 'HTMLPurifier/DefinitionCache.php';
|
||||||
|
|
||||||
|
require_once 'HTMLPurifier/DefinitionCache/Decorator/Memory.php';
|
||||||
|
|
||||||
|
class HTMLPurifier_DefinitionCache_Decorator extends HTMLPurifier_DefinitionCache
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cache object we are decorating
|
||||||
|
*/
|
||||||
|
var $cache;
|
||||||
|
|
||||||
|
function HTMLPurifier_DefinitionCache_Decorator() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lazy decorator function
|
||||||
|
* @param $cache Reference to cache object to decorate
|
||||||
|
*/
|
||||||
|
function decorate(&$cache) {
|
||||||
|
$decorator = $this->copy();
|
||||||
|
// reference is necessary for mocks in PHP 4
|
||||||
|
$decorator->cache =& $cache;
|
||||||
|
$decorator->type = $cache->type;
|
||||||
|
return $decorator;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cross-compatible clone substitute
|
||||||
|
*/
|
||||||
|
function copy() {
|
||||||
|
return new HTMLPurifier_DefinitionCache_Decorator();
|
||||||
|
}
|
||||||
|
|
||||||
|
function add($def, $config) {
|
||||||
|
return $this->cache->add($def, $config);
|
||||||
|
}
|
||||||
|
|
||||||
|
function set($def, $config) {
|
||||||
|
return $this->cache->set($def, $config);
|
||||||
|
}
|
||||||
|
|
||||||
|
function replace($def, $config) {
|
||||||
|
return $this->cache->replace($def, $config);
|
||||||
|
}
|
||||||
|
|
||||||
|
function get($config) {
|
||||||
|
return $this->cache->get($config);
|
||||||
|
}
|
||||||
|
|
||||||
|
function flush() {
|
||||||
|
return $this->cache->flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
function cleanup($config) {
|
||||||
|
return $this->cache->cleanup($config);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
47
library/HTMLPurifier/DefinitionCache/Decorator/Memory.php
Normal file
47
library/HTMLPurifier/DefinitionCache/Decorator/Memory.php
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once 'HTMLPurifier/DefinitionCache/Decorator.php';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Definition cache decorator class that saves all cache retrievals
|
||||||
|
* to PHP's memory; good for unit tests or circumstances where
|
||||||
|
* there are lots of configuration objects floating around.
|
||||||
|
*/
|
||||||
|
class HTMLPurifier_DefinitionCache_Decorator_Memory extends
|
||||||
|
HTMLPurifier_DefinitionCache_Decorator
|
||||||
|
{
|
||||||
|
|
||||||
|
var $definitions;
|
||||||
|
|
||||||
|
function copy() {
|
||||||
|
return new HTMLPurifier_DefinitionCache_Decorator_Memory();
|
||||||
|
}
|
||||||
|
|
||||||
|
function add($def, $config) {
|
||||||
|
$status = parent::add($def, $config);
|
||||||
|
if ($status) $this->definitions[$this->generateKey($config)] = $def;
|
||||||
|
return $status;
|
||||||
|
}
|
||||||
|
|
||||||
|
function set($def, $config) {
|
||||||
|
$status = parent::set($def, $config);
|
||||||
|
if ($status) $this->definitions[$this->generateKey($config)] = $def;
|
||||||
|
return $status;
|
||||||
|
}
|
||||||
|
|
||||||
|
function replace($def, $config) {
|
||||||
|
$status = parent::replace($def, $config);
|
||||||
|
if ($status) $this->definitions[$this->generateKey($config)] = $def;
|
||||||
|
return $status;
|
||||||
|
}
|
||||||
|
|
||||||
|
function get($config) {
|
||||||
|
$key = $this->generateKey($config);
|
||||||
|
if (isset($this->definitions[$key])) return $this->definitions[$key];
|
||||||
|
$this->definitions[$key] = parent::get($config);
|
||||||
|
return $this->definitions[$key];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once 'HTMLPurifier/DefinitionCache/Decorator.php';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Definition cache decorator template.
|
||||||
|
*/
|
||||||
|
class HTMLPurifier_DefinitionCache_Decorator_Template extends
|
||||||
|
HTMLPurifier_DefinitionCache_Decorator
|
||||||
|
{
|
||||||
|
|
||||||
|
// must be defined!!!
|
||||||
|
|
||||||
|
function copy() {
|
||||||
|
return new HTMLPurifier_DefinitionCache_Decorator_Template();
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove methods you don't need
|
||||||
|
|
||||||
|
function add($def, $config) {
|
||||||
|
return parent::add($def, $config);
|
||||||
|
}
|
||||||
|
|
||||||
|
function set($def, $config) {
|
||||||
|
return parent::set($def, $config);
|
||||||
|
}
|
||||||
|
|
||||||
|
function replace($def, $config) {
|
||||||
|
return parent::replace($def, $config);
|
||||||
|
}
|
||||||
|
|
||||||
|
function get($config) {
|
||||||
|
return parent::get($config);
|
||||||
|
}
|
||||||
|
|
||||||
|
function flush() {
|
||||||
|
return parent::flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
function cleanup($config) {
|
||||||
|
return parent::cleanup($config);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
37
library/HTMLPurifier/DefinitionCache/Null.php
Normal file
37
library/HTMLPurifier/DefinitionCache/Null.php
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once 'HTMLPurifier/DefinitionCache.php';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Null cache object to use when no caching is on.
|
||||||
|
*/
|
||||||
|
class HTMLPurifier_DefinitionCache_Null extends HTMLPurifier_DefinitionCache
|
||||||
|
{
|
||||||
|
|
||||||
|
function add($def, $config) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function set($def, $config) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function replace($def, $config) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function get($config) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function flush() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function cleanup($config) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
82
library/HTMLPurifier/DefinitionCacheFactory.php
Normal file
82
library/HTMLPurifier/DefinitionCacheFactory.php
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once 'HTMLPurifier/DefinitionCache.php';
|
||||||
|
|
||||||
|
HTMLPurifier_ConfigSchema::define(
|
||||||
|
'Core', 'DefinitionCache', 'Serializer', 'string/null', '
|
||||||
|
This directive defines which method to use when caching definitions,
|
||||||
|
the complex data-type that makes HTML Purifier tick. Set to null
|
||||||
|
to disable caching (not recommended, as you will see a definite
|
||||||
|
performance degradation). This directive has been available since 1.7.0.
|
||||||
|
');
|
||||||
|
|
||||||
|
HTMLPurifier_ConfigSchema::defineAllowedValues(
|
||||||
|
'Core', 'DefinitionCache', array('Serializer')
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Responsible for creating definition caches.
|
||||||
|
*/
|
||||||
|
class HTMLPurifier_DefinitionCacheFactory
|
||||||
|
{
|
||||||
|
|
||||||
|
var $caches = array('Serializer' => array());
|
||||||
|
var $decorators = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves an instance of global definition cache factory.
|
||||||
|
* @static
|
||||||
|
*/
|
||||||
|
function &instance($prototype = null) {
|
||||||
|
static $instance;
|
||||||
|
if ($prototype !== null) {
|
||||||
|
$instance = $prototype;
|
||||||
|
} elseif ($instance === null || $prototype === true) {
|
||||||
|
$instance = new HTMLPurifier_DefinitionCacheFactory();
|
||||||
|
}
|
||||||
|
return $instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Factory method that creates a cache object based on configuration
|
||||||
|
* @param $name Name of definitions handled by cache
|
||||||
|
* @param $config Instance of HTMLPurifier_Config
|
||||||
|
*/
|
||||||
|
function &create($type, $config) {
|
||||||
|
// only one implementation as for right now, $config will
|
||||||
|
// be used to determine implementation
|
||||||
|
$method = $config->get('Core', 'DefinitionCache');
|
||||||
|
if ($method === null) {
|
||||||
|
$null = new HTMLPurifier_DefinitionCache_Null($type);
|
||||||
|
return $null;
|
||||||
|
}
|
||||||
|
if (!empty($this->caches[$method][$type])) {
|
||||||
|
return $this->caches[$method][$type];
|
||||||
|
}
|
||||||
|
$cache = new HTMLPurifier_DefinitionCache_Serializer($type);
|
||||||
|
foreach ($this->decorators as $decorator) {
|
||||||
|
$new_cache = $decorator->decorate($cache);
|
||||||
|
// prevent infinite recursion in PHP 4
|
||||||
|
unset($cache);
|
||||||
|
$cache = $new_cache;
|
||||||
|
}
|
||||||
|
$this->caches[$method][$type] = $cache;
|
||||||
|
return $this->caches[$method][$type];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers a decorator to add to all new cache objects
|
||||||
|
* @param
|
||||||
|
*/
|
||||||
|
function addDecorator($decorator) {
|
||||||
|
if (is_string($decorator)) {
|
||||||
|
$class = "HTMLPurifier_DefinitionCache_Decorator_$decorator";
|
||||||
|
$decorator = new $class;
|
||||||
|
}
|
||||||
|
$this->decorators[] = $decorator;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
79
tests/HTMLPurifier/DefinitionCache/Decorator/MemoryTest.php
Normal file
79
tests/HTMLPurifier/DefinitionCache/Decorator/MemoryTest.php
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once 'HTMLPurifier/DefinitionCacheHarness.php';
|
||||||
|
require_once 'HTMLPurifier/DefinitionCache/Decorator/Memory.php';
|
||||||
|
|
||||||
|
generate_mock_once('HTMLPurifier_DefinitionCache');
|
||||||
|
|
||||||
|
class HTMLPurifier_DefinitionCache_Decorator_MemoryTest extends HTMLPurifier_DefinitionCacheHarness
|
||||||
|
{
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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('get');
|
||||||
|
}
|
||||||
|
|
||||||
|
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('get', array($this->config));
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_set() {
|
||||||
|
$this->setupMockForSuccess('set');
|
||||||
|
$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->assertEqual($this->cache->set($this->def, $this->config), false);
|
||||||
|
$this->cache->get($this->config);
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_replace() {
|
||||||
|
$this->setupMockForSuccess('replace');
|
||||||
|
$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->assertEqual($this->cache->replace($this->def, $this->config), false);
|
||||||
|
$this->cache->get($this->config);
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_add() {
|
||||||
|
$this->setupMockForSuccess('add');
|
||||||
|
$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->assertEqual($this->cache->add($this->def, $this->config), false);
|
||||||
|
$this->cache->get($this->config);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
45
tests/HTMLPurifier/DefinitionCache/DecoratorTest.php
Normal file
45
tests/HTMLPurifier/DefinitionCache/DecoratorTest.php
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once 'HTMLPurifier/DefinitionCacheHarness.php';
|
||||||
|
require_once 'HTMLPurifier/DefinitionCache/Decorator.php';
|
||||||
|
|
||||||
|
class HTMLPurifier_DefinitionCache_DecoratorTest extends HTMLPurifier_DefinitionCacheHarness
|
||||||
|
{
|
||||||
|
|
||||||
|
function test() {
|
||||||
|
|
||||||
|
generate_mock_once('HTMLPurifier_DefinitionCache');
|
||||||
|
$mock =& new HTMLPurifier_DefinitionCacheMock($this);
|
||||||
|
$mock->type = 'Test';
|
||||||
|
|
||||||
|
$cache = new HTMLPurifier_DefinitionCache_Decorator();
|
||||||
|
$cache = $cache->decorate($mock);
|
||||||
|
|
||||||
|
$this->assertIdentical($cache->type, $mock->type);
|
||||||
|
|
||||||
|
$def = $this->generateDefinition();
|
||||||
|
$config = $this->generateConfigMock();
|
||||||
|
|
||||||
|
$mock->expectOnce('add', array($def, $config));
|
||||||
|
$cache->add($def, $config);
|
||||||
|
|
||||||
|
$mock->expectOnce('set', array($def, $config));
|
||||||
|
$cache->set($def, $config);
|
||||||
|
|
||||||
|
$mock->expectOnce('replace', array($def, $config));
|
||||||
|
$cache->replace($def, $config);
|
||||||
|
|
||||||
|
$mock->expectOnce('get', array($config));
|
||||||
|
$cache->get($config);
|
||||||
|
|
||||||
|
$mock->expectOnce('flush', array());
|
||||||
|
$cache->flush();
|
||||||
|
|
||||||
|
$mock->expectOnce('cleanup', array($config));
|
||||||
|
$cache->cleanup($config);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
@ -3,62 +3,18 @@
|
|||||||
require_once 'HTMLPurifier/DefinitionCacheHarness.php';
|
require_once 'HTMLPurifier/DefinitionCacheHarness.php';
|
||||||
require_once 'HTMLPurifier/DefinitionCache/Serializer.php';
|
require_once 'HTMLPurifier/DefinitionCache/Serializer.php';
|
||||||
|
|
||||||
class HTMLPurifier_Definition_SerializerMock extends HTMLPurifier_Definition
|
|
||||||
{
|
|
||||||
|
|
||||||
var $_test;
|
|
||||||
var $_expect = false;
|
|
||||||
|
|
||||||
function HTMLPurifier_Definition_SerializerMock(&$test_case) {
|
|
||||||
$this->_test =& $test_case;
|
|
||||||
}
|
|
||||||
|
|
||||||
function expectDoSetupOnce() {$this->_expect = true;}
|
|
||||||
|
|
||||||
function doSetup($config) {
|
|
||||||
if ($this->_expect) {
|
|
||||||
$this->_test->pass();
|
|
||||||
} else {
|
|
||||||
$this->_test->fail('Unexpected call to doSetup');
|
|
||||||
}
|
|
||||||
unset($this->_test, $this->_expect);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
class HTMLPurifier_DefinitionCache_SerializerTest extends HTMLPurifier_DefinitionCacheHarness
|
class HTMLPurifier_DefinitionCache_SerializerTest extends HTMLPurifier_DefinitionCacheHarness
|
||||||
{
|
{
|
||||||
|
|
||||||
function test__SerializerMock_pass() {
|
|
||||||
$config = 'config';
|
|
||||||
generate_mock_once('UnitTestCase');
|
|
||||||
$test =& new UnitTestCaseMock($this);
|
|
||||||
$test->expectOnce('pass');
|
|
||||||
$mock = new HTMLPurifier_Definition_SerializerMock($test);
|
|
||||||
$mock->expectDoSetupOnce();
|
|
||||||
$mock->doSetup($config);
|
|
||||||
}
|
|
||||||
|
|
||||||
function test__SerializerMock_fail() {
|
|
||||||
$config = 'config';
|
|
||||||
generate_mock_once('UnitTestCase');
|
|
||||||
$test =& new UnitTestCaseMock($this);
|
|
||||||
$test->expectOnce('fail');
|
|
||||||
$mock = new HTMLPurifier_Definition_SerializerMock($test);
|
|
||||||
$mock->doSetup($config);
|
|
||||||
}
|
|
||||||
|
|
||||||
function test() {
|
function test() {
|
||||||
|
|
||||||
$cache = new HTMLPurifier_DefinitionCache_Serializer('Test');
|
$cache = new HTMLPurifier_DefinitionCache_Serializer('Test');
|
||||||
|
|
||||||
$config_array = array('Foo' => 'Bar');
|
$config = $this->generateConfigMock('serial');
|
||||||
|
|
||||||
$config = $this->generateConfigMock($config_array);
|
|
||||||
$config->version = '1.0.0';
|
$config->version = '1.0.0';
|
||||||
$config->revision = 2;
|
$config->revision = 2;
|
||||||
|
|
||||||
$config_md5 = '1.0.0-' . $config->revision . '-' . md5(serialize($config_array));
|
$config_md5 = '1.0.0-' . $config->revision . '-serial';
|
||||||
|
|
||||||
$file = realpath(
|
$file = realpath(
|
||||||
$rel_file = dirname(__FILE__) .
|
$rel_file = dirname(__FILE__) .
|
||||||
@ -114,7 +70,7 @@ class HTMLPurifier_DefinitionCache_SerializerTest extends HTMLPurifier_Definitio
|
|||||||
$def = new HTMLPurifier_Definition();
|
$def = new HTMLPurifier_Definition();
|
||||||
$def->setup = true;
|
$def->setup = true;
|
||||||
$def->type = 'NotTest';
|
$def->type = 'NotTest';
|
||||||
$config = $this->generateConfigMock(array('Test' => 'foo'));
|
$config = $this->generateConfigMock('testfoo');
|
||||||
|
|
||||||
$this->expectError('Cannot use definition of type NotTest in cache for Test');
|
$this->expectError('Cannot use definition of type NotTest in cache for Test');
|
||||||
$cache->add($def, $config);
|
$cache->add($def, $config);
|
||||||
@ -130,9 +86,9 @@ class HTMLPurifier_DefinitionCache_SerializerTest extends HTMLPurifier_Definitio
|
|||||||
|
|
||||||
$cache = new HTMLPurifier_DefinitionCache_Serializer('Test');
|
$cache = new HTMLPurifier_DefinitionCache_Serializer('Test');
|
||||||
|
|
||||||
$config1 = $this->generateConfigMock(array('Test' => 1));
|
$config1 = $this->generateConfigMock('test1');
|
||||||
$config2 = $this->generateConfigMock(array('Test' => 2));
|
$config2 = $this->generateConfigMock('test2');
|
||||||
$config3 = $this->generateConfigMock(array('Test' => 3));
|
$config3 = $this->generateConfigMock('test3');
|
||||||
|
|
||||||
$def1 = $this->generateDefinition(array('info_candles' => 1));
|
$def1 = $this->generateDefinition(array('info_candles' => 1));
|
||||||
$def2 = $this->generateDefinition(array('info_candles' => 2));
|
$def2 = $this->generateDefinition(array('info_candles' => 2));
|
||||||
|
65
tests/HTMLPurifier/DefinitionCacheFactoryTest.php
Normal file
65
tests/HTMLPurifier/DefinitionCacheFactoryTest.php
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once 'HTMLPurifier/DefinitionCacheFactory.php';
|
||||||
|
|
||||||
|
class HTMLPurifier_DefinitionCacheFactoryTest extends UnitTestCase
|
||||||
|
{
|
||||||
|
|
||||||
|
var $newFactory;
|
||||||
|
var $oldFactory;
|
||||||
|
|
||||||
|
function setup() {
|
||||||
|
$new = new HTMLPurifier_DefinitionCacheFactory();
|
||||||
|
$this->oldFactory = HTMLPurifier_DefinitionCacheFactory::instance();
|
||||||
|
HTMLPurifier_DefinitionCacheFactory::instance($new);
|
||||||
|
}
|
||||||
|
|
||||||
|
function teardown() {
|
||||||
|
HTMLPurifier_DefinitionCacheFactory::instance($this->oldFactory);
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_create() {
|
||||||
|
$config = HTMLPurifier_Config::createDefault();
|
||||||
|
$factory = HTMLPurifier_DefinitionCacheFactory::instance();
|
||||||
|
$cache = $factory->create('Test', $config);
|
||||||
|
$this->assertEqual($cache, new HTMLPurifier_DefinitionCache_Serializer('Test'));
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_create_withDecorator() {
|
||||||
|
$config = HTMLPurifier_Config::createDefault();
|
||||||
|
$factory =& HTMLPurifier_DefinitionCacheFactory::instance();
|
||||||
|
$factory->addDecorator('Memory');
|
||||||
|
$cache =& $factory->create('Test', $config);
|
||||||
|
$cache_real = new HTMLPurifier_DefinitionCache_Decorator_Memory();
|
||||||
|
$cache_real = $cache_real->decorate(new HTMLPurifier_DefinitionCache_Serializer('Test'));
|
||||||
|
$this->assertEqual($cache, $cache_real);
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_create_withDecoratorObject() {
|
||||||
|
$config = HTMLPurifier_Config::createDefault();
|
||||||
|
$factory =& HTMLPurifier_DefinitionCacheFactory::instance();
|
||||||
|
$factory->addDecorator(new HTMLPurifier_DefinitionCache_Decorator_Memory());
|
||||||
|
$cache =& $factory->create('Test', $config);
|
||||||
|
$cache_real = new HTMLPurifier_DefinitionCache_Decorator_Memory();
|
||||||
|
$cache_real = $cache_real->decorate(new HTMLPurifier_DefinitionCache_Serializer('Test'));
|
||||||
|
$this->assertEqual($cache, $cache_real);
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_create_recycling() {
|
||||||
|
$config = HTMLPurifier_Config::createDefault();
|
||||||
|
$factory =& HTMLPurifier_DefinitionCacheFactory::instance();
|
||||||
|
$cache =& $factory->create('Test', $config);
|
||||||
|
$cache2 =& $factory->create('Test', $config);
|
||||||
|
$this->assertReference($cache, $cache2);
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_null() {
|
||||||
|
$config = HTMLPurifier_Config::create(array('Core.DefinitionCache' => null));
|
||||||
|
$factory =& HTMLPurifier_DefinitionCacheFactory::instance();
|
||||||
|
$cache =& $factory->create('Test', $config);
|
||||||
|
$this->assertEqual($cache, new HTMLPurifier_DefinitionCache_Null('Test'));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
@ -8,10 +8,10 @@ class HTMLPurifier_DefinitionCacheHarness extends UnitTestCase
|
|||||||
* to a getBatch() call
|
* to a getBatch() call
|
||||||
* @param $values Values to return when getBatch is invoked
|
* @param $values Values to return when getBatch is invoked
|
||||||
*/
|
*/
|
||||||
function generateConfigMock($values = array()) {
|
function generateConfigMock($serial = 'defaultserial') {
|
||||||
generate_mock_once('HTMLPurifier_Config');
|
generate_mock_once('HTMLPurifier_Config');
|
||||||
$config = new HTMLPurifier_ConfigMock($this);
|
$config = new HTMLPurifier_ConfigMock($this);
|
||||||
$config->setReturnValue('getBatch', $values, array('Test'));
|
$config->setReturnValue('getBatchSerial', $serial, array('Test'));
|
||||||
$config->version = '1.0.0';
|
$config->version = '1.0.0';
|
||||||
$config->revision = 1;
|
$config->revision = 1;
|
||||||
return $config;
|
return $config;
|
||||||
|
@ -4,11 +4,6 @@ require_once 'HTMLPurifier/DefinitionCache.php';
|
|||||||
|
|
||||||
class HTMLPurifier_DefinitionCacheTest extends UnitTestCase
|
class HTMLPurifier_DefinitionCacheTest extends UnitTestCase
|
||||||
{
|
{
|
||||||
function test_create() {
|
|
||||||
$config = HTMLPurifier_Config::createDefault();
|
|
||||||
$cache = HTMLPurifier_DefinitionCache::create('Test', $config);
|
|
||||||
$this->assertEqual($cache, new HTMLPurifier_DefinitionCache_Serializer('Test'));
|
|
||||||
}
|
|
||||||
|
|
||||||
function test_isOld() {
|
function test_isOld() {
|
||||||
$cache = new HTMLPurifier_DefinitionCache('Test'); // non-functional
|
$cache = new HTMLPurifier_DefinitionCache('Test'); // non-functional
|
||||||
|
@ -40,6 +40,10 @@ if ( is_string($GLOBALS['HTMLPurifierTest']['PEAR']) ) {
|
|||||||
// initialize and load HTML Purifier
|
// initialize and load HTML Purifier
|
||||||
require_once '../library/HTMLPurifier.auto.php';
|
require_once '../library/HTMLPurifier.auto.php';
|
||||||
|
|
||||||
|
// setup special DefinitionCacheFactory decorator
|
||||||
|
$factory =& HTMLPurifier_DefinitionCacheFactory::instance();
|
||||||
|
$factory->addDecorator('Memory'); // since we deal with a lot of config objects
|
||||||
|
|
||||||
// load tests
|
// load tests
|
||||||
$test_files = array();
|
$test_files = array();
|
||||||
require 'test_files.php'; // populates $test_files array
|
require 'test_files.php'; // populates $test_files array
|
||||||
|
@ -61,7 +61,10 @@ $test_files[] = 'HTMLPurifier/ChildDef/TableTest.php';
|
|||||||
$test_files[] = 'HTMLPurifier/ConfigSchemaTest.php';
|
$test_files[] = 'HTMLPurifier/ConfigSchemaTest.php';
|
||||||
$test_files[] = 'HTMLPurifier/ConfigTest.php';
|
$test_files[] = 'HTMLPurifier/ConfigTest.php';
|
||||||
$test_files[] = 'HTMLPurifier/ContextTest.php';
|
$test_files[] = 'HTMLPurifier/ContextTest.php';
|
||||||
|
$test_files[] = 'HTMLPurifier/DefinitionCacheFactoryTest.php';
|
||||||
$test_files[] = 'HTMLPurifier/DefinitionCacheTest.php';
|
$test_files[] = 'HTMLPurifier/DefinitionCacheTest.php';
|
||||||
|
$test_files[] = 'HTMLPurifier/DefinitionCache/Decorator/MemoryTest.php';
|
||||||
|
$test_files[] = 'HTMLPurifier/DefinitionCache/DecoratorTest.php';
|
||||||
$test_files[] = 'HTMLPurifier/DefinitionCache/SerializerTest.php';
|
$test_files[] = 'HTMLPurifier/DefinitionCache/SerializerTest.php';
|
||||||
$test_files[] = 'HTMLPurifier/DefinitionTest.php';
|
$test_files[] = 'HTMLPurifier/DefinitionTest.php';
|
||||||
$test_files[] = 'HTMLPurifier/DoctypeRegistryTest.php';
|
$test_files[] = 'HTMLPurifier/DoctypeRegistryTest.php';
|
||||||
|
Loading…
Reference in New Issue
Block a user