You are here

public function MigrateEventsTest::testMigrateEvents in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/migrate/src/Tests/MigrateEventsTest.php \Drupal\migrate\Tests\MigrateEventsTest::testMigrateEvents()

Tests migration events.

File

core/modules/migrate/src/Tests/MigrateEventsTest.php, line 66
Contains \Drupal\migrate\Tests\MigrateEventsTest.

Class

MigrateEventsTest
Tests events fired on migrations.

Namespace

Drupal\migrate\Tests

Code

public function testMigrateEvents() {

  // Run a simple little migration, which should trigger one of each event
  // other than map_delete.
  $config = [
    'id' => 'sample_data',
    'migration_tags' => [
      'Event test',
    ],
    'source' => [
      'plugin' => 'embedded_data',
      'data_rows' => [
        [
          'data' => 'dummy value',
        ],
      ],
      'ids' => [
        'data' => [
          'type' => 'string',
        ],
      ],
    ],
    'process' => [
      'value' => 'data',
    ],
    'destination' => [
      'plugin' => 'dummy',
    ],
  ];
  $migration = Migration::create($config);

  /** @var MigrationInterface $migration */
  $executable = new MigrateExecutable($migration, new MigrateMessage());

  // As the import runs, events will be dispatched, recording the received
  // information in state.
  $executable
    ->import();

  // Validate from the recorded state that the events were received.
  $event = $this->state
    ->get('migrate_events_test.pre_import_event', []);
  $this
    ->assertIdentical($event['event_name'], MigrateEvents::PRE_IMPORT);
  $this
    ->assertIdentical($event['migration']
    ->id(), $migration
    ->id());
  $event = $this->state
    ->get('migrate_events_test.post_import_event', []);
  $this
    ->assertIdentical($event['event_name'], MigrateEvents::POST_IMPORT);
  $this
    ->assertIdentical($event['migration']
    ->id(), $migration
    ->id());
  $event = $this->state
    ->get('migrate_events_test.map_save_event', []);
  $this
    ->assertIdentical($event['event_name'], MigrateEvents::MAP_SAVE);

  // Validating the last row processed.
  $this
    ->assertIdentical($event['fields']['sourceid1'], 'dummy value');
  $this
    ->assertIdentical($event['fields']['destid1'], 'dummy value');
  $this
    ->assertIdentical($event['fields']['source_row_status'], 0);
  $event = $this->state
    ->get('migrate_events_test.map_delete_event', []);
  $this
    ->assertIdentical($event, []);
  $event = $this->state
    ->get('migrate_events_test.pre_row_save_event', []);
  $this
    ->assertIdentical($event['event_name'], MigrateEvents::PRE_ROW_SAVE);
  $this
    ->assertIdentical($event['migration']
    ->id(), $migration
    ->id());

  // Validating the last row processed.
  $this
    ->assertIdentical($event['row']
    ->getSourceProperty('data'), 'dummy value');
  $event = $this->state
    ->get('migrate_events_test.post_row_save_event', []);
  $this
    ->assertIdentical($event['event_name'], MigrateEvents::POST_ROW_SAVE);
  $this
    ->assertIdentical($event['migration']
    ->id(), $migration
    ->id());

  // Validating the last row processed.
  $this
    ->assertIdentical($event['row']
    ->getSourceProperty('data'), 'dummy value');
  $this
    ->assertIdentical($event['destination_id_values']['value'], 'dummy value');

  // Generate a map delete event.
  $migration
    ->getIdMap()
    ->delete([
    'data' => 'dummy value',
  ]);
  $event = $this->state
    ->get('migrate_events_test.map_delete_event', []);
  $this
    ->assertIdentical($event['event_name'], MigrateEvents::MAP_DELETE);
  $this
    ->assertIdentical($event['source_id'], [
    'data' => 'dummy value',
  ]);
}