Skip navigation
Help

cache.test

  1. drupal
    1. 7 drupal/modules/simpletest/tests/cache.test

Classes

NameDescription
CacheClearCaseTest cache clearing methods.
CacheGetMultipleUnitTestTest cache_get_multiple().
CacheIsEmptyCaseTest cache_is_empty() function.
CacheSavingCase
CacheTestCase

File

drupal/modules/simpletest/tests/cache.test
View source
  1. <?php
  2. class CacheTestCase extends DrupalWebTestCase {
  3. protected $default_bin = 'cache';
  4. protected $default_cid = 'test_temporary';
  5. protected $default_value = 'CacheTest';
  6. /**
  7. * Check whether or not a cache entry exists.
  8. *
  9. * @param $cid
  10. * The cache id.
  11. * @param $var
  12. * The variable the cache should contain.
  13. * @param $bin
  14. * The bin the cache item was stored in.
  15. * @return
  16. * TRUE on pass, FALSE on fail.
  17. */
  18. protected function checkCacheExists($cid, $var, $bin = NULL) {
  19. if ($bin == NULL) {
  20. $bin = $this->default_bin;
  21. }
  22. $cache = cache_get($cid, $bin);
  23. return isset($cache->data) && $cache->data == $var;
  24. }
  25. /**
  26. * Assert or a cache entry exists.
  27. *
  28. * @param $message
  29. * Message to display.
  30. * @param $var
  31. * The variable the cache should contain.
  32. * @param $cid
  33. * The cache id.
  34. * @param $bin
  35. * The bin the cache item was stored in.
  36. */
  37. protected function assertCacheExists($message, $var = NULL, $cid = NULL, $bin = NULL) {
  38. if ($bin == NULL) {
  39. $bin = $this->default_bin;
  40. }
  41. if ($cid == NULL) {
  42. $cid = $this->default_cid;
  43. }
  44. if ($var == NULL) {
  45. $var = $this->default_value;
  46. }
  47. $this->assertTrue($this->checkCacheExists($cid, $var, $bin), $message);
  48. }
  49. /**
  50. * Assert or a cache entry has been removed.
  51. *
  52. * @param $message
  53. * Message to display.
  54. * @param $cid
  55. * The cache id.
  56. * @param $bin
  57. * The bin the cache item was stored in.
  58. */
  59. function assertCacheRemoved($message, $cid = NULL, $bin = NULL) {
  60. if ($bin == NULL) {
  61. $bin = $this->default_bin;
  62. }
  63. if ($cid == NULL) {
  64. $cid = $this->default_cid;
  65. }
  66. $cache = cache_get($cid, $bin);
  67. $this->assertFalse($cache, $message);
  68. }
  69. /**
  70. * Perform the general wipe.
  71. * @param $bin
  72. * The bin to perform the wipe on.
  73. */
  74. protected function generalWipe($bin = NULL) {
  75. if ($bin == NULL) {
  76. $bin = $this->default_bin;
  77. }
  78. cache_clear_all(NULL, $bin);
  79. }
  80. /**
  81. * Setup the lifetime settings for caching.
  82. *
  83. * @param $time
  84. * The time in seconds the cache should minimal live.
  85. */
  86. protected function setupLifetime($time) {
  87. variable_set('cache_lifetime', $time);
  88. variable_set('cache_flush', 0);
  89. }
  90. }
  91. class CacheSavingCase extends CacheTestCase {
  92. public static function getInfo() {
  93. return array(
  94. 'name' => 'Cache saving test',
  95. 'description' => 'Check our variables are saved and restored the right way.',
  96. 'group' => 'Cache'
  97. );
  98. }
  99. /**
  100. * Test the saving and restoring of a string.
  101. */
  102. function testString() {
  103. $this->checkVariable($this->randomName(100));
  104. }
  105. /**
  106. * Test the saving and restoring of an integer.
  107. */
  108. function testInteger() {
  109. $this->checkVariable(100);
  110. }
  111. /**
  112. * Test the saving and restoring of a double.
  113. */
  114. function testDouble() {
  115. $this->checkVariable(1.29);
  116. }
  117. /**
  118. * Test the saving and restoring of an array.
  119. */
  120. function testArray() {
  121. $this->checkVariable(array('drupal1', 'drupal2' => 'drupal3', 'drupal4' => array('drupal5', 'drupal6')));
  122. }
  123. /**
  124. * Test the saving and restoring of an object.
  125. */
  126. function testObject() {
  127. $test_object = new stdClass();
  128. $test_object->test1 = $this->randomName(100);
  129. $test_object->test2 = 100;
  130. $test_object->test3 = array('drupal1', 'drupal2' => 'drupal3', 'drupal4' => array('drupal5', 'drupal6'));
  131. cache_set('test_object', $test_object, 'cache');
  132. $cache = cache_get('test_object', 'cache');
  133. $this->assertTrue(isset($cache->data) && $cache->data == $test_object, t('Object is saved and restored properly.'));
  134. }
  135. /*
  136. * Check or a variable is stored and restored properly.
  137. **/
  138. function checkVariable($var) {
  139. cache_set('test_var', $var, 'cache');
  140. $cache = cache_get('test_var', 'cache');
  141. $this->assertTrue(isset($cache->data) && $cache->data === $var, t('@type is saved and restored properly.', array('@type' => ucfirst(gettype($var)))));
  142. }
  143. }
  144. /**
  145. * Test cache_get_multiple().
  146. */
  147. class CacheGetMultipleUnitTest extends CacheTestCase {
  148. public static function getInfo() {
  149. return array(
  150. 'name' => 'Fetching multiple cache items',
  151. 'description' => 'Confirm that multiple records are fetched correctly.',
  152. 'group' => 'Cache',
  153. );
  154. }
  155. function setUp() {
  156. $this->default_bin = 'cache_page';
  157. parent::setUp();
  158. }
  159. /**
  160. * Test cache_get_multiple().
  161. */
  162. function testCacheMultiple() {
  163. $item1 = $this->randomName(10);
  164. $item2 = $this->randomName(10);
  165. cache_set('item1', $item1, $this->default_bin);
  166. cache_set('item2', $item2, $this->default_bin);
  167. $this->assertTrue($this->checkCacheExists('item1', $item1), t('Item 1 is cached.'));
  168. $this->assertTrue($this->checkCacheExists('item2', $item2), t('Item 2 is cached.'));
  169. // Fetch both records from the database with cache_get_multiple().
  170. $item_ids = array('item1', 'item2');
  171. $items = cache_get_multiple($item_ids, $this->default_bin);
  172. $this->assertEqual($items['item1']->data, $item1, t('Item was returned from cache successfully.'));
  173. $this->assertEqual($items['item2']->data, $item2, t('Item was returned from cache successfully.'));
  174. // Remove one item from the cache.
  175. cache_clear_all('item2', $this->default_bin);
  176. // Confirm that only one item is returned by cache_get_multiple().
  177. $item_ids = array('item1', 'item2');
  178. $items = cache_get_multiple($item_ids, $this->default_bin);
  179. $this->assertEqual($items['item1']->data, $item1, t('Item was returned from cache successfully.'));
  180. $this->assertFalse(isset($items['item2']), t('Item was not returned from the cache.'));
  181. $this->assertTrue(count($items) == 1, t('Only valid cache entries returned.'));
  182. }
  183. }
  184. /**
  185. * Test cache clearing methods.
  186. */
  187. class CacheClearCase extends CacheTestCase {
  188. public static function getInfo() {
  189. return array(
  190. 'name' => 'Cache clear test',
  191. 'description' => 'Check our clearing is done the proper way.',
  192. 'group' => 'Cache'
  193. );
  194. }
  195. function setUp() {
  196. $this->default_bin = 'cache_page';
  197. $this->default_value = $this->randomName(10);
  198. parent::setUp();
  199. }
  200. /**
  201. * Test clearing using a cid.
  202. */
  203. function testClearCid() {
  204. cache_set('test_cid_clear', $this->default_value, $this->default_bin);
  205. $this->assertCacheExists(t('Cache was set for clearing cid.'), $this->default_value, 'test_cid_clear');
  206. cache_clear_all('test_cid_clear', $this->default_bin);
  207. $this->assertCacheRemoved(t('Cache was removed after clearing cid.'), 'test_cid_clear');
  208. cache_set('test_cid_clear1', $this->default_value, $this->default_bin);
  209. cache_set('test_cid_clear2', $this->default_value, $this->default_bin);
  210. $this->assertTrue($this->checkCacheExists('test_cid_clear1', $this->default_value)
  211. && $this->checkCacheExists('test_cid_clear2', $this->default_value),
  212. t('Two caches were created for checking cid "*" with wildcard false.'));
  213. cache_clear_all('*', $this->default_bin);
  214. $this->assertTrue($this->checkCacheExists('test_cid_clear1', $this->default_value)
  215. && $this->checkCacheExists('test_cid_clear2', $this->default_value),
  216. t('Two caches still exists after clearing cid "*" with wildcard false.'));
  217. }
  218. /**
  219. * Test clearing using wildcard.
  220. */
  221. function testClearWildcard() {
  222. cache_set('test_cid_clear1', $this->default_value, $this->default_bin);
  223. cache_set('test_cid_clear2', $this->default_value, $this->default_bin);
  224. $this->assertTrue($this->checkCacheExists('test_cid_clear1', $this->default_value)
  225. && $this->checkCacheExists('test_cid_clear2', $this->default_value),
  226. t('Two caches were created for checking cid "*" with wildcard true.'));
  227. cache_clear_all('*', $this->default_bin, TRUE);
  228. $this->assertFalse($this->checkCacheExists('test_cid_clear1', $this->default_value)
  229. || $this->checkCacheExists('test_cid_clear2', $this->default_value),
  230. t('Two caches removed after clearing cid "*" with wildcard true.'));
  231. cache_set('test_cid_clear1', $this->default_value, $this->default_bin);
  232. cache_set('test_cid_clear2', $this->default_value, $this->default_bin);
  233. $this->assertTrue($this->checkCacheExists('test_cid_clear1', $this->default_value)
  234. && $this->checkCacheExists('test_cid_clear2', $this->default_value),
  235. t('Two caches were created for checking cid substring with wildcard true.'));
  236. cache_clear_all('test_', $this->default_bin, TRUE);
  237. $this->assertFalse($this->checkCacheExists('test_cid_clear1', $this->default_value)
  238. || $this->checkCacheExists('test_cid_clear2', $this->default_value),
  239. t('Two caches removed after clearing cid substring with wildcard true.'));
  240. }
  241. /**
  242. * Test clearing using an array.
  243. */
  244. function testClearArray() {
  245. // Create three cache entries.
  246. cache_set('test_cid_clear1', $this->default_value, $this->default_bin);
  247. cache_set('test_cid_clear2', $this->default_value, $this->default_bin);
  248. cache_set('test_cid_clear3', $this->default_value, $this->default_bin);
  249. $this->assertTrue($this->checkCacheExists('test_cid_clear1', $this->default_value)
  250. && $this->checkCacheExists('test_cid_clear2', $this->default_value)
  251. && $this->checkCacheExists('test_cid_clear3', $this->default_value),
  252. t('Three cache entries were created.'));
  253. // Clear two entries using an array.
  254. cache_clear_all(array('test_cid_clear1', 'test_cid_clear2'), $this->default_bin);
  255. $this->assertFalse($this->checkCacheExists('test_cid_clear1', $this->default_value)
  256. || $this->checkCacheExists('test_cid_clear2', $this->default_value),
  257. t('Two cache entries removed after clearing with an array.'));
  258. $this->assertTrue($this->checkCacheExists('test_cid_clear3', $this->default_value),
  259. t('Entry was not cleared from the cache'));
  260. // Set the cache clear threshold to 2 to confirm that the full bin is cleared
  261. // when the threshold is exceeded.
  262. variable_set('cache_clear_threshold', 2);
  263. cache_set('test_cid_clear1', $this->default_value, $this->default_bin);
  264. cache_set('test_cid_clear2', $this->default_value, $this->default_bin);
  265. $this->assertTrue($this->checkCacheExists('test_cid_clear1', $this->default_value)
  266. && $this->checkCacheExists('test_cid_clear2', $this->default_value),
  267. t('Two cache entries were created.'));
  268. cache_clear_all(array('test_cid_clear1', 'test_cid_clear2', 'test_cid_clear3'), $this->default_bin);
  269. $this->assertFalse($this->checkCacheExists('test_cid_clear1', $this->default_value)
  270. || $this->checkCacheExists('test_cid_clear2', $this->default_value)
  271. || $this->checkCacheExists('test_cid_clear3', $this->default_value),
  272. t('All cache entries removed when the array exceeded the cache clear threshold.'));
  273. }
  274. }
  275. /**
  276. * Test cache_is_empty() function.
  277. */
  278. class CacheIsEmptyCase extends CacheTestCase {
  279. public static function getInfo() {
  280. return array(
  281. 'name' => 'Cache emptiness test',
  282. 'description' => 'Check if a cache bin is empty after performing clear operations.',
  283. 'group' => 'Cache'
  284. );
  285. }
  286. function setUp() {
  287. $this->default_bin = 'cache_page';
  288. $this->default_value = $this->randomName(10);
  289. parent::setUp();
  290. }
  291. /**
  292. * Test clearing using a cid.
  293. */
  294. function testIsEmpty() {
  295. // Clear the cache bin.
  296. cache_clear_all('*', $this->default_bin);
  297. $this->assertTrue(cache_is_empty($this->default_bin), t('The cache bin is empty'));
  298. // Add some data to the cache bin.
  299. cache_set($this->default_cid, $this->default_value, $this->default_bin);
  300. $this->assertCacheExists(t('Cache was set.'), $this->default_value, $this->default_cid);
  301. $this->assertFalse(cache_is_empty($this->default_bin), t('The cache bin is not empty'));
  302. // Remove the cached data.
  303. cache_clear_all($this->default_cid, $this->default_bin);
  304. $this->assertCacheRemoved(t('Cache was removed.'), $this->default_cid);
  305. $this->assertTrue(cache_is_empty($this->default_bin), t('The cache bin is empty'));
  306. }
  307. }