function EntityQueryTest::testEntityQuery

Test basic functionality.

File

drupal/core/modules/system/lib/Drupal/system/Tests/Entity/EntityQueryTest.php, line 136
Definition of Drupal\system\Tests\Entity\EntityQueryTest.

Class

EntityQueryTest
Tests the basic Entity API.

Namespace

Drupal\system\Tests\Entity

Code

function testEntityQuery() {
  $greetings = $this->greetings;
  $figures = $this->figures;
  $this->queryResults = $this->factory
    ->get('test_entity')
    ->exists($greetings, 'tr')
    ->condition("{$figures}.color", 'red')
    ->sort('ftid')
    ->execute();

  // As unit 0 was the red triangle and unit 2 was the turkish greeting,
  // bit 0 and bit 2 needs to be set.
  $this
    ->assertResult(5, 7, 13, 15);
  $query = $this->factory
    ->get('test_entity', 'OR')
    ->exists($greetings, 'tr')
    ->condition("{$figures}.color", 'red')
    ->sort('ftid');
  $count_query = clone $query;
  $this
    ->assertEqual(12, $count_query
    ->count()
    ->execute());
  $this->queryResults = $query
    ->execute();

  // Now bit 0 (1, 3, 5, 7, 9, 11, 13, 15) or bit 2 (4, 5, 6, 7, 12, 13, 14,
  // 15) needs to be set.
  $this
    ->assertResult(1, 3, 4, 5, 6, 7, 9, 11, 12, 13, 14, 15);

  // Test cloning of query conditions.
  $query = $this->factory
    ->get('test_entity')
    ->condition("{$figures}.color", 'red')
    ->sort('ftid');
  $cloned_query = clone $query;
  $cloned_query
    ->condition("{$figures}.shape", 'circle');

  // Bit 0 (1, 3, 5, 7, 9, 11, 13, 15) needs to be set.
  $this->queryResults = $query
    ->execute();
  $this
    ->assertResult(1, 3, 5, 7, 9, 11, 13, 15);

  // No red color has a circle shape.
  $this->queryResults = $cloned_query
    ->execute();
  $this
    ->assertResult();
  $query = $this->factory
    ->get('test_entity');
  $group = $query
    ->orConditionGroup()
    ->exists($greetings, 'tr')
    ->condition("{$figures}.color", 'red');
  $this->queryResults = $query
    ->condition($group)
    ->condition("{$greetings}.value", 'sie', 'STARTS_WITH')
    ->sort('ftvid')
    ->execute();

  // Bit 3 and (bit 0 or 2) -- the above 8 part of the above.
  $this
    ->assertResult(9, 11, 12, 13, 14, 15);

  // No figure has both the colors blue and red at the same time.
  $this->queryResults = $this->factory
    ->get('test_entity')
    ->condition("{$figures}.color", 'blue')
    ->condition("{$figures}.color", 'red')
    ->sort('ftid')
    ->execute();
  $this
    ->assertResult();

  // But an entity might have a red and a blue figure both.
  $query = $this->factory
    ->get('test_entity');
  $group_blue = $query
    ->andConditionGroup()
    ->condition("{$figures}.color", 'blue');
  $group_red = $query
    ->andConditionGroup()
    ->condition("{$figures}.color", 'red');
  $this->queryResults = $query
    ->condition($group_blue)
    ->condition($group_red)
    ->sort('ftvid')
    ->execute();

  // Unit 0 and unit 1, so bits 0 1.
  $this
    ->assertResult(3, 7, 11, 15);
  $this->queryResults = $this->factory
    ->get('test_entity')
    ->exists("{$figures}.color")
    ->notExists("{$greetings}.value")
    ->sort('ftid')
    ->execute();

  // Bit 0 or 1 is on but 2 and 3 are not.
  $this
    ->assertResult(1, 2, 3);

  // Now update the 'merhaba' string to xsiemax which is not a meaningful
  // word but allows us to test revisions and string operations.
  $ids = $this->factory
    ->get('test_entity')
    ->condition("{$greetings}.value", 'merhaba')
    ->execute();
  $entities = entity_load_multiple('test_entity', $ids);
  foreach ($entities as $entity) {
    $entity
      ->setNewRevision();
    $entity->{$greetings}['tr'][0]['value'] = 'xsiemax';
    $entity
      ->save();
  }

  // When querying current revisions, this string is no longer found.
  $this->queryResults = $this->factory
    ->get('test_entity')
    ->condition("{$greetings}.value", 'merhaba')
    ->execute();
  $this
    ->assertResult();
  $this->queryResults = $this->factory
    ->get('test_entity')
    ->condition("{$greetings}.value", 'merhaba')
    ->age(FIELD_LOAD_REVISION)
    ->sort('ftvid')
    ->execute();

  // Bit 2 needs to be set.
  // The keys must be 16-23 because the first batch stopped at 15 so the
  // second started at 16 and eight entities were saved.
  $assert = $this
    ->assertRevisionResult(range(16, 23), array(
    4,
    5,
    6,
    7,
    12,
    13,
    14,
    15,
  ));
  $results = $this->factory
    ->get('test_entity')
    ->condition("{$greetings}.value", 'siema', 'CONTAINS')
    ->sort('ftid')
    ->execute();

  // This is the same as the previous one because xsiemax replaced merhaba
  // but also it contains the entities that siema originally but not
  // merhaba.
  $assert = array_slice($assert, 0, 4, TRUE) + array(
    8 => '8',
    9 => '9',
    10 => '10',
    11 => '11',
  ) + array_slice($assert, 4, 4, TRUE);
  $this
    ->assertIdentical($results, $assert);
  $results = $this->factory
    ->get('test_entity')
    ->condition("{$greetings}.value", 'siema', 'STARTS_WITH')
    ->execute();

  // Now we only get the ones that originally were siema, entity id 8 and
  // above.
  $this
    ->assertIdentical($results, array_slice($assert, 4, 8, TRUE));
  $results = $this->factory
    ->get('test_entity')
    ->condition("{$greetings}.value", 'a', 'ENDS_WITH')
    ->execute();

  // It is very important that we do not get the ones which only have
  // xsiemax despite originally they were merhaba, ie. ended with a.
  $this
    ->assertIdentical($results, array_slice($assert, 4, 8, TRUE));
  $results = $this->factory
    ->get('test_entity')
    ->condition("{$greetings}.value", 'a', 'ENDS_WITH')
    ->age(FIELD_LOAD_REVISION)
    ->sort('ftid')
    ->execute();

  // Now we get everything.
  $this
    ->assertIdentical($results, $assert);
}