diff --git a/src/Plugin/migrate/process/StrReplace.php b/src/Plugin/migrate/process/StrReplace.php index 08dece79fd1471e3beb3b4f152be478e71067b1e..b8a5cd04848c585c0e69e58df46b873eca013605 100644 --- a/src/Plugin/migrate/process/StrReplace.php +++ b/src/Plugin/migrate/process/StrReplace.php @@ -125,6 +125,14 @@ class StrReplace extends ProcessPluginBase { if ($this->configuration['regex']) { $function = 'preg_replace'; } + if($this->multiple) { + foreach($value as $key => $item) { + $item = (string) $item; + $value[$key] = $function($this->configuration['search'], $this->configuration['replace'], $item); + } + return $value; + } + $value = (string) $value; return $function($this->configuration['search'], $this->configuration['replace'], $value); } diff --git a/tests/src/Unit/process/StrReplaceTest.php b/tests/src/Unit/process/StrReplaceTest.php index 320c4c9ffb6e98ae53ca73316cd8e471138c5c0d..51825686db928c1b9b808e4bddc7354cc5577b49 100644 --- a/tests/src/Unit/process/StrReplaceTest.php +++ b/tests/src/Unit/process/StrReplaceTest.php @@ -27,10 +27,68 @@ final class StrReplaceTest extends MigrateProcessTestCase { $actual = $plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty'); $this->assertSame('vero eos that accusam that justo vero', $actual); + } + + /** + * Test for a simple str_replace given NULL. + */ + public function testStrReplaceNull(): void { + $configuration = []; + $value = NULL; + $configuration['search'] = ''; + $configuration['replace'] = 'that'; + $plugin = new StrReplace($configuration, 'str_replace', []); + $actual = $plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty'); + $this->assertSame('', $actual); + + } + + /** + * Test for a simple str_replace given int 1. + */ + public function testStrReplaceInt(): void { + $configuration = []; + $value = 1; + $configuration['search'] = '1'; + $configuration['replace'] = 'that'; + $plugin = new StrReplace($configuration, 'str_replace', []); + $actual = $plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty'); + $this->assertSame('that', $actual); + + } + + + /** + * Test for a simple str_replace given TRUE. + */ + public function testStrReplaceTrue(): void { + $configuration = []; + $value = TRUE; + $configuration['search'] = '1'; + $configuration['replace'] = 'that'; + $plugin = new StrReplace($configuration, 'str_replace', []); + $actual = $plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty'); + $this->assertSame('that', $actual); + + } + + + /** + * Test for a simple str_replace given FALSE. + */ + public function testStrReplaceFalse(): void { + $configuration = []; + $value = FALSE; + $configuration['search'] = ''; + $configuration['replace'] = 'that'; + $plugin = new StrReplace($configuration, 'str_replace', []); + $actual = $plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty'); + $this->assertSame('', $actual); + } /** - * Test for case insensitive searches. + * Test for case-insensitive searches. */ public function testStrIreplace(): void { $configuration = [];