View source
<?php
namespace Drupal\field\Tests\EntityReference;
use Drupal\Component\Utility\Unicode;
use Drupal\field\Entity\FieldConfig;
use Drupal\node\Entity\NodeType;
use Drupal\simpletest\KernelTestBase;
use Drupal\taxonomy\Entity\Vocabulary;
class EntityReferenceSettingsTest extends KernelTestBase {
use EntityReferenceTestTrait;
public static $modules = [
'node',
'taxonomy',
'field',
'user',
'text',
'entity_reference',
'entity_test',
];
protected $nodeType;
protected $vocabulary;
protected $customBundle;
protected function setUp() {
parent::setup();
$this
->installEntitySchema('node');
$this
->installEntitySchema('taxonomy_term');
$this
->installEntitySchema('entity_test');
$this->nodeType = NodeType::create([
'type' => Unicode::strtolower($this
->randomMachineName()),
'name' => $this
->randomString(),
]);
$this->nodeType
->save();
$this->vocabulary = Vocabulary::create([
'vid' => Unicode::strtolower($this
->randomMachineName()),
'name' => $this
->randomString(),
]);
$this->vocabulary
->save();
$this->customBundle = 'test_bundle_' . Unicode::strtolower($this
->randomMachineName());
entity_test_create_bundle($this->customBundle, NULL, 'entity_test');
}
public function testConfigTargetBundleDeletion() {
$name = Unicode::strtolower($this
->randomMachineName());
$label = $this
->randomString();
$vid = $this->vocabulary
->id();
$handler_settings = [
'target_bundles' => [
$vid => $vid,
],
];
$this
->createEntityReferenceField('node', $this->nodeType
->id(), $name, $label, 'taxonomy_term', 'default', $handler_settings);
$field_config = FieldConfig::loadByName('node', $this->nodeType
->id(), $name);
$actual_handler_settings = $field_config
->getSetting('handler_settings');
$this
->assertEqual($handler_settings, $actual_handler_settings);
$this->vocabulary
->delete();
$field_config = FieldConfig::loadByName('node', $this->nodeType
->id(), $name);
$handler_settings = $field_config
->getSetting('handler_settings');
$this
->assertTrue(empty($handler_settings['target_bundles']));
}
public function testCustomTargetBundleDeletion() {
$name = Unicode::strtolower($this
->randomMachineName());
$label = $this
->randomString();
$handler_settings = [
'target_bundles' => [
$this->customBundle => $this->customBundle,
],
];
$this
->createEntityReferenceField('node', $this->nodeType
->id(), $name, $label, 'entity_test', 'default', $handler_settings);
$field_config = FieldConfig::loadByName('node', $this->nodeType
->id(), $name);
$actual_handler_settings = $field_config
->getSetting('handler_settings');
$this
->assertEqual($handler_settings, $actual_handler_settings);
entity_test_delete_bundle($this->customBundle, 'entity_test');
$field_config = FieldConfig::loadByName('node', $this->nodeType
->id(), $name);
$handler_settings = $field_config
->getSetting('handler_settings');
$this
->assertTrue(empty($handler_settings['target_bundles']));
}
}