From 7080c75945ea95aaae06f6f661591935bb5a4145 Mon Sep 17 00:00:00 2001 From: webchick Date: Wed, 18 Dec 2013 14:13:07 -0800 Subject: [PATCH] Issue #2152735 by chx: Migrate process plugin: extract. --- .../Plugin/migrate/process/Extract.php | 42 ++++++++++++ .../migrate/Tests/process/ExtractTest.php | 68 +++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 core/modules/migrate/lib/Drupal/migrate/Plugin/migrate/process/Extract.php create mode 100644 core/modules/migrate/tests/Drupal/migrate/Tests/process/ExtractTest.php diff --git a/core/modules/migrate/lib/Drupal/migrate/Plugin/migrate/process/Extract.php b/core/modules/migrate/lib/Drupal/migrate/Plugin/migrate/process/Extract.php new file mode 100644 index 00000000000..3c825b133d3 --- /dev/null +++ b/core/modules/migrate/lib/Drupal/migrate/Plugin/migrate/process/Extract.php @@ -0,0 +1,42 @@ +configuration['index'], $key_exists); + if (!$key_exists) { + throw new MigrateException('Array index missing, extraction failed.'); + } + return $new_value; + } + +} + diff --git a/core/modules/migrate/tests/Drupal/migrate/Tests/process/ExtractTest.php b/core/modules/migrate/tests/Drupal/migrate/Tests/process/ExtractTest.php new file mode 100644 index 00000000000..5e1e7d99963 --- /dev/null +++ b/core/modules/migrate/tests/Drupal/migrate/Tests/process/ExtractTest.php @@ -0,0 +1,68 @@ + 'Extract process plugin', + 'description' => 'Tests the extract process plugin.', + 'group' => 'Migrate', + ); + } + + /** + * {@inheritdoc} + */ + public function setUp() { + $configuration['index'] = array('foo'); + $this->plugin = new Extract($configuration, 'map', array()); + parent::setUp(); + } + + /** + * Tests successful extraction. + */ + public function testExtract() { + $value = $this->plugin->transform(array('foo' => 'bar'), $this->migrateExecutable, $this->row, 'destinationproperty'); + $this->assertSame($value, 'bar'); + } + + /** + * Tests invalid input. + * + * @expectedException \Drupal\migrate\MigrateException + * @expectedExceptionMessage Input should be an array. + */ + public function testExtractFromString() { + $this->plugin->transform('bar', $this->migrateExecutable, $this->row, 'destinationproperty'); + } + + /** + * Tests unsuccessful extraction. + * + * @expectedException \Drupal\migrate\MigrateException + * @expectedExceptionMessage Array index missing, extraction failed. + */ + public function testExtractFail() { + $this->plugin->transform(array('bar' => 'foo'), $this->migrateExecutable, $this->row, 'destinationproperty'); + } + +}