diff --git a/core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php b/core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php index e86fe94..fb66a15 100644 --- a/core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php +++ b/core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php @@ -174,11 +174,10 @@ public static function process($element, FormStateInterface $form_state, $form) $variables['width'] = $element['#value']['width']; $variables['height'] = $element['#value']['height']; } - // In hte case of a multiple upload at once, avoid retrieving the image + // In the case of a multiple upload at once, avoid retrieving the image // dimensions as it can lead to an issue where all images end up having // the same dimensions. These dimensions are going to be calculated over // a second phase. - // See https://www.drupal.org/node/2644468. elseif (count($element['#files']) > 1) { $variables['width'] = $variables['height'] = NULL; } diff --git a/core/modules/image/src/Tests/ImageFieldWidgetTest.php b/core/modules/image/src/Tests/ImageFieldWidgetTest.php index e24d344..1076930 100644 --- a/core/modules/image/src/Tests/ImageFieldWidgetTest.php +++ b/core/modules/image/src/Tests/ImageFieldWidgetTest.php @@ -18,6 +18,26 @@ class ImageFieldWidgetTest extends ImageFieldTestBase { /** + * @var \Drupal\Core\File\FileSystem + */ + protected $fileSystem; + + /** + * @var \Drupal\Core\Image\ImageFactory + */ + protected $imageFactory; + + /** + * {@inheritdoc} + */ + protected function setUp() { + parent::setUp(); + + $this->fileSystem = $this->container->get('file_system'); + $this->imageFactory = $this->container->get('image.factory'); + } + + /** * Tests file widget element. */ public function testWidgetElement() { @@ -34,27 +54,29 @@ public function testWidgetElement() { $this->drupalGet('node/add/article'); $this->assertNotEqual(0, count($this->xpath('//div[contains(@class, "field--widget-image-image")]')), 'Image field widget found on add/node page', 'Browser'); - $test_image_1 = $this->drupalGetTestFiles('image')[5]; - $test_image_2 = $this->drupalGetTestFiles('image')[7]; - $edit = array( - 'files[image_0][]' => array(drupal_realpath($test_image_1->uri), drupal_realpath($test_image_2->uri)) - ); - $this->drupalPostAjaxForm(NULL, $edit, 'image_0_upload_button'); + // @todo Use the new drupalGetTestImageFile() when + // https://www.drupal.org/node/2377747 gets commited. + $test_images = [ + 0 => $this->drupalGetTestFiles('image')[5], + 1 => $this->drupalGetTestFiles('image')[7], + ]; + $edit = ['files[' . $field_name . '_0][]' => []]; + foreach ($test_images as $delta => $test_image) { + $edit['files[' . $field_name . '_0][]'][$delta] = $this->fileSystem->realpath($test_image->uri); + } + $this->drupalPostAjaxForm(NULL, $edit, $field_name . '_0_upload_button'); $this->drupalPostForm(NULL, array('title[0][value]' => 'Image upload'), 'Save and publish'); $node = Node::load(1); - foreach ($node->image as $delta => $image) { - if ($delta == 0) { - $this->assertEqual($image->width, 80, 'Stored width of first image is 80 px'); - $this->assertEqual($image->height, 60, 'Stored height of first image is 60px'); - } - else { - $original_image = \Drupal::service('image.factory')->get($test_image_2->uri); + foreach ($node->{$field_name} as $delta => $image) { + $test_image = $test_images[$delta]; + $original_image = $this->imageFactory->get($test_image->uri); + if ($original_image->getWidth() > $max_resolution || $original_image->getHeight() > $max_resolution) { $original_image->scale($max_resolution, $max_resolution); - $this->assertEqual($image->width, $original_image->getWidth(), 'Stored width of second image is ' . $original_image->getWidth() . 'px'); - $this->assertEqual($image->height, $original_image->getHeight(), 'Stored height of second image is ' . $original_image->getHeight() . 'px'); } + + $this->assertEqual($image->width, $original_image->getWidth(), 'Stored width of image #' . $delta . ' is ' . $original_image->getWidth() . 'px'); + $this->assertEqual($image->height, $original_image->getHeight(), 'Stored height of image #' . $delta . ' is ' . $original_image->getHeight() . 'px'); } } } -