cache.test 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. * Test no empty cids are written in cache table.
  145. */
  146. function testNoEmptyCids() {
  147. $this->drupalGet('user/register');
  148. $this->assertFalse(cache_get(''), t('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), t('Item 1 is cached.'));
  175. $this->assertTrue($this->checkCacheExists('item2', $item2), t('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, t('Item was returned from cache successfully.'));
  180. $this->assertEqual($items['item2']->data, $item2, t('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, t('Item was returned from cache successfully.'));
  187. $this->assertFalse(isset($items['item2']), t('Item was not returned from the cache.'));
  188. $this->assertTrue(count($items) == 1, t('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. t('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. t('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. t('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. t('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. t('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. t('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. t('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. t('Two cache entries removed after clearing with an array.'));
  265. $this->assertTrue($this->checkCacheExists('test_cid_clear3', $this->default_value),
  266. t('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. t('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. t('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), t('All cache entries removed from @bin.', array('@bin' => $bin)));
  297. }
  298. }
  299. /**
  300. * Test minimum cache lifetime.
  301. */
  302. function testMinimumCacheLifetime() {
  303. // Set a minimum/maximum cache lifetime.
  304. $this->setupLifetime(300);
  305. // Login as a newly-created user.
  306. $account = $this->drupalCreateUser(array());
  307. $this->drupalLogin($account);
  308. // Set two cache objects in different bins.
  309. $data = $this->randomName(100);
  310. cache_set($data, $data, 'cache', CACHE_TEMPORARY);
  311. $cached = cache_get($data);
  312. $this->assertTrue(isset($cached->data) && $cached->data === $data, 'Cached item retrieved.');
  313. cache_set($data, $data, 'cache_page', CACHE_TEMPORARY);
  314. // Expire temporary items in the 'page' bin.
  315. cache_clear_all(NULL, 'cache_page');
  316. // Since the database cache uses REQUEST_TIME, set the $_SESSION variable
  317. // manually to force it to the current time.
  318. $_SESSION['cache_expiration']['cache_page'] = time();
  319. // Items in the default cache bin should not be expired.
  320. $cached = cache_get($data);
  321. $this->assertTrue(isset($cached->data) && $cached->data == $data, 'Cached item retrieved');
  322. // Despite the minimum cache lifetime, the item in the 'page' bin should
  323. // be invalidated for the current user.
  324. $cached = cache_get($data, 'cache_page');
  325. $this->assertFalse($cached, 'Cached item was invalidated');
  326. }
  327. }
  328. /**
  329. * Test cache_is_empty() function.
  330. */
  331. class CacheIsEmptyCase extends CacheTestCase {
  332. public static function getInfo() {
  333. return array(
  334. 'name' => 'Cache emptiness test',
  335. 'description' => 'Check if a cache bin is empty after performing clear operations.',
  336. 'group' => 'Cache'
  337. );
  338. }
  339. function setUp() {
  340. $this->default_bin = 'cache_page';
  341. $this->default_value = $this->randomName(10);
  342. parent::setUp();
  343. }
  344. /**
  345. * Test clearing using a cid.
  346. */
  347. function testIsEmpty() {
  348. // Clear the cache bin.
  349. cache_clear_all('*', $this->default_bin);
  350. $this->assertTrue(cache_is_empty($this->default_bin), t('The cache bin is empty'));
  351. // Add some data to the cache bin.
  352. cache_set($this->default_cid, $this->default_value, $this->default_bin);
  353. $this->assertCacheExists(t('Cache was set.'), $this->default_value, $this->default_cid);
  354. $this->assertFalse(cache_is_empty($this->default_bin), t('The cache bin is not empty'));
  355. // Remove the cached data.
  356. cache_clear_all($this->default_cid, $this->default_bin);
  357. $this->assertCacheRemoved(t('Cache was removed.'), $this->default_cid);
  358. $this->assertTrue(cache_is_empty($this->default_bin), t('The cache bin is empty'));
  359. }
  360. }