You are here

public function ViewsHandlerManyToOneTest::testReducedExposedGroupedOptions in Views (for Drupal 7) 7.3

Tests exposed filter with "Reduce duplicates." and grouped options.

File

tests/handlers/views_handler_manytoone.test, line 328

Class

ViewsHandlerManyToOneTest
Tests the many to one helper handler class.

Code

public function testReducedExposedGroupedOptions() {

  // Assert that nodes have been created and have expected field values.
  $value = field_get_items('node', $this->nodes[0], 'field_list', LANGUAGE_NONE);
  $value = isset($value[0]['value']) ? (int) $value[0]['value'] : 0;
  $this
    ->assertIdentical($value, 1, 'First node has been created and list field has value 1.');
  $value = field_get_items('node', $this->nodes[1], 'field_list', LANGUAGE_NONE);
  $this
    ->assertFalse($value, 'Second node has been created and list field is empty.');
  $value = field_get_items('node', $this->nodes[2], 'field_list', LANGUAGE_NONE);
  $this
    ->assertFalse($value, 'Third node has been created and list field is empty.');

  // Assert that user has been created and has expected field values.
  $value = field_get_items('user', $this->accounts[0], 'field_list', LANGUAGE_NONE);
  $value = isset($value[0]['value']) ? (int) $value[0]['value'] : 0;
  $this
    ->assertIdentical($value, 1, 'User has been created and list field has value 1.');

  // Assert that node ID with empty field value matches user ID so that the
  // node would be excluded from the result option 1, if the joins are missing
  // extras.
  $this
    ->assertIdentical((int) $this->accounts[0]->uid, (int) $this->nodes[1]->nid, 'Node ID of second node matches UID of first user.');

  // Default option: Any.
  $view = $this
    ->getReducedExposedGroupedOptionsTestView();
  $this
    ->executeView($view);

  // Assert correct result set.
  $result_count = isset($view->result) && is_array($view->result) ? count($view->result) : 0;
  $this
    ->assertEqual($result_count, 3, 'Default option: View has three results.');
  $nid = isset($view->result[0]->nid) ? (int) $view->result[0]->nid : 0;
  $result1 = $nid === (int) $this->nodes[0]->nid;
  $nid = isset($view->result[1]->nid) ? (int) $view->result[1]->nid : 0;
  $result2 = $nid === (int) $this->nodes[1]->nid;
  $nid = isset($view->result[2]->nid) ? (int) $view->result[2]->nid : 0;
  $result3 = $nid === (int) $this->nodes[2]->nid;
  $this
    ->assertTrue($result1 && $result2 && $result3, 'Default option: View result has correct node ID.');

  // Option 1: Is none of 1 or 2.
  $view = $this
    ->getReducedExposedGroupedOptionsTestView();
  $view
    ->set_exposed_input(array(
    'field_list_value' => '1',
  ));
  $this
    ->executeView($view);

  // Assert correct result set.
  $result_count = isset($view->result) && is_array($view->result) ? count($view->result) : 0;
  $this
    ->assertEqual($result_count, 2, 'Option 1: View has two results.');
  $nid = isset($view->result[0]->nid) ? (int) $view->result[0]->nid : 0;
  $result1 = $nid === (int) $this->nodes[1]->nid;
  $nid = isset($view->result[1]->nid) ? (int) $view->result[1]->nid : 0;
  $result2 = $nid === (int) $this->nodes[2]->nid;
  $this
    ->assertTrue($result1 && $result2, 'Option 1: View result has correct node ID.');

  // Option 2: Is one of 1.
  $view = $this
    ->getReducedExposedGroupedOptionsTestView();
  $view
    ->set_exposed_input(array(
    'field_list_value' => '2',
  ));
  $this
    ->executeView($view);

  // Assert correct result set.
  $result_count = isset($view->result) && is_array($view->result) ? count($view->result) : 0;
  $this
    ->assertEqual($result_count, 1, 'Option 2: View has one result.');
  $nid = isset($view->result[0]->nid) ? (int) $view->result[0]->nid : 0;
  $this
    ->assertIdentical($nid, (int) $this->nodes[0]->nid, 'Option 2: View result has correct node ID.');

  // Option 3: Is one of 1 or 2.
  $view = $this
    ->getReducedExposedGroupedOptionsTestView();
  $view
    ->set_exposed_input(array(
    'field_list_value' => '3',
  ));
  $this
    ->executeView($view);

  // Assert correct result set.
  $result_count = isset($view->result) && is_array($view->result) ? count($view->result) : 0;
  $this
    ->assertEqual($result_count, 1, 'Option 3: View has one result.');
  $nid = isset($view->result[0]->nid) ? (int) $view->result[0]->nid : 0;
  $this
    ->assertIdentical($nid, (int) $this->nodes[0]->nid, 'Option 3: View result has correct node ID.');

  /* @todo: Fix and uncomment in issue #3045168.
   * // Option 4: Is all of 1 and 2.
   * $view = $this->getReducedExposedGroupedOptionsTestView();
   * $view->set_exposed_input(array(
   *   'field_list_value' => '4',
   * ));
   * $this->executeView($view);
   *
   * // Assert correct result set.
   * $result_count = isset($view->result) && is_array($view->result) ? count($view->result) : 1;
   * $this->assertEqual($result_count, 0, 'Option 4: View has empty result.');
   */

  // Option 5: Is empty.
  $view = $this
    ->getReducedExposedGroupedOptionsTestView();
  $view
    ->set_exposed_input(array(
    'field_list_value' => '5',
  ));
  $this
    ->executeView($view);

  // Assert correct result set.
  $result_count = isset($view->result) && is_array($view->result) ? count($view->result) : 0;
  $this
    ->assertEqual($result_count, 2, 'Option 5: View has two results.');
  $nid = isset($view->result[0]->nid) ? (int) $view->result[0]->nid : 0;
  $result1 = $nid === (int) $this->nodes[1]->nid;
  $nid = isset($view->result[1]->nid) ? (int) $view->result[1]->nid : 0;
  $result2 = $nid === (int) $this->nodes[2]->nid;
  $this
    ->assertTrue($result1 && $result2, 'Option 5: View result has correct node IDs.');

  // Option 6: Is not empty.
  $view = $this
    ->getReducedExposedGroupedOptionsTestView();
  $view
    ->set_exposed_input(array(
    'field_list_value' => '6',
  ));
  $this
    ->executeView($view);

  // Assert correct result set.
  $result_count = isset($view->result) && is_array($view->result) ? count($view->result) : 0;
  $this
    ->assertEqual($result_count, 1, 'Option 6: View has one result.');
  $nid = isset($view->result[0]->nid) ? (int) $view->result[0]->nid : 0;
  $this
    ->assertIdentical($nid, (int) $this->nodes[0]->nid, 'Option 6: View result has correct node ID.');
}