From 0aed30b60ec326e775c0a036b35d94d3fe2c5c71 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Thu, 27 Oct 2016 22:36:17 -0700 Subject: [PATCH] Issue #2675156 by maijs, rocketeerbkw, mitrpaka, quietone: Pipeline using explode and extract fails --- .../src/Plugin/migrate/process/Extract.php | 3 +- .../tests/src/Kernel/process/ExtractTest.php | 111 ++++++++++++++++++ 2 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 core/modules/migrate/tests/src/Kernel/process/ExtractTest.php diff --git a/core/modules/migrate/src/Plugin/migrate/process/Extract.php b/core/modules/migrate/src/Plugin/migrate/process/Extract.php index 81671d87945..1ac88ca5bf9 100644 --- a/core/modules/migrate/src/Plugin/migrate/process/Extract.php +++ b/core/modules/migrate/src/Plugin/migrate/process/Extract.php @@ -14,7 +14,8 @@ use Drupal\migrate\Row; * @link https://www.drupal.org/node/2152731 Online handbook documentation for extract process plugin @endlink * * @MigrateProcessPlugin( - * id = "extract" + * id = "extract", + * handle_multiples = TRUE * ) */ class Extract extends ProcessPluginBase { diff --git a/core/modules/migrate/tests/src/Kernel/process/ExtractTest.php b/core/modules/migrate/tests/src/Kernel/process/ExtractTest.php new file mode 100644 index 00000000000..8c747abadf5 --- /dev/null +++ b/core/modules/migrate/tests/src/Kernel/process/ExtractTest.php @@ -0,0 +1,111 @@ + [ + 'plugin' => 'embedded_data', + 'data_rows' => [], + 'ids' => [ + 'id' => ['type' => 'string'], + ], + ], + 'process' => [ + 'first' => [ + 'plugin' => 'extract', + 'index' => [0], + 'source' => 'simple_array', + ], + 'second' => [ + 'plugin' => 'extract', + 'index' => [1], + 'source' => 'complex_array', + ], + ], + 'destination' => [ + 'plugin' => 'config', + 'config_name' => 'migrate_test.settings', + ], + ]; + } + + /** + * Tests multiple value handling. + * + * @dataProvider multipleValueProviderSource + * + * @param array $source_data + * @param array $expected_data + */ + public function testMultipleValueExplode(array $source_data, array $expected_data) { + $definition = $this->getDefinition(); + $definition['source']['data_rows'] = [$source_data]; + + $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition); + + $executable = new MigrateExecutable($migration, new MigrateMessage()); + $result = $executable->import(); + + // Migration needs to succeed before further assertions are made. + $this->assertSame(MigrationInterface::RESULT_COMPLETED, $result); + + // Compare with expected data. + $this->assertEquals($expected_data, \Drupal::config('migrate_test.settings')->get()); + } + + /** + * Provides multiple source data for "extract" process plugin test. + */ + public function multipleValueProviderSource() { + $tests = [ + [ + 'source_data' => [ + 'id' => '1', + 'simple_array' => ['alpha', 'beta'], + 'complex_array' => [['alpha', 'beta'], ['psi', 'omega']], + ], + 'expected_data' => [ + 'first' => 'alpha', + 'second' => ['psi', 'omega'], + ], + ], + [ + 'source_data' => [ + 'id' => '2', + 'simple_array' => ['one'], + 'complex_array' => [0, 1], + ], + 'expected_data' => [ + 'first' => 'one', + 'second' => 1, + ], + ], + ]; + + return $tests; + } + +}