Commit cfba66de authored by Nikita Hovratov's avatar Nikita Hovratov Committed by Benni Mack
Browse files

[TASK] Add tests for DataHandler->applyFiltersToValues

This adds missing tests for the TCA "filter" option
for group, select and inline.

Resolves: #96339
Related: #36810
Releases: main, 11.5
Change-Id: I402edf4774f35c3b15aa53eb630d9d098f847e87
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/72585

Tested-by: core-ci's avatarcore-ci <typo3@b13.com>
Tested-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
Reviewed-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
parent 130f3423
......@@ -2095,19 +2095,22 @@ class DataHandler implements LoggerAwareInterface
*/
protected function applyFiltersToValues(array $tcaFieldConfiguration, array $values)
{
if (empty($tcaFieldConfiguration['filter']) || !is_array($tcaFieldConfiguration['filter'])) {
if (!is_array($tcaFieldConfiguration['filter'] ?? null)) {
return $values;
}
foreach ($tcaFieldConfiguration['filter'] as $filter) {
if (empty($filter['userFunc'])) {
continue;
}
$parameters = $filter['parameters'] ?: [];
$parameters = $filter['parameters'] ?? [];
if (!is_array($parameters)) {
$parameters = [];
}
$parameters['values'] = $values;
$parameters['tcaFieldConfig'] = $tcaFieldConfiguration;
$values = GeneralUtility::callUserFunction($filter['userFunc'], $parameters, $this);
if (!is_array($values)) {
throw new \RuntimeException('Failed calling filter userFunc.', 1336051942);
throw new \RuntimeException('Expected userFunc filter "' . $filter['userFunc'] . '" to return an array. Got ' . gettype($values) . '.', 1336051942);
}
}
return $values;
......
......@@ -30,6 +30,7 @@ use TYPO3\CMS\Core\SysLog\Action as SystemLogGenericAction;
use TYPO3\CMS\Core\SysLog\Error as SystemLogErrorClassification;
use TYPO3\CMS\Core\Tests\Unit\DataHandling\Fixtures\AllowAccessHookFixture;
use TYPO3\CMS\Core\Tests\Unit\DataHandling\Fixtures\InvalidHookFixture;
use TYPO3\CMS\Core\Tests\Unit\DataHandling\Fixtures\UserOddNumberFilter;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\StringUtility;
use TYPO3\TestingFramework\Core\AccessibleObjectInterface;
......@@ -1100,4 +1101,72 @@ class DataHandlerTest extends UnitTestCase
$GLOBALS['TCA']['testTable']['ctrl']['prependAtCopy'] = 'testLabel';
self::assertEquals($expected, (new DataHandler())->clearPrefixFromValue('testTable', $input));
}
public function applyFiltersToValuesFiltersValuesDataProvider(): iterable
{
yield 'values are filtered by provided user function' => [
'tcaFieldConfiguration' => [
'filter' => [
[
'userFunc' => UserOddNumberFilter::class . '->filter',
],
],
],
'values' => [1, 2, 3, 4, 5],
'expected' => [1, 3, 5],
];
yield 'parameters are passed to the user function' => [
'tcaFieldConfiguration' => [
'filter' => [
[
'userFunc' => UserOddNumberFilter::class . '->filter',
'parameters' => [
'exclude' => 1,
],
],
],
],
'values' => [1, 2, 3, 4, 5],
'expected' => [3, 5],
];
yield 'no filters return value as is' => [
'tcaFieldConfiguration' => [],
'values' => [1, 2, 3, 4, 5],
'expected' => [1, 2, 3, 4, 5],
];
}
/**
* @dataProvider applyFiltersToValuesFiltersValuesDataProvider
* @test
*/
public function applyFiltersToValuesFiltersValues(array $tcaFieldConfiguration, array $values, array $expected): void
{
self::assertEqualsCanonicalizing($expected, $this->subject->_call('applyFiltersToValues', $tcaFieldConfiguration, $values));
}
/**
* @test
*/
public function applyFiltersToValuesExpectsArray(): void
{
$tcaFieldConfiguration = [
'filter' => [
[
'userFunc' => UserOddNumberFilter::class . '->filter',
'parameters' => [
'break' => true,
],
],
],
];
$values = [1, 2, 3, 4, 5];
$this->expectException(\RuntimeException::class);
$this->expectExceptionCode(1336051942);
$this->expectDeprecationMessage('Expected userFunc filter "TYPO3\CMS\Core\Tests\Unit\DataHandling\Fixtures\UserOddNumberFilter->filter" to return an array. Got NULL.');
$this->subject->_call('applyFiltersToValues', $tcaFieldConfiguration, $values);
}
}
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\Core\Tests\Unit\DataHandling\Fixtures;
use TYPO3\CMS\Core\DataHandling\DataHandler;
class UserOddNumberFilter
{
public function filter(array $parameters, DataHandler $dataHandler)
{
if ($parameters['break'] ?? false) {
return null;
}
$values = $parameters['values'];
$values = array_filter($values, static fn ($number) => $number % 2 !== 0);
if (isset($parameters['exclude'])) {
$values = array_filter($values, static fn ($number) => $number !== $parameters['exclude']);
}
return $values;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment