Newer
Older
Alex Pott
committed
<?php
Alex Pott
committed
namespace Drupal\field\Tests\EntityReference;
Alex Pott
committed
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\field\Entity\FieldConfig;
Alex Pott
committed
use Drupal\simpletest\WebTestBase;
use Drupal\field\Entity\FieldStorageConfig;
Alex Pott
committed
/**
* Tests an autocomplete widget with file upload.
*
* @group entity_reference
*/
class EntityReferenceFileUploadTest extends WebTestBase {
public static $modules = ['entity_reference', 'node', 'file'];
Alex Pott
committed
/**
* The name of a content type that will reference $referencedType.
*
* @var string
*/
protected $referencingType;
/**
* The name of a content type that will be referenced by $referencingType.
*
* @var string
*/
protected $referencedType;
/**
* Node id.
*
Jennifer Hodgdon
committed
* @var int
Alex Pott
committed
*/
protected $nodeId;
protected function setUp() {
parent::setUp();
// Create "referencing" and "referenced" node types.
$referencing = $this->drupalCreateContentType();
$this->referencingType = $referencing->id();
$referenced = $this->drupalCreateContentType();
$this->referencedType = $referenced->id();
$this->nodeId = $this->drupalCreateNode(['type' => $referenced->id()])->id();
Alex Pott
committed
FieldStorageConfig::create([
Alex Pott
committed
'field_name' => 'test_field',
'entity_type' => 'node',
'translatable' => FALSE,
'entity_types' => [],
'settings' => [
Alex Pott
committed
'target_type' => 'node',
],
Alex Pott
committed
'type' => 'entity_reference',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
])->save();
Alex Pott
committed
FieldConfig::create([
Alex Pott
committed
'label' => 'Entity reference field',
'field_name' => 'test_field',
'entity_type' => 'node',
'required' => TRUE,
'bundle' => $referencing->id(),
'settings' => [
Alex Pott
committed
'handler' => 'default',
'handler_settings' => [
Alex Pott
committed
// Reference a single vocabulary.
'target_bundles' => [
Alex Pott
committed
$referenced->id(),
],
],
],
])->save();
Alex Pott
committed
// Create a file field.
$file_field_name = 'file_field';
$field_storage = FieldStorageConfig::create([
Alex Pott
committed
'field_name' => $file_field_name,
'entity_type' => 'node',
'type' => 'file'
]);
Alex Pott
committed
$field_storage->save();
FieldConfig::create([
Alex Pott
committed
'entity_type' => 'node',
'field_storage' => $field_storage,
'bundle' => $referencing->id(),
'label' => $this->randomMachineName() . '_label',
])->save();
Alex Pott
committed
entity_get_display('node', $referencing->id(), 'default')
->setComponent('test_field')
->setComponent($file_field_name)
->save();
entity_get_form_display('node', $referencing->id(), 'default')
->setComponent('test_field', [
Alex Pott
committed
'type' => 'entity_reference_autocomplete',
])
->setComponent($file_field_name, [
Alex Pott
committed
'type' => 'file_generic',
])
Alex Pott
committed
->save();
}
/**
* Tests that the autocomplete input element does not cause ajax fatal.
*/
public function testFileUpload() {
$user1 = $this->drupalCreateUser(['access content', "create $this->referencingType content"]);
Alex Pott
committed
$this->drupalLogin($user1);
$test_file = current($this->drupalGetTestFiles('text'));
$edit['files[file_field_0]'] = drupal_realpath($test_file->uri);
$this->drupalPostForm('node/add/' . $this->referencingType, $edit, 'Upload');
$this->assertResponse(200);
$edit = [
Alex Pott
committed
'title[0][value]' => $this->randomMachineName(),
'test_field[0][target_id]' => $this->nodeId,
];
Alex Pott
committed
$this->drupalPostForm(NULL, $edit, 'Save');
$this->assertResponse(200);
}