View source
<?php
namespace Drupal\Tests\tamper\Unit\Plugin\Tamper;
use Drupal\tamper\Plugin\Tamper\FindReplaceRegex;
class FindReplaceRegexTest extends TamperPluginTestBase {
protected function instantiatePlugin() {
return new FindReplaceRegex([], 'find_replace_regex', [], $this
->getMockSourceDefinition());
}
public function testSingleValue() {
$config = [
FindReplaceRegex::SETTING_FIND => '/cat/',
FindReplaceRegex::SETTING_REPLACE => 'dog',
FindReplaceRegex::SETTING_LIMIT => '',
];
$plugin = new FindReplaceRegex($config, 'find_replace_regex', [], $this
->getMockSourceDefinition());
$this
->assertEquals('The dog went to the park.', $plugin
->tamper('The cat went to the park.'));
}
public function testSingleValueCaseSensitive() {
$config = [
FindReplaceRegex::SETTING_FIND => '/cat/i',
FindReplaceRegex::SETTING_REPLACE => 'dog',
FindReplaceRegex::SETTING_LIMIT => '',
];
$plugin = new FindReplaceRegex($config, 'find_replace_regex', [], $this
->getMockSourceDefinition());
$this
->assertEquals('The dog went to the park.', $plugin
->tamper('The Cat went to the park.'));
}
public function testSingleValueWordBoundaries() {
$config = [
FindReplaceRegex::SETTING_FIND => '/cat\\b/i',
FindReplaceRegex::SETTING_REPLACE => 'dog',
FindReplaceRegex::SETTING_LIMIT => '',
];
$plugin = new FindReplaceRegex($config, 'find_replace_regex', [], $this
->getMockSourceDefinition());
$this
->assertEquals('The Catwent to the park.', $plugin
->tamper('The Catwent to the park.'));
}
public function testSingleValueLineBreak() {
$config = [
FindReplaceRegex::SETTING_FIND => '/cat\\n/',
FindReplaceRegex::SETTING_REPLACE => 'dog',
FindReplaceRegex::SETTING_LIMIT => '',
];
$plugin = new FindReplaceRegex($config, 'find_replace_regex', [], $this
->getMockSourceDefinition());
$this
->assertEquals('The dog went to the park.', $plugin
->tamper("The cat\n went to the park."));
$config = [
FindReplaceRegex::SETTING_FIND => '/cat\\r\\n/',
FindReplaceRegex::SETTING_REPLACE => 'dog',
FindReplaceRegex::SETTING_LIMIT => '',
];
$plugin = new FindReplaceRegex($config, 'find_replace_regex', [], $this
->getMockSourceDefinition());
$this
->assertEquals('The dog went to the park.', $plugin
->tamper("The cat\r\n went to the park."));
}
public function testSingleValueWhiteSpace() {
$config = [
FindReplaceRegex::SETTING_FIND => '/cat\\s/',
FindReplaceRegex::SETTING_REPLACE => 'dog',
FindReplaceRegex::SETTING_LIMIT => '',
];
$plugin = new FindReplaceRegex($config, 'find_replace_regex', [], $this
->getMockSourceDefinition());
$this
->assertEquals('The dog went to the park.', $plugin
->tamper("The cat\n went to the park."));
$config = [
FindReplaceRegex::SETTING_FIND => '/cat\\t/',
FindReplaceRegex::SETTING_REPLACE => 'dog',
FindReplaceRegex::SETTING_LIMIT => '',
];
$plugin = new FindReplaceRegex($config, 'find_replace_regex', [], $this
->getMockSourceDefinition());
$this
->assertEquals('The dog went to the park.', $plugin
->tamper("The cat\t went to the park."));
}
}