More updates to stop using dev or alpha or beta versions.
[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     // 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',
85       'type' => 'file'
86     ]);
87     $field_storage->save();
88     FieldConfig::create([
89       'entity_type' => 'node',
90       'field_storage' => $field_storage,
91       'bundle' => $referencing->id(),
92       'label' => $this->randomMachineName() . '_label',
93     ])->save();
94
95     entity_get_display('node', $referencing->id(), 'default')
96       ->setComponent('test_field')
97       ->setComponent($file_field_name)
98       ->save();
99     entity_get_form_display('node', $referencing->id(), 'default')
100       ->setComponent('test_field', [
101         'type' => 'entity_reference_autocomplete',
102       ])
103       ->setComponent($file_field_name, [
104          'type' => 'file_generic',
105       ])
106       ->save();
107   }
108
109   /**
110    * Tests that the autocomplete input element does not cause ajax fatal.
111    */
112   public function testFileUpload() {
113     $user1 = $this->drupalCreateUser(['access content', "create $this->referencingType content"]);
114     $this->drupalLogin($user1);
115
116     $test_file = current($this->drupalGetTestFiles('text'));
117     $edit['files[file_field_0]'] = \Drupal::service('file_system')->realpath($test_file->uri);
118     $this->drupalPostForm('node/add/' . $this->referencingType, $edit, 'Upload');
119     $this->assertResponse(200);
120     $edit = [
121       'title[0][value]' => $this->randomMachineName(),
122       'test_field[0][target_id]' => $this->nodeId,
123     ];
124     $this->drupalPostForm(NULL, $edit, 'Save');
125     $this->assertResponse(200);
126   }
127
128 }