You are here

class ImageFieldWidgetMultipleTest in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/image/tests/src/FunctionalJavascript/ImageFieldWidgetMultipleTest.php \Drupal\Tests\image\FunctionalJavascript\ImageFieldWidgetMultipleTest
  2. 9 core/modules/image/tests/src/FunctionalJavascript/ImageFieldWidgetMultipleTest.php \Drupal\Tests\image\FunctionalJavascript\ImageFieldWidgetMultipleTest

Tests the image field widget support multiple upload correctly.

@group image

Hierarchy

Expanded class hierarchy of ImageFieldWidgetMultipleTest

File

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

Namespace

Drupal\Tests\image\FunctionalJavascript
View source
class ImageFieldWidgetMultipleTest extends WebDriverTestBase {
  use ImageFieldCreationTrait;
  use TestFileCreationTrait;

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'node',
    'field_ui',
    'image',
  ];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * Tests image widget element support multiple upload correctly.
   */
  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}");
    }
  }

}

Members