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

/src_8/core/modules/field/tests/src/Functional/EntityReference/EntityReferenceIntegrationTest.php

https://bitbucket.org/razum-io/ns-hub
PHP | 223 lines | 121 code | 34 blank | 68 comment | 3 complexity | 843e40f10d4ef8116dd9a761640075c9 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, CC-BY-SA-3.0, MIT, BSD-3-Clause
  1. <?php
  2. namespace Drupal\Tests\field\Functional\EntityReference;
  3. use Drupal\Component\Utility\SafeMarkup;
  4. use Drupal\entity_test\Entity\EntityTest;
  5. use Drupal\field\Entity\FieldConfig;
  6. use Drupal\Tests\BrowserTestBase;
  7. use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
  8. use Drupal\Tests\config\Traits\AssertConfigEntityImportTrait;
  9. /**
  10. * Tests various Entity reference UI components.
  11. *
  12. * @group entity_reference
  13. */
  14. class EntityReferenceIntegrationTest extends BrowserTestBase {
  15. use AssertConfigEntityImportTrait;
  16. use EntityReferenceTestTrait;
  17. /**
  18. * The entity type used in this test.
  19. *
  20. * @var string
  21. */
  22. protected $entityType = 'entity_test';
  23. /**
  24. * The bundle used in this test.
  25. *
  26. * @var string
  27. */
  28. protected $bundle = 'entity_test';
  29. /**
  30. * The name of the field used in this test.
  31. *
  32. * @var string
  33. */
  34. protected $fieldName;
  35. /**
  36. * Modules to install.
  37. *
  38. * @var array
  39. */
  40. public static $modules = ['config_test', 'entity_test', 'field_ui'];
  41. /**
  42. * {@inheritdoc}
  43. */
  44. protected function setUp() {
  45. parent::setUp();
  46. // Create a test user.
  47. $web_user = $this->drupalCreateUser(['administer entity_test content', 'administer entity_test fields', 'view test entity']);
  48. $this->drupalLogin($web_user);
  49. }
  50. /**
  51. * Tests the entity reference field with all its supported field widgets.
  52. */
  53. public function testSupportedEntityTypesAndWidgets() {
  54. foreach ($this->getTestEntities() as $key => $referenced_entities) {
  55. $this->fieldName = 'field_test_' . $referenced_entities[0]->getEntityTypeId();
  56. // Create an Entity reference field.
  57. $this->createEntityReferenceField($this->entityType, $this->bundle, $this->fieldName, $this->fieldName, $referenced_entities[0]->getEntityTypeId(), 'default', [], 2);
  58. // Test the default 'entity_reference_autocomplete' widget.
  59. entity_get_form_display($this->entityType, $this->bundle, 'default')->setComponent($this->fieldName)->save();
  60. $entity_name = $this->randomMachineName();
  61. $edit = [
  62. 'name[0][value]' => $entity_name,
  63. $this->fieldName . '[0][target_id]' => $referenced_entities[0]->label() . ' (' . $referenced_entities[0]->id() . ')',
  64. // Test an input of the entity label without a ' (entity_id)' suffix.
  65. $this->fieldName . '[1][target_id]' => $referenced_entities[1]->label(),
  66. ];
  67. $this->drupalPostForm($this->entityType . '/add', $edit, t('Save'));
  68. $this->assertFieldValues($entity_name, $referenced_entities);
  69. // Try to post the form again with no modification and check if the field
  70. // values remain the same.
  71. /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
  72. $storage = $this->container->get('entity_type.manager')->getStorage($this->entityType);
  73. $entity = current($storage->loadByProperties(['name' => $entity_name]));
  74. $this->drupalGet($this->entityType . '/manage/' . $entity->id() . '/edit');
  75. $this->assertFieldByName($this->fieldName . '[0][target_id]', $referenced_entities[0]->label() . ' (' . $referenced_entities[0]->id() . ')');
  76. $this->assertFieldByName($this->fieldName . '[1][target_id]', $referenced_entities[1]->label() . ' (' . $referenced_entities[1]->id() . ')');
  77. $this->drupalPostForm(NULL, [], t('Save'));
  78. $this->assertFieldValues($entity_name, $referenced_entities);
  79. // Test the 'entity_reference_autocomplete_tags' widget.
  80. entity_get_form_display($this->entityType, $this->bundle, 'default')->setComponent($this->fieldName, [
  81. 'type' => 'entity_reference_autocomplete_tags',
  82. ])->save();
  83. $entity_name = $this->randomMachineName();
  84. $target_id = $referenced_entities[0]->label() . ' (' . $referenced_entities[0]->id() . ')';
  85. // Test an input of the entity label without a ' (entity_id)' suffix.
  86. $target_id .= ', ' . $referenced_entities[1]->label();
  87. $edit = [
  88. 'name[0][value]' => $entity_name,
  89. $this->fieldName . '[target_id]' => $target_id,
  90. ];
  91. $this->drupalPostForm($this->entityType . '/add', $edit, t('Save'));
  92. $this->assertFieldValues($entity_name, $referenced_entities);
  93. // Try to post the form again with no modification and check if the field
  94. // values remain the same.
  95. $entity = current($storage->loadByProperties(['name' => $entity_name]));
  96. $this->drupalGet($this->entityType . '/manage/' . $entity->id() . '/edit');
  97. $this->assertFieldByName($this->fieldName . '[target_id]', $target_id . ' (' . $referenced_entities[1]->id() . ')');
  98. $this->drupalPostForm(NULL, [], t('Save'));
  99. $this->assertFieldValues($entity_name, $referenced_entities);
  100. // Test all the other widgets supported by the entity reference field.
  101. // Since we don't know the form structure for these widgets, just test
  102. // that editing and saving an already created entity works.
  103. $exclude = ['entity_reference_autocomplete', 'entity_reference_autocomplete_tags'];
  104. $entity = current($storage->loadByProperties(['name' => $entity_name]));
  105. $supported_widgets = \Drupal::service('plugin.manager.field.widget')->getOptions('entity_reference');
  106. $supported_widget_types = array_diff(array_keys($supported_widgets), $exclude);
  107. foreach ($supported_widget_types as $widget_type) {
  108. entity_get_form_display($this->entityType, $this->bundle, 'default')->setComponent($this->fieldName, [
  109. 'type' => $widget_type,
  110. ])->save();
  111. $this->drupalPostForm($this->entityType . '/manage/' . $entity->id() . '/edit', [], t('Save'));
  112. $this->assertFieldValues($entity_name, $referenced_entities);
  113. }
  114. // Reset to the default 'entity_reference_autocomplete' widget.
  115. entity_get_form_display($this->entityType, $this->bundle, 'default')->setComponent($this->fieldName)->save();
  116. // Set first entity as the default_value.
  117. $field_edit = [
  118. 'default_value_input[' . $this->fieldName . '][0][target_id]' => $referenced_entities[0]->label() . ' (' . $referenced_entities[0]->id() . ')',
  119. ];
  120. if ($key == 'content') {
  121. $field_edit['settings[handler_settings][target_bundles][' . $referenced_entities[0]->getEntityTypeId() . ']'] = TRUE;
  122. }
  123. $this->drupalPostForm($this->entityType . '/structure/' . $this->bundle . '/fields/' . $this->entityType . '.' . $this->bundle . '.' . $this->fieldName, $field_edit, t('Save settings'));
  124. // Ensure the configuration has the expected dependency on the entity that
  125. // is being used a default value.
  126. $field = FieldConfig::loadByName($this->entityType, $this->bundle, $this->fieldName);
  127. $this->assertTrue(in_array($referenced_entities[0]->getConfigDependencyName(), $field->getDependencies()[$key]), SafeMarkup::format('Expected @type dependency @name found', ['@type' => $key, '@name' => $referenced_entities[0]->getConfigDependencyName()]));
  128. // Ensure that the field can be imported without change even after the
  129. // default value deleted.
  130. $referenced_entities[0]->delete();
  131. // Reload the field since deleting the default value can change the field.
  132. \Drupal::entityManager()->getStorage($field->getEntityTypeId())->resetCache([$field->id()]);
  133. $field = FieldConfig::loadByName($this->entityType, $this->bundle, $this->fieldName);
  134. $this->assertConfigEntityImport($field);
  135. // Once the default value has been removed after saving the dependency
  136. // should be removed.
  137. $field = FieldConfig::loadByName($this->entityType, $this->bundle, $this->fieldName);
  138. $field->save();
  139. $dependencies = $field->getDependencies();
  140. $this->assertFalse(isset($dependencies[$key]) && in_array($referenced_entities[0]->getConfigDependencyName(), $dependencies[$key]), SafeMarkup::format('@type dependency @name does not exist.', ['@type' => $key, '@name' => $referenced_entities[0]->getConfigDependencyName()]));
  141. }
  142. }
  143. /**
  144. * Asserts that the reference field values are correct.
  145. *
  146. * @param string $entity_name
  147. * The name of the test entity.
  148. * @param \Drupal\Core\Entity\EntityInterface[] $referenced_entities
  149. * An array of referenced entities.
  150. */
  151. protected function assertFieldValues($entity_name, $referenced_entities) {
  152. $entity = current($this->container->get('entity_type.manager')->getStorage(
  153. $this->entityType)->loadByProperties(['name' => $entity_name]));
  154. $this->assertTrue($entity, format_string('%entity_type: Entity found in the database.', ['%entity_type' => $this->entityType]));
  155. $this->assertEqual($entity->{$this->fieldName}->target_id, $referenced_entities[0]->id());
  156. $this->assertEqual($entity->{$this->fieldName}->entity->id(), $referenced_entities[0]->id());
  157. $this->assertEqual($entity->{$this->fieldName}->entity->label(), $referenced_entities[0]->label());
  158. $this->assertEqual($entity->{$this->fieldName}[1]->target_id, $referenced_entities[1]->id());
  159. $this->assertEqual($entity->{$this->fieldName}[1]->entity->id(), $referenced_entities[1]->id());
  160. $this->assertEqual($entity->{$this->fieldName}[1]->entity->label(), $referenced_entities[1]->label());
  161. }
  162. /**
  163. * Creates two content and two config test entities.
  164. *
  165. * @return array
  166. * An array of entity objects.
  167. */
  168. protected function getTestEntities() {
  169. $storage = \Drupal::entityTypeManager()->getStorage('config_test');
  170. $config_entity_1 = $storage->create(['id' => $this->randomMachineName(), 'label' => $this->randomMachineName()]);
  171. $config_entity_1->save();
  172. $config_entity_2 = $storage->create(['id' => $this->randomMachineName(), 'label' => $this->randomMachineName()]);
  173. $config_entity_2->save();
  174. $content_entity_1 = EntityTest::create(['name' => $this->randomMachineName()]);
  175. $content_entity_1->save();
  176. $content_entity_2 = EntityTest::create(['name' => $this->randomMachineName()]);
  177. $content_entity_2->save();
  178. return [
  179. 'config' => [
  180. $config_entity_1,
  181. $config_entity_2,
  182. ],
  183. 'content' => [
  184. $content_entity_1,
  185. $content_entity_2,
  186. ],
  187. ];
  188. }
  189. }