You are here

function RelationViewsTestCase::testSymmetricRelations in Relation 7

Tests views with symmetric relations.

File

tests/relation.views.test, line 86
Tests for Views support in the Relation module.

Class

RelationViewsTestCase
Functional test of Relation's integration with Views.

Code

function testSymmetricRelations() {
  foreach (array(
    FALSE,
    TRUE,
  ) as $required) {
    $view = new view();
    $handler = $view
      ->new_display('default');
    $handler->display->display_options['relationships']['relation_symmetric_node']['id'] = 'relation_symmetric_node';
    $handler->display->display_options['relationships']['relation_symmetric_node']['table'] = 'node';
    $handler->display->display_options['relationships']['relation_symmetric_node']['field'] = 'relation_symmetric_node';
    $handler->display->display_options['relationships']['relation_symmetric_node']['required'] = $required;
    $handler->display->display_options['fields']['nid']['id'] = 'nid';
    $handler->display->display_options['fields']['nid']['table'] = 'node';
    $handler->display->display_options['fields']['nid']['field'] = 'nid';
    $handler->display->display_options['fields']['nid']['relationship'] = 'relation_symmetric_node';
    $handler->display->display_options['fields']['nid2']['id'] = 'nid2';
    $handler->display->display_options['fields']['nid2']['table'] = 'node';
    $handler->display->display_options['fields']['nid2']['field'] = 'nid';
    $handler->display->display_options['arguments']['nid']['id'] = 'nid';
    $handler->display->display_options['arguments']['nid']['table'] = 'node';
    $handler->display->display_options['arguments']['nid']['field'] = 'nid';

    // The result should be the same for required and non-required for
    // argument node 1: Relation from 1 to 4.
    $view
      ->set_arguments(array(
      $this->node1->nid,
    ));
    $view
      ->execute();
    $this
      ->assertEqual(count($view->result), 1);
    $result = array_pop($view->result);
    $this
      ->assertEqual($result->nid, $this->node1->nid);
    $this
      ->assertEqual($result->node_node_nid, $this->node4->nid);
    $view = $view
      ->clone_view();

    // The result should be different for required and non-required for
    // argument node 6: for required, there should be no results. For
    // non-required, node 6 may be returned.
    $view
      ->set_arguments(array(
      $this->node6->nid,
    ));
    $view
      ->execute();
    if ($required) {
      $this
        ->assertEqual(count($view->result), 0);
    }
    else {
      $this
        ->assertEqual(count($view->result), 1);
      $result = array_pop($view->result);
      $this
        ->assertEqual($result->nid, $this->node6->nid);
      $this
        ->assertEqual($result->node_node_nid, NULL);
    }
  }
}