tamper-8.x-1.x-dev/tests/src/Unit/Plugin/Tamper/FindReplaceTest.php
tests/src/Unit/Plugin/Tamper/FindReplaceTest.php
<?php
namespace Drupal\Tests\tamper\Unit\Plugin\Tamper;
use Drupal\tamper\Exception\TamperException;
use Drupal\tamper\Plugin\Tamper\FindReplace;
/**
* Tests the find and replace plugin.
*
* @coversDefaultClass \Drupal\tamper\Plugin\Tamper\FindReplace
* @group tamper
*/
class FindReplaceTest extends TamperPluginTestBase {
/**
* {@inheritdoc}
*/
protected function instantiatePlugin() {
$config = [
FindReplace::SETTING_FIND => '',
FindReplace::SETTING_REPLACE => '',
FindReplace::SETTING_CASE_SENSITIVE => FALSE,
FindReplace::SETTING_WORD_BOUNDARIES => FALSE,
FindReplace::SETTING_WHOLE => FALSE,
];
return new FindReplace($config, 'find_replace', [], $this->getMockSourceDefinition());
}
/**
* Test the plugin with a single value.
*/
public function testSingleValue() {
$config = [
FindReplace::SETTING_FIND => 'cat',
FindReplace::SETTING_REPLACE => 'dog',
FindReplace::SETTING_CASE_SENSITIVE => FALSE,
FindReplace::SETTING_WORD_BOUNDARIES => FALSE,
FindReplace::SETTING_WHOLE => FALSE,
];
$plugin = new FindReplace($config, 'find_replace', [], $this->getMockSourceDefinition());
$this->assertEquals('The dog went to the park.', $plugin->tamper('The cat went to the park.'));
$this->assertEquals('The dog went to the park.', $plugin->tamper('The Cat went to the park.'));
$this->assertEquals('The dogwent to the park.', $plugin->tamper('The Catwent to the park.'));
}
/**
* Test the plugin as case sensitive.
*/
public function testSingleValueCaseSensitive() {
$config = [
FindReplace::SETTING_FIND => 'cat',
FindReplace::SETTING_REPLACE => 'dog',
FindReplace::SETTING_CASE_SENSITIVE => TRUE,
FindReplace::SETTING_WORD_BOUNDARIES => FALSE,
FindReplace::SETTING_WHOLE => FALSE,
];
$plugin = new FindReplace($config, 'find_replace', [], $this->getMockSourceDefinition());
$this->assertEquals('The dog went to the park.', $plugin->tamper('The cat went to the park.'));
$this->assertEquals('The Cat went to the park.', $plugin->tamper('The Cat went to the park.'));
$this->assertEquals('The dogwent to the park.', $plugin->tamper('The catwent to the park.'));
}
/**
* Test the plugin with a single numeric value.
*/
public function testNumericValue() {
$config = [
FindReplace::SETTING_FIND => '6',
FindReplace::SETTING_REPLACE => '8',
FindReplace::SETTING_CASE_SENSITIVE => FALSE,
FindReplace::SETTING_WORD_BOUNDARIES => FALSE,
FindReplace::SETTING_WHOLE => FALSE,
];
$plugin = new FindReplace($config, 'find_replace', [], $this->getMockSourceDefinition());
$this->assertEquals('8', $plugin->tamper(6));
$this->assertEquals('7', $plugin->tamper(7));
}
/**
* Test the plugin as respecting word boundaries.
*/
public function testSingleValueWordBoundaries() {
$config = [
FindReplace::SETTING_FIND => 'cat',
FindReplace::SETTING_REPLACE => 'dog',
FindReplace::SETTING_CASE_SENSITIVE => FALSE,
FindReplace::SETTING_WORD_BOUNDARIES => TRUE,
FindReplace::SETTING_WHOLE => FALSE,
];
$plugin = new FindReplace($config, 'find_replace', [], $this->getMockSourceDefinition());
$this->assertEquals('The dog went to the park.', $plugin->tamper('The cat went to the park.'));
$this->assertEquals('The dog went to the park.', $plugin->tamper('The Cat went to the park.'));
$this->assertEquals('The catwent to the park.', $plugin->tamper('The catwent to the park.'));
}
/**
* Test the plugin as replace whole words only.
*/
public function testSingleValueWhole() {
$config = [
FindReplace::SETTING_FIND => 'cat',
FindReplace::SETTING_REPLACE => 'dog',
FindReplace::SETTING_CASE_SENSITIVE => FALSE,
FindReplace::SETTING_WORD_BOUNDARIES => FALSE,
FindReplace::SETTING_WHOLE => TRUE,
];
$plugin = new FindReplace($config, 'find_replace', [], $this->getMockSourceDefinition());
$this->assertEquals('The cat went to the park.', $plugin->tamper('The cat went to the park.'));
$this->assertEquals('dog', $plugin->tamper('cat'));
$this->assertEquals('dog', $plugin->tamper('Cat'));
}
/**
* Test the plugin with a multiple values.
*/
public function testMultipleValues() {
$plugin = new FindReplace([], 'find_replace', [], $this->getMockSourceDefinition());
$this->expectException(TamperException::class);
$this->expectExceptionMessage('Input should be a string or numeric.');
$plugin->tamper(['foo', 'bar', 'baz']);
}
}
