You are here

public function WebformElementManagedFileTest::testFileUpload in Webform 8.5

Same name and namespace in other branches
  1. 6.x tests/src/Functional/Element/WebformElementManagedFileTest.php \Drupal\Tests\webform\Functional\Element\WebformElementManagedFileTest::testFileUpload()

Test single and multiple file upload.

File

tests/src/Functional/Element/WebformElementManagedFileTest.php, line 64

Class

WebformElementManagedFileTest
Test for webform element managed file handling.

Namespace

Drupal\Tests\webform\Functional\Element

Code

public function testFileUpload() {

  /* Element rendering */
  $this
    ->drupalGet('/webform/test_element_managed_file');

  // Check single file upload button.
  $this
    ->assertRaw('<label for="edit-managed-file-single-button-upload-button--2" class="button button-action webform-file-button">Choose file</label>');

  // Check multiple file upload button.
  $this
    ->assertRaw('<label for="edit-managed-file-multiple-button-upload-button--2" class="button button-action webform-file-button">Choose files</label>');

  // Check single custom file upload button.
  $this
    ->assertRaw('<label style="color: red" for="edit-managed-file-single-button-custom-upload-button--2" class="button button-action webform-file-button">{Custom label}</label>');

  // Check comma delimited file extensions.
  $this
    ->assertRaw('Allowed types: txt, text.');

  /* Element processing */
  $this
    ->checkFileUpload('single', $this->files[0], $this->files[1]);
  $this
    ->checkFileUpload('multiple', $this->files[2], $this->files[3]);

  /* Multiple processing */

  // Check file input is visible.
  $this
    ->drupalGet('/webform/test_element_managed_file');
  $this
    ->assertFieldByName('files[managed_file_multiple_two][]');
  $this
    ->assertFieldByName('managed_file_multiple_two_upload_button');

  // Check that only two files can be uploaded.
  // @todo Determine how to submit multiple files.

  /*
  $edit = [
    'files[managed_file_multiple_two][]' => [
      \Drupal::service('file_system')->realpath($this->files[0]->uri),
      \Drupal::service('file_system')->realpath($this->files[1]->uri),
      \Drupal::service('file_system')->realpath($this->files[2]->uri),
    ],
  ];
  $this->drupalPostForm('/webform/test_element_managed_file', $edit, 'Upload');
  $this->assertRaw('<em class="placeholder">managed_file_multiple_two</em> can only hold 2 values but there were 3 uploaded. The following files have been omitted as a result: <em class="placeholder">text-2.txt</em>.');

  // Check file input is removed.
  $this->assertNoFieldByName('files[managed_file_multiple_two][]');
  $this->assertNoFieldByName('managed_file_multiple_two_upload_button');
  */

  /* File placeholder */

  // Check placeholder is displayed.
  $this
    ->drupalGet('/webform/test_element_managed_file');
  $this
    ->assertRaw('<div class="webform-managed-file-placeholder managed-file-placeholder js-form-wrapper form-wrapper" data-drupal-selector="edit-managed-file-single-placeholder-file-placeholder" id="edit-managed-file-single-placeholder-file-placeholder">This is the single file upload placeholder</div>');
  $this
    ->assertRaw('<div class="webform-managed-file-placeholder managed-file-placeholder js-form-wrapper form-wrapper" data-drupal-selector="edit-managed-file-multiple-placeholder-file-placeholder" id="edit-managed-file-multiple-placeholder-file-placeholder">This is the multiple file upload placeholder</div>');
  $this
    ->drupalLogin($this->rootUser);

  // Check placeholder is not displayed when files are uploaded.
  $this
    ->drupalGet('/webform/test_element_managed_file/test');
  $this
    ->assertNoRaw('<div class="webform-managed-file-placeholder managed-file-placeholder js-form-wrapper form-wrapper" data-drupal-selector="edit-managed-file-single-placeholder-file-placeholder" id="edit-managed-file-single-placeholder-file-placeholder">This is the single file upload placeholder</div>');
  $this
    ->assertNoRaw('<div class="webform-managed-file-placeholder managed-file-placeholder js-form-wrapper form-wrapper" data-drupal-selector="edit-managed-file-multiple-placeholder-file-placeholder" id="edit-managed-file-multiple-placeholder-file-placeholder">This is the multiple file upload placeholder</div>');
  $this
    ->drupalLogout();

  /* Required error */

  // Set required error.

  /** @var \Drupal\webform\WebformInterface $webform */
  $webform = Webform::load('test_element_managed_file');
  $webform
    ->setElementProperties('managed_file_single', $webform
    ->getElementDecoded('managed_file_single') + [
    '#required' => TRUE,
    '#required_error' => '{Custom required error}',
  ]);
  $webform
    ->save();

  // Check that required error is displayed.
  $this
    ->postSubmission($webform);
  $this
    ->assertRaw('<h2 class="visually-hidden">Error message</h2>');
  $this
    ->assertRaw('{Custom required error}');
}