class EntityReferenceFileUploadTest
Same name and namespace in other branches
- 11.x core/modules/field/tests/src/Functional/EntityReference/EntityReferenceFileUploadTest.php \Drupal\Tests\field\Functional\EntityReference\EntityReferenceFileUploadTest
- 10 core/modules/field/tests/src/Functional/EntityReference/EntityReferenceFileUploadTest.php \Drupal\Tests\field\Functional\EntityReference\EntityReferenceFileUploadTest
- 8.9.x core/modules/field/tests/src/Functional/EntityReference/EntityReferenceFileUploadTest.php \Drupal\Tests\field\Functional\EntityReference\EntityReferenceFileUploadTest
Tests an autocomplete widget with file upload.
@group entity_reference
Hierarchy
- class \Drupal\Tests\BrowserTestBase uses \Drupal\Core\Test\FunctionalTestSetupTrait, \Drupal\Tests\UiHelperTrait, \Drupal\Core\Test\TestSetupTrait, \Drupal\Tests\block\Traits\BlockCreationTrait, \Drupal\FunctionalTests\AssertLegacyTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\node\Traits\NodeCreationTrait, \Drupal\Tests\node\Traits\ContentTypeCreationTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\user\Traits\UserCreationTrait, \Drupal\Tests\XdebugRequestTrait, \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, \Drupal\Tests\ExtensionListTestTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\field\Functional\EntityReference\EntityReferenceFileUploadTest uses \Drupal\Tests\TestFileCreationTrait extends \Drupal\Tests\BrowserTestBase
Expanded class hierarchy of EntityReferenceFileUploadTest
File
-
core/
modules/ field/ tests/ src/ Functional/ EntityReference/ EntityReferenceFileUploadTest.php, line 16
Namespace
Drupal\Tests\field\Functional\EntityReferenceView source
class EntityReferenceFileUploadTest extends BrowserTestBase {
use TestFileCreationTrait;
protected static $modules = [
'node',
'file',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* 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.
*
* @var int
*/
protected $nodeId;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
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();
FieldStorageConfig::create([
'field_name' => 'test_field',
'entity_type' => 'node',
'translatable' => FALSE,
'entity_types' => [],
'settings' => [
'target_type' => 'node',
],
'type' => 'entity_reference',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
])->save();
FieldConfig::create([
'label' => 'Entity reference field',
'field_name' => 'test_field',
'entity_type' => 'node',
'required' => TRUE,
'bundle' => $referencing->id(),
'settings' => [
'handler' => 'default',
'handler_settings' => [
// Reference a single vocabulary.
'target_bundles' => [
$referenced->id(),
],
],
],
])
->save();
// Create a file field.
$file_field_name = 'file_field';
$field_storage = FieldStorageConfig::create([
'field_name' => $file_field_name,
'entity_type' => 'node',
'type' => 'file',
]);
$field_storage->save();
FieldConfig::create([
'entity_type' => 'node',
'field_storage' => $field_storage,
'bundle' => $referencing->id(),
'label' => $this->randomMachineName() . '_label',
])
->save();
/** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
$display_repository = \Drupal::service('entity_display.repository');
$display_repository->getViewDisplay('node', $referencing->id())
->setComponent('test_field')
->setComponent($file_field_name)
->save();
$display_repository->getFormDisplay('node', $referencing->id())
->setComponent('test_field', [
'type' => 'entity_reference_autocomplete',
])
->setComponent($file_field_name, [
'type' => 'file_generic',
])
->save();
}
/**
* Tests that the autocomplete input element does not cause ajax fatal.
*/
public function testFileUpload() {
$user1 = $this->drupalCreateUser([
'access content',
"create {$this->referencingType} content",
]);
$this->drupalLogin($user1);
$test_file = current($this->getTestFiles('text'));
$edit['files[file_field_0]'] = \Drupal::service('file_system')->realpath($test_file->uri);
$this->drupalGet('node/add/' . $this->referencingType);
$this->submitForm($edit, 'Upload');
$this->assertSession()
->statusCodeEquals(200);
$edit = [
'title[0][value]' => $this->randomMachineName(),
'test_field[0][target_id]' => $this->nodeId,
];
$this->submitForm($edit, 'Save');
$this->assertSession()
->statusCodeEquals(200);
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.