MigrateExecutable example


      'destination' => [
        'plugin' => 'entity:taxonomy_term',
        'default_bundle' => 'categories',
      ],
      'migration_dependencies' => [],
    ];

    $term_migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);

    // Import and validate the term entity was created with the correct bundle.     $term_executable = new MigrateExecutable($term_migration$this);
    $term_executable->import();
    /** @var \Drupal\taxonomy\Entity\Term $term */
    $term = Term::load(1);
    $this->assertEquals('categories', $term->bundle());
  }

  /** * Tests setting the bundle in the process pipeline. */
  public function testProcessBundle() {
    $term_data_rows = [
      [

  protected function executeRollback($migration) {
    if (is_string($migration)) {
      $this->migration = $this->getMigration($migration);
    }
    else {
      $this->migration = $migration;
    }
    (new MigrateExecutable($this->migration, $this))->rollback();
  }

}
'plugin' => 'embedded_data',
        'data_rows' => $vocabulary_data_rows,
        'ids' => $ids,
      ],
      'process' => [
        'vid' => 'id',
        'name' => 'name',
      ],
      'destination' => ['plugin' => 'entity:taxonomy_vocabulary'],
    ];
    $vocabulary_migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
    $vocabulary_executable = new MigrateExecutable($vocabulary_migration$this);
    $vocabulary_executable->import();

    // We have a term referencing an unmigrated parent, forcing a stub to be     // created.     $migration = $this->getMigration('taxonomy_term_stub_test');
    $term_executable = new MigrateExecutable($migration$this);
    $term_executable->import();
    $this->assertNotEmpty($migration->getIdMap()->getRowBySource(['2']), 'Stub row exists in the ID map table');

    // Load the referenced term, which should exist as a stub.     /** @var \Drupal\Core\Entity\ContentEntityBase $stub_entity */
    

  protected function doCreateStub(MigrationInterface $migration, array $source_ids, array $default_values = []) {
    $destination = $migration->getDestinationPlugin(TRUE);
    $process = $migration->getProcess();
    $id_map = $migration->getIdMap();
    $migrate_executable = new MigrateExecutable($migration);
    $row = new Row($source_ids + $migration->getSourceConfiguration()$migration->getSourcePlugin()->getIds(), TRUE);
    $migrate_executable->processRow($row$process);
    foreach ($default_values as $key => $value) {
      $row->setDestinationProperty($key$value);
    }
    $destination_ids = [];
    try {
      $destination_ids = $destination->import($row);
    }
    catch (\Exception $e) {
      $id_map->saveMessage($row->getSourceIdValues()$e->getMessage());
    }

  protected function getMigrateExecutable($migration) {
    /** @var \Drupal\migrate\MigrateMessageInterface $message */
    $message = $this->createMock('Drupal\migrate\MigrateMessageInterface');
    /** @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface $event_dispatcher */
    $event_dispatcher = $this->createMock('Symfony\Contracts\EventDispatcher\EventDispatcherInterface');
    return new MigrateExecutable($migration$message$event_dispatcher);
  }

}

/** * Stubbed source plugin for testing base class implementations. */
class StubSourcePlugin extends SourcePluginBase {

  /** * Helper for setting internal module handler implementation. * * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler. */

