View source
<?php
namespace Drupal\Tests\migrate_plus\Unit\process;
use Drupal\migrate_plus\Plugin\migrate\process\StrReplace;
use Drupal\Tests\migrate\Unit\process\MigrateProcessTestCase;
class StrReplaceTest extends MigrateProcessTestCase {
public function testStrReplace() {
$value = 'vero eos et accusam et justo vero';
$configuration['search'] = 'et';
$configuration['replace'] = 'that';
$plugin = new StrReplace($configuration, 'str_replace', []);
$actual = $plugin
->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
$this
->assertSame('vero eos that accusam that justo vero', $actual);
}
public function testStrIreplace() {
$value = 'VERO eos et accusam et justo vero';
$configuration['search'] = 'vero';
$configuration['replace'] = 'that';
$configuration['case_insensitive'] = TRUE;
$plugin = new StrReplace($configuration, 'str_replace', []);
$actual = $plugin
->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
$this
->assertSame('that eos et accusam et justo that', $actual);
}
public function testPregReplace() {
$value = 'vero eos et 123 accusam et justo 123 duo';
$configuration['search'] = '/[0-9]{3}/';
$configuration['replace'] = 'the';
$configuration['regex'] = TRUE;
$plugin = new StrReplace($configuration, 'str_replace', []);
$actual = $plugin
->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
$this
->assertSame('vero eos et the accusam et justo the duo', $actual);
}
public function testSearchMigrateException() {
$value = 'vero eos et accusam et justo vero';
$configuration['replace'] = 'that';
$plugin = new StrReplace($configuration, 'str_replace', []);
$this
->setExpectedException('\\Drupal\\migrate\\MigrateException', '"search" must be configured.');
$plugin
->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
}
public function testReplaceMigrateException() {
$value = 'vero eos et accusam et justo vero';
$configuration['search'] = 'et';
$plugin = new StrReplace($configuration, 'str_replace', []);
$this
->setExpectedException('\\Drupal\\migrate\\MigrateException', '"replace" must be configured.');
$plugin
->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
}
public function testIsMultiple() {
$value = [
'vero eos et accusam et justo vero',
'et eos vero accusam vero justo et',
];
$expected = [
'vero eos that accusam that justo vero',
'that eos vero accusam vero justo that',
];
$configuration['search'] = 'et';
$configuration['replace'] = 'that';
$plugin = new StrReplace($configuration, 'str_replace', []);
$actual = $plugin
->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
$this
->assertArrayEquals($expected, $actual);
$this
->assertTrue($plugin
->multiple());
}
}