3 namespace Drupal\field\Tests\EntityReference;
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\simpletest\WebTestBase;
8 use Drupal\field\Entity\FieldStorageConfig;
11 * Tests an autocomplete widget with file upload.
13 * @group entity_reference
15 class EntityReferenceFileUploadTest extends WebTestBase {
17 public static $modules = ['entity_reference', 'node', 'file'];
20 * The name of a content type that will reference $referencedType.
24 protected $referencingType;
27 * The name of a content type that will be referenced by $referencingType.
31 protected $referencedType;
40 protected function setUp() {
43 // Create "referencing" and "referenced" node types.
44 $referencing = $this->drupalCreateContentType();
45 $this->referencingType = $referencing->id();
47 $referenced = $this->drupalCreateContentType();
48 $this->referencedType = $referenced->id();
49 $this->nodeId = $this->drupalCreateNode(['type' => $referenced->id()])->id();
51 FieldStorageConfig::create([
52 'field_name' => 'test_field',
53 'entity_type' => 'node',
54 'translatable' => FALSE,
57 'target_type' => 'node',
59 'type' => 'entity_reference',
60 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
64 'label' => 'Entity reference field',
65 'field_name' => 'test_field',
66 'entity_type' => 'node',
68 'bundle' => $referencing->id(),
70 'handler' => 'default',
71 'handler_settings' => [
72 // Reference a single vocabulary.
81 // Create a file field.
82 $file_field_name = 'file_field';
83 $field_storage = FieldStorageConfig::create([
84 'field_name' => $file_field_name,
85 'entity_type' => 'node',
88 $field_storage->save();
90 'entity_type' => 'node',
91 'field_storage' => $field_storage,
92 'bundle' => $referencing->id(),
93 'label' => $this->randomMachineName() . '_label',
96 entity_get_display('node', $referencing->id(), 'default')
97 ->setComponent('test_field')
98 ->setComponent($file_field_name)
100 entity_get_form_display('node', $referencing->id(), 'default')
101 ->setComponent('test_field', [
102 'type' => 'entity_reference_autocomplete',
104 ->setComponent($file_field_name, [
105 'type' => 'file_generic',
111 * Tests that the autocomplete input element does not cause ajax fatal.
113 public function testFileUpload() {
114 $user1 = $this->drupalCreateUser(['access content', "create $this->referencingType content"]);
115 $this->drupalLogin($user1);
117 $test_file = current($this->drupalGetTestFiles('text'));
118 $edit['files[file_field_0]'] = drupal_realpath($test_file->uri);
119 $this->drupalPostForm('node/add/' . $this->referencingType, $edit, 'Upload');
120 $this->assertResponse(200);
122 'title[0][value]' => $this->randomMachineName(),
123 'test_field[0][target_id]' => $this->nodeId,
125 $this->drupalPostForm(NULL, $edit, 'Save');
126 $this->assertResponse(200);