2007-05-25 01:32:29 +00:00
|
|
|
<?php
|
|
|
|
|
2007-05-27 15:52:45 +00:00
|
|
|
require_once 'HTMLPurifier/DefinitionCacheHarness.php';
|
2007-05-25 01:32:29 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-05-27 15:52:45 +00:00
|
|
|
class HTMLPurifier_DefinitionCache_SerializerTest extends HTMLPurifier_DefinitionCacheHarness
|
2007-05-25 01:32:29 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
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() {
|
|
|
|
|
|
|
|
$cache = new HTMLPurifier_DefinitionCache_Serializer('Test');
|
|
|
|
|
|
|
|
$config_array = array('Foo' => 'Bar');
|
2007-05-29 18:19:42 +00:00
|
|
|
|
|
|
|
$config = $this->generateConfigMock($config_array);
|
|
|
|
$config->version = '1.0.0';
|
|
|
|
$config->revision = 2;
|
|
|
|
|
|
|
|
$config_md5 = '1.0.0-' . $config->revision . '-' . md5(serialize($config_array));
|
2007-05-25 01:32:29 +00:00
|
|
|
|
|
|
|
$file = realpath(
|
|
|
|
$rel_file = dirname(__FILE__) .
|
|
|
|
'/../../../library/HTMLPurifier/DefinitionCache/Serializer/Test/' .
|
|
|
|
$config_md5 . '.ser'
|
|
|
|
);
|
2007-05-27 14:27:54 +00:00
|
|
|
if($file && file_exists($file)) unlink($file); // prevent previous failures from causing problems
|
2007-05-25 01:32:29 +00:00
|
|
|
|
|
|
|
$this->assertIdentical($config_md5, $cache->generateKey($config));
|
|
|
|
|
|
|
|
$def_original = $this->generateDefinition();
|
|
|
|
|
|
|
|
$cache->add($def_original, $config);
|
|
|
|
$this->assertFileExist($rel_file);
|
|
|
|
|
|
|
|
$file_generated = $cache->generateFilePath($config);
|
|
|
|
$this->assertIdentical(realpath($rel_file), realpath($file_generated));
|
|
|
|
|
|
|
|
$def_1 = $cache->get($config);
|
|
|
|
$this->assertIdentical($def_original, $def_1);
|
|
|
|
|
|
|
|
$def_original->info_random = 'changed';
|
|
|
|
|
|
|
|
$cache->set($def_original, $config);
|
|
|
|
$def_2 = $cache->get($config);
|
|
|
|
|
|
|
|
$this->assertIdentical($def_original, $def_2);
|
|
|
|
$this->assertNotEqual ($def_original, $def_1);
|
|
|
|
|
|
|
|
$def_original->info_random = 'did it change?';
|
|
|
|
|
|
|
|
$this->assertFalse($cache->add($def_original, $config));
|
|
|
|
$def_3 = $cache->get($config);
|
|
|
|
|
|
|
|
$this->assertNotEqual ($def_original, $def_3); // did not change!
|
|
|
|
$this->assertIdentical($def_3, $def_2);
|
|
|
|
|
2007-05-27 13:25:54 +00:00
|
|
|
$cache->replace($def_original, $config);
|
|
|
|
$def_4 = $cache->get($config);
|
|
|
|
$this->assertIdentical($def_original, $def_4);
|
|
|
|
|
2007-05-25 01:32:29 +00:00
|
|
|
$cache->remove($config);
|
|
|
|
$this->assertFileNotExist($file);
|
|
|
|
|
2007-05-27 13:25:54 +00:00
|
|
|
$this->assertFalse($cache->replace($def_original, $config));
|
|
|
|
$def_5 = $cache->get($config);
|
|
|
|
$this->assertFalse($def_5);
|
2007-05-25 01:32:29 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_errors() {
|
2007-05-27 13:25:54 +00:00
|
|
|
$cache = new HTMLPurifier_DefinitionCache_Serializer('Test');
|
2007-05-25 01:32:29 +00:00
|
|
|
$def = new HTMLPurifier_Definition();
|
|
|
|
$def->setup = true;
|
|
|
|
$def->type = 'NotTest';
|
2007-05-27 13:25:54 +00:00
|
|
|
$config = $this->generateConfigMock(array('Test' => 'foo'));
|
|
|
|
|
|
|
|
$this->expectError('Cannot use definition of type NotTest in cache for Test');
|
|
|
|
$cache->add($def, $config);
|
2007-05-25 01:32:29 +00:00
|
|
|
|
2007-05-27 13:25:54 +00:00
|
|
|
$this->expectError('Cannot use definition of type NotTest in cache for Test');
|
|
|
|
$cache->set($def, $config);
|
|
|
|
|
|
|
|
$this->expectError('Cannot use definition of type NotTest in cache for Test');
|
|
|
|
$cache->replace($def, $config);
|
2007-05-25 01:32:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function test_flush() {
|
|
|
|
|
2007-05-27 13:25:54 +00:00
|
|
|
$cache = new HTMLPurifier_DefinitionCache_Serializer('Test');
|
|
|
|
|
|
|
|
$config1 = $this->generateConfigMock(array('Test' => 1));
|
|
|
|
$config2 = $this->generateConfigMock(array('Test' => 2));
|
|
|
|
$config3 = $this->generateConfigMock(array('Test' => 3));
|
2007-05-25 01:32:29 +00:00
|
|
|
|
|
|
|
$def1 = $this->generateDefinition(array('info_candles' => 1));
|
|
|
|
$def2 = $this->generateDefinition(array('info_candles' => 2));
|
|
|
|
$def3 = $this->generateDefinition(array('info_candles' => 3));
|
|
|
|
|
|
|
|
$cache->add($def1, $config1);
|
|
|
|
$cache->add($def2, $config2);
|
|
|
|
$cache->add($def3, $config3);
|
|
|
|
|
2007-05-27 13:25:54 +00:00
|
|
|
$this->assertEqual($def1, $cache->get($config1));
|
|
|
|
$this->assertEqual($def2, $cache->get($config2));
|
|
|
|
$this->assertEqual($def3, $cache->get($config3));
|
|
|
|
|
|
|
|
$cache->flush();
|
|
|
|
|
|
|
|
$this->assertFalse($cache->get($config1));
|
|
|
|
$this->assertFalse($cache->get($config2));
|
|
|
|
$this->assertFalse($cache->get($config3));
|
2007-05-25 01:32:29 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-05-29 18:19:42 +00:00
|
|
|
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));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-05-25 01:32:29 +00:00
|
|
|
/**
|
|
|
|
* Asserts that a file exists, ignoring the stat cache
|
|
|
|
*/
|
|
|
|
function assertFileExist($file) {
|
|
|
|
clearstatcache();
|
|
|
|
$this->assertTrue(file_exists($file), 'Expected ' . $file . ' exists');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asserts that a file does not exist, ignoring the stat cache
|
|
|
|
*/
|
|
|
|
function assertFileNotExist($file) {
|
|
|
|
$this->assertFalse(file_exists($file), 'Expected ' . $file . ' does not exist');
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|