You are here

public function EntityMetadataTestCase::testEntityQuery in Entity API 7

Tests using entity_property_query().

File

./entity.test, line 1059
Entity CRUD API tests.

Class

EntityMetadataTestCase
Tests metadata wrappers.

Code

public function testEntityQuery() {

  // Create a test node.
  $title = '<b>Is it bold?<b>';
  $values[LANGUAGE_NONE][0] = array(
    'value' => 'foo',
  );
  $node = $this
    ->drupalCreateNode(array(
    $this->field_name => $values,
    'title' => $title,
    'uid' => $GLOBALS['user']->uid,
  ));
  $results = entity_property_query('node', 'title', $title);
  $this
    ->assertEqual($results, array(
    $node->nid,
  ), 'Queried nodes with a given title.');
  $results = entity_property_query('node', $this->field_name, 'foo');
  $this
    ->assertEqual($results, array(
    $node->nid,
  ), 'Queried nodes with a given field value.');
  $results = entity_property_query('node', $this->field_name, array(
    'foo',
    'bar',
  ));
  $this
    ->assertEqual($results, array(
    $node->nid,
  ), 'Queried nodes with a list of possible values.');
  $results = entity_property_query('node', 'author', $GLOBALS['user']);
  $this
    ->assertEqual($results, array(
    $node->nid,
  ), 'Queried nodes with a given author.');

  // Create another test node and try querying for tags.
  $tag = entity_property_values_create_entity('taxonomy_term', array(
    'name' => $this
      ->randomName(),
    'vocabulary' => 1,
  ))
    ->save();
  $field_tag_value[LANGUAGE_NONE][0]['tid'] = $tag
    ->getIdentifier();
  $node = $this
    ->drupalCreateNode(array(
    'type' => 'article',
    'field_tags' => $field_tag_value,
  ));

  // Try query-ing with a single value.
  $results = entity_property_query('node', 'field_tags', $tag
    ->getIdentifier());
  $this
    ->assertEqual($results, array(
    $node->nid,
  ), 'Queried nodes with a given term id.');
  $results = entity_property_query('node', 'field_tags', $tag
    ->value());
  $this
    ->assertEqual($results, array(
    $node->nid,
  ), 'Queried nodes with a given term object.');

  // Try query-ing with a list of possible values.
  $results = entity_property_query('node', 'field_tags', array(
    $tag
      ->getIdentifier(),
  ));
  $this
    ->assertEqual($results, array(
    $node->nid,
  ), 'Queried nodes with a list of term ids.');
}