You are here

public function ConfigEntityQueryTest::testConfigEntityQuery in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityQueryTest.php \Drupal\KernelTests\Core\Entity\ConfigEntityQueryTest::testConfigEntityQuery()

Tests basic functionality.

File

core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityQueryTest.php, line 143

Class

ConfigEntityQueryTest
Tests Config Entity Query functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testConfigEntityQuery() {

  // Run a test without any condition.
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->execute();
  $this
    ->assertResults([
    '1',
    '2',
    '3',
    '4',
    '5',
    '6',
    '7',
  ]);

  // No conditions, OR.
  $this->queryResults = $this->entityStorage
    ->getQuery('OR')
    ->execute();
  $this
    ->assertResults([
    '1',
    '2',
    '3',
    '4',
    '5',
    '6',
    '7',
  ]);

  // Filter by ID with equality.
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->condition('id', '3')
    ->execute();
  $this
    ->assertResults([
    '3',
  ]);

  // Filter by label with a known prefix.
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->condition('label', 'test_prefix', 'STARTS_WITH')
    ->execute();
  $this
    ->assertResults([
    '3',
  ]);

  // Filter by label with a known suffix.
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->condition('label', 'test_suffix', 'ENDS_WITH')
    ->execute();
  $this
    ->assertResults([
    '4',
  ]);

  // Filter by label with a known containing word.
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->condition('label', 'test_contains', 'CONTAINS')
    ->execute();
  $this
    ->assertResults([
    '5',
  ]);

  // Filter by ID with the IN operator.
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->condition('id', [
    '2',
    '3',
  ], 'IN')
    ->execute();
  $this
    ->assertResults([
    '2',
    '3',
  ]);

  // Filter by ID with the implicit IN operator.
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->condition('id', [
    '2',
    '3',
  ])
    ->execute();
  $this
    ->assertResults([
    '2',
    '3',
  ]);

  // Filter by ID with the > operator.
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->condition('id', '3', '>')
    ->execute();
  $this
    ->assertResults([
    '4',
    '5',
    '6',
    '7',
  ]);

  // Filter by ID with the >= operator.
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->condition('id', '3', '>=')
    ->execute();
  $this
    ->assertResults([
    '3',
    '4',
    '5',
    '6',
    '7',
  ]);

  // Filter by ID with the <> operator.
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->condition('id', '3', '<>')
    ->execute();
  $this
    ->assertResults([
    '1',
    '2',
    '4',
    '5',
    '6',
    '7',
  ]);

  // Filter by ID with the < operator.
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->condition('id', '3', '<')
    ->execute();
  $this
    ->assertResults([
    '1',
    '2',
  ]);

  // Filter by ID with the <= operator.
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->condition('id', '3', '<=')
    ->execute();
  $this
    ->assertResults([
    '1',
    '2',
    '3',
  ]);

  // Filter by two conditions on the same field.
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->condition('label', 'test_pref', 'STARTS_WITH')
    ->condition('label', 'test_prefix', 'STARTS_WITH')
    ->execute();
  $this
    ->assertResults([
    '3',
  ]);

  // Filter by two conditions on different fields. The first query matches for
  // a different ID, so the result is empty.
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->condition('label', 'test_prefix', 'STARTS_WITH')
    ->condition('id', '5')
    ->execute();
  $this
    ->assertResults([]);

  // Filter by two different conditions on different fields. This time the
  // first condition matches on one item, but the second one does as well.
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->condition('label', 'test_prefix', 'STARTS_WITH')
    ->condition('id', '3')
    ->execute();
  $this
    ->assertResults([
    '3',
  ]);

  // Filter by two different conditions, of which the first one matches for
  // every entry, the second one as well, but just the third one filters so
  // that just two are left.
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->condition('id', '1', '>=')
    ->condition('number', 10, '>=')
    ->condition('number', 50, '>=')
    ->execute();
  $this
    ->assertResults([
    '3',
    '5',
    '7',
  ]);

  // Filter with an OR condition group.
  $this->queryResults = $this->entityStorage
    ->getQuery('OR')
    ->condition('id', 1)
    ->condition('id', '2')
    ->execute();
  $this
    ->assertResults([
    '1',
    '2',
  ]);

  // Simplify it with IN.
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->condition('id', [
    '1',
    '2',
  ])
    ->execute();
  $this
    ->assertResults([
    '1',
    '2',
  ]);

  // Try explicit IN.
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->condition('id', [
    '1',
    '2',
  ], 'IN')
    ->execute();
  $this
    ->assertResults([
    '1',
    '2',
  ]);

  // Try not IN.
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->condition('id', [
    '1',
    '2',
  ], 'NOT IN')
    ->execute();
  $this
    ->assertResults([
    '3',
    '4',
    '5',
    '6',
    '7',
  ]);

  // Filter with an OR condition group on different fields.
  $this->queryResults = $this->entityStorage
    ->getQuery('OR')
    ->condition('id', 1)
    ->condition('number', 41)
    ->execute();
  $this
    ->assertResults([
    '1',
    '2',
  ]);

  // Filter with an OR condition group on different fields but matching on the
  // same entity.
  $this->queryResults = $this->entityStorage
    ->getQuery('OR')
    ->condition('id', 1)
    ->condition('number', 31)
    ->execute();
  $this
    ->assertResults([
    '1',
  ]);

  // NO simple conditions, YES complex conditions, 'AND'.
  $query = $this->entityStorage
    ->getQuery('AND');
  $and_condition_1 = $query
    ->orConditionGroup()
    ->condition('id', '2')
    ->condition('label', $this->entities[0]->label);
  $and_condition_2 = $query
    ->orConditionGroup()
    ->condition('id', 1)
    ->condition('label', $this->entities[3]->label);
  $this->queryResults = $query
    ->condition($and_condition_1)
    ->condition($and_condition_2)
    ->execute();
  $this
    ->assertResults([
    '1',
  ]);

  // NO simple conditions, YES complex conditions, 'OR'.
  $query = $this->entityStorage
    ->getQuery('OR');
  $and_condition_1 = $query
    ->andConditionGroup()
    ->condition('id', 1)
    ->condition('label', $this->entities[0]->label);
  $and_condition_2 = $query
    ->andConditionGroup()
    ->condition('id', '2')
    ->condition('label', $this->entities[1]->label);
  $this->queryResults = $query
    ->condition($and_condition_1)
    ->condition($and_condition_2)
    ->execute();
  $this
    ->assertResults([
    '1',
    '2',
  ]);

  // YES simple conditions, YES complex conditions, 'AND'.
  $query = $this->entityStorage
    ->getQuery('AND');
  $and_condition_1 = $query
    ->orConditionGroup()
    ->condition('id', '2')
    ->condition('label', $this->entities[0]->label);
  $and_condition_2 = $query
    ->orConditionGroup()
    ->condition('id', 1)
    ->condition('label', $this->entities[3]->label);
  $this->queryResults = $query
    ->condition('number', 31)
    ->condition($and_condition_1)
    ->condition($and_condition_2)
    ->execute();
  $this
    ->assertResults([
    '1',
  ]);

  // YES simple conditions, YES complex conditions, 'OR'.
  $query = $this->entityStorage
    ->getQuery('OR');
  $and_condition_1 = $query
    ->orConditionGroup()
    ->condition('id', '2')
    ->condition('label', $this->entities[0]->label);
  $and_condition_2 = $query
    ->orConditionGroup()
    ->condition('id', 1)
    ->condition('label', $this->entities[3]->label);
  $this->queryResults = $query
    ->condition('number', 53)
    ->condition($and_condition_1)
    ->condition($and_condition_2)
    ->execute();
  $this
    ->assertResults([
    '1',
    '2',
    '4',
    '5',
  ]);

  // Test the exists and notExists conditions.
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->exists('id')
    ->execute();
  $this
    ->assertResults([
    '1',
    '2',
    '3',
    '4',
    '5',
    '6',
    '7',
  ]);
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->exists('non-existent')
    ->execute();
  $this
    ->assertResults([]);
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->notExists('id')
    ->execute();
  $this
    ->assertResults([]);
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->notExists('non-existent')
    ->execute();
  $this
    ->assertResults([
    '1',
    '2',
    '3',
    '4',
    '5',
    '6',
    '7',
  ]);
}