SerializerTest.php 7.8 KB

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