You are here

public function HandlerTest::testSetRelationship 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::testSetRelationship()

Tests the relationship method on the base class.

File

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

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
    ->assertFalse(!empty($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.');
}