You are here

public function ImageFieldWidgetMultipleTest::testWidgetElementMultipleUploads in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/image/tests/src/FunctionalJavascript/ImageFieldWidgetMultipleTest.php \Drupal\Tests\image\FunctionalJavascript\ImageFieldWidgetMultipleTest::testWidgetElementMultipleUploads()

Tests image widget element support multiple upload correctly.

File

core/modules/image/tests/src/FunctionalJavascript/ImageFieldWidgetMultipleTest.php, line 34

Class

ImageFieldWidgetMultipleTest
Tests the image field widget support multiple upload correctly.

Namespace

Drupal\Tests\image\FunctionalJavascript

Code

public function testWidgetElementMultipleUploads() : void {
  $image_factory = \Drupal::service('image.factory');
  $file_system = \Drupal::service('file_system');
  $web_driver = $this
    ->getSession()
    ->getDriver();
  $this
    ->drupalCreateContentType([
    'type' => 'article',
    'name' => 'Article',
  ]);
  $field_name = 'images';
  $storage_settings = [
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
  ];
  $field_settings = [
    'alt_field_required' => 0,
  ];
  $this
    ->createImageField($field_name, 'article', $storage_settings, $field_settings);
  $this
    ->drupalLogin($this
    ->drupalCreateUser([
    'access content',
    'create article content',
  ]));
  $this
    ->drupalGet('node/add/article');
  $this
    ->assertSession()
    ->fieldExists('title[0][value]')
    ->setValue('Test');
  $images = $this
    ->getTestFiles('image');
  $images = array_slice($images, 0, 5);
  $paths = [];
  foreach ($images as $image) {
    $paths[] = $file_system
      ->realpath($image->uri);
  }
  $remote_paths = [];
  foreach ($paths as $path) {
    $remote_paths[] = $web_driver
      ->uploadFileAndGetRemoteFilePath($path);
  }
  $multiple_field = $this
    ->assertSession()
    ->elementExists('xpath', '//input[@multiple]');
  $multiple_field
    ->setValue(implode("\n", $remote_paths));
  $this
    ->assertSession()
    ->waitForElementVisible('css', '[data-drupal-selector="edit-images-4-preview"]');
  $this
    ->getSession()
    ->getPage()
    ->findButton('Save')
    ->click();
  $node = Node::load(1);
  foreach ($paths as $delta => $path) {
    $node_image = $node->{$field_name}[$delta];
    $original_image = $image_factory
      ->get($path);
    $this
      ->assertEquals($original_image
      ->getWidth(), $node_image->width, "Correct width of image #{$delta}");
    $this
      ->assertEquals($original_image
      ->getHeight(), $node_image->height, "Correct height of image #{$delta}");
  }
}