Skip to content
Snippets Groups Projects
Commit b7a818b0 authored by Nicolas Tostin's avatar Nicolas Tostin Committed by Youri van Koppen
Browse files

Issue #3170553 by tostinni, MegaChriz, jamesdixon, jrochate, NWOM: Allow...

Issue #3170553 by tostinni, MegaChriz, jamesdixon, jrochate, NWOM: Allow numeric to be tampered in FindReplace & sprintf functions.
parent 9886834f
No related branches found
No related tags found
No related merge requests found
......@@ -96,8 +96,8 @@ class FindReplace extends TamperBase {
* {@inheritdoc}
*/
public function tamper($data, TamperableItemInterface $item = NULL) {
if (!is_string($data)) {
throw new TamperException('Input should be a string.');
if (!is_string($data) && !is_numeric($data)) {
throw new TamperException('Input should be a string or numeric.');
}
$function = $this->getFunction();
......
......@@ -90,8 +90,8 @@ class FindReplaceRegex extends TamperBase {
* {@inheritdoc}
*/
public function tamper($data, TamperableItemInterface $item = NULL) {
if (!is_string($data)) {
throw new TamperException('Input should be a string.');
if (!is_string($data) && !is_numeric($data)) {
throw new TamperException('Input should be a string or numeric.');
}
$find = $this->getSetting(self::SETTING_FIND);
$replace = $this->getSetting(self::SETTING_REPLACE);
......
......@@ -61,8 +61,8 @@ class Sprintf extends TamperBase {
* {@inheritdoc}
*/
public function tamper($data, TamperableItemInterface $item = NULL) {
if (!is_string($data)) {
throw new TamperException('Input should be a string.');
if (!is_string($data) && !is_numeric($data)) {
throw new TamperException('Input should be a string or numeric.');
}
return sprintf($this->getSetting(self::SETTING_TEXT_FORMAT), $data);
}
......
......@@ -89,8 +89,8 @@ class StrPad extends TamperBase {
* {@inheritdoc}
*/
public function tamper($data, TamperableItemInterface $item = NULL) {
if (!is_string($data)) {
throw new TamperException('Input should be a string.');
if (!is_string($data) && !is_numeric($data)) {
throw new TamperException('Input should be a string or numeric.');
}
return str_pad($data, $this->getSetting(self::SETTING_PAD_LENGTH), $this->getSetting(self::SETTING_PAD_STRING), $this->getSetting(self::SETTING_PAD_TYPE));
......
......@@ -45,6 +45,20 @@ class FindReplaceRegexTest extends TamperPluginTestBase {
$this->assertEquals('The dog went to the park.', $plugin->tamper('The Cat went to the park.'));
}
/**
* Test the plugin with a single numeric value.
*/
public function testNumericValue() {
$config = [
FindReplaceRegex::SETTING_FIND => '/5/',
FindReplaceRegex::SETTING_REPLACE => '8',
FindReplaceRegex::SETTING_LIMIT => '',
];
$plugin = new FindReplaceRegex($config, 'find_replace_regex', [], $this->getMockSourceDefinition());
$this->assertEquals('8', $plugin->tamper(5));
$this->assertEquals('7', $plugin->tamper(7));
}
/**
* Test the plugin as respecting word boundaries.
*/
......
......@@ -45,7 +45,7 @@ class FindReplaceTest extends TamperPluginTestBase {
}
/**
* Test the plugin as case sensitve.
* Test the plugin as case sensitive.
*/
public function testSingleValueCaseSensitive() {
$config = [
......@@ -61,6 +61,22 @@ class FindReplaceTest extends TamperPluginTestBase {
$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.
*/
......@@ -101,7 +117,7 @@ class FindReplaceTest extends TamperPluginTestBase {
public function testMultipleValues() {
$plugin = new FindReplace([], 'find_replace', [], $this->getMockSourceDefinition());
$this->expectException(TamperException::class);
$this->expectExceptionMessage('Input should be a string.');
$this->expectExceptionMessage('Input should be a string or numeric.');
$plugin->tamper(['foo', 'bar', 'baz']);
}
......
......@@ -47,7 +47,7 @@ class SprintfTest extends TamperPluginTestBase {
];
$plugin = new Sprintf($config, 'sprintf', [], $this->getMockSourceDefinition());
$this->assertEquals('A', $plugin->tamper('65'));
$this->assertEquals('A', $plugin->tamper(65));
}
}
......@@ -68,6 +68,23 @@ class StrPadTest extends TamperPluginTestBase {
$this->assertEquals('A.000', $plugin->tamper('A.0'));
}
/**
* Test adding leading zeros.
*
* @covers ::tamper
*/
public function testStrPadLeftWithLeadingZeros() {
$config = [
StrPad::SETTING_PAD_LENGTH => '5',
StrPad::SETTING_PAD_STRING => '0',
StrPad::SETTING_PAD_TYPE => STR_PAD_LEFT,
];
$plugin = new StrPad($config, 'StrPad', [], $this->getMockSourceDefinition());
$this->assertEquals('00123', $plugin->tamper(123));
$this->assertEquals('00008', $plugin->tamper(8));
$this->assertEquals('12345', $plugin->tamper(12345));
}
/**
* @covers ::tamper
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment