getRowBySource example

    // plugin for the next possible row.     while (!isset($this->currentRow) && $this->getIterator()->valid()) {

      $row_data = $this->getIterator()->current() + $this->configuration;
      $this->fetchNextRow();
      $row = new Row($row_data$this->getIds());

      // Populate the source key for this row.       $this->currentSourceIds = $row->getSourceIdValues();

      // Pick up the existing map row, if any, unless fetchNextRow() did it.       if (!$this->mapRowAdded && ($id_map = $this->idMap->getRowBySource($this->currentSourceIds))) {
        $row->setIdMap($id_map);
      }

      // Clear any previous messages for this row before potentially adding       // new ones.       if (!empty($this->currentSourceIds)) {
        $this->idMap->delete($this->currentSourceIds, TRUE);
      }

      // Preparing the row gives source plugins the chance to skip.       if ($this->prepareRow($row) === FALSE) {
        
$config_id_map = $config_migration->getIdMap();

    // Rollback is not enabled for configuration translations.     $this->assertFalse($config_migration->getDestinationPlugin()->supportsRollback());

    // Import and validate config entities were created.     $config_executable = new MigrateExecutable($config_migration$this);
    $config_executable->import();
    $config = $this->config('system.site');
    $this->assertSame('Some site', $config->get('name'));
    $this->assertSame('Awesome slogan', $config->get('slogan'));
    $map_row = $config_id_map->getRowBySource(['id' => $variable[0]['id']]);
    $this->assertNotNull($map_row['destid1']);

    // Rollback and verify the configuration changes are still there.     $config_executable->rollback();
    $config = $this->config('system.site');
    $this->assertSame('Some site', $config->get('name'));
    $this->assertSame('Awesome slogan', $config->get('slogan'));
    // Confirm the map row is deleted.     $this->assertFalse($config_id_map->getRowBySource(['id' => $variable[0]['id']]));

    // We use system configuration to demonstrate importing and rolling back


    $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();

    // Check that the destination has been stored.     $map_row = $id_map_plugin->getRowBySource(['id' => 123, 'version' => 'foo']);
    $this->assertEquals(123, $map_row['destid1']);
    $map_row = $id_map_plugin->getRowBySource(['id' => 123456789012, 'version' => 'bar']);
    $this->assertEquals(123456789012, $map_row['destid1']);
  }

  /** * Tests empty destinations. */
  public function testEmptyDestinations() {
    $this->enableModules(['migrate_entity_test']);
    $this->installEntitySchema('migrate_string_id_entity_test');

    
public function testTrackChanges() {
    // Assert all of the terms have been imported.     $this->assertTermExists('name', 'Item 1');
    $this->assertTermExists('name', 'Item 2');
    $this->assertTermExists('description', 'Text item 3');
    $this->assertTermExists('description', 'Text item 4');

    // Save the original hash, rerun the migration and check that the hashes     // are the same.     $id_map = $this->migration->getIdMap();
    for ($i = 1; $i < 5; $i++) {
      $row = $id_map->getRowBySource(['tid' => $i]);
      $original_hash[$i] = $row['hash'];
    }
    $this->executeMigration($this->migration);
    for ($i = 1; $i < 5; $i++) {
      $row = $id_map->getRowBySource(['tid' => $i]);
      $new_hash[$i] = $row['hash'];
    }
    $this->assertEquals($original_hash$new_hash);

    // Update Item 1 triggering its track_changes by name.     $this->sourceDatabase->update('track_changes_term')
      


    $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);

    // The second row is not recorded in the map.     $map_row = $id_map_plugin->getRowBySource(['id' => 2]);
    $this->assertFalse($map_row);
    // Check that the correct message has been logged for the second exception.     $messages = $id_map_plugin->getMessages(['id' => 2])->fetchAll();
    $this->assertCount(1, $messages);
    
'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 */
    $stub_entity = Term::load(2);
    $this->assertNotEmpty($stub_entity, 'Stub successfully created');
    if ($stub_entity) {
      $this->assertCount(0, $stub_entity->validate(), 'Stub is a valid entity');
    }
  }

}
$plugin_manager = \Drupal::service('plugin.manager.migration');
    $version = $this->getLegacyDrupalVersion($this->sourceDatabase);
    /** @var \Drupal\migrate\Plugin\Migration[] $all_migrations */
    $all_migrations = $plugin_manager->createInstancesByTag('Drupal ' . $version);
    foreach ($all_migrations as $migration) {
      $id_map = $migration->getIdMap();
      foreach ($id_map as $source_id => $map) {
        // Convert $source_id into a keyless array so that         // \Drupal\migrate\Plugin\migrate\id_map\Sql::getSourceHash() works as         // expected.         $source_id_values = array_values(unserialize($source_id));
        $row = $id_map->getRowBySource($source_id_values);
        $destination = serialize($id_map->currentDestination());
        $message = "Migration of $source_id to $destination as part of the {$migration->id()} migration. The source row status is " . $row['source_row_status'];
        // A completed migration should have maps with         // MigrateIdMapInterface::STATUS_IGNORED or         // MigrateIdMapInterface::STATUS_IMPORTED.         $this->assertNotSame(MigrateIdMapInterface::STATUS_FAILED, $row['source_row_status']$message);
        $this->assertNotSame(MigrateIdMapInterface::STATUS_NEEDS_UPDATE, $row['source_row_status']$message);
      }
    }
  }

  
$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']);
    }

    // We use taxonomy terms to demonstrate importing and rolling back content     // entities.     $term_data_rows = [
      ['id' => '1', 'vocab' => '1', 'name' => 'music'],
      ['id' => '2', 'vocab' => '2', 'name' => 'Bach'],
      ['id' => '3', 'vocab' => '2', 'name' => 'Beethoven'],
    ];
    $ids = ['id' => ['type' => 'integer']];
    
$executable = new MigrateExecutable($migration);
    $result = $executable->import();

    // Check that the migration has completed.     $this->assertEquals(MigrationInterface::RESULT_COMPLETED, $result);

    /** @var \Drupal\migrate\Plugin\MigrateIdMapInterface $id_map_plugin */
    $id_map_plugin = $migration->getIdMap();

    // Check that the first row was marked as failed in the id map table.     $map_row = $id_map_plugin->getRowBySource(['url' => $invalid_url]);
    $this->assertEquals(MigrateIdMapInterface::STATUS_FAILED, $map_row['source_row_status']);
    $this->assertNull($map_row['destid1']);

    // Check that a message with the thrown exception has been logged.     $messages = $id_map_plugin->getMessages(['url' => $invalid_url])->fetchAll();
    $this->assertCount(1, $messages);
    $message = reset($messages);

    // Assert critical parts of the error message, but not the exact message,     // since it depends on Guzzle's internal implementation of PSR-7.     $id = $migration->getPluginId();
    

    }
    // Assert that test values exist.     $this->queryResultTest($this->getIdMapContents()$expected_results);

    // Assert a single row needs an update.     $row_needing_update = $id_map->getRowsNeedingUpdate(1);
    $this->assertCount(1, $row_needing_update);

    // Assert the row matches its original source.     $source_id = $expected_results[MigrateIdMapInterface::STATUS_NEEDS_UPDATE]['sourceid1'];
    $test_row = $id_map->getRowBySource(['source_id_property' => $source_id]);
    // $row_needing_update is an array of objects returned from the database,     // but $test_row is an array, so the cast is necessary.     $this->assertSame($test_row(array) $row_needing_update[0]);

    // Add additional row that needs an update.     $source = ['source_id_property' => 'source_value_multiple'];
    $row = new Row($source['source_id_property' => []]);
    $destination = ['destination_id_property' => 'destination_value_multiple'];
    $id_map->saveIdMapping($row$destination, MigrateIdMapInterface::STATUS_NEEDS_UPDATE);

    // Assert multiple rows need an update.
$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 = [
      [
        'id' => '1',
        'name' => '1',
        'language' => 'fr',
        'property' => 'name',
        'translation' => 'fr - categories',
      ],
Home | Imprint | This part of the site doesn't use cookies.