function FieldEntitySettingsTest::testFieldEntitiesCarryDefaultSettings

Same name in other branches
  1. 10 core/modules/field/tests/src/Kernel/Entity/FieldEntitySettingsTest.php \Drupal\Tests\field\Kernel\Entity\FieldEntitySettingsTest::testFieldEntitiesCarryDefaultSettings()

@group legacy

File

core/modules/field/tests/src/Kernel/Entity/FieldEntitySettingsTest.php, line 35

Class

FieldEntitySettingsTest
Tests the ways that field entities handle their settings.

Namespace

Drupal\Tests\field\Kernel\Entity

Code

public function testFieldEntitiesCarryDefaultSettings() : void {
    
    /** @var \Drupal\field\FieldStorageConfigInterface $field_storage */
    $field_storage = FieldStorageConfig::create([
        'type' => 'integer',
        'entity_type' => 'entity_test_with_bundle',
        'field_name' => 'test',
    ]);
    $field = FieldConfig::create([
        'field_storage' => $field_storage,
        'bundle' => 'test',
    ]);
    
    /** @var \Drupal\Core\Field\FieldTypePluginManagerInterface $plugin_manager */
    $plugin_manager = $this->container
        ->get('plugin.manager.field.field_type');
    $default_storage_settings = $plugin_manager->getDefaultStorageSettings('integer');
    $default_field_settings = $plugin_manager->getDefaultFieldSettings('integer');
    // Both entities should have the complete, default settings for their
    // field type.
    $this->assertSame($default_storage_settings, $field_storage->get('settings'));
    $this->assertSame($default_field_settings, $field->get('settings'));
    // If we try to set incomplete settings, the existing values should be
    // retained.
    $storage_settings = $field_storage->setSettings([
        'size' => 'big',
    ])
        ->get('settings');
    // There should be no missing settings.
    $missing_storage_settings = array_diff_key($default_storage_settings, $storage_settings);
    $this->assertEmpty($missing_storage_settings);
    // The value we set should be remembered.
    $this->assertSame('big', $storage_settings['size']);
    $field_settings = $field->setSetting('min', 10)
        ->getSettings();
    $missing_field_settings = array_diff_key($default_field_settings, $field_settings);
    $this->assertEmpty($missing_field_settings);
    $this->assertSame(10, $field_settings['min']);
    $field_settings = $field->setSettings([
        'max' => 39,
    ])
        ->get('settings');
    $missing_field_settings = array_diff_key($default_field_settings, $field_settings);
    $this->assertEmpty($missing_field_settings);
    $this->assertSame(39, $field_settings['max']);
    // Test that saving settings with incomplete settings is not triggering
    // error, and values are retained.
    $field_storage->save();
    $missing_storage_settings = array_diff_key($default_storage_settings, $storage_settings);
    $this->assertEmpty($missing_storage_settings);
    // The value we set should be remembered.
    $this->assertSame('big', $storage_settings['size']);
    $field->save();
    $missing_field_settings = array_diff_key($default_field_settings, $field_settings);
    $this->assertEmpty($missing_field_settings);
    $this->assertSame(39, $field_settings['max']);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.