EntityFormDisplayTest.php 10.1 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\field_ui\Tests\EntityFormDisplayTest.
6 7
 */

8
namespace Drupal\field_ui\Tests;
9

10
use Drupal\Core\Entity\Entity\EntityFormMode;
11
use Drupal\simpletest\KernelTestBase;
12 13

/**
14
 * Tests the entity display configuration entities.
15
 *
16
 * @group field_ui
17
 */
18
class EntityFormDisplayTest extends KernelTestBase {
19

20
  public static $modules = array('field_ui', 'field', 'entity_test', 'field_test', 'user', 'text');
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

  protected function setUp() {
    parent::setUp();
    $this->installConfig(array('field'));
  }

  /**
   * Tests entity_get_form_display().
   */
  public function testEntityGetFromDisplay() {
    // Check that entity_get_form_display() returns a fresh object when no
    // configuration entry exists.
    $form_display = entity_get_form_display('entity_test', 'entity_test', 'default');
    $this->assertTrue($form_display->isNew());

    // Add some components and save the display.
    $form_display->setComponent('component_1', array('weight' => 10))
      ->save();

    // Check that entity_get_form_display() returns the correct object.
    $form_display = entity_get_form_display('entity_test', 'entity_test', 'default');
    $this->assertFalse($form_display->isNew());
    $this->assertEqual($form_display->id, 'entity_test.entity_test.default');
    $this->assertEqual($form_display->getComponent('component_1'), array('weight' => 10));
  }

