Skip to content
Snippets Groups Projects
Commit adbd5eac authored by Lucas Hedding's avatar Lucas Hedding Committed by Mike Ryan
Browse files

Issue #2711949 by heddn: Migrate Skip on Value (or non Value)

parent bb546170
No related branches found
No related tags found
No related merge requests found
<?php
/**
* @file
* Contains \Drupal\migrate_plus\Plugin\migrate\process\SkipOnValue.
*/
namespace Drupal\migrate_plus\Plugin\migrate\process;
use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\MigrateSkipProcessException;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
/**
* If the source evaluates to a configured value, skip processing or whole row.
*
* @MigrateProcessPlugin(
* id = "skip_on_value"
* )
*/
class SkipOnValue extends ProcessPluginBase {
/**
* {@inheritdoc}
*/
public function row($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if (empty($this->configuration['value']) && !array_key_exists('value', $this->configuration)) {
throw new MigrateException('Skip on value plugin is missing value configuration.');
}
if (is_array($this->configuration['value'])) {
foreach ($this->configuration['value'] as $skipValue) {
if ($this->compareValue($value, $skipValue, !isset($this->configuration['not_equals']))) {
throw new MigrateSkipRowException();
}
}
}
elseif ($this->compareValue($value, $this->configuration['value'], !isset($this->configuration['not_equals']))) {
throw new MigrateSkipRowException();
}
return $value;
}
/**
* {@inheritdoc}
*/
public function process($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if (empty($this->configuration['value']) && !array_key_exists('value', $this->configuration)) {
throw new MigrateException('Skip on value plugin is missing value configuration.');
}
if (is_array($this->configuration['value'])) {
foreach ($this->configuration['value'] as $skipValue) {
if ($this->compareValue($value, $skipValue, !isset($this->configuration['not_equals']))) {
throw new MigrateSkipProcessException();
}
}
}
elseif ($this->compareValue($value, $this->configuration['value'], !isset($this->configuration['not_equals']))) {
throw new MigrateSkipProcessException();
}
return $value;
}
/**
* Compare values to see if they are equal.
*
* @param $value
* Actual value
* @param $skipValue
* Value to compare against.
* @param $equal
* Compare as equal or not equal.
*
* @return bool
* True if the compare successfully, FALSE otherwise.
*/
protected function compareValue($value, $skipValue, $equal = TRUE) {
if ($equal) {
return (string) $value == (string) $skipValue;
}
return (string) $value != (string) $skipValue;
}
}
<?php
/**
* @file
* Contains \Drupal\Tests\migrate_plus\Unit\process\SkipOnValueTest.
*/
namespace Drupal\Tests\migrate_plus\Unit\process;
use Drupal\migrate_plus\Plugin\migrate\process\SkipOnValue;
use Drupal\Tests\migrate\Unit\process\MigrateProcessTestCase;
/**
* Tests the skip on value process plugin.
*
* @group migrate
* @coversDefaultClass \Drupal\migrate_plus\Plugin\migrate\process\SkipOnValue
*/
class SkipOnValueTest extends MigrateProcessTestCase {
/**
* @covers ::process
* @expectedException \Drupal\migrate\MigrateSkipProcessException
*/
public function testProcessSkipsOnValue() {
$configuration['method'] = 'process';
$configuration['value'] = 86;
(new SkipOnValue($configuration, 'skip_on_value', []))
->transform('86', $this->migrateExecutable, $this->row, 'destinationproperty');
}
/**
* @covers ::process
* @expectedException \Drupal\migrate\MigrateSkipProcessException
*/
public function testProcessSkipsOnMultipleValue() {
$configuration['method'] = 'process';
$configuration['value'] = [1, 1, 2, 3, 5, 8];
(new SkipOnValue($configuration, 'skip_on_value', []))
->transform('5', $this->migrateExecutable, $this->row, 'destinationproperty');
}
/**
* @covers ::process
*/
public function testProcessBypassesOnNonValue() {
$configuration['method'] = 'process';
$configuration['value'] = 'sourcevalue';
$configuration['not_equals'] = TRUE;
$value = (new SkipOnValue($configuration, 'skip_on_value', []))
->transform('sourcevalue', $this->migrateExecutable, $this->row, 'destinationproperty');
$this->assertEquals($value, 'sourcevalue');
$configuration['value'] = 86;
$value = (new SkipOnValue($configuration, 'skip_on_value', []))
->transform('86', $this->migrateExecutable, $this->row, 'destinationproperty');
$this->assertEquals($value, '86');
}
/**
* @covers ::process
*/
public function testProcessSkipsOnMultipleNonValue() {
$configuration['method'] = 'process';
$configuration['value'] = [1, 1, 2, 3, 5, 8];
$value = (new SkipOnValue($configuration, 'skip_on_value', []))
->transform(4, $this->migrateExecutable, $this->row, 'destinationproperty');
$this->assertEquals($value, '4');
}
/**
* @covers ::row
* @expectedException \Drupal\migrate\MigrateSkipRowException
*/
public function testRowSkipsOnValue() {
$configuration['method'] = 'row';
$configuration['value'] = 86;
(new SkipOnValue($configuration, 'skip_on_value', []))
->transform('86', $this->migrateExecutable, $this->row, 'destinationproperty');
}
/**
* @covers ::row
*/
public function testRowBypassesOnNonValue() {
$configuration['method'] = 'row';
$configuration['value'] = 'sourcevalue';
$configuration['not_equals'] = TRUE;
$value = (new SkipOnValue($configuration, 'skip_on_value', []))
->transform('sourcevalue', $this->migrateExecutable, $this->row, 'destinationproperty');
$this->assertEquals($value, 'sourcevalue');
$configuration['value'] = 86;
$value = (new SkipOnValue($configuration, 'skip_on_value', []))
->transform('86', $this->migrateExecutable, $this->row, 'destinationproperty');
$this->assertEquals($value, 86);
}
/**
* @covers ::row
* @expectedException \Drupal\migrate\MigrateException
*/
public function testRequiredRowConfiguration() {
$configuration['method'] = 'row';
(new SkipOnValue($configuration, 'skip_on_value', []))
->transform('sourcevalue', $this->migrateExecutable, $this->row, 'destinationproperty');
}
/**
* @covers ::process
* @expectedException \Drupal\migrate\MigrateException
*/
public function testRequiredProcessConfiguration() {
$configuration['method'] = 'process';
(new SkipOnValue($configuration, 'skip_on_value', []))
->transform('sourcevalue', $this->migrateExecutable, $this->row, 'destinationproperty');
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment