public function FileEntityTypeTest::testTypeWithCandidates in File Entity (fieldable files) 8.2
Make sure candidates are presented in the case of multiple file types.
File
- tests/
src/ Functional/ FileEntityTypeTest.php, line 51
Class
- FileEntityTypeTest
- Tests the file entity types.
Namespace
Drupal\Tests\file_entity\FunctionalCode
public function testTypeWithCandidates() {
// Create multiple file types with the same mime types.
$types = array(
'image1' => $this
->createFileType(array(
'id' => 'image1',
'label' => 'Image 1',
)),
'image2' => $this
->createFileType(array(
'id' => 'image2',
'label' => 'Image 2',
)),
);
// Attach a text field to one of the file types.
$field_name = mb_strtolower($this
->randomMachineName());
$field_storage = FieldStorageConfig::create(array(
'field_name' => $field_name,
'entity_type' => 'file',
'type' => 'string',
));
$field_storage
->save();
$field_instance = FieldConfig::create(array(
'field_storage' => $field_storage,
'entity_type' => 'file',
'bundle' => 'image2',
));
$field_instance
->save();
\Drupal::service('entity_display.repository')
->getFormDisplay('file', 'image2')
->setComponent($field_name, array(
'type' => 'text_textfield',
))
->save();
// Create a user with file creation access.
$user = $this
->drupalCreateUser(array(
'create files',
));
$this
->drupalLogin($user);
// Step 1: Upload file.
$file = reset($this->files['image']);
$edit = array();
$edit['files[upload]'] = \Drupal::service('file_system')
->realpath($file
->getFileUri());
$this
->drupalPostForm('file/add', $edit, t('Next'));
// Step 2: Select file type candidate.
$this
->assertText('Image 1');
$this
->assertText('Image 2');
$edit = array();
$edit['type'] = 'image2';
$this
->drupalPostForm(NULL, $edit, t('Next'));
// Step 3: Select file scheme candidate.
$this
->assertText('Public local files served by the webserver.');
$this
->assertText('Private local files served by Drupal.');
$edit = array();
$edit['scheme'] = 'public';
$this
->drupalPostForm(NULL, $edit, t('Next'));
// Step 4: Complete field widgets.
$edit = array();
$edit["{$field_name}[0][value]"] = $this
->randomMachineName();
$edit['filename[0][value]'] = $this
->randomMachineName();
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertRaw(t('@type %name was uploaded.', array(
'@type' => 'Image 2',
'%name' => $edit['filename[0][value]'],
)));
// Check that the file exists in the database.
$file = $this
->getFileByFilename($edit['filename[0][value]']);
$this
->assertInstanceOf(FileInterface::class, $file, t('File found in database.'));
// Checks if configurable field exists in the database.
$this
->assertTrue($file
->hasField($field_name), 'Found configurable field in database');
}