      'process' => ['value' => 'data'],
      'destination' => [
        'plugin' => 'config',
        'config_name' => 'migrate_test.settings',
      ],
      'load' => ['plugin' => 'null'],
    ];

    $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);

    $executable = new MigrateExecutable($migration);
    $result = $executable->import();
    $this->assertEquals(MigrationInterface::RESULT_COMPLETED, $result);

    /** @var \Drupal\migrate\Plugin\MigrateIdMapInterface $id_map_plugin */
    $id_map_plugin = $migration->getIdMap();
    // The first row is recorded in the map as ignored.     $map_row = $id_map_plugin->getRowBySource(['id' => 1]);
    $this->assertEquals(MigrateIdMapInterface::STATUS_IGNORED, $map_row['source_row_status']);
    // Check that no message has been logged for the first exception.     $messages = $id_map_plugin->getMessages(['id' => 1])->fetchAll();
    $this->assertEmpty($messages);

    
        $this->assertEmpty($user->user_picture->target_id, sprintf('User %s does not have a picture', $user->id()));
      }

      // Use the API to check if the password has been salted and re-hashed to       // conform to Drupal >= 7 for non-admin users.       if ($user->id() != 1) {
        $this->assertTrue(\Drupal::service('password')
          ->check($source->pass_plain, $user->getPassword()));
      }
    }
    // Rollback the migration and make sure everything is deleted but uid 1.     (new MigrateExecutable($this->migration, $this))->rollback();
    $users = Database::getConnection('default', 'migrate')
      ->select('users', 'u')
      ->fields('u', ['uid'])
      ->condition('uid', 0, '>')
      ->execute()
      ->fetchCol();
    foreach ($users as $uid) {
      $account = User::load($uid);
      if ($uid == 1) {
        $this->assertNotNull($account, 'User 1 was preserved after rollback');
      }
      


    /** @var \Drupal\migrate\Plugin\Migration $vocabulary_migration */
    $vocabulary_migration = \Drupal::service('plugin.manager.migration')
      ->createStubMigration($definition);
    $vocabulary_id_map = $vocabulary_migration->getIdMap();

    $this->assertTrue($vocabulary_migration->getDestinationPlugin()
      ->supportsRollback());

    // Import and validate vocabulary config entities were created.     $vocabulary_executable = new MigrateExecutable($vocabulary_migration$this);
    $vocabulary_executable->import();
    foreach ($vocabulary_data_rows as $row) {
      /** @var \Drupal\taxonomy\Entity\Vocabulary $vocabulary */
      $vocabulary = Vocabulary::load($row['id']);
      $this->assertNotEmpty($vocabulary);
      $map_row = $vocabulary_id_map->getRowBySource(['id' => $row['id']]);
      $this->assertNotNull($map_row['destid1']);
    }

    // Second, import translations of the vocabulary name property.     $vocabulary_i18n_data_rows = [
      [
$node = $storage->load(3);
    $this->assertEquals('en', $node->language()->getId());
    $this->assertEquals('Monkey', $node->title->value);
    $this->assertFalse($node->hasTranslation('fr'), "No french translation for node 3");
    $this->assertFalse($node->hasTranslation('es'), "No spanish translation for node 3");

    $this->assertNull($storage->load(4), "No node 4 migrated");

    // Roll back the migrations.     foreach ($migration_ids as $migration_id) {
      $migration = $this->getMigration($migration_id);
      $executable = new MigrateExecutable($migration$this);
      $executable->rollback();
    }

    $this->assertCount(0, $storage->loadMultiple());
  }

}

  protected function executeRollback($migration) {
    if (is_string($migration)) {
      $this->migration = $this->getMigration($migration);
    }
    else {
      $this->migration = $migration;
    }
    (new MigrateExecutable($this->migration, $this))->rollback();
  }

}

        'ids' => [
          'data' => ['type' => 'string'],
        ],
      ],
      'process' => ['value' => 'data'],
      'destination' => ['plugin' => 'dummy'],
    ];

    $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);

    $executable = new MigrateExecutable($migration);
    // When the import runs, the first row imported will trigger an     // interruption.     $result = $executable->import();

    $this->assertEquals(MigrationInterface::RESULT_INCOMPLETE, $result);

    // The status should have been reset to IDLE.     $this->assertEquals(MigrationInterface::STATUS_IDLE, $migration->getStatus());
  }

  /** * Reacts to post-row-save event. * * @param \Drupal\migrate\Event\MigratePostRowSaveEvent $event * The migration event. * @param string $name * The event name. */
$sub_process_plugins[$destination][] = new Get(['source' => $source], 'get', []);
    }
    // Set up the key plugins.     $key_plugin['key'][] = new Get(['source' => '@id'], 'get', []);
    $migration->expects($this->exactly(2))
      ->method('getProcessPlugins')
      ->willReturnOnConsecutiveCalls(
        $sub_process_plugins,
        $key_plugin,
      );
    $event_dispatcher = $this->createMock(EventDispatcherInterface::class);
    $migrate_executable = new MigrateExecutable($migration$this->createMock(MigrateMessageInterface::class)$event_dispatcher);

    // The current value of the pipeline.     $current_value = [
      [
        'source_foo' => 'test',
        'source_id' => 42,
      ] + $source_values,
    ];
    // This is not used but the interface requires it, so create an empty row.     $row = new Row($source_values);

    

        'ids' => [
          'data' => ['type' => 'string'],
        ],
      ],
      'process' => ['value' => 'data'],
      'destination' => ['plugin' => 'dummy'],
    ];

    $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);

    $executable = new MigrateExecutable($migration);
    // 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->assertSame(MigrateEvents::PRE_IMPORT, $event['event_name']);
    $this->assertSame($migration->id()$event['migration']->id());

    $event = $this->state->get('migrate_events_test.post_import_event', []);
    $this->assertSame(MigrateEvents::POST_IMPORT, $event['event_name']);
    

  public function execute() {
    $migration_plugin_manager = \Drupal::service('plugin.manager.migration');
    $definitions = $migration_plugin_manager->getDefinitions();
    if ($definitions['node_migration_no_migrate_drupal']['label'] !== 'Node Migration No Migrate Drupal') {
      throw new InvalidPluginDefinitionException('node_migration_no_migrate_drupal');
    }
    $migrations = $migration_plugin_manager->createInstances('');
    $result = (new MigrateExecutable($migrations['node_migration_no_migrate_drupal']))->import();
    if ($result !== MigrationInterface::RESULT_COMPLETED) {
      throw new \RuntimeException('Migration failed');
    }

    return [
      '#type' => 'markup',
      '#markup' => 'Migration was successful.',
    ];
  }

}


    $message = $this->prophesize(MigrateMessageInterface::class);
    // Match the expected message. Can't use default argument types, because     // we need to convert to string from TranslatableMarkup.     $argument = Argument::that(function D$msg) {
      return str_contains((string) $msghtmlentities('The "no_language_entity_test" entity type does not support translations.'));
    });
    $message->display($argument, Argument::any())
      ->shouldBeCalled();

    $executable = new MigrateExecutable($migration$message->reveal());
    $executable->import();
  }

}
Home | Imprint | This part of the site doesn't use cookies.