View source
<?php
namespace Drupal\KernelTests\Core\Entity;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\taxonomy\Entity\Term;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
class EntityQueryRelationshipTest extends EntityKernelTestBase {
use EntityReferenceTestTrait;
protected static $modules = [
'taxonomy',
];
protected $terms;
public $accounts;
protected $entities;
protected $fieldName;
protected $queryResults;
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('taxonomy_term');
$vocabulary = Vocabulary::create([
'vid' => mb_strtolower($this
->randomMachineName()),
]);
$vocabulary
->save();
entity_test_create_bundle('test_bundle');
$this->fieldName = strtolower($this
->randomMachineName());
$handler_settings = [
'target_bundles' => [
$vocabulary
->id() => $vocabulary
->id(),
],
'auto_create' => TRUE,
];
$this
->createEntityReferenceField('entity_test', 'test_bundle', $this->fieldName, NULL, 'taxonomy_term', 'default', $handler_settings);
for ($i = 0; $i <= 1; $i++) {
$term = Term::create([
'name' => $this
->randomMachineName(),
'vid' => $vocabulary
->id(),
]);
$term
->save();
$this->terms[] = $term;
$this->accounts[] = $this
->createUser();
}
for ($i = 0; $i <= 2; $i++) {
$entity = EntityTest::create([
'type' => 'test_bundle',
]);
$entity->name->value = $this
->randomMachineName();
$index = $i ? 1 : 0;
$entity->user_id->target_id = $this->accounts[$index]
->id();
$entity->{$this->fieldName}->target_id = $this->terms[$index]
->id();
$entity
->save();
$this->entities[] = $entity;
}
}
public function testQuery() {
$storage = $this->container
->get('entity_type.manager')
->getStorage('entity_test');
$this->queryResults = $storage
->getQuery()
->accessCheck(FALSE)
->condition("user_id.entity.name", $this->accounts[0]
->getAccountName())
->execute();
$this
->assertResults([
0,
]);
$this->queryResults = $storage
->getQuery()
->accessCheck(FALSE)
->condition("user_id.entity.name", $this->accounts[0]
->getAccountName(), '<>')
->execute();
$this
->assertResults([
1,
2,
]);
$this->queryResults = $storage
->getQuery()
->accessCheck(FALSE)
->exists("user_id.entity.name")
->execute();
$this
->assertResults([
0,
1,
2,
]);
$this->queryResults = $storage
->getQuery()
->accessCheck(FALSE)
->notExists("user_id.entity.name")
->execute();
$this
->assertCount(0, $this->queryResults);
$this->queryResults = $storage
->getQuery()
->accessCheck(FALSE)
->condition("{$this->fieldName}.entity.name", $this->terms[0]->name->value)
->execute();
$this
->assertResults([
0,
]);
$this->queryResults = $storage
->getQuery()
->accessCheck(FALSE)
->condition("{$this->fieldName}.target_id.entity.name", $this->terms[0]->name->value)
->execute();
$this
->assertResults([
0,
]);
$this->queryResults = $storage
->getQuery()
->accessCheck(FALSE)
->condition("{$this->fieldName}.entity.name", $this->terms[0]->name->value, '<>')
->execute();
$this
->assertResults([
1,
2,
]);
$this->queryResults = $storage
->getQuery()
->accessCheck(FALSE)
->condition("user_id.entity:user.name", $this->accounts[0]
->getAccountName())
->execute();
$this
->assertResults([
0,
]);
$this->queryResults = $storage
->getQuery()
->accessCheck(FALSE)
->condition("user_id.entity:user.name", $this->accounts[0]
->getAccountName(), '<>')
->execute();
$this
->assertResults([
1,
2,
]);
$this->queryResults = $storage
->getQuery()
->accessCheck(FALSE)
->exists("user_id.entity:user.name")
->execute();
$this
->assertResults([
0,
1,
2,
]);
$this->queryResults = $storage
->getQuery()
->accessCheck(FALSE)
->notExists("user_id.entity:user.name")
->execute();
$this
->assertCount(0, $this->queryResults);
$this->queryResults = $storage
->getQuery()
->accessCheck(FALSE)
->condition("{$this->fieldName}.entity:taxonomy_term.name", $this->terms[0]->name->value)
->execute();
$this
->assertResults([
0,
]);
$this->queryResults = $storage
->getQuery()
->accessCheck(FALSE)
->condition("{$this->fieldName}.target_id.entity:taxonomy_term.name", $this->terms[0]->name->value)
->execute();
$this
->assertResults([
0,
]);
$this->queryResults = $storage
->getQuery()
->accessCheck(FALSE)
->condition("{$this->fieldName}.entity:taxonomy_term.name", $this->terms[0]->name->value, '<>')
->execute();
$this
->assertResults([
1,
2,
]);
}
public function testInvalidSpecifier() {
$this
->expectException(PluginNotFoundException::class);
$this->container
->get('entity_type.manager')
->getStorage('taxonomy_term')
->getQuery()
->accessCheck(FALSE)
->condition('langcode.language.foo', 'bar')
->execute();
}
protected function assertResults(array $expected) : void {
$expected_count = count($expected);
$this
->assertCount($expected_count, $this->queryResults);
foreach ($expected as $key) {
$id = $this->entities[$key]
->id();
$this
->assertEquals($id, $this->queryResults[$id]);
}
}
}