You are here

public function FileFieldWidgetTest::testSingleValuedWidget in Drupal 10

Same name in this branch
  1. 10 core/modules/file/tests/src/Functional/FileFieldWidgetTest.php \Drupal\Tests\file\Functional\FileFieldWidgetTest::testSingleValuedWidget()
  2. 10 core/modules/file/tests/src/FunctionalJavascript/FileFieldWidgetTest.php \Drupal\Tests\file\FunctionalJavascript\FileFieldWidgetTest::testSingleValuedWidget()
Same name and namespace in other branches
  1. 8 core/modules/file/tests/src/FunctionalJavascript/FileFieldWidgetTest.php \Drupal\Tests\file\FunctionalJavascript\FileFieldWidgetTest::testSingleValuedWidget()
  2. 9 core/modules/file/tests/src/FunctionalJavascript/FileFieldWidgetTest.php \Drupal\Tests\file\FunctionalJavascript\FileFieldWidgetTest::testSingleValuedWidget()

Tests uploading and remove buttons for a single-valued File field.

File

core/modules/file/tests/src/FunctionalJavascript/FileFieldWidgetTest.php, line 139

Class

FileFieldWidgetTest
Tests the file field widget, single and multi-valued, using AJAX upload.

Namespace

Drupal\Tests\file\FunctionalJavascript

Code

public function testSingleValuedWidget() {
  $type_name = 'article';
  $field_name = 'test_file_field_1';
  $cardinality = 1;
  $this
    ->createFileField($field_name, 'node', $type_name, [
    'cardinality' => $cardinality,
  ]);
  $page = $this
    ->getSession()
    ->getPage();
  $assert_session = $this
    ->assertSession();
  $test_file = current($this
    ->getTestFiles('text'));
  $test_file_path = \Drupal::service('file_system')
    ->realpath($test_file->uri);
  $this
    ->drupalGet("node/add/{$type_name}");
  $page
    ->findField('title[0][value]')
    ->setValue($this
    ->randomString());
  $page
    ->attachFileToField('files[' . $field_name . '_0]', $test_file_path);
  $remove_button = $assert_session
    ->waitForElementVisible('css', '[name="' . $field_name . '_0_remove_button"]');
  $this
    ->assertNotNull($remove_button);
  $remove_button
    ->click();
  $upload_field = $assert_session
    ->waitForElementVisible('css', 'input[type="file"]');
  $this
    ->assertNotEmpty($upload_field);
  $page
    ->attachFileToField('files[' . $field_name . '_0]', $test_file_path);
  $remove_button = $assert_session
    ->waitForElementVisible('css', '[name="' . $field_name . '_0_remove_button"]');
  $this
    ->assertNotNull($remove_button);
  $page
    ->pressButton('Save');
  $page
    ->hasContent($test_file->name);

  // Create a new node and try to upload a file with an invalid extension.
  $test_image = current($this
    ->getTestFiles('image'));
  $test_image_path = \Drupal::service('file_system')
    ->realpath($test_image->uri);
  $this
    ->drupalGet("node/add/{$type_name}");
  $page
    ->findField('title[0][value]')
    ->setValue($this
    ->randomString());
  $page
    ->attachFileToField('files[' . $field_name . '_0]', $test_image_path);
  $messages = $assert_session
    ->waitForElementVisible('css', '.file-upload-js-error');
  $this
    ->assertEquals('The selected file image-test.png cannot be uploaded. Only files with the following extensions are allowed: txt.', $messages
    ->getText());

  // Make sure the error disappears when a valid file is uploaded.
  $page
    ->attachFileToField('files[' . $field_name . '_0]', $test_file_path);
  $remove_button = $assert_session
    ->waitForElementVisible('css', '[name="' . $field_name . '_0_remove_button"]');
  $this
    ->assertNotEmpty($remove_button);
  $this
    ->assertEmpty($this
    ->cssSelect('.file-upload-js-error'));
}