<?php

/**
 * Tests the many to one helper handler class.
 */
class ViewsHandlerManyToOneTest extends ViewsSqlTest {

  /**
   * Field definitions used by this test.
   *
   * @var array
   */
  protected $fields;

  /**
   * Instances of fields used by this test.
   *
   * @var array
   */
  protected $instances;

  /**
   * Nodes used by this test.
   *
   * @var object[]
   */
  protected $nodes;

  /**
   * Accounts used by this test.
   *
   * @var object[]
   */
  protected $accounts;

  /**
   * Terms used by this test.
   *
   * @var object[]
   */
  protected $terms;

  /**
   * Provide the test's meta information.
   */
  public static function getInfo() {
    return array(
      'name' => 'Handler: Many To One Helper',
      'description' => 'Tests the many to one helper handler',
      'group' => 'Views Handlers',
    );
  }

  /**
   * Clears views data cache.
   */
  protected function clearViewsDataCache() {
    drupal_static_reset('_views_fetch_data_cache');
    drupal_static_reset('_views_fetch_data_recursion_protected');
    drupal_static_reset('_views_fetch_data_fully_loaded');
  }

  /**
   * Returns a new term with random properties.
   *
   * @param string $vocabulary
   *   Vocabulary ID to create term in.
   *
   * @return object
   *   Term with random properties.
   */
  protected function createTerm($vocabulary) {
    $term = new stdClass();
    $term->name = $this->randomName();
    $term->description = $this->randomName();
    // Use the first available text format.
    $term->format = db_query_range('SELECT format FROM {filter_format}', 0, 1)->fetchField();
    $term->vid = $vocabulary->vid;
    taxonomy_term_save($term);
    return $term;
  }

  /**
   * {@inheritdoc}
   */
  public function setUp(array $modules = array()) {
    parent::setUp($modules);

    // Create boolean field.
    $this->fields[0] = array(
      'field_name' => 'field_bool',
      'type' => 'list_boolean',
      'cardinality' => 1,
      'settings' => array(
        'allowed_values' => array(
          0 => '',
          1 => '',
        ),
      ),
    );
    $this->fields[0] = field_create_field($this->fields[0]);

    // Create text list field.
    $this->fields[1] = array(
      'field_name' => 'field_list',
      'type' => 'list_text',
      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
      'settings' => array(
        'allowed_values' => array(
          1 => '1',
          2 => '2',
          3 => '3',
        ),
      ),
    );
    $this->fields[1] = field_create_field($this->fields[1]);

    // Create boolean field instance for article nodes.
    $instance = array(
      'field_name' => $this->fields[0]['field_name'],
      'entity_type' => 'node',
      'bundle' => 'article',
      'widget' => array(
        'type' => 'options_onoff',
      ),
    );
    $this->instances[0][] = field_create_instance($instance);

    // Create text list field instance for article nodes.
    $instance = array(
      'field_name' => $this->fields[1]['field_name'],
      'entity_type' => 'node',
      'bundle' => 'article',
      'widget' => array(
        'type' => 'options_buttons',
      ),
    );
    $this->instances[1][] = field_create_instance($instance);

    // Create boolean field instance for users.
    $instance = array(
      'field_name' => $this->fields[0]['field_name'],
      'entity_type' => 'user',
      'bundle' => 'user',
      'widget' => array(
        'type' => 'options_onoff',
      ),
    );
    $this->instances[0][] = field_create_instance($instance);

    // Create text list field instance for users.
    $instance = array(
      'field_name' => $this->fields[1]['field_name'],
      'entity_type' => 'user',
      'bundle' => 'user',
      'widget' => array(
        'type' => 'options_buttons',
      ),
    );
    $this->instances[1][] = field_create_instance($instance);

    // Create tags field instance for users.
    $instance = array(
      'field_name' => 'field_tags',
      'entity_type' => 'user',
      'bundle' => 'user',
    );
    $this->instances[2][] = field_create_instance($instance);

    // Clear views data cache.
    $this->clearViewsDataCache();

    // Create 62 tags.
    $vocabulary = taxonomy_vocabulary_machine_name_load('tags');
    for ($i = 0; $i < 62; $i++) {
      $this->terms[] = $this->createTerm($vocabulary);
    }

    // Create a node where the field_bool is checked, field_list is '1' and
    // tag is term 2.
    $node = array();
    $node['type'] = 'article';
    $node[$this->fields[0]['field_name']][LANGUAGE_NONE][]['value'] = '1';
    $node[$this->fields[1]['field_name']][LANGUAGE_NONE][]['value'] = '1';
    $node['field_tags'][LANGUAGE_NONE][]['tid'] = $this->terms[1]->tid;
    $this->nodes[0] = $this->drupalCreateNode($node);

    // Create a node where the field_bool is not checked, field_list is empty
    // and tag is term 1.
    $node = array();
    $node['type'] = 'article';
    $node[$this->fields[0]['field_name']] = array();
    $node[$this->fields[1]['field_name']] = array();
    $node['field_tags'][LANGUAGE_NONE][]['tid'] = $this->terms[0]->tid;
    $this->nodes[1] = $this->drupalCreateNode($node);

    // Create a node where the field_bool is not checked, field_list is empty
    // and tag is term 1 and 2.
    $node = array();
    $node['type'] = 'article';
    $node[$this->fields[0]['field_name']] = array();
    $node[$this->fields[1]['field_name']] = array();
    $node['field_tags'][LANGUAGE_NONE][]['tid'] = $this->terms[0]->tid;
    $node['field_tags'][LANGUAGE_NONE][]['tid'] = $this->terms[1]->tid;
    $this->nodes[2] = $this->drupalCreateNode($node);

    // Create a user where field_bool is checked, field_list is '1' and tag is
    // term 1.
    $permissions = array('access content');
    $account = $this->drupalCreateUser($permissions);
    $account->{$this->fields[0]['field_name']}[LANGUAGE_NONE][]['value'] = '1';
    $account->{$this->fields[1]['field_name']}[LANGUAGE_NONE][]['value'] = '1';
    $account->field_tags[LANGUAGE_NONE][]['tid'] = $this->terms[0]->tid;
    $this->accounts[0] = user_save($account);
  }

  /**
   * Tests "none of" filter with terms in excess of JOIN limit selected.
   */
  public function testJoinLimitNoneOf() {
    $view = $this->getJoinLimitNoneOfTestView();
    $this->executeView($view);

    // Assert that nodes have been created and have expected field values.
    $value = field_get_items('node', $this->nodes[0], 'field_tags', LANGUAGE_NONE);
    $value = isset($value[0]['tid']) ? (int) $value[0]['tid'] : 0;
    $this->assertIdentical($value, 2, 'First node has been created and tags field references term 2.');

    $value = field_get_items('node', $this->nodes[1], 'field_tags', LANGUAGE_NONE);
    $value = isset($value[0]['tid']) ? (int) $value[0]['tid'] : 0;
    $this->assertIdentical($value, 1, 'Second node has been created and tags field references term 1.');

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

    // Assert that node id with empty field value matches user id so that the
    // node would be excluded from the result, 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.');

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

  /**
   * Tests duplicate grouped "none of" filters on boolean field.
   */
  public function testGroupedNoneOf() {
    $view = $this->getGroupedNoneOfTestView();
    $this->executeView($view);

    // Assert that nodes have been created and have expected field values.
    $value = field_get_items('node', $this->nodes[0], $this->fields[0]['field_name'], LANGUAGE_NONE);
    $value = isset($value[0]['value']) ? (int) $value[0]['value'] : 0;
    $this->assertIdentical($value, 1, 'First node has been created and boolean field is checked.');

    $value = field_get_items('node', $this->nodes[1], $this->fields[0]['field_name'], LANGUAGE_NONE);
    $this->assertFalse($value, 'Second node has been created and boolean field is not checked.');

    $value = field_get_items('node', $this->nodes[2], $this->fields[0]['field_name'], LANGUAGE_NONE);
    $this->assertFalse($value, 'Third node has been created and boolean field is not checked.');

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

    // Assert that node ID with empty field value matches user ID so that the
    // node would be excluded from the result, 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.');

    // Assert correct result set.
    $result_count = isset($view->result) && is_array($view->result) ? count($view->result) : 0;
    $this->assertEqual($result_count, 2, '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, 'View result has correct node IDs.');
  }

  /**
   * Tests duplicate grouped "one of" filters on taxonomy term field.
   */
  public function testGroupedOneOf() {
    $view = $this->getGroupedOneOfTestView();
    $this->executeView($view);

    // Assert that nodes have been created and have expected field values.
    $value = field_get_items('node', $this->nodes[0], 'field_tags', LANGUAGE_NONE);
    $value = isset($value[0]['tid']) ? (int) $value[0]['tid'] : 0;
    $this->assertIdentical($value, 2, 'First node has been created and tags field references term 2.');

    $value = field_get_items('node', $this->nodes[1], 'field_tags', LANGUAGE_NONE);
    $value = isset($value[0]['tid']) ? (int) $value[0]['tid'] : 0;
    $this->assertIdentical($value, 1, 'Second node has been created and tags field references term 1.');

    $value = field_get_items('node', $this->nodes[2], 'field_tags', LANGUAGE_NONE);
    $value = !empty($value[0]['tid']) && !empty($value[1]['tid']);
    $this->assertTrue($value, 'Third node has been created and tags field references both terms 1 and 2.');

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

    // Assert that node ID with empty field value matches user ID so that the
    // node would be excluded from the result, 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.');

    // Assert correct result set.
    $result_count = isset($view->result) && is_array($view->result) ? count($view->result) : 0;
    $this->assertEqual($result_count, 2, '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, 'View result has correct node IDs.');
  }

  /**
   * Tests exposed filter with "Reduce duplicates." and grouped options.
   */
  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.');
  }

  /**
   * Tests exposed filter on term ID with grouped options.
   */
  public function testTermIdExposedGroupedOptions() {
    // Assert that nodes have been created and have expected field values.
    $value = field_get_items('node', $this->nodes[0], 'field_tags', LANGUAGE_NONE);
    $value = isset($value[0]['tid']) ? (int) $value[0]['tid'] : 0;
    $this->assertIdentical($value, 2, 'First node has been created and tags field references term 2.');

    $value = field_get_items('node', $this->nodes[1], 'field_tags', LANGUAGE_NONE);
    $value = isset($value[0]['tid']) ? (int) $value[0]['tid'] : 0;
    $this->assertIdentical($value, 1, 'Second node has been created and tags field references term 1.');

    // Assert that user has been created and has expected field values.
    $value = field_get_items('user', $this->accounts[0], 'field_tags', LANGUAGE_NONE);
    $value = isset($value[0]['tid']) ? (int) $value[0]['tid'] : 0;
    $this->assertIdentical($value, 1, 'User has been created and tags field references term 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->getTermIdExposedGroupedOptionsTestView();
    $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 2.
    $view = $this->getTermIdExposedGroupedOptionsTestView();
    $view->set_exposed_input(array(
      'field_tags_tid' => '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, 1, 'Option 1: View has one result.');
    $nid = isset($view->result[0]->nid) ? (int) $view->result[0]->nid : 0;
    $this->assertIdentical($nid, (int) $this->nodes[1]->nid, 'Option 1: View result has correct node ID.');

    // Option 2: Is none of 1 or 2.
    $view = $this->getTermIdExposedGroupedOptionsTestView();
    $view->set_exposed_input(array(
      'field_tags_tid' => '2',
    ));
    $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 2: View has empty result.');

    // Option 3: Is one of 1.
    $view = $this->getTermIdExposedGroupedOptionsTestView();
    $view->set_exposed_input(array(
      'field_tags_tid' => '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, 2, 'Option 3: 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 3: View result has correct node ID.');

    // Option 4: Is one of 1 or 2.
    $view = $this->getTermIdExposedGroupedOptionsTestView();
    $view->set_exposed_input(array(
      'field_tags_tid' => '4',
    ));
    $this->executeView($view);
    $this->verbose(isset($view->result) ? $view->result : NULL);

    // Assert correct result set.
    $result_count = isset($view->result) && is_array($view->result) ? count($view->result) : 0;
    $this->assertEqual($result_count, 4, 'Option 4: View has four 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);
    $nid = isset($view->result[3]->nid) ? (int) $view->result[3]->nid : 0;
    $result4 = ($nid === (int) $this->nodes[2]->nid);
    $this->assertTrue($result1 && $result2 && $result3 && $result4, 'Option 4: View result has correct node ID.');

    // @todo: Fix and uncomment in issue #3045168.
    // Option 5: Is all of 1 and 2.
    // @code
    // $view = $this->getTermIdExposedGroupedOptionsTestView();
    // $view->set_exposed_input(array(
    //   'field_tags_tid' => '5',
    // ));
    // $this->executeView($view);
    //
    // // Assert correct result set.
    // $result_count = isset($view->result) && is_array($view->result) ? count($view->result) : 0;
    // $nid = isset($view->result[0]->nid) ? (int) $view->result[0]->nid : 0;
    // $this->assertIdentical($nid, (int) $this->nodes[2]->nid, 'Option 5: View result has correct node ID.');
    // $this->assertIdentical($result_count, 1, 'Option 5: View has one result.');
    // @endcode

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

    // Assert correct result set.
    $result_count = isset($view->result) && is_array($view->result) ? count($view->result) : 1;
    $this->assertIdentical($result_count, 0, 'Option 6: View has empty result.');

    // Option 7: Is not empty.
    $view = $this->getTermIdExposedGroupedOptionsTestView();
    $view->set_exposed_input(array(
      'field_tags_tid' => '7',
    ));
    $this->executeView($view);

    // Assert correct result set.
    $result_count = isset($view->result) && is_array($view->result) ? count($view->result) : 0;
    $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);
    $nid = isset($view->result[3]->nid) ? (int) $view->result[3]->nid : 0;
    $result4 = ($nid === (int) $this->nodes[2]->nid);
    $this->assertTrue($result1 && $result2 && $result3 && $result4, 'Option 7: View result has correct node ID.');
    $this->verbose($view->result);
    $this->assertIdentical($result_count, 4, 'Option 7: View has four results.');
  }

  /**
   * Generates test_not view.
   */
  protected function getGroupedNoneOfTestView() {
    $view = new view();
    $view->name = 'test_not';
    $view->description = '';
    $view->tag = 'default';
    $view->base_table = 'node';
    $view->human_name = 'test_not';
    $view->core = 7;
    $view->api_version = '3.0';
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

    /* Display: Master */
    $handler = $view->new_display('default', 'Master', 'default');
    $handler->display->display_options['use_more_always'] = FALSE;
    $handler->display->display_options['access']['type'] = 'perm';
    $handler->display->display_options['cache']['type'] = 'none';
    $handler->display->display_options['query']['type'] = 'views_query';
    $handler->display->display_options['exposed_form']['type'] = 'basic';
    $handler->display->display_options['pager']['type'] = 'full';
    $handler->display->display_options['style_plugin'] = 'default';
    $handler->display->display_options['row_plugin'] = 'fields';
    /* Field: Content: Title */
    $handler->display->display_options['fields']['title']['id'] = 'title';
    $handler->display->display_options['fields']['title']['table'] = 'node';
    $handler->display->display_options['fields']['title']['field'] = 'title';
    $handler->display->display_options['fields']['title']['label'] = '';
    $handler->display->display_options['fields']['title']['alter']['word_boundary'] = FALSE;
    $handler->display->display_options['fields']['title']['alter']['ellipsis'] = FALSE;
    /* Sort criterion: Content: Nid */
    $handler->display->display_options['sorts']['nid']['id'] = 'nid';
    $handler->display->display_options['sorts']['nid']['table'] = 'node';
    $handler->display->display_options['sorts']['nid']['field'] = 'nid';
    $handler->display->display_options['filter_groups']['operator'] = 'OR';
    $handler->display->display_options['filter_groups']['groups'] = array(
      1 => 'AND',
      2 => 'AND',
    );
    /* Filter criterion: Content: Published */
    $handler->display->display_options['filters']['status']['id'] = 'status';
    $handler->display->display_options['filters']['status']['table'] = 'node';
    $handler->display->display_options['filters']['status']['field'] = 'status';
    $handler->display->display_options['filters']['status']['value'] = 1;
    $handler->display->display_options['filters']['status']['group'] = 1;
    $handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
    /* Filter criterion: Content: Type */
    $handler->display->display_options['filters']['type']['id'] = 'type';
    $handler->display->display_options['filters']['type']['table'] = 'node';
    $handler->display->display_options['filters']['type']['field'] = 'type';
    $handler->display->display_options['filters']['type']['value'] = array(
      'article' => 'article',
    );
    $handler->display->display_options['filters']['type']['group'] = 1;
    /* Filter criterion: Field: field_bool (field_bool) */
    $handler->display->display_options['filters']['field_bool_value']['id'] = 'field_bool_value';
    $handler->display->display_options['filters']['field_bool_value']['table'] = 'field_data_field_bool';
    $handler->display->display_options['filters']['field_bool_value']['field'] = 'field_bool_value';
    $handler->display->display_options['filters']['field_bool_value']['operator'] = 'not';
    $handler->display->display_options['filters']['field_bool_value']['value'] = array(
      1 => '1',
    );
    $handler->display->display_options['filters']['field_bool_value']['group'] = 1;
    /* Filter criterion: Field: field_bool (field_bool) */
    $handler->display->display_options['filters']['field_bool_value_1']['id'] = 'field_bool_value_1';
    $handler->display->display_options['filters']['field_bool_value_1']['table'] = 'field_data_field_bool';
    $handler->display->display_options['filters']['field_bool_value_1']['field'] = 'field_bool_value';
    $handler->display->display_options['filters']['field_bool_value_1']['operator'] = 'not';
    $handler->display->display_options['filters']['field_bool_value_1']['value'] = array(
      1 => '1',
    );
    $handler->display->display_options['filters']['field_bool_value_1']['group'] = 2;
    /* Filter criterion: Content: Type */
    $handler->display->display_options['filters']['type_1']['id'] = 'type_1';
    $handler->display->display_options['filters']['type_1']['table'] = 'node';
    $handler->display->display_options['filters']['type_1']['field'] = 'type';
    $handler->display->display_options['filters']['type_1']['value'] = array(
      'article' => 'article',
    );
    $handler->display->display_options['filters']['type_1']['group'] = 2;
    /* Filter criterion: Content: Published */
    $handler->display->display_options['filters']['status_1']['id'] = 'status_1';
    $handler->display->display_options['filters']['status_1']['table'] = 'node';
    $handler->display->display_options['filters']['status_1']['field'] = 'status';
    $handler->display->display_options['filters']['status_1']['value'] = '1';
    $handler->display->display_options['filters']['status_1']['group'] = 2;

    return $view;
  }

  /**
   * Generates test_oneof view.
   */
  protected function getGroupedOneOfTestView() {
    $view = new view();
    $view->name = 'test_oneof';
    $view->description = '';
    $view->tag = 'default';
    $view->base_table = 'node';
    $view->human_name = 'test_oneof';
    $view->core = 7;
    $view->api_version = '3.0';
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

    /* Display: Master */
    $handler = $view->new_display('default', 'Master', 'default');
    $handler->display->display_options['use_more_always'] = FALSE;
    $handler->display->display_options['access']['type'] = 'perm';
    $handler->display->display_options['cache']['type'] = 'none';
    $handler->display->display_options['query']['type'] = 'views_query';
    $handler->display->display_options['exposed_form']['type'] = 'basic';
    $handler->display->display_options['pager']['type'] = 'full';
    $handler->display->display_options['style_plugin'] = 'default';
    $handler->display->display_options['row_plugin'] = 'fields';
    /* Field: Content: Title */
    $handler->display->display_options['fields']['title']['id'] = 'title';
    $handler->display->display_options['fields']['title']['table'] = 'node';
    $handler->display->display_options['fields']['title']['field'] = 'title';
    $handler->display->display_options['fields']['title']['label'] = '';
    $handler->display->display_options['fields']['title']['alter']['word_boundary'] = FALSE;
    $handler->display->display_options['fields']['title']['alter']['ellipsis'] = FALSE;
    /* Sort criterion: Content: Nid */
    $handler->display->display_options['sorts']['nid']['id'] = 'nid';
    $handler->display->display_options['sorts']['nid']['table'] = 'node';
    $handler->display->display_options['sorts']['nid']['field'] = 'nid';
    $handler->display->display_options['filter_groups']['operator'] = 'OR';
    $handler->display->display_options['filter_groups']['groups'] = array(
      1 => 'AND',
      2 => 'AND',
    );
    /* Filter criterion: Content: Tags (field_tags) */
    $handler->display->display_options['filters']['field_tags_tid']['id'] = 'field_tags_tid';
    $handler->display->display_options['filters']['field_tags_tid']['table'] = 'field_data_field_tags';
    $handler->display->display_options['filters']['field_tags_tid']['field'] = 'field_tags_tid';
    $handler->display->display_options['filters']['field_tags_tid']['value'] = array(
      1 => '1',
    );
    $handler->display->display_options['filters']['field_tags_tid']['group'] = 2;
    $handler->display->display_options['filters']['field_tags_tid']['reduce_duplicates'] = TRUE;
    $handler->display->display_options['filters']['field_tags_tid']['type'] = 'select';
    $handler->display->display_options['filters']['field_tags_tid']['vocabulary'] = 'tags';
    /* Filter criterion: Content: Tags (field_tags) */
    $handler->display->display_options['filters']['field_tags_tid_1']['id'] = 'field_tags_tid_1';
    $handler->display->display_options['filters']['field_tags_tid_1']['table'] = 'field_data_field_tags';
    $handler->display->display_options['filters']['field_tags_tid_1']['field'] = 'field_tags_tid';
    $handler->display->display_options['filters']['field_tags_tid_1']['value'] = array(
      1 => '1',
    );
    $handler->display->display_options['filters']['field_tags_tid_1']['reduce_duplicates'] = TRUE;
    $handler->display->display_options['filters']['field_tags_tid_1']['type'] = 'select';
    $handler->display->display_options['filters']['field_tags_tid_1']['vocabulary'] = 'tags';
    return $view;
  }

  /**
   * Generates test_reduced_exposed_grouped_options view.
   */
  protected function getReducedExposedGroupedOptionsTestView() {
    $view = new view();
    $view->name = 'test_reduced_exposed_grouped_options';
    $view->description = '';
    $view->tag = 'default';
    $view->base_table = 'node';
    $view->human_name = 'test_reduced_exposed_grouped_options';
    $view->core = 7;
    $view->api_version = '3.0';
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

    /* Display: Master */
    $handler = $view->new_display('default', 'Master', 'default');
    $handler->display->display_options['use_more_always'] = FALSE;
    $handler->display->display_options['access']['type'] = 'perm';
    $handler->display->display_options['cache']['type'] = 'none';
    $handler->display->display_options['query']['type'] = 'views_query';
    $handler->display->display_options['exposed_form']['type'] = 'basic';
    $handler->display->display_options['pager']['type'] = 'full';
    $handler->display->display_options['style_plugin'] = 'default';
    $handler->display->display_options['row_plugin'] = 'fields';
    /* Field: Content: Title */
    $handler->display->display_options['fields']['title']['id'] = 'title';
    $handler->display->display_options['fields']['title']['table'] = 'node';
    $handler->display->display_options['fields']['title']['field'] = 'title';
    $handler->display->display_options['fields']['title']['label'] = '';
    $handler->display->display_options['fields']['title']['alter']['word_boundary'] = FALSE;
    $handler->display->display_options['fields']['title']['alter']['ellipsis'] = FALSE;
    /* Sort criterion: Content: Nid */
    $handler->display->display_options['sorts']['nid']['id'] = 'nid';
    $handler->display->display_options['sorts']['nid']['table'] = 'node';
    $handler->display->display_options['sorts']['nid']['field'] = 'nid';
    /* Filter criterion: Content: Published */
    $handler->display->display_options['filters']['status']['id'] = 'status';
    $handler->display->display_options['filters']['status']['table'] = 'node';
    $handler->display->display_options['filters']['status']['field'] = 'status';
    $handler->display->display_options['filters']['status']['value'] = 1;
    $handler->display->display_options['filters']['status']['group'] = 1;
    $handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
    /* Filter criterion: Content: list (field_list) */
    $handler->display->display_options['filters']['field_list_value']['id'] = 'field_list_value';
    $handler->display->display_options['filters']['field_list_value']['table'] = 'field_data_field_list';
    $handler->display->display_options['filters']['field_list_value']['field'] = 'field_list_value';
    $handler->display->display_options['filters']['field_list_value']['exposed'] = TRUE;
    $handler->display->display_options['filters']['field_list_value']['expose']['operator_id'] = 'field_list_value_op';
    $handler->display->display_options['filters']['field_list_value']['expose']['label'] = 'list (field_list)';
    $handler->display->display_options['filters']['field_list_value']['expose']['operator'] = 'field_list_value_op';
    $handler->display->display_options['filters']['field_list_value']['expose']['identifier'] = 'field_list_value';
    $handler->display->display_options['filters']['field_list_value']['is_grouped'] = TRUE;
    $handler->display->display_options['filters']['field_list_value']['group_info']['label'] = 'list (field_list)';
    $handler->display->display_options['filters']['field_list_value']['group_info']['identifier'] = 'field_list_value';
    $handler->display->display_options['filters']['field_list_value']['group_info']['group_items'] = array(
      1 => array(
        'title' => 'Not 1 or 2',
        'operator' => 'not',
        'value' => array(
          1 => '1',
          2 => '2',
        ),
      ),
      2 => array(
        'title' => '1',
        'operator' => 'or',
        'value' => array(
          1 => '1',
        ),
      ),
      3 => array(
        'title' => '1 or 2',
        'operator' => 'or',
        'value' => array(
          1 => '1',
          2 => '2',
        ),
      ),
      4 => array(
        'title' => '1 and 2',
        'operator' => 'and',
        'value' => array(
          1 => '1',
          2 => '2',
        ),
      ),
      5 => array(
        'title' => 'empty',
        'operator' => 'empty',
        'value' => array(),
      ),
      6 => array(
        'title' => 'not empty',
        'operator' => 'not empty',
        'value' => array(),
      ),
    );
    return $view;
  }

  /**
   * Generates test_tid_exposed_grouped_options view.
   */
  protected function getTermIdExposedGroupedOptionsTestView() {
    $view = new view();
    $view->name = 'test_tid_exposed_grouped_options';
    $view->description = '';
    $view->tag = 'default';
    $view->base_table = 'node';
    $view->human_name = 'test_tid_exposed_grouped_options';
    $view->core = 7;
    $view->api_version = '3.0';
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

    /* Display: Master */
    $handler = $view->new_display('default', 'Master', 'default');
    $handler->display->display_options['use_more_always'] = FALSE;
    $handler->display->display_options['access']['type'] = 'perm';
    $handler->display->display_options['cache']['type'] = 'none';
    $handler->display->display_options['query']['type'] = 'views_query';
    $handler->display->display_options['exposed_form']['type'] = 'basic';
    $handler->display->display_options['pager']['type'] = 'full';
    $handler->display->display_options['style_plugin'] = 'default';
    $handler->display->display_options['row_plugin'] = 'fields';
    /* Field: Content: Title */
    $handler->display->display_options['fields']['title']['id'] = 'title';
    $handler->display->display_options['fields']['title']['table'] = 'node';
    $handler->display->display_options['fields']['title']['field'] = 'title';
    $handler->display->display_options['fields']['title']['label'] = '';
    $handler->display->display_options['fields']['title']['alter']['word_boundary'] = FALSE;
    $handler->display->display_options['fields']['title']['alter']['ellipsis'] = FALSE;
    /* Sort criterion: Content: Nid */
    $handler->display->display_options['sorts']['nid']['id'] = 'nid';
    $handler->display->display_options['sorts']['nid']['table'] = 'node';
    $handler->display->display_options['sorts']['nid']['field'] = 'nid';
    /* Filter criterion: Content: Published */
    $handler->display->display_options['filters']['status']['id'] = 'status';
    $handler->display->display_options['filters']['status']['table'] = 'node';
    $handler->display->display_options['filters']['status']['field'] = 'status';
    $handler->display->display_options['filters']['status']['value'] = 1;
    $handler->display->display_options['filters']['status']['group'] = 1;
    $handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
    /* Filter criterion: Content: Tags (field_tags) */
    $handler->display->display_options['filters']['field_tags_tid']['id'] = 'field_tags_tid';
    $handler->display->display_options['filters']['field_tags_tid']['table'] = 'field_data_field_tags';
    $handler->display->display_options['filters']['field_tags_tid']['field'] = 'field_tags_tid';
    $handler->display->display_options['filters']['field_tags_tid']['value'] = array(
      1 => '1',
      2 => '2',
    );
    $handler->display->display_options['filters']['field_tags_tid']['exposed'] = TRUE;
    $handler->display->display_options['filters']['field_tags_tid']['expose']['operator_id'] = 'field_tags_tid_op';
    $handler->display->display_options['filters']['field_tags_tid']['expose']['label'] = 'Tags (field_tags)';
    $handler->display->display_options['filters']['field_tags_tid']['expose']['operator'] = 'field_tags_tid_op';
    $handler->display->display_options['filters']['field_tags_tid']['expose']['identifier'] = 'field_tags_tid';
    $handler->display->display_options['filters']['field_tags_tid']['expose']['remember_roles'] = array(
      2 => '2',
    );
    $handler->display->display_options['filters']['field_tags_tid']['is_grouped'] = TRUE;
    $handler->display->display_options['filters']['field_tags_tid']['group_info']['label'] = 'Tags (field_tags)';
    $handler->display->display_options['filters']['field_tags_tid']['group_info']['identifier'] = 'field_tags_tid';
    $handler->display->display_options['filters']['field_tags_tid']['group_info']['group_items'] = array(
      1 => array(
        'title' => 'Is none of 2',
        'operator' => 'not',
        'value' => array(
          2 => '2',
        ),
      ),
      2 => array(
        'title' => 'Is none of 1 or 2',
        'operator' => 'not',
        'value' => array(
          1 => '1',
          2 => '2',
        ),
      ),
      3 => array(
        'title' => 'Is one of 1',
        'operator' => 'or',
        'value' => array(
          1 => '1',
        ),
      ),
      4 => array(
        'title' => 'Is one of 1 or 2',
        'operator' => 'or',
        'value' => array(
          1 => '1',
          2 => '2',
        ),
      ),
      5 => array(
        'title' => 'Is all of 1 and 2',
        'operator' => 'and',
        'value' => array(
          1 => '1',
          2 => '2',
        ),
      ),
      6 => array(
        'title' => 'Is empty',
        'operator' => 'empty',
        'value' => array(
          1 => '1',
          2 => '2',
        ),
      ),
      7 => array(
        'title' => 'Is not empty',
        'operator' => 'not empty',
        'value' => array(
          1 => '1',
          2 => '2',
        ),
      ),
    );
    $handler->display->display_options['filters']['field_tags_tid']['type'] = 'select';
    $handler->display->display_options['filters']['field_tags_tid']['vocabulary'] = 'tags';
    return $view;
  }

  /**
   * Generates test_join_limit_none_of view.
   */
  protected function getJoinLimitNoneOfTestView() {
    $view = new view();
    $view->name = 'test_join_limit_none_of';
    $view->description = '';
    $view->tag = 'default';
    $view->base_table = 'node';
    $view->human_name = 'test_join_limit_none_of';
    $view->core = 7;
    $view->api_version = '3.0';
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

    /* Display: Master */
    $handler = $view->new_display('default', 'Master', 'default');
    $handler->display->display_options['use_more_always'] = FALSE;
    $handler->display->display_options['access']['type'] = 'perm';
    $handler->display->display_options['cache']['type'] = 'none';
    $handler->display->display_options['query']['type'] = 'views_query';
    $handler->display->display_options['exposed_form']['type'] = 'basic';
    $handler->display->display_options['pager']['type'] = 'full';
    $handler->display->display_options['style_plugin'] = 'default';
    $handler->display->display_options['row_plugin'] = 'fields';
    /* Field: Content: Title */
    $handler->display->display_options['fields']['title']['id'] = 'title';
    $handler->display->display_options['fields']['title']['table'] = 'node';
    $handler->display->display_options['fields']['title']['field'] = 'title';
    $handler->display->display_options['fields']['title']['label'] = '';
    $handler->display->display_options['fields']['title']['alter']['word_boundary'] = FALSE;
    $handler->display->display_options['fields']['title']['alter']['ellipsis'] = FALSE;
    /* Sort criterion: Content: Post date */
    $handler->display->display_options['sorts']['created']['id'] = 'created';
    $handler->display->display_options['sorts']['created']['table'] = 'node';
    $handler->display->display_options['sorts']['created']['field'] = 'created';
    $handler->display->display_options['sorts']['created']['order'] = 'DESC';
    /* Filter criterion: Content: Published */
    $handler->display->display_options['filters']['status']['id'] = 'status';
    $handler->display->display_options['filters']['status']['table'] = 'node';
    $handler->display->display_options['filters']['status']['field'] = 'status';
    $handler->display->display_options['filters']['status']['value'] = 1;
    $handler->display->display_options['filters']['status']['group'] = 1;
    $handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
    /* Filter criterion: Content: Tags (field_tags) */
    $handler->display->display_options['filters']['field_tags_tid']['id'] = 'field_tags_tid';
    $handler->display->display_options['filters']['field_tags_tid']['table'] = 'field_data_field_tags';
    $handler->display->display_options['filters']['field_tags_tid']['field'] = 'field_tags_tid';
    $handler->display->display_options['filters']['field_tags_tid']['operator'] = 'not';
    $handler->display->display_options['filters']['field_tags_tid']['value'] = array(
      2 => '2',
      3 => '3',
      4 => '4',
      5 => '5',
      6 => '6',
      7 => '7',
      8 => '8',
      9 => '9',
      10 => '10',
      11 => '11',
      12 => '12',
      13 => '13',
      14 => '14',
      15 => '15',
      16 => '16',
      17 => '17',
      18 => '18',
      19 => '19',
      20 => '20',
      21 => '21',
      22 => '22',
      23 => '23',
      24 => '24',
      25 => '25',
      26 => '26',
      27 => '27',
      28 => '28',
      29 => '29',
      30 => '30',
      31 => '31',
      32 => '32',
      33 => '33',
      34 => '34',
      35 => '35',
      36 => '36',
      37 => '37',
      38 => '38',
      39 => '39',
      40 => '40',
      41 => '41',
      42 => '42',
      43 => '43',
      44 => '44',
      45 => '45',
      46 => '46',
      47 => '47',
      48 => '48',
      49 => '49',
      50 => '50',
      51 => '51',
      52 => '52',
      53 => '53',
      54 => '54',
      55 => '55',
      56 => '56',
      57 => '57',
      58 => '58',
      59 => '59',
      60 => '60',
      61 => '61',
      62 => '62',
      63 => '63',
      64 => '64',
      65 => '65',
      66 => '66',
      67 => '67',
      68 => '68',
      69 => '69',
      61 => '61',
      62 => '62',
    );
    $handler->display->display_options['filters']['field_tags_tid']['type'] = 'select';
    $handler->display->display_options['filters']['field_tags_tid']['vocabulary'] = 'tags';
    return $view;
  }

}
