You are here

public function HandlerTest::testRelationshipUI in Drupal 9

Same name and namespace in other branches
  1. 8 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 251

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
    ->assertSession()
    ->linkByHrefExists($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
    ->assertSession()
    ->fieldExists($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
    ->assertEquals($expected_options, $options);

  // Remove the relationship and make sure no relationship option appears.
  $this
    ->drupalGet('admin/structure/views/nojs/handler/test_handler_relationships/default/relationship/nid');
  $this
    ->submitForm([], 'Remove');
  $this
    ->drupalGet($handler_options_path);
  $this
    ->assertSession()
    ->fieldNotExists($relationship_name);

  // Create a view of comments with node relationship.
  View::create([
    'base_table' => 'comment_field_data',
    'id' => 'test_get_entity_type',
  ])
    ->save();
  $this
    ->drupalGet('admin/structure/views/nojs/add-handler/test_get_entity_type/default/relationship');
  $this
    ->submitForm([
    'name[comment_field_data.node]' => 'comment_field_data.node',
  ], 'Add and configure relationships');
  $this
    ->submitForm([], 'Apply');

  // Add a content type filter.
  $this
    ->drupalGet('admin/structure/views/nojs/add-handler/test_get_entity_type/default/filter');
  $this
    ->submitForm([
    'name[node_field_data.type]' => 'node_field_data.type',
  ], 'Add and configure filter criteria');
  $this
    ->assertTrue($this
    ->assertSession()
    ->optionExists('edit-options-relationship', 'node')
    ->isSelected());
  $this
    ->submitForm([
    'options[value][page]' => 'page',
  ], 'Apply');

  // Check content type filter options.
  $this
    ->drupalGet('admin/structure/views/nojs/handler/test_get_entity_type/default/filter/type');
  $this
    ->assertTrue($this
    ->assertSession()
    ->optionExists('edit-options-relationship', 'node')
    ->isSelected());
  $this
    ->assertSession()
    ->checkboxChecked('edit-options-value-page');
}