cache.test

File

drupal-7.x/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, '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, format_string('@type is saved and restored properly.', array('@type' => ucfirst(gettype($var)))));
  142. }
  143. /**
  144. * Test no empty cids are written in cache table.
  145. */
  146. function testNoEmptyCids() {
  147. $this->drupalGet('user/register');
  148. $this->assertFalse(cache_get(''), 'No cache entry is written with an empty cid.');
  149. }
  150. }
  151. /**
  152. * Test cache_get_multiple().
  153. */
  154. class CacheGetMultipleUnitTest extends CacheTestCase {
  155. public static function getInfo() {
  156. return array(
  157. 'name' => 'Fetching multiple cache items',
  158. 'description' => 'Confirm that multiple records are fetched correctly.',
  159. 'group' => 'Cache',
  160. );
  161. }
  162. function setUp() {
  163. $this->default_bin = 'cache_page';
  164. parent::setUp();
  165. }
  166. /**
  167. * Test cache_get_multiple().
  168. */
  169. function testCacheMultiple() {
  170. $item1 = $this->randomName(10);
  171. $item2 = $this->randomName(10);
  172. cache_set('item1', $item1, $this->default_bin);
  173. cache_set('item2', $item2, $this->default_bin);
  174. $this->assertTrue($this->checkCacheExists('item1', $item1), 'Item 1 is cached.');
  175. $this->assertTrue($this->checkCacheExists('item2', $item2), 'Item 2 is cached.');
  176. // Fetch both records from the database with 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, 'Item was returned from cache successfully.');
  180. $this->assertEqual($items['item2']->data, $item2, 'Item was returned from cache successfully.');
  181. // Remove one item from the cache.
  182. cache_clear_all('item2', $this->default_bin);
  183. // Confirm that only one item is returned by cache_get_multiple().
  184. $item_ids = array('item1', 'item2');
  185. $items = cache_get_multiple($item_ids, $this->default_bin);
  186. $this->assertEqual($items['item1']->data, $item1, 'Item was returned from cache successfully.');
  187. $this->assertFalse(isset($items['item2']), 'Item was not returned from the cache.');
  188. $this->assertTrue(count($items) == 1, 'Only valid cache entries returned.');
  189. }
  190. }
  191. /**
  192. * Test cache clearing methods.
  193. */
  194. class CacheClearCase extends CacheTestCase {
  195. public static function getInfo() {
  196. return array(
  197. 'name' => 'Cache clear test',
  198. 'description' => 'Check our clearing is done the proper way.',
  199. 'group' => 'Cache'
  200. );
  201. }
  202. function setUp() {
  203. $this->default_bin = 'cache_page';
  204. $this->default_value = $this->randomName(10);
  205. parent::setUp();
  206. }
  207. /**
  208. * Test clearing using a cid.
  209. */
  210. function testClearCid() {
  211. cache_set('test_cid_clear', $this->default_value, $this->default_bin);
  212. $this->assertCacheExists(t('Cache was set for clearing cid.'), $this->default_value, 'test_cid_clear');
  213. cache_clear_all('test_cid_clear', $this->default_bin);
  214. $this->assertCacheRemoved(t('Cache was removed after clearing cid.'), 'test_cid_clear');
  215. cache_set('test_cid_clear1', $this->default_value, $this->default_bin);
  216. cache_set('test_cid_clear2', $this->default_value, $this->default_bin);
  217. $this->assertTrue($this->checkCacheExists('test_cid_clear1', $this->default_value)
  218. && $this->checkCacheExists('test_cid_clear2', $this->default_value),
  219. 'Two caches were created for checking cid "*" with wildcard false.');
  220. cache_clear_all('*', $this->default_bin);
  221. $this->assertTrue($this->checkCacheExists('test_cid_clear1', $this->default_value)
  222. && $this->checkCacheExists('test_cid_clear2', $this->default_value),
  223. 'Two caches still exists after clearing cid "*" with wildcard false.');
  224. }
  225. /**
  226. * Test clearing using wildcard.
  227. */
  228. function testClearWildcard() {
  229. cache_set('test_cid_clear1', $this->default_value, $this->default_bin);
  230. cache_set('test_cid_clear2', $this->default_value, $this->default_bin);
  231. $this->assertTrue($this->checkCacheExists('test_cid_clear1', $this->default_value)
  232. && $this->checkCacheExists('test_cid_clear2', $this->default_value),
  233. 'Two caches were created for checking cid "*" with wildcard true.');
  234. cache_clear_all('*', $this->default_bin, TRUE);
  235. $this->assertFalse($this->checkCacheExists('test_cid_clear1', $this->default_value)
  236. || $this->checkCacheExists('test_cid_clear2', $this->default_value),
  237. 'Two caches removed after clearing cid "*" with wildcard true.');
  238. cache_set('test_cid_clear1', $this->default_value, $this->default_bin);
  239. cache_set('test_cid_clear2', $this->default_value, $this->default_bin);
  240. $this->assertTrue($this->checkCacheExists('test_cid_clear1', $this->default_value)
  241. && $this->checkCacheExists('test_cid_clear2', $this->default_value),
  242. 'Two caches were created for checking cid substring with wildcard true.');
  243. cache_clear_all('test_', $this->default_bin, TRUE);
  244. $this->assertFalse($this->checkCacheExists('test_cid_clear1', $this->default_value)
  245. || $this->checkCacheExists('test_cid_clear2', $this->default_value),
  246. 'Two caches removed after clearing cid substring with wildcard true.');
  247. }
  248. /**
  249. * Test clearing using an array.
  250. */
  251. function testClearArray() {
  252. // Create three cache entries.
  253. cache_set('test_cid_clear1', $this->default_value, $this->default_bin);
  254. cache_set('test_cid_clear2', $this->default_value, $this->default_bin);
  255. cache_set('test_cid_clear3', $this->default_value, $this->default_bin);
  256. $this->assertTrue($this->checkCacheExists('test_cid_clear1', $this->default_value)
  257. && $this->checkCacheExists('test_cid_clear2', $this->default_value)
  258. && $this->checkCacheExists('test_cid_clear3', $this->default_value),
  259. 'Three cache entries were created.');
  260. // Clear two entries using an array.
  261. cache_clear_all(array('test_cid_clear1', 'test_cid_clear2'), $this->default_bin);
  262. $this->assertFalse($this->checkCacheExists('test_cid_clear1', $this->default_value)
  263. || $this->checkCacheExists('test_cid_clear2', $this->default_value),
  264. 'Two cache entries removed after clearing with an array.');
  265. $this->assertTrue($this->checkCacheExists('test_cid_clear3', $this->default_value),
  266. 'Entry was not cleared from the cache');
  267. // Set the cache clear threshold to 2 to confirm that the full bin is cleared
  268. // when the threshold is exceeded.
  269. variable_set('cache_clear_threshold', 2);
  270. cache_set('test_cid_clear1', $this->default_value, $this->default_bin);
  271. cache_set('test_cid_clear2', $this->default_value, $this->default_bin);
  272. $this->assertTrue($this->checkCacheExists('test_cid_clear1', $this->default_value)
  273. && $this->checkCacheExists('test_cid_clear2', $this->default_value),
  274. 'Two cache entries were created.');
  275. cache_clear_all(array('test_cid_clear1', 'test_cid_clear2', 'test_cid_clear3'), $this->default_bin);
  276. $this->assertFalse($this->checkCacheExists('test_cid_clear1', $this->default_value)
  277. || $this->checkCacheExists('test_cid_clear2', $this->default_value)
  278. || $this->checkCacheExists('test_cid_clear3', $this->default_value),
  279. 'All cache entries removed when the array exceeded the cache clear threshold.');
  280. }
  281. /**
  282. * Test drupal_flush_all_caches().
  283. */
  284. function testFlushAllCaches() {
  285. // Create cache entries for each flushed cache bin.
  286. $bins = array('cache', 'cache_filter', 'cache_page', 'cache_boostrap', 'cache_path');
  287. $bins = array_merge(module_invoke_all('flush_caches'), $bins);
  288. foreach ($bins as $id => $bin) {
  289. $id = 'test_cid_clear' . $id;
  290. cache_set($id, $this->default_value, $bin);
  291. }
  292. // Remove all caches then make sure that they are cleared.
  293. drupal_flush_all_caches();
  294. foreach ($bins as $id => $bin) {
  295. $id = 'test_cid_clear' . $id;
  296. $this->assertFalse($this->checkCacheExists($id, $this->default_value, $bin), format_string('All cache entries removed from @bin.', array('@bin' => $bin)));
  297. }
  298. }
  299. /**
  300. * Test DrupalDatabaseCache::isValidBin().
  301. */
  302. function testIsValidBin() {
  303. // Retrieve existing cache bins.
  304. $valid_bins = array('cache', 'cache_filter', 'cache_page', 'cache_boostrap', 'cache_path');
  305. $valid_bins = array_merge(module_invoke_all('flush_caches'), $valid_bins);
  306. foreach ($valid_bins as $id => $bin) {
  307. $cache = _cache_get_object($bin);
  308. if ($cache instanceof DrupalDatabaseCache) {
  309. $this->assertTrue($cache->isValidBin(), format_string('Cache bin @bin is valid.', array('@bin' => $bin)));
  310. }
  311. }
  312. // Check for non-cache tables and invalid bins.
  313. $invalid_bins = array('block', 'filter', 'missing_table', $this->randomName());
  314. foreach ($invalid_bins as $id => $bin) {
  315. $cache = _cache_get_object($bin);
  316. if ($cache instanceof DrupalDatabaseCache) {
  317. $this->assertFalse($cache->isValidBin(), format_string('Cache bin @bin is not valid.', array('@bin' => $bin)));
  318. }
  319. }
  320. }
  321. /**
  322. * Test minimum cache lifetime.
  323. */
  324. function testMinimumCacheLifetime() {
  325. // Set a minimum/maximum cache lifetime.
  326. $this->setupLifetime(300);
  327. // Login as a newly-created user.
  328. $account = $this->drupalCreateUser(array());
  329. $this->drupalLogin($account);
  330. // Set two cache objects in different bins.
  331. $data = $this->randomName(100);
  332. cache_set($data, $data, 'cache', CACHE_TEMPORARY);
  333. $cached = cache_get($data);
  334. $this->assertTrue(isset($cached->data) && $cached->data === $data, 'Cached item retrieved.');
  335. cache_set($data, $data, 'cache_page', CACHE_TEMPORARY);
  336. // Expire temporary items in the 'page' bin.
  337. cache_clear_all(NULL, 'cache_page');
  338. // Since the database cache uses REQUEST_TIME, set the $_SESSION variable
  339. // manually to force it to the current time.
  340. $_SESSION['cache_expiration']['cache_page'] = time();
  341. // Items in the default cache bin should not be expired.
  342. $cached = cache_get($data);
  343. $this->assertTrue(isset($cached->data) && $cached->data == $data, 'Cached item retrieved');
  344. // Despite the minimum cache lifetime, the item in the 'page' bin should
  345. // be invalidated for the current user.
  346. $cached = cache_get($data, 'cache_page');
  347. $this->assertFalse($cached, 'Cached item was invalidated');
  348. }
  349. }
  350. /**
  351. * Test cache_is_empty() function.
  352. */
  353. class CacheIsEmptyCase extends CacheTestCase {
  354. public static function getInfo() {
  355. return array(
  356. 'name' => 'Cache emptiness test',
  357. 'description' => 'Check if a cache bin is empty after performing clear operations.',
  358. 'group' => 'Cache'
  359. );
  360. }
  361. function setUp() {
  362. $this->default_bin = 'cache_page';
  363. $this->default_value = $this->randomName(10);
  364. parent::setUp();
  365. }
  366. /**
  367. * Test clearing using a cid.
  368. */
  369. function testIsEmpty() {
  370. // Clear the cache bin.
  371. cache_clear_all('*', $this->default_bin);
  372. $this->assertTrue(cache_is_empty($this->default_bin), 'The cache bin is empty');
  373. // Add some data to the cache bin.
  374. cache_set($this->default_cid, $this->default_value, $this->default_bin);
  375. $this->assertCacheExists(t('Cache was set.'), $this->default_value, $this->default_cid);
  376. $this->assertFalse(cache_is_empty($this->default_bin), 'The cache bin is not empty');
  377. // Remove the cached data.
  378. cache_clear_all($this->default_cid, $this->default_bin);
  379. $this->assertCacheRemoved(t('Cache was removed.'), $this->default_cid);
  380. $this->assertTrue(cache_is_empty($this->default_bin), 'The cache bin is empty');
  381. }
  382. }