summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathaniel Catchpole2017-09-18 11:25:20 (GMT)
committerNathaniel Catchpole2017-09-18 11:25:20 (GMT)
commit6fc52f12a7948f1ae239954a420313ef1e14b968 (patch)
tree9847007437974ffd814816c96578b68f7f7fb80c
parent76fd2bdbdebe2f709d81c24eda1d82a1d1f5d4b4 (diff)
Issue #2705925 by heddn, Adita, jibran, Berdir, joelpittet, jazzdrive3: ImageItem presave() fails when pointing to a non-existing file entity
-rw-r--r--core/modules/file/tests/src/Kernel/Migrate/d7/FileMigrationSetupTrait.php38
-rw-r--r--core/modules/file/tests/src/Kernel/Migrate/d7/MigrateFileTest.php21
-rw-r--r--core/modules/image/src/Plugin/Field/FieldType/ImageItem.php16
-rw-r--r--core/modules/image/tests/src/Kernel/ImageItemTest.php23
-rw-r--r--core/modules/node/tests/src/Kernel/Migrate/d7/MigrateNodeTest.php7
5 files changed, 81 insertions, 24 deletions
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 0000000..a59176b
--- /dev/null
+++ b/core/modules/file/tests/src/Kernel/Migrate/d7/FileMigrationSetupTrait.php
@@ -0,0 +1,38 @@
+<?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);
+ }
+
+}
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 240c9e0..88d7c79 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 @@ use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
*/
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 7b8b275..9e58ccd 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 @@ class ImageItem extends FileItem {
$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 f7a1305..951d190 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 @@ class ImageItemTest extends FieldKernelTestBase {
$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 9ca1d4a..b4fd246 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 @@ use Drupal\node\NodeInterface;
*/
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']);