public function ViewsDataTest::testViewsFetchData

Tests the views.views_data service.

See also

\Drupal\views\ViewsData

File

drupal/core/modules/views/lib/Drupal/views/Tests/ViewsDataTest.php, line 63
Definition of Drupal\views\Tests\ViewsDataTest.

Class

ViewsDataTest
Tests the fetching of views data.

Namespace

Drupal\views\Tests

Code

public function testViewsFetchData() {
  $table_name = 'views_test_data';
  $random_table_name = $this
    ->randomName();

  // Invoke expected data directly from hook_views_data implementations.
  $expected_data = $this->container
    ->get('module_handler')
    ->invokeAll('views_data');

  // Verify that views_test_data_views_data() has only been called once after
  // calling clear().
  $this
    ->startCount();
  $this->viewsData
    ->get();

  // Test views data has been invoked.
  $this
    ->assertCountIncrement();

  // Clear the storage/cache.
  $this->viewsData
    ->clear();

  // Get the data again.
  $this->viewsData
    ->get();
  $this->viewsData
    ->get($table_name);
  $this->viewsData
    ->get($random_table_name);

  // Verify that view_test_data_views_data() has run once.
  $this
    ->assertCountIncrement();

  // Get the data again.
  $this->viewsData
    ->get();
  $this->viewsData
    ->get($table_name);
  $this->viewsData
    ->get($random_table_name);

  // Verify that view_test_data_views_data() has not run again.
  $this
    ->assertCountIncrement(FALSE);

  // Clear the views data, and test all table data.
  $this->viewsData
    ->clear();
  $this
    ->startCount();
  $data = $this->viewsData
    ->get();
  $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.
  $data = $this->viewsData
    ->get();
  $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.
  $this->viewsData
    ->clear();
  $this
    ->startCount();
  $data = $this->viewsData
    ->get($table_name);
  $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.
  $data = $this->viewsData
    ->get($table_name);
  $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.
  $data = $this->viewsData
    ->get();
  $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.
  $this->viewsData
    ->clear();
  $this
    ->startCount();

  // All views data should be requested on the first try.
  $data = $this->viewsData
    ->get($random_table_name);
  $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.
  $data = $this->viewsData
    ->get($random_table_name);
  $this
    ->assertEqual($data, array(), 'Make sure fetching views data for an invalid table returns an empty array.');
  $this
    ->assertCountIncrement(FALSE);
}