You are here

public function FileEntityTypeTest::testTypeWithoutCandidates in File Entity (fieldable files) 8.2

Make sure no candidates appear when only one mime type is available.

File

tests/src/Functional/FileEntityTypeTest.php, line 121

Class

FileEntityTypeTest
Tests the file entity types.

Namespace

Drupal\Tests\file_entity\Functional

Code

public function testTypeWithoutCandidates() {

  // Attach a text field to the default image file type.
  $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' => 'image',
  ));
  $field_instance
    ->save();
  \Drupal::entityTypeManager()
    ->getStorage('entity_form_display')
    ->load('file.image.default')
    ->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: Scheme selection.
  if ($this
    ->xpath('//input[@name="scheme"]')) {
    $this
      ->drupalPostForm(NULL, array(), t('Next'));
  }

  // Step 3: 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',
    '%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');
}