function EntityFieldQueryTestCase::testEntityFieldQueryRouting

Tests the routing feature of EntityFieldQuery.

File

drupal/modules/simpletest/tests/entity_query.test, line 1305
Unit test file for the entity API.

Class

EntityFieldQueryTestCase
Tests EntityFieldQuery.

Code

function testEntityFieldQueryRouting() {

  // Entity-only query.
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', 'test_entity_bundle_key');
  $this
    ->assertIdentical($query
    ->queryCallback(), array(
    $query,
    'propertyQuery',
  ), 'Entity-only queries are handled by the propertyQuery handler.');

  // Field-only query.
  $query = new EntityFieldQuery();
  $query
    ->fieldCondition($this->fields[0], 'value', '3');
  $this
    ->assertIdentical($query
    ->queryCallback(), 'field_sql_storage_field_storage_query', 'Pure field queries are handled by the Field storage handler.');

  // Mixed entity and field query.
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', 'test_entity_bundle_key')
    ->fieldCondition($this->fields[0], 'value', '3');
  $this
    ->assertIdentical($query
    ->queryCallback(), 'field_sql_storage_field_storage_query', 'Mixed queries are handled by the Field storage handler.');

  // Overriding with $query->executeCallback.
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', 'test_entity_bundle_key');
  $query->executeCallback = 'field_test_dummy_field_storage_query';
  $this
    ->assertEntityFieldQuery($query, array(
    array(
      'user',
      1,
    ),
  ), 'executeCallback can override the query handler.');

  // Overriding with $query->executeCallback via hook_entity_query_alter().
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', 'test_entity_bundle_key');

  // Add a flag that will be caught by field_test_entity_query_alter().
  $query->alterMyExecuteCallbackPlease = TRUE;
  $this
    ->assertEntityFieldQuery($query, array(
    array(
      'user',
      1,
    ),
  ), 'executeCallback can override the query handler when set in a hook_entity_query_alter().');

  // Mixed-storage queries.
  $query = new EntityFieldQuery();
  $query
    ->fieldCondition($this->fields[0], 'value', '3')
    ->fieldCondition($this->fields[1], 'shape', 'squ', 'STARTS_WITH');

  // Alter the storage of the field.
  $query->fields[1]['storage']['module'] = 'dummy_storage';
  try {
    $query
      ->queryCallback();
  } catch (EntityFieldQueryException $exception) {
    $pass = $exception
      ->getMessage() == t("Can't handle more than one field storage engine");
  }
  $this
    ->assertTrue($pass, 'Cannot query across field storage engines.');
}