View source
<?php
namespace Drupal\Tests\dynamic_entity_reference\Functional;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\node\Entity\Node;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\SchemaCheckTestTrait;
class DynamicEntityReferenceFieldDefaultValueTest extends BrowserTestBase {
use SchemaCheckTestTrait;
protected $adminUser;
protected static $modules = [
'dynamic_entity_reference',
'field_ui',
'node',
];
protected $permissions = [
'access content',
'administer content types',
'administer node fields',
'administer node form display',
'bypass node access',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'reference_content',
]);
$this
->drupalCreateContentType([
'type' => 'referenced_content',
]);
$this->adminUser = $this
->drupalCreateUser($this->permissions);
$this
->drupalLogin($this->adminUser);
}
public function testEntityReferenceDefaultValue() {
$assert_session = $this
->assertSession();
$referenced_node = $this
->drupalCreateNode([
'type' => 'referenced_content',
]);
$field_name = mb_strtolower($this
->randomMachineName());
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'node',
'type' => 'dynamic_entity_reference',
'settings' => [
'exclude_entity_types' => FALSE,
'entity_type_ids' => [
'node',
'user',
],
],
]);
$field_storage
->save();
FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'reference_content',
'settings' => [
'node' => [
'handler' => 'default',
'handler_settings' => [
'target_bundles' => [
'referenced_content',
],
'sort' => [
'field' => '_none',
],
],
],
'user' => [
'handler' => 'default:user',
'handler_settings' => [
'target_bundles' => NULL,
],
],
],
])
->save();
$field_edit = [
'default_value_input[' . $field_name . '][0][target_type]' => $referenced_node
->getEntityTypeId(),
'default_value_input[' . $field_name . '][0][target_id]' => $referenced_node
->getTitle() . ' (' . $referenced_node
->id() . ')',
];
$this
->drupalGet('admin/structure/types/manage/reference_content/fields/node.reference_content.' . $field_name);
$this
->submitForm($field_edit, t('Save settings'));
$this
->drupalGet('admin/structure/types/manage/reference_content/fields/node.reference_content.' . $field_name);
$assert_session
->optionExists("default_value_input[{$field_name}][0][target_type]", $referenced_node
->getEntityTypeId());
$assert_session
->responseContains('name="default_value_input[' . $field_name . '][0][target_id]" value="' . $referenced_node
->getTitle() . ' (' . $referenced_node
->id() . ')');
$config_entity = $this
->config('field.field.node.reference_content.' . $field_name)
->get();
$this
->assertTrue(isset($config_entity['default_value'][0]['target_uuid']), 'Default value contains target_uuid property');
$this
->assertEquals($config_entity['default_value'][0]['target_uuid'], $referenced_node
->uuid(), 'Content uuid and config entity uuid are the same');
$this
->assertEquals([
$referenced_node
->getConfigDependencyName(),
], $config_entity['dependencies']['content']);
\Drupal::service('entity_field.manager')
->clearCachedFieldDefinitions();
$new_node = Node::create([
'type' => 'reference_content',
]);
$this
->assertEquals($new_node
->get($field_name)
->offsetGet(0)->target_type, $referenced_node
->getEntityTypeId());
$this
->assertEquals($new_node
->get($field_name)
->offsetGet(0)->target_id, $referenced_node
->id());
$field_config = $this
->config('field.field.node.reference_content.' . $field_name);
$this
->assertConfigSchema(\Drupal::service('config.typed'), $field_config
->getName(), $field_config
->get());
$field_storage_config = $this
->config('field.storage.node.' . $field_name);
$this
->assertConfigSchema(\Drupal::service('config.typed'), $field_storage_config
->getName(), $field_storage_config
->get());
}
}