function HandlerTest::testSetRelationship

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

Tests the relationship method on the base class.

File

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

Class

HandlerTest
Tests abstract handler definitions.

Namespace

Drupal\Tests\views\Functional\Handler

Code

public function testSetRelationship() {
    $view = Views::getView('test_handler_relationships');
    $view->setDisplay();
    // Setup a broken relationship.
    $view->addHandler('default', 'relationship', $this->randomMachineName(), $this->randomMachineName(), [], 'broken_relationship');
    // Setup a valid relationship.
    $view->addHandler('default', 'relationship', 'comment_field_data', 'node', [
        'relationship' => 'cid',
    ], 'valid_relationship');
    $view->initHandlers();
    $field = $view->field['title'];
    $field->options['relationship'] = NULL;
    $field->setRelationship();
    $this->assertNull($field->relationship, 'Make sure that an empty relationship does not create a relationship on the field.');
    $field->options['relationship'] = $this->randomMachineName();
    $field->setRelationship();
    $this->assertNull($field->relationship, 'Make sure that a random relationship does not create a relationship on the field.');
    $field->options['relationship'] = 'broken_relationship';
    $field->setRelationship();
    $this->assertNull($field->relationship, 'Make sure that a broken relationship does not create a relationship on the field.');
    $field->options['relationship'] = 'valid_relationship';
    $field->setRelationship();
    $this->assertEmpty($field->relationship, 'Make sure that the relationship alias was not set without building a views query before.');
    // Remove the invalid relationship.
    unset($view->relationship['broken_relationship']);
    $view->build();
    $field->setRelationship();
    $this->assertEquals($field->relationship, $view->relationship['valid_relationship']->alias, 'Make sure that a valid relationship does create the right relationship query alias.');
}

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