summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2017-04-11 21:40:13 (GMT)
committerAlex Pott2017-04-11 21:40:13 (GMT)
commite53654a7650643e4909e5c044770fe74b6e587f2 (patch)
treee45dbf876c011f49292d4b9e32785d669e463728
parentc9f41a1cd29c09c74fa95e2db9403e38fe797b0f (diff)
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
-rw-r--r--core/modules/migrate/src/Plugin/migrate/process/SkipOnEmpty.php10
-rw-r--r--core/modules/migrate/src/Plugin/migrate/process/SkipRowIfNotSet.php9
-rw-r--r--core/modules/migrate/tests/src/Unit/process/SkipOnEmptyTest.php30
-rw-r--r--core/modules/migrate/tests/src/Unit/process/SkipRowIfNotSetTest.php45
4 files changed, 90 insertions, 4 deletions
diff --git a/core/modules/migrate/src/Plugin/migrate/process/SkipOnEmpty.php b/core/modules/migrate/src/Plugin/migrate/process/SkipOnEmpty.php
index 45626be..c7d3ad5 100644
--- a/core/modules/migrate/src/Plugin/migrate/process/SkipOnEmpty.php
+++ b/core/modules/migrate/src/Plugin/migrate/process/SkipOnEmpty.php
@@ -21,6 +21,9 @@ use Drupal\migrate\MigrateSkipRowException;
* - row: Skips the entire row when an empty value is encountered.
* - process: Prevents further processing of the input property when the value
* 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:
*
@@ -30,9 +33,11 @@ use Drupal\migrate\MigrateSkipRowException;
* plugin: skip_on_empty
* method: row
* source: field_name
+ * message: 'Field field_name is missed'
* @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
* process:
@@ -79,7 +84,8 @@ class SkipOnEmpty extends ProcessPluginBase {
*/
public function row($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if (!$value) {
- throw new MigrateSkipRowException();
+ $message = !empty($this->configuration['message']) ? $this->configuration['message'] : '';
+ throw new MigrateSkipRowException($message);
}
return $value;
}
diff --git a/core/modules/migrate/src/Plugin/migrate/process/SkipRowIfNotSet.php b/core/modules/migrate/src/Plugin/migrate/process/SkipRowIfNotSet.php
index ab7552e..fa9b3f6 100644
--- a/core/modules/migrate/src/Plugin/migrate/process/SkipRowIfNotSet.php
+++ b/core/modules/migrate/src/Plugin/migrate/process/SkipRowIfNotSet.php
@@ -16,6 +16,8 @@ use Drupal\migrate\MigrateSkipRowException;
*
* Available configuration keys:
* - 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:
*
@@ -26,10 +28,12 @@ use Drupal\migrate\MigrateSkipRowException;
* plugin: skip_row_if_not_set
* index: contact
* source: data
+ * message: "Missed the 'data' key"
* @endcode
*
* 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
*
@@ -45,7 +49,8 @@ class SkipRowIfNotSet extends ProcessPluginBase {
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
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']];
}
diff --git a/core/modules/migrate/tests/src/Unit/process/SkipOnEmptyTest.php b/core/modules/migrate/tests/src/Unit/process/SkipOnEmptyTest.php
index 66b27cc..08fbef2 100644
--- a/core/modules/migrate/tests/src/Unit/process/SkipOnEmptyTest.php
+++ b/core/modules/migrate/tests/src/Unit/process/SkipOnEmptyTest.php
@@ -1,6 +1,7 @@
<?php
namespace Drupal\Tests\migrate\Unit\process;
+
use Drupal\migrate\MigrateSkipProcessException;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate\Plugin\migrate\process\SkipOnEmpty;
@@ -53,4 +54,33 @@ class SkipOnEmptyTest extends MigrateProcessTestCase {
$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');
+ }
+
}
diff --git a/core/modules/migrate/tests/src/Unit/process/SkipRowIfNotSetTest.php b/core/modules/migrate/tests/src/Unit/process/SkipRowIfNotSetTest.php
new file mode 100644
index 0000000..26e5b3f
--- /dev/null
+++ b/core/modules/migrate/tests/src/Unit/process/SkipRowIfNotSetTest.php
@@ -0,0 +1,45 @@
+<?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');
+ }
+
+}