Issue #3440038 by longwave, quietone, mondrake, catch: ImageItem logs an warning instead of triggering an E_USER_WARNING

merge-requests/7531/head
Alex Pott 2024-04-15 22:32:31 +01:00
parent 10466dba9d
commit 0298c95971
No known key found for this signature in database
GPG Key ID: BDA67E7EE836E5CE
2 changed files with 21 additions and 15 deletions

View File

@ -9,6 +9,7 @@ use Drupal\Core\Field\FieldStorageDefinitionInterface;
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 @@ use Drupal\file\Plugin\Field\FieldType\FileItem;
*/
class ImageItem extends FileItem {
use LoggerChannelTrait;
/**
* {@inheritdoc}
*/
@ -332,7 +335,7 @@ class ImageItem extends FileItem {
}
}
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]);
}
}

View File

@ -4,7 +4,7 @@ declare(strict_types=1);
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\Tests\field\Kernel\FieldKernelTestBase;
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 @@ class ImageItemTest extends FieldKernelTestBase {
* 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());
$this->assertEmpty($entity->image_test->width);
$this->assertEmpty($entity->image_test->height);
}
$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);
}
}