View source
<?php
namespace Drupal\system\Tests\Entity;
use Drupal\Core\Config\Entity\Query\QueryFactory;
use Drupal\simpletest\KernelTestBase;
class ConfigEntityQueryTest extends KernelTestBase {
static $modules = array(
'config_test',
);
protected $queryResults;
protected $factory;
protected $entities;
protected function setUp() {
parent::setUp();
$this->entities = array();
$this->factory = $this->container
->get('entity.query');
$array['level1']['level2a'] = 9;
$array['level1a']['level2'] = 9;
$array['level1']['level2'] = 1;
$entity = entity_create('config_query_test', array(
'label' => $this
->randomMachineName(),
'id' => '1',
'number' => 31,
'array' => $array,
));
$this->entities[] = $entity;
$entity
->enforceIsNew();
$entity
->save();
$array['level1']['level2'] = 2;
$entity = entity_create('config_query_test', array(
'label' => $this
->randomMachineName(),
'id' => '2',
'number' => 41,
'array' => $array,
));
$this->entities[] = $entity;
$entity
->enforceIsNew();
$entity
->save();
$array['level1']['level2'] = 1;
$entity = entity_create('config_query_test', array(
'label' => 'test_prefix_' . $this
->randomMachineName(),
'id' => '3',
'number' => 59,
'array' => $array,
));
$this->entities[] = $entity;
$entity
->enforceIsNew();
$entity
->save();
$array['level1']['level2'] = 2;
$entity = entity_create('config_query_test', array(
'label' => $this
->randomMachineName() . '_test_suffix',
'id' => '4',
'number' => 26,
'array' => $array,
));
$this->entities[] = $entity;
$entity
->enforceIsNew();
$entity
->save();
$array['level1']['level2'] = 3;
$entity = entity_create('config_query_test', array(
'label' => $this
->randomMachineName() . '_TEST_contains_' . $this
->randomMachineName(),
'id' => '5',
'number' => 53,
'array' => $array,
));
$this->entities[] = $entity;
$entity
->enforceIsNew();
$entity
->save();
}
public function testConfigEntityQuery() {
$this->queryResults = $this->factory
->get('config_query_test')
->execute();
$this
->assertResults(array(
'1',
'2',
'3',
'4',
'5',
));
$this->queryResults = $this->factory
->get('config_query_test', 'OR')
->execute();
$this
->assertResults(array(
'1',
'2',
'3',
'4',
'5',
));
$this->queryResults = $this->factory
->get('config_query_test')
->condition('id', '3')
->execute();
$this
->assertResults(array(
'3',
));
$this->queryResults = $this->factory
->get('config_query_test')
->condition('label', 'test_prefix', 'STARTS_WITH')
->execute();
$this
->assertResults(array(
'3',
));
$this->queryResults = $this->factory
->get('config_query_test')
->condition('label', 'test_suffix', 'ENDS_WITH')
->execute();
$this
->assertResults(array(
'4',
));
$this->queryResults = $this->factory
->get('config_query_test')
->condition('label', 'test_contains', 'CONTAINS')
->execute();
$this
->assertResults(array(
'5',
));
$this->queryResults = $this->factory
->get('config_query_test')
->condition('id', array(
'2',
'3',
), 'IN')
->execute();
$this
->assertResults(array(
'2',
'3',
));
$this->queryResults = $this->factory
->get('config_query_test')
->condition('id', array(
'2',
'3',
))
->execute();
$this
->assertResults(array(
'2',
'3',
));
$this->queryResults = $this->factory
->get('config_query_test')
->condition('id', '3', '>')
->execute();
$this
->assertResults(array(
'4',
'5',
));
$this->queryResults = $this->factory
->get('config_query_test')
->condition('id', '3', '>=')
->execute();
$this
->assertResults(array(
'3',
'4',
'5',
));
$this->queryResults = $this->factory
->get('config_query_test')
->condition('id', '3', '<>')
->execute();
$this
->assertResults(array(
'1',
'2',
'4',
'5',
));
$this->queryResults = $this->factory
->get('config_query_test')
->condition('id', '3', '<')
->execute();
$this
->assertResults(array(
'1',
'2',
));
$this->queryResults = $this->factory
->get('config_query_test')
->condition('id', '3', '<=')
->execute();
$this
->assertResults(array(
'1',
'2',
'3',
));
$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',
));
$this->queryResults = $this->factory
->get('config_query_test')
->condition('label', 'test_prefix', 'STARTS_WITH')
->condition('id', '5')
->execute();
$this
->assertResults(array());
$this->queryResults = $this->factory
->get('config_query_test')
->condition('label', 'test_prefix', 'STARTS_WITH')
->condition('id', '3')
->execute();
$this
->assertResults(array(
'3',
));
$this->queryResults = $this->factory
->get('config_query_test')
->condition('id', '1', '>=')
->condition('number', 10, '>=')
->condition('number', 50, '>=')
->execute();
$this
->assertResults(array(
'3',
'5',
));
$this->queryResults = $this->factory
->get('config_query_test', 'OR')
->condition('id', 1)
->condition('id', '2')
->execute();
$this
->assertResults(array(
'1',
'2',
));
$this->queryResults = $this->factory
->get('config_query_test')
->condition('id', array(
'1',
'2',
))
->execute();
$this
->assertResults(array(
'1',
'2',
));
$this->queryResults = $this->factory
->get('config_query_test')
->condition('id', array(
'1',
'2',
), 'IN')
->execute();
$this
->assertResults(array(
'1',
'2',
));
$this->queryResults = $this->factory
->get('config_query_test')
->condition('id', array(
'1',
'2',
), 'NOT IN')
->execute();
$this
->assertResults(array(
'3',
'4',
'5',
));
$this->queryResults = $this->factory
->get('config_query_test', 'OR')
->condition('id', 1)
->condition('number', 41)
->execute();
$this
->assertResults(array(
'1',
'2',
));
$this->queryResults = $this->factory
->get('config_query_test', 'OR')
->condition('id', 1)
->condition('number', 31)
->execute();
$this
->assertResults(array(
'1',
));
$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',
));
$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',
));
$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',
));
$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',
));
$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',
));
}
public function testStringIdConditions() {
$entity = entity_create('config_query_test', array(
'label' => $this
->randomMachineName(),
'id' => 'foo.bar',
));
$this->entities[] = $entity;
$entity
->enforceIsNew();
$entity
->save();
$this->queryResults = $this->factory
->get('config_query_test')
->condition('id', 'foo.bar', 'STARTS_WITH')
->execute();
$this
->assertResults(array(
'foo.bar',
));
$this->queryResults = $this->factory
->get('config_query_test')
->condition('id', 'f', 'STARTS_WITH')
->execute();
$this
->assertResults(array(
'foo.bar',
));
$this->queryResults = $this->factory
->get('config_query_test')
->condition('id', 'miss', 'STARTS_WITH')
->execute();
$this
->assertResults(array());
$this->queryResults = $this->factory
->get('config_query_test')
->condition('id', 'foo.bar', 'CONTAINS')
->execute();
$this
->assertResults(array(
'foo.bar',
));
$this->queryResults = $this->factory
->get('config_query_test')
->condition('id', 'oo.ba', 'CONTAINS')
->execute();
$this
->assertResults(array(
'foo.bar',
));
$this->queryResults = $this->factory
->get('config_query_test')
->condition('id', 'miss', 'CONTAINS')
->execute();
$this
->assertResults(array());
$this->queryResults = $this->factory
->get('config_query_test')
->condition('id', 'foo.bar', 'ENDS_WITH')
->execute();
$this
->assertResults(array(
'foo.bar',
));
$this->queryResults = $this->factory
->get('config_query_test')
->condition('id', 'r', 'ENDS_WITH')
->execute();
$this
->assertResults(array(
'foo.bar',
));
$this->queryResults = $this->factory
->get('config_query_test')
->condition('id', 'miss', 'ENDS_WITH')
->execute();
$this
->assertResults(array());
}
public function testCount() {
$count = $this->factory
->get('config_query_test')
->count()
->execute();
$this
->assertIdentical($count, count($this->entities));
$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);
$count = $query
->condition($and_condition_1)
->condition($and_condition_2)
->count()
->execute();
$this
->assertIdentical($count, 2);
}
public function testSortRange() {
$this->queryResults = $this->factory
->get('config_query_test')
->sort('number', 'DESC')
->execute();
$this
->assertIdentical(array_values($this->queryResults), array(
'3',
'5',
'2',
'1',
'4',
));
$this->queryResults = $this->factory
->get('config_query_test')
->sort('number', 'ASC')
->execute();
$this
->assertIdentical(array_values($this->queryResults), array(
'4',
'1',
'2',
'5',
'3',
));
$this->queryResults = $this->factory
->get('config_query_test')
->condition('id', '3', '>')
->sort('number', 'DESC')
->execute();
$this
->assertIdentical(array_values($this->queryResults), array(
'5',
'4',
));
$this->queryResults = $this->factory
->get('config_query_test')
->condition('id', '3', '>')
->sort('number', 'ASC')
->execute();
$this
->assertIdentical(array_values($this->queryResults), array(
'4',
'5',
));
$this->queryResults = $this->factory
->get('config_query_test')
->sort('number', 'DESC')
->range('2', '2')
->execute();
$this
->assertIdentical(array_values($this->queryResults), array(
'2',
'1',
));
$this->queryResults = $this->factory
->get('config_query_test')
->sort('number', 'ASC')
->range('2', '2')
->execute();
$this
->assertIdentical(array_values($this->queryResults), array(
'2',
'5',
));
$this->queryResults = $this->factory
->get('config_query_test')
->range(0, '3')
->sort('id', 'ASC')
->execute();
$this
->assertIdentical(array_values($this->queryResults), array(
'1',
'2',
'3',
));
$this->queryResults = $this->factory
->get('config_query_test')
->pager('4', 0)
->sort('id', 'ASC')
->execute();
$this
->assertIdentical(array_values($this->queryResults), array(
'1',
'2',
'3',
'4',
));
}
public function testTableSort() {
$header = array(
array(
'data' => t('ID'),
'specifier' => 'id',
),
array(
'data' => t('Number'),
'specifier' => 'number',
),
);
$this->queryResults = $this->factory
->get('config_query_test')
->tableSort($header)
->sort('id', 'DESC')
->execute();
$this
->assertIdentical(array_values($this->queryResults), array(
'5',
'4',
'3',
'2',
'1',
));
$this->queryResults = $this->factory
->get('config_query_test')
->tableSort($header)
->sort('id', 'ASC')
->execute();
$this
->assertIdentical(array_values($this->queryResults), array(
'1',
'2',
'3',
'4',
'5',
));
$this->queryResults = $this->factory
->get('config_query_test')
->tableSort($header)
->sort('id', 'desc')
->execute();
$this
->assertIdentical(array_values($this->queryResults), array(
'5',
'4',
'3',
'2',
'1',
));
$this->queryResults = $this->factory
->get('config_query_test')
->tableSort($header)
->sort('id', 'asc')
->execute();
$this
->assertIdentical(array_values($this->queryResults), array(
'1',
'2',
'3',
'4',
'5',
));
$this->queryResults = $this->factory
->get('config_query_test')
->tableSort($header)
->sort('number', 'DeSc')
->execute();
$this
->assertIdentical(array_values($this->queryResults), array(
'3',
'5',
'2',
'1',
'4',
));
$this->queryResults = $this->factory
->get('config_query_test')
->tableSort($header)
->sort('number', 'AsC')
->execute();
$this
->assertIdentical(array_values($this->queryResults), array(
'4',
'1',
'2',
'5',
'3',
));
$this->queryResults = $this->factory
->get('config_query_test')
->tableSort($header)
->sort('number', 'dEsC')
->execute();
$this
->assertIdentical(array_values($this->queryResults), array(
'3',
'5',
'2',
'1',
'4',
));
$this->queryResults = $this->factory
->get('config_query_test')
->tableSort($header)
->sort('number', 'aSc')
->execute();
$this
->assertIdentical(array_values($this->queryResults), array(
'4',
'1',
'2',
'5',
'3',
));
}
public function testDotted() {
$this->queryResults = $this->factory
->get('config_query_test')
->condition('array.level1.*', 1)
->execute();
$this
->assertResults(array(
'1',
'3',
));
$this->queryResults = $this->factory
->get('config_query_test')
->condition('*.level1.level2', 2)
->execute();
$this
->assertResults(array(
'2',
'4',
));
$this->queryResults = $this->factory
->get('config_query_test')
->condition('array.level1.*', 3)
->execute();
$this
->assertResults(array(
'5',
));
$this->queryResults = $this->factory
->get('config_query_test')
->condition('array.level1.level2', 3)
->execute();
$this
->assertResults(array(
'5',
));
$this->queryResults = $this->factory
->get('config_query_test')
->condition('*.level1.level2', 41)
->execute();
$this
->assertResults(array());
}
public function testCaseSensitivity() {
$this->queryResults = $this->factory
->get('config_query_test')
->condition('label', 'TEST', 'CONTAINS')
->execute();
$this
->assertResults(array(
'3',
'4',
'5',
));
$this->queryResults = $this->factory
->get('config_query_test')
->condition('label', 'test', 'CONTAINS')
->execute();
$this
->assertResults(array(
'3',
'4',
'5',
));
}
public function testLookupKeys() {
\Drupal::service('state')
->set('config_test.lookup_keys', TRUE);
\Drupal::entityManager()
->clearCachedDefinitions();
$key_value = $this->container
->get('keyvalue')
->get(QueryFactory::CONFIG_LOOKUP_PREFIX . 'config_test');
$test_entities = [];
$entity = entity_create('config_test', array(
'label' => $this
->randomMachineName(),
'id' => '1',
'style' => 'test',
));
$test_entities[$entity
->getConfigDependencyName()] = $entity;
$entity
->enforceIsNew();
$entity
->save();
$expected[] = $entity
->getConfigDependencyName();
$this
->assertEqual($expected, $key_value
->get('style:test'));
$entity = entity_create('config_test', array(
'label' => $this
->randomMachineName(),
'id' => '2',
'style' => 'test',
));
$test_entities[$entity
->getConfigDependencyName()] = $entity;
$entity
->enforceIsNew();
$entity
->save();
$expected[] = $entity
->getConfigDependencyName();
$this
->assertEqual($expected, $key_value
->get('style:test'));
$entity = entity_create('config_test', array(
'label' => $this
->randomMachineName(),
'id' => '3',
'style' => 'blah',
));
$entity
->enforceIsNew();
$entity
->save();
$this
->assertEqual($expected, $key_value
->get('style:test'));
$this
->assertEqual([
$entity
->getConfigDependencyName(),
], $key_value
->get('style:blah'));
$entity
->delete();
$this
->assertEqual([], $key_value
->get('style:blah'));
$entity_id = array_pop($expected);
$test_entities[$entity_id]
->delete();
$this
->assertEqual($expected, $key_value
->get('style:test'));
$entity_id = array_pop($expected);
$test_entities[$entity_id]
->delete();
$this
->assertEqual($expected, $key_value
->get('style:test'));
}
protected function assertResults($expected) {
$this
->assertIdentical(count($this->queryResults), count($expected));
foreach ($expected as $value) {
$this
->assertIdentical($this->queryResults[$value], $value);
}
}
}