You are here

function EntityQueryTest::testEntityQuery in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/EntityQueryTest.php \Drupal\system\Tests\Entity\EntityQueryTest::testEntityQuery()

Test basic functionality.

File

core/modules/system/src/Tests/Entity/EntityQueryTest.php, line 152
Contains \Drupal\system\Tests\Entity\EntityQueryTest.

Class

EntityQueryTest
Tests Entity Query functionality.

Namespace

Drupal\system\Tests\Entity

Code

function testEntityQuery() {
  $greetings = $this->greetings;
  $figures = $this->figures;
  $this->queryResults = $this->factory
    ->get('entity_test_mulrev')
    ->exists($greetings, 'tr')
    ->condition("{$figures}.color", 'red')
    ->sort('id')
    ->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('entity_test_mulrev', 'OR')
    ->exists($greetings, 'tr')
    ->condition("{$figures}.color", 'red')
    ->sort('id');
  $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('entity_test_mulrev')
    ->condition("{$figures}.color", 'red')
    ->sort('id');
  $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('entity_test_mulrev');
  $group = $query
    ->orConditionGroup()
    ->exists($greetings, 'tr')
    ->condition("{$figures}.color", 'red');
  $this->queryResults = $query
    ->condition($group)
    ->condition("{$greetings}.value", 'sie', 'STARTS_WITH')
    ->sort('revision_id')
    ->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('entity_test_mulrev')
    ->condition("{$figures}.color", 'blue')
    ->condition("{$figures}.color", 'red')
    ->sort('id')
    ->execute();
  $this
    ->assertResult();

  // But an entity might have a red and a blue figure both.
  $query = $this->factory
    ->get('entity_test_mulrev');
  $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('revision_id')
    ->execute();

  // Unit 0 and unit 1, so bits 0 1.
  $this
    ->assertResult(3, 7, 11, 15);

  // Do the same test but with IN operator.
  $query = $this->factory
    ->get('entity_test_mulrev');
  $group_blue = $query
    ->andConditionGroup()
    ->condition("{$figures}.color", array(
    'blue',
  ), 'IN');
  $group_red = $query
    ->andConditionGroup()
    ->condition("{$figures}.color", array(
    'red',
  ), 'IN');
  $this->queryResults = $query
    ->condition($group_blue)
    ->condition($group_red)
    ->sort('id')
    ->execute();

  // Unit 0 and unit 1, so bits 0 1.
  $this
    ->assertResult(3, 7, 11, 15);

  // An entity might have either red or blue figure.
  $this->queryResults = $this->factory
    ->get('entity_test_mulrev')
    ->condition("{$figures}.color", array(
    'blue',
    'red',
  ), 'IN')
    ->sort('id')
    ->execute();

  // Bit 0 or 1 is on.
  $this
    ->assertResult(1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 14, 15);
  $this->queryResults = $this->factory
    ->get('entity_test_mulrev')
    ->exists("{$figures}.color")
    ->notExists("{$greetings}.value")
    ->sort('id')
    ->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('entity_test_mulrev')
    ->condition("{$greetings}.value", 'merhaba')
    ->sort('id')
    ->execute();
  $entities = entity_load_multiple('entity_test_mulrev', $ids);
  $first_entity = reset($entities);
  $old_name = $first_entity->name->value;
  foreach ($entities as $entity) {
    $entity
      ->setNewRevision();
    $entity
      ->getTranslation('tr')->{$greetings}->value = 'xsiemax';
    $entity->name->value .= 'x';
    $entity
      ->save();
  }

  // We changed the entity names, so the current revision should not match.
  $this->queryResults = $this->factory
    ->get('entity_test_mulrev')
    ->condition('name.value', $old_name)
    ->execute();
  $this
    ->assertResult();

  // Only if all revisions are queried, we find the old revision.
  $this->queryResults = $this->factory
    ->get('entity_test_mulrev')
    ->condition('name.value', $old_name)
    ->allRevisions()
    ->sort('revision_id')
    ->execute();
  $this
    ->assertRevisionResult(array(
    $first_entity
      ->id(),
  ), array(
    $first_entity
      ->id(),
  ));

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

  // The query only matches the original revisions.
  $this
    ->assertRevisionResult(array(
    4,
    5,
    6,
    7,
    12,
    13,
    14,
    15,
  ), array(
    4,
    5,
    6,
    7,
    12,
    13,
    14,
    15,
  ));
  $results = $this->factory
    ->get('entity_test_mulrev')
    ->condition("{$greetings}.value", 'siema', 'CONTAINS')
    ->sort('id')
    ->execute();

  // This matches both the original and new current revisions, multiple
  // revisions are returned for some entities.
  $assert = array(
    16 => '4',
    17 => '5',
    18 => '6',
    19 => '7',
    8 => '8',
    9 => '9',
    10 => '10',
    11 => '11',
    20 => '12',
    21 => '13',
    22 => '14',
    23 => '15',
  );
  $this
    ->assertIdentical($results, $assert);
  $results = $this->factory
    ->get('entity_test_mulrev')
    ->condition("{$greetings}.value", 'siema', 'STARTS_WITH')
    ->sort('revision_id')
    ->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('entity_test_mulrev')
    ->condition("{$greetings}.value", 'a', 'ENDS_WITH')
    ->sort('revision_id')
    ->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('entity_test_mulrev')
    ->condition("{$greetings}.value", 'a', 'ENDS_WITH')
    ->allRevisions()
    ->sort('id')
    ->sort('revision_id')
    ->execute();

  // Now we get everything.
  $assert = array(
    4 => '4',
    5 => '5',
    6 => '6',
    7 => '7',
    8 => '8',
    9 => '9',
    10 => '10',
    11 => '11',
    12 => '12',
    20 => '12',
    13 => '13',
    21 => '13',
    14 => '14',
    22 => '14',
    15 => '15',
    23 => '15',
  );
  $this
    ->assertIdentical($results, $assert);
}