You are here

function FieldApiDataTest::testViewsData in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/views/src/Tests/FieldApiDataTest.php \Drupal\views\Tests\FieldApiDataTest::testViewsData()

Unit testing the views data structure.

We check data structure for both node and node revision tables.

File

core/modules/views/src/Tests/FieldApiDataTest.php, line 58
Contains \Drupal\views\Tests\FieldApiDataTest.

Class

FieldApiDataTest
Tests the Field Views data.

Namespace

Drupal\views\Tests

Code

function testViewsData() {
  $table_mapping = \Drupal::entityManager()
    ->getStorage('node')
    ->getTableMapping();
  $field_storage = $this->fieldStorages[0];
  $current_table = $table_mapping
    ->getDedicatedDataTableName($field_storage);
  $revision_table = $table_mapping
    ->getDedicatedRevisionTableName($field_storage);
  $data = $this
    ->getViewsData();
  $this
    ->assertTrue(isset($data[$current_table]));
  $this
    ->assertTrue(isset($data[$revision_table]));

  // The node field should join against node_field_data.
  $this
    ->assertTrue(isset($data[$current_table]['table']['join']['node_field_data']));
  $this
    ->assertTrue(isset($data[$revision_table]['table']['join']['node_field_revision']));
  $expected_join = array(
    'left_field' => 'nid',
    'field' => 'entity_id',
    'extra' => array(
      array(
        'field' => 'deleted',
        'value' => 0,
        'numeric' => TRUE,
      ),
      array(
        'left_field' => 'langcode',
        'field' => 'langcode',
      ),
    ),
  );
  $this
    ->assertEqual($expected_join, $data[$current_table]['table']['join']['node_field_data']);
  $expected_join = array(
    'left_field' => 'vid',
    'field' => 'revision_id',
    'extra' => array(
      array(
        'field' => 'deleted',
        'value' => 0,
        'numeric' => TRUE,
      ),
      array(
        'left_field' => 'langcode',
        'field' => 'langcode',
      ),
    ),
  );
  $this
    ->assertEqual($expected_join, $data[$revision_table]['table']['join']['node_field_revision']);

  // Test click sortable.
  $this
    ->assertTrue($data[$current_table][$field_storage
    ->getName()]['field']['click sortable'], 'String field is click sortable.');

  // Click sort should only be on the primary field.
  $this
    ->assertTrue(empty($data[$revision_table][$field_storage
    ->getName()]['field']['click sortable']), 'Non-primary fields are not click sortable');
  $this
    ->assertTrue($data[$current_table][$field_storage
    ->getName()]['help'] instanceof MarkupInterface);
  $this
    ->assertEqual($data[$current_table][$field_storage
    ->getName()]['help'], 'Appears in: page, article. Also known as: Content: GiraffeB" label');
  $this
    ->assertTrue($data[$current_table][$field_storage
    ->getName() . '_value']['help'] instanceof MarkupInterface);
  $this
    ->assertEqual($data[$current_table][$field_storage
    ->getName() . '_value']['help'], 'Appears in: page, article. Also known as: Content: GiraffeA" label (field_name_0)');

  // Since each label is only used once, views_entity_field_label() will
  // return a label using alphabetical sorting.
  $this
    ->assertEqual('GiraffeA" label (field_name_0)', $data[$current_table][$field_storage
    ->getName() . '_value']['title']);

  // Attach the same field to a different bundle with a different label.
  $this
    ->drupalCreateContentType([
    'type' => 'news',
  ]);
  FieldConfig::create([
    'field_name' => $this->fieldStorages[0]
      ->getName(),
    'entity_type' => 'node',
    'bundle' => 'news',
    'label' => 'GiraffeB" label',
  ])
    ->save();
  $this->container
    ->get('views.views_data')
    ->clear();
  $data = $this
    ->getViewsData();

  // Now the 'GiraffeB" label' is used twice and therefore will be
  // selected by views_entity_field_label().
  $this
    ->assertEqual('GiraffeB" label (field_name_0)', $data[$current_table][$field_storage
    ->getName() . '_value']['title']);
  $this
    ->assertTrue($data[$current_table][$field_storage
    ->getName()]['help'] instanceof MarkupInterface);
  $this
    ->assertEqual($data[$current_table][$field_storage
    ->getName()]['help'], 'Appears in: page, article, news. Also known as: Content: GiraffeA" label');
}