Version 1
[yaffs-website] / web / core / modules / field / src / Tests / EntityReference / EntityReferenceFileUploadTest.php
1 <?php
2
3 namespace Drupal\field\Tests\EntityReference;
4
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\simpletest\WebTestBase;
8 use Drupal\field\Entity\FieldStorageConfig;
9
10 /**
11  * Tests an autocomplete widget with file upload.
12  *
13  * @group entity_reference
14  */
15 class EntityReferenceFileUploadTest extends WebTestBase {
16
17   public static $modules = ['entity_reference', 'node', 'file'];
18
19   /**
20    * The name of a content type that will reference $referencedType.
21    *
22    * @var string
23    */
24   protected $referencingType;
25
26   /**
27    * The name of a content type that will be referenced by $referencingType.
28    *
29    * @var string
30    */
31   protected $referencedType;
32
33   /**
34    * Node id.
35    *
36    * @var int
37    */
38   protected $nodeId;
39
40   protected function setUp() {
41     parent::setUp();
42
43     // Create "referencing" and "referenced" node types.
44     $referencing = $this->drupalCreateContentType();
45     $this->referencingType = $referencing->id();
46
47     $referenced = $this->drupalCreateContentType();
48     $this->referencedType = $referenced->id();
49     $this->nodeId = $this->drupalCreateNode(['type' => $referenced->id()])->id();
50
51     FieldStorageConfig::create([
52       'field_name' => 'test_field',
53       'entity_type' => 'node',
54       'translatable' => FALSE,
55       'entity_types' => [],
56       'settings' => [
57         'target_type' => 'node',
58       ],
59       'type' => 'entity_reference',
60       'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
61     ])->save();
62
63     FieldConfig::create([
64       'label' => 'Entity reference field',
65       'field_name' => 'test_field',
66       'entity_type' => 'node',
67       'required' => TRUE,
68       'bundle' => $referencing->id(),
69       'settings' => [
70         'handler' => 'default',
71         'handler_settings' => [
72           // Reference a single vocabulary.
73           'target_bundles' => [
74             $referenced->id(),
75           ],
76         ],
77       ],
78     ])->save();
79
80
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',
86       'type' => 'file'
87     ]);
88     $field_storage->save();
89     FieldConfig::create([
90       'entity_type' => 'node',
91       'field_storage' => $field_storage,
92       'bundle' => $referencing->id(),
93       'label' => $this->randomMachineName() . '_label',
94     ])->save();
95
96     entity_get_display('node', $referencing->id(), 'default')
97       ->setComponent('test_field')
98       ->setComponent($file_field_name)
99       ->save();
100     entity_get_form_display('node', $referencing->id(), 'default')
101       ->setComponent('test_field', [
102         'type' => 'entity_reference_autocomplete',
103       ])
104       ->setComponent($file_field_name, [
105          'type' => 'file_generic',
106       ])
107       ->save();
108   }
109
110   /**
111    * Tests that the autocomplete input element does not cause ajax fatal.
112    */
113   public function testFileUpload() {
114     $user1 = $this->drupalCreateUser(['access content', "create $this->referencingType content"]);
115     $this->drupalLogin($user1);
116
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);
121     $edit = [
122       'title[0][value]' => $this->randomMachineName(),
123       'test_field[0][target_id]' => $this->nodeId,
124     ];
125     $this->drupalPostForm(NULL, $edit, 'Save');
126     $this->assertResponse(200);
127   }
128
129 }