PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/field_ui/tests/src/Kernel/EntityFormDisplayTest.php

https://gitlab.com/guillaumev/alkarama
PHP | 266 lines | 180 code | 32 blank | 54 comment | 0 complexity | 28db8d78e75eebc602f36d17323ea77e MD5 | raw file
  1. <?php
  2. namespace Drupal\Tests\field_ui\Kernel;
  3. use Drupal\Core\Entity\Entity\EntityFormDisplay;
  4. use Drupal\Core\Entity\Entity\EntityFormMode;
  5. use Drupal\field\Entity\FieldConfig;
  6. use Drupal\field\Entity\FieldStorageConfig;
  7. use Drupal\KernelTests\KernelTestBase;
  8. /**
  9. * Tests the entity display configuration entities.
  10. *
  11. * @group field_ui
  12. */
  13. class EntityFormDisplayTest extends KernelTestBase {
  14. /**
  15. * Modules to install.
  16. *
  17. * @var string[]
  18. */
  19. public static $modules = ['field_ui', 'field', 'entity_test', 'field_test', 'user', 'text'];
  20. protected function setUp() {
  21. parent::setUp();
  22. }
  23. /**
  24. * Tests entity_get_form_display().
  25. */
  26. public function testEntityGetFromDisplay() {
  27. // Check that entity_get_form_display() returns a fresh object when no
  28. // configuration entry exists.
  29. $form_display = entity_get_form_display('entity_test', 'entity_test', 'default');
  30. $this->assertTrue($form_display->isNew());
  31. // Add some components and save the display.
  32. $form_display->setComponent('component_1', array('weight' => 10))
  33. ->save();
  34. // Check that entity_get_form_display() returns the correct object.
  35. $form_display = entity_get_form_display('entity_test', 'entity_test', 'default');
  36. $this->assertFalse($form_display->isNew());
  37. $this->assertEqual($form_display->id(), 'entity_test.entity_test.default');
  38. $this->assertEqual($form_display->getComponent('component_1'), array('weight' => 10, 'settings' => array(), 'third_party_settings' => array()));
  39. }
  40. /**
  41. * Tests the behavior of a field component within an EntityFormDisplay object.
  42. */
  43. public function testFieldComponent() {
  44. // Create a field storage and a field.
  45. $field_name = 'test_field';
  46. $field_storage = FieldStorageConfig::create(array(
  47. 'field_name' => $field_name,
  48. 'entity_type' => 'entity_test',
  49. 'type' => 'test_field'
  50. ));
  51. $field_storage->save();
  52. $field = FieldConfig::create(array(
  53. 'field_storage' => $field_storage,
  54. 'bundle' => 'entity_test',
  55. ));
  56. $field->save();
  57. $form_display = EntityFormDisplay::create(array(
  58. 'targetEntityType' => 'entity_test',
  59. 'bundle' => 'entity_test',
  60. 'mode' => 'default',
  61. ));
  62. // Check that providing no options results in default values being used.
  63. $form_display->setComponent($field_name);
  64. $field_type_info = \Drupal::service('plugin.manager.field.field_type')->getDefinition($field_storage->getType());
  65. $default_widget = $field_type_info['default_widget'];
  66. $widget_settings = \Drupal::service('plugin.manager.field.widget')->getDefaultSettings($default_widget);
  67. $expected = array(
  68. 'weight' => 3,
  69. 'type' => $default_widget,
  70. 'settings' => $widget_settings,
  71. 'third_party_settings' => array(),
  72. );
  73. $this->assertEqual($form_display->getComponent($field_name), $expected);
  74. // Check that the getWidget() method returns the correct widget plugin.
  75. $widget = $form_display->getRenderer($field_name);
  76. $this->assertEqual($widget->getPluginId(), $default_widget);
  77. $this->assertEqual($widget->getSettings(), $widget_settings);
  78. // Check that the widget is statically persisted, by assigning an
  79. // arbitrary property and reading it back.
  80. $random_value = $this->randomString();
  81. $widget->randomValue = $random_value;
  82. $widget = $form_display->getRenderer($field_name);
  83. $this->assertEqual($widget->randomValue, $random_value);
  84. // Check that changing the definition creates a new widget.
  85. $form_display->setComponent($field_name, array(
  86. 'type' => 'field_test_multiple',
  87. ));
  88. $widget = $form_display->getRenderer($field_name);
  89. $this->assertEqual($widget->getPluginId(), 'test_field_widget');
  90. $this->assertFalse(isset($widget->randomValue));
  91. // Check that specifying an unknown widget (e.g. case of a disabled module)
  92. // gets stored as is in the display, but results in the default widget being
  93. // used.
  94. $form_display->setComponent($field_name, array(
  95. 'type' => 'unknown_widget',
  96. ));
  97. $options = $form_display->getComponent($field_name);
  98. $this->assertEqual($options['type'], 'unknown_widget');
  99. $widget = $form_display->getRenderer($field_name);
  100. $this->assertEqual($widget->getPluginId(), $default_widget);
  101. }
  102. /**
  103. * Tests the behavior of a field component for a base field.
  104. */
  105. public function testBaseFieldComponent() {
  106. $display = EntityFormDisplay::create(array(
  107. 'targetEntityType' => 'entity_test_base_field_display',
  108. 'bundle' => 'entity_test_base_field_display',
  109. 'mode' => 'default',
  110. ));
  111. // Check that default options are correctly filled in.
  112. $formatter_settings = \Drupal::service('plugin.manager.field.widget')->getDefaultSettings('text_textfield');
  113. $expected = array(
  114. 'test_no_display' => NULL,
  115. 'test_display_configurable' => array(
  116. 'type' => 'text_textfield',
  117. 'settings' => $formatter_settings,
  118. 'third_party_settings' => array(),
  119. 'weight' => 10,
  120. ),
  121. 'test_display_non_configurable' => array(
  122. 'type' => 'text_textfield',
  123. 'settings' => $formatter_settings,
  124. 'third_party_settings' => array(),
  125. 'weight' => 11,
  126. ),
  127. );
  128. foreach ($expected as $field_name => $options) {
  129. $this->assertEqual($display->getComponent($field_name), $options);
  130. }
  131. // Check that saving the display only writes data for fields whose display
  132. // is configurable.
  133. $display->save();
  134. $config = $this->config('core.entity_form_display.' . $display->id());
  135. $data = $config->get();
  136. $this->assertFalse(isset($data['content']['test_no_display']));
  137. $this->assertFalse(isset($data['hidden']['test_no_display']));
  138. $this->assertEqual($data['content']['test_display_configurable'], $expected['test_display_configurable']);
  139. $this->assertFalse(isset($data['content']['test_display_non_configurable']));
  140. $this->assertFalse(isset($data['hidden']['test_display_non_configurable']));
  141. // Check that defaults are correctly filled when loading the display.
  142. $display = EntityFormDisplay::load($display->id());
  143. foreach ($expected as $field_name => $options) {
  144. $this->assertEqual($display->getComponent($field_name), $options);
  145. }
  146. // Check that data manually written for fields whose display is not
  147. // configurable is discarded when loading the display.
  148. $data['content']['test_display_non_configurable'] = $expected['test_display_non_configurable'];
  149. $data['content']['test_display_non_configurable']['weight']++;
  150. $config->setData($data)->save();
  151. $display = EntityFormDisplay::load($display->id());
  152. foreach ($expected as $field_name => $options) {
  153. $this->assertEqual($display->getComponent($field_name), $options);
  154. }
  155. }
  156. /**
  157. * Tests deleting field.
  158. */
  159. public function testDeleteField() {
  160. $field_name = 'test_field';
  161. // Create a field storage and a field.
  162. $field_storage = FieldStorageConfig::create(array(
  163. 'field_name' => $field_name,
  164. 'entity_type' => 'entity_test',
  165. 'type' => 'test_field'
  166. ));
  167. $field_storage->save();
  168. $field = FieldConfig::create(array(
  169. 'field_storage' => $field_storage,
  170. 'bundle' => 'entity_test',
  171. ));
  172. $field->save();
  173. // Create default and compact entity display.
  174. EntityFormMode::create(array('id' => 'entity_test.compact', 'targetEntityType' => 'entity_test'))->save();
  175. EntityFormDisplay::create(array(
  176. 'targetEntityType' => 'entity_test',
  177. 'bundle' => 'entity_test',
  178. 'mode' => 'default',
  179. ))->setComponent($field_name)->save();
  180. EntityFormDisplay::create(array(
  181. 'targetEntityType' => 'entity_test',
  182. 'bundle' => 'entity_test',
  183. 'mode' => 'compact',
  184. ))->setComponent($field_name)->save();
  185. // Check the component exists.
  186. $display = entity_get_form_display('entity_test', 'entity_test', 'default');
  187. $this->assertTrue($display->getComponent($field_name));
  188. $display = entity_get_form_display('entity_test', 'entity_test', 'compact');
  189. $this->assertTrue($display->getComponent($field_name));
  190. // Delete the field.
  191. $field->delete();
  192. // Check that the component has been removed from the entity displays.
  193. $display = entity_get_form_display('entity_test', 'entity_test', 'default');
  194. $this->assertFalse($display->getComponent($field_name));
  195. $display = entity_get_form_display('entity_test', 'entity_test', 'compact');
  196. $this->assertFalse($display->getComponent($field_name));
  197. }
  198. /**
  199. * Tests \Drupal\Core\Entity\EntityDisplayBase::onDependencyRemoval().
  200. */
  201. public function testOnDependencyRemoval() {
  202. $this->enableModules(array('field_plugins_test'));
  203. $field_name = 'test_field';
  204. // Create a field.
  205. $field_storage = FieldStorageConfig::create(array(
  206. 'field_name' => $field_name,
  207. 'entity_type' => 'entity_test',
  208. 'type' => 'text'
  209. ));
  210. $field_storage->save();
  211. $field = FieldConfig::create(array(
  212. 'field_storage' => $field_storage,
  213. 'bundle' => 'entity_test',
  214. ));
  215. $field->save();
  216. EntityFormDisplay::create(array(
  217. 'targetEntityType' => 'entity_test',
  218. 'bundle' => 'entity_test',
  219. 'mode' => 'default',
  220. ))->setComponent($field_name, array('type' => 'field_plugins_test_text_widget'))->save();
  221. // Check the component exists and is of the correct type.
  222. $display = entity_get_form_display('entity_test', 'entity_test', 'default');
  223. $this->assertEqual($display->getComponent($field_name)['type'], 'field_plugins_test_text_widget');
  224. // Removing the field_plugins_test module should change the component to use
  225. // the default widget for test fields.
  226. \Drupal::service('config.manager')->uninstall('module', 'field_plugins_test');
  227. $display = entity_get_form_display('entity_test', 'entity_test', 'default');
  228. $this->assertEqual($display->getComponent($field_name)['type'], 'text_textfield');
  229. // Removing the text module should remove the field from the form display.
  230. \Drupal::service('config.manager')->uninstall('module', 'text');
  231. $display = entity_get_form_display('entity_test', 'entity_test', 'default');
  232. $this->assertFalse($display->getComponent($field_name));
  233. }
  234. }