You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

230 lines
7.9KB

  1. <?php
  2. class HTMLPurifier_DefinitionCache_SerializerTest extends HTMLPurifier_DefinitionCacheHarness
  3. {
  4. public function test()
  5. {
  6. // XXX SimpleTest does some really crazy stuff in the background
  7. // to do equality checks. Unfortunately, this makes some
  8. // versions of PHP segfault. So we need to define a better,
  9. // homebrew notion of equality and use that instead. For now,
  10. // the identical asserts are commented out.
  11. $cache = new HTMLPurifier_DefinitionCache_Serializer('Test');
  12. $config = $this->generateConfigMock('serial');
  13. $config->setReturnValue('get', 2, array('Test.DefinitionRev'));
  14. $config->version = '1.0.0';
  15. $config_md5 = '1.0.0,serial,2';
  16. $file = realpath(
  17. $rel_file = HTMLPURIFIER_PREFIX . '/HTMLPurifier/DefinitionCache/Serializer/Test/' .
  18. $config_md5 . '.ser'
  19. );
  20. if($file && file_exists($file)) unlink($file); // prevent previous failures from causing problems
  21. $this->assertIdentical($config_md5, $cache->generateKey($config));
  22. $def_original = $this->generateDefinition();
  23. $cache->add($def_original, $config);
  24. $this->assertFileExist($rel_file);
  25. $file_generated = $cache->generateFilePath($config);
  26. $this->assertIdentical(realpath($rel_file), realpath($file_generated));
  27. $def_1 = $cache->get($config);
  28. // $this->assertIdentical($def_original, $def_1);
  29. $def_original->info_random = 'changed';
  30. $cache->set($def_original, $config);
  31. $def_2 = $cache->get($config);
  32. // $this->assertIdentical($def_original, $def_2);
  33. // $this->assertNotEqual ($def_original, $def_1);
  34. $def_original->info_random = 'did it change?';
  35. $this->assertFalse($cache->add($def_original, $config));
  36. $def_3 = $cache->get($config);
  37. // $this->assertNotEqual ($def_original, $def_3); // did not change!
  38. // $this->assertIdentical($def_3, $def_2);
  39. $cache->replace($def_original, $config);
  40. $def_4 = $cache->get($config);
  41. // $this->assertIdentical($def_original, $def_4);
  42. $cache->remove($config);
  43. $this->assertFileNotExist($file);
  44. $this->assertFalse($cache->replace($def_original, $config));
  45. $def_5 = $cache->get($config);
  46. $this->assertFalse($def_5);
  47. }
  48. public function test_errors()
  49. {
  50. $cache = new HTMLPurifier_DefinitionCache_Serializer('Test');
  51. $def = $this->generateDefinition();
  52. $def->setup = true;
  53. $def->type = 'NotTest';
  54. $config = $this->generateConfigMock('testfoo');
  55. $this->expectError('Cannot use definition of type NotTest in cache for Test');
  56. $cache->add($def, $config);
  57. $this->expectError('Cannot use definition of type NotTest in cache for Test');
  58. $cache->set($def, $config);
  59. $this->expectError('Cannot use definition of type NotTest in cache for Test');
  60. $cache->replace($def, $config);
  61. }
  62. public function test_flush()
  63. {
  64. $cache = new HTMLPurifier_DefinitionCache_Serializer('Test');
  65. $config1 = $this->generateConfigMock('test1');
  66. $config2 = $this->generateConfigMock('test2');
  67. $config3 = $this->generateConfigMock('test3');
  68. $def1 = $this->generateDefinition(array('info_candles' => 1));
  69. $def2 = $this->generateDefinition(array('info_candles' => 2));
  70. $def3 = $this->generateDefinition(array('info_candles' => 3));
  71. $cache->add($def1, $config1);
  72. $cache->add($def2, $config2);
  73. $cache->add($def3, $config3);
  74. $this->assertEqual($def1, $cache->get($config1));
  75. $this->assertEqual($def2, $cache->get($config2));
  76. $this->assertEqual($def3, $cache->get($config3));
  77. $cache->flush($config1); // only essential directive is %Cache.SerializerPath
  78. $this->assertFalse($cache->get($config1));
  79. $this->assertFalse($cache->get($config2));
  80. $this->assertFalse($cache->get($config3));
  81. }
  82. public function testCleanup()
  83. {
  84. $cache = new HTMLPurifier_DefinitionCache_Serializer('Test');
  85. // in order of age, oldest first
  86. // note that configurations are all identical, but version/revision
  87. // are different
  88. $config1 = $this->generateConfigMock();
  89. $config1->version = '0.9.0';
  90. $config1->setReturnValue('get', 574, array('Test.DefinitionRev'));
  91. $def1 = $this->generateDefinition(array('info' => 1));
  92. $config2 = $this->generateConfigMock();
  93. $config2->version = '1.0.0beta';
  94. $config2->setReturnValue('get', 1, array('Test.DefinitionRev'));
  95. $def2 = $this->generateDefinition(array('info' => 3));
  96. $cache->set($def1, $config1);
  97. $cache->cleanup($config1);
  98. $this->assertEqual($def1, $cache->get($config1)); // no change
  99. $cache->cleanup($config2);
  100. $this->assertFalse($cache->get($config1));
  101. $this->assertFalse($cache->get($config2));
  102. }
  103. public function testCleanupOnlySameID()
  104. {
  105. $cache = new HTMLPurifier_DefinitionCache_Serializer('Test');
  106. $config1 = $this->generateConfigMock('serial1');
  107. $config1->version = '1.0.0';
  108. $config1->setReturnValue('get', 1, array('Test.DefinitionRev'));
  109. $def1 = $this->generateDefinition(array('info' => 1));
  110. $config2 = $this->generateConfigMock('serial2');
  111. $config2->version = '1.0.0';
  112. $config2->setReturnValue('get', 34, array('Test.DefinitionRev'));
  113. $def2 = $this->generateDefinition(array('info' => 3));
  114. $cache->set($def1, $config1);
  115. $cache->cleanup($config1);
  116. $this->assertEqual($def1, $cache->get($config1)); // no change
  117. $cache->set($def2, $config2);
  118. $cache->cleanup($config2);
  119. $this->assertEqual($def1, $cache->get($config1));
  120. $this->assertEqual($def2, $cache->get($config2));
  121. $cache->flush($config1);
  122. }
  123. /**
  124. * Asserts that a file exists, ignoring the stat cache
  125. */
  126. public function assertFileExist($file)
  127. {
  128. clearstatcache();
  129. $this->assertTrue(file_exists($file), 'Expected ' . $file . ' exists');
  130. }
  131. /**
  132. * Asserts that a file does not exist, ignoring the stat cache
  133. */
  134. public function assertFileNotExist($file)
  135. {
  136. clearstatcache();
  137. $this->assertFalse(file_exists($file), 'Expected ' . $file . ' does not exist');
  138. }
  139. public function testAlternatePath()
  140. {
  141. $cache = new HTMLPurifier_DefinitionCache_Serializer('Test');
  142. $config = $this->generateConfigMock('serial');
  143. $config->version = '1.0.0';
  144. $config->setReturnValue('get', 1, array('Test.DefinitionRev'));
  145. $dir = dirname(__FILE__) . '/SerializerTest';
  146. $config->setReturnValue('get', $dir, array('Cache.SerializerPath'));
  147. $def_original = $this->generateDefinition();
  148. $cache->add($def_original, $config);
  149. $this->assertFileExist($dir . '/Test/1.0.0,serial,1.ser');
  150. unlink($dir . '/Test/1.0.0,serial,1.ser');
  151. rmdir( $dir . '/Test');
  152. }
  153. public function testAlternatePermissions()
  154. {
  155. $cache = new HTMLPurifier_DefinitionCache_Serializer('Test');
  156. $config = $this->generateConfigMock('serial');
  157. $config->version = '1.0.0';
  158. $config->setReturnValue('get', 1, array('Test.DefinitionRev'));
  159. $dir = dirname(__FILE__) . '/SerializerTest';
  160. $config->setReturnValue('get', $dir, array('Cache.SerializerPath'));
  161. $config->setReturnValue('get', 0777, array('Cache.SerializerPermissions'));
  162. $def_original = $this->generateDefinition();
  163. $cache->add($def_original, $config);
  164. $this->assertFileExist($dir . '/Test/1.0.0,serial,1.ser');
  165. $this->assertEqual(0666, 0777 & fileperms($dir . '/Test/1.0.0,serial,1.ser'));
  166. $this->assertEqual(0777, 0777 & fileperms($dir . '/Test'));
  167. unlink($dir . '/Test/1.0.0,serial,1.ser');
  168. rmdir( $dir . '/Test');
  169. }
  170. }
  171. // vim: et sw=4 sts=4