/** * Sets up the test. */ protected function setUp() { parent::setUp(); $this->installEntitySchema('entity_test_string_id'); $this->installEntitySchema('taxonomy_term'); $this->installEntitySchema('node'); $this->installEntitySchema('comment'); $this->installEntitySchema('file'); $this->installSchema('comment', ['comment_entity_statistics']); $this->installSchema('node', ['node_access']); $this->vocabulary = Vocabulary::create(['name' => $this->randomMachineName(), 'vid' => Unicode::strtolower($this->randomMachineName()), 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED]); $this->vocabulary->save(); $this->term = Term::create(['name' => $this->randomMachineName(), 'vid' => $this->vocabulary->id(), 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED]); $this->term->save(); $this->entityStringId = EntityTestStringId::create(['id' => $this->randomMachineName()]); $this->entityStringId->save(); // Use the util to create an instance. $this->createEntityReferenceField('entity_test', 'entity_test', 'field_test_taxonomy_term', 'Test content entity reference', 'taxonomy_term'); $this->createEntityReferenceField('entity_test', 'entity_test', 'field_test_entity_test_string_id', 'Test content entity reference with string ID', 'entity_test_string_id'); $this->createEntityReferenceField('entity_test', 'entity_test', 'field_test_taxonomy_vocabulary', 'Test config entity reference', 'taxonomy_vocabulary'); $this->createEntityReferenceField('entity_test', 'entity_test', 'field_test_node', 'Test node entity reference', 'node', 'default', [], FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); $this->createEntityReferenceField('entity_test', 'entity_test', 'field_test_user', 'Test user entity reference', 'user'); $this->createEntityReferenceField('entity_test', 'entity_test', 'field_test_comment', 'Test comment entity reference', 'comment'); $this->createEntityReferenceField('entity_test', 'entity_test', 'field_test_file', 'Test file entity reference', 'file'); }
/** * Sets up the test. */ protected function setUp() { parent::setUp(); $this->installEntitySchema('entity_test_string_id'); $this->installEntitySchema('taxonomy_term'); $this->vocabulary = entity_create('taxonomy_vocabulary', array('name' => $this->randomMachineName(), 'vid' => Unicode::strtolower($this->randomMachineName()), 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED)); $this->vocabulary->save(); $this->term = entity_create('taxonomy_term', array('name' => $this->randomMachineName(), 'vid' => $this->vocabulary->id(), 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED)); $this->term->save(); $this->entityStringId = EntityTestStringId::create(['id' => $this->randomMachineName()]); $this->entityStringId->save(); // Use the util to create an instance. $this->createEntityReferenceField('entity_test', 'entity_test', 'field_test_taxonomy_term', 'Test content entity reference', 'taxonomy_term'); $this->createEntityReferenceField('entity_test', 'entity_test', 'field_test_entity_test_string_id', 'Test content entity reference with string ID', 'entity_test_string_id'); $this->createEntityReferenceField('entity_test', 'entity_test', 'field_test_taxonomy_vocabulary', 'Test config entity reference', 'taxonomy_vocabulary'); }
/** * Tests referencing entities with string IDs. */ public function testReferencedEntitiesStringId() { $field_name = 'entity_reference_string_id'; $this->installEntitySchema('entity_test_string_id'); $this->createEntityReferenceField($this->entityType, $this->bundle, $field_name, 'Field test', 'entity_test_string_id', 'default', array('target_bundles' => array($this->bundle)), FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); // Create the parent entity. $entity = $this->container->get('entity_type.manager')->getStorage($this->entityType)->create(array('type' => $this->bundle)); // Create the default target entity. $target_entity = EntityTestStringId::create(['id' => $this->randomString(), 'type' => $this->bundle]); $target_entity->save(); // Set the field value. $entity->{$field_name}->setValue(array(array('target_id' => $target_entity->id()))); // Load the target entities using EntityReferenceField::referencedEntities(). $entities = $entity->{$field_name}->referencedEntities(); $this->assertEqual($entities[0]->id(), $target_entity->id()); // Test that a string ID works as a default value and the field's config // schema is correct. $field = FieldConfig::loadByName($this->entityType, $this->bundle, $field_name); $field->setDefaultValue($target_entity->id()); $field->save(); $this->assertConfigSchema(\Drupal::service('config.typed'), 'field.field.' . $field->id(), $field->toArray()); // Test that the default value works. $entity = $this->container->get('entity_type.manager')->getStorage($this->entityType)->create(array('type' => $this->bundle)); $entities = $entity->{$field_name}->referencedEntities(); $this->assertEqual($entities[0]->id(), $target_entity->id()); }