function FieldApiDataTest::testViewsData

Same name and namespace in other branches
  1. 9 core/modules/views/tests/src/Kernel/FieldApiDataTest.php \Drupal\Tests\views\Kernel\FieldApiDataTest::testViewsData()
  2. 8.9.x core/modules/views/tests/src/Kernel/FieldApiDataTest.php \Drupal\Tests\views\Kernel\FieldApiDataTest::testViewsData()
  3. 11.x core/modules/views/tests/src/Kernel/FieldApiDataTest.php \Drupal\Tests\views\Kernel\FieldApiDataTest::testViewsData()

Unit testing the views data structure.

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

File

core/modules/views/tests/src/Kernel/FieldApiDataTest.php, line 54

Class

FieldApiDataTest
Tests the Field Views data.

Namespace

Drupal\Tests\views\Kernel

Code

public function testViewsData() : void {
  $field_storage_string = FieldStorageConfig::create([
    'field_name' => 'field_string',
    'entity_type' => 'node',
    'type' => 'string',
  ]);
  $field_storage_string->save();
  $field_storage_string_long = FieldStorageConfig::create([
    'field_name' => 'field_string_long',
    'entity_type' => 'node',
    'type' => 'string_long',
  ]);
  $field_storage_string_long->save();
  NodeType::create([
    'type' => 'page',
    'name' => 'Page',
  ])->save();
  NodeType::create([
    'type' => 'article',
    'name' => 'Article',
  ])->save();
  // Attach the field to nodes.
  FieldConfig::create([
    'field_name' => 'field_string',
    'entity_type' => 'node',
    'bundle' => 'page',
    'label' => 'GiraffeA" label',
  ])->save();
  // Attach the string_long field to the page node type.
  FieldConfig::create([
    'field_name' => 'field_string_long',
    'entity_type' => 'node',
    'bundle' => 'page',
    'label' => 'string_long label',
  ])->save();
  // Attach the same field to a different bundle with a different label.
  FieldConfig::create([
    'field_name' => 'field_string',
    'entity_type' => 'node',
    'bundle' => 'article',
    'label' => 'GiraffeB" label',
  ])->save();
  // Now create some example nodes/users for the view result.
  for ($i = 0; $i < 5; $i++) {
    $edit = [
      'field_string' => [
        [
          'value' => $this->randomMachineName(),
        ],
      ],
    ];
    $nodes[] = Node::create([
      'type' => 'page',
    ] + $edit);
  }
  /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
  $table_mapping = $this->container
    ->get('entity_type.manager')
    ->getStorage('node')
    ->getTableMapping();
  $current_table = $table_mapping->getDedicatedDataTableName($field_storage_string);
  $revision_table = $table_mapping->getDedicatedRevisionTableName($field_storage_string);
  $data = $this->getViewsData();
  $this->assertArrayHasKey($current_table, $data);
  $this->assertArrayHasKey($revision_table, $data);
  // The node field should join against node_field_data.
  $this->assertArrayHasKey('node_field_data', $data[$current_table]['table']['join']);
  $this->assertArrayHasKey('node_field_revision', $data[$revision_table]['table']['join']);
  $expected_join = [
    'table' => $current_table,
    'left_field' => 'nid',
    'field' => 'entity_id',
    'extra' => [
      [
        'field' => 'deleted',
        'value' => 0,
        'numeric' => TRUE,
      ],
      [
        'left_field' => 'langcode',
        'field' => 'langcode',
      ],
    ],
  ];
  $this->assertSame($expected_join, $data[$current_table]['table']['join']['node_field_data']);
  $expected_join = [
    'table' => $revision_table,
    'left_field' => 'vid',
    'field' => 'revision_id',
    'extra' => [
      [
        'field' => 'deleted',
        'value' => 0,
        'numeric' => TRUE,
      ],
      [
        'left_field' => 'langcode',
        'field' => 'langcode',
      ],
    ],
  ];
  $this->assertSame($expected_join, $data[$revision_table]['table']['join']['node_field_revision']);
  // Test click sortable for string field.
  $this->assertTrue($data[$current_table][$field_storage_string->getName()]['field']['click sortable']);
  // Click sort should only be on the primary field.
  $this->assertArrayNotHasKey($field_storage_string->getName(), $data[$revision_table]);
  // Test click sortable for long text field.
  $data_long = $this->getViewsData('field_string_long');
  $current_table_long = $table_mapping->getDedicatedDataTableName($field_storage_string_long);
  $this->assertTrue($data_long[$current_table_long][$field_storage_string_long->getName()]['field']['click sortable']);
  $this->assertInstanceOf(MarkupInterface::class, $data[$current_table][$field_storage_string->getName()]['help']);
  $this->assertEquals('Appears in: page, article. Also known as: Content: GiraffeB&quot; label', $data[$current_table][$field_storage_string->getName()]['help']);
  $this->assertInstanceOf(MarkupInterface::class, $data[$current_table][$field_storage_string->getName() . '_value']['help']);
  $this->assertEquals('Appears in: page, article. Also known as: Content: GiraffeA&quot; label (field_string)', $data[$current_table][$field_storage_string->getName() . '_value']['help']);
  // Since each label is only used once, views_entity_field_label() will
  // return a label using alphabetical sorting.
  $this->assertEquals('GiraffeA&quot; label (field_string)', $data[$current_table][$field_storage_string->getName() . '_value']['title']);
  // Attach the same field to a different bundle with a different label.
  NodeType::create([
    'type' => 'news',
    'name' => 'News',
  ])->save();
  FieldConfig::create([
    'field_name' => $field_storage_string->getName(),
    'entity_type' => 'node',
    'bundle' => 'news',
    'label' => 'GiraffeB" label',
  ])
    ->save();
  $this->container
    ->get('views.views_data')
    ->clear();
  $data = $this->getViewsData();
  // Now the 'GiraffeB&quot; label' is used twice and therefore will be
  // selected by views_entity_field_label().
  $this->assertEquals('GiraffeB&quot; label (field_string)', $data[$current_table][$field_storage_string->getName() . '_value']['title']);
  $this->assertInstanceOf(MarkupInterface::class, $data[$current_table][$field_storage_string->getName()]['help']);
  $this->assertEquals('Appears in: page, article, news. Also known as: Content: GiraffeA&quot; label', $data[$current_table][$field_storage_string->getName()]['help']);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.