You are here

public function HandlerTest::testSetRelationship in Views (for Drupal 7) 8.3

Tests the relationship method on the base class.

File

lib/Drupal/views/Tests/Handler/HandlerTest.php, line 265
Definition of Drupal\views\Tests\Handler\HandlerTest.

Class

HandlerTest
Tests abstract handlers of views.

Namespace

Drupal\views\Tests\Handler

Code

public function testSetRelationship() {
  $view = $this
    ->createViewFromConfig('test_handler_relationships');

  // Setup a broken relationship.
  $view
    ->addItem('default', 'relationship', $this
    ->randomName(), $this
    ->randomName(), array(), 'broken_relationship');

  // Setup a valid relationship.
  $view
    ->addItem('default', 'relationship', 'comment', 'nid', 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
    ->randomName();
  $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.');
}