'label' => 'Images',
'settings' => [
'file_extensions' => 'jpg',
+ 'file_directory' => 'entity-browser-test',
+ 'max_resolution' => '40x40',
'title_field' => TRUE,
],
])->save();
- file_unmanaged_copy(\Drupal::root() . '/core/misc/druplicon.png', 'public://example.jpg');
+ file_unmanaged_copy(\Drupal::root() . '/core/modules/simpletest/files/image-test.jpg', 'public://example.jpg');
$this->image = File::create([
'uri' => 'public://example.jpg',
]);
->load('test_entity_browser_iframe_view');
$browser->setDisplay('iframe');
$browser->getDisplay()->setConfiguration($display_config);
+ $browser->addWidget([
+ // These settings should get overridden by our field settings.
+ 'settings' => [
+ 'upload_location' => 'public://',
+ 'extensions' => 'png',
+ ],
+ 'weight' => 1,
+ 'label' => 'Upload images',
+ 'id' => 'upload',
+ ]);
+ $browser->setWidgetSelector('tabs');
$browser->save();
$account = $this->drupalCreateUser([
* Tests basic usage for an image field.
*/
public function testImageFieldUsage() {
-
$this->drupalGet('node/add/article');
$this->assertSession()->linkExists('Select images');
$this->getSession()->getPage()->clickLink('Select images');
$this->assertSession()->linkExists('Select entities');
}
+ /**
+ * Tests that settings are passed from the image field to the upload widget.
+ */
+ public function testImageFieldSettings() {
+ $root = \Drupal::root();
+ $file_wrong_type = $root . '/core/misc/druplicon.png';
+ $file_too_big = $root . '/core/modules/simpletest/files/image-2.jpg';
+ $file_just_right = $root . '/core/modules/simpletest/files/image-test.jpg';
+ $this->drupalGet('node/add/article');
+ $this->assertSession()->linkExists('Select images');
+ $this->getSession()->getPage()->clickLink('Select images');
+ $this->getSession()->switchToIFrame('entity_browser_iframe_test_entity_browser_iframe_view');
+ // Switch to the image tab.
+ $this->clickLink('Upload images');
+ // Attempt to upload an invalid image type. The upload widget is configured
+ // to allow png but the field widget is configured to allow jpg, so we
+ // expect the field to override the widget.
+ $this->getSession()->getPage()->attachFileToField('files[upload][]', $file_wrong_type);
+ $this->waitForAjaxToFinish();
+ $this->assertSession()->pageTextContains('Only files with the following extensions are allowed: jpg');
+ $this->assertSession()->pageTextContains('The specified file druplicon.png could not be uploaded');
+ // Upload an image bigger than the field widget's configured max size.
+ $this->getSession()->getPage()->attachFileToField('files[upload][]', $file_too_big);
+ $this->waitForAjaxToFinish();
+ $this->assertSession()->pageTextContains('The image was resized to fit within the maximum allowed dimensions of 40x40 pixels.');
+ // Upload an image that passes validation and finish the upload.
+ $this->getSession()->getPage()->attachFileToField('files[upload][]', $file_just_right);
+ $this->waitForAjaxToFinish();
+ $this->getSession()->getPage()->pressButton('Select files');
+ $this->getSession()->getPage()->pressButton('Use selected');
+ $this->assertSession()->pageTextContains('image-test.jpg');
+ // Check that the file has uploaded to the correct sub-directory.
+ $this->getSession()->switchToIFrame();
+ $this->waitForAjaxToFinish();
+ $entity_id = $this->getSession()->evaluateScript('jQuery("#edit-field-image-wrapper [data-entity-id]").data("entity-id")');
+ $this->assertStringStartsWith('file:', $entity_id);
+ /** @var \Drupal\file\Entity\File $file */
+ $fid = explode(':', $entity_id)[1];
+ $file = File::load($fid);
+ $this->assertContains('entity-browser-test', $file->getFileUri());
+ }
+
}