View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\field_ui\Functional;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\field_ui\Traits\FieldUiTestTrait;
use Drupal\user\Entity\User;
class ManageFieldsTest extends BrowserTestBase {
use FieldUiTestTrait;
protected static $modules = [
'field_test',
'field_ui',
'field_ui_test',
'node',
'text',
];
protected $defaultTheme = 'stark';
protected $adminUser;
protected function setUp() : void {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'administer node fields',
]);
$this
->drupalLogin($this->adminUser);
$this
->config('system.logging')
->set('error_level', ERROR_REPORTING_DISPLAY_ALL)
->save();
}
public function testFieldDropButtonOperations() {
$assert_session = $this
->assertSession();
$node_type = $this
->drupalCreateContentType();
$bundle = $node_type
->id();
$storage = $this->container
->get('entity_type.manager')
->getStorage('field_storage_config')
->create([
'type' => 'string',
'field_name' => 'highlander',
'entity_type' => 'node',
]);
$storage
->save();
$this->container
->get('entity_type.manager')
->getStorage('field_config')
->create([
'field_storage' => $storage,
'bundle' => $bundle,
])
->save();
$this
->drupalGet("/admin/structure/types/manage/{$bundle}/fields");
$element = $assert_session
->elementExists('css', '#highlander');
$summary = $assert_session
->elementExists('css', '.field-settings-summary-cell > ul > li', $element);
$field_label = $this->container
->get('plugin.manager.field.field_type')
->getDefinitions()['string']['label'];
$this
->assertEquals($field_label, $summary
->getText());
$storage = $this->container
->get('entity_type.manager')
->getStorage('field_storage_config')
->create([
'type' => 'entity_reference',
'field_name' => 'downlander',
'entity_type' => 'node',
'settings' => [
'target_type' => 'node',
],
]);
$storage
->save();
$this->container
->get('entity_type.manager')
->getStorage('field_config')
->create([
'field_storage' => $storage,
'bundle' => $bundle,
'entity_type' => 'node',
'settings' => [
'handler_settings' => [
'target_bundles' => [
$bundle => $bundle,
],
],
],
])
->save();
$this
->drupalGet("/admin/structure/types/manage/{$bundle}/fields");
$element = $assert_session
->elementExists('css', '#downlander');
$custom_summary_text = 'Reference type: Content';
$allowed_bundles_text = "Content type: {$bundle}";
$this
->assertStringContainsString($custom_summary_text, $element
->getText());
$this
->assertStringContainsString($allowed_bundles_text, $element
->getText());
}
public function testAddField() {
$page = $this
->getSession()
->getPage();
$type = $this
->drupalCreateContentType([
'name' => 'Article',
'type' => 'article',
]);
$this
->drupalGet('/admin/structure/types/manage/' . $type
->id() . '/fields/add-field');
$edit = [
'new_storage_type' => 'field_test_descriptions',
];
$this
->submitForm($edit, 'Continue');
$this
->assertSession()
->pageTextContains('This one-line field description is important for testing');
$this
->assertSession()
->pageTextContains('This multiple line description needs to use an array');
$this
->assertSession()
->pageTextContains('This second line contains important information');
$this
->fieldUIAddNewField('admin/structure/types/manage/' . $type
->id(), 'test_field', 'Test field', 'test_field', [], [], FALSE);
$this
->assertNull(FieldStorageConfig::loadByName('node', "field_test_field"));
$this
->drupalGet('/admin/structure/types/manage/' . $type
->id() . '/fields/add-field');
$edit = [
'new_storage_type' => 'test_field',
];
$this
->submitForm($edit, 'Continue');
$edit = [
'label' => 'Test field',
'field_name' => 'test_field',
];
$this
->submitForm($edit, 'Continue');
$this
->assertSession()
->statusMessageNotContains('Saved');
$edit = [
'field_storage[subform][cardinality_number]' => 5,
];
$this
->submitForm($edit, 'Update settings');
$this
->assertSession()
->statusMessageNotContains('Saved');
$this
->assertEquals(5, $page
->findField('field_storage[subform][cardinality_number]')
->getValue());
$this
->drupalGet('/admin/structure/types/manage/' . $type
->id() . '/fields/add-field');
$edit = [
'new_storage_type' => 'test_field',
];
$this
->submitForm($edit, 'Continue');
$edit = [
'label' => 'Test field',
'field_name' => 'test_field',
];
$this
->submitForm($edit, 'Continue');
$this
->assertEquals(1, $page
->findField('field_storage[subform][cardinality_number]')
->getValue());
$this
->submitForm([], 'Update settings');
$this
->assertSession()
->statusMessageNotContains('Saved');
$this
->submitForm([], 'Save settings');
$this
->assertSession()
->statusMessageContains('Saved');
$this
->assertEquals(1, FieldStorageConfig::loadByName('node', 'field_test_field')
->getCardinality());
}
public function testAddFieldWithMultipleUsers() {
$page = $this
->getSession()
->getPage();
$user1 = $this
->drupalCreateUser([
'administer node fields',
]);
$user2 = $this
->drupalCreateUser([
'administer node fields',
]);
$node_type = $this
->drupalCreateContentType();
$bundle_path = '/admin/structure/types/manage/' . $node_type
->id();
$this
->drupalLogin($user1);
$this
->drupalGet($bundle_path . '/fields/add-field');
$edit = [
'new_storage_type' => 'test_field',
];
$this
->submitForm($edit, 'Continue');
$edit = [
'label' => 'Test field',
'field_name' => 'test_field',
];
$this
->submitForm($edit, 'Continue');
$edit = [
'field_storage[subform][cardinality_number]' => 5,
];
$storage_form_url = $this
->getUrl();
$this
->submitForm($edit, 'Update settings');
$this
->drupalLogout();
$this
->drupalLogin($user2);
$this
->drupalGet($bundle_path . '/fields/add-field');
$edit = [
'new_storage_type' => 'test_field',
];
$this
->submitForm($edit, 'Continue');
$edit = [
'label' => 'Test field',
'field_name' => 'test_field',
];
$this
->submitForm($edit, 'Continue');
$allowed_no_of_values = $page
->findField('field_storage[subform][cardinality_number]')
->getValue();
$this
->assertEquals(1, $allowed_no_of_values);
$this
->submitForm([
'field_storage[subform][cardinality_number]' => 2,
], 'Update settings');
$this
->submitForm([], 'Save settings');
$this
->assertSession()
->pageTextContains("Saved Test field configuration.");
$this
->drupalLogout();
$this
->drupalLogin($user1);
$this
->drupalGet($storage_form_url);
$this
->assertSession()
->pageTextNotContains('error');
$this
->submitForm([], 'Save settings');
$this
->assertSession()
->statusMessageContains("An error occurred while saving the field: 'field_storage_config' entity with ID 'node.field_test_field' already exists.");
}
public function testEditFieldWithLeftOverFieldInTempStore() {
$user = $this
->drupalCreateUser([
'administer node fields',
]);
$node_type = $this
->drupalCreateContentType();
$bundle_path = '/admin/structure/types/manage/' . $node_type
->id();
$this
->drupalLogin($user);
$this
->drupalGet($bundle_path . '/fields/add-field');
$edit = [
'new_storage_type' => 'test_field',
];
$this
->submitForm($edit, 'Continue');
$edit = [
'label' => 'Test field',
'field_name' => 'test_field',
];
$this
->submitForm($edit, 'Continue');
$storage = $this->container
->get('entity_type.manager')
->getStorage('field_storage_config')
->create([
'type' => 'test_field',
'field_name' => 'test_field',
'entity_type' => 'node',
]);
$storage
->save();
$this->container
->get('entity_type.manager')
->getStorage('field_config')
->create([
'field_storage' => $storage,
'bundle' => $node_type
->id(),
'entity_type' => 'node',
])
->save();
$this
->drupalGet("{$bundle_path}/fields/node.{$node_type->id()}.test_field");
$this
->submitForm([], 'Save settings');
$this
->assertSession()
->statusMessageContains('Saved test_field configuration.', 'status');
}
public function testEntityReferenceToNonBundleableEntity() {
$type = $this
->drupalCreateContentType([
'name' => 'kittens',
'type' => 'kittens',
]);
$bundle_path = 'admin/structure/types/manage/' . $type
->id();
$field_name = 'field_user_reference';
$field_edit = [
'set_default_value' => '1',
"default_value_input[{$field_name}][0][target_id]" => $this->adminUser
->label() . ' (' . $this->adminUser
->id() . ')',
];
$this
->fieldUIAddNewField($bundle_path, 'user_reference', NULL, 'field_ui:entity_reference:user', [], $field_edit);
$field = FieldConfig::loadByName('node', 'kittens', $field_name);
$this
->assertEquals([
[
'target_id' => $this->adminUser
->id(),
],
], $field
->getDefaultValue(User::create([
'name' => '1337',
])));
}
public function testFieldStorageFormAlter() {
$this->container
->get('module_installer')
->install([
'field_ui_test_deprecated',
]);
$this
->rebuildContainer();
$node_type = $this
->drupalCreateContentType();
$bundle = $node_type
->id();
$this
->expectDeprecation('The deprecated alter hook hook_form_field_storage_config_edit_form_alter() is implemented in these functions: field_ui_test_deprecated_form_field_storage_config_edit_form_alter. Use hook_form_field_config_edit_form_alter() instead. See https://www.drupal.org/node/3386675.');
$this
->drupalGet("/admin/structure/types/manage/{$bundle}/fields/node.{$bundle}.body");
$this
->assertSession()
->elementTextContains('css', '#edit-field-storage', 'Greetings from the field_storage_config_edit_form() alter.');
}
public function testFieldTypeCardinalityAlter() {
$node_type = $this
->drupalCreateContentType();
$bundle = $node_type
->id();
$storage = $this->container
->get('entity_type.manager')
->getStorage('field_storage_config')
->create([
'type' => 'test_field',
'field_name' => 'field_test_field',
'entity_type' => 'node',
]);
$storage
->save();
$this->container
->get('entity_type.manager')
->getStorage('field_config')
->create([
'field_storage' => $storage,
'bundle' => $bundle,
'entity_type' => 'node',
])
->save();
$this
->drupalGet("/admin/structure/types/manage/{$bundle}/fields/node.{$bundle}.field_test_field");
$this
->assertSession()
->elementTextContains('css', '#edit-field-storage', 'Greetings from Drupal\\field_test\\Plugin\\Field\\FieldType\\TestItem::storageSettingsForm');
}
public function testFieldUiDefinitionsAlter() {
$user = $this
->drupalCreateUser([
'administer node fields',
]);
$node_type = $this
->drupalCreateContentType();
$this
->drupalLogin($user);
$this
->drupalGet('/admin/structure/types/manage/' . $node_type
->id() . '/fields/add-field');
$this
->assertSession()
->pageTextContains('Boolean (overridden by alter)');
}
public function testFieldCategoryFallbackWithoutDescription() {
$user = $this
->drupalCreateUser([
'administer node fields',
]);
$node_type = $this
->drupalCreateContentType();
$this
->drupalLogin($user);
$this
->drupalGet('/admin/structure/types/manage/' . $node_type
->id() . '/fields/add-field');
$field_type = $this
->assertSession()
->elementExists('xpath', '//label[text()="Test field"]');
$description_container = $field_type
->getParent()
->find('css', '.field-option__description');
$this
->assertNotNull($description_container);
$this
->assertEquals('', $description_container
->getText());
}
}