function MigrateEntityContentBaseTest::testFields
Test destination fields() method.
File
-
core/
modules/ migrate/ tests/ src/ Kernel/ MigrateEntityContentBaseTest.php, line 370
Class
- MigrateEntityContentBaseTest
- Tests the EntityContentBase destination.
Namespace
Drupal\Tests\migrate\KernelCode
public function testFields() : void {
$entity_type_manager = $this->container
->get('entity_type.manager');
// Create two bundles for the entity_test_with_bundle entity type.
$bundle_storage = $entity_type_manager->getStorage('entity_test_bundle');
$bundle_storage->create([
'id' => 'test_bundle_no_fields',
'label' => 'Test bundle without fields',
])
->save();
$bundle_storage->create([
'id' => 'test_bundle_with_fields',
'label' => 'Test bundle with fields',
])
->save();
// Create a mock migration and get the destination plugin manager.
$migration = $this->prophesize(MigrationInterface::class)
->reveal();
/** @var \Drupal\migrate\Plugin\MigrateDestinationPluginManager $manager */
$manager = \Drupal::service('plugin.manager.migrate.destination');
// Test with an entity type with no bundles.
$destination_plugin = $manager->createInstance('entity:entity_test_no_bundle', [], $migration);
$fields = $destination_plugin->fields();
$this->assertArrayHasKey('id', $fields);
// Confirm the test field is not found.
$this->assertArrayNotHasKey('field_text', $fields);
// Create a text field attached to the entity with no bundles.
FieldStorageConfig::create([
'type' => 'string',
'entity_type' => 'entity_test_no_bundle',
'field_name' => 'field_text',
])->save();
FieldConfig::create([
'entity_type' => 'entity_test_no_bundle',
'bundle' => 'entity_test_no_bundle',
'field_name' => 'field_text',
])->save();
// Confirm that the 'field_text' is now found.
$destination_plugin = $manager->createInstance('entity:entity_test_no_bundle', [], $migration);
$fields = $destination_plugin->fields();
$this->assertArrayHasKey('id', $fields);
$this->assertArrayHasKey('field_text', $fields);
// Repeat the test with an entity with bundles.
$destination_plugin = $manager->createInstance('entity:entity_test_with_bundle', [], $migration);
$fields = $destination_plugin->fields();
$this->assertArrayHasKey('id', $fields);
$this->assertArrayNotHasKey('field_text', $fields);
// Create a text field attached to the entity with bundles.
FieldStorageConfig::create([
'type' => 'string',
'entity_type' => 'entity_test_with_bundle',
'field_name' => 'field_text',
])->save();
FieldConfig::create([
'entity_type' => 'entity_test_with_bundle',
'bundle' => 'test_bundle_with_fields',
'field_name' => 'field_text',
])->save();
// Confirm that the 'field_text' is found when the default bundle is set.
$destination_plugin = $manager->createInstance('entity:entity_test_with_bundle', [
'default_bundle' => 'test_bundle_with_fields',
], $migration);
$fields = $destination_plugin->fields();
$this->assertArrayHasKey('id', $fields);
$this->assertArrayHasKey('field_text', $fields);
// Confirm that the 'field_text' is not found when the default bundle is not
// set.
$destination_plugin = $manager->createInstance('entity:entity_test_with_bundle', [], $migration);
$fields = $destination_plugin->fields();
$this->assertArrayHasKey('id', $fields);
$this->assertArrayNotHasKey('field_text', $fields);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.