function QuickEditImageControllerTest::testInvalidUpload

Same name in other branches
  1. 8.9.x core/modules/image/tests/src/Functional/QuickEditImageControllerTest.php \Drupal\Tests\image\Functional\QuickEditImageControllerTest::testInvalidUpload()

Tests that uploading an invalid image does not work.

File

core/modules/quickedit/tests/src/Functional/QuickEditImageControllerTest.php, line 145

Class

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

Namespace

Drupal\Tests\quickedit\Functional

Code

public function testInvalidUpload() {
    // Create a test Node.
    $node = $this->drupalCreateNode([
        'type' => 'article',
        'title' => '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.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.