View source
<?php
namespace Drupal\Tests\taxonomy_entity_index\Kernel;
use Drupal\Component\Utility\Unicode;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
abstract class TaxonomyEntityIndexKernelTestBase extends KernelTestBase {
use TaxonomyTestTrait;
protected $entityTypes = [
'entity_test',
];
protected static $modules = [
'system',
'user',
'taxonomy_entity_index',
'taxonomy',
'text',
'entity_test',
'field',
];
protected function setUp() {
parent::setUp();
foreach (array_merge($this->entityTypes, [
'taxonomy_term',
'user',
]) as $entity_type_id) {
$this
->installEntitySchema($entity_type_id);
}
$this
->installConfig('taxonomy_entity_index');
$this
->installSchema('system', 'sequences');
$this
->installSchema('taxonomy_entity_index', 'taxonomy_entity_index');
$this
->config('taxonomy_entity_index.settings')
->set('types', $this->entityTypes)
->save();
foreach ($this->entityTypes as $entity_type_id) {
foreach ($this
->createBundles($entity_type_id) as $bundle) {
$this
->setupTermField($entity_type_id, $bundle, 'termz');
}
}
}
protected abstract function createBundles($entity_type_id);
protected function setupTermField($entity_type_id, $bundle, $field_name) {
if (!FieldStorageConfig::load("{$entity_type_id}.{$field_name}")) {
$storage = FieldStorageConfig::create([
'entity_type' => $entity_type_id,
'field_name' => $field_name,
'id' => "{$entity_type_id}.{$field_name}",
'type' => 'entity_reference',
'settings' => [
'target_type' => 'taxonomy_term',
],
]);
$storage
->save();
}
if (!FieldConfig::load("{$entity_type_id}.{$bundle}.{$field_name}")) {
$config = FieldConfig::create([
'field_name' => $field_name,
'entity_type' => $entity_type_id,
'bundle' => $bundle,
'id' => "{$entity_type_id}.{$bundle}.{$field_name}",
'label' => Unicode::ucfirst($field_name),
]);
$config
->save();
}
}
}