You are here

public function QuickEditImageControllerTest::testInvalidUpload in Drupal 8

Tests that uploading a invalid image does not work.

File

core/modules/image/tests/src/Functional/QuickEditImageControllerTest.php, line 144

Class

QuickEditImageControllerTest
Tests the endpoints used by the "image" in-place editor.

Namespace

Drupal\Tests\image\Functional

Code

public function testInvalidUpload() {

  // Create a test Node.
  $node = $this
    ->drupalCreateNode([
    'type' => 'article',
    'title' => t('Test Node'),
  ]);

  // We want a test image that will fail validation.
  $invalid_image = FALSE;

  /** @var \Drupal\Core\Image\ImageFactory $image_factory */
  $image_factory = $this->container
    ->get('image.factory');
  foreach ($this
    ->drupalGetTestFiles('image') as $image) {

    /** @var \Drupal\Core\Image\ImageInterface $image_file */
    $image_file = $image_factory
      ->get($image->uri);
    if ($image_file
      ->getWidth() < 50 || $image_file
      ->getWidth() > 100) {
      $invalid_image = $image;
      break;
    }
  }
  $this
    ->assertNotFalse($invalid_image);
  $this
    ->drupalLogin($this->contentAuthorUser);
  $this
    ->uploadImage($invalid_image, $node
    ->id(), $this->fieldName, $node
    ->language()
    ->getId());
  $this
    ->assertStringContainsString('"main_error":"The image failed validation."', $this
    ->getSession()
    ->getPage()
    ->getContent(), 'Invalid upload returned errors.');
}