protected function setUp()
 {
     parent::setUp();
     $this->installEntitySchema('entity_test_rev');
     // Create a field.
     entity_reference_create_field($this->entityType, $this->bundle, $this->fieldName, 'Field test', $this->referencedEntityType, 'default', array('target_bundles' => array($this->bundle)), FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
 }
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     parent::setUp();
     $this->role1 = entity_create('user_role', array('id' => strtolower($this->randomMachineName(8)), 'label' => $this->randomMachineName(8)));
     $this->role1->save();
     $this->role2 = entity_create('user_role', array('id' => strtolower($this->randomMachineName(8)), 'label' => $this->randomMachineName(8)));
     $this->role2->save();
     entity_reference_create_field('user', 'user', 'user_reference', 'User reference', 'user');
 }
 /**
  * Sets up the test.
  */
 protected function setUp()
 {
     parent::setUp();
     $this->installEntitySchema('taxonomy_term');
     $this->vocabulary = entity_create('taxonomy_vocabulary', array('name' => $this->randomMachineName(), 'vid' => drupal_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();
     // Use the util to create an instance.
     entity_reference_create_field('entity_test', 'entity_test', 'field_test_taxonomy_term', 'Test content entity reference', 'taxonomy_term');
     entity_reference_create_field('entity_test', 'entity_test', 'field_test_taxonomy_vocabulary', 'Test config entity reference', 'taxonomy_vocabulary');
 }
 protected function setUp()
 {
     parent::setUp();
     entity_reference_create_field($this->entityType, $this->bundle, $this->fieldName, 'Field test', $this->entityType);
     // Set up a field, so that the entity that'll be referenced bubbles up a
     // cache tag when rendering it entirely.
     entity_create('field_storage_config', array('field_name' => 'body', 'entity_type' => $this->entityType, 'type' => 'text', 'settings' => array()))->save();
     entity_create('field_config', array('entity_type' => $this->entityType, 'bundle' => $this->bundle, 'field_name' => 'body', 'label' => 'Body'))->save();
     entity_get_display($this->entityType, $this->bundle, 'default')->setComponent('body', array('type' => 'text_default', 'settings' => array()))->save();
     entity_create('filter_format', array('format' => 'full_html', 'name' => 'Full HTML'))->save();
     // Create the entity to be referenced.
     $this->referencedEntity = entity_create($this->entityType, array('name' => $this->randomMachineName()));
     $this->referencedEntity->body = array('value' => '<p>Hello, world!</p>', 'format' => 'full_html');
     $this->referencedEntity->save();
 }
 /**
  * Tests the entity reference field with all its supported field widgets.
  */
 public function testSupportedEntityTypesAndWidgets()
 {
     foreach ($this->getTestEntities() as $referenced_entities) {
         $this->fieldName = 'field_test_' . $referenced_entities[0]->getEntityTypeId();
         // Create an Entity reference field.
         entity_reference_create_field($this->entityType, $this->bundle, $this->fieldName, $this->fieldName, $referenced_entities[0]->getEntityTypeId(), 'default', array(), 2);
         // Test the default 'entity_reference_autocomplete' widget.
         entity_get_form_display($this->entityType, $this->bundle, 'default')->setComponent($this->fieldName)->save();
         $entity_name = $this->randomMachineName();
         $edit = array('name[0][value]' => $entity_name, $this->fieldName . '[0][target_id]' => $referenced_entities[0]->label() . ' (' . $referenced_entities[0]->id() . ')', $this->fieldName . '[1][target_id]' => $referenced_entities[1]->label());
         $this->drupalPostForm($this->entityType . '/add', $edit, t('Save'));
         $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->drupalPostForm($this->entityType . '/manage/' . $entity->id(), array(), t('Save'));
         $this->assertFieldValues($entity_name, $referenced_entities);
         // Test the 'entity_reference_autocomplete_tags' widget.
         entity_get_form_display($this->entityType, $this->bundle, 'default')->setComponent($this->fieldName, array('type' => 'entity_reference_autocomplete_tags'))->save();
         $entity_name = $this->randomMachineName();
         $target_id = $referenced_entities[0]->label() . ' (' . $referenced_entities[0]->id() . ')';
         // Test an input of the entity label without a ' (entity_id)' suffix.
         $target_id .= ', ' . $referenced_entities[1]->label();
         $edit = array('name[0][value]' => $entity_name, $this->fieldName . '[target_id]' => $target_id);
         $this->drupalPostForm($this->entityType . '/add', $edit, t('Save'));
         $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->drupalPostForm($this->entityType . '/manage/' . $entity->id(), array(), t('Save'));
         $this->assertFieldValues($entity_name, $referenced_entities);
         // Test all the other widgets supported by the entity reference field.
         // Since we don't know the form structure for these widgets, just test
         // that editing and saving an already created entity works.
         $exclude = array('entity_reference_autocomplete', 'entity_reference_autocomplete_tags');
         $entity = current(entity_load_multiple_by_properties($this->entityType, array('name' => $entity_name)));
         $supported_widgets = \Drupal::service('plugin.manager.field.widget')->getOptions('entity_reference');
         $supported_widget_types = array_diff(array_keys($supported_widgets), $exclude);
         foreach ($supported_widget_types as $widget_type) {
             entity_get_form_display($this->entityType, $this->bundle, 'default')->setComponent($this->fieldName, array('type' => $widget_type))->save();
             $this->drupalPostForm($this->entityType . '/manage/' . $entity->id(), array(), t('Save'));
             $this->assertFieldValues($entity_name, $referenced_entities);
         }
     }
 }
 protected function setUp()
 {
     parent::setUp();
     $this->installEntitySchema('entity_test_rev');
     entity_reference_create_field($this->entityType, $this->bundle, $this->fieldName, 'Field test', $this->entityType);
     // Add the mapping.
     $mapping = rdf_get_mapping('entity_test', 'entity_test');
     $mapping->setFieldMapping($this->fieldName, array('properties' => array('schema:knows')))->save();
     // Create the entity to be referenced.
     $this->target_entity = entity_create($this->entityType, array('name' => $this->randomMachineName()));
     $this->target_entity->save();
     // Create the entity that will have the entity reference field.
     $this->entity = entity_create($this->entityType, array('name' => $this->randomMachineName()));
     $this->entity->save();
     $this->entity->{$this->fieldName}->entity = $this->target_entity;
     $this->entity->{$this->fieldName}->access = TRUE;
     $this->uri = $this->getAbsoluteUri($this->entity);
 }
 /**
  * Tests entity render cache with references.
  */
 public function testEntityViewBuilderCacheWithReferences()
 {
     // Force a request via GET so we can get drupal_render() cache working.
     $request = \Drupal::request();
     $request_method = $request->server->get('REQUEST_METHOD');
     $request->setMethod('GET');
     // Create an entity reference field and an entity that will be referenced.
     entity_reference_create_field('entity_test', 'entity_test', 'reference_field', 'Reference', 'entity_test');
     entity_get_display('entity_test', 'entity_test', 'full')->setComponent('reference_field', ['type' => 'entity_reference_entity_view', 'settings' => ['link' => FALSE]])->save();
     $entity_test_reference = $this->createTestEntity('entity_test');
     $entity_test_reference->save();
     // Get a fully built entity view render array for the referenced entity.
     $build = $this->container->get('entity.manager')->getViewBuilder('entity_test')->view($entity_test_reference, 'full');
     $cid_reference = drupal_render_cid_create($build);
     $bin_reference = $build['#cache']['bin'];
     // Mock the build array to not require the theme registry.
     unset($build['#theme']);
     $build['#markup'] = 'entity_render_test';
     drupal_render($build);
     // Test that a cache entry was created for the referenced entity.
     $this->assertTrue($this->container->get('cache.' . $bin_reference)->get($cid_reference), 'The entity render element for the referenced entity has been cached.');
     // Create another entity that references the first one.
     $entity_test = $this->createTestEntity('entity_test');
     $entity_test->reference_field->entity = $entity_test_reference;
     $entity_test->reference_field->access = TRUE;
     $entity_test->save();
     // Get a fully built entity view render array.
     $build = $this->container->get('entity.manager')->getViewBuilder('entity_test')->view($entity_test, 'full');
     $cid = drupal_render_cid_create($build);
     $bin = $build['#cache']['bin'];
     // Mock the build array to not require the theme registry.
     unset($build['#theme']);
     $build['#markup'] = 'entity_render_test';
     drupal_render($build);
     // Test that a cache entry is created.
     $this->assertTrue($this->container->get('cache.' . $bin)->get($cid), 'The entity render element has been cached.');
     // Save the entity and verify that both cache entries have been deleted.
     $entity_test_reference->save();
     $this->assertFalse($this->container->get('cache.' . $bin)->get($cid), 'The entity render cache has been cleared when the entity was deleted.');
     $this->assertFalse($this->container->get('cache.' . $bin_reference)->get($cid_reference), 'The entity render cache for the referenced entity has been cleared when the entity was deleted.');
     // Restore the previous request method.
     $request->setMethod($request_method);
 }
 /**
  * Tests the entity reference field with all its supported field widgets.
  */
 public function testSupportedEntityTypesAndWidgets()
 {
     foreach ($this->getTestEntities() as $key => $referenced_entities) {
         $this->fieldName = 'field_test_' . $referenced_entities[0]->getEntityTypeId();
         // Create an Entity reference field.
         entity_reference_create_field($this->entityType, $this->bundle, $this->fieldName, $this->fieldName, $referenced_entities[0]->getEntityTypeId(), 'default', array(), 2);
         // Test the default 'entity_reference_autocomplete' widget.
         entity_get_form_display($this->entityType, $this->bundle, 'default')->setComponent($this->fieldName)->save();
         $entity_name = $this->randomMachineName();
         $edit = array('name[0][value]' => $entity_name, $this->fieldName . '[0][target_id]' => $referenced_entities[0]->label() . ' (' . $referenced_entities[0]->id() . ')', $this->fieldName . '[1][target_id]' => $referenced_entities[1]->label());
         $this->drupalPostForm($this->entityType . '/add', $edit, t('Save'));
         $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->drupalPostForm($this->entityType . '/manage/' . $entity->id(), array(), t('Save'));
         $this->assertFieldValues($entity_name, $referenced_entities);
         // Test the 'entity_reference_autocomplete_tags' widget.
         entity_get_form_display($this->entityType, $this->bundle, 'default')->setComponent($this->fieldName, array('type' => 'entity_reference_autocomplete_tags'))->save();
         $entity_name = $this->randomMachineName();
         $target_id = $referenced_entities[0]->label() . ' (' . $referenced_entities[0]->id() . ')';
         // Test an input of the entity label without a ' (entity_id)' suffix.
         $target_id .= ', ' . $referenced_entities[1]->label();
         $edit = array('name[0][value]' => $entity_name, $this->fieldName . '[target_id]' => $target_id);
         $this->drupalPostForm($this->entityType . '/add', $edit, t('Save'));
         $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->drupalPostForm($this->entityType . '/manage/' . $entity->id(), array(), t('Save'));
         $this->assertFieldValues($entity_name, $referenced_entities);
         // Test all the other widgets supported by the entity reference field.
         // Since we don't know the form structure for these widgets, just test
         // that editing and saving an already created entity works.
         $exclude = array('entity_reference_autocomplete', 'entity_reference_autocomplete_tags');
         $entity = current(entity_load_multiple_by_properties($this->entityType, array('name' => $entity_name)));
         $supported_widgets = \Drupal::service('plugin.manager.field.widget')->getOptions('entity_reference');
         $supported_widget_types = array_diff(array_keys($supported_widgets), $exclude);
         foreach ($supported_widget_types as $widget_type) {
             entity_get_form_display($this->entityType, $this->bundle, 'default')->setComponent($this->fieldName, array('type' => $widget_type))->save();
             $this->drupalPostForm($this->entityType . '/manage/' . $entity->id(), array(), t('Save'));
             $this->assertFieldValues($entity_name, $referenced_entities);
         }
         // Reset to the default 'entity_reference_autocomplete' widget.
         entity_get_form_display($this->entityType, $this->bundle, 'default')->setComponent($this->fieldName)->save();
         // Set first entity as the default_value.
         $field_edit = array('default_value_input[' . $this->fieldName . '][0][target_id]' => $referenced_entities[0]->label() . ' (' . $referenced_entities[0]->id() . ')');
         if ($key == 'content') {
             $field_edit['field[settings][handler_settings][target_bundles][' . $referenced_entities[0]->getEntityTypeId() . ']'] = TRUE;
         }
         $this->drupalPostForm($this->entityType . '/structure/' . $this->bundle . '/fields/' . $this->entityType . '.' . $this->bundle . '.' . $this->fieldName, $field_edit, t('Save settings'));
         // Ensure the configuration has the expected dependency on the entity that
         // is being used a default value.
         $field = FieldConfig::loadByName($this->entityType, $this->bundle, $this->fieldName);
         $this->assertTrue(in_array($referenced_entities[0]->getConfigDependencyName(), $field->getDependencies()[$key]), String::format('Expected @type dependency @name found', ['@type' => $key, '@name' => $referenced_entities[0]->getConfigDependencyName()]));
         // Ensure that the field can be imported without change even after the
         // default value deleted.
         $referenced_entities[0]->delete();
         $this->assertConfigEntityImport($field);
         // Once the default value has been removed after saving the dependency
         // should be removed.
         $field = FieldConfig::loadByName($this->entityType, $this->bundle, $this->fieldName);
         $field->save();
         $dependencies = $field->getDependencies();
         $this->assertFalse(isset($dependencies[$key]) && in_array($referenced_entities[0]->getConfigDependencyName(), $dependencies[$key]), String::format('@type dependency @name does not exist.', ['@type' => $key, '@name' => $referenced_entities[0]->getConfigDependencyName()]));
     }
 }
 protected function setUp()
 {
     parent::setUp();
     entity_reference_create_field($this->entityType, $this->bundle, $this->fieldName, 'Field test', $this->entityType);
 }