You are here

public function HandlerTest::testSetRelationship in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/views/src/Tests/Handler/HandlerTest.php \Drupal\views\Tests\Handler\HandlerTest::testSetRelationship()

Tests the relationship method on the base class.

File

core/modules/views/src/Tests/Handler/HandlerTest.php, line 271
Contains \Drupal\views\Tests\Handler\HandlerTest.

Class

HandlerTest
Tests abstract handler definitions.

Namespace

Drupal\views\Tests\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(), array(), 'broken_relationship');

  // Setup a valid relationship.
  $view
    ->addHandler('default', 'relationship', 'comment_field_data', 'node', array(
    'relationship' => 'cid',
  ), 'valid_relationship');
  $view
    ->initHandlers();
  $field = $view->field['title'];
  $field->options['relationship'] = NULL;
  $field
    ->setRelationship();
  $this
    ->assertFalse($field->relationship, 'Make sure that an empty relationship does not create a relationship on the field.');
  $field->options['relationship'] = $this
    ->randomMachineName();
  $field
    ->setRelationship();
  $this
    ->assertFalse($field->relationship, 'Make sure that a random relationship does not create a relationship on the field.');
  $field->options['relationship'] = 'broken_relationship';
  $field
    ->setRelationship();
  $this
    ->assertFalse($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
    ->assertEqual($field->relationship, $view->relationship['valid_relationship']->alias, 'Make sure that a valid relationship does create the right relationship query alias.');
}