2007-05-25 01:32:29 +00:00
|
|
|
<?php
|
|
|
|
|
2007-05-27 15:52:45 +00:00
|
|
|
class HTMLPurifier_DefinitionCache_SerializerTest extends HTMLPurifier_DefinitionCacheHarness
|
2007-05-25 01:32:29 +00:00
|
|
|
{
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test()
|
|
|
|
{
|
2012-01-18 23:57:13 +00:00
|
|
|
// XXX SimpleTest does some really crazy stuff in the background
|
|
|
|
// to do equality checks. Unfortunately, this makes some
|
|
|
|
// versions of PHP segfault. So we need to define a better,
|
|
|
|
// homebrew notion of equality and use that instead. For now,
|
|
|
|
// the identical asserts are commented out.
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-25 01:32:29 +00:00
|
|
|
$cache = new HTMLPurifier_DefinitionCache_Serializer('Test');
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-29 20:21:33 +00:00
|
|
|
$config = $this->generateConfigMock('serial');
|
2016-03-24 07:08:03 +00:00
|
|
|
$config->returns('get', 2, array('Test.DefinitionRev'));
|
2007-05-29 18:19:42 +00:00
|
|
|
$config->version = '1.0.0';
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-04-26 19:28:14 +00:00
|
|
|
$config_md5 = '1.0.0,serial,2';
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-25 01:32:29 +00:00
|
|
|
$file = realpath(
|
2007-07-30 16:56:50 +00:00
|
|
|
$rel_file = HTMLPURIFIER_PREFIX . '/HTMLPurifier/DefinitionCache/Serializer/Test/' .
|
2007-05-25 01:32:29 +00:00
|
|
|
$config_md5 . '.ser'
|
|
|
|
);
|
2007-05-27 14:27:54 +00:00
|
|
|
if($file && file_exists($file)) unlink($file); // prevent previous failures from causing problems
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-25 01:32:29 +00:00
|
|
|
$this->assertIdentical($config_md5, $cache->generateKey($config));
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-25 01:32:29 +00:00
|
|
|
$def_original = $this->generateDefinition();
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-25 01:32:29 +00:00
|
|
|
$cache->add($def_original, $config);
|
|
|
|
$this->assertFileExist($rel_file);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-25 01:32:29 +00:00
|
|
|
$file_generated = $cache->generateFilePath($config);
|
|
|
|
$this->assertIdentical(realpath($rel_file), realpath($file_generated));
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-25 01:32:29 +00:00
|
|
|
$def_1 = $cache->get($config);
|
2012-01-18 23:57:13 +00:00
|
|
|
// $this->assertIdentical($def_original, $def_1);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-25 01:32:29 +00:00
|
|
|
$def_original->info_random = 'changed';
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-25 01:32:29 +00:00
|
|
|
$cache->set($def_original, $config);
|
|
|
|
$def_2 = $cache->get($config);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2012-01-18 23:57:13 +00:00
|
|
|
// $this->assertIdentical($def_original, $def_2);
|
|
|
|
// $this->assertNotEqual ($def_original, $def_1);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-25 01:32:29 +00:00
|
|
|
$def_original->info_random = 'did it change?';
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-25 01:32:29 +00:00
|
|
|
$this->assertFalse($cache->add($def_original, $config));
|
|
|
|
$def_3 = $cache->get($config);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2012-01-18 23:57:13 +00:00
|
|
|
// $this->assertNotEqual ($def_original, $def_3); // did not change!
|
|
|
|
// $this->assertIdentical($def_3, $def_2);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-27 13:25:54 +00:00
|
|
|
$cache->replace($def_original, $config);
|
|
|
|
$def_4 = $cache->get($config);
|
2012-01-18 23:57:13 +00:00
|
|
|
// $this->assertIdentical($def_original, $def_4);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-25 01:32:29 +00:00
|
|
|
$cache->remove($config);
|
|
|
|
$this->assertFileNotExist($file);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-27 13:25:54 +00:00
|
|
|
$this->assertFalse($cache->replace($def_original, $config));
|
|
|
|
$def_5 = $cache->get($config);
|
|
|
|
$this->assertFalse($def_5);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-25 01:32:29 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_errors()
|
|
|
|
{
|
2007-05-27 13:25:54 +00:00
|
|
|
$cache = new HTMLPurifier_DefinitionCache_Serializer('Test');
|
2007-11-25 02:24:39 +00:00
|
|
|
$def = $this->generateDefinition();
|
2007-05-25 01:32:29 +00:00
|
|
|
$def->setup = true;
|
|
|
|
$def->type = 'NotTest';
|
2007-05-29 20:21:33 +00:00
|
|
|
$config = $this->generateConfigMock('testfoo');
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-27 13:25:54 +00:00
|
|
|
$this->expectError('Cannot use definition of type NotTest in cache for Test');
|
|
|
|
$cache->add($def, $config);
|
2008-12-06 07:28:20 +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);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-27 13:25:54 +00:00
|
|
|
$this->expectError('Cannot use definition of type NotTest in cache for Test');
|
|
|
|
$cache->replace($def, $config);
|
2007-05-25 01:32:29 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_flush()
|
|
|
|
{
|
2007-05-27 13:25:54 +00:00
|
|
|
$cache = new HTMLPurifier_DefinitionCache_Serializer('Test');
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-29 20:21:33 +00:00
|
|
|
$config1 = $this->generateConfigMock('test1');
|
|
|
|
$config2 = $this->generateConfigMock('test2');
|
|
|
|
$config3 = $this->generateConfigMock('test3');
|
2008-12-06 07:28:20 +00:00
|
|
|
|
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));
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-25 01:32:29 +00:00
|
|
|
$cache->add($def1, $config1);
|
|
|
|
$cache->add($def2, $config2);
|
|
|
|
$cache->add($def3, $config3);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
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));
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-06-16 20:46:44 +00:00
|
|
|
$cache->flush($config1); // only essential directive is %Cache.SerializerPath
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-27 13:25:54 +00:00
|
|
|
$this->assertFalse($cache->get($config1));
|
|
|
|
$this->assertFalse($cache->get($config2));
|
|
|
|
$this->assertFalse($cache->get($config3));
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-25 01:32:29 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function testCleanup()
|
|
|
|
{
|
2007-05-29 18:19:42 +00:00
|
|
|
$cache = new HTMLPurifier_DefinitionCache_Serializer('Test');
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-29 18:19:42 +00:00
|
|
|
// in order of age, oldest first
|
|
|
|
// note that configurations are all identical, but version/revision
|
|
|
|
// are different
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-29 18:19:42 +00:00
|
|
|
$config1 = $this->generateConfigMock();
|
|
|
|
$config1->version = '0.9.0';
|
2016-03-24 07:08:03 +00:00
|
|
|
$config1->returns('get', 574, array('Test.DefinitionRev'));
|
2007-05-29 18:19:42 +00:00
|
|
|
$def1 = $this->generateDefinition(array('info' => 1));
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-29 18:19:42 +00:00
|
|
|
$config2 = $this->generateConfigMock();
|
|
|
|
$config2->version = '1.0.0beta';
|
2016-03-24 07:08:03 +00:00
|
|
|
$config2->returns('get', 1, array('Test.DefinitionRev'));
|
2007-05-29 18:19:42 +00:00
|
|
|
$def2 = $this->generateDefinition(array('info' => 3));
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-29 18:19:42 +00:00
|
|
|
$cache->set($def1, $config1);
|
|
|
|
$cache->cleanup($config1);
|
|
|
|
$this->assertEqual($def1, $cache->get($config1)); // no change
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-29 18:19:42 +00:00
|
|
|
$cache->cleanup($config2);
|
|
|
|
$this->assertFalse($cache->get($config1));
|
|
|
|
$this->assertFalse($cache->get($config2));
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-29 18:19:42 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function testCleanupOnlySameID()
|
|
|
|
{
|
2007-06-23 14:05:09 +00:00
|
|
|
$cache = new HTMLPurifier_DefinitionCache_Serializer('Test');
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-06-23 14:05:09 +00:00
|
|
|
$config1 = $this->generateConfigMock('serial1');
|
|
|
|
$config1->version = '1.0.0';
|
2016-03-24 07:08:03 +00:00
|
|
|
$config1->returns('get', 1, array('Test.DefinitionRev'));
|
2007-06-23 14:05:09 +00:00
|
|
|
$def1 = $this->generateDefinition(array('info' => 1));
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-06-23 14:05:09 +00:00
|
|
|
$config2 = $this->generateConfigMock('serial2');
|
|
|
|
$config2->version = '1.0.0';
|
2016-03-24 07:08:03 +00:00
|
|
|
$config2->returns('get', 34, array('Test.DefinitionRev'));
|
2007-06-23 14:05:09 +00:00
|
|
|
$def2 = $this->generateDefinition(array('info' => 3));
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-06-23 14:05:09 +00:00
|
|
|
$cache->set($def1, $config1);
|
|
|
|
$cache->cleanup($config1);
|
|
|
|
$this->assertEqual($def1, $cache->get($config1)); // no change
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-06-23 14:05:09 +00:00
|
|
|
$cache->set($def2, $config2);
|
|
|
|
$cache->cleanup($config2);
|
|
|
|
$this->assertEqual($def1, $cache->get($config1));
|
|
|
|
$this->assertEqual($def2, $cache->get($config2));
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-06-23 14:05:09 +00:00
|
|
|
$cache->flush($config1);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-25 01:32:29 +00:00
|
|
|
/**
|
|
|
|
* Asserts that a file exists, ignoring the stat cache
|
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
public function assertFileExist($file)
|
|
|
|
{
|
2007-05-25 01:32:29 +00:00
|
|
|
clearstatcache();
|
|
|
|
$this->assertTrue(file_exists($file), 'Expected ' . $file . ' exists');
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-25 01:32:29 +00:00
|
|
|
/**
|
|
|
|
* Asserts that a file does not exist, ignoring the stat cache
|
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
public function assertFileNotExist($file)
|
|
|
|
{
|
2010-12-30 23:51:53 +00:00
|
|
|
clearstatcache();
|
2007-05-25 01:32:29 +00:00
|
|
|
$this->assertFalse(file_exists($file), 'Expected ' . $file . ' does not exist');
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function testAlternatePath()
|
|
|
|
{
|
2007-06-16 20:46:44 +00:00
|
|
|
$cache = new HTMLPurifier_DefinitionCache_Serializer('Test');
|
|
|
|
$config = $this->generateConfigMock('serial');
|
|
|
|
$config->version = '1.0.0';
|
2016-03-24 07:08:03 +00:00
|
|
|
$config->returns('get', 1, array('Test.DefinitionRev'));
|
2007-06-16 20:46:44 +00:00
|
|
|
$dir = dirname(__FILE__) . '/SerializerTest';
|
2016-03-24 07:08:03 +00:00
|
|
|
$config->returns('get', $dir, array('Cache.SerializerPath'));
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-06-16 20:46:44 +00:00
|
|
|
$def_original = $this->generateDefinition();
|
|
|
|
$cache->add($def_original, $config);
|
2008-04-26 19:28:14 +00:00
|
|
|
$this->assertFileExist($dir . '/Test/1.0.0,serial,1.ser');
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-04-26 19:28:14 +00:00
|
|
|
unlink($dir . '/Test/1.0.0,serial,1.ser');
|
2007-06-16 20:46:44 +00:00
|
|
|
rmdir( $dir . '/Test');
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-06-16 20:46:44 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function testAlternatePermissions()
|
|
|
|
{
|
2011-01-13 14:05:12 +00:00
|
|
|
$cache = new HTMLPurifier_DefinitionCache_Serializer('Test');
|
|
|
|
$config = $this->generateConfigMock('serial');
|
|
|
|
$config->version = '1.0.0';
|
2016-03-24 07:08:03 +00:00
|
|
|
$config->returns('get', 1, array('Test.DefinitionRev'));
|
2011-01-13 14:05:12 +00:00
|
|
|
$dir = dirname(__FILE__) . '/SerializerTest';
|
2016-03-24 07:08:03 +00:00
|
|
|
$config->returns('get', $dir, array('Cache.SerializerPath'));
|
|
|
|
$config->returns('get', 0700, array('Cache.SerializerPermissions'));
|
2011-01-13 14:05:12 +00:00
|
|
|
|
|
|
|
$def_original = $this->generateDefinition();
|
|
|
|
$cache->add($def_original, $config);
|
|
|
|
$this->assertFileExist($dir . '/Test/1.0.0,serial,1.ser');
|
|
|
|
|
2014-12-09 02:21:56 +00:00
|
|
|
$this->assertEqual(0600, 0777 & fileperms($dir . '/Test/1.0.0,serial,1.ser'));
|
|
|
|
$this->assertEqual(0700, 0777 & fileperms($dir . '/Test'));
|
2011-01-13 14:05:12 +00:00
|
|
|
|
|
|
|
unlink($dir . '/Test/1.0.0,serial,1.ser');
|
|
|
|
rmdir( $dir . '/Test');
|
|
|
|
|
|
|
|
}
|
2016-03-27 21:53:31 +00:00
|
|
|
|
|
|
|
public function testNoInfiniteLoop()
|
|
|
|
{
|
|
|
|
$cache = new HTMLPurifier_DefinitionCache_Serializer('Test');
|
|
|
|
|
|
|
|
$config = $this->generateConfigMock('serial');
|
|
|
|
$config->version = '1.0.0';
|
|
|
|
$config->returns('get', 1, array('Test.DefinitionRev'));
|
|
|
|
$dir = dirname(__FILE__) . '/SerializerTest';
|
|
|
|
$config->returns('get', $dir, array('Cache.SerializerPath'));
|
|
|
|
$config->returns('get', 0400, array('Cache.SerializerPermissions'));
|
|
|
|
|
|
|
|
$cache->cleanup($config);
|
|
|
|
}
|
2007-05-25 01:32:29 +00:00
|
|
|
}
|
|
|
|
|
2008-12-06 09:24:59 +00:00
|
|
|
// vim: et sw=4 sts=4
|