Add vim modelines to all files.
[htmlpurifier/bfroehle.git] / tests / HTMLPurifier / DefinitionCache / SerializerTest.php
blob5791bbdf8acbd29577ab32bbdffd625e47025780
1 <?php
3 class HTMLPurifier_DefinitionCache_SerializerTest extends HTMLPurifier_DefinitionCacheHarness
6 function test() {
8 $cache = new HTMLPurifier_DefinitionCache_Serializer('Test');
10 $config = $this->generateConfigMock('serial');
11 $config->setReturnValue('get', 2, array('Test', 'DefinitionRev'));
12 $config->version = '1.0.0';
14 $config_md5 = '1.0.0,serial,2';
16 $file = realpath(
17 $rel_file = HTMLPURIFIER_PREFIX . '/HTMLPurifier/DefinitionCache/Serializer/Test/' .
18 $config_md5 . '.ser'
20 if($file && file_exists($file)) unlink($file); // prevent previous failures from causing problems
22 $this->assertIdentical($config_md5, $cache->generateKey($config));
24 $def_original = $this->generateDefinition();
26 $cache->add($def_original, $config);
27 $this->assertFileExist($rel_file);
29 $file_generated = $cache->generateFilePath($config);
30 $this->assertIdentical(realpath($rel_file), realpath($file_generated));
32 $def_1 = $cache->get($config);
33 $this->assertIdentical($def_original, $def_1);
35 $def_original->info_random = 'changed';
37 $cache->set($def_original, $config);
38 $def_2 = $cache->get($config);
40 $this->assertIdentical($def_original, $def_2);
41 $this->assertNotEqual ($def_original, $def_1);
43 $def_original->info_random = 'did it change?';
45 $this->assertFalse($cache->add($def_original, $config));
46 $def_3 = $cache->get($config);
48 $this->assertNotEqual ($def_original, $def_3); // did not change!
49 $this->assertIdentical($def_3, $def_2);
51 $cache->replace($def_original, $config);
52 $def_4 = $cache->get($config);
53 $this->assertIdentical($def_original, $def_4);
55 $cache->remove($config);
56 $this->assertFileNotExist($file);
58 $this->assertFalse($cache->replace($def_original, $config));
59 $def_5 = $cache->get($config);
60 $this->assertFalse($def_5);
64 function test_errors() {
65 $cache = new HTMLPurifier_DefinitionCache_Serializer('Test');
66 $def = $this->generateDefinition();
67 $def->setup = true;
68 $def->type = 'NotTest';
69 $config = $this->generateConfigMock('testfoo');
71 $this->expectError('Cannot use definition of type NotTest in cache for Test');
72 $cache->add($def, $config);
74 $this->expectError('Cannot use definition of type NotTest in cache for Test');
75 $cache->set($def, $config);
77 $this->expectError('Cannot use definition of type NotTest in cache for Test');
78 $cache->replace($def, $config);
81 function test_flush() {
83 $cache = new HTMLPurifier_DefinitionCache_Serializer('Test');
85 $config1 = $this->generateConfigMock('test1');
86 $config2 = $this->generateConfigMock('test2');
87 $config3 = $this->generateConfigMock('test3');
89 $def1 = $this->generateDefinition(array('info_candles' => 1));
90 $def2 = $this->generateDefinition(array('info_candles' => 2));
91 $def3 = $this->generateDefinition(array('info_candles' => 3));
93 $cache->add($def1, $config1);
94 $cache->add($def2, $config2);
95 $cache->add($def3, $config3);
97 $this->assertEqual($def1, $cache->get($config1));
98 $this->assertEqual($def2, $cache->get($config2));
99 $this->assertEqual($def3, $cache->get($config3));
101 $cache->flush($config1); // only essential directive is %Cache.SerializerPath
103 $this->assertFalse($cache->get($config1));
104 $this->assertFalse($cache->get($config2));
105 $this->assertFalse($cache->get($config3));
109 function testCleanup() {
111 $cache = new HTMLPurifier_DefinitionCache_Serializer('Test');
113 // in order of age, oldest first
114 // note that configurations are all identical, but version/revision
115 // are different
117 $config1 = $this->generateConfigMock();
118 $config1->version = '0.9.0';
119 $config1->setReturnValue('get', 574, array('Test', 'DefinitionRev'));
120 $def1 = $this->generateDefinition(array('info' => 1));
122 $config2 = $this->generateConfigMock();
123 $config2->version = '1.0.0beta';
124 $config2->setReturnValue('get', 1, array('Test', 'DefinitionRev'));
125 $def2 = $this->generateDefinition(array('info' => 3));
127 $cache->set($def1, $config1);
128 $cache->cleanup($config1);
129 $this->assertEqual($def1, $cache->get($config1)); // no change
131 $cache->cleanup($config2);
132 $this->assertFalse($cache->get($config1));
133 $this->assertFalse($cache->get($config2));
137 function testCleanupOnlySameID() {
139 $cache = new HTMLPurifier_DefinitionCache_Serializer('Test');
141 $config1 = $this->generateConfigMock('serial1');
142 $config1->version = '1.0.0';
143 $config1->setReturnValue('get', 1, array('Test', 'DefinitionRev'));
144 $def1 = $this->generateDefinition(array('info' => 1));
146 $config2 = $this->generateConfigMock('serial2');
147 $config2->version = '1.0.0';
148 $config2->setReturnValue('get', 34, array('Test', 'DefinitionRev'));
149 $def2 = $this->generateDefinition(array('info' => 3));
151 $cache->set($def1, $config1);
152 $cache->cleanup($config1);
153 $this->assertEqual($def1, $cache->get($config1)); // no change
155 $cache->set($def2, $config2);
156 $cache->cleanup($config2);
157 $this->assertEqual($def1, $cache->get($config1));
158 $this->assertEqual($def2, $cache->get($config2));
160 $cache->flush($config1);
164 * Asserts that a file exists, ignoring the stat cache
166 function assertFileExist($file) {
167 clearstatcache();
168 $this->assertTrue(file_exists($file), 'Expected ' . $file . ' exists');
172 * Asserts that a file does not exist, ignoring the stat cache
174 function assertFileNotExist($file) {
175 $this->assertFalse(file_exists($file), 'Expected ' . $file . ' does not exist');
178 function testAlternatePath() {
180 $cache = new HTMLPurifier_DefinitionCache_Serializer('Test');
181 $config = $this->generateConfigMock('serial');
182 $config->version = '1.0.0';
183 $config->setReturnValue('get', 1, array('Test', 'DefinitionRev'));
184 $dir = dirname(__FILE__) . '/SerializerTest';
185 $config->setReturnValue('get', $dir, array('Cache', 'SerializerPath'));
187 $def_original = $this->generateDefinition();
188 $cache->add($def_original, $config);
189 $this->assertFileExist($dir . '/Test/1.0.0,serial,1.ser');
191 unlink($dir . '/Test/1.0.0,serial,1.ser');
192 rmdir( $dir . '/Test');
198 // vim: et sw=4 sts=4