function HandlerTest::testRelationshipUI

Same name and namespace in other branches
  1. 11.x core/modules/views/tests/src/Functional/Handler/HandlerTest.php \Drupal\Tests\views\Functional\Handler\HandlerTest::testRelationshipUI()
  2. 10 core/modules/views/tests/src/Functional/Handler/HandlerTest.php \Drupal\Tests\views\Functional\Handler\HandlerTest::testRelationshipUI()
  3. 9 core/modules/views/tests/src/Functional/Handler/HandlerTest.php \Drupal\Tests\views\Functional\Handler\HandlerTest::testRelationshipUI()

Tests the relationship ui for field/filter/argument/relationship.

File

core/modules/views/tests/src/Functional/Handler/HandlerTest.php, line 250

Class

HandlerTest
Tests abstract handler definitions.

Namespace

Drupal\Tests\views\Functional\Handler

Code

public function testRelationshipUI() {
  $views_admin = $this->drupalCreateUser([
    'administer views',
  ]);
  $this->drupalLogin($views_admin);
  // Make sure the link to the field options exists.
  $handler_options_path = 'admin/structure/views/nojs/handler/test_handler_relationships/default/field/title';
  $view_edit_path = 'admin/structure/views/view/test_handler_relationships/edit';
  $this->drupalGet($view_edit_path);
  $this->assertLinkByHref($handler_options_path);
  // The test view has a relationship to node_revision so the field should
  // show a relationship selection.
  $this->drupalGet($handler_options_path);
  $relationship_name = 'options[relationship]';
  $this->assertFieldByName($relationship_name);
  // Check for available options.
  $fields = $this->getSession()
    ->getPage()
    ->findAll('named_exact', [
    'field',
    $relationship_name,
  ]);
  $options = [];
  foreach ($fields as $field) {
    $items = $field->findAll('css', 'option');
    foreach ($items as $item) {
      $options[] = $item->getAttribute('value');
    }
  }
  $expected_options = [
    'none',
    'nid',
  ];
  $this->assertEqual($options, $expected_options);
  // Remove the relationship and make sure no relationship option appears.
  $this->drupalPostForm('admin/structure/views/nojs/handler/test_handler_relationships/default/relationship/nid', [], t('Remove'));
  $this->drupalGet($handler_options_path);
  $this->assertNoFieldByName($relationship_name, NULL, 'Make sure that no relationship option is available');
  // Create a view of comments with node relationship.
  View::create([
    'base_table' => 'comment_field_data',
    'id' => 'test_get_entity_type',
  ])->save();
  $this->drupalPostForm('admin/structure/views/nojs/add-handler/test_get_entity_type/default/relationship', [
    'name[comment_field_data.node]' => 'comment_field_data.node',
  ], t('Add and configure relationships'));
  $this->drupalPostForm(NULL, [], t('Apply'));
  // Add a content type filter.
  $this->drupalPostForm('admin/structure/views/nojs/add-handler/test_get_entity_type/default/filter', [
    'name[node_field_data.type]' => 'node_field_data.type',
  ], t('Add and configure filter criteria'));
  $this->assertOptionSelected('edit-options-relationship', 'node');
  $this->drupalPostForm(NULL, [
    'options[value][page]' => 'page',
  ], t('Apply'));
  // Check content type filter options.
  $this->drupalGet('admin/structure/views/nojs/handler/test_get_entity_type/default/filter/type');
  $this->assertOptionSelected('edit-options-relationship', 'node');
  $this->assertFieldChecked('edit-options-value-page');
}

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