Commit 81482505 authored by jigarius's avatar jigarius Committed by heddn

Issue #2881779 by jigarius, Jo Fitzgerald, heddn: Support "shift" and "pop"...

Issue #2881779 by jigarius, Jo Fitzgerald, heddn: Support "shift" and "pop" functionality in "extract" plugin
parent 89172075
<?php
namespace Drupal\migrate_plus\Plugin\migrate\process;
use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
/**
* Performs an array_pop() on a source array.
*
* @MigrateProcessPlugin(
* id = "array_pop",
* handle_multiples = TRUE
* )
*
* The "extract" plugin in core can extract array values when indexes are
* already known. This plugin helps extract the last value in an array by
* performing a "pop" operation.
*
* Example: Say, the migration source has an associative array of names in
* a property called "authors" and the keys in the array can vary, you
* can extract the last value like this:
*
* @code
* last_author:
* plugin: array_pop
* source: authors
* @endcode
*/
class ArrayPop extends ProcessPluginBase {
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if (!is_array($value)) {
throw new MigrateException('Input should be an array.');
}
return array_pop($value);
}
}
<?php
namespace Drupal\migrate_plus\Plugin\migrate\process;
use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
/**
* Performs an array_shift() on a source array.
*
* @MigrateProcessPlugin(
* id = "array_shift",
* handle_multiples = TRUE
* )
*
* The "extract" plugin in core can extract array values when indexes are
* already known. This plugin helps extract the first value in an array by
* performing a "shift" operation.
*
* Example: Say, the migration source has an associative array of names in
* a property called "authors" and the keys in the array can vary, you
* can extract the first value like this:
*
* @code
* first_author:
* plugin: array_shift
* source: authors
* @endcode
*/
class ArrayShift extends ProcessPluginBase {
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if (!is_array($value)) {
throw new MigrateException('Input should be an array.');
}
return array_shift($value);
}
}
......@@ -81,4 +81,3 @@ class Transliteration extends ProcessPluginBase implements ContainerFactoryPlugi
}
}
......@@ -2,11 +2,8 @@
namespace Drupal\Tests\migrate_plus\Kernel\Plugin\migrate_plus\data_fetcher;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\KernelTests\KernelTestBase;
use Drupal\migrate_plus\Plugin\migrate_plus\data_fetcher\Http;
use Drupal\Tests\Core\Test\KernelTestBaseTest;
use Drupal\Tests\UnitTestCase;
/**
* Class HttpTest
......
<?php
namespace Drupal\Tests\migrate_plus\Unit\process;
use Drupal\Tests\migrate\Unit\process\MigrateProcessTestCase;
use Drupal\migrate_plus\Plugin\migrate\process\ArrayPop;
use Drupal\migrate\MigrateException;
/**
* Tests the array pop process plugin.
*
* @group migrate
* @coversDefaultClass \Drupal\migrate_plus\Plugin\migrate\process\ArrayPop
*/
class ArrayPopTest extends MigrateProcessTestCase {
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->plugin = new ArrayPop([], 'array_pop', []);
parent::setUp();
}
/**
* Data provider for testArrayPop().
*
* @return array
* An array containing input values and expected output values.
*/
public function arrayPopDataProvider() {
return [
'indexed array' => [
'input' => ['v1', 'v2', 'v3'],
'expected_output' => 'v3',
],
'associative array' => [
'input' => ['i1' => 'v1', 'i2' => 'v2', 'i3' => 'v3'],
'expected_output' => 'v3',
],
'empty array' => [
'input' => [],
'expected_output' => NULL,
],
];
}
/**
* Test array pop plugin.
*
* @param array $input
* The input values.
* @param mixed $expected_output
* The expected output.
*
* @dataProvider arrayPopDataProvider
*/
public function testArrayPop(array $input, $expected_output) {
$output = $this->plugin->transform($input, $this->migrateExecutable, $this->row, 'destinationproperty');
$this->assertSame($output, $expected_output);
}
/**
* Test invalid input.
*/
public function testArrayPopFromString() {
$this->setExpectedException(MigrateException::class, 'Input should be an array.');
$this->plugin->transform('foo', $this->migrateExecutable, $this->row, 'destinationproperty');
}
}
<?php
namespace Drupal\Tests\migrate_plus\Unit\process;
use Drupal\Tests\migrate\Unit\process\MigrateProcessTestCase;
use Drupal\migrate_plus\Plugin\migrate\process\ArrayShift;
use Drupal\migrate\MigrateException;
/**
* Tests the array shift process plugin.
*
* @group migrate
* @coversDefaultClass \Drupal\migrate_plus\Plugin\migrate\process\ArrayShift
*/
class ArrayShiftTest extends MigrateProcessTestCase {
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->plugin = new ArrayShift([], 'array_shift', []);
parent::setUp();
}
/**
* Data provider for testArrayShift().
*
* @return array
* An array containing input values and expected output values.
*/
public function arrayShiftDataProvider() {
return [
'indexed array' => [
'input' => ['v1', 'v2', 'v3'],
'expected_output' => 'v1',
],
'associative array' => [
'input' => ['i1' => 'v1', 'i2' => 'v2', 'i3' => 'v3'],
'expected_output' => 'v1',
],
'empty array' => [
'input' => [],
'expected_output' => NULL,
],
];
}
/**
* Test array shift plugin.
*
* @param array $input
* The input values.
* @param mixed $expected_output
* The expected output.
*
* @dataProvider arrayShiftDataProvider
*/
public function testArrayShift(array $input, $expected_output) {
$output = $this->plugin->transform($input, $this->migrateExecutable, $this->row, 'destinationproperty');
$this->assertSame($output, $expected_output);
}
/**
* Test invalid input.
*/
public function testArrayShiftFromString() {
$this->setExpectedException(MigrateException::class, 'Input should be an array.');
$this->plugin->transform('foo', $this->migrateExecutable, $this->row, 'destinationproperty');
}
}
......@@ -56,7 +56,6 @@ class StrReplaceTest extends MigrateProcessTestCase {
/**
* Test for MigrateException for "search" configuration.
*
*/
public function testSearchMigrateException() {
$value = 'vero eos et accusam et justo vero';
......@@ -80,11 +79,11 @@ class StrReplaceTest extends MigrateProcessTestCase {
/**
* Test for multiple.
*/
public function testIsMultiple() {
public function testIsMultiple() {
$value = [
'vero eos et accusam et justo vero',
'et eos vero accusam vero justo et',
];
];
$expected = [
'vero eos that accusam that justo vero',
......
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