function EntityQueryTest::testDelta
Same name in other branches
- 9 core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php \Drupal\KernelTests\Core\Entity\EntityQueryTest::testDelta()
- 10 core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php \Drupal\KernelTests\Core\Entity\EntityQueryTest::testDelta()
- 11.x core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php \Drupal\KernelTests\Core\Entity\EntityQueryTest::testDelta()
Test queries with delta conditions.
File
-
core/
tests/ Drupal/ KernelTests/ Core/ Entity/ EntityQueryTest.php, line 599
Class
- EntityQueryTest
- Tests Entity Query functionality.
Namespace
Drupal\KernelTests\Core\EntityCode
public function testDelta() {
$figures = $this->figures;
// Test numeric delta value in field condition.
$this->queryResults = $this->storage
->getQuery()
->condition("{$figures}.0.color", 'red')
->sort('id')
->execute();
// As unit 0 at delta 0 was the red triangle bit 0 needs to be set.
$this->assertResult(1, 3, 5, 7, 9, 11, 13, 15);
$this->queryResults = $this->storage
->getQuery()
->condition("{$figures}.1.color", 'red')
->sort('id')
->execute();
// Delta 1 is not red.
$this->assertResult();
// Test on two different deltas.
$query = $this->storage
->getQuery();
$or = $query->andConditionGroup()
->condition("{$figures}.0.color", 'red')
->condition("{$figures}.1.color", 'blue');
$this->queryResults = $query->condition($or)
->sort('id')
->execute();
$this->assertResult(3, 7, 11, 15);
// Test the delta range condition.
$this->queryResults = $this->storage
->getQuery()
->condition("{$figures}.%delta.color", [
'blue',
'red',
], 'IN')
->condition("{$figures}.%delta", [
0,
1,
], 'IN')
->sort('id')
->execute();
// Figure delta 0 or 1 can be blue or red, this matches a lot of entities.
$this->assertResult(1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 14, 15);
// Test the delta range condition without conditions on the value.
$this->queryResults = $this->storage
->getQuery()
->condition("{$figures}.%delta", 1)
->sort('id')
->execute();
// Entity needs to have at least two figures.
$this->assertResult(3, 7, 11, 15);
// Numeric delta on single value base field should return results only if
// the first item is being targeted.
$this->queryResults = $this->storage
->getQuery()
->condition("id.0.value", [
1,
3,
5,
], 'IN')
->sort('id')
->execute();
$this->assertResult(1, 3, 5);
$this->queryResults = $this->storage
->getQuery()
->condition("id.1.value", [
1,
3,
5,
], 'IN')
->sort('id')
->execute();
$this->assertResult();
// Delta range condition on single value base field should return results
// only if just the field value is targeted.
$this->queryResults = $this->storage
->getQuery()
->condition("id.%delta.value", [
1,
3,
5,
], 'IN')
->sort('id')
->execute();
$this->assertResult(1, 3, 5);
$this->queryResults = $this->storage
->getQuery()
->condition("id.%delta.value", [
1,
3,
5,
], 'IN')
->condition("id.%delta", 0, '=')
->sort('id')
->execute();
$this->assertResult(1, 3, 5);
$this->queryResults = $this->storage
->getQuery()
->condition("id.%delta.value", [
1,
3,
5,
], 'IN')
->condition("id.%delta", 1, '=')
->sort('id')
->execute();
$this->assertResult();
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.