You are here

function RelationViewsTestCase::testDeduplication in Relation 7

Tests views deduplication.

File

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

Class

RelationViewsTestCase
Functional test of Relation's integration with Views.

Code

function testDeduplication() {
  for ($i = 0; $i < 2; $i++) {
    $view = new view();
    $view->base_table = 'node';
    $handler = $view
      ->new_display('default');
    $handler->display->display_options['relationships']['relation_directional_entitydifferent_user']['id'] = 'relation_directional_entitydifferent_user';
    $handler->display->display_options['relationships']['relation_directional_entitydifferent_user']['table'] = 'node';
    $handler->display->display_options['relationships']['relation_directional_entitydifferent_user']['field'] = 'relation_directional_entitydifferent_user';
    $handler->display->display_options['relationships']['relation_directional_entitydifferent_user']['required'] = 1;
    $handler->display->display_options['relationships']['relation_directional_entitydifferent_node']['id'] = 'relation_directional_entitydifferent_node';
    $handler->display->display_options['relationships']['relation_directional_entitydifferent_node']['table'] = 'users';
    $handler->display->display_options['relationships']['relation_directional_entitydifferent_node']['field'] = 'relation_directional_entitydifferent_node';
    $handler->display->display_options['relationships']['relation_directional_entitydifferent_node']['relationship'] = 'relation_directional_entitydifferent_user';
    $handler->display->display_options['relationships']['relation_directional_entitydifferent_node']['required'] = 1;
    $handler->display->display_options['relationships']['relation_directional_entitydifferent_node']['entity_deduplication_right'] = $i;
    $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_1']['id'] = 'nid_1';
    $handler->display->display_options['fields']['nid_1']['table'] = 'node';
    $handler->display->display_options['fields']['nid_1']['field'] = 'nid';
    $handler->display->display_options['fields']['nid_1']['relationship'] = 'relation_directional_entitydifferent_node';
    $handler->display->display_options['arguments']['uid']['id'] = 'uid';
    $handler->display->display_options['arguments']['uid']['table'] = 'users';
    $handler->display->display_options['arguments']['uid']['field'] = 'uid';
    $handler->display->display_options['arguments']['uid']['relationship'] = 'relation_directional_entitydifferent_user';
    $view
      ->set_arguments(array(
      $this->user1->uid,
    ));
    $view
      ->execute();
    $this
      ->assertEqual(count($view->result), 4 - 2 * $i);
    $possible_nids = array(
      $this->node3->nid => TRUE,
      $this->node4->nid => TRUE,
    );
    $fail = FALSE;
    $all_results = array();
    foreach ($view->result as $result) {
      $this
        ->assertTrue(isset($possible_nids[$result->nid]) && isset($possible_nids[$result->node_users_nid]), 'Correct nid found');
      $this
        ->assertFalse(isset($all_results[$result->nid][$result->node_users_nid]), 'No row duplication');
      $all_results[$result->nid][$result->node_users_nid] = TRUE;
      $fail = $fail || $i && $result->nid == $result->node_users_nid;
    }
    if ($i) {
      $this
        ->assertFalse($fail, 'Deduplication worked');
    }
  }
}