| Drupal | PHP Cross Reference | Content Management Systems |
1 <?php 2 3 class CacheTestCase extends DrupalWebTestCase { 4 protected $default_bin = 'cache'; 5 protected $default_cid = 'test_temporary'; 6 protected $default_value = 'CacheTest'; 7 8 /** 9 * Check whether or not a cache entry exists. 10 * 11 * @param $cid 12 * The cache id. 13 * @param $var 14 * The variable the cache should contain. 15 * @param $bin 16 * The bin the cache item was stored in. 17 * @return 18 * TRUE on pass, FALSE on fail. 19 */ 20 protected function checkCacheExists($cid, $var, $bin = NULL) { 21 if ($bin == NULL) { 22 $bin = $this->default_bin; 23 } 24 25 $cache = cache_get($cid, $bin); 26 27 return isset($cache->data) && $cache->data == $var; 28 } 29 30 /** 31 * Assert or a cache entry exists. 32 * 33 * @param $message 34 * Message to display. 35 * @param $var 36 * The variable the cache should contain. 37 * @param $cid 38 * The cache id. 39 * @param $bin 40 * The bin the cache item was stored in. 41 */ 42 protected function assertCacheExists($message, $var = NULL, $cid = NULL, $bin = NULL) { 43 if ($bin == NULL) { 44 $bin = $this->default_bin; 45 } 46 if ($cid == NULL) { 47 $cid = $this->default_cid; 48 } 49 if ($var == NULL) { 50 $var = $this->default_value; 51 } 52 53 $this->assertTrue($this->checkCacheExists($cid, $var, $bin), $message); 54 } 55 56 /** 57 * Assert or a cache entry has been removed. 58 * 59 * @param $message 60 * Message to display. 61 * @param $cid 62 * The cache id. 63 * @param $bin 64 * The bin the cache item was stored in. 65 */ 66 function assertCacheRemoved($message, $cid = NULL, $bin = NULL) { 67 if ($bin == NULL) { 68 $bin = $this->default_bin; 69 } 70 if ($cid == NULL) { 71 $cid = $this->default_cid; 72 } 73 74 $cache = cache_get($cid, $bin); 75 $this->assertFalse($cache, $message); 76 } 77 78 /** 79 * Perform the general wipe. 80 * @param $bin 81 * The bin to perform the wipe on. 82 */ 83 protected function generalWipe($bin = NULL) { 84 if ($bin == NULL) { 85 $bin = $this->default_bin; 86 } 87 88 cache_clear_all(NULL, $bin); 89 } 90 91 /** 92 * Setup the lifetime settings for caching. 93 * 94 * @param $time 95 * The time in seconds the cache should minimal live. 96 */ 97 protected function setupLifetime($time) { 98 variable_set('cache_lifetime', $time); 99 variable_set('cache_flush', 0); 100 } 101 } 102 103 class CacheSavingCase extends CacheTestCase { 104 public static function getInfo() { 105 return array( 106 'name' => 'Cache saving test', 107 'description' => 'Check our variables are saved and restored the right way.', 108 'group' => 'Cache' 109 ); 110 } 111 112 /** 113 * Test the saving and restoring of a string. 114 */ 115 function testString() { 116 $this->checkVariable($this->randomName(100)); 117 } 118 119 /** 120 * Test the saving and restoring of an integer. 121 */ 122 function testInteger() { 123 $this->checkVariable(100); 124 } 125 126 /** 127 * Test the saving and restoring of a double. 128 */ 129 function testDouble() { 130 $this->checkVariable(1.29); 131 } 132 133 /** 134 * Test the saving and restoring of an array. 135 */ 136 function testArray() { 137 $this->checkVariable(array('drupal1', 'drupal2' => 'drupal3', 'drupal4' => array('drupal5', 'drupal6'))); 138 } 139 140 /** 141 * Test the saving and restoring of an object. 142 */ 143 function testObject() { 144 $test_object = new stdClass(); 145 $test_object->test1 = $this->randomName(100); 146 $test_object->test2 = 100; 147 $test_object->test3 = array('drupal1', 'drupal2' => 'drupal3', 'drupal4' => array('drupal5', 'drupal6')); 148 149 cache_set('test_object', $test_object, 'cache'); 150 $cache = cache_get('test_object', 'cache'); 151 $this->assertTrue(isset($cache->data) && $cache->data == $test_object, t('Object is saved and restored properly.')); 152 } 153 154 /** 155 * Check or a variable is stored and restored properly. 156 */ 157 function checkVariable($var) { 158 cache_set('test_var', $var, 'cache'); 159 $cache = cache_get('test_var', 'cache'); 160 $this->assertTrue(isset($cache->data) && $cache->data === $var, t('@type is saved and restored properly.', array('@type' => ucfirst(gettype($var))))); 161 } 162 163 /** 164 * Test no empty cids are written in cache table. 165 */ 166 function testNoEmptyCids() { 167 $this->drupalGet('user/register'); 168 $this->assertFalse(cache_get(''), t('No cache entry is written with an empty cid.')); 169 } 170 } 171 172 /** 173 * Test cache_get_multiple(). 174 */ 175 class CacheGetMultipleUnitTest extends CacheTestCase { 176 177 public static function getInfo() { 178 return array( 179 'name' => 'Fetching multiple cache items', 180 'description' => 'Confirm that multiple records are fetched correctly.', 181 'group' => 'Cache', 182 ); 183 } 184 185 function setUp() { 186 $this->default_bin = 'cache_page'; 187 parent::setUp(); 188 } 189 190 /** 191 * Test cache_get_multiple(). 192 */ 193 function testCacheMultiple() { 194 $item1 = $this->randomName(10); 195 $item2 = $this->randomName(10); 196 cache_set('item1', $item1, $this->default_bin); 197 cache_set('item2', $item2, $this->default_bin); 198 $this->assertTrue($this->checkCacheExists('item1', $item1), t('Item 1 is cached.')); 199 $this->assertTrue($this->checkCacheExists('item2', $item2), t('Item 2 is cached.')); 200 201 // Fetch both records from the database with cache_get_multiple(). 202 $item_ids = array('item1', 'item2'); 203 $items = cache_get_multiple($item_ids, $this->default_bin); 204 $this->assertEqual($items['item1']->data, $item1, t('Item was returned from cache successfully.')); 205 $this->assertEqual($items['item2']->data, $item2, t('Item was returned from cache successfully.')); 206 207 // Remove one item from the cache. 208 cache_clear_all('item2', $this->default_bin); 209 210 // Confirm that only one item is returned by cache_get_multiple(). 211 $item_ids = array('item1', 'item2'); 212 $items = cache_get_multiple($item_ids, $this->default_bin); 213 $this->assertEqual($items['item1']->data, $item1, t('Item was returned from cache successfully.')); 214 $this->assertFalse(isset($items['item2']), t('Item was not returned from the cache.')); 215 $this->assertTrue(count($items) == 1, t('Only valid cache entries returned.')); 216 } 217 } 218 219 /** 220 * Test cache clearing methods. 221 */ 222 class CacheClearCase extends CacheTestCase { 223 public static function getInfo() { 224 return array( 225 'name' => 'Cache clear test', 226 'description' => 'Check our clearing is done the proper way.', 227 'group' => 'Cache' 228 ); 229 } 230 231 function setUp() { 232 $this->default_bin = 'cache_page'; 233 $this->default_value = $this->randomName(10); 234 235 parent::setUp(); 236 } 237 238 /** 239 * Test clearing using a cid. 240 */ 241 function testClearCid() { 242 cache_set('test_cid_clear', $this->default_value, $this->default_bin); 243 244 $this->assertCacheExists(t('Cache was set for clearing cid.'), $this->default_value, 'test_cid_clear'); 245 cache_clear_all('test_cid_clear', $this->default_bin); 246 247 $this->assertCacheRemoved(t('Cache was removed after clearing cid.'), 'test_cid_clear'); 248 249 cache_set('test_cid_clear1', $this->default_value, $this->default_bin); 250 cache_set('test_cid_clear2', $this->default_value, $this->default_bin); 251 $this->assertTrue($this->checkCacheExists('test_cid_clear1', $this->default_value) 252 && $this->checkCacheExists('test_cid_clear2', $this->default_value), 253 t('Two caches were created for checking cid "*" with wildcard false.')); 254 cache_clear_all('*', $this->default_bin); 255 $this->assertTrue($this->checkCacheExists('test_cid_clear1', $this->default_value) 256 && $this->checkCacheExists('test_cid_clear2', $this->default_value), 257 t('Two caches still exists after clearing cid "*" with wildcard false.')); 258 } 259 260 /** 261 * Test clearing using wildcard. 262 */ 263 function testClearWildcard() { 264 cache_set('test_cid_clear1', $this->default_value, $this->default_bin); 265 cache_set('test_cid_clear2', $this->default_value, $this->default_bin); 266 $this->assertTrue($this->checkCacheExists('test_cid_clear1', $this->default_value) 267 && $this->checkCacheExists('test_cid_clear2', $this->default_value), 268 t('Two caches were created for checking cid "*" with wildcard true.')); 269 cache_clear_all('*', $this->default_bin, TRUE); 270 $this->assertFalse($this->checkCacheExists('test_cid_clear1', $this->default_value) 271 || $this->checkCacheExists('test_cid_clear2', $this->default_value), 272 t('Two caches removed after clearing cid "*" with wildcard true.')); 273 274 cache_set('test_cid_clear1', $this->default_value, $this->default_bin); 275 cache_set('test_cid_clear2', $this->default_value, $this->default_bin); 276 $this->assertTrue($this->checkCacheExists('test_cid_clear1', $this->default_value) 277 && $this->checkCacheExists('test_cid_clear2', $this->default_value), 278 t('Two caches were created for checking cid substring with wildcard true.')); 279 cache_clear_all('test_', $this->default_bin, TRUE); 280 $this->assertFalse($this->checkCacheExists('test_cid_clear1', $this->default_value) 281 || $this->checkCacheExists('test_cid_clear2', $this->default_value), 282 t('Two caches removed after clearing cid substring with wildcard true.')); 283 } 284 285 /** 286 * Test clearing using an array. 287 */ 288 function testClearArray() { 289 // Create three cache entries. 290 cache_set('test_cid_clear1', $this->default_value, $this->default_bin); 291 cache_set('test_cid_clear2', $this->default_value, $this->default_bin); 292 cache_set('test_cid_clear3', $this->default_value, $this->default_bin); 293 $this->assertTrue($this->checkCacheExists('test_cid_clear1', $this->default_value) 294 && $this->checkCacheExists('test_cid_clear2', $this->default_value) 295 && $this->checkCacheExists('test_cid_clear3', $this->default_value), 296 t('Three cache entries were created.')); 297 298 // Clear two entries using an array. 299 cache_clear_all(array('test_cid_clear1', 'test_cid_clear2'), $this->default_bin); 300 $this->assertFalse($this->checkCacheExists('test_cid_clear1', $this->default_value) 301 || $this->checkCacheExists('test_cid_clear2', $this->default_value), 302 t('Two cache entries removed after clearing with an array.')); 303 304 $this->assertTrue($this->checkCacheExists('test_cid_clear3', $this->default_value), 305 t('Entry was not cleared from the cache')); 306 307 // Set the cache clear threshold to 2 to confirm that the full bin is cleared 308 // when the threshold is exceeded. 309 variable_set('cache_clear_threshold', 2); 310 cache_set('test_cid_clear1', $this->default_value, $this->default_bin); 311 cache_set('test_cid_clear2', $this->default_value, $this->default_bin); 312 $this->assertTrue($this->checkCacheExists('test_cid_clear1', $this->default_value) 313 && $this->checkCacheExists('test_cid_clear2', $this->default_value), 314 t('Two cache entries were created.')); 315 cache_clear_all(array('test_cid_clear1', 'test_cid_clear2', 'test_cid_clear3'), $this->default_bin); 316 $this->assertFalse($this->checkCacheExists('test_cid_clear1', $this->default_value) 317 || $this->checkCacheExists('test_cid_clear2', $this->default_value) 318 || $this->checkCacheExists('test_cid_clear3', $this->default_value), 319 t('All cache entries removed when the array exceeded the cache clear threshold.')); 320 } 321 322 /** 323 * Test drupal_flush_all_caches(). 324 */ 325 function testFlushAllCaches() { 326 // Create cache entries for each flushed cache bin. 327 $bins = array('cache', 'cache_filter', 'cache_page', 'cache_boostrap', 'cache_path'); 328 $bins = array_merge(module_invoke_all('flush_caches'), $bins); 329 foreach ($bins as $id => $bin) { 330 $id = 'test_cid_clear' . $id; 331 cache_set($id, $this->default_value, $bin); 332 } 333 334 // Remove all caches then make sure that they are cleared. 335 drupal_flush_all_caches(); 336 337 foreach ($bins as $id => $bin) { 338 $id = 'test_cid_clear' . $id; 339 $this->assertFalse($this->checkCacheExists($id, $this->default_value, $bin), t('All cache entries removed from @bin.', array('@bin' => $bin))); 340 } 341 } 342 343 /** 344 * Test minimum cache lifetime. 345 */ 346 function testMinimumCacheLifetime() { 347 // Set a minimum/maximum cache lifetime. 348 $this->setupLifetime(300); 349 // Login as a newly-created user. 350 $account = $this->drupalCreateUser(array()); 351 $this->drupalLogin($account); 352 353 // Set two cache objects in different bins. 354 $data = $this->randomName(100); 355 cache_set($data, $data, 'cache', CACHE_TEMPORARY); 356 $cached = cache_get($data); 357 $this->assertTrue(isset($cached->data) && $cached->data === $data, 'Cached item retrieved.'); 358 cache_set($data, $data, 'cache_page', CACHE_TEMPORARY); 359 360 // Expire temporary items in the 'page' bin. 361 cache_clear_all(NULL, 'cache_page'); 362 363 // Since the database cache uses REQUEST_TIME, set the $_SESSION variable 364 // manually to force it to the current time. 365 $_SESSION['cache_expiration']['cache_page'] = time(); 366 367 // Items in the default cache bin should not be expired. 368 $cached = cache_get($data); 369 $this->assertTrue(isset($cached->data) && $cached->data == $data, 'Cached item retrieved'); 370 371 // Despite the minimum cache lifetime, the item in the 'page' bin should 372 // be invalidated for the current user. 373 $cached = cache_get($data, 'cache_page'); 374 $this->assertFalse($cached, 'Cached item was invalidated'); 375 } 376 } 377 378 /** 379 * Test cache_is_empty() function. 380 */ 381 class CacheIsEmptyCase extends CacheTestCase { 382 public static function getInfo() { 383 return array( 384 'name' => 'Cache emptiness test', 385 'description' => 'Check if a cache bin is empty after performing clear operations.', 386 'group' => 'Cache' 387 ); 388 } 389 390 function setUp() { 391 $this->default_bin = 'cache_page'; 392 $this->default_value = $this->randomName(10); 393 394 parent::setUp(); 395 } 396 397 /** 398 * Test clearing using a cid. 399 */ 400 function testIsEmpty() { 401 // Clear the cache bin. 402 cache_clear_all('*', $this->default_bin); 403 $this->assertTrue(cache_is_empty($this->default_bin), t('The cache bin is empty')); 404 // Add some data to the cache bin. 405 cache_set($this->default_cid, $this->default_value, $this->default_bin); 406 $this->assertCacheExists(t('Cache was set.'), $this->default_value, $this->default_cid); 407 $this->assertFalse(cache_is_empty($this->default_bin), t('The cache bin is not empty')); 408 // Remove the cached data. 409 cache_clear_all($this->default_cid, $this->default_bin); 410 $this->assertCacheRemoved(t('Cache was removed.'), $this->default_cid); 411 $this->assertTrue(cache_is_empty($this->default_bin), t('The cache bin is empty')); 412 } 413 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
title