You are here

public function ConfigEntityQueryTest::testConfigEntityQuery in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/system/src/Tests/Entity/ConfigEntityQueryTest.php \Drupal\system\Tests\Entity\ConfigEntityQueryTest::testConfigEntityQuery()

Tests basic functionality.

File

core/modules/system/src/Tests/Entity/ConfigEntityQueryTest.php, line 119
Contains \Drupal\system\Tests\Entity\ConfigEntityQueryTest.

Class

ConfigEntityQueryTest
Tests Config Entity Query functionality.

Namespace

Drupal\system\Tests\Entity

Code

public function testConfigEntityQuery() {

  // Run a test without any condition.
  $this->queryResults = $this->factory
    ->get('config_query_test')
    ->execute();
  $this
    ->assertResults(array(
    '1',
    '2',
    '3',
    '4',
    '5',
  ));

  // No conditions, OR.
  $this->queryResults = $this->factory
    ->get('config_query_test', 'OR')
    ->execute();
  $this
    ->assertResults(array(
    '1',
    '2',
    '3',
    '4',
    '5',
  ));

  // Filter by ID with equality.
  $this->queryResults = $this->factory
    ->get('config_query_test')
    ->condition('id', '3')
    ->execute();
  $this
    ->assertResults(array(
    '3',
  ));

  // Filter by label with a known prefix.
  $this->queryResults = $this->factory
    ->get('config_query_test')
    ->condition('label', 'test_prefix', 'STARTS_WITH')
    ->execute();
  $this
    ->assertResults(array(
    '3',
  ));

  // Filter by label with a known suffix.
  $this->queryResults = $this->factory
    ->get('config_query_test')
    ->condition('label', 'test_suffix', 'ENDS_WITH')
    ->execute();
  $this
    ->assertResults(array(
    '4',
  ));

  // Filter by label with a known containing word.
  $this->queryResults = $this->factory
    ->get('config_query_test')
    ->condition('label', 'test_contains', 'CONTAINS')
    ->execute();
  $this
    ->assertResults(array(
    '5',
  ));

  // Filter by ID with the IN operator.
  $this->queryResults = $this->factory
    ->get('config_query_test')
    ->condition('id', array(
    '2',
    '3',
  ), 'IN')
    ->execute();
  $this
    ->assertResults(array(
    '2',
    '3',
  ));

  // Filter by ID with the implicit IN operator.
  $this->queryResults = $this->factory
    ->get('config_query_test')
    ->condition('id', array(
    '2',
    '3',
  ))
    ->execute();
  $this
    ->assertResults(array(
    '2',
    '3',
  ));

  // Filter by ID with the > operator.
  $this->queryResults = $this->factory
    ->get('config_query_test')
    ->condition('id', '3', '>')
    ->execute();
  $this
    ->assertResults(array(
    '4',
    '5',
  ));

  // Filter by ID with the >= operator.
  $this->queryResults = $this->factory
    ->get('config_query_test')
    ->condition('id', '3', '>=')
    ->execute();
  $this
    ->assertResults(array(
    '3',
    '4',
    '5',
  ));

  // Filter by ID with the <> operator.
  $this->queryResults = $this->factory
    ->get('config_query_test')
    ->condition('id', '3', '<>')
    ->execute();
  $this
    ->assertResults(array(
    '1',
    '2',
    '4',
    '5',
  ));

  // Filter by ID with the < operator.
  $this->queryResults = $this->factory
    ->get('config_query_test')
    ->condition('id', '3', '<')
    ->execute();
  $this
    ->assertResults(array(
    '1',
    '2',
  ));

  // Filter by ID with the <= operator.
  $this->queryResults = $this->factory
    ->get('config_query_test')
    ->condition('id', '3', '<=')
    ->execute();
  $this
    ->assertResults(array(
    '1',
    '2',
    '3',
  ));

  // Filter by two conditions on the same field.
  $this->queryResults = $this->factory
    ->get('config_query_test')
    ->condition('label', 'test_pref', 'STARTS_WITH')
    ->condition('label', 'test_prefix', 'STARTS_WITH')
    ->execute();
  $this
    ->assertResults(array(
    '3',
  ));

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

  // 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->factory
    ->get('config_query_test')
    ->condition('label', 'test_prefix', 'STARTS_WITH')
    ->condition('id', '3')
    ->execute();
  $this
    ->assertResults(array(
    '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->factory
    ->get('config_query_test')
    ->condition('id', '1', '>=')
    ->condition('number', 10, '>=')
    ->condition('number', 50, '>=')
    ->execute();
  $this
    ->assertResults(array(
    '3',
    '5',
  ));

  // Filter with an OR condition group.
  $this->queryResults = $this->factory
    ->get('config_query_test', 'OR')
    ->condition('id', 1)
    ->condition('id', '2')
    ->execute();
  $this
    ->assertResults(array(
    '1',
    '2',
  ));

  // Simplify it with IN.
  $this->queryResults = $this->factory
    ->get('config_query_test')
    ->condition('id', array(
    '1',
    '2',
  ))
    ->execute();
  $this
    ->assertResults(array(
    '1',
    '2',
  ));

  // Try explicit IN.
  $this->queryResults = $this->factory
    ->get('config_query_test')
    ->condition('id', array(
    '1',
    '2',
  ), 'IN')
    ->execute();
  $this
    ->assertResults(array(
    '1',
    '2',
  ));

  // Try not IN.
  $this->queryResults = $this->factory
    ->get('config_query_test')
    ->condition('id', array(
    '1',
    '2',
  ), 'NOT IN')
    ->execute();
  $this
    ->assertResults(array(
    '3',
    '4',
    '5',
  ));

  // Filter with an OR condition group on different fields.
  $this->queryResults = $this->factory
    ->get('config_query_test', 'OR')
    ->condition('id', 1)
    ->condition('number', 41)
    ->execute();
  $this
    ->assertResults(array(
    '1',
    '2',
  ));

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

  // NO simple conditions, YES complex conditions, 'AND'.
  $query = $this->factory
    ->get('config_query_test', '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(array(
    '1',
  ));

  // NO simple conditions, YES complex conditions, 'OR'.
  $query = $this->factory
    ->get('config_query_test', '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(array(
    '1',
    '2',
  ));

  // YES simple conditions, YES complex conditions, 'AND'.
  $query = $this->factory
    ->get('config_query_test', '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(array(
    '1',
  ));

  // YES simple conditions, YES complex conditions, 'OR'.
  $query = $this->factory
    ->get('config_query_test', '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(array(
    '1',
    '2',
    '4',
    '5',
  ));

  // Test the exists and notExists conditions.
  $this->queryResults = $this->factory
    ->get('config_query_test')
    ->exists('id')
    ->execute();
  $this
    ->assertResults(array(
    '1',
    '2',
    '3',
    '4',
    '5',
  ));
  $this->queryResults = $this->factory
    ->get('config_query_test')
    ->exists('non-existent')
    ->execute();
  $this
    ->assertResults(array());
  $this->queryResults = $this->factory
    ->get('config_query_test')
    ->notExists('id')
    ->execute();
  $this
    ->assertResults(array());
  $this->queryResults = $this->factory
    ->get('config_query_test')
    ->notExists('non-existent')
    ->execute();
  $this
    ->assertResults(array(
    '1',
    '2',
    '3',
    '4',
    '5',
  ));
}