3 namespace Drupal\Tests\migrate_plus\Unit\process;
5 use Drupal\migrate_plus\Plugin\migrate\process\StrReplace;
6 use Drupal\Tests\migrate\Unit\process\MigrateProcessTestCase;
9 * Tests the str replace process plugin.
12 * @coversDefaultClass \Drupal\migrate_plus\Plugin\migrate\process\StrReplace
14 class StrReplaceTest extends MigrateProcessTestCase {
17 * Test for a simple str_replace string.
19 public function testStrReplace() {
20 $value = 'vero eos et accusam et justo vero';
21 $configuration['search'] = 'et';
22 $configuration['replace'] = 'that';
23 $plugin = new StrReplace($configuration, 'str_replace', []);
24 $actual = $plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
25 $this->assertSame('vero eos that accusam that justo vero', $actual);
30 * Test for case insensitive searches.
32 public function testStrIreplace() {
33 $value = 'VERO eos et accusam et justo vero';
34 $configuration['search'] = 'vero';
35 $configuration['replace'] = 'that';
36 $configuration['case_insensitive'] = TRUE;
37 $plugin = new StrReplace($configuration, 'str_replace', []);
38 $actual = $plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
39 $this->assertSame('that eos et accusam et justo that', $actual);
44 * Test for regular expressions.
46 public function testPregReplace() {
47 $value = 'vero eos et 123 accusam et justo 123 duo';
48 $configuration['search'] = '/[0-9]{3}/';
49 $configuration['replace'] = 'the';
50 $configuration['regex'] = TRUE;
51 $plugin = new StrReplace($configuration, 'str_replace', []);
52 $actual = $plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
53 $this->assertSame('vero eos et the accusam et justo the duo', $actual);
57 * Test for MigrateException for "search" configuration.
59 public function testSearchMigrateException() {
60 $value = 'vero eos et accusam et justo vero';
61 $configuration['replace'] = 'that';
62 $plugin = new StrReplace($configuration, 'str_replace', []);
63 $this->setExpectedException('\Drupal\migrate\MigrateException', '"search" must be configured.');
64 $plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
68 * Test for MigrateException for "replace" configuration.
70 public function testReplaceMigrateException() {
71 $value = 'vero eos et accusam et justo vero';
72 $configuration['search'] = 'et';
73 $plugin = new StrReplace($configuration, 'str_replace', []);
74 $this->setExpectedException('\Drupal\migrate\MigrateException', '"replace" must be configured.');
75 $plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
81 public function testIsMultiple() {
83 'vero eos et accusam et justo vero',
84 'et eos vero accusam vero justo et',
88 'vero eos that accusam that justo vero',
89 'that eos vero accusam vero justo that',
91 $configuration['search'] = 'et';
92 $configuration['replace'] = 'that';
93 $plugin = new StrReplace($configuration, 'str_replace', []);
94 $actual = $plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
95 $this->assertArrayEquals($expected, $actual);
97 $this->assertTrue($plugin->multiple());