diff --git a/src/Plugin/migrate/process/SkipOnValue.php b/src/Plugin/migrate/process/SkipOnValue.php new file mode 100644 index 0000000000000000000000000000000000000000..0e8083acb5dd5ef55c051b1cf39dfbee31227d4e --- /dev/null +++ b/src/Plugin/migrate/process/SkipOnValue.php @@ -0,0 +1,92 @@ +<?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; + + } + +} diff --git a/tests/src/Unit/process/SkipOnValueTest.php b/tests/src/Unit/process/SkipOnValueTest.php new file mode 100644 index 0000000000000000000000000000000000000000..9ea11c271b977201e95b6e3b8eaf8eca0f1ab0b8 --- /dev/null +++ b/tests/src/Unit/process/SkipOnValueTest.php @@ -0,0 +1,117 @@ +<?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'); + } + +}