Drupal PHP Cross Reference Content Management Systems

Source: /modules/simpletest/tests/entity_query.test - 1681 lines - 67554 bytes - Summary - Text - Print

   1  <?php
   2  
   3  
   4  /**
   5   * @file
   6   * Unit test file for the entity API.
   7   */
   8  
   9  /**
  10   * Tests EntityFieldQuery.
  11   */
  12  class EntityFieldQueryTestCase extends DrupalWebTestCase {
  13  
  14    public static function getInfo() {
  15      return array(
  16        'name' => 'Entity query',
  17        'description' => 'Test the EntityFieldQuery class.',
  18        'group' => 'Entity API',
  19      );
  20    }
  21  
  22    function setUp() {
  23      parent::setUp(array('node', 'field_test', 'entity_query_access_test', 'node_access_test'));
  24  
  25      field_test_create_bundle('bundle1');
  26      field_test_create_bundle('bundle2');
  27      field_test_create_bundle('test_bundle');
  28      field_test_create_bundle('test_entity_bundle');
  29  
  30      $instances = array();
  31      $this->fields = array();
  32      $this->field_names[0] = $field_name = drupal_strtolower($this->randomName() . '_field_name');
  33      $field = array('field_name' => $field_name, 'type' => 'test_field', 'cardinality' => 4);
  34      $field = field_create_field($field);
  35      $this->fields[0] = $field;
  36      $instance = array(
  37        'field_name' => $field_name,
  38        'entity_type' => '',
  39        'bundle' => '',
  40        'label' => $this->randomName() . '_label',
  41        'description' => $this->randomName() . '_description',
  42        'weight' => mt_rand(0, 127),
  43        'settings' => array(
  44          'test_instance_setting' => $this->randomName(),
  45        ),
  46        'widget' => array(
  47          'type' => 'test_field_widget',
  48          'label' => 'Test Field',
  49          'settings' => array(
  50            'test_widget_setting' => $this->randomName(),
  51          )
  52        )
  53      );
  54  
  55      $instances[0] = $instance;
  56  
  57      // Add an instance to that bundle.
  58      $instances[0]['bundle'] = 'bundle1';
  59      $instances[0]['entity_type'] = 'test_entity_bundle_key';
  60      field_create_instance($instances[0]);
  61      $instances[0]['bundle'] = 'bundle2';
  62      field_create_instance($instances[0]);
  63      $instances[0]['bundle'] = $instances[0]['entity_type'] = 'test_entity_bundle';
  64      field_create_instance($instances[0]);
  65  
  66      $this->field_names[1] = $field_name = drupal_strtolower($this->randomName() . '_field_name');
  67      $field = array('field_name' => $field_name, 'type' => 'shape', 'cardinality' => 4);
  68      $field = field_create_field($field);
  69      $this->fields[1] = $field;
  70      $instance = array(
  71        'field_name' => $field_name,
  72        'entity_type' => '',
  73        'bundle' => '',
  74        'label' => $this->randomName() . '_label',
  75        'description' => $this->randomName() . '_description',
  76        'weight' => mt_rand(0, 127),
  77        'settings' => array(
  78          'test_instance_setting' => $this->randomName(),
  79        ),
  80        'widget' => array(
  81          'type' => 'test_field_widget',
  82          'label' => 'Test Field',
  83          'settings' => array(
  84            'test_widget_setting' => $this->randomName(),
  85          )
  86        )
  87      );
  88  
  89      $instances[1] = $instance;
  90  
  91      // Add a field instance to the bundles.
  92      $instances[1]['bundle'] = 'bundle1';
  93      $instances[1]['entity_type'] = 'test_entity_bundle_key';
  94      field_create_instance($instances[1]);
  95      $instances[1]['bundle'] = $instances[1]['entity_type'] = 'test_entity_bundle';
  96      field_create_instance($instances[1]);
  97  
  98      $this->instances = $instances;
  99      // Write entity base table if there is one.
 100      $entities = array();
 101  
 102      // Create entities which have a 'bundle key' defined.
 103      for ($i = 1; $i < 7; $i++) {
 104        $entity = new stdClass();
 105        $entity->ftid = $i;
 106        $entity->fttype = ($i < 5) ? 'bundle1' : 'bundle2';
 107  
 108        $entity->{$this->field_names[0]}[LANGUAGE_NONE][0]['value'] = $i;
 109        drupal_write_record('test_entity_bundle_key', $entity);
 110        field_attach_insert('test_entity_bundle_key', $entity);
 111      }
 112  
 113      $entity = new stdClass();
 114      $entity->ftid = 5;
 115      $entity->fttype = 'test_entity_bundle';
 116      $entity->{$this->field_names[1]}[LANGUAGE_NONE][0]['shape'] = 'square';
 117      $entity->{$this->field_names[1]}[LANGUAGE_NONE][0]['color'] = 'red';
 118      $entity->{$this->field_names[1]}[LANGUAGE_NONE][1]['shape'] = 'circle';
 119      $entity->{$this->field_names[1]}[LANGUAGE_NONE][1]['color'] = 'blue';
 120      drupal_write_record('test_entity_bundle', $entity);
 121      field_attach_insert('test_entity_bundle', $entity);
 122  
 123      $instances[2] = $instance;
 124      $instances[2]['bundle'] = 'test_bundle';
 125      $instances[2]['field_name'] = $this->field_names[0];
 126      $instances[2]['entity_type'] = 'test_entity';
 127      field_create_instance($instances[2]);
 128  
 129      // Create entities with support for revisions.
 130      for ($i = 1; $i < 5; $i++) {
 131        $entity = new stdClass();
 132        $entity->ftid = $i;
 133        $entity->ftvid = $i;
 134        $entity->fttype = 'test_bundle';
 135        $entity->{$this->field_names[0]}[LANGUAGE_NONE][0]['value'] = $i;
 136  
 137        drupal_write_record('test_entity', $entity);
 138        field_attach_insert('test_entity', $entity);
 139        drupal_write_record('test_entity_revision', $entity);
 140      }
 141  
 142      // Add two revisions to an entity.
 143      for ($i = 100; $i < 102; $i++) {
 144        $entity = new stdClass();
 145        $entity->ftid = 4;
 146        $entity->ftvid = $i;
 147        $entity->fttype = 'test_bundle';
 148        $entity->{$this->field_names[0]}[LANGUAGE_NONE][0]['value'] = $i;
 149  
 150        drupal_write_record('test_entity', $entity, 'ftid');
 151        drupal_write_record('test_entity_revision', $entity);
 152  
 153        db_update('test_entity')
 154         ->fields(array('ftvid' => $entity->ftvid))
 155         ->condition('ftid', $entity->ftid)
 156         ->execute();
 157  
 158        field_attach_update('test_entity', $entity);
 159      }
 160    }
 161  
 162    /**
 163     * Tests EntityFieldQuery.
 164     */
 165    function testEntityFieldQuery() {
 166      $query = new EntityFieldQuery();
 167      $query
 168        ->entityCondition('entity_type', 'test_entity_bundle')
 169        ->entityCondition('entity_id', '5');
 170      $this->assertEntityFieldQuery($query, array(
 171        array('test_entity_bundle', 5),
 172      ), t('Test query on an entity type with a generated bundle.'));
 173  
 174      // Test entity_type condition.
 175      $query = new EntityFieldQuery();
 176      $query->entityCondition('entity_type', 'test_entity_bundle_key');
 177      $this->assertEntityFieldQuery($query, array(
 178        array('test_entity_bundle_key', 1),
 179        array('test_entity_bundle_key', 2),
 180        array('test_entity_bundle_key', 3),
 181        array('test_entity_bundle_key', 4),
 182        array('test_entity_bundle_key', 5),
 183        array('test_entity_bundle_key', 6),
 184      ), t('Test entity entity_type condition.'));
 185  
 186      // Test entity_id condition.
 187      $query = new EntityFieldQuery();
 188      $query
 189        ->entityCondition('entity_type', 'test_entity_bundle_key')
 190        ->entityCondition('entity_id', '3');
 191      $this->assertEntityFieldQuery($query, array(
 192        array('test_entity_bundle_key', 3),
 193      ), t('Test entity entity_id condition.'));
 194  
 195      $query = new EntityFieldQuery();
 196      $query
 197        ->entityCondition('entity_type', 'test_entity_bundle_key')
 198        ->propertyCondition('ftid', '3');
 199      $this->assertEntityFieldQuery($query, array(
 200        array('test_entity_bundle_key', 3),
 201      ), t('Test entity entity_id condition and entity_id property condition.'));
 202  
 203      // Test bundle condition.
 204      $query = new EntityFieldQuery();
 205      $query
 206        ->entityCondition('entity_type', 'test_entity_bundle_key')
 207        ->entityCondition('bundle', 'bundle1');
 208      $this->assertEntityFieldQuery($query, array(
 209        array('test_entity_bundle_key', 1),
 210        array('test_entity_bundle_key', 2),
 211        array('test_entity_bundle_key', 3),
 212        array('test_entity_bundle_key', 4),
 213      ), t('Test entity bundle condition: bundle1.'));
 214  
 215      $query = new EntityFieldQuery();
 216      $query
 217        ->entityCondition('entity_type', 'test_entity_bundle_key')
 218        ->entityCondition('bundle', 'bundle2');
 219      $this->assertEntityFieldQuery($query, array(
 220        array('test_entity_bundle_key', 5),
 221        array('test_entity_bundle_key', 6),
 222      ), t('Test entity bundle condition: bundle2.'));
 223  
 224      $query = new EntityFieldQuery();
 225      $query
 226        ->entityCondition('entity_type', 'test_entity_bundle_key')
 227        ->propertyCondition('fttype', 'bundle2');
 228      $this->assertEntityFieldQuery($query, array(
 229        array('test_entity_bundle_key', 5),
 230        array('test_entity_bundle_key', 6),
 231      ), t('Test entity bundle condition and bundle property condition.'));
 232  
 233      // Test revision_id condition.
 234      $query = new EntityFieldQuery();
 235      $query
 236        ->entityCondition('entity_type', 'test_entity')
 237        ->entityCondition('revision_id', '3');
 238      $this->assertEntityFieldQuery($query, array(
 239        array('test_entity', 3),
 240      ), t('Test entity revision_id condition.'));
 241  
 242      $query = new EntityFieldQuery();
 243      $query
 244        ->entityCondition('entity_type', 'test_entity')
 245        ->propertyCondition('ftvid', '3');
 246      $this->assertEntityFieldQuery($query, array(
 247        array('test_entity', 3),
 248      ), t('Test entity revision_id condition and revision_id property condition.'));
 249  
 250      $query = new EntityFieldQuery();
 251      $query
 252        ->entityCondition('entity_type', 'test_entity')
 253        ->fieldCondition($this->fields[0], 'value', 100, '>=')
 254        ->age(FIELD_LOAD_REVISION);
 255      $this->assertEntityFieldQuery($query, array(
 256          array('test_entity', 100),
 257          array('test_entity', 101),
 258      ), t('Test revision age.'));
 259  
 260      // Test that fields attached to the non-revision supporting entity
 261      // 'test_entity_bundle_key' are reachable in FIELD_LOAD_REVISION.
 262      $query = new EntityFieldQuery();
 263      $query
 264        ->fieldCondition($this->fields[0], 'value', 100, '<')
 265        ->age(FIELD_LOAD_REVISION);
 266      $this->assertEntityFieldQuery($query, array(
 267          array('test_entity_bundle_key', 1),
 268          array('test_entity_bundle_key', 2),
 269          array('test_entity_bundle_key', 3),
 270          array('test_entity_bundle_key', 4),
 271          array('test_entity_bundle_key', 5),
 272          array('test_entity_bundle_key', 6),
 273          array('test_entity', 1),
 274          array('test_entity', 2),
 275          array('test_entity', 3),
 276          array('test_entity', 4),
 277      ), t('Test that fields are reachable from FIELD_LOAD_REVISION even for non-revision entities.'));
 278  
 279      // Test entity sort by entity_id.
 280      $query = new EntityFieldQuery();
 281      $query
 282        ->entityCondition('entity_type', 'test_entity_bundle_key')
 283        ->entityOrderBy('entity_id', 'ASC');
 284      $this->assertEntityFieldQuery($query, array(
 285        array('test_entity_bundle_key', 1),
 286        array('test_entity_bundle_key', 2),
 287        array('test_entity_bundle_key', 3),
 288        array('test_entity_bundle_key', 4),
 289        array('test_entity_bundle_key', 5),
 290        array('test_entity_bundle_key', 6),
 291      ), t('Test sort entity entity_id in ascending order.'), TRUE);
 292  
 293      $query = new EntityFieldQuery();
 294      $query
 295        ->entityCondition('entity_type', 'test_entity_bundle_key')
 296        ->entityOrderBy('entity_id', 'DESC');
 297      $this->assertEntityFieldQuery($query, array(
 298        array('test_entity_bundle_key', 6),
 299        array('test_entity_bundle_key', 5),
 300        array('test_entity_bundle_key', 4),
 301        array('test_entity_bundle_key', 3),
 302        array('test_entity_bundle_key', 2),
 303        array('test_entity_bundle_key', 1),
 304      ), t('Test sort entity entity_id in descending order.'), TRUE);
 305  
 306      // Test entity sort by entity_id, with a field condition.
 307      $query = new EntityFieldQuery();
 308      $query
 309        ->entityCondition('entity_type', 'test_entity_bundle_key')
 310        ->fieldCondition($this->fields[0], 'value', 0, '>')
 311        ->entityOrderBy('entity_id', 'ASC');
 312      $this->assertEntityFieldQuery($query, array(
 313        array('test_entity_bundle_key', 1),
 314        array('test_entity_bundle_key', 2),
 315        array('test_entity_bundle_key', 3),
 316        array('test_entity_bundle_key', 4),
 317        array('test_entity_bundle_key', 5),
 318        array('test_entity_bundle_key', 6),
 319      ), t('Test sort entity entity_id in ascending order, with a field condition.'), TRUE);
 320  
 321      $query = new EntityFieldQuery();
 322      $query
 323        ->entityCondition('entity_type', 'test_entity_bundle_key')
 324        ->fieldCondition($this->fields[0], 'value', 0, '>')
 325        ->propertyOrderBy('ftid', 'DESC');
 326      $this->assertEntityFieldQuery($query, array(
 327        array('test_entity_bundle_key', 6),
 328        array('test_entity_bundle_key', 5),
 329        array('test_entity_bundle_key', 4),
 330        array('test_entity_bundle_key', 3),
 331        array('test_entity_bundle_key', 2),
 332        array('test_entity_bundle_key', 1),
 333      ), t('Test sort entity entity_id property in descending order, with a field condition.'), TRUE);
 334  
 335      // Test property sort by entity id.
 336      $query = new EntityFieldQuery();
 337      $query
 338        ->entityCondition('entity_type', 'test_entity_bundle_key')
 339        ->propertyOrderBy('ftid', 'ASC');
 340      $this->assertEntityFieldQuery($query, array(
 341        array('test_entity_bundle_key', 1),
 342        array('test_entity_bundle_key', 2),
 343        array('test_entity_bundle_key', 3),
 344        array('test_entity_bundle_key', 4),
 345        array('test_entity_bundle_key', 5),
 346        array('test_entity_bundle_key', 6),
 347      ), t('Test sort entity entity_id property in ascending order.'), TRUE);
 348  
 349      $query = new EntityFieldQuery();
 350      $query
 351        ->entityCondition('entity_type', 'test_entity_bundle_key')
 352        ->propertyOrderBy('ftid', 'DESC');
 353      $this->assertEntityFieldQuery($query, array(
 354        array('test_entity_bundle_key', 6),
 355        array('test_entity_bundle_key', 5),
 356        array('test_entity_bundle_key', 4),
 357        array('test_entity_bundle_key', 3),
 358        array('test_entity_bundle_key', 2),
 359        array('test_entity_bundle_key', 1),
 360      ), t('Test sort entity entity_id property in descending order.'), TRUE);
 361  
 362      // Test property sort by entity id, with a field condition.
 363      $query = new EntityFieldQuery();
 364      $query
 365        ->entityCondition('entity_type', 'test_entity_bundle_key')
 366        ->fieldCondition($this->fields[0], 'value', 0, '>')
 367        ->propertyOrderBy('ftid', 'ASC');
 368      $this->assertEntityFieldQuery($query, array(
 369        array('test_entity_bundle_key', 1),
 370        array('test_entity_bundle_key', 2),
 371        array('test_entity_bundle_key', 3),
 372        array('test_entity_bundle_key', 4),
 373        array('test_entity_bundle_key', 5),
 374        array('test_entity_bundle_key', 6),
 375      ), t('Test sort entity entity_id property in ascending order, with a field condition.'), TRUE);
 376  
 377      $query = new EntityFieldQuery();
 378      $query
 379        ->entityCondition('entity_type', 'test_entity_bundle_key')
 380        ->fieldCondition($this->fields[0], 'value', 0, '>')
 381        ->propertyOrderBy('ftid', 'DESC');
 382      $this->assertEntityFieldQuery($query, array(
 383        array('test_entity_bundle_key', 6),
 384        array('test_entity_bundle_key', 5),
 385        array('test_entity_bundle_key', 4),
 386        array('test_entity_bundle_key', 3),
 387        array('test_entity_bundle_key', 2),
 388        array('test_entity_bundle_key', 1),
 389      ), t('Test sort entity entity_id property in descending order, with a field condition.'), TRUE);
 390  
 391      // Test entity sort by bundle.
 392      $query = new EntityFieldQuery();
 393      $query
 394        ->entityCondition('entity_type', 'test_entity_bundle_key')
 395        ->entityOrderBy('bundle', 'ASC')
 396        ->propertyOrderBy('ftid', 'DESC');
 397      $this->assertEntityFieldQuery($query, array(
 398        array('test_entity_bundle_key', 4),
 399        array('test_entity_bundle_key', 3),
 400        array('test_entity_bundle_key', 2),
 401        array('test_entity_bundle_key', 1),
 402        array('test_entity_bundle_key', 6),
 403        array('test_entity_bundle_key', 5),
 404      ), t('Test sort entity bundle in ascending order, property in descending order.'), TRUE);
 405  
 406      $query = new EntityFieldQuery();
 407      $query
 408        ->entityCondition('entity_type', 'test_entity_bundle_key')
 409        ->entityOrderBy('bundle', 'DESC')
 410        ->propertyOrderBy('ftid', 'ASC');
 411      $this->assertEntityFieldQuery($query, array(
 412        array('test_entity_bundle_key', 5),
 413        array('test_entity_bundle_key', 6),
 414        array('test_entity_bundle_key', 1),
 415        array('test_entity_bundle_key', 2),
 416        array('test_entity_bundle_key', 3),
 417        array('test_entity_bundle_key', 4),
 418      ), t('Test sort entity bundle in descending order, property in ascending order.'), TRUE);
 419  
 420      // Test entity sort by bundle, with a field condition.
 421      $query = new EntityFieldQuery();
 422      $query
 423        ->entityCondition('entity_type', 'test_entity_bundle_key')
 424        ->fieldCondition($this->fields[0], 'value', 0, '>')
 425        ->entityOrderBy('bundle', 'ASC')
 426        ->propertyOrderBy('ftid', 'DESC');
 427      $this->assertEntityFieldQuery($query, array(
 428        array('test_entity_bundle_key', 4),
 429        array('test_entity_bundle_key', 3),
 430        array('test_entity_bundle_key', 2),
 431        array('test_entity_bundle_key', 1),
 432        array('test_entity_bundle_key', 6),
 433        array('test_entity_bundle_key', 5),
 434      ), t('Test sort entity bundle in ascending order, property in descending order, with a field condition.'), TRUE);
 435  
 436      $query = new EntityFieldQuery();
 437      $query
 438        ->entityCondition('entity_type', 'test_entity_bundle_key')
 439        ->fieldCondition($this->fields[0], 'value', 0, '>')
 440        ->entityOrderBy('bundle', 'DESC')
 441        ->propertyOrderBy('ftid', 'ASC');
 442      $this->assertEntityFieldQuery($query, array(
 443        array('test_entity_bundle_key', 5),
 444        array('test_entity_bundle_key', 6),
 445        array('test_entity_bundle_key', 1),
 446        array('test_entity_bundle_key', 2),
 447        array('test_entity_bundle_key', 3),
 448        array('test_entity_bundle_key', 4),
 449      ), t('Test sort entity bundle in descending order, property in ascending order, with a field condition.'), TRUE);
 450  
 451      // Test entity sort by bundle, field.
 452      $query = new EntityFieldQuery();
 453      $query
 454        ->entityCondition('entity_type', 'test_entity_bundle_key')
 455        ->entityOrderBy('bundle', 'ASC')
 456        ->fieldOrderBy($this->fields[0], 'value', 'DESC');
 457      $this->assertEntityFieldQuery($query, array(
 458        array('test_entity_bundle_key', 4),
 459        array('test_entity_bundle_key', 3),
 460        array('test_entity_bundle_key', 2),
 461        array('test_entity_bundle_key', 1),
 462        array('test_entity_bundle_key', 6),
 463        array('test_entity_bundle_key', 5),
 464      ), t('Test sort entity bundle in ascending order, field in descending order.'), TRUE);
 465  
 466      $query = new EntityFieldQuery();
 467      $query
 468        ->entityCondition('entity_type', 'test_entity_bundle_key')
 469        ->entityOrderBy('bundle', 'DESC')
 470        ->fieldOrderBy($this->fields[0], 'value', 'ASC');
 471      $this->assertEntityFieldQuery($query, array(
 472        array('test_entity_bundle_key', 5),
 473        array('test_entity_bundle_key', 6),
 474        array('test_entity_bundle_key', 1),
 475        array('test_entity_bundle_key', 2),
 476        array('test_entity_bundle_key', 3),
 477        array('test_entity_bundle_key', 4),
 478      ), t('Test sort entity bundle in descending order, field in ascending order.'), TRUE);
 479  
 480      // Test entity sort by revision_id.
 481      $query = new EntityFieldQuery();
 482      $query
 483        ->entityCondition('entity_type', 'test_entity')
 484        ->entityOrderBy('revision_id', 'ASC');
 485      $this->assertEntityFieldQuery($query, array(
 486        array('test_entity', 1),
 487        array('test_entity', 2),
 488        array('test_entity', 3),
 489        array('test_entity', 4),
 490      ), t('Test sort entity revision_id in ascending order.'), TRUE);
 491  
 492      $query = new EntityFieldQuery();
 493      $query
 494        ->entityCondition('entity_type', 'test_entity')
 495        ->entityOrderBy('revision_id', 'DESC');
 496      $this->assertEntityFieldQuery($query, array(
 497        array('test_entity', 4),
 498        array('test_entity', 3),
 499        array('test_entity', 2),
 500        array('test_entity', 1),
 501      ), t('Test sort entity revision_id in descending order.'), TRUE);
 502  
 503      // Test entity sort by revision_id, with a field condition.
 504      $query = new EntityFieldQuery();
 505      $query
 506        ->entityCondition('entity_type', 'test_entity')
 507        ->fieldCondition($this->fields[0], 'value', 0, '>')
 508        ->entityOrderBy('revision_id', 'ASC');
 509      $this->assertEntityFieldQuery($query, array(
 510        array('test_entity', 1),
 511        array('test_entity', 2),
 512        array('test_entity', 3),
 513        array('test_entity', 4),
 514      ), t('Test sort entity revision_id in ascending order, with a field condition.'), TRUE);
 515  
 516      $query = new EntityFieldQuery();
 517      $query
 518        ->entityCondition('entity_type', 'test_entity')
 519        ->fieldCondition($this->fields[0], 'value', 0, '>')
 520        ->entityOrderBy('revision_id', 'DESC');
 521      $this->assertEntityFieldQuery($query, array(
 522        array('test_entity', 4),
 523        array('test_entity', 3),
 524        array('test_entity', 2),
 525        array('test_entity', 1),
 526      ), t('Test sort entity revision_id in descending order, with a field condition.'), TRUE);
 527  
 528      // Test property sort by revision_id.
 529      $query = new EntityFieldQuery();
 530      $query
 531        ->entityCondition('entity_type', 'test_entity')
 532        ->propertyOrderBy('ftvid', 'ASC');
 533      $this->assertEntityFieldQuery($query, array(
 534        array('test_entity', 1),
 535        array('test_entity', 2),
 536        array('test_entity', 3),
 537        array('test_entity', 4),
 538      ), t('Test sort entity revision_id property in ascending order.'), TRUE);
 539  
 540      $query = new EntityFieldQuery();
 541      $query
 542        ->entityCondition('entity_type', 'test_entity')
 543        ->propertyOrderBy('ftvid', 'DESC');
 544      $this->assertEntityFieldQuery($query, array(
 545        array('test_entity', 4),
 546        array('test_entity', 3),
 547        array('test_entity', 2),
 548        array('test_entity', 1),
 549      ), t('Test sort entity revision_id property in descending order.'), TRUE);
 550  
 551      // Test property sort by revision_id, with a field condition.
 552      $query = new EntityFieldQuery();
 553      $query
 554        ->entityCondition('entity_type', 'test_entity')
 555        ->fieldCondition($this->fields[0], 'value', 0, '>')
 556        ->propertyOrderBy('ftvid', 'ASC');
 557      $this->assertEntityFieldQuery($query, array(
 558        array('test_entity', 1),
 559        array('test_entity', 2),
 560        array('test_entity', 3),
 561        array('test_entity', 4),
 562      ), t('Test sort entity revision_id property in ascending order, with a field condition.'), TRUE);
 563  
 564      $query = new EntityFieldQuery();
 565      $query
 566        ->entityCondition('entity_type', 'test_entity')
 567        ->fieldCondition($this->fields[0], 'value', 0, '>')
 568        ->propertyOrderBy('ftvid', 'DESC');
 569      $this->assertEntityFieldQuery($query, array(
 570        array('test_entity', 4),
 571        array('test_entity', 3),
 572        array('test_entity', 2),
 573        array('test_entity', 1),
 574      ), t('Test sort entity revision_id property in descending order, with a field condition.'), TRUE);
 575  
 576      $query = new EntityFieldQuery();
 577      $query
 578        ->entityCondition('entity_type', 'test_entity_bundle_key')
 579        ->fieldOrderBy($this->fields[0], 'value', 'ASC');
 580      $this->assertEntityFieldQuery($query, array(
 581        array('test_entity_bundle_key', 1),
 582        array('test_entity_bundle_key', 2),
 583        array('test_entity_bundle_key', 3),
 584        array('test_entity_bundle_key', 4),
 585        array('test_entity_bundle_key', 5),
 586        array('test_entity_bundle_key', 6),
 587      ), t('Test sort field in ascending order without field condition.'), TRUE);
 588  
 589      $query = new EntityFieldQuery();
 590      $query
 591        ->entityCondition('entity_type', 'test_entity_bundle_key')
 592        ->fieldOrderBy($this->fields[0], 'value', 'DESC');
 593      $this->assertEntityFieldQuery($query, array(
 594        array('test_entity_bundle_key', 6),
 595        array('test_entity_bundle_key', 5),
 596        array('test_entity_bundle_key', 4),
 597        array('test_entity_bundle_key', 3),
 598        array('test_entity_bundle_key', 2),
 599        array('test_entity_bundle_key', 1),
 600      ), t('Test sort field in descending order without field condition.'), TRUE);
 601  
 602      $query = new EntityFieldQuery();
 603      $query
 604        ->entityCondition('entity_type', 'test_entity_bundle_key')
 605        ->fieldCondition($this->fields[0], 'value', 0, '>')
 606        ->fieldOrderBy($this->fields[0], 'value', 'ASC');
 607      $this->assertEntityFieldQuery($query, array(
 608        array('test_entity_bundle_key', 1),
 609        array('test_entity_bundle_key', 2),
 610        array('test_entity_bundle_key', 3),
 611        array('test_entity_bundle_key', 4),
 612        array('test_entity_bundle_key', 5),
 613        array('test_entity_bundle_key', 6),
 614      ), t('Test sort field in ascending order.'), TRUE);
 615  
 616      $query = new EntityFieldQuery();
 617      $query
 618        ->entityCondition('entity_type', 'test_entity_bundle_key')
 619        ->fieldCondition($this->fields[0], 'value', 0, '>')
 620        ->fieldOrderBy($this->fields[0], 'value', 'DESC');
 621      $this->assertEntityFieldQuery($query, array(
 622        array('test_entity_bundle_key', 6),
 623        array('test_entity_bundle_key', 5),
 624        array('test_entity_bundle_key', 4),
 625        array('test_entity_bundle_key', 3),
 626        array('test_entity_bundle_key', 2),
 627        array('test_entity_bundle_key', 1),
 628      ), t('Test sort field in descending order.'), TRUE);
 629  
 630      // Test "in" operation with entity entity_type condition and entity_id
 631      // property condition.
 632      $query = new EntityFieldQuery();
 633      $query
 634        ->entityCondition('entity_type', 'test_entity_bundle_key')
 635        ->propertyCondition('ftid', array(1, 3, 4), 'IN');
 636      $this->assertEntityFieldQuery($query, array(
 637        array('test_entity_bundle_key', 1),
 638        array('test_entity_bundle_key', 3),
 639        array('test_entity_bundle_key', 4),
 640      ), t('Test "in" operation with entity entity_type condition and entity_id property condition.'));
 641  
 642      // Test "in" operation with entity entity_type condition and entity_id
 643      // property condition. Sort in descending order by entity_id.
 644      $query = new EntityFieldQuery();
 645      $query
 646        ->entityCondition('entity_type', 'test_entity_bundle_key')
 647        ->propertyCondition('ftid', array(1, 3, 4), 'IN')
 648        ->propertyOrderBy('ftid', 'DESC');
 649      $this->assertEntityFieldQuery($query, array(
 650        array('test_entity_bundle_key', 4),
 651        array('test_entity_bundle_key', 3),
 652        array('test_entity_bundle_key', 1),
 653      ), t('Test "in" operation with entity entity_type condition and entity_id property condition. Sort entity_id in descending order.'), TRUE);
 654  
 655      // Test query count
 656      $query = new EntityFieldQuery();
 657      $query_count = $query
 658        ->entityCondition('entity_type', 'test_entity_bundle_key')
 659        ->count()
 660        ->execute();
 661      $this->assertEqual($query_count, 6, t('Test query count on entity condition.'));
 662  
 663      $query = new EntityFieldQuery();
 664      $query_count = $query
 665        ->entityCondition('entity_type', 'test_entity_bundle_key')
 666        ->propertyCondition('ftid', '1')
 667        ->count()
 668        ->execute();
 669      $this->assertEqual($query_count, 1, t('Test query count on entity and property condition.'));
 670  
 671      $query = new EntityFieldQuery();
 672      $query_count = $query
 673        ->entityCondition('entity_type', 'test_entity_bundle_key')
 674        ->propertyCondition('ftid', '4', '>')
 675        ->count()
 676        ->execute();
 677      $this->assertEqual($query_count, 2, t('Test query count on entity and property condition with operator.'));
 678  
 679      $query = new EntityFieldQuery();
 680      $query_count = $query
 681        ->entityCondition('entity_type', 'test_entity_bundle_key')
 682        ->fieldCondition($this->fields[0], 'value', 3, '=')
 683        ->count()
 684        ->execute();
 685      $this->assertEqual($query_count, 1, t('Test query count on field condition.'));
 686  
 687      // First, test without options.
 688      $query = new EntityFieldQuery();
 689      $query
 690        ->entityCondition('entity_type', 'test_entity_bundle_key')
 691        ->propertyCondition('fttype', 'und', 'CONTAINS');
 692      $this->assertEntityFieldQuery($query, array(
 693        array('test_entity_bundle_key', 1),
 694        array('test_entity_bundle_key', 2),
 695        array('test_entity_bundle_key', 3),
 696        array('test_entity_bundle_key', 4),
 697        array('test_entity_bundle_key', 5),
 698        array('test_entity_bundle_key', 6),
 699      ), t('Test the "contains" operation on a property.'));
 700  
 701      $query = new EntityFieldQuery();
 702      $query->fieldCondition($this->fields[1], 'shape', 'uar', 'CONTAINS');
 703      $this->assertEntityFieldQuery($query, array(
 704        array('test_entity_bundle', 5),
 705      ), t('Test the "contains" operation on a field.'));
 706  
 707      $query = new EntityFieldQuery();
 708      $query
 709        ->entityCondition('entity_type', 'test_entity_bundle_key')
 710        ->propertyCondition('ftid', 1, '=');
 711      $this->assertEntityFieldQuery($query, array(
 712        array('test_entity_bundle_key', 1),
 713      ), t('Test the "equal to" operation on a property.'));
 714  
 715      $query = new EntityFieldQuery();
 716      $query->fieldCondition($this->fields[0], 'value', 3, '=');
 717      $this->assertEntityFieldQuery($query, array(
 718        array('test_entity_bundle_key', 3),
 719        array('test_entity', 3),
 720      ), t('Test the "equal to" operation on a field.'));
 721  
 722      $query = new EntityFieldQuery();
 723      $query
 724        ->entityCondition('entity_type', 'test_entity_bundle_key')
 725        ->propertyCondition('ftid', 3, '<>');
 726      $this->assertEntityFieldQuery($query, array(
 727        array('test_entity_bundle_key', 1),
 728        array('test_entity_bundle_key', 2),
 729        array('test_entity_bundle_key', 4),
 730        array('test_entity_bundle_key', 5),
 731        array('test_entity_bundle_key', 6),
 732      ), t('Test the "not equal to" operation on a property.'));
 733  
 734      $query = new EntityFieldQuery();
 735      $query->fieldCondition($this->fields[0], 'value', 3, '<>');
 736      $this->assertEntityFieldQuery($query, array(
 737        array('test_entity_bundle_key', 1),
 738        array('test_entity_bundle_key', 2),
 739        array('test_entity_bundle_key', 4),
 740        array('test_entity_bundle_key', 5),
 741        array('test_entity_bundle_key', 6),
 742        array('test_entity', 1),
 743        array('test_entity', 2),
 744        array('test_entity', 4),
 745      ), t('Test the "not equal to" operation on a field.'));
 746  
 747      $query = new EntityFieldQuery();
 748      $query
 749        ->entityCondition('entity_type', 'test_entity_bundle_key')
 750        ->propertyCondition('ftid', 3, '!=');
 751      $this->assertEntityFieldQuery($query, array(
 752        array('test_entity_bundle_key', 1),
 753        array('test_entity_bundle_key', 2),
 754        array('test_entity_bundle_key', 4),
 755        array('test_entity_bundle_key', 5),
 756        array('test_entity_bundle_key', 6),
 757      ), t('Test the "not equal to" operation on a property.'));
 758  
 759      $query = new EntityFieldQuery();
 760      $query->fieldCondition($this->fields[0], 'value', 3, '!=');
 761      $this->assertEntityFieldQuery($query, array(
 762        array('test_entity_bundle_key', 1),
 763        array('test_entity_bundle_key', 2),
 764        array('test_entity_bundle_key', 4),
 765        array('test_entity_bundle_key', 5),
 766        array('test_entity_bundle_key', 6),
 767        array('test_entity', 1),
 768        array('test_entity', 2),
 769        array('test_entity', 4),
 770      ), t('Test the "not equal to" operation on a field.'));
 771  
 772      $query = new EntityFieldQuery();
 773      $query
 774        ->entityCondition('entity_type', 'test_entity_bundle_key')
 775        ->propertyCondition('ftid', 2, '<');
 776      $this->assertEntityFieldQuery($query, array(
 777        array('test_entity_bundle_key', 1),
 778      ), t('Test the "less than" operation on a property.'));
 779  
 780      $query = new EntityFieldQuery();
 781      $query->fieldCondition($this->fields[0], 'value', 2, '<');
 782      $this->assertEntityFieldQuery($query, array(
 783        array('test_entity_bundle_key', 1),
 784        array('test_entity', 1),
 785      ), t('Test the "less than" operation on a field.'));
 786  
 787      $query = new EntityFieldQuery();
 788      $query
 789        ->entityCondition('entity_type', 'test_entity_bundle_key')
 790        ->propertyCondition('ftid', 2, '<=');
 791      $this->assertEntityFieldQuery($query, array(
 792        array('test_entity_bundle_key', 1),
 793        array('test_entity_bundle_key', 2),
 794      ), t('Test the "less than or equal to" operation on a property.'));
 795  
 796      $query = new EntityFieldQuery();
 797      $query->fieldCondition($this->fields[0], 'value', 2, '<=');
 798      $this->assertEntityFieldQuery($query, array(
 799        array('test_entity_bundle_key', 1),
 800        array('test_entity_bundle_key', 2),
 801        array('test_entity', 1),
 802        array('test_entity', 2),
 803      ), t('Test the "less than or equal to" operation on a field.'));
 804  
 805      $query = new EntityFieldQuery();
 806      $query
 807        ->entityCondition('entity_type', 'test_entity_bundle_key')
 808        ->propertyCondition('ftid', 4, '>');
 809      $this->assertEntityFieldQuery($query, array(
 810        array('test_entity_bundle_key', 5),
 811        array('test_entity_bundle_key', 6),
 812      ), t('Test the "greater than" operation on a property.'));
 813  
 814      $query = new EntityFieldQuery();
 815      $query->fieldCondition($this->fields[0], 'value', 2, '>');
 816      $this->assertEntityFieldQuery($query, array(
 817        array('test_entity_bundle_key', 3),
 818        array('test_entity_bundle_key', 4),
 819        array('test_entity_bundle_key', 5),
 820        array('test_entity_bundle_key', 6),
 821        array('test_entity', 3),
 822        array('test_entity', 4),
 823      ), t('Test the "greater than" operation on a field.'));
 824  
 825      $query = new EntityFieldQuery();
 826      $query
 827        ->entityCondition('entity_type', 'test_entity_bundle_key')
 828        ->propertyCondition('ftid', 4, '>=');
 829      $this->assertEntityFieldQuery($query, array(
 830        array('test_entity_bundle_key', 4),
 831        array('test_entity_bundle_key', 5),
 832        array('test_entity_bundle_key', 6),
 833      ), t('Test the "greater than or equal to" operation on a property.'));
 834  
 835      $query = new EntityFieldQuery();
 836      $query->fieldCondition($this->fields[0], 'value', 3, '>=');
 837      $this->assertEntityFieldQuery($query, array(
 838        array('test_entity_bundle_key', 3),
 839        array('test_entity_bundle_key', 4),
 840        array('test_entity_bundle_key', 5),
 841        array('test_entity_bundle_key', 6),
 842        array('test_entity', 3),
 843        array('test_entity', 4),
 844      ), t('Test the "greater than or equal to" operation on a field.'));
 845  
 846      $query = new EntityFieldQuery();
 847      $query
 848        ->entityCondition('entity_type', 'test_entity_bundle_key')
 849        ->propertyCondition('ftid', array(3, 4), 'NOT IN');
 850      $this->assertEntityFieldQuery($query, array(
 851        array('test_entity_bundle_key', 1),
 852        array('test_entity_bundle_key', 2),
 853        array('test_entity_bundle_key', 5),
 854        array('test_entity_bundle_key', 6),
 855      ), t('Test the "not in" operation on a property.'));
 856  
 857      $query = new EntityFieldQuery();
 858      $query->fieldCondition($this->fields[0], 'value', array(3, 4, 100, 101), 'NOT IN');
 859      $this->assertEntityFieldQuery($query, array(
 860        array('test_entity_bundle_key', 1),
 861        array('test_entity_bundle_key', 2),
 862        array('test_entity_bundle_key', 5),
 863        array('test_entity_bundle_key', 6),
 864        array('test_entity', 1),
 865        array('test_entity', 2),
 866      ), t('Test the "not in" operation on a field.'));
 867  
 868      $query = new EntityFieldQuery();
 869      $query
 870        ->entityCondition('entity_type', 'test_entity_bundle_key')
 871        ->propertyCondition('ftid', array(3, 4), 'IN');
 872      $this->assertEntityFieldQuery($query, array(
 873        array('test_entity_bundle_key', 3),
 874        array('test_entity_bundle_key', 4),
 875      ), t('Test the "in" operation on a property.'));
 876  
 877      $query = new EntityFieldQuery();
 878      $query->fieldCondition($this->fields[0], 'value', array(2, 3), 'IN');
 879      $this->assertEntityFieldQuery($query, array(
 880        array('test_entity_bundle_key', 2),
 881        array('test_entity_bundle_key', 3),
 882        array('test_entity', 2),
 883        array('test_entity', 3),
 884      ), t('Test the "in" operation on a field.'));
 885  
 886      $query = new EntityFieldQuery();
 887      $query
 888        ->entityCondition('entity_type', 'test_entity_bundle_key')
 889        ->propertyCondition('ftid', array(1, 3), 'BETWEEN');
 890      $this->assertEntityFieldQuery($query, array(
 891        array('test_entity_bundle_key', 1),
 892        array('test_entity_bundle_key', 2),
 893        array('test_entity_bundle_key', 3),
 894      ), t('Test the "between" operation on a property.'));
 895  
 896      $query = new EntityFieldQuery();
 897      $query->fieldCondition($this->fields[0], 'value', array(1, 3), 'BETWEEN');
 898      $this->assertEntityFieldQuery($query, array(
 899        array('test_entity_bundle_key', 1),
 900        array('test_entity_bundle_key', 2),
 901        array('test_entity_bundle_key', 3),
 902        array('test_entity', 1),
 903        array('test_entity', 2),
 904        array('test_entity', 3),
 905      ), t('Test the "between" operation on a field.'));
 906  
 907      $query = new EntityFieldQuery();
 908      $query
 909        ->entityCondition('entity_type', 'test_entity_bundle_key')
 910        ->propertyCondition('fttype', 'bun', 'STARTS_WITH');
 911      $this->assertEntityFieldQuery($query, array(
 912        array('test_entity_bundle_key', 1),
 913        array('test_entity_bundle_key', 2),
 914        array('test_entity_bundle_key', 3),
 915        array('test_entity_bundle_key', 4),
 916        array('test_entity_bundle_key', 5),
 917        array('test_entity_bundle_key', 6),
 918      ), t('Test the "starts_with" operation on a property.'));
 919  
 920      $query = new EntityFieldQuery();
 921      $query->fieldCondition($this->fields[1], 'shape', 'squ', 'STARTS_WITH');
 922      $this->assertEntityFieldQuery($query, array(
 923        array('test_entity_bundle', 5),
 924      ), t('Test the "starts_with" operation on a field.'));
 925  
 926      $query = new EntityFieldQuery();
 927      $query->fieldCondition($this->fields[0], 'value', 3);
 928      $this->assertEntityFieldQuery($query, array(
 929        array('test_entity_bundle_key', 3),
 930        array('test_entity', 3),
 931      ), t('Test omission of an operator with a single item.'));
 932  
 933      $query = new EntityFieldQuery();
 934      $query->fieldCondition($this->fields[0], 'value', array(2, 3));
 935      $this->assertEntityFieldQuery($query, array(
 936        array('test_entity_bundle_key', 2),
 937        array('test_entity_bundle_key', 3),
 938        array('test_entity', 2),
 939        array('test_entity', 3),
 940      ), t('Test omission of an operator with multiple items.'));
 941  
 942      $query = new EntityFieldQuery();
 943      $query
 944        ->entityCondition('entity_type', 'test_entity_bundle_key')
 945        ->propertyCondition('ftid', 1, '>')
 946        ->fieldCondition($this->fields[0], 'value', 4, '<');
 947      $this->assertEntityFieldQuery($query, array(
 948        array('test_entity_bundle_key', 2),
 949        array('test_entity_bundle_key', 3),
 950      ), t('Test entity, property and field conditions.'));
 951  
 952      $query = new EntityFieldQuery();
 953      $query
 954        ->entityCondition('entity_type', 'test_entity_bundle_key')
 955        ->entityCondition('bundle', 'bundle', 'STARTS_WITH')
 956        ->propertyCondition('ftid', 4)
 957        ->fieldCondition($this->fields[0], 'value', 4);
 958      $this->assertEntityFieldQuery($query, array(
 959        array('test_entity_bundle_key', 4),
 960      ), t('Test entity condition with "starts_with" operation, and property and field conditions.'));
 961  
 962      $query = new EntityFieldQuery();
 963      $query
 964        ->entityCondition('entity_type', 'test_entity_bundle_key')
 965        ->propertyOrderBy('ftid', 'ASC')
 966        ->range(0, 2);
 967      $this->assertEntityFieldQuery($query, array(
 968        array('test_entity_bundle_key', 1),
 969        array('test_entity_bundle_key', 2),
 970      ), t('Test limit on a property.'), TRUE);
 971  
 972      $query = new EntityFieldQuery();
 973      $query
 974        ->entityCondition('entity_type', 'test_entity_bundle_key')
 975        ->fieldCondition($this->fields[0], 'value', 0, '>=')
 976        ->fieldOrderBy($this->fields[0], 'value', 'ASC')
 977        ->range(0, 2);
 978      $this->assertEntityFieldQuery($query, array(
 979        array('test_entity_bundle_key', 1),
 980        array('test_entity_bundle_key', 2),
 981      ), t('Test limit on a field.'), TRUE);
 982  
 983      $query = new EntityFieldQuery();
 984      $query
 985        ->entityCondition('entity_type', 'test_entity_bundle_key')
 986        ->propertyOrderBy('ftid', 'ASC')
 987        ->range(4, 6);
 988      $this->assertEntityFieldQuery($query, array(
 989        array('test_entity_bundle_key', 5),
 990        array('test_entity_bundle_key', 6),
 991      ), t('Test offset on a property.'), TRUE);
 992  
 993      $query = new EntityFieldQuery();
 994      $query
 995        ->entityCondition('entity_type', 'test_entity_bundle_key')
 996        ->fieldCondition($this->fields[0], 'value', 0, '>')
 997        ->fieldOrderBy($this->fields[0], 'value', 'ASC')
 998        ->range(2, 4);
 999      $this->assertEntityFieldQuery($query, array(
1000        array('test_entity_bundle_key', 3),
1001        array('test_entity_bundle_key', 4),
1002        array('test_entity_bundle_key', 5),
1003        array('test_entity_bundle_key', 6),
1004      ), t('Test offset on a field.'), TRUE);
1005  
1006      for ($i = 6; $i < 10; $i++) {
1007        $entity = new stdClass();
1008        $entity->ftid = $i;
1009        $entity->fttype = 'test_entity_bundle';
1010        $entity->{$this->field_names[0]}[LANGUAGE_NONE][0]['value'] = $i - 5;
1011        drupal_write_record('test_entity_bundle', $entity);
1012        field_attach_insert('test_entity_bundle', $entity);
1013      }
1014  
1015      $query = new EntityFieldQuery();
1016      $query->fieldCondition($this->fields[0], 'value', 2, '>');
1017      $this->assertEntityFieldQuery($query, array(
1018        array('test_entity_bundle_key', 3),
1019        array('test_entity_bundle_key', 4),
1020        array('test_entity_bundle_key', 5),
1021        array('test_entity_bundle_key', 6),
1022        array('test_entity', 3),
1023        array('test_entity', 4),
1024        array('test_entity_bundle', 8),
1025        array('test_entity_bundle', 9),
1026      ), t('Select a field across multiple entities.'));
1027  
1028      $query = new EntityFieldQuery();
1029      $query
1030        ->fieldCondition($this->fields[1], 'shape', 'square')
1031        ->fieldCondition($this->fields[1], 'color', 'blue');
1032      $this->assertEntityFieldQuery($query, array(
1033        array('test_entity_bundle', 5),
1034      ), t('Test without a delta group.'));
1035  
1036      $query = new EntityFieldQuery();
1037      $query
1038        ->fieldCondition($this->fields[1], 'shape', 'square', '=', 'group')
1039        ->fieldCondition($this->fields[1], 'color', 'blue', '=', 'group');
1040      $this->assertEntityFieldQuery($query, array(), t('Test with a delta group.'));
1041  
1042      // Test query on a deleted field.
1043      field_attach_delete_bundle('test_entity_bundle_key', 'bundle1');
1044      field_attach_delete_bundle('test_entity', 'test_bundle');
1045      $query = new EntityFieldQuery();
1046      $query->fieldCondition($this->fields[0], 'value', '3');
1047      $this->assertEntityFieldQuery($query, array(
1048        array('test_entity_bundle', 8),
1049      ), t('Test query on a field after deleting field from some entities.'));
1050  
1051      field_attach_delete_bundle('test_entity_bundle', 'test_entity_bundle');
1052      $query = new EntityFieldQuery();
1053      $query->fieldCondition($this->fields[0], 'value', '3');
1054      $this->assertEntityFieldQuery($query, array(), t('Test query on a field after deleting field from all entities.'));
1055  
1056      $query = new EntityFieldQuery();
1057      $query
1058        ->fieldCondition($this->fields[0], 'value', '3')
1059        ->deleted(TRUE);
1060      $this->assertEntityFieldQuery($query, array(
1061        array('test_entity_bundle_key', 3),
1062        array('test_entity_bundle', 8),
1063        array('test_entity', 3),
1064      ), t('Test query on a deleted field with deleted option set to TRUE.'));
1065  
1066      $pass = FALSE;
1067      $query = new EntityFieldQuery();
1068      try {
1069        $query->execute();
1070      }
1071      catch (EntityFieldQueryException $exception) {
1072        $pass = ($exception->getMessage() == t('For this query an entity type must be specified.'));
1073      }
1074      $this->assertTrue($pass, t("Can't query the universe."));
1075    }
1076  
1077    /**
1078     * Tests querying translatable fields.
1079     */
1080    function testEntityFieldQueryTranslatable() {
1081  
1082      // Make a test field translatable AND cardinality one.
1083      $this->fields[0]['translatable'] = TRUE;
1084      $this->fields[0]['cardinality'] = 1;
1085      field_update_field($this->fields[0]);
1086      field_test_entity_info_translatable('test_entity', TRUE);
1087  
1088      // Create more items with different languages.
1089      $entity = new stdClass();
1090      $entity->ftid = 1;
1091      $entity->ftvid = 1;
1092      $entity->fttype = 'test_bundle';
1093  
1094      // Set fields in two languages with one field value.
1095      foreach (array(LANGUAGE_NONE, 'en') as $langcode) {
1096        $entity->{$this->field_names[0]}[$langcode][0]['value'] = 1234;
1097      }
1098  
1099      field_attach_update('test_entity', $entity);
1100  
1101      // Look up number of results when querying a single entity with multilingual
1102      // field values.
1103      $query = new EntityFieldQuery();
1104      $query_count = $query
1105        ->entityCondition('entity_type', 'test_entity')
1106        ->entityCondition('bundle', 'test_bundle')
1107        ->entityCondition('entity_id', '1')
1108        ->fieldCondition($this->fields[0])
1109        ->count()
1110        ->execute();
1111  
1112      $this->assertEqual($query_count, 1, t("Count on translatable cardinality one field is correct."));
1113    }
1114  
1115    /**
1116     * Tests field meta conditions.
1117     */
1118    function testEntityFieldQueryMetaConditions() {
1119      // Make a test field translatable.
1120      $this->fields[0]['translatable'] = TRUE;
1121      field_update_field($this->fields[0]);
1122      field_test_entity_info_translatable('test_entity', TRUE);
1123  
1124      // Create more items with different languages.
1125      $entity = new stdClass();
1126      $entity->ftid = 1;
1127      $entity->ftvid = 1;
1128      $entity->fttype = 'test_bundle';
1129      $j = 0;
1130  
1131      foreach (array(LANGUAGE_NONE, 'en') as $langcode) {
1132        for ($i = 0; $i < 4; $i++) {
1133          $entity->{$this->field_names[0]}[$langcode][$i]['value'] = $i + $j;
1134        }
1135        $j += 4;
1136      }
1137  
1138      field_attach_update('test_entity', $entity);
1139  
1140      // Test delta field meta condition.
1141      $query = new EntityFieldQuery();
1142      $query
1143        ->entityCondition('entity_type', 'test_entity', '=')
1144        ->fieldDeltaCondition($this->fields[0], 0, '>');
1145      $this->assertEntityFieldQuery($query, array(
1146        array('test_entity', 1),
1147      ), t('Test with a delta meta condition.'));
1148  
1149      // Test language field meta condition.
1150      $query = new EntityFieldQuery();
1151      $query
1152        ->entityCondition('entity_type', 'test_entity', '=')
1153        ->fieldLanguageCondition($this->fields[0], LANGUAGE_NONE, '<>');
1154      $this->assertEntityFieldQuery($query, array(
1155        array('test_entity', 1),
1156      ), t('Test with a language meta condition.'));
1157  
1158      // Test language field meta condition.
1159      $query = new EntityFieldQuery();
1160      $query
1161        ->entityCondition('entity_type', 'test_entity', '=')
1162        ->fieldLanguageCondition($this->fields[0], LANGUAGE_NONE, '!=');
1163      $this->assertEntityFieldQuery($query, array(
1164        array('test_entity', 1),
1165      ), t('Test with a language meta condition.'));
1166  
1167      // Test delta grouping.
1168      $query = new EntityFieldQuery();
1169      $query
1170        ->entityCondition('entity_type', 'test_entity', '=')
1171        ->fieldCondition($this->fields[0], 'value', 0, '=', 'group')
1172        ->fieldDeltaCondition($this->fields[0], 1, '<', 'group');
1173      $this->assertEntityFieldQuery($query, array(
1174        array('test_entity', 1),
1175      ), t('Test with a grouped delta meta condition.'));
1176  
1177      $query = new EntityFieldQuery();
1178      $query
1179        ->entityCondition('entity_type', 'test_entity', '=')
1180        ->fieldCondition($this->fields[0], 'value', 0, '=', 'group')
1181        ->fieldDeltaCondition($this->fields[0], 1, '>=', 'group');
1182      $this->assertEntityFieldQuery($query, array(), t('Test with a grouped delta meta condition (empty result set).'));
1183  
1184      // Test language grouping.
1185      $query = new EntityFieldQuery();
1186      $query
1187        ->entityCondition('entity_type', 'test_entity', '=')
1188        ->fieldCondition($this->fields[0], 'value', 0, '=', NULL, 'group')
1189        ->fieldLanguageCondition($this->fields[0], 'en', '<>', NULL, 'group');
1190      $this->assertEntityFieldQuery($query, array(
1191        array('test_entity', 1),
1192      ), t('Test with a grouped language meta condition.'));
1193  
1194      // Test language grouping.
1195      $query = new EntityFieldQuery();
1196      $query
1197        ->entityCondition('entity_type', 'test_entity', '=')
1198        ->fieldCondition($this->fields[0], 'value', 0, '=', NULL, 'group')
1199        ->fieldLanguageCondition($this->fields[0], 'en', '!=', NULL, 'group');
1200      $this->assertEntityFieldQuery($query, array(
1201        array('test_entity', 1),
1202      ), t('Test with a grouped language meta condition.'));
1203  
1204      $query = new EntityFieldQuery();
1205      $query
1206        ->entityCondition('entity_type', 'test_entity', '=')
1207        ->fieldCondition($this->fields[0], 'value', 0, '=', NULL, 'group')
1208        ->fieldLanguageCondition($this->fields[0], LANGUAGE_NONE, '<>', NULL, 'group');
1209      $this->assertEntityFieldQuery($query, array(), t('Test with a grouped language meta condition (empty result set).'));
1210  
1211      $query = new EntityFieldQuery();
1212      $query
1213        ->entityCondition('entity_type', 'test_entity', '=')
1214        ->fieldCondition($this->fields[0], 'value', 0, '=', NULL, 'group')
1215        ->fieldLanguageCondition($this->fields[0], LANGUAGE_NONE, '!=', NULL, 'group');
1216      $this->assertEntityFieldQuery($query, array(), t('Test with a grouped language meta condition (empty result set).'));
1217  
1218      // Test delta and language grouping.
1219      $query = new EntityFieldQuery();
1220      $query
1221        ->entityCondition('entity_type', 'test_entity', '=')
1222        ->fieldCondition($this->fields[0], 'value', 0, '=', 'delta', 'language')
1223        ->fieldDeltaCondition($this->fields[0], 1, '<', 'delta', 'language')
1224        ->fieldLanguageCondition($this->fields[0], 'en', '<>', 'delta', 'language');
1225      $this->assertEntityFieldQuery($query, array(
1226        array('test_entity', 1),
1227      ), t('Test with a grouped delta + language meta condition.'));
1228  
1229      // Test delta and language grouping.
1230      $query = new EntityFieldQuery();
1231      $query
1232        ->entityCondition('entity_type', 'test_entity', '=')
1233        ->fieldCondition($this->fields[0], 'value', 0, '=', 'delta', 'language')
1234        ->fieldDeltaCondition($this->fields[0], 1, '<', 'delta', 'language')
1235        ->fieldLanguageCondition($this->fields[0], 'en', '!=', 'delta', 'language');
1236      $this->assertEntityFieldQuery($query, array(
1237        array('test_entity', 1),
1238      ), t('Test with a grouped delta + language meta condition.'));
1239  
1240      $query = new EntityFieldQuery();
1241      $query
1242        ->entityCondition('entity_type', 'test_entity', '=')
1243        ->fieldCondition($this->fields[0], 'value', 0, '=', 'delta', 'language')
1244        ->fieldDeltaCondition($this->fields[0], 1, '>=', 'delta', 'language')
1245        ->fieldLanguageCondition($this->fields[0], 'en', '<>', 'delta', 'language');
1246      $this->assertEntityFieldQuery($query, array(), t('Test with a grouped delta + language meta condition (empty result set, delta condition unsatisifed).'));
1247  
1248      $query = new EntityFieldQuery();
1249      $query
1250        ->entityCondition('entity_type', 'test_entity', '=')
1251        ->fieldCondition($this->fields[0], 'value', 0, '=', 'delta', 'language')
1252        ->fieldDeltaCondition($this->fields[0], 1, '>=', 'delta', 'language')
1253        ->fieldLanguageCondition($this->fields[0], 'en', '!=', 'delta', 'language');
1254      $this->assertEntityFieldQuery($query, array(), t('Test with a grouped delta + language meta condition (empty result set, delta condition unsatisifed).'));
1255  
1256      $query = new EntityFieldQuery();
1257      $query
1258        ->entityCondition('entity_type', 'test_entity', '=')
1259        ->fieldCondition($this->fields[0], 'value', 0, '=', 'delta', 'language')
1260        ->fieldDeltaCondition($this->fields[0], 1, '<', 'delta', 'language')
1261        ->fieldLanguageCondition($this->fields[0], LANGUAGE_NONE, '<>', 'delta', 'language');
1262      $this->assertEntityFieldQuery($query, array(), t('Test with a grouped delta + language meta condition (empty result set, language condition unsatisifed).'));
1263  
1264      $query = new EntityFieldQuery();
1265      $query
1266        ->entityCondition('entity_type', 'test_entity', '=')
1267        ->fieldCondition($this->fields[0], 'value', 0, '=', 'delta', 'language')
1268        ->fieldDeltaCondition($this->fields[0], 1, '<', 'delta', 'language')
1269        ->fieldLanguageCondition($this->fields[0], LANGUAGE_NONE, '!=', 'delta', 'language');
1270      $this->assertEntityFieldQuery($query, array(), t('Test with a grouped delta + language meta condition (empty result set, language condition unsatisifed).'));
1271  
1272      $query = new EntityFieldQuery();
1273      $query
1274        ->entityCondition('entity_type', 'test_entity', '=')
1275        ->fieldCondition($this->fields[0], 'value', 0, '=', 'delta', 'language')
1276        ->fieldDeltaCondition($this->fields[0], 1, '>=', 'delta', 'language')
1277        ->fieldLanguageCondition($this->fields[0], LANGUAGE_NONE, '<>', 'delta', 'language');
1278      $this->assertEntityFieldQuery($query, array(), t('Test with a grouped delta + language meta condition (empty result set, both conditions unsatisifed).'));
1279  
1280      $query = new EntityFieldQuery();
1281      $query
1282        ->entityCondition('entity_type', 'test_entity', '=')
1283        ->fieldCondition($this->fields[0], 'value', 0, '=', 'delta', 'language')
1284        ->fieldDeltaCondition($this->fields[0], 1, '>=', 'delta', 'language')
1285        ->fieldLanguageCondition($this->fields[0], LANGUAGE_NONE, '!=', 'delta', 'language');
1286      $this->assertEntityFieldQuery($query, array(), t('Test with a grouped delta + language meta condition (empty result set, both conditions unsatisifed).'));
1287  
1288      // Test grouping with another field to ensure that grouping cache is reset
1289      // properly.
1290      $query = new EntityFieldQuery();
1291      $query
1292        ->entityCondition('entity_type', 'test_entity_bundle', '=')
1293        ->fieldCondition($this->fields[1], 'shape', 'circle', '=', 'delta', 'language')
1294        ->fieldCondition($this->fields[1], 'color', 'blue', '=', 'delta', 'language')
1295        ->fieldDeltaCondition($this->fields[1], 1, '=', 'delta', 'language')
1296        ->fieldLanguageCondition($this->fields[1], LANGUAGE_NONE, '=', 'delta', 'language');
1297      $this->assertEntityFieldQuery($query, array(
1298        array('test_entity_bundle', 5),
1299      ), t('Test grouping cache.'));
1300    }
1301  
1302    /**
1303     * Tests the routing feature of EntityFieldQuery.
1304     */
1305    function testEntityFieldQueryRouting() {
1306      // Entity-only query.
1307      $query = new EntityFieldQuery();
1308      $query->entityCondition('entity_type', 'test_entity_bundle_key');
1309      $this->assertIdentical($query->queryCallback(), array($query, 'propertyQuery'), t('Entity-only queries are handled by the propertyQuery handler.'));
1310  
1311      // Field-only query.
1312      $query = new EntityFieldQuery();
1313      $query->fieldCondition($this->fields[0], 'value', '3');
1314      $this->assertIdentical($query->queryCallback(), 'field_sql_storage_field_storage_query', t('Pure field queries are handled by the Field storage handler.'));
1315  
1316      // Mixed entity and field query.
1317      $query = new EntityFieldQuery();
1318      $query
1319        ->entityCondition('entity_type', 'test_entity_bundle_key')
1320        ->fieldCondition($this->fields[0], 'value', '3');
1321      $this->assertIdentical($query->queryCallback(), 'field_sql_storage_field_storage_query', t('Mixed queries are handled by the Field storage handler.'));
1322  
1323      // Overriding with $query->executeCallback.
1324      $query = new EntityFieldQuery();
1325      $query->entityCondition('entity_type', 'test_entity_bundle_key');
1326      $query->executeCallback = 'field_test_dummy_field_storage_query';
1327      $this->assertEntityFieldQuery($query, array(
1328        array('user', 1),
1329      ), t('executeCallback can override the query handler.'));
1330  
1331      // Overriding with $query->executeCallback via hook_entity_query_alter().
1332      $query = new EntityFieldQuery();
1333      $query->entityCondition('entity_type', 'test_entity_bundle_key');
1334      // Add a flag that will be caught by field_test_entity_query_alter().
1335      $query->alterMyExecuteCallbackPlease = TRUE;
1336      $this->assertEntityFieldQuery($query, array(
1337        array('user', 1),
1338      ), t('executeCallback can override the query handler when set in a hook_entity_query_alter().'));
1339  
1340      // Mixed-storage queries.
1341      $query = new EntityFieldQuery();
1342      $query
1343        ->fieldCondition($this->fields[0], 'value', '3')
1344        ->fieldCondition($this->fields[1], 'shape', 'squ', 'STARTS_WITH');
1345      // Alter the storage of the field.
1346      $query->fields[1]['storage']['module'] = 'dummy_storage';
1347      try {
1348        $query->queryCallback();
1349      }
1350      catch (EntityFieldQueryException $exception) {
1351        $pass = ($exception->getMessage() == t("Can't handle more than one field storage engine"));
1352      }
1353      $this->assertTrue($pass, t('Cannot query across field storage engines.'));
1354    }
1355  
1356    /**
1357     * Tests the pager integration of EntityFieldQuery.
1358     */
1359    function testEntityFieldQueryPager() {
1360      // Test pager in propertyQuery
1361      $_GET['page'] = '0,1';
1362      $query = new EntityFieldQuery();
1363      $query
1364        ->entityCondition('entity_type', 'test_entity_bundle_key')
1365        ->propertyOrderBy('ftid', 'ASC')
1366        ->pager(3, 0);
1367      $this->assertEntityFieldQuery($query, array(
1368        array('test_entity_bundle_key', 1),
1369        array('test_entity_bundle_key', 2),
1370        array('test_entity_bundle_key', 3),
1371      ), t('Test pager integration in propertyQuery: page 1.'), TRUE);
1372  
1373      $query = new EntityFieldQuery();
1374      $query
1375        ->entityCondition('entity_type', 'test_entity_bundle_key')
1376        ->propertyOrderBy('ftid', 'ASC')
1377        ->pager(3, 1);
1378      $this->assertEntityFieldQuery($query, array(
1379        array('test_entity_bundle_key', 4),
1380        array('test_entity_bundle_key', 5),
1381        array('test_entity_bundle_key', 6),
1382      ), t('Test pager integration in propertyQuery: page 2.'), TRUE);
1383  
1384      // Test pager in field storage
1385      $_GET['page'] = '0,1';
1386      $query = new EntityFieldQuery();
1387      $query
1388        ->entityCondition('entity_type', 'test_entity_bundle_key')
1389        ->fieldCondition($this->fields[0], 'value', 0, '>')
1390        ->propertyOrderBy('ftid', 'ASC')
1391        ->pager(2, 0);
1392      $this->assertEntityFieldQuery($query, array(
1393        array('test_entity_bundle_key', 1),
1394        array('test_entity_bundle_key', 2),
1395      ), t('Test pager integration in field storage: page 1.'), TRUE);
1396  
1397      $query = new EntityFieldQuery();
1398      $query
1399        ->entityCondition('entity_type', 'test_entity_bundle_key')
1400        ->fieldCondition($this->fields[0], 'value', 0, '>')
1401        ->propertyOrderBy('ftid', 'ASC')
1402        ->pager(2, 1);
1403      $this->assertEntityFieldQuery($query, array(
1404        array('test_entity_bundle_key', 3),
1405        array('test_entity_bundle_key', 4),
1406      ), t('Test pager integration in field storage: page 2.'), TRUE);
1407  
1408      unset($_GET['page']);
1409    }
1410  
1411    /**
1412     * Tests disabling the pager in EntityFieldQuery.
1413     */
1414    function testEntityFieldQueryDisablePager() {
1415      // Test enabling a pager and then disabling it.
1416      $query = new EntityFieldQuery();
1417      $query
1418        ->entityCondition('entity_type', 'test_entity_bundle_key')
1419        ->propertyOrderBy('ftid', 'ASC')
1420        ->pager(1)
1421        ->pager(0);
1422      $this->assertEntityFieldQuery($query, array(
1423        array('test_entity_bundle_key', 1),
1424        array('test_entity_bundle_key', 2),
1425        array('test_entity_bundle_key', 3),
1426        array('test_entity_bundle_key', 4),
1427        array('test_entity_bundle_key', 5),
1428        array('test_entity_bundle_key', 6),
1429      ), 'All test entities are listed when the pager is enabled and then disabled.', TRUE);
1430    }
1431  
1432    /**
1433     * Tests the TableSort integration of EntityFieldQuery.
1434     */
1435    function testEntityFieldQueryTableSort() {
1436      // Test TableSort in propertyQuery
1437      $_GET['sort'] = 'asc';
1438      $_GET['order'] = 'Id';
1439      $header = array(
1440        'id' => array('data' => 'Id', 'type' => 'property',  'specifier' => 'ftid'),
1441        'type' => array('data' => 'Type', 'type' => 'entity', 'specifier' => 'bundle'),
1442      );
1443      $query = new EntityFieldQuery();
1444      $query
1445        ->entityCondition('entity_type', 'test_entity_bundle_key')
1446        ->tableSort($header);
1447      $this->assertEntityFieldQuery($query, array(
1448        array('test_entity_bundle_key', 1),
1449        array('test_entity_bundle_key', 2),
1450        array('test_entity_bundle_key', 3),
1451        array('test_entity_bundle_key', 4),
1452        array('test_entity_bundle_key', 5),
1453        array('test_entity_bundle_key', 6),
1454      ), t('Test TableSort by property: ftid ASC in propertyQuery.'), TRUE);
1455  
1456      $_GET['sort'] = 'desc';
1457      $_GET['order'] = 'Id';
1458      $query = new EntityFieldQuery();
1459      $query
1460        ->entityCondition('entity_type', 'test_entity_bundle_key')
1461        ->tableSort($header);
1462      $this->assertEntityFieldQuery($query, array(
1463        array('test_entity_bundle_key', 6),
1464        array('test_entity_bundle_key', 5),
1465        array('test_entity_bundle_key', 4),
1466        array('test_entity_bundle_key', 3),
1467        array('test_entity_bundle_key', 2),
1468        array('test_entity_bundle_key', 1),
1469      ), t('Test TableSort by property: ftid DESC in propertyQuery.'), TRUE);
1470  
1471      $_GET['sort'] = 'asc';
1472      $_GET['order'] = 'Type';
1473      $query = new EntityFieldQuery();
1474      $query
1475        ->entityCondition('entity_type', 'test_entity_bundle_key')
1476        ->tableSort($header);
1477      $this->assertEntityFieldQuery($query, array(
1478        array('test_entity_bundle_key', 1),
1479        array('test_entity_bundle_key', 2),
1480        array('test_entity_bundle_key', 3),
1481        array('test_entity_bundle_key', 4),
1482        array('test_entity_bundle_key', 5),
1483        array('test_entity_bundle_key', 6),
1484      ), t('Test TableSort by entity: bundle ASC in propertyQuery.'), TRUE);
1485  
1486      $_GET['sort'] = 'desc';
1487      $_GET['order'] = 'Type';
1488      $query = new EntityFieldQuery();
1489      $query
1490        ->entityCondition('entity_type', 'test_entity_bundle_key')
1491        ->tableSort($header);
1492      $this->assertEntityFieldQuery($query, array(
1493        array('test_entity_bundle_key', 5),
1494        array('test_entity_bundle_key', 6),
1495        array('test_entity_bundle_key', 1),
1496        array('test_entity_bundle_key', 2),
1497        array('test_entity_bundle_key', 3),
1498        array('test_entity_bundle_key', 4),
1499      ), t('Test TableSort by entity: bundle DESC in propertyQuery.'), TRUE);
1500  
1501      // Test TableSort in field storage
1502      $_GET['sort'] = 'asc';
1503      $_GET['order'] = 'Id';
1504      $header = array(
1505        'id' => array('data' => 'Id', 'type' => 'property',  'specifier' => 'ftid'),
1506        'type' => array('data' => 'Type', 'type' => 'entity', 'specifier' => 'bundle'),
1507        'field' => array('data' => 'Field', 'type' => 'field', 'specifier' => array('field' => $this->field_names[0], 'column' => 'value')),
1508      );
1509      $query = new EntityFieldQuery();
1510      $query
1511        ->entityCondition('entity_type', 'test_entity_bundle_key')
1512        ->fieldCondition($this->fields[0], 'value', 0, '>')
1513        ->tableSort($header);
1514      $this->assertEntityFieldQuery($query, array(
1515        array('test_entity_bundle_key', 1),
1516        array('test_entity_bundle_key', 2),
1517        array('test_entity_bundle_key', 3),
1518        array('test_entity_bundle_key', 4),
1519        array('test_entity_bundle_key', 5),
1520        array('test_entity_bundle_key', 6),
1521      ), t('Test TableSort by property: ftid ASC in field storage.'), TRUE);
1522  
1523      $_GET['sort'] = 'desc';
1524      $_GET['order'] = 'Id';
1525      $query = new EntityFieldQuery();
1526      $query
1527        ->entityCondition('entity_type', 'test_entity_bundle_key')
1528        ->fieldCondition($this->fields[0], 'value', 0, '>')
1529        ->tableSort($header);
1530      $this->assertEntityFieldQuery($query, array(
1531        array('test_entity_bundle_key', 6),
1532        array('test_entity_bundle_key', 5),
1533        array('test_entity_bundle_key', 4),
1534        array('test_entity_bundle_key', 3),
1535        array('test_entity_bundle_key', 2),
1536        array('test_entity_bundle_key', 1),
1537      ), t('Test TableSort by property: ftid DESC in field storage.'), TRUE);
1538  
1539      $_GET['sort'] = 'asc';
1540      $_GET['order'] = 'Type';
1541      $query = new EntityFieldQuery();
1542      $query
1543        ->entityCondition('entity_type', 'test_entity_bundle_key')
1544        ->fieldCondition($this->fields[0], 'value', 0, '>')
1545        ->tableSort($header)
1546        ->entityOrderBy('entity_id', 'DESC');
1547      $this->assertEntityFieldQuery($query, array(
1548        array('test_entity_bundle_key', 4),
1549        array('test_entity_bundle_key', 3),
1550        array('test_entity_bundle_key', 2),
1551        array('test_entity_bundle_key', 1),
1552        array('test_entity_bundle_key', 6),
1553        array('test_entity_bundle_key', 5),
1554      ), t('Test TableSort by entity: bundle ASC in field storage.'), TRUE);
1555  
1556      $_GET['sort'] = 'desc';
1557      $_GET['order'] = 'Type';
1558      $query = new EntityFieldQuery();
1559      $query
1560        ->entityCondition('entity_type', 'test_entity_bundle_key')
1561        ->fieldCondition($this->fields[0], 'value', 0, '>')
1562        ->tableSort($header)
1563        ->entityOrderBy('entity_id', 'ASC');
1564      $this->assertEntityFieldQuery($query, array(
1565        array('test_entity_bundle_key', 5),
1566        array('test_entity_bundle_key', 6),
1567        array('test_entity_bundle_key', 1),
1568        array('test_entity_bundle_key', 2),
1569        array('test_entity_bundle_key', 3),
1570        array('test_entity_bundle_key', 4),
1571      ), t('Test TableSort by entity: bundle DESC in field storage.'), TRUE);
1572  
1573      $_GET['sort'] = 'asc';
1574      $_GET['order'] = 'Field';
1575      $query = new EntityFieldQuery();
1576      $query
1577        ->entityCondition('entity_type', 'test_entity_bundle_key')
1578        ->fieldCondition($this->fields[0], 'value', 0, '>')
1579        ->tableSort($header);
1580      $this->assertEntityFieldQuery($query, array(
1581        array('test_entity_bundle_key', 1),
1582        array('test_entity_bundle_key', 2),
1583        array('test_entity_bundle_key', 3),
1584        array('test_entity_bundle_key', 4),
1585        array('test_entity_bundle_key', 5),
1586        array('test_entity_bundle_key', 6),
1587      ), t('Test TableSort by field ASC.'), TRUE);
1588  
1589      $_GET['sort'] = 'desc';
1590      $_GET['order'] = 'Field';
1591      $query = new EntityFieldQuery();
1592      $query
1593        ->entityCondition('entity_type', 'test_entity_bundle_key')
1594        ->fieldCondition($this->fields[0], 'value', 0, '>')
1595        ->tableSort($header);
1596      $this->assertEntityFieldQuery($query, array(
1597        array('test_entity_bundle_key', 6),
1598        array('test_entity_bundle_key', 5),
1599        array('test_entity_bundle_key', 4),
1600        array('test_entity_bundle_key', 3),
1601        array('test_entity_bundle_key', 2),
1602        array('test_entity_bundle_key', 1),
1603      ), t('Test TableSort by field DESC.'), TRUE);
1604  
1605      unset($_GET['sort']);
1606      unset($_GET['order']);
1607    }
1608  
1609    /**
1610     * Tests EntityFieldQuery access on non-node entities.
1611     */
1612    function testEntityFieldQueryAccess() {
1613      // Test as a user with ability to bypass node access.
1614      $privileged_user = $this->drupalCreateUser(array('bypass node access', 'access content'));
1615      $this->drupalLogin($privileged_user);
1616      $this->drupalGet('entity-query-access/test/' . $this->fields[0]['field_name']);
1617      $this->assertText('Found entity', 'Returned access response with entities.');
1618      $this->drupalLogout();
1619  
1620      // Test as a user that does not have ability to bypass node access or view
1621      // all nodes.
1622      $regular_user = $this->drupalCreateUser(array('access content'));
1623      $this->drupalLogin($regular_user);
1624      $this->drupalGet('entity-query-access/test/' . $this->fields[0]['field_name']);
1625      $this->assertText('Found entity', 'Returned access response with entities.');
1626      $this->drupalLogout();
1627    }
1628  
1629    /**
1630     * Fetches the results of an EntityFieldQuery and compares.
1631     *
1632     * @param $query
1633     *   An EntityFieldQuery to run.
1634     * @param $intended_results
1635     *   A list of results, every entry is again a list, first being the entity
1636     *   type, the second being the entity_id.
1637     * @param $message
1638     *   The message to be displayed as the result of this test.
1639     * @param $ordered
1640     *   If FALSE then the result of EntityFieldQuery will match
1641     *   $intended_results even if the order is not the same. If TRUE then order
1642     *   should match too.
1643     */
1644    function assertEntityFieldQuery($query, $intended_results, $message, $ordered = FALSE) {
1645      $results = array();
1646      try {
1647        foreach ($query->execute() as $entity_type => $entity_ids) {
1648          foreach ($entity_ids as $entity_id => $stub_entity) {
1649            $results[] = array($entity_type, $entity_id);
1650          }
1651        }
1652        if (!isset($ordered) || !$ordered) {
1653          sort($results);
1654          sort($intended_results);
1655        }
1656        $this->assertEqual($results, $intended_results, $message);
1657      }
1658      catch (Exception $e) {
1659        $this->fail('Exception thrown: '. $e->getMessage());
1660      }
1661    }
1662  
1663    /**
1664     * Tests EFQ table prefixing with multiple conditions and an altered join.
1665     *
1666     * @see field_test_query_efq_table_prefixing_test_alter()
1667     */
1668    function testTablePrefixing() {
1669      $query = new EntityFieldQuery();
1670      $query = $query
1671        ->entityCondition('entity_type', 'test_entity')
1672        ->entityCondition('bundle', 'test_bundle')
1673        ->entityCondition('entity_id', '1')
1674        ->addTag('efq_table_prefixing_test');
1675  
1676      $expected = array(array('test_entity', 1));
1677  
1678      $this->assertEntityFieldQuery($query, $expected, 'An EntityFieldQuery returns the expected results when altered with an additional join on the base table.');
1679    }
1680  
1681  }

title

Description

title

Description

title

Description

title

title

Body