diff --git a/core/modules/file/tests/src/Kernel/Migrate/d7/FileMigrationSetupTrait.php b/core/modules/file/tests/src/Kernel/Migrate/d7/FileMigrationSetupTrait.php new file mode 100644 index 0000000000000000000000000000000000000000..a59176b784ba9f85dfd393ac602355563ab976a8 --- /dev/null +++ b/core/modules/file/tests/src/Kernel/Migrate/d7/FileMigrationSetupTrait.php @@ -0,0 +1,38 @@ +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); + } + +} diff --git a/core/modules/file/tests/src/Kernel/Migrate/d7/MigrateFileTest.php b/core/modules/file/tests/src/Kernel/Migrate/d7/MigrateFileTest.php index 240c9e0a9647236eaf5bf69e04aa392721cc7502..88d7c79e554288505d38e3ceb35469c452fbb56a 100644 --- a/core/modules/file/tests/src/Kernel/Migrate/d7/MigrateFileTest.php +++ b/core/modules/file/tests/src/Kernel/Migrate/d7/MigrateFileTest.php @@ -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(); } /** diff --git a/core/modules/image/src/Plugin/Field/FieldType/ImageItem.php b/core/modules/image/src/Plugin/Field/FieldType/ImageItem.php index 7b8b27521650e6f2db3231d1a8ec7a74ce495d3e..9e58ccd70dbcb46d0b07899661d4629efa3aa0ee 100644 --- a/core/modules/image/src/Plugin/Field/FieldType/ImageItem.php +++ b/core/modules/image/src/Plugin/Field/FieldType/ImageItem.php @@ -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); + } } /** diff --git a/core/modules/image/tests/src/Kernel/ImageItemTest.php b/core/modules/image/tests/src/Kernel/ImageItemTest.php index f7a130545085d8365fe3bf30643e2c195f1d4def..951d1907f88f1b9ed4e3c0963b86d3d73e1caf08 100644 --- a/core/modules/image/tests/src/Kernel/ImageItemTest.php +++ b/core/modules/image/tests/src/Kernel/ImageItemTest.php @@ -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); + } + + } + } diff --git a/core/modules/node/tests/src/Kernel/Migrate/d7/MigrateNodeTest.php b/core/modules/node/tests/src/Kernel/Migrate/d7/MigrateNodeTest.php index 9ca1d4a935fcfa8a98e20843ceadae13835497a9..b4fd24606a75a03ccf4e8ccae05476de38586ac5 100644 --- a/core/modules/node/tests/src/Kernel/Migrate/d7/MigrateNodeTest.php +++ b/core/modules/node/tests/src/Kernel/Migrate/d7/MigrateNodeTest.php @@ -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']);