Skip to content
Snippets Groups Projects
Commit e612db7c authored by Vasyl Kydyba's avatar Vasyl Kydyba
Browse files

Merge remote-tracking branch 'origin/8.x-5.x' into 3015199-allow-skiponvalue-to

parents 81fa445d 4fb94a8f
No related branches found
No related tags found
1 merge request!33Issue #3015199: Allow SkipOnValue to include a message in the MigrateSkipRowException.
......@@ -151,7 +151,7 @@ class EntityValue extends ProcessPluginBase implements ContainerFactoryPluginInt
}
else {
if ($langcode) {
throw new \InvalidArgumentException('Langcode can only be used with content entities currently.');
throw new MigrateException('Langcode can only be used with content entities currently.');
}
}
try {
......
......@@ -85,20 +85,20 @@ class Gate extends ProcessPluginBase {
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if (empty($this->configuration['valid_keys']) && !array_key_exists('valid_keys', $this->configuration)) {
if (!array_key_exists('valid_keys', $this->configuration)) {
throw new MigrateException('Gate plugin is missing valid_keys configuration.');
}
if (empty($this->configuration['use_as_key']) && !array_key_exists('use_as_key', $this->configuration)) {
if (!array_key_exists('use_as_key', $this->configuration)) {
throw new MigrateException('Gate plugin is missing use_as_key configuration.');
}
if (empty($this->configuration['key_direction']) && !array_key_exists('key_direction', $this->configuration)) {
if (!array_key_exists('key_direction', $this->configuration)) {
throw new MigrateException('Gate plugin is missing key_direction configuration.');
}
if (!in_array($this->configuration['key_direction'], ['lock', 'unlock'], TRUE)) {
throw new MigrateException('Gate plugin only accepts the following values for key_direction: lock and unlock.');
}
$valid_keys = is_array($this->configuration['valid_keys']) ? $this->configuration['valid_keys'] : [$this->configuration['valid_keys']];
$valid_keys = (array) $this->configuration['valid_keys'];
$key = $row->get($this->configuration['use_as_key']);
$key_is_valid = in_array($key, $valid_keys, TRUE);
$key_direction = $this->configuration['key_direction'];
......
......@@ -66,11 +66,17 @@ 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.');
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
if (empty($configuration['value']) && !array_key_exists('value', $configuration)) {
throw new \InvalidArgumentException('Skip on value plugin is missing value configuration.');
}
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public function row($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
$message = !empty($this->configuration['message']) ? $this->configuration['message'] : '';
if (is_array($this->configuration['value'])) {
......@@ -96,10 +102,6 @@ class SkipOnValue extends ProcessPluginBase {
* {@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'])) {
$value_in_array = FALSE;
$not_equals = isset($this->configuration['not_equals']);
......
......@@ -81,13 +81,21 @@ class StrReplace extends ProcessPluginBase {
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if (!isset($this->configuration['search'])) {
throw new MigrateException('"search" must be configured.');
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
if (!isset($configuration['search'])) {
throw new \InvalidArgumentException('The "search" must be set.');
}
if (!isset($this->configuration['replace'])) {
throw new MigrateException('"replace" must be configured.');
if (!isset($configuration['replace'])) {
throw new \InvalidArgumentException('The "replace" must be set.');
}
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
$this->multiple = is_array($value);
$this->configuration += [
'case_insensitive' => FALSE,
......
......@@ -3,10 +3,12 @@
namespace Drupal\Tests\migrate_plus\Kernel\Plugin\migrate\process;
use Drupal\KernelTests\KernelTestBase;
use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;
use Drupal\node\Entity\Node;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\node\Entity\NodeType;
/**
* Tests the entity_value plugin.
......@@ -150,6 +152,31 @@ class EntityValueTest extends KernelTestBase {
$this->assertTrue($this->plugin->multiple());
}
/**
* Test the EntityLoad plugin failure.
*
* @covers ::transform
*/
public function testEntityValueLangException() {
$config_entity = NodeType::create(['type' => 'page', 'name' => 'page']);
$config_entity->save();
$this->plugin = \Drupal::service('plugin.manager.migrate.process')
->createInstance('entity_value', [
'entity_type' => 'node_type',
'langcode' => 'es',
'field_name' => 'type',
]);
$executable = $this->prophesize(MigrateExecutableInterface::class)
->reveal();
$row = new Row();
// Ensure that the entity is returned if it really exists.
$this->expectException(MigrateException::class);
$this->expectExceptionMessage('Langcode can only be used with content entities currently.');
$this->plugin->transform([$config_entity->id()], $executable, $row, 'dummmy');
}
/**
* Test the EntityLoad plugin throwing.
*
......@@ -161,7 +188,7 @@ class EntityValueTest extends KernelTestBase {
*/
public function testEntityValueConfig($config) {
$this->expectException(\InvalidArgumentException::class);
$plugin = \Drupal::service('plugin.manager.migrate.process')
\Drupal::service('plugin.manager.migrate.process')
->createInstance('entity_value', $config);
}
......
......@@ -140,23 +140,14 @@ class SkipOnValueTest extends MigrateProcessTestCase {
}
/**
* @covers ::row
* @covers ::__construct
*/
public function testRequiredRowConfiguration(): void {
public function testRequiredConfiguration() {
// It doesn't meter which method we will put here, because it should throw
// error on contraction of Plugin.
$configuration['method'] = 'row';
$this->expectException(MigrateException::class);
(new SkipOnValue($configuration, 'skip_on_value', []))
->transform('sourcevalue', $this->migrateExecutable, $this->row, 'destinationproperty');
}
/**
* @covers ::process
*/
public function testRequiredProcessConfiguration(): void {
$configuration['method'] = 'process';
$this->expectException(MigrateException::class);
(new SkipOnValue($configuration, 'skip_on_value', []))
->transform('sourcevalue', $this->migrateExecutable, $this->row, 'destinationproperty');
$this->expectException(\InvalidArgumentException::class);
(new SkipOnValue($configuration, 'skip_on_value', []));
}
}
......@@ -55,27 +55,23 @@ class StrReplaceTest extends MigrateProcessTestCase {
}
/**
* Test for MigrateException for "search" configuration.
* Test for InvalidArgumentException for "search" configuration.
*/
public function testSearchMigrateException(): void {
$value = 'vero eos et accusam et justo vero';
public function testSearchInvalidArgumentException(): void {
$configuration['replace'] = 'that';
$plugin = new StrReplace($configuration, 'str_replace', []);
$this->expectException(MigrateException::class);
$this->expectExceptionMessage('"search" must be configured.');
$plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The "search" must be set.');
new StrReplace($configuration, 'str_replace', []);
}
/**
* Test for MigrateException for "replace" configuration.
* Test for InvalidArgumentException for "replace" configuration.
*/
public function testReplaceMigrateException(): void {
$value = 'vero eos et accusam et justo vero';
public function testReplaceInvalidArgumentException(): void {
$configuration['search'] = 'et';
$plugin = new StrReplace($configuration, 'str_replace', []);
$this->expectException(MigrateException::class);
$this->expectExceptionMessage('"replace" must be configured.');
$plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The "replace" must be set.');
new StrReplace($configuration, 'str_replace', []);
}
/**
......
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