  /**
   * Tests the behavior of a field component within an EntityFormDisplay object.
   */
  public function testFieldComponent() {
51
    // Create a field storage and an instance.
52
    $field_name = 'test_field';
53
    $field_storage = entity_create('field_storage_config', array(
54 55
      'name' => $field_name,
      'entity_type' => 'entity_test',
56
      'type' => 'test_field'
57
    ));
58
    $field_storage->save();
59
    $instance = entity_create('field_instance_config', array(
60
      'field_storage' => $field_storage,
61
      'bundle' => 'entity_test',
62 63
    ));
    $instance->save();
64

65 66 67 68 69 70
    $form_display = entity_create('entity_form_display', array(
      'targetEntityType' => 'entity_test',
      'bundle' => 'entity_test',
      'mode' => 'default',
    ));

71
    // Check that providing no options results in default values being used.
72
    $form_display->setComponent($field_name);
73
    $field_type_info = \Drupal::service('plugin.manager.field.field_type')->getDefinition($field_storage->type);
74
    $default_widget = $field_type_info['default_widget'];
75
    $widget_settings = \Drupal::service('plugin.manager.field.widget')->getDefaultSettings($default_widget);
76 77 78
    $expected = array(
      'weight' => 0,
      'type' => $default_widget,
79
      'settings' => $widget_settings,
80
      'third_party_settings' => array(),
81
    );
82
    $this->assertEqual($form_display->getComponent($field_name), $expected);
83 84

    // Check that the getWidget() method returns the correct widget plugin.
85
    $widget = $form_display->getRenderer($field_name);
86
    $this->assertEqual($widget->getPluginId(), $default_widget);
87
    $this->assertEqual($widget->getSettings(), $widget_settings);
88 89 90 91 92

    // Check that the widget is statically persisted, by assigning an
    // arbitrary property and reading it back.
    $random_value = $this->randomString();
    $widget->randomValue = $random_value;
93
    $widget = $form_display->getRenderer($field_name);
94 95 96
    $this->assertEqual($widget->randomValue, $random_value);

    // Check that changing the definition creates a new widget.
97
    $form_display->setComponent($field_name, array(
98 99
      'type' => 'field_test_multiple',
    ));
100
    $widget = $form_display->getRenderer($field_name);
101 102 103 104 105 106
    $this->assertEqual($widget->getPluginId(), 'test_field_widget');
    $this->assertFalse(isset($widget->randomValue));

    // Check that specifying an unknown widget (e.g. case of a disabled module)
    // gets stored as is in the display, but results in the default widget being
    // used.
107
    $form_display->setComponent($field_name, array(
108 109
      'type' => 'unknown_widget',
    ));
110
    $options = $form_display->getComponent($field_name);
111
    $this->assertEqual($options['type'], 'unknown_widget');
112
    $widget = $form_display->getRenderer($field_name);
113 114 115
    $this->assertEqual($widget->getPluginId(), $default_widget);
  }

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
  /**
   * Tests the behavior of a field component for a base field.
   */
  public function testBaseFieldComponent() {
    $display = entity_create('entity_form_display', array(
      'targetEntityType' => 'entity_test_base_field_display',
      'bundle' => 'entity_test_base_field_display',
      'mode' => 'default',
    ));

    // Check that default options are correctly filled in.
    $formatter_settings =  \Drupal::service('plugin.manager.field.widget')->getDefaultSettings('text_textfield');
    $expected = array(
      'test_no_display' => NULL,
      'test_display_configurable' => array(
        'type' => 'text_textfield',
        'settings' => $formatter_settings,
133
        'third_party_settings' => array(),
134 135 136 137 138
        'weight' => 10,
      ),
      'test_display_non_configurable' => array(
        'type' => 'text_textfield',
        'settings' => $formatter_settings,
139
        'third_party_settings' => array(),
140 141 142 143 144 145 146 147 148 149
        'weight' => 11,
      ),
    );
    foreach ($expected as $field_name => $options) {
      $this->assertEqual($display->getComponent($field_name), $options);
    }

    // Check that saving the display only writes data for fields whose display
    // is configurable.
    $display->save();
150
    $config = \Drupal::config('core.entity_form_display.' . $display->id());
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    $data = $config->get();
    $this->assertFalse(isset($data['content']['test_no_display']));
    $this->assertFalse(isset($data['hidden']['test_no_display']));
    $this->assertEqual($data['content']['test_display_configurable'], $expected['test_display_configurable']);
    $this->assertFalse(isset($data['content']['test_display_non_configurable']));
    $this->assertFalse(isset($data['hidden']['test_display_non_configurable']));

    // Check that defaults are correctly filled when loading the display.
    $display = entity_load('entity_form_display', $display->id());
    foreach ($expected as $field_name => $options) {
      $this->assertEqual($display->getComponent($field_name), $options);
    }

    // Check that data manually written for fields whose display is not
    // configurable is discarded when loading the display.
    $data['content']['test_display_non_configurable'] = $expected['test_display_non_configurable'];
    $data['content']['test_display_non_configurable']['weight']++;
    $config->setData($data)->save();
    $display = entity_load('entity_form_display', $display->id());
    foreach ($expected as $field_name => $options) {
      $this->assertEqual($display->getComponent($field_name), $options);
    }
  }

175 176 177 178 179
  /**
   * Tests deleting field instance.
   */
  public function testDeleteFieldInstance() {
    $field_name = 'test_field';
180 181
    // Create a field storage and an instance.
    $field_storage = entity_create('field_storage_config', array(
182 183 184 185
      'name' => $field_name,
      'entity_type' => 'entity_test',
      'type' => 'test_field'
    ));
186
    $field_storage->save();
187
    $instance = entity_create('field_instance_config', array(
188
      'field_storage' => $field_storage,
189 190 191 192 193
      'bundle' => 'entity_test',
    ));
    $instance->save();

    // Create default and compact entity display.
194
    EntityFormMode::create(array('id' =>  'entity_test.compact', 'targetEntityType' => 'entity_test'))->save();
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
    entity_create('entity_form_display', array(
      'targetEntityType' => 'entity_test',
      'bundle' => 'entity_test',
      'mode' => 'default',
    ))->setComponent($field_name)->save();
    entity_create('entity_form_display', array(
      'targetEntityType' => 'entity_test',
      'bundle' => 'entity_test',
      'mode' => 'compact',
    ))->setComponent($field_name)->save();

    // Check the component exists.
    $display = entity_get_form_display('entity_test', 'entity_test', 'default');
    $this->assertTrue($display->getComponent($field_name));
    $display = entity_get_form_display('entity_test', 'entity_test', 'compact');
    $this->assertTrue($display->getComponent($field_name));

    // Delete the instance.
    $instance->delete();

    // Check that the component has been removed from the entity displays.
    $display = entity_get_form_display('entity_test', 'entity_test', 'default');
    $this->assertFalse($display->getComponent($field_name));
    $display = entity_get_form_display('entity_test', 'entity_test', 'compact');
    $this->assertFalse($display->getComponent($field_name));
  }

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
  /**
   * Tests \Drupal\entity\EntityDisplayBase::onDependencyRemoval().
   */
  public function testOnDependencyRemoval() {
    $this->enableModules(array('field_plugins_test'));

    $field_name = 'test_field';
    // Create a field and an instance.
    $field = entity_create('field_storage_config', array(
      'name' => $field_name,
      'entity_type' => 'entity_test',
      'type' => 'text'
    ));
    $field->save();
    $instance = entity_create('field_instance_config', array(
      'field_storage' => $field,
      'bundle' => 'entity_test',
    ));
    $instance->save();

    entity_create('entity_form_display', array(
      'targetEntityType' => 'entity_test',
      'bundle' => 'entity_test',
      'mode' => 'default',
    ))->setComponent($field_name, array('type' => 'field_plugins_test_text_widget'))->save();

    // Check the component exists and is of the correct type.
    $display = entity_get_form_display('entity_test', 'entity_test', 'default');
    $this->assertEqual($display->getComponent($field_name)['type'], 'field_plugins_test_text_widget');

    // Removing the field_plugins_test module should change the component to use
    // the default widget for test fields.
    \Drupal::service('config.manager')->uninstall('module', 'field_plugins_test');
    $display = entity_get_form_display('entity_test', 'entity_test', 'default');
    $this->assertEqual($display->getComponent($field_name)['type'], 'text_textfield');

    // Removing the text module should remove the field from the form display.
    \Drupal::service('config.manager')->uninstall('module', 'text');
    $display = entity_get_form_display('entity_test', 'entity_test', 'default');
    $this->assertFalse($display->getComponent($field_name));
  }

264
}