public function EntityGenerateTest::testTransform in Migrate Plus 8.5
Same name and namespace in other branches
- 8.3 tests/src/Kernel/Plugin/migrate/process/EntityGenerateTest.php \Drupal\Tests\migrate_plus\Kernel\Plugin\migrate\process\EntityGenerateTest::testTransform()
- 8.4 tests/src/Kernel/Plugin/migrate/process/EntityGenerateTest.php \Drupal\Tests\migrate_plus\Kernel\Plugin\migrate\process\EntityGenerateTest::testTransform()
Tests generating an entity.
@dataProvider transformDataProvider
@covers ::transform
File
- tests/
src/ Kernel/ Plugin/ migrate/ process/ EntityGenerateTest.php, line 135
Class
- EntityGenerateTest
- Tests the migration plugin.
Namespace
Drupal\Tests\migrate_plus\Kernel\Plugin\migrate\processCode
public function testTransform(array $definition, array $expected, array $preSeed = []) : void {
// Pre seed some test data.
foreach ($preSeed as $storageName => $values) {
// If the first element of $values is a non-empty array, create multiple
// entities. Otherwise, create just one entity.
if (isset($values[0])) {
foreach ($values as $itemValues) {
$this
->createTestData($storageName, $itemValues);
}
}
else {
$this
->createTestData($storageName, $values);
}
}
/** @var \Drupal\migrate\Plugin\Migration $migration */
$migration = $this->migrationPluginManager
->createStubMigration($definition);
$reflector = new \ReflectionObject($migration
->getDestinationPlugin());
$attribute = $reflector
->getProperty('storage');
$attribute
->setAccessible(true);
/** @var \Drupal\Core\Entity\EntityStorageBase $storage */
$storage = $attribute
->getValue($migration
->getDestinationPlugin());
$migrationExecutable = new MigrateExecutable($migration, $this);
$migrationExecutable
->import();
foreach ($expected as $row) {
$entity = $storage
->load($row['id']);
$properties = array_diff_key($row, array_flip([
'id',
]));
foreach ($properties as $property => $value) {
if (is_array($value)) {
if (empty($value)) {
$this
->assertEmpty($entity->{$property}
->getValue(), "Expected value is 'unset' but field {$property} is set.");
}
else {
// Check if we're testing multiple values in one field. If so, loop
// through them one-by-one and check that they're present in the
// $entity.
if (isset($value[0])) {
foreach ($value as $valueID => $valueToCheck) {
foreach ($valueToCheck as $key => $expectedValue) {
if (empty($expectedValue)) {
if (!$entity->{$property}
->isEmpty()) {
$this
->assertTrue($entity->{$property}[0]->entity->{$key}
->isEmpty(), "Expected value is empty but field {$property}.{$key} is not empty.");
}
else {
$this
->assertTrue($entity->{$property}
->isEmpty(), "Expected value is empty but field {$property} is not empty.");
}
}
elseif ($entity->{$property}
->getValue()) {
$this
->assertEquals($expectedValue, $entity
->get($property)
->offsetGet($valueID)->entity->{$key}->value);
}
else {
$this
->fail("Expected value: {$expectedValue} does not exist in {$property}.");
}
}
}
}
else {
foreach ($value as $key => $expectedValue) {
if (empty($expectedValue)) {
if (!$entity->{$property}
->isEmpty()) {
$this
->assertTrue($entity->{$property}[0]->entity->{$key}
->isEmpty(), "Expected value is empty but field {$property}.{$key} is not empty.");
}
else {
$this
->assertTrue($entity->{$property}
->isEmpty(), "BINBAZ Expected value is empty but field {$property} is not empty.");
}
}
elseif ($entity->{$property}
->getValue()) {
$referenced_entity = $entity->{$property}[0]->entity;
$result_value = $referenced_entity instanceof ConfigEntityInterface ? $referenced_entity
->get($key) : $referenced_entity
->get($key)->value;
$this
->assertEquals($expectedValue, $result_value);
}
else {
$this
->fail("Expected value: {$expectedValue} does not exist in {$property}.");
}
}
}
}
}
else {
$this
->assertNotEmpty($entity, 'Entity with label ' . $row[$property] . ' is empty');
$this
->assertEquals($row[$property], $entity
->label());
}
}
}
}