View source
<?php
namespace Drupal\Tests\taxonomy_entity_index\Kernel;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\taxonomy\TermInterface;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
class TaxonomyEntityIndexEntityTest extends TaxonomyEntityIndexKernelTestBase {
use ContentTypeCreationTrait;
use NodeCreationTrait;
protected $entityTypes = [
'entity_test',
];
protected $database;
protected static $modules = [
'node',
'text',
'filter',
'options',
];
protected function setUp() {
parent::setUp();
$this
->installConfig([
'node',
'filter',
]);
$this
->installEntitySchema('node');
$this
->createContentType([
'type' => 'page',
]);
$this
->setupTermField('node', 'page', 'termz');
$this->database = \Drupal::service('database');
}
protected function createBundles($entity_type_id) {
entity_test_create_bundle('termz');
return [
'termz',
];
}
public function testWritingToIndexTable() {
$vocab = $this
->createVocabulary();
$term1 = $this
->createTerm($vocab);
$term2 = $this
->createTerm($vocab);
$term3 = $this
->createTerm($vocab);
$term4 = $this
->createTerm($vocab);
$this
->assertThatDisabledEntityTypesDoNotWriteToTheIndex([
$term1,
$term2,
]);
$entity = $this
->assertThatEntityInsertWritesToTheIndex([
$term1,
$term2,
$term3,
]);
$this
->assertThatEntityInsertWritesToTheIndex([
$term1,
]);
$entity = $this
->assertThatEntityUpdatesModifyTheIndex($entity, [
$term1,
$term2,
$term4,
]);
$this
->assertThatTermDeletionUpdatesTheIndex($entity, [
$term1,
$term2,
], [
$term4,
]);
$this
->assertThatEntityDeletionUpdatesTheIndex($entity);
}
protected function assertThatDisabledEntityTypesDoNotWriteToTheIndex(array $terms) {
$this
->createNode([
'type' => 'page',
'termz' => $terms,
]);
$this
->assertCount(0, $this->database
->select('taxonomy_entity_index', 'tei')
->fields('tei')
->execute()
->fetchAll());
}
protected function assertThatEntityInsertWritesToTheIndex(array $terms) {
$entity = EntityTest::create([
'type' => 'termz',
'termz' => $terms,
]);
$entity
->save();
$this
->assertCount(count($terms), $this->database
->select('taxonomy_entity_index', 'tei')
->fields('tei')
->condition('entity_id', $entity
->id())
->condition('entity_type', $entity
->getEntityTypeId())
->execute()
->fetchAll());
return $entity;
}
protected function assertThatEntityUpdatesModifyTheIndex(EntityTest $entity, array $terms) {
$entity->termz = $terms;
$entity
->save();
$records = $this->database
->select('taxonomy_entity_index', 'tei')
->fields('tei')
->condition('entity_id', $entity
->id())
->condition('entity_type', $entity
->getEntityTypeId())
->execute()
->fetchAll();
$this
->assertCount(count($terms), $records);
$delta = 0;
$this
->assertEquals(array_map(function (TermInterface $term) use ($entity, &$delta) {
return (object) [
'entity_type' => 'entity_test',
'bundle' => 'termz',
'entity_id' => $entity
->id(),
'revision_id' => $entity
->id(),
'field_name' => 'termz',
'delta' => $delta++,
'tid' => $term
->id(),
];
}, $terms), $records);
return $entity;
}
protected function assertThatTermDeletionUpdatesTheIndex(EntityTest $entity, array $expected_terms, array $terms_to_delete) {
foreach ($terms_to_delete as $term) {
$term
->delete();
}
$records = $this->database
->select('taxonomy_entity_index', 'tei')
->fields('tei')
->condition('entity_id', $entity
->id())
->condition('entity_type', $entity
->getEntityTypeId())
->execute()
->fetchAll();
$this
->assertCount(count($expected_terms), $records);
$delta = 0;
$this
->assertEquals(array_map(function (TermInterface $term) use ($entity, &$delta) {
return (object) [
'entity_type' => 'entity_test',
'bundle' => 'termz',
'entity_id' => $entity
->id(),
'revision_id' => $entity
->id(),
'field_name' => 'termz',
'delta' => $delta++,
'tid' => $term
->id(),
];
}, $expected_terms), $records);
}
protected function assertThatEntityDeletionUpdatesTheIndex(EntityTest $entity) {
$entity
->delete();
$this
->assertCount(0, $this->database
->select('taxonomy_entity_index', 'tei')
->fields('tei')
->condition('entity_id', $entity
->id())
->condition('entity_type', $entity
->getEntityTypeId())
->execute()
->fetchAll());
$this
->assertGreaterThan(0, $this->database
->select('taxonomy_entity_index', 'tei')
->fields('tei')
->condition('entity_id', $entity
->id(), '<>')
->condition('entity_type', $entity
->getEntityTypeId())
->execute()
->fetchAll());
}
}