Verified Commit 0298c959 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3440038 by longwave, quietone, mondrake, catch: ImageItem logs an...

Issue #3440038 by longwave, quietone, mondrake, catch: ImageItem logs an warning instead of triggering an E_USER_WARNING
parent 10466dba
Loading
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -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;
@@ -52,6 +53,8 @@
 */
class ImageItem extends FileItem {

  use LoggerChannelTrait;

  /**
   * {@inheritdoc}
   */
@@ -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]);
    }
  }

+17 −14
Original line number Diff line number Diff line
@@ -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;
@@ -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.
@@ -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);
  }

}

}