diff --git a/Classes/DataProcessing/CopyToProcessor.php b/Classes/DataProcessing/CopyToProcessor.php index e9eb8cf..a0b41de 100644 --- a/Classes/DataProcessing/CopyToProcessor.php +++ b/Classes/DataProcessing/CopyToProcessor.php @@ -20,18 +20,30 @@ namespace Codappix\SearchCore\DataProcessing; * 02110-1301, USA. */ +use TYPO3\CMS\Core\Utility\GeneralUtility; + /** * Copies values from one field to another one. */ class CopyToProcessor implements ProcessorInterface { + /** + * @var array + */ + protected $keysToCopy = []; + public function processRecord(array $record, array $configuration) { - $all = []; + $result = []; + $this->keysToCopy = array_keys($record); - $this->addArray($all, $record); - $all = array_filter($all); - $record[$configuration['to']] = implode(PHP_EOL, $all); + if (isset($configuration['from'])) { + $this->keysToCopy = GeneralUtility::trimExplode(',', $configuration['from']); + } + + $this->addArray($result, $record); + $result = array_filter($result); + $record[$configuration['to']] = implode(PHP_EOL, $result); return $record; } @@ -43,6 +55,9 @@ class CopyToProcessor implements ProcessorInterface protected function addArray(array &$to, array $from) { foreach ($from as $property => $value) { + if (!in_array($property, $this->keysToCopy)) { + continue; + } if (is_array($value)) { $this->addArray($to, $value); continue; diff --git a/Tests/Unit/DataProcessing/CopyToProcessorTest.php b/Tests/Unit/DataProcessing/CopyToProcessorTest.php new file mode 100644 index 0000000..67c60f4 --- /dev/null +++ b/Tests/Unit/DataProcessing/CopyToProcessorTest.php @@ -0,0 +1,113 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +use Codappix\SearchCore\DataProcessing\CopyToProcessor; +use Codappix\SearchCore\Tests\Unit\AbstractUnitTestCase; + +class CopyToProcessorTest extends AbstractUnitTestCase +{ + /** + * @param array $record + * @param array $configuration + * @param array $expectedRecord + * + * @test + * @dataProvider getTestData + */ + public function recordIsReturnedExpectedtoProvidedConfiguration(array $record, array $configuration, array $expectedRecord) + { + $processor = new CopyToProcessor(); + + $this->assertSame( + $expectedRecord, + $processor->processRecord($record, $configuration), + '' + ); + } + + public function getTestData() + { + return [ + 'copy all' => [ + 'record' => [ + 'field_1' => 'Some content', + 'field_2' => 'Another content', + ], + 'configuration' => [ + 'to' => 'new field', + ], + 'expectedRecord' => [ + 'field_1' => 'Some content', + 'field_2' => 'Another content', + 'new field' => "Some content\nAnother content", + ], + ], + 'copy one' => [ + 'record' => [ + 'field_1' => 'Some content', + 'field_2' => 'Another content', + ], + 'configuration' => [ + 'from' => 'field_1', + 'to' => 'new field', + ], + 'expectedRecord' => [ + 'field_1' => 'Some content', + 'field_2' => 'Another content', + 'new field' => 'Some content', + ], + ], + 'copy some' => [ + 'record' => [ + 'field_1' => 'Some content', + 'field_2' => 'Another content', + 'field_3' => 'Another one', + ], + 'configuration' => [ + 'from' => 'field_1, field_3', + 'to' => 'new field', + ], + 'expectedRecord' => [ + 'field_1' => 'Some content', + 'field_2' => 'Another content', + 'field_3' => 'Another one', + 'new field' => "Some content\nAnother one", + ], + ], + 'configure to copy non existing fields' => [ + 'record' => [ + 'field_1' => 'Some content', + 'field_2' => 'Another content', + ], + 'configuration' => [ + 'from' => 'non_existing, field_1', + 'to' => 'new field', + ], + 'expectedRecord' => [ + 'field_1' => 'Some content', + 'field_2' => 'Another content', + 'new field' => 'Some content', + ], + ], + ]; + } +}