You are here

public function ImageFieldTest::testImageFieldSettings in Entity Browser 8

Same name and namespace in other branches
  1. 8.2 tests/src/FunctionalJavascript/ImageFieldTest.php \Drupal\Tests\entity_browser\FunctionalJavascript\ImageFieldTest::testImageFieldSettings()

Tests that settings are passed from the image field to the upload widget.

File

tests/src/FunctionalJavascript/ImageFieldTest.php, line 199

Class

ImageFieldTest
Tests the image field widget.

Namespace

Drupal\Tests\entity_browser\FunctionalJavascript

Code

public function testImageFieldSettings() {
  $root = \Drupal::root();
  $file_wrong_type = $root . '/core/misc/druplicon.png';
  $test_files = $this
    ->getTestFiles('image');
  $file_system = $this->container
    ->get('file_system');
  foreach ($test_files as $test_file) {
    if ($test_file->filename === 'image-test.jpg') {
      $file_just_right = $file_system
        ->realpath($test_file->uri);
    }
    elseif ($test_file->filename === 'image-2.jpg') {
      $file_too_big = $file_system
        ->realpath($test_file->uri);
    }
  }
  $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();
  if (version_compare(\Drupal::VERSION, '8.7', '>=')) {
    $this
      ->assertSession()
      ->responseContains('Only files with the following extensions are allowed: <em class="placeholder">jpg</em>.');
    $this
      ->assertSession()
      ->responseContains('The selected file <em class="placeholder">druplicon.png</em> cannot be uploaded.');
  }
  else {
    $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
    ->waitForAjaxToFinish();
  $button = $this
    ->assertSession()
    ->waitForButton('Use selected');
  $this
    ->assertSession()
    ->pageTextContains('image-test.jpg');
  $button
    ->press();
  $this
    ->waitForAjaxToFinish();

  // 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());
}