Commit 427d0805 authored by alexpott's avatar alexpott

Issue #2544690 by mikeryan, phenaproxima: Create new source plugin taking data from plugin config

parent a7fc1a27
...@@ -11,3 +11,24 @@ migrate.source.empty: ...@@ -11,3 +11,24 @@ migrate.source.empty:
provider: provider:
type: string type: string
label: 'Provider' label: 'Provider'
migrate.source.embedded_data:
type: migrate_source
label: 'Embedded data source'
mapping:
data_rows:
type: sequence
label: 'Data rows'
sequence:
type: ignore
label: 'Data row'
ids:
type: sequence
label: 'Unique key'
sequence:
type: mapping
label: 'Key column'
mapping:
type:
type: string
label: 'Column type'
<?php
/**
* @file
* Contains \Drupal\migrate\Plugin\migrate\source\EmbeddedDataSource.
*/
namespace Drupal\migrate\Plugin\migrate\source;
use Drupal\migrate\Entity\MigrationInterface;
/**
* Source which takes its data directly from the plugin config.
*
* @MigrateSource(
* id = "embedded_data"
* )
*/
class EmbeddedDataSource extends SourcePluginBase {
/**
* Data obtained from the source plugin configuration.
*
* @var array[]
* Array of data rows, each one an array of values keyed by field names.
*/
protected $dataRows = [];
/**
* Description of the unique ID fields for this source.
*
* @var array[]
* Each array member is keyed by a field name, with a value that is an
* array with a single member with key 'type' and value a column type such
* as 'integer'.
*/
protected $ids = [];
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
$this->dataRows = $configuration['data_rows'];
$this->ids = $configuration['ids'];
}
/**
* {@inheritdoc}
*/
public function fields() {
if ($this->count() > 0) {
$first_row = reset($this->dataRows);
$field_names = array_keys($first_row);
return array_combine($field_names, $field_names);
}
else {
return [];
}
}
/**
* {@inheritdoc}
*/
public function initializeIterator() {
return new \ArrayIterator($this->dataRows);
}
/**
* {@inheritdoc}
*/
public function __toString() {
return 'Embedded data';
}
/**
* {@inheritdoc}
*/
public function getIds() {
return $this->ids;
}
/**
* {@inheritdoc}
*/
public function count() {
return count($this->dataRows);
}
}
<?php
/**
* @file
* Contains \Drupal\migrate\Tests\MigrateEmbeddedDataTest.
*/
namespace Drupal\migrate\Tests;
use Drupal\migrate\Entity\Migration;
use Drupal\migrate\Row;
use Drupal\simpletest\KernelTestBase;
/**
* Tests the EmbeddedDataSource plugin.
*
* @group migrate
*/
class MigrateEmbeddedDataTest extends KernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['migrate'];
/**
* Tests the embedded_data source plugin.
*/
public function testEmbeddedData() {
$data_rows = [
['key' => '1', 'field1' => 'f1value1', 'field2' => 'f2value1'],
['key' => '2', 'field1' => 'f1value2', 'field2' => 'f2value2'],
];
$ids = ['key' => ['type' => 'integer']];
$config = [
'id' => 'sample_data',
'migration_tags' => ['Embedded data test'],
'source' => [
'plugin' => 'embedded_data',
'data_rows' => $data_rows,
'ids' => $ids,
],
'process' => [],
'destination' => ['plugin' => 'null'],
'load' => ['plugin' => 'null'],
];
$migration = Migration::create($config);
$source = $migration->getSourcePlugin();
// Validate the plugin returns the source data that was provided.
$results = [];
/** @var Row $row */
foreach ($source as $row) {
$data_row = $row->getSource();
// The "data" row returned by getSource() also includes all source
// configuration - we remove it so we see only the data itself.
unset($data_row['plugin']);
unset($data_row['data_rows']);
unset($data_row['ids']);
$results[] = $data_row;
}
$this->assertIdentical($results, $data_rows);
// Validate the public APIs.
$this->assertIdentical($source->count(), count($data_rows));
$this->assertIdentical($source->getIds(), $ids);
$expected_fields = [
'key' => 'key',
'field1' => 'field1',
'field2' => 'field2',
];
$this->assertIdentical($source->fields(), $expected_fields);
}
}
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
namespace Drupal\migrate\Tests; namespace Drupal\migrate\Tests;
use Drupal\Core\State\State;
use Drupal\migrate\Entity\Migration; use Drupal\migrate\Entity\Migration;
use Drupal\migrate\Event\MigrateImportEvent; use Drupal\migrate\Event\MigrateImportEvent;
use Drupal\migrate\Event\MigrateMapDeleteEvent; use Drupal\migrate\Event\MigrateMapDeleteEvent;
...@@ -70,7 +69,15 @@ public function testMigrateEvents() { ...@@ -70,7 +69,15 @@ public function testMigrateEvents() {
$config = [ $config = [
'id' => 'sample_data', 'id' => 'sample_data',
'migration_tags' => ['Event test'], 'migration_tags' => ['Event test'],
'source' => ['plugin' => 'data'], 'source' => [
'plugin' => 'embedded_data',
'data_rows' => [
['data' => 'dummy value'],
],
'ids' => [
'data' => ['type' => 'string'],
],
],
'process' => ['value' => 'data'], 'process' => ['value' => 'data'],
'destination' => ['plugin' => 'dummy'], 'destination' => ['plugin' => 'dummy'],
'load' => ['plugin' => 'null'], 'load' => ['plugin' => 'null'],
...@@ -96,8 +103,8 @@ public function testMigrateEvents() { ...@@ -96,8 +103,8 @@ public function testMigrateEvents() {
$event = $this->state->get('migrate_events_test.map_save_event', []); $event = $this->state->get('migrate_events_test.map_save_event', []);
$this->assertIdentical($event['event_name'], MigrateEvents::MAP_SAVE); $this->assertIdentical($event['event_name'], MigrateEvents::MAP_SAVE);
// Validating the last row processed. // Validating the last row processed.
$this->assertIdentical($event['fields']['sourceid1'], 'dummy value2'); $this->assertIdentical($event['fields']['sourceid1'], 'dummy value');
$this->assertIdentical($event['fields']['destid1'], 'dummy value2'); $this->assertIdentical($event['fields']['destid1'], 'dummy value');
$this->assertIdentical($event['fields']['source_row_status'], 0); $this->assertIdentical($event['fields']['source_row_status'], 0);
$event = $this->state->get('migrate_events_test.map_delete_event', []); $event = $this->state->get('migrate_events_test.map_delete_event', []);
...@@ -107,14 +114,14 @@ public function testMigrateEvents() { ...@@ -107,14 +114,14 @@ public function testMigrateEvents() {
$this->assertIdentical($event['event_name'], MigrateEvents::PRE_ROW_SAVE); $this->assertIdentical($event['event_name'], MigrateEvents::PRE_ROW_SAVE);
$this->assertIdentical($event['migration']->id(), $migration->id()); $this->assertIdentical($event['migration']->id(), $migration->id());
// Validating the last row processed. // Validating the last row processed.
$this->assertIdentical($event['row']->getSourceProperty('data'), 'dummy value2'); $this->assertIdentical($event['row']->getSourceProperty('data'), 'dummy value');
$event = $this->state->get('migrate_events_test.post_row_save_event', []); $event = $this->state->get('migrate_events_test.post_row_save_event', []);
$this->assertIdentical($event['event_name'], MigrateEvents::POST_ROW_SAVE); $this->assertIdentical($event['event_name'], MigrateEvents::POST_ROW_SAVE);
$this->assertIdentical($event['migration']->id(), $migration->id()); $this->assertIdentical($event['migration']->id(), $migration->id());
// Validating the last row processed. // Validating the last row processed.
$this->assertIdentical($event['row']->getSourceProperty('data'), 'dummy value2'); $this->assertIdentical($event['row']->getSourceProperty('data'), 'dummy value');
$this->assertIdentical($event['destination_id_values']['value'], 'dummy value2'); $this->assertIdentical($event['destination_id_values']['value'], 'dummy value');
// Generate a map delete event. // Generate a map delete event.
$migration->getIdMap()->delete(['data' => 'dummy value']); $migration->getIdMap()->delete(['data' => 'dummy value']);
......
...@@ -46,8 +46,17 @@ public function testMigrateEvents() { ...@@ -46,8 +46,17 @@ public function testMigrateEvents() {
// other than map_delete. // other than map_delete.
$config = [ $config = [
'id' => 'sample_data', 'id' => 'sample_data',
'migration_tags' => ['Event test'], 'migration_tags' => ['Interruption test'],
'source' => ['plugin' => 'data'], 'source' => [
'plugin' => 'embedded_data',
'data_rows' => [
['data' => 'dummy value'],
['data' => 'dummy value2'],
],
'ids' => [
'data' => ['type' => 'string'],
],
],
'process' => ['value' => 'data'], 'process' => ['value' => 'data'],
'destination' => ['plugin' => 'dummy'], 'destination' => ['plugin' => 'dummy'],
'load' => ['plugin' => 'null'], 'load' => ['plugin' => 'null'],
......
<?php
/**
* @file
* Contains \Drupal\migrate_events_test\Plugin\migrate\source\DataSource.
*/
namespace Drupal\migrate_events_test\Plugin\migrate\source;
use Drupal\migrate\Plugin\migrate\source\SourcePluginBase;
/**
* Source returning a single hard-coded data row.
*
* @MigrateSource(
* id = "data"
* )
*/
class DataSource extends SourcePluginBase {
/**
* {@inheritdoc}
*/
public function fields() {
return array(
'data' => t('Data'),
);
}
/**
* {@inheritdoc}
*/
public function initializeIterator() {
return new \ArrayIterator([
['data' => 'dummy value'],
['data' => 'dummy value2'],
]);
}
public function __toString() {
return 'Sample data for testing';
}
/**
* {@inheritdoc}
*/
public function getIds() {
$ids['data']['type'] = 'string';
return $ids;
}
/**
* {@inheritdoc}
*/
public function count() {
return 2;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment