1.20.x views_module.test ViewsModuleTest::testFetchData()

Tests views_fetch_data().

File

modules/views/tests/views_module.test, line 149
Definition of ViewsModuleTest.

Class

ViewsModuleTest
Tests basic functions from the Views module.

Code

function testFetchData() {

  // Make sure we start with a empty cache.
  $this->resetStaticViewsDataCache();
  cache('views')->flush();
  state_set('views_test_views_data_count', 0);

  // Request info about an existing table.
  $this->assertTrue(views_fetch_data('views_test'), 'Data about existing table returned');
  // This should have triggered a views data rebuild, and written a cache
  // entry for all tables and the requested table but no other tables.
  $this->assertEqual(state_get('views_test_views_data_count', 0), 1, 'Views data rebuilt once');
  $this->assertTrue(cache('views')->get('views_data:en'), 'Cache for all tables written.');
  $this->assertTrue(cache('views')->get('views_data:views_test:en'), 'Cache for requested table written.');
  $this->assertFalse(cache('views')->get('views_data:views_test_previous:en'), 'No Cache written for not requested table.');
  $this->assertTrue(backdrop_static('_views_fetch_data_fully_loaded'), 'Views data is fully loaded');

  $this->resetStaticViewsDataCache();

  // Request the same table again.
  $this->assertTrue(views_fetch_data('views_test'), 'Data about existing table returned');
  $this->assertEqual(state_get('views_test_views_data_count', 0), 1, 'Views data rebuilt once');
  $this->assertFalse(backdrop_static('_views_fetch_data_fully_loaded'), 'Views data is not fully loaded');

  $this->resetStaticViewsDataCache();

  // Request a missing table, this should load the full cache from cache but
  // not rebuilt.
  $this->assertFalse(views_fetch_data('views_test_missing'), 'No data about missing table returned');
  $this->assertEqual(state_get('views_test_views_data_count', 0), 1, 'Views data rebuilt once');
  $this->assertTrue(backdrop_static('_views_fetch_data_fully_loaded'), 'Views data is fully loaded');

  $this->resetStaticViewsDataCache();

  // Request the same empty table again, this should load only that (empty)
  // cache for that table.
  $this->assertFalse(views_fetch_data('views_test_missing'), 'No data about missing table returned');
  $this->assertEqual(state_get('views_test_views_data_count', 0), 1, 'Views data rebuilt once');
  $this->assertFalse(backdrop_static('_views_fetch_data_fully_loaded'), 'Views data is not fully loaded');

  // Test if the cache consistency is ensured. There was an issue where
  // calling _views_fetch_data() first with a table, would prevent the
  // function from properly rebuilding, and missing the general cache entry.
  // See https://www.drupal.org/node/2475669 for details.
  // Make sure we start with an empty cache.
  $this->resetStaticViewsDataCache();
  cache_clear_all('*', 'cache_views', TRUE);

  // Prime the static cache of _views_fetch_data(), by calling it with a table
  // first.
  views_fetch_data('views_test');
  // Remove the general cache.
  cache_clear_all('views_data:en', 'cache_views');
  // Reset the static cache, to see if fetches from the persistent cache
  // properly rebuild the static cache.
  $this->resetStaticViewsDataCache();
  // Prime the static cache of _views_fetch_data() by calling it with a table
  // first.
  views_fetch_data('views_test');
  // Fetch the general cache, which was deleted, and see if it has been
  // properly rebuilt.
  views_fetch_data();
  $this->assertTrue(cache_get('views_data:en', 'cache_views'), 'Cache for all tables was properly rebuilt.');
}