ViewsDataTest.php 13.5 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Definition of Drupal\views\Tests\ViewsDataTest.
 */

namespace Drupal\views\Tests;

10
use Drupal\Core\Cache\MemoryCounterBackend;
11
use Drupal\Core\Language\LanguageManager;
12
use Drupal\views\ViewsData;
13

14 15 16 17 18
/**
 * Tests the fetching of views data.
 *
 * @see hook_views_data
 */
19
class ViewsDataTest extends ViewUnitTestBase {
20

21 22 23
  /**
   * Stores the views data cache service used by this test.
   *
24
   * @var \Drupal\views\ViewsData
25
   */
26
  protected $viewsData;
27

28 29 30 31 32 33 34
  /**
   * Stores a count for hook_views_data being invoked.
   *
   * @var int
   */
  protected $count = 0;

35 36 37 38 39 40 41
  /**
   * The memory backend to use for the test.
   *
   * @var \Drupal\Core\Cache\MemoryCounterBackend
   */
  protected $memoryCounterBackend;

42 43
  public static function getInfo() {
    return array(
44
      'name' => 'Views data',
45 46 47 48 49
      'description' => 'Tests the fetching of views data.',
      'group' => 'Views',
    );
  }

50 51 52
  protected function setUp() {
    parent::setUp();

53
    $this->memoryCounterBackend = new MemoryCounterBackend('views_info');
54
    $this->state = $this->container->get('state');
55

56
    $this->initViewsData();
57 58
  }

59
  /**
60
   * Tests the views.views_data service.
61
   *
62
   * @see \Drupal\views\ViewsData
63 64 65
   */
  public function testViewsFetchData() {
    $table_name = 'views_test_data';
66 67 68
    $random_table_name = $this->randomName();
    // Invoke expected data directly from hook_views_data implementations.
    $expected_data = $this->container->get('module_handler')->invokeAll('views_data');
69

70 71 72
    // Verify that views_test_data_views_data() has only been called once after
    // calling clear().
    $this->startCount();
73
    $this->viewsData->get();
74 75
    // Test views data has been invoked.
    $this->assertCountIncrement();
76
    // Clear the storage/cache.
77
    $this->viewsData->clear();
78
    // Get the data again.
79 80 81
    $this->viewsData->get();
    $this->viewsData->get($table_name);
    $this->viewsData->get($random_table_name);
82 83
    // Verify that view_test_data_views_data() has run once.
    $this->assertCountIncrement();
84 85

    // Get the data again.
86 87 88
    $this->viewsData->get();
    $this->viewsData->get($table_name);
    $this->viewsData->get($random_table_name);
89
    // Verify that view_test_data_views_data() has not run again.
90 91 92
    $this->assertCountIncrement(FALSE);

    // Clear the views data, and test all table data.
93
    $this->viewsData->clear();
94
    $this->startCount();
95
    $data = $this->viewsData->get();
96 97 98 99
    $this->assertEqual($data, $expected_data, 'Make sure fetching all views data by works as expected.');
    // Views data should be invoked once.
    $this->assertCountIncrement();
    // Calling get() again, the count for this table should stay the same.
100
    $data = $this->viewsData->get();
101 102 103 104
    $this->assertEqual($data, $expected_data, 'Make sure fetching all cached views data works as expected.');
    $this->assertCountIncrement(FALSE);

    // Clear the views data, and test data for a specific table.
105
    $this->viewsData->clear();
106
    $this->startCount();
107
    $data = $this->viewsData->get($table_name);
108 109 110 111
    $this->assertEqual($data, $expected_data[$table_name], 'Make sure fetching views data by table works as expected.');
    // Views data should be invoked once.
    $this->assertCountIncrement();
    // Calling get() again, the count for this table should stay the same.
112
    $data = $this->viewsData->get($table_name);
113 114 115
    $this->assertEqual($data, $expected_data[$table_name], 'Make sure fetching cached views data by table works as expected.');
    $this->assertCountIncrement(FALSE);
    // Test that this data is present if all views data is returned.
116
    $data = $this->viewsData->get();
117 118 119 120
    $this->assertTrue(isset($data[$table_name]), 'Make sure the views_test_data info appears in the total views data.');
    $this->assertEqual($data[$table_name], $expected_data[$table_name], 'Make sure the views_test_data has the expected values.');

    // Clear the views data, and test data for an invalid table.
121
    $this->viewsData->clear();
122 123
    $this->startCount();
    // All views data should be requested on the first try.
124
    $data = $this->viewsData->get($random_table_name);
125 126 127
    $this->assertEqual($data, array(), 'Make sure fetching views data for an invalid table returns an empty array.');
    $this->assertCountIncrement();
    // Test no data is rebuilt when requesting an invalid table again.
128
    $data = $this->viewsData->get($random_table_name);
129 130 131 132
    $this->assertEqual($data, array(), 'Make sure fetching views data for an invalid table returns an empty array.');
    $this->assertCountIncrement(FALSE);
  }

133 134 135 136 137 138 139 140 141 142 143
  /**
   * Ensures that cache entries are only set and get when necessary.
   */
  public function testCacheRequests() {
    // Request the same table 5 times. The caches are empty at this point, so
    // what will happen is that it will first check for a cache entry for the
    // given table, get a cache miss, then try the cache entry for all tables,
    // which does not exist yet either. As a result, it rebuilds the information
    // and writes a cache entry for all tables and the requested table.
    $table_name = 'views_test_data';
    for ($i = 0; $i < 5; $i++) {
144
      $this->viewsData->get($table_name);
145 146 147 148 149 150 151 152 153 154 155
    }

    // Assert cache set and get calls.
    $this->assertEqual($this->memoryCounterBackend->getCounter('get', 'views_data:views_test_data:en'), 1, 'Requested the cache for the table-specific cache entry.');
    $this->assertEqual($this->memoryCounterBackend->getCounter('get', 'views_data:en'), 1, 'Requested the cache for all tables.');
    $this->assertEqual($this->memoryCounterBackend->getCounter('set', 'views_data:views_test_data:en'), 1, 'Wrote the cache for the requested once.');
    $this->assertEqual($this->memoryCounterBackend->getCounter('set', 'views_data:en'), 1, 'Wrote the cache for the all tables once.');

    // Re-initialize the views data cache to simulate a new request and repeat.
    // We have a warm cache now, so this will only request the tables-specific
    // cache entry and return that.
156
    $this->initViewsData();
157
    for ($i = 0; $i < 5; $i++) {
158
      $this->viewsData->get($table_name);
159 160 161 162 163 164 165 166 167 168 169 170
    }

    // Assert cache set and get calls.
    $this->assertEqual($this->memoryCounterBackend->getCounter('get', 'views_data:views_test_data:en'), 1, 'Requested the cache for the table-specific cache entry.');
    $this->assertEqual($this->memoryCounterBackend->getCounter('get', 'views_data:en'), 0, 'Did not request to load the cache entry for all tables.');
    $this->assertEqual($this->memoryCounterBackend->getCounter('set', 'views_data:views_test_data:en'), 0, 'Did not write the cache for the requested table.');
    $this->assertEqual($this->memoryCounterBackend->getCounter('set', 'views_data:en'), 0, 'Did not write the cache for all tables.');

    // Re-initialize the views data cache to simulate a new request and request
    // a different table. This will fail to get a table specific cache entry,
    // load the cache entry for all tables and save a cache entry for this table
    // but not all.
171
    $this->initViewsData();
172 173
    $another_table_name = 'views';
    for ($i = 0; $i < 5; $i++) {
174
      $this->viewsData->get($another_table_name);
175 176 177 178 179 180 181 182 183 184 185 186 187
    }

    // Assert cache set and get calls.
    $this->assertEqual($this->memoryCounterBackend->getCounter('get', 'views_data:views:en'), 1, 'Requested the cache for the table-specific cache entry.');
    $this->assertEqual($this->memoryCounterBackend->getCounter('get', 'views_data:en'), 1, 'Requested the cache for all tables.');
    $this->assertEqual($this->memoryCounterBackend->getCounter('set', 'views_data:views:en'), 1, 'Wrote the cache for the requested once.');
    $this->assertEqual($this->memoryCounterBackend->getCounter('set', 'views_data:en'), 0, 'Did not write the cache for all tables.');

    // Re-initialize the views data cache to simulate a new request and request
    // a non-existing table. This will result in the same cache requests as we
    // explicitly write an empty cache entry for non-existing tables to avoid
    // unecessary requests in those situations. We do have to load the cache
    // entry for all tables to check if the table does exist or not.
188
    $this->initViewsData();
189 190
    $non_existing_table = $this->randomName();
    for ($i = 0; $i < 5; $i++) {
191
      $this->viewsData->get($non_existing_table);
192 193 194 195 196 197 198 199 200 201 202
    }

    // Assert cache set and get calls.
    $this->assertEqual($this->memoryCounterBackend->getCounter('get', "views_data:$non_existing_table:en"), 1, 'Requested the cache for the table-specific cache entry.');
    $this->assertEqual($this->memoryCounterBackend->getCounter('get', 'views_data:en'), 1, 'Requested the cache for all tables.');
    $this->assertEqual($this->memoryCounterBackend->getCounter('set', "views_data:$non_existing_table:en"), 1, 'Wrote the cache for the requested once.');
    $this->assertEqual($this->memoryCounterBackend->getCounter('set', 'views_data:en'), 0, 'Did not write the cache for all tables.');

    // Re-initialize the views data cache to simulate a new request and request
    // the same non-existing table. This will load the table-specific cache
    // entry and return the stored empty array for that.
203
    $this->initViewsData();
204
    for ($i = 0; $i < 5; $i++) {
205
      $this->viewsData->get($non_existing_table);
206 207 208 209 210 211 212 213 214 215
    }

    // Assert cache set and get calls.
    $this->assertEqual($this->memoryCounterBackend->getCounter('get', "views_data:$non_existing_table:en"), 1, 'Requested the cache for the table-specific cache entry.');
    $this->assertEqual($this->memoryCounterBackend->getCounter('get', 'views_data:en'), 0, 'Did not request to load the cache entry for all tables.');
    $this->assertEqual($this->memoryCounterBackend->getCounter('set', "views_data:$non_existing_table:en"), 0, 'Did not write the cache for the requested table.');
    $this->assertEqual($this->memoryCounterBackend->getCounter('set', 'views_data:en'), 0, 'Did not write the cache for all tables.');

    // Re-initialize the views data cache and repeat with no specified table.
    // This should only load the cache entry for all tables.
216
    $this->initViewsData();
217
    for ($i = 0; $i < 5; $i++) {
218
      $this->viewsData->get();
219 220 221 222 223 224 225 226 227 228 229 230
    }

    // This only requested the full information. No other cache requests should
    // have been made.
    $this->assertEqual($this->memoryCounterBackend->getCounter('get', 'views_data:views_test_data:en'), 0);
    $this->assertEqual($this->memoryCounterBackend->getCounter('get', 'views_data:en'), 1);
    $this->assertEqual($this->memoryCounterBackend->getCounter('set', 'views_data:views_test_data:en'), 0);
    $this->assertEqual($this->memoryCounterBackend->getCounter('set', 'views_data:en'), 0);

  }

  /**
231
   * Initializes a new ViewsData instance and resets the cache backend.
232
   */
233
  protected function initViewsData() {
234
    $this->memoryCounterBackend->resetCounter();
235
    $this->viewsData = new ViewsData($this->memoryCounterBackend, $this->container->get('config.factory'), $this->container->get('module_handler'), $this->container->get('language_manager'));
236 237
  }

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
  /**
   * Starts a count for hook_views_data being invoked.
   */
  protected function startCount() {
    $count = $this->state->get('views_test_data_views_data_count');
    $this->count = isset($count) ? $count : 0;
  }

  /**
   * Asserts that the count for hook_views_data either equal or has increased.
   *
   * @param bool $equal
   *   Whether to assert that the count should be equal. Defaults to FALSE.
   */
  protected function assertCountIncrement($increment = TRUE) {
    if ($increment) {
      // If an incremented count is expected, increment this now.
      $this->count++;
      $message = 'hook_views_data has been invoked.';
    }
    else {
      $message = 'hook_views_data has not been invoked';
    }

    $this->assertEqual($this->count, $this->state->get('views_test_data_views_data_count'), $message);
263 264
  }

265 266 267 268 269 270
  /**
   * Overrides Drupal\views\Tests\ViewTestBase::viewsData().
   */
  protected function viewsData() {
    $data = parent::viewsData();

271 272
    // Tweak the views data to have a base for testing
    // \Drupal\views\ViewsDataHelper::fetchFields().
273 274 275 276 277
    unset($data['views_test_data']['id']['field']);
    unset($data['views_test_data']['name']['argument']);
    unset($data['views_test_data']['age']['filter']);
    unset($data['views_test_data']['job']['sort']);
    $data['views_test_data']['created']['area']['id'] = 'text';
278 279 280 281 282
    $data['views_test_data']['age']['area']['id'] = 'text';
    $data['views_test_data']['age']['area']['sub_type'] = 'header';
    $data['views_test_data']['job']['area']['id'] = 'text';
    $data['views_test_data']['job']['area']['sub_type'] = array('header', 'footer');

283 284 285 286

    return $data;
  }

287 288 289 290 291 292
  /**
   * Tests the fetchBaseTables() method.
   */
  public function testFetchBaseTables() {
    // Enabled node module so there is more than 1 base table to test.
    $this->enableModules(array('node'));
293 294
    $data = $this->viewsData->get();
    $base_tables = $this->viewsData->fetchBaseTables();
295 296 297

    // Test the number of tables returned and their order.
    $this->assertEqual(count($base_tables), 3, 'The correct amount of base tables were returned.');
298
    $this->assertIdentical(array_keys($base_tables), array('node', 'node_field_revision', 'views_test_data'), 'The tables are sorted as expected.');
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314

    // Test the values returned for each base table.
    $defaults = array(
      'title' => '',
      'help' => '',
      'weight' => 0,
    );
    foreach ($base_tables as $base_table => $info) {
      // Merge in default values as in fetchBaseTables().
      $expected = $data[$base_table]['table']['base'] += $defaults;
      foreach ($defaults as $key => $default) {
        $this->assertEqual($info[$key], $expected[$key]);
      }
    }
  }

315
}