Commit e53654a7 authored by alexpott's avatar alexpott

Issue #2655154 by generalredneck, claudiu.cristea, gaurav.kapoor, olegel, Jo...

Issue #2655154 by generalredneck, claudiu.cristea, gaurav.kapoor, olegel, Jo Fitzgerald, mikeryan, phenaproxima: Optionally log messages for skip_on_empty and skip_row_if_not_set
parent c9f41a1c
...@@ -21,6 +21,9 @@ ...@@ -21,6 +21,9 @@
* - row: Skips the entire row when an empty value is encountered. * - row: Skips the entire row when an empty value is encountered.
* - process: Prevents further processing of the input property when the value * - process: Prevents further processing of the input property when the value
* is empty. * is empty.
* - message: (optional) A message to be logged in the {migrate_message_*} table
* for this row. Messages are only logged for the 'row' skip level. If not
* set, nothing is logged in the message table.
* *
* Examples: * Examples:
* *
...@@ -30,9 +33,11 @@ ...@@ -30,9 +33,11 @@
* plugin: skip_on_empty * plugin: skip_on_empty
* method: row * method: row
* source: field_name * source: field_name
* message: 'Field field_name is missed'
* @endcode * @endcode
* *
* If field_name is empty, skips the entire row. * If field_name is empty, skips the entire row and the message 'Field
* field_name is missed' is logged in the message table.
* *
* @code * @code
* process: * process:
...@@ -79,7 +84,8 @@ class SkipOnEmpty extends ProcessPluginBase { ...@@ -79,7 +84,8 @@ class SkipOnEmpty extends ProcessPluginBase {
*/ */
public function row($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { public function row($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if (!$value) { if (!$value) {
throw new MigrateSkipRowException(); $message = !empty($this->configuration['message']) ? $this->configuration['message'] : '';
throw new MigrateSkipRowException($message);
} }
return $value; return $value;
} }
......
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
* *
* Available configuration keys: * Available configuration keys:
* - index: The source property to check for. * - index: The source property to check for.
* - message: (optional) A message to be logged in the {migrate_message_*} table
* for this row. If not set, nothing is logged in the message table.
* *
* Example: * Example:
* *
...@@ -26,10 +28,12 @@ ...@@ -26,10 +28,12 @@
* plugin: skip_row_if_not_set * plugin: skip_row_if_not_set
* index: contact * index: contact
* source: data * source: data
* message: "Missed the 'data' key"
* @endcode * @endcode
* *
* This will return $data['contact'] if it exists. Otherwise, the row will be * This will return $data['contact'] if it exists. Otherwise, the row will be
* skipped. * skipped and the message "Missed the 'data' key" will be logged in the
* message table.
* *
* @see \Drupal\migrate\Plugin\MigrateProcessInterface * @see \Drupal\migrate\Plugin\MigrateProcessInterface
* *
...@@ -45,7 +49,8 @@ class SkipRowIfNotSet extends ProcessPluginBase { ...@@ -45,7 +49,8 @@ class SkipRowIfNotSet extends ProcessPluginBase {
*/ */
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if (!isset($value[$this->configuration['index']])) { if (!isset($value[$this->configuration['index']])) {
throw new MigrateSkipRowException(); $message = !empty($this->configuration['message']) ? $this->configuration['message'] : '';
throw new MigrateSkipRowException($message);
} }
return $value[$this->configuration['index']]; return $value[$this->configuration['index']];
} }
......
<?php <?php
namespace Drupal\Tests\migrate\Unit\process; namespace Drupal\Tests\migrate\Unit\process;
use Drupal\migrate\MigrateSkipProcessException; use Drupal\migrate\MigrateSkipProcessException;
use Drupal\migrate\MigrateSkipRowException; use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate\Plugin\migrate\process\SkipOnEmpty; use Drupal\migrate\Plugin\migrate\process\SkipOnEmpty;
...@@ -53,4 +54,33 @@ public function testRowBypassesOnNonEmpty() { ...@@ -53,4 +54,33 @@ public function testRowBypassesOnNonEmpty() {
$this->assertSame($value, ' '); $this->assertSame($value, ' ');
} }
/**
* Tests that a skip row exception without a message is raised.
*
* @covers ::row
*/
public function testRowSkipWithoutMessage() {
$configuration = [
'method' => 'row',
];
$process = new SkipOnEmpty($configuration, 'skip_on_empty', []);
$this->setExpectedException(MigrateSkipRowException::class);
$process->transform('', $this->migrateExecutable, $this->row, 'destinationproperty');
}
/**
* Tests that a skip row exception with a message is raised.
*
* @covers ::row
*/
public function testRowSkipWithMessage() {
$configuration = [
'method' => 'row',
'message' => 'The value is empty',
];
$process = new SkipOnEmpty($configuration, 'skip_on_empty', []);
$this->setExpectedException(MigrateSkipRowException::class, 'The value is empty');
$process->transform('', $this->migrateExecutable, $this->row, 'destinationproperty');
}
} }
<?php
namespace Drupal\Tests\migrate\Unit\process;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate\Plugin\migrate\process\SkipRowIfNotSet;
/**
* Tests the skip row if not set process plugin.
*
* @group migrate
* @coversDefaultClass \Drupal\migrate\Plugin\migrate\process\SkipRowIfNotSet
*/
class SkipRowIfNotSetTest extends MigrateProcessTestCase {
/**
* Tests that a skip row exception without a message is raised.
*
* @covers ::transform
*/
public function testRowSkipWithoutMessage() {
$configuration = [
'index' => 'some_key',
];
$process = new SkipRowIfNotSet($configuration, 'skip_row_if_not_set', []);
$this->setExpectedException(MigrateSkipRowException::class);
$process->transform('', $this->migrateExecutable, $this->row, 'destinationproperty');
}
/**
* Tests that a skip row exception with a message is raised.
*
* @covers ::transform
*/
public function testRowSkipWithMessage() {
$configuration = [
'index' => 'some_key',
'message' => "The 'some_key' key is not set",
];
$process = new SkipRowIfNotSet($configuration, 'skip_row_if_not_set', []);
$this->setExpectedException(MigrateSkipRowException::class, "The 'some_key' key is not set");
$process->transform('', $this->migrateExecutable, $this->row, 'destinationproperty');
}
}
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