You are here

public function TaxonomyRelationshipTest::testTaxonomyRelationships in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/taxonomy/tests/src/Functional/Views/TaxonomyRelationshipTest.php \Drupal\Tests\taxonomy\Functional\Views\TaxonomyRelationshipTest::testTaxonomyRelationships()
  2. 10 core/modules/taxonomy/tests/src/Functional/Views/TaxonomyRelationshipTest.php \Drupal\Tests\taxonomy\Functional\Views\TaxonomyRelationshipTest::testTaxonomyRelationships()

Tests the taxonomy parent plugin UI.

File

core/modules/taxonomy/tests/src/Functional/Views/TaxonomyRelationshipTest.php, line 60

Class

TaxonomyRelationshipTest
Tests taxonomy relationships with parent term and node.

Namespace

Drupal\Tests\taxonomy\Functional\Views

Code

public function testTaxonomyRelationships() {

  // Check the generated views data of taxonomy_index.
  $views_data = Views::viewsData()
    ->get('taxonomy_index');

  // Check the table join data.
  $this
    ->assertEqual($views_data['table']['join']['taxonomy_term_field_data']['left_field'], 'tid');
  $this
    ->assertEqual($views_data['table']['join']['taxonomy_term_field_data']['field'], 'tid');
  $this
    ->assertEqual($views_data['table']['join']['node_field_data']['left_field'], 'nid');
  $this
    ->assertEqual($views_data['table']['join']['node_field_data']['field'], 'nid');
  $this
    ->assertEqual($views_data['table']['join']['taxonomy_term__parent']['left_field'], 'entity_id');
  $this
    ->assertEqual($views_data['table']['join']['taxonomy_term__parent']['field'], 'tid');

  // Check the generated views data of taxonomy_term__parent.
  $views_data = Views::viewsData()
    ->get('taxonomy_term__parent');

  // Check the table join data.
  $this
    ->assertEqual($views_data['table']['join']['taxonomy_term__parent']['left_field'], 'entity_id');
  $this
    ->assertEqual($views_data['table']['join']['taxonomy_term__parent']['field'], 'parent_target_id');
  $this
    ->assertEqual($views_data['table']['join']['taxonomy_term_field_data']['left_field'], 'tid');
  $this
    ->assertEqual($views_data['table']['join']['taxonomy_term_field_data']['field'], 'entity_id');

  // Check the parent relationship data.
  $this
    ->assertEqual($views_data['parent_target_id']['relationship']['base'], 'taxonomy_term_field_data');
  $this
    ->assertEqual($views_data['parent_target_id']['relationship']['base field'], 'tid');
  $this
    ->assertEqual($views_data['parent_target_id']['relationship']['label'], t('Parent'));
  $this
    ->assertEqual($views_data['parent_target_id']['relationship']['id'], 'standard');

  // Check the parent filter and argument data.
  $this
    ->assertEqual($views_data['parent_target_id']['filter']['id'], 'numeric');
  $this
    ->assertEqual($views_data['parent_target_id']['argument']['id'], 'taxonomy');

  // Check an actual test view.
  $view = Views::getView('test_taxonomy_term_relationship');
  $this
    ->executeView($view);

  /** @var \Drupal\views\ResultRow $row */
  foreach ($view->result as $index => $row) {

    // Check that the actual ID of the entity is the expected one.
    $this
      ->assertEqual($row->tid, $this->terms[$index]
      ->id());

    // Also check that we have the correct result entity.
    $this
      ->assertEqual($row->_entity
      ->id(), $this->terms[$index]
      ->id());
    $this
      ->assertInstanceOf(TermInterface::class, $row->_entity);
    if (!$index) {
      $this
        ->assertInstanceOf(TermInterface::class, $row->_relationship_entities['parent']);
      $this
        ->assertEqual($row->_relationship_entities['parent']
        ->id(), $this->term2
        ->id());
      $this
        ->assertEqual($row->taxonomy_term_field_data_taxonomy_term__parent_tid, $this->term2
        ->id());
    }
    $this
      ->assertInstanceOf(NodeInterface::class, $row->_relationship_entities['nid']);
    $this
      ->assertEqual($row->_relationship_entities['nid']
      ->id(), $this->nodes[$index]
      ->id());
  }

  // Test node fields are available through relationship.
  \Drupal::service('module_installer')
    ->install([
    'views_ui',
  ]);
  $this
    ->drupalLogin($this
    ->createUser([
    'administer views',
  ]));
  $this
    ->drupalGet('admin/structure/views/view/test_taxonomy_term_relationship');
  $this
    ->click('#views-add-field');
  $this
    ->assertSession()
    ->pageTextContains('Body');
}