You are here

public function ViewsDataTest::testFetchBaseTables in Drupal 8

Tests the fetchBaseTables() method.

File

core/modules/views/tests/src/Unit/ViewsDataTest.php, line 149

Class

ViewsDataTest
@coversDefaultClass \Drupal\views\ViewsData @group views

Namespace

Drupal\Tests\views\Unit

Code

public function testFetchBaseTables() {
  $this
    ->setupMockedModuleHandler();
  $data = $this->viewsData
    ->getAll();
  $base_tables = $this->viewsData
    ->fetchBaseTables();

  // Ensure that 'provider' is set for each base table.
  foreach (array_keys($base_tables) as $base_table) {
    $this
      ->assertEquals('views_test_data', $data[$base_table]['table']['provider']);
  }

  // Test the number of tables returned and their order.
  $this
    ->assertCount(6, $base_tables, 'The correct amount of base tables were returned.');
  $base_tables_keys = array_keys($base_tables);
  for ($i = 1; $i < count($base_tables); ++$i) {
    $prev = $base_tables[$base_tables_keys[$i - 1]];
    $current = $base_tables[$base_tables_keys[$i]];
    $this
      ->assertTrue($prev['weight'] <= $current['weight'] && $prev['title'] <= $prev['title'], 'The tables are sorted as expected.');
  }

  // Test the values returned for each base table.
  $defaults = [
    '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
        ->assertSame($info[$key], $expected[$key]);
    }
  }
}