View source  
  <?php
namespace Drupal\Tests\tamper\Unit\Plugin\Tamper;
use Drupal\tamper\Exception\TamperException;
use Drupal\tamper\Plugin\Tamper\FindReplace;
class FindReplaceTest extends TamperPluginTestBase {
  
  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());
  }
  
  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.'));
  }
  
  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.'));
  }
  
  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.'));
  }
  
  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'));
  }
  
  public function testMultipleValues() {
    $plugin = new FindReplace([], 'find_replace', [], $this
      ->getMockSourceDefinition());
    $this
      ->expectException(TamperException::class);
    $this
      ->expectExceptionMessage('Input should be a string.');
    $plugin
      ->tamper([
      'foo',
      'bar',
      'baz',
    ]);
  }
}