function ImageItemTest::testImageItemMalformed

Same name in other branches
  1. 9 core/modules/image/tests/src/Kernel/ImageItemTest.php \Drupal\Tests\image\Kernel\ImageItemTest::testImageItemMalformed()
  2. 8.9.x core/modules/image/tests/src/Kernel/ImageItemTest.php \Drupal\Tests\image\Kernel\ImageItemTest::testImageItemMalformed()
  3. 11.x core/modules/image/tests/src/Kernel/ImageItemTest.php \Drupal\Tests\image\Kernel\ImageItemTest::testImageItemMalformed()

Tests a malformed image.

File

core/modules/image/tests/src/Kernel/ImageItemTest.php, line 178

Class

ImageItemTest
Tests using entity fields of the image field type.

Namespace

Drupal\Tests\image\Kernel

Code

public function testImageItemMalformed() : void {
    \Drupal::service('module_installer')->install([
        'dblog',
    ]);
    // Validate entity is an image and don't gather dimensions if it is not.
    $entity = EntityTest::create();
    $entity->image_test = NULL;
    $entity->image_test->target_id = 9999;
    $entity->save();
    // Check that the proper warning has been logged.
    $arguments = [
        '%id' => 9999,
    ];
    $logged = Database::getConnection()->select('watchdog')
        ->fields('watchdog', [
        'variables',
    ])
        ->condition('type', 'image')
        ->condition('message', "Missing file with ID %id.")
        ->execute()
        ->fetchField();
    $this->assertEquals(serialize($arguments), $logged);
    $this->assertEmpty($entity->image_test->width);
    $this->assertEmpty($entity->image_test->height);
}

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