Loading core/modules/image/src/Plugin/Field/FieldType/ImageItem.php +4 −1 Original line number Diff line number Diff line Loading @@ -9,6 +9,7 @@ use Drupal\Core\File\Exception\FileException; use Drupal\Core\File\FileSystemInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Logger\LoggerChannelTrait; use Drupal\Core\StreamWrapper\StreamWrapperInterface; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\Core\TypedData\DataDefinition; Loading Loading @@ -52,6 +53,8 @@ */ class ImageItem extends FileItem { use LoggerChannelTrait; /** * {@inheritdoc} */ Loading Loading @@ -332,7 +335,7 @@ public function preSave() { } } else { trigger_error(sprintf("Missing file with ID %s.", $this->target_id), E_USER_WARNING); $this->getLogger('image')->warning("Missing file with ID %id.", ['%id' => $this->target_id]); } } Loading core/modules/image/tests/src/Kernel/ImageItemTest.php +17 −14 Original line number Diff line number Diff line Loading @@ -4,7 +4,7 @@ namespace Drupal\Tests\image\Kernel; use Drupal\Core\Entity\EntityStorageException; use Drupal\Core\Database\Database; use Drupal\Core\Field\FieldItemInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface; Loading @@ -14,7 +14,6 @@ use Drupal\field\Entity\FieldStorageConfig; use Drupal\file\Entity\File; use Drupal\user\Entity\Role; use PHPUnit\Framework\Error\Warning; /** * Tests using entity fields of the image field type. Loading Loading @@ -179,22 +178,26 @@ public function testImageItemSampleValueGeneration() { * Tests a malformed image. */ public function testImageItemMalformed() { \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; // PHPUnit re-throws E_USER_WARNING as an exception. try { $entity->save(); $this->fail('Exception did not fail'); } catch (EntityStorageException $exception) { $this->assertInstanceOf(Warning::class, $exception->getPrevious()); $this->assertEquals('Missing file with ID 9999.', $exception->getMessage()); // 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); } } } Loading
core/modules/image/src/Plugin/Field/FieldType/ImageItem.php +4 −1 Original line number Diff line number Diff line Loading @@ -9,6 +9,7 @@ use Drupal\Core\File\Exception\FileException; use Drupal\Core\File\FileSystemInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Logger\LoggerChannelTrait; use Drupal\Core\StreamWrapper\StreamWrapperInterface; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\Core\TypedData\DataDefinition; Loading Loading @@ -52,6 +53,8 @@ */ class ImageItem extends FileItem { use LoggerChannelTrait; /** * {@inheritdoc} */ Loading Loading @@ -332,7 +335,7 @@ public function preSave() { } } else { trigger_error(sprintf("Missing file with ID %s.", $this->target_id), E_USER_WARNING); $this->getLogger('image')->warning("Missing file with ID %id.", ['%id' => $this->target_id]); } } Loading
core/modules/image/tests/src/Kernel/ImageItemTest.php +17 −14 Original line number Diff line number Diff line Loading @@ -4,7 +4,7 @@ namespace Drupal\Tests\image\Kernel; use Drupal\Core\Entity\EntityStorageException; use Drupal\Core\Database\Database; use Drupal\Core\Field\FieldItemInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface; Loading @@ -14,7 +14,6 @@ use Drupal\field\Entity\FieldStorageConfig; use Drupal\file\Entity\File; use Drupal\user\Entity\Role; use PHPUnit\Framework\Error\Warning; /** * Tests using entity fields of the image field type. Loading Loading @@ -179,22 +178,26 @@ public function testImageItemSampleValueGeneration() { * Tests a malformed image. */ public function testImageItemMalformed() { \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; // PHPUnit re-throws E_USER_WARNING as an exception. try { $entity->save(); $this->fail('Exception did not fail'); } catch (EntityStorageException $exception) { $this->assertInstanceOf(Warning::class, $exception->getPrevious()); $this->assertEquals('Missing file with ID 9999.', $exception->getMessage()); // 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); } } }