You are here

function RelationViewsTestCase::testDirectionalRelations in Relation 7

Tests views with directional relations to source, to target and to both.

File

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

Class

RelationViewsTestCase
Functional test of Relation's integration with Views.

Code

function testDirectionalRelations() {
  foreach (array(
    FALSE,
    TRUE,
  ) as $required) {
    for ($r_index = -1; $r_index < 2; $r_index++) {
      $view = new view();
      $handler = $view
        ->new_display('default');
      $handler->display->display_options['relationships']['relation_directional_node']['id'] = 'relation_directional_node';
      $handler->display->display_options['relationships']['relation_directional_node']['table'] = 'node';
      $handler->display->display_options['relationships']['relation_directional_node']['field'] = 'relation_directional_node';
      $handler->display->display_options['relationships']['relation_directional_node']['required'] = $required;
      $handler->display->display_options['relationships']['relation_directional_node']['r_index'] = $r_index;
      $handler->display->display_options['fields']['nid_source']['id'] = 'nid';
      $handler->display->display_options['fields']['nid_source']['table'] = 'node';
      $handler->display->display_options['fields']['nid_source']['field'] = 'nid';
      $handler->display->display_options['fields']['nid_target']['id'] = 'nid';
      $handler->display->display_options['fields']['nid_target']['table'] = 'node';
      $handler->display->display_options['fields']['nid_target']['field'] = 'nid';
      $handler->display->display_options['fields']['nid_target']['relationship'] = 'relation_directional_node';
      $handler->display->display_options['arguments']['nid']['id'] = 'nid';
      $handler->display->display_options['arguments']['nid']['table'] = 'node';
      $handler->display->display_options['arguments']['nid']['field'] = 'nid';

      // First test: node that has relations. The results should be the same
      // for required and non-required relations.
      $view
        ->set_arguments(array(
        $this->node3->nid,
      ));
      $view
        ->execute();
      switch ($r_index) {
        case -1:

          // Directional, both ways.
          $this
            ->assertEqual(count($view->result), 2);
          $targetmatches = array(
            $this->node1->nid => TRUE,
            $this->node4->nid => TRUE,
          );
          foreach ($view->result as $result) {
            $this
              ->assertEqual($result->nid, $this->node3->nid);
            unset($targetmatches[$result->node_node_nid]);
          }
          $this
            ->assertFalse($targetmatches);
          break;
        case 0:

          // Source. This finds the 3->4 relation.
          $this
            ->assertEqual(count($view->result), 1);
          $this
            ->assertEqual($view->result[0]->nid, $this->node3->nid);
          $this
            ->assertEqual($view->result[0]->node_node_nid, $this->node4->nid);
          break;
        case 1:

          // Target. This finds the 1->3 relation.
          $this
            ->assertEqual(count($view->result), 1);
          $this
            ->assertEqual($view->result[0]->nid, $this->node3->nid);
          $this
            ->assertEqual($view->result[0]->node_node_nid, $this->node1->nid);
          break;
      }

      // Second test: node that has no relations. The results should be that
      // no results are found for the required relation, and 6 / NULL
      // for the optional relation.
      $view = $view
        ->clone_view();
      $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);
      }
    }
  }
}