function HandlerTest::testRelationshipUI

Same name in other branches
  1. 9 core/modules/views/tests/src/Functional/Handler/HandlerTest.php \Drupal\Tests\views\Functional\Handler\HandlerTest::testRelationshipUI()
  2. 8.9.x core/modules/views/tests/src/Functional/Handler/HandlerTest.php \Drupal\Tests\views\Functional\Handler\HandlerTest::testRelationshipUI()
  3. 10 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 232

Class

HandlerTest
Tests abstract handler definitions.

Namespace

Drupal\Tests\views\Functional\Handler

Code

public function testRelationshipUI() : void {
    $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);
    // Change the Row plugin to display "Content".
    $this->drupalGet('admin/structure/views/nojs/display/test_handler_relationships/default/row');
    $this->submitForm([
        'row[type]' => 'entity:node',
    ], 'Apply');
    $this->assertSession()
        ->fieldExists('row_options[relationship]');
    $this->submitForm([
        'row_options[view_mode]' => 'default',
    ], 'Apply');
    // 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',
        'label' => 'Test',
    ])->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');
}

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