function RowTest::testRowDestinations

Tests the destination properties of the Row class.

File

core/modules/migrate/tests/src/Kernel/RowTest.php, line 77

Class

RowTest
Tests the Row class.

Namespace

Drupal\Tests\migrate\Kernel

Code

public function testRowDestinations() : void {
  $storage = $this->entityTypeManager
    ->getStorage('entity_test');
  // Execute a migration that creates an entity with two fields.
  $data_rows = [
    [
      'id' => 1,
      'field1' => 'f1value',
      'field2' => 'f2value',
    ],
  ];
  $ids = [
    'id' => [
      'type' => 'integer',
    ],
  ];
  $definition = [
    'source' => [
      'plugin' => 'embedded_data',
      'data_rows' => $data_rows,
      'ids' => $ids,
    ],
    'process' => [
      'id' => 'id',
      'field1' => 'field1',
      'field2' => 'field2',
    ],
    'destination' => [
      'plugin' => 'entity:entity_test',
    ],
  ];
  $this->executeMigrationImport($definition);
  $entity = $storage->load(1);
  $this->assertEquals('f1value', $entity->get('field1')
    ->getValue()[0]['value']);
  $this->assertEquals('f2value', $entity->get('field2')
    ->getValue()[0]['value']);
  // Execute a second migration that attempts to remove both field values.
  // The event listener prevents the removal of the second field.
  $data_rows = [
    [
      'id' => 1,
      'field1' => NULL,
      'field2' => NULL,
    ],
  ];
  $definition['source']['data_rows'] = $data_rows;
  $this->eventDispatcher
    ->addListener(MigrateEvents::PRE_ROW_SAVE, [
    $this,
    'preventFieldRemoval',
  ]);
  $this->executeMigrationImport($definition);
  // The first field is now empty but the second field is still set.
  $entity = $storage->load(1);
  $this->assertTrue($entity->get('field1')
    ->isEmpty());
  $this->assertEquals('f2value', $entity->get('field2')
    ->getValue()[0]['value']);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.