InstallTest.php 5.48 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
<?php

/**
 * @file
 * Definition of Drupal\system\Tests\Cache\InstallTest.
 */

namespace Drupal\system\Tests\Cache;

use Drupal\Core\Cache\DatabaseBackend;
use Drupal\Core\Cache\InstallBackend;
12
use Exception;
13
14
15
16
17

/**
 * Tests the behavior of the cache backend used for installing Drupal.
 */
class InstallTest extends CacheTestBase {
18
19
20
21
22
23
24
25

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('cache_test');

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  protected $profile = 'testing';

  public static function getInfo() {
    return array(
      'name' => 'Cache install test',
      'description' => 'Confirm that the cache backend used for installing Drupal works correctly.',
      'group' => 'Cache',
    );
  }

  /**
   * Tests the behavior of the cache backend used for installing Drupal.
   *
   * While Drupal is being installed, the cache system must deal with the fact
   * that the database is not initially available, and, after it is available,
   * the fact that other requests that take place while Drupal is being
   * installed (for example, Ajax requests triggered via the installer's user
   * interface) may cache data in the database, which needs to be cleared when
   * the installer makes changes that would result in it becoming stale.
   *
   * We cannot test this process directly, so instead we test it by switching
   * between the normal database cache (Drupal\Core\Cache\DatabaseBackend) and
   * the installer cache (Drupal\Core\Cache\InstallBackend) while setting and
   * clearing various items in the cache.
   */
  function testCacheInstall() {
    $database_cache = new DatabaseBackend('test');
    $install_cache = new InstallBackend('test');

    // Store an item in the database cache, and confirm that the installer's
    // cache backend recognizes that the cache is not empty.
    $database_cache->set('cache_one', 'One');
    $this->assertFalse($install_cache->isEmpty());
    $database_cache->delete('cache_one');
    $this->assertTrue($install_cache->isEmpty());

    // Store an item in the database cache, then use the installer's cache
    // backend to delete it. Afterwards, confirm that it is no longer in the
    // database cache.
    $database_cache->set('cache_one', 'One');
    $this->assertEqual($database_cache->get('cache_one')->data, 'One');
    $install_cache->delete('cache_one');
    $this->assertFalse($database_cache->get('cache_one'));

    // Store multiple items in the database cache, then use the installer's
    // cache backend to delete them. Afterwards, confirm that they are no
    // longer in the database cache.
    $database_cache->set('cache_one', 'One');
    $database_cache->set('cache_two', 'Two');
    $this->assertEqual($database_cache->get('cache_one')->data, 'One');
    $this->assertEqual($database_cache->get('cache_two')->data, 'Two');
    $install_cache->deleteMultiple(array('cache_one', 'cache_two'));
    $this->assertFalse($database_cache->get('cache_one'));
    $this->assertFalse($database_cache->get('cache_two'));

    // Store multiple items in the database cache, then use the installer's
    // cache backend to delete them via a wildcard prefix. Afterwards, confirm
    // that they are no longer in the database cache.
    $database_cache->set('cache_one', 'One');
    $database_cache->set('cache_two', 'Two');
    $this->assertEqual($database_cache->get('cache_one')->data, 'One');
    $this->assertEqual($database_cache->get('cache_two')->data, 'Two');
    $install_cache->deletePrefix('cache_');
    $this->assertFalse($database_cache->get('cache_one'));
    $this->assertFalse($database_cache->get('cache_two'));

    // Store multiple items in the database cache, then use the installer's
    // cache backend to flush the cache. Afterwards, confirm that they are no
    // longer in the database cache.
    $database_cache->set('cache_one', 'One');
    $database_cache->set('cache_two', 'Two');
    $this->assertEqual($database_cache->get('cache_one')->data, 'One');
    $this->assertEqual($database_cache->get('cache_two')->data, 'Two');
    $install_cache->flush();
    $this->assertFalse($database_cache->get('cache_one'));
    $this->assertFalse($database_cache->get('cache_two'));

    // Invalidate a tag using the installer cache, then check that the
    // invalidation was recorded correctly in the database.
    $install_cache->invalidateTags(array('tag'));
    $invalidations = db_query("SELECT invalidations FROM {cache_tags} WHERE tag = 'tag'")->fetchField();
    $this->assertEqual($invalidations, 1);

    // For each cache clearing event that we tried above, try it again after
    // dropping the {cache_test} table. This simulates the early stages of the
    // installer (when the database cache tables won't be available yet) and
    // thereby confirms that the installer's cache backend does not produce
    // errors if the installer ever calls any code early on that tries to clear
    // items from the cache.
    db_drop_table('cache_test');
    try {
      $install_cache->isEmpty();
      $install_cache->delete('cache_one');
      $install_cache->deleteMultiple(array('cache_one', 'cache_two'));
      $install_cache->deletePrefix('cache_');
      $install_cache->flush();
      $install_cache->expire();
      $install_cache->garbageCollection();
      $install_cache->invalidateTags(array('tag'));
      $this->pass("The installer's cache backend can be used even when the cache database tables are unavailable.");
    }
    catch (Exception $e) {
      $this->fail("The installer's cache backend can be used even when the cache database tables are unavailable.");
    }
  }
}