diff --git a/core/modules/entity_reference/src/Tests/DynamicEntityReferenceIntegrationTest.php b/core/modules/entity_reference/src/Tests/DynamicEntityReferenceIntegrationTest.php new file mode 100644 index 0000000..b4ac5d2 --- /dev/null +++ b/core/modules/entity_reference/src/Tests/DynamicEntityReferenceIntegrationTest.php @@ -0,0 +1,91 @@ +webUser = $this->drupalCreateUser(array('administer entity_test content', 'administer entity_test fields')); + $this->drupalLogin($this->webUser); + \Drupal::service('entity.definition_update_manager')->applyUpdates(); + } + + /** + * Tests the entity reference field with all its supported field widgets. + */ + public function testDynamicEntityTypesAndWidgets() { + + $entity_name = $this->randomMachineName(); + $edit = array( + 'name[0][value]' => $entity_name, + 'dynamic_references[0][target_type]' => 'user', + 'dynamic_references[0][target_id]' => $this->webUser->label() . ' (' . $this->webUser->id() . ')', + ); + $this->drupalPostForm($this->entityType . '/add', $edit, t('Save')); + $entity = current(entity_load_multiple_by_properties($this->entityType, array('name' => $entity_name))); + $this->assertEqual($entity->dynamic_references->target_id, $this->webUser->id()); + $this->assertEqual($entity->dynamic_references->entity->id(), $this->webUser->id()); + $this->assertEqual($entity->dynamic_references->entity->label(), $this->webUser->label()); + $this->assertEqual($entity->dynamic_references->target_type, 'user'); + + } + + +} diff --git a/core/modules/entity_reference/src/Tests/EntityReferenceIntegrationTest.php b/core/modules/entity_reference/src/Tests/EntityReferenceIntegrationTest.php index 858ee61..6c5f8fc 100644 --- a/core/modules/entity_reference/src/Tests/EntityReferenceIntegrationTest.php +++ b/core/modules/entity_reference/src/Tests/EntityReferenceIntegrationTest.php @@ -42,18 +42,11 @@ class EntityReferenceIntegrationTest extends WebTestBase { protected $fieldName; /** - * The logged in user. - * - * @var \Drupal\user\UserInterface - */ - protected $webUser; - - /** * Modules to install. * * @var array */ - public static $modules = array('config_test', 'entity_test', 'entity_reference', 'field_ui', 'entity_reference_test'); + public static $modules = array('config_test', 'entity_test', 'entity_reference', 'field_ui'); /** * {@inheritdoc} @@ -62,9 +55,8 @@ protected function setUp() { parent::setUp(); // Create a test user. - $this->webUser = $this->drupalCreateUser(array('administer entity_test content', 'administer entity_test fields')); - $this->drupalLogin($this->webUser); - \Drupal::service('entity.definition_update_manager')->applyUpdates(); + $web_user = $this->drupalCreateUser(array('administer entity_test content', 'administer entity_test fields')); + $this->drupalLogin($web_user); } /** @@ -86,18 +78,13 @@ public function testSupportedEntityTypesAndWidgets() { $this->fieldName . '[0][target_id]' => $referenced_entities[0]->label() . ' (' . $referenced_entities[0]->id() . ')', // Test an input of the entity label without a ' (entity_id)' suffix. $this->fieldName . '[1][target_id]' => $referenced_entities[1]->label(), - 'dynamic_references[0][target_type]' => 'user', - 'dynamic_references[0][target_id]' => $this->webUser->label() . ' (' . $this->webUser->id() . ')', ); $this->drupalPostForm($this->entityType . '/add', $edit, t('Save')); - $entity = $this->assertFieldValues($entity_name, $referenced_entities); - $this->assertEqual($entity->dynamic_references->target_id, $this->webUser->id()); - $this->assertEqual($entity->dynamic_references->entity->id(), $this->webUser->id()); - $this->assertEqual($entity->dynamic_references->entity->label(), $this->webUser->label()); - $this->assertEqual($entity->dynamic_references->target_type, 'user'); + $this->assertFieldValues($entity_name, $referenced_entities); // Try to post the form again with no modification and check if the field // values remain the same. + $entity = current(entity_load_multiple_by_properties($this->entityType, array('name' => $entity_name))); $this->drupalGet($this->entityType . '/manage/' . $entity->id()); $this->assertFieldByName($this->fieldName . '[0][target_id]', $referenced_entities[0]->label() . ' (' . $referenced_entities[0]->id() . ')'); $this->assertFieldByName($this->fieldName . '[1][target_id]', $referenced_entities[1]->label() . ' (' . $referenced_entities[1]->id() . ')'); @@ -183,9 +170,6 @@ public function testSupportedEntityTypesAndWidgets() { * The name of the test entity. * @param \Drupal\Core\Entity\EntityInterface[] $referenced_entities * An array of referenced entities. - * - * @return \Drupal\Core\Entity\EntityInterface|FALSE - * The entity with the given entity name if it exists, otherwise FALSE. */ protected function assertFieldValues($entity_name, $referenced_entities) { $entity = current(entity_load_multiple_by_properties($this->entityType, array('name' => $entity_name))); @@ -199,7 +183,6 @@ protected function assertFieldValues($entity_name, $referenced_entities) { $this->assertEqual($entity->{$this->fieldName}[1]->target_id, $referenced_entities[1]->id()); $this->assertEqual($entity->{$this->fieldName}[1]->entity->id(), $referenced_entities[1]->id()); $this->assertEqual($entity->{$this->fieldName}[1]->entity->label(), $referenced_entities[1]->label()); - return $entity; } /** diff --git a/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/dynamic_entity_reference_test.info.yml b/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/dynamic_entity_reference_test.info.yml new file mode 100644 index 0000000..657ebe55 --- /dev/null +++ b/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/dynamic_entity_reference_test.info.yml @@ -0,0 +1,8 @@ +name: "Dynamic Entity Reference Test" +type: module +description: "Support module for the Dynamic Entity Reference tests." +core: 8.x +package: Testing +version: VERSION +dependencies: + - entity_reference \ No newline at end of file diff --git a/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/dynamic_entity_reference_test.module b/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/dynamic_entity_reference_test.module new file mode 100644 index 0000000..c7d81ab --- /dev/null +++ b/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/dynamic_entity_reference_test.module @@ -0,0 +1,30 @@ +id() === 'entity_test') { + $fields['dynamic_references'] = BaseFieldDefinition::create('dynamic_entity_reference') + ->setLabel(t('References')) + ->setDescription(t('Other references.')) + ->setSetting('handler', 'default') + ->setRequired(FALSE) + ->setDisplayOptions('form', [ + 'type' => 'dynamic_entity_reference_default', + 'weight' => 10, + ]); + } + + return $fields; +} \ No newline at end of file diff --git a/core/modules/entity_reference/tests/modules/entity_reference_test/entity_reference_test.module b/core/modules/entity_reference/tests/modules/entity_reference_test/entity_reference_test.module index 73245b6..51b506b 100644 --- a/core/modules/entity_reference/tests/modules/entity_reference_test/entity_reference_test.module +++ b/core/modules/entity_reference/tests/modules/entity_reference_test/entity_reference_test.module @@ -20,15 +20,6 @@ function entity_reference_test_entity_base_field_info(EntityTypeInterface $entit ->setDescription(t('The role of the associated user.')) ->setSetting('target_type', 'user_role') ->setSetting('handler', 'default'); - $fields['dynamic_references'] = BaseFieldDefinition::create('dynamic_entity_reference') - ->setLabel(t('References')) - ->setDescription(t('Other references.')) - ->setSetting('handler', 'default') - ->setRequired(FALSE) - ->setDisplayOptions('form', [ - 'type' => 'dynamic_entity_reference_default', - 'weight' => 10, - ]); } return $fields;