Projet

Général

Profil

Paste
Télécharger (15,4 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / simpletest / tests / cache.test @ 76597ebf

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, '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, format_string('@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(''), '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), 'Item 1 is cached.');
199
    $this->assertTrue($this->checkCacheExists('item2', $item2), '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, 'Item was returned from cache successfully.');
205
    $this->assertEqual($items['item2']->data, $item2, '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, 'Item was returned from cache successfully.');
214
    $this->assertFalse(isset($items['item2']), 'Item was not returned from the cache.');
215
    $this->assertTrue(count($items) == 1, '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
                      '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
                      '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
                      '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
                      '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
                      '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
                      '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
                      '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
                       'Two cache entries removed after clearing with an array.');
303

    
304
    $this->assertTrue($this->checkCacheExists('test_cid_clear3', $this->default_value),
305
                      '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
                      '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
                       '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), format_string('All cache entries removed from @bin.', array('@bin' => $bin)));
340
    }
341
  }
342

    
343
  /**
344
   * Test DrupalDatabaseCache::isValidBin().
345
   */
346
  function testIsValidBin() {
347
    // Retrieve existing cache bins.
348
    $valid_bins = array('cache', 'cache_filter', 'cache_page', 'cache_boostrap', 'cache_path');
349
    $valid_bins = array_merge(module_invoke_all('flush_caches'), $valid_bins);
350
    foreach ($valid_bins as $id => $bin) {
351
      $cache = _cache_get_object($bin);
352
      if ($cache instanceof DrupalDatabaseCache) {
353
        $this->assertTrue($cache->isValidBin(), format_string('Cache bin @bin is valid.', array('@bin' => $bin)));
354
      }
355
    }
356

    
357
    // Check for non-cache tables and invalid bins.
358
    $invalid_bins = array('block', 'filter', 'missing_table', $this->randomName());
359
    foreach ($invalid_bins as $id => $bin) {
360
      $cache = _cache_get_object($bin);
361
      if ($cache instanceof DrupalDatabaseCache) {
362
        $this->assertFalse($cache->isValidBin(), format_string('Cache bin @bin is not valid.', array('@bin' => $bin)));
363
      }
364
    }
365
  }
366

    
367
  /**
368
   * Test minimum cache lifetime.
369
   */
370
  function testMinimumCacheLifetime() {
371
    // Set a minimum/maximum cache lifetime.
372
    $this->setupLifetime(300);
373
    // Login as a newly-created user.
374
    $account = $this->drupalCreateUser(array());
375
    $this->drupalLogin($account);
376

    
377
    // Set two cache objects in different bins.
378
    $data = $this->randomName(100);
379
    cache_set($data, $data, 'cache', CACHE_TEMPORARY);
380
    $cached = cache_get($data);
381
    $this->assertTrue(isset($cached->data) && $cached->data === $data, 'Cached item retrieved.');
382
    cache_set($data, $data, 'cache_page', CACHE_TEMPORARY);
383

    
384
    // Expire temporary items in the 'page' bin.
385
    cache_clear_all(NULL, 'cache_page');
386

    
387
    // Since the database cache uses REQUEST_TIME, set the $_SESSION variable
388
    // manually to force it to the current time.
389
    $_SESSION['cache_expiration']['cache_page'] = time();
390

    
391
    // Items in the default cache bin should not be expired.
392
    $cached = cache_get($data);
393
    $this->assertTrue(isset($cached->data) && $cached->data == $data, 'Cached item retrieved');
394

    
395
    // Despite the minimum cache lifetime, the item in the 'page' bin should
396
    // be invalidated for the current user.
397
    $cached = cache_get($data, 'cache_page');
398
    $this->assertFalse($cached, 'Cached item was invalidated');
399
  }
400
}
401

    
402
/**
403
 * Test cache_is_empty() function.
404
 */
405
class CacheIsEmptyCase extends CacheTestCase {
406
  public static function getInfo() {
407
    return array(
408
      'name' => 'Cache emptiness test',
409
      'description' => 'Check if a cache bin is empty after performing clear operations.',
410
      'group' => 'Cache'
411
    );
412
  }
413

    
414
  function setUp() {
415
    $this->default_bin = 'cache_page';
416
    $this->default_value = $this->randomName(10);
417

    
418
    parent::setUp();
419
  }
420

    
421
  /**
422
   * Test clearing using a cid.
423
   */
424
  function testIsEmpty() {
425
    // Clear the cache bin.
426
    cache_clear_all('*', $this->default_bin);
427
    $this->assertTrue(cache_is_empty($this->default_bin), 'The cache bin is empty');
428
    // Add some data to the cache bin.
429
    cache_set($this->default_cid, $this->default_value, $this->default_bin);
430
    $this->assertCacheExists(t('Cache was set.'), $this->default_value, $this->default_cid);
431
    $this->assertFalse(cache_is_empty($this->default_bin), 'The cache bin is not empty');
432
    // Remove the cached data.
433
    cache_clear_all($this->default_cid, $this->default_bin);
434
    $this->assertCacheRemoved(t('Cache was removed.'), $this->default_cid);
435
    $this->assertTrue(cache_is_empty($this->default_bin), 'The cache bin is empty');
436
  }
437
}