diff --git a/core/modules/entity_reference/src/Tests/DynamicEntityReferenceIntegrationTest.php b/core/modules/entity_reference/src/Tests/DynamicEntityReferenceIntegrationTest.php index b4ac5d2..c12627c 100644 --- a/core/modules/entity_reference/src/Tests/DynamicEntityReferenceIntegrationTest.php +++ b/core/modules/entity_reference/src/Tests/DynamicEntityReferenceIntegrationTest.php @@ -13,7 +13,7 @@ use Drupal\simpletest\WebTestBase; /** - * Tests various Entity reference UI components. + * Tests various Dynamic Entity reference base field. * * @group entity_reference */ @@ -25,14 +25,14 @@ class DynamicEntityReferenceIntegrationTest extends WebTestBase { * * @var string */ - protected $entityType = 'entity_test'; + protected $entityType = 'dynamic_entity_test'; /** * The bundle used in this test. * * @var string */ - protected $bundle = 'entity_test'; + protected $bundle = 'dynamic_entity_test'; /** * The name of the field used in this test. @@ -53,7 +53,7 @@ class DynamicEntityReferenceIntegrationTest extends WebTestBase { * * @var array */ - public static $modules = array('config_test', 'entity_test', 'entity_reference', 'field_ui', 'dynamic_entity_reference_test'); + public static $modules = array('entity_reference', 'field_ui', 'dynamic_entity_reference_test'); /** * {@inheritdoc} @@ -62,29 +62,71 @@ protected function setUp() { parent::setUp(); // Create a test user. - $this->webUser = $this->drupalCreateUser(array('administer entity_test content', 'administer entity_test fields')); + $this->webUser = $this->drupalCreateUser(array('administer dynamic_entity_test content')); $this->drupalLogin($this->webUser); - \Drupal::service('entity.definition_update_manager')->applyUpdates(); } /** - * Tests the entity reference field with all its supported field widgets. + * Tests the dynamic entity reference base field. */ public function testDynamicEntityTypesAndWidgets() { - $entity_name = $this->randomMachineName(); + $entity_name1 = $this->randomMachineName(); $edit = array( - 'name[0][value]' => $entity_name, + 'name[0][value]' => $entity_name1, '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'); + $entity1 = current(entity_load_multiple_by_properties($this->entityType, array('name' => $entity_name1))); + $this->assertEqual($entity1->dynamic_references->target_id, $this->webUser->id()); + $this->assertEqual($entity1->dynamic_references->target_type, 'user'); + $this->assertEqual($entity1->dynamic_references->entity->id(), $this->webUser->id()); + $this->assertEqual($entity1->dynamic_references->entity->label(), $this->webUser->label()); + + $entity_name2 = $this->randomMachineName(); + $edit = array( + 'name[0][value]' => $entity_name2, + 'dynamic_references[0][target_type]' => $this->entityType, + 'dynamic_references[0][target_id]' => $entity1->label() . ' (' . $entity1->id() . ')', + ); + $this->drupalPostForm($this->entityType . '/add', $edit, t('Save')); + + $entity2 = current(entity_load_multiple_by_properties($this->entityType, array('name' => $entity_name2))); + $this->assertEqual($entity2->dynamic_references->target_id, $entity1->id()); + $this->assertEqual($entity2->dynamic_references->target_type, $this->entityType); + $this->assertEqual($entity2->dynamic_references->entity->id(), $entity1->id()); + $this->assertEqual($entity2->dynamic_references->entity->label(), $entity1->label()); + + // Edit the reference to be to the user. + $edit = array( + 'dynamic_references[0][target_type]' => 'user', + 'dynamic_references[0][target_id]' => $this->webUser->label() . ' (' . $this->webUser->id() . ')', + ); + $this->drupalPostForm($this->entityType . '/' . $entity2->id() . '/edit', $edit, t('Save')); + $entity2 = entity_load($this->entityType, $entity2->id(), TRUE); + $this->assertEqual($entity2->dynamic_references->target_id, $this->webUser->id()); + $this->assertEqual($entity2->dynamic_references->target_type, 'user'); + $this->assertEqual($entity2->dynamic_references->entity->id(), $this->webUser->id()); + $this->assertEqual($entity2->dynamic_references->entity->label(), $this->webUser->label()); + + + /* + // Test another input of the entity label without a ' (entity_id)' suffix. + $entity_name3 = $this->randomMachineName(); + $edit = array( + 'name[0][value]' => $entity_name3, + 'dynamic_references[0][target_type]' => 'user', + 'dynamic_references[0][target_id]' => $this->webUser->label(), + ); + $this->drupalPostForm($this->entityType . '/add', $edit, t('Save')); + $entity3 = current(entity_load_multiple_by_properties($this->entityType, array('name' => $entity_name3))); + $this->assertEqual($entity3->dynamic_references->target_id, $this->webUser->id()); + $this->assertEqual($entity3->dynamic_references->target_type, 'user'); + $this->assertEqual($entity3->dynamic_references->entity->id(), $this->webUser->id()); + $this->assertEqual($entity3->dynamic_references->entity->label(), $this->webUser->label()); + */ } 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 deleted file mode 100644 index c7d81ab..0000000 --- a/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/dynamic_entity_reference_test.module +++ /dev/null @@ -1,30 +0,0 @@ -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/dynamic_entity_reference_test/dynamic_entity_reference_test.permissions.yml b/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/dynamic_entity_reference_test.permissions.yml new file mode 100644 index 0000000..84bf73b --- /dev/null +++ b/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/dynamic_entity_reference_test.permissions.yml @@ -0,0 +1,3 @@ +administer dynamic_entity_test content: + title: 'Administer dynamic_entity_test content' + description: 'Manage dynamic_entity_test content' \ No newline at end of file diff --git a/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/dynamic_entity_reference_test.routing.yml b/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/dynamic_entity_reference_test.routing.yml new file mode 100644 index 0000000..be0e0a8 --- /dev/null +++ b/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/dynamic_entity_reference_test.routing.yml @@ -0,0 +1,23 @@ +entity.dynamic_entity_test.canonical: + path: '/dynamic_entity_test/{dynamic_entity_test}' + defaults: + _entity_view: 'dynamic_entity_test.full' + _title: 'Test full view mode' + requirements: + _access: 'TRUE' + +entity.dynamic_entity_test.edit_form: + path: '/dynamic_entity_test/{dynamic_entity_test}/edit' + defaults: + _title: 'Edit' + _entity_form: 'dynamic_entity_test.default' + requirements: + _entity_access: 'dynamic_entity_test.update' + +entity.dynamic_entity_test.add_form: + path: '/dynamic_entity_test/add' + defaults: + _title: 'Add' + _controller: '\Drupal\dynamic_entity_reference_test\Controller\DynamicEntityController::addForm' + requirements: + _permission: 'administer dynamic_entity_test content' \ No newline at end of file diff --git a/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/src/Controller/DynamicEntityController.php b/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/src/Controller/DynamicEntityController.php new file mode 100644 index 0000000..292ac70 --- /dev/null +++ b/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/src/Controller/DynamicEntityController.php @@ -0,0 +1,28 @@ +entityManager()->getStorage('dynamic_entity_test')->create(); + return $this->entityFormBuilder()->getForm($entity); + } + +} \ No newline at end of file diff --git a/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/src/DynamicEntityTestAccessControlHandler.php b/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/src/DynamicEntityTestAccessControlHandler.php new file mode 100644 index 0000000..5e427cf --- /dev/null +++ b/core/modules/entity_reference/tests/modules/dynamic_entity_reference_test/src/DynamicEntityTestAccessControlHandler.php @@ -0,0 +1,43 @@ +getEntityTypeId(); + } + } + + /** + * {@inheritdoc} + */ + public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { + $fields['id'] = BaseFieldDefinition::create('integer') + ->setLabel(t('ID')) + ->setDescription(t('The ID of the test entity.')) + ->setReadOnly(TRUE) + ->setSetting('unsigned', TRUE); + + $fields['uuid'] = BaseFieldDefinition::create('uuid') + ->setLabel(t('UUID')) + ->setDescription(t('The UUID of the test entity.')) + ->setReadOnly(TRUE); + + $fields['langcode'] = BaseFieldDefinition::create('language') + ->setLabel(t('Language code')) + ->setDescription(t('The language code of the test entity.')); + + $fields['name'] = BaseFieldDefinition::create('string') + ->setLabel(t('Name')) + ->setDescription(t('The name of the test entity.')) + ->setTranslatable(TRUE) + ->setSetting('max_length', 32) + ->setDisplayOptions('view', array( + 'label' => 'hidden', + 'type' => 'string', + 'weight' => -5, + )) + ->setDisplayOptions('form', array( + 'type' => 'string_textfield', + 'weight' => -5, + )); + + // @todo: Add allowed values validation. + $fields['type'] = BaseFieldDefinition::create('string') + ->setLabel(t('Type')) + ->setDescription(t('The bundle of the test entity.')) + ->setRequired(TRUE); + + $fields['user_id'] = BaseFieldDefinition::create('entity_reference') + ->setLabel(t('User ID')) + ->setDescription(t('The ID of the associated user.')) + ->setSetting('target_type', 'user') + ->setSetting('handler', 'default') + // Default EntityTest entities to have the root user as the owner, to + // simplify testing. + ->setDefaultValue(array(0 => array('target_id' => 1))) + ->setTranslatable(TRUE) + ->setDisplayOptions('form', array( + 'type' => 'entity_reference_autocomplete', + 'weight' => -1, + 'settings' => array( + 'match_operator' => 'CONTAINS', + 'size' => '60', + 'autocomplete_type' => 'tags', + 'placeholder' => '', + ), + )); + $fields['dynamic_references'] = BaseFieldDefinition::create('dynamic_entity_reference') + ->setLabel(t('References')) + ->setDescription(t('Reference another entity.')) + ->setSetting('handler', 'default') + ->setRequired(FALSE) + ->setDisplayOptions('form', [ + 'type' => 'dynamic_entity_reference_default', + 'weight' => 10, + ]); + + return $fields; + } + + /** + * {@inheritdoc} + */ + public function getOwner() { + return $this->get('user_id')->entity; + } + + /** + * {@inheritdoc} + */ + public function getOwnerId() { + return $this->get('user_id')->target_id; + } + + /** + * {@inheritdoc} + */ + public function setOwnerId($uid) { + $this->set('user_id', $uid); + return $this; + } + + /** + * {@inheritdoc} + */ + public function setOwner(UserInterface $account) { + $this->set('user_id', $account->id()); + return $this; + } + + /** + * Sets the name. + * + * @param string $name + * Name of the entity. + * + * @return $this + */ + public function setName($name) { + $this->set('name', $name); + return $this; + } + + /** + * Returns the name. + * + * @return string + */ + public function getName() { + return $this->get('name')->value; + } + +}