Commit 6fc52f12 authored by catch's avatar catch

Issue #2705925 by heddn, Adita, jibran, Berdir, joelpittet, jazzdrive3:...

Issue #2705925 by heddn, Adita, jibran, Berdir, joelpittet, jazzdrive3: ImageItem presave() fails when pointing to a non-existing file entity
parent 76fd2bdb
<?php
namespace Drupal\Tests\file\Kernel\Migrate\d7;
use Drupal\Core\StreamWrapper\PublicStream;
use Drupal\Core\StreamWrapper\StreamWrapperInterface;
/**
* A trait to setup the file migration.
*/
trait FileMigrationSetupTrait {
/**
* Prepare the file migration for running.
*/
protected function fileMigrationSetup() {
$this->installSchema('file', ['file_usage']);
$this->installEntitySchema('file');
$this->container->get('stream_wrapper_manager')->registerWrapper('public', PublicStream::class, StreamWrapperInterface::NORMAL);
$fs = \Drupal::service('file_system');
// The public file directory active during the test will serve as the
// root of the fictional Drupal 7 site we're migrating.
$fs->mkdir('public://sites/default/files', NULL, TRUE);
file_put_contents('public://sites/default/files/cube.jpeg', str_repeat('*', 3620));
/** @var \Drupal\migrate\Plugin\Migration $migration */
$migration = $this->getMigration('d7_file');
// Set the source plugin's source_base_path configuration value, which
// would normally be set by the user running the migration.
$source = $migration->getSourceConfiguration();
$source['constants']['source_base_path'] = $fs->realpath('public://');
$migration->set('source', $source);
$this->executeMigration($migration);
}
}
......@@ -2,7 +2,6 @@
namespace Drupal\Tests\file\Kernel\Migrate\d7;
use Drupal\Core\StreamWrapper\StreamWrapperInterface;
use Drupal\file\Entity\File;
use Drupal\file\FileInterface;
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
......@@ -14,6 +13,8 @@
*/
class MigrateFileTest extends MigrateDrupal7TestBase {
use FileMigrationSetupTrait;
public static $modules = ['file'];
/**
......@@ -22,23 +23,7 @@ class MigrateFileTest extends MigrateDrupal7TestBase {
protected function setUp() {
parent::setUp();
$this->installEntitySchema('file');
$this->container->get('stream_wrapper_manager')->registerWrapper('public', 'Drupal\Core\StreamWrapper\PublicStream', StreamWrapperInterface::NORMAL);
$fs = \Drupal::service('file_system');
// The public file directory active during the test will serve as the
// root of the fictional Drupal 7 site we're migrating.
$fs->mkdir('public://sites/default/files', NULL, TRUE);
file_put_contents('public://sites/default/files/cube.jpeg', str_repeat('*', 3620));
/** @var \Drupal\migrate\Plugin\Migration $migration */
$migration = $this->getMigration('d7_file');
// Set the source plugin's source_base_path configuration value, which
// would normally be set by the user running the migration.
$source = $migration->getSourceConfiguration();
$source['constants']['source_base_path'] = $fs->realpath('public://');
$migration->set('source', $source);
$this->executeMigration($migration);
$this->fileMigrationSetup();
}
/**
......
......@@ -3,6 +3,7 @@
namespace Drupal\image\Plugin\Field\FieldType;
use Drupal\Component\Utility\Random;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
......@@ -313,13 +314,18 @@ public function preSave() {
$height = $this->height;
// Determine the dimensions if necessary.
if (empty($width) || empty($height)) {
$image = \Drupal::service('image.factory')->get($this->entity->getFileUri());
if ($image->isValid()) {
$this->width = $image->getWidth();
$this->height = $image->getHeight();
if ($this->entity && $this->entity instanceof EntityInterface) {
if (empty($width) || empty($height)) {
$image = \Drupal::service('image.factory')->get($this->entity->getFileUri());
if ($image->isValid()) {
$this->width = $image->getWidth();
$this->height = $image->getHeight();
}
}
}
else {
trigger_error(sprintf("Missing file with ID %s.", $this->target_id), E_USER_WARNING);
}
}
/**
......
......@@ -2,6 +2,7 @@
namespace Drupal\Tests\image\Kernel;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
......@@ -129,4 +130,26 @@ public function testImageItem() {
$this->assertEqual($entity->image_test->entity->get('filemime')->value, 'image/jpeg');
}
/**
* Tests a malformed image.
*/
public function testImageItemMalformed() {
// 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(\PHPUnit_Framework_Error_Warning::class, $exception->getPrevious());
$this->assertEquals($exception->getMessage(), 'Missing file with ID 9999.');
$this->assertEmpty($entity->image_test->width);
$this->assertEmpty($entity->image_test->height);
}
}
}
......@@ -2,6 +2,7 @@
namespace Drupal\Tests\node\Kernel\Migrate\d7;
use Drupal\Tests\file\Kernel\Migrate\d7\FileMigrationSetupTrait;
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
......@@ -13,6 +14,8 @@
*/
class MigrateNodeTest extends MigrateDrupal7TestBase {
use FileMigrationSetupTrait;
/**
* {@inheritdoc}
*/
......@@ -20,6 +23,7 @@ class MigrateNodeTest extends MigrateDrupal7TestBase {
'content_translation',
'comment',
'datetime',
'file',
'filter',
'image',
'language',
......@@ -37,10 +41,11 @@ class MigrateNodeTest extends MigrateDrupal7TestBase {
protected function setUp() {
parent::setUp();
$this->fileMigrationSetup();
$this->installEntitySchema('node');
$this->installEntitySchema('comment');
$this->installEntitySchema('taxonomy_term');
$this->installEntitySchema('file');
$this->installConfig(static::$modules);
$this->installSchema('node', ['node_access']);
$this->installSchema('system', ['sequences']);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment