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.
80 // Create a file field.
81 $file_field_name = 'file_field';
82 $field_storage = FieldStorageConfig::create([
83 'field_name' => $file_field_name,
84 'entity_type' => 'node',
87 $field_storage->save();
89 'entity_type' => 'node',
90 'field_storage' => $field_storage,
91 'bundle' => $referencing->id(),
92 'label' => $this->randomMachineName() . '_label',
95 entity_get_display('node', $referencing->id(), 'default')
96 ->setComponent('test_field')
97 ->setComponent($file_field_name)
99 entity_get_form_display('node', $referencing->id(), 'default')
100 ->setComponent('test_field', [
101 'type' => 'entity_reference_autocomplete',
103 ->setComponent($file_field_name, [
104 'type' => 'file_generic',
110 * Tests that the autocomplete input element does not cause ajax fatal.
112 public function testFileUpload() {
113 $user1 = $this->drupalCreateUser(['access content', "create $this->referencingType content"]);
114 $this->drupalLogin($user1);
116 $test_file = current($this->drupalGetTestFiles('text'));
117 $edit['files[file_field_0]'] = drupal_realpath($test_file->uri);
118 $this->drupalPostForm('node/add/' . $this->referencingType, $edit, 'Upload');
119 $this->assertResponse(200);
121 'title[0][value]' => $this->randomMachineName(),
122 'test_field[0][target_id]' => $this->nodeId,
124 $this->drupalPostForm(NULL, $edit, 'Save');
125 $this->assertResponse(200);