diff -u b/core/modules/aggregator/src/Tests/Migrate/MigrateAggregatorStubTest.php b/core/modules/aggregator/src/Tests/Migrate/MigrateAggregatorStubTest.php --- b/core/modules/aggregator/src/Tests/Migrate/MigrateAggregatorStubTest.php +++ b/core/modules/aggregator/src/Tests/Migrate/MigrateAggregatorStubTest.php @@ -7,6 +7,7 @@ namespace Drupal\aggregator\Tests\Migrate; +use Drupal\migrate\MigrateException; use Drupal\migrate_drupal\Tests\MigrateDrupalTestBase; /** @@ -41,6 +42,18 @@ * Tests creation of aggregator feed items. */ public function testItemStub() { + try { + // We expect an exception, because there's no feed to reference. + $this->performStubTest('aggregator_item'); + $this->fail(t('Expected exception has not been thrown.')); + } + catch (MigrateException $e) { + $this->assertIdentical($e->getMessage(), + 'Stubbing failed, unable to generate value for field fid'); + } + + // The stub should pass when there's a feed to point to. + $this->createStub('aggregator_feed'); $this->performStubTest('aggregator_item'); } diff -u b/core/modules/comment/src/Tests/Migrate/MigrateCommentStubTest.php b/core/modules/comment/src/Tests/Migrate/MigrateCommentStubTest.php --- b/core/modules/comment/src/Tests/Migrate/MigrateCommentStubTest.php +++ b/core/modules/comment/src/Tests/Migrate/MigrateCommentStubTest.php @@ -7,6 +7,7 @@ namespace Drupal\comment\Tests\Migrate; +use Drupal\migrate\MigrateException; use Drupal\migrate_drupal\Tests\MigrateDrupalTestBase; /** @@ -34,7 +35,18 @@ * Tests creation of comment stubs. */ public function testStub() { - // @todo: We need to create a node for the comment stub to attach to. + try { + // We expect an exception, because there's no node to reference. + $this->performStubTest('comment'); + $this->fail(t('Expected exception has not been thrown.')); + } + catch (MigrateException $e) { + $this->assertIdentical($e->getMessage(), + 'Stubbing failed, unable to generate value for field entity_id'); + } + + // The stub should pass when there's a node to point to. + $this->createStub('node'); $this->performStubTest('comment'); } diff -u b/core/modules/migrate/src/Plugin/migrate/destination/Entity.php b/core/modules/migrate/src/Plugin/migrate/destination/Entity.php --- b/core/modules/migrate/src/Plugin/migrate/destination/Entity.php +++ b/core/modules/migrate/src/Plugin/migrate/destination/Entity.php @@ -13,6 +13,8 @@ use Drupal\Core\Field\FieldItemInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\migrate\Entity\MigrationInterface; +use Drupal\migrate\MigrateException; +use Drupal\migrate\Plugin\MigrateIdMapInterface; use Drupal\migrate\Row; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -165,14 +167,16 @@ ->getPluginClass($field_definition->getType()); $value = $field_type_class::generateSampleValue($field_definition); if (is_null($value)) { - // Not all field types provide sample values. Do the best we can. + // Handle failure to generate a sample value. switch ($field_definition->getType()) { - case 'entity_reference': - // @todo Look for an existing entity of the appropriate type. - break; case 'list_integer': + // @todo Remove when https://www.drupal.org/node/2536374 is committed. $value = 1; break; + default: + throw new MigrateException($this->t('Stubbing failed, unable to generate value for field @name', + ['@name' => $field_name])); + break; } } } diff -u b/core/modules/migrate_drupal/src/Tests/MigrateDrupalTestBase.php b/core/modules/migrate_drupal/src/Tests/MigrateDrupalTestBase.php --- b/core/modules/migrate_drupal/src/Tests/MigrateDrupalTestBase.php +++ b/core/modules/migrate_drupal/src/Tests/MigrateDrupalTestBase.php @@ -84,6 +84,26 @@ * The entity type we are stubbing. */ protected function performStubTest($entity_type_id) { + $destination_id = $this->createStub($entity_type_id); + $controller = \Drupal::entityManager()->getStorage($entity_type_id); + /** @var \Drupal\Core\Entity\ContentEntityBase $stub_entity */ + $stub_entity = $controller->load($destination_id); + $this->assertTrue($stub_entity, 'Stub successfully created'); + if ($stub_entity) { + $this->assertTrue($stub_entity->validate(), 'Stub is a valid entity'); + } + } + + /** + * Create a stub of the given entity type. + * + * @param string $entity_type_id + * The entity type we are stubbing. + * + * @return int + * ID of the created entity. + */ + protected function createStub($entity_type_id) { // Create a dummy migration to pass to the destination plugin. $config = [ 'id' => 'dummy', @@ -96,13 +116,7 @@ $destination_plugin = $migration->getDestinationPlugin(TRUE); $stub_row = new Row([], [], TRUE); $destination_ids = $destination_plugin->import($stub_row); - $controller = \Drupal::entityManager()->getStorage($entity_type_id); - /** @var \Drupal\Core\Entity\ContentEntityBase $stub_entity */ - $stub_entity = $controller->load(reset($destination_ids)); - $this->assertTrue($stub_entity, 'Stub successfully created'); - if ($stub_entity) { - $this->assertTrue($stub_entity->validate(), 'Stub is a valid entity'); - } + return reset($destination_ids); } } diff -u b/core/modules/shortcut/src/Tests/Migrate/MigrateShortcutStubTest.php b/core/modules/shortcut/src/Tests/Migrate/MigrateShortcutStubTest.php --- b/core/modules/shortcut/src/Tests/Migrate/MigrateShortcutStubTest.php +++ b/core/modules/shortcut/src/Tests/Migrate/MigrateShortcutStubTest.php @@ -27,10 +27,12 @@ protected function setUp() { parent::setUp(); $this->installEntitySchema('shortcut'); + // Make sure the 'default' shortcut_set is installed. + $this->installConfig(['shortcut']); } /** - * Tests creation of menu link content stubs. + * Tests creation of shortcut stubs. */ public function testStub() { $this->performStubTest('shortcut'); only in patch2: unchanged: --- /dev/null +++ b/core/modules/aggregator/src/Plugin/migrate/destination/AggregatorItem.php @@ -0,0 +1,42 @@ +getDestinationProperty('fid')) { + $field_definitions = \Drupal::entityManager() + ->getFieldDefinitions($this->storage->getEntityTypeId(), + $this->getKey('bundle')); + $value = EntityReferenceItem::generateSampleValue($field_definitions['fid']); + if (is_null($value)) { + throw new MigrateException($this->t('Stubbing failed, unable to generate value for field @name', + ['@name' => 'fid'])); + } + $row->setDestinationProperty('fid', $value); + } + } + +} only in patch2: unchanged: --- a/core/modules/comment/src/Plugin/migrate/destination/EntityComment.php +++ b/core/modules/comment/src/Plugin/migrate/destination/EntityComment.php @@ -110,32 +110,9 @@ public function import(Row $row, array $old_destination_id_values = array()) { */ protected function processStubRow(Row $row) { parent::processStubRow($row); - $stub_commented_entity_type = $row->getDestinationProperty('entity_type'); - - // While parent::getEntity() fills the bundle property for stub entities - // if it's still empty, here we must also make sure entity_id/entity_type - // are filled (so $comment->getCommentedEntity() always returns a value). - if (empty($this->stubCommentedEntityIds[$stub_commented_entity_type])) { - // Fill stub entity id. Any id will do, as long as it exists. - $entity_type = $this->entityManager->getDefinition($stub_commented_entity_type); - $id_key = $entity_type->getKey('id'); - $result = $this->entityQuery - ->get($stub_commented_entity_type) - ->range(0, 1) - ->execute(); - if ($result) { - $this->stubCommentedEntityIds[$stub_commented_entity_type] = array_pop($result); - $row->setSourceProperty($id_key, $this->stubCommentedEntityIds[$stub_commented_entity_type]); - } - else { - throw new MigrateException(t('Could not find parent entity to use for comment %id', ['%id' => implode(':', $row->getSourceIdValues())]), MigrationInterface::MESSAGE_ERROR); - } - } - - $row->setDestinationProperty('entity_id', $this->stubCommentedEntityIds[$stub_commented_entity_type]); - $row->setDestinationProperty('entity_type', $stub_commented_entity_type); - $row->setDestinationProperty('created', REQUEST_TIME); - $row->setDestinationProperty('changed', REQUEST_TIME); + // Neither uid nor name is required in itself, but it is required to set one + // of them. + $row->setDestinationProperty('name', 'anonymous_stub'); } } only in patch2: unchanged: --- a/core/modules/taxonomy/src/Plugin/migrate/destination/EntityTaxonomyTerm.php +++ /dev/null @@ -1,30 +0,0 @@ -isStub()) { - $row->setDestinationProperty('name', $this->t('Stub name for source tid:') . $row->getSourceProperty('tid')); - } - return parent::getEntity($row, $old_destination_id_values); - } - -} only in patch2: unchanged: --- a/core/modules/user/src/Plugin/migrate/destination/EntityUser.php +++ b/core/modules/user/src/Plugin/migrate/destination/EntityUser.php @@ -7,6 +7,7 @@ namespace Drupal\user\Plugin\migrate\destination; +use Drupal\Component\Utility\Unicode; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Password\PasswordInterface; @@ -98,4 +99,20 @@ public function import(Row $row, array $old_destination_id_values = array()) { return $ids; } + /** + * {@inheritdoc} + */ + protected function processStubRow(Row $row) { + parent::processStubRow($row); + // Work-around for https://www.drupal.org/node/2602066 - this function can + // be removed when that's committed. + $name = $row->getDestinationProperty('name'); + if (is_array($name)) { + $name = reset($name); + } + if (Unicode::strlen($name) > USERNAME_MAX_LENGTH) { + $row->setDestinationProperty('name', Unicode::substr($name, 0, USERNAME_MAX_LENGTH)); + } + } + }