EntityQueryRelationshipTest.php 5.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
<?php

/**
 * @file
 * Definition of Drupal\Core\Entity\Tests\EntityQueryRelationshipTest.
 */

namespace Drupal\system\Tests\Entity;

/**
 * Tests Entity Query API relationship functionality.
 */
13
class EntityQueryRelationshipTest extends EntityUnitTestBase  {
14 15 16 17 18 19

  /**
   * Modules to enable.
   *
   * @var array
   */
20
  public static $modules = array('taxonomy', 'options');
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

  /**
   * @var \Drupal\field_sql_storage\Entity\QueryFactory
   */
  protected $factory;

  /**
   * Term entities.
   *
   * @var array
   */
  protected $terms;

  /**
   * User entities.
   *
   * @var array
   */
  public $accounts;

  /**
   * entity_test entities.
   *
   * @var array
   */
  protected $entities;

  /**
   * The name of the taxonomy field used for test.
   *
   * @var string
   */
  protected $fieldName;

  /**
   * The results returned by EntityQuery.
   *
   * @var array
   */
  protected $queryResults;

  public static function getInfo() {
    return array(
      'name' => 'Entity Query relationship',
      'description' => 'Tests the Entity Query relationship API',
      'group' => 'Entity API',
    );
  }

70
  public function setUp() {
71
    parent::setUp();
72 73 74

    $this->installSchema('taxonomy', array('taxonomy_term_data', 'taxonomy_term_hierarchy'));

75 76 77 78 79 80 81 82 83 84 85 86 87
    // We want a taxonomy term reference field. It needs a vocabulary, terms,
    // a field and an instance. First, create the vocabulary.
    $vocabulary = entity_create('taxonomy_vocabulary', array(
      'vid' => drupal_strtolower($this->randomName()),
    ));
    $vocabulary->save();
    // Second, create the field.
    $this->fieldName = strtolower($this->randomName());
    $field = array(
      'field_name' => $this->fieldName,
      'type' => 'taxonomy_term_reference',
    );
    $field['settings']['allowed_values']['vocabulary'] = $vocabulary->id();
88
    entity_create('field_entity', $field)->save();
89
    // Third, create the instance.
90
    entity_create('field_instance', array(
91 92 93
      'entity_type' => 'entity_test',
      'field_name' => $this->fieldName,
      'bundle' => 'entity_test',
94
    ))->save();
95 96 97 98 99 100 101 102
    // Create two terms and also two accounts.
    for ($i = 0; $i <= 1; $i++) {
      $term = entity_create('taxonomy_term', array(
        'name' => $this->randomName(),
        'vid' => $vocabulary->id(),
      ));
      $term->save();
      $this->terms[] = $term;
103
      $this->accounts[] = $this->createUser();
104 105 106 107 108 109 110 111
    }
    // Create three entity_test entities, the 0th entity will point to the
    // 0th account and 0th term, the 1st and 2nd entity will point to the
    // 1st account and 1st term.
    for ($i = 0; $i <= 2; $i++) {
      $entity = entity_create('entity_test', array());
      $entity->name->value = $this->randomName();
      $index = $i ? 1 : 0;
112
      $entity->user_id->target_id = $this->accounts[$index]->uid;
113
      $entity->{$this->fieldName}->target_id = $this->terms[$index]->id();
114 115 116
      $entity->save();
      $this->entities[] = $entity;
    }
117
    $this->factory = \Drupal::service('entity.query');
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  }

  /**
   * Tests querying.
   */
  public function testQuery() {
    // This returns the 0th entity as that's only one pointing to the 0th
    // account.
    $this->queryResults = $this->factory->get('entity_test')
      ->condition("user_id.entity.name", $this->accounts[0]->name)
      ->execute();
    $this->assertResults(array(0));
    // This returns the 1st and 2nd entity as those point to the 1st account.
    $this->queryResults = $this->factory->get('entity_test')
      ->condition("user_id.entity.name", $this->accounts[0]->name, '<>')
      ->execute();
    $this->assertResults(array(1, 2));
    // This returns all three entities because all of them point to an
    // account.
    $this->queryResults = $this->factory->get('entity_test')
      ->exists("user_id.entity.name")
      ->execute();
    $this->assertResults(array(0, 1, 2));
    // This returns no entities because all of them point to an account.
    $this->queryResults = $this->factory->get('entity_test')
      ->notExists("user_id.entity.name")
      ->execute();
    $this->assertEqual(count($this->queryResults), 0);
    // This returns the 0th entity as that's only one pointing to the 0th
    // term (test without specifying the field column).
    $this->queryResults = $this->factory->get('entity_test')
149
      ->condition("$this->fieldName.entity.name", $this->terms[0]->name->value)
150 151 152 153 154
      ->execute();
    $this->assertResults(array(0));
    // This returns the 0th entity as that's only one pointing to the 0th
    // term (test with specifying the column name).
    $this->queryResults = $this->factory->get('entity_test')
155
      ->condition("$this->fieldName.target_id.entity.name", $this->terms[0]->name->value)
156 157 158 159
      ->execute();
    $this->assertResults(array(0));
    // This returns the 1st and 2nd entity as those point to the 1st term.
    $this->queryResults = $this->factory->get('entity_test')
160
      ->condition("$this->fieldName.entity.name", $this->terms[0]->name->value, '<>')
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
      ->execute();
    $this->assertResults(array(1, 2));
  }

  /**
   * Assert the results.
   *
   * @param array $expected
   *   A list of indexes in the $this->entities array.
   */
  protected function assertResults($expected) {
    $this->assertEqual(count($this->queryResults), count($expected));
    foreach ($expected as $key) {
      $id = $this->entities[$key]->id();
      $this->assertEqual($this->queryResults[$id], $id);
    }
  }
}