EntityFormDisplayTest.php 10.2 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 21 22 23 24 25 26 27 28
  /**
   * Set to TRUE to strict check all configuration saved.
   *
   * @see \Drupal\Core\Config\Testing\ConfigSchemaChecker
   *
   * @var bool
   */
  protected $strictConfigSchema = TRUE;

29
  public static $modules = array('field_ui', 'field', 'entity_test', 'field_test', 'user', 'text', 'entity_reference');
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

  protected function setUp() {
    parent::setUp();
  }

  /**
   * 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() {
59
    // Create a field storage and a field.
60
    $field_name = 'test_field';
61
    $field_storage = entity_create('field_storage_config', array(
62
      'field_name' => $field_name,
63
      'entity_type' => 'entity_test',
64
      'type' => 'test_field'
65
    ));
66
    $field_storage->save();
67
    $field = entity_create('field_config', array(
68
      'field_storage' => $field_storage,
69
      'bundle' => 'entity_test',
70
    ));
71
    $field->save();
72

73 74 75 76 77 78
    $form_display = entity_create('entity_form_display', array(
      'targetEntityType' => 'entity_test',
      'bundle' => 'entity_test',
      'mode' => 'default',
    ));

79
    // Check that providing no options results in default values being used.
80
    $form_display->setComponent($field_name);
81
    $field_type_info = \Drupal::service('plugin.manager.field.field_type')->getDefinition($field_storage->type);
82
    $default_widget = $field_type_info['default_widget'];
83
    $widget_settings = \Drupal::service('plugin.manager.field.widget')->getDefaultSettings($default_widget);
84 85 86
    $expected = array(
      'weight' => 0,
      'type' => $default_widget,
87
      'settings' => $widget_settings,
88
      'third_party_settings' => array(),
89
    );
90
    $this->assertEqual($form_display->getComponent($field_name), $expected);
91 92

    // Check that the getWidget() method returns the correct widget plugin.
93
    $widget = $form_display->getRenderer($field_name);
94
    $this->assertEqual($widget->getPluginId(), $default_widget);
95
    $this->assertEqual($widget->getSettings(), $widget_settings);
96 97 98 99 100

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

    // Check that changing the definition creates a new widget.
105
    $form_display->setComponent($field_name, array(
106 107
      'type' => 'field_test_multiple',
    ));
108
    $widget = $form_display->getRenderer($field_name);
109 110 111 112 113 114
    $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.
115
    $form_display->setComponent($field_name, array(
116 117
      'type' => 'unknown_widget',
    ));
118
    $options = $form_display->getComponent($field_name);
119
    $this->assertEqual($options['type'], 'unknown_widget');
120
    $widget = $form_display->getRenderer($field_name);
121 122 123
    $this->assertEqual($widget->getPluginId(), $default_widget);
  }

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
  /**
   * 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,
141
        'third_party_settings' => array(),
142 143 144 145 146
        'weight' => 10,
      ),
      'test_display_non_configurable' => array(
        'type' => 'text_textfield',
        'settings' => $formatter_settings,
147
        'third_party_settings' => array(),
148 149 150 151 152 153 154 155 156 157
        '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();
158
    $config = \Drupal::config('core.entity_form_display.' . $display->id());
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
    $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);
    }
  }

183
  /**
184
   * Tests deleting field.
185
   */
186
  public function testDeleteField() {
187
    $field_name = 'test_field';
188
    // Create a field storage and a field.
189
    $field_storage = entity_create('field_storage_config', array(
190
      'field_name' => $field_name,
191 192 193
      'entity_type' => 'entity_test',
      'type' => 'test_field'
    ));
194
    $field_storage->save();
195
    $field = entity_create('field_config', array(
196
      'field_storage' => $field_storage,
197 198
      'bundle' => 'entity_test',
    ));
199
    $field->save();
200 201

    // Create default and compact entity display.
202
    EntityFormMode::create(array('id' =>  'entity_test.compact', 'targetEntityType' => 'entity_test'))->save();
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    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));

220 221
    // Delete the field.
    $field->delete();
222 223 224 225 226 227 228 229

    // 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));
  }

230 231 232 233 234 235 236
  /**
   * Tests \Drupal\entity\EntityDisplayBase::onDependencyRemoval().
   */
  public function testOnDependencyRemoval() {
    $this->enableModules(array('field_plugins_test'));

    $field_name = 'test_field';
237 238
    // Create a field.
    $field_storage = entity_create('field_storage_config', array(
239
      'field_name' => $field_name,
240 241 242
      'entity_type' => 'entity_test',
      'type' => 'text'
    ));
243 244 245
    $field_storage->save();
    $field = entity_create('field_config', array(
      'field_storage' => $field_storage,
246 247
      'bundle' => 'entity_test',
    ));
248
    $field->save();
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271

    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));
  }

272
}