Skip to content

Commit

Permalink
[TASK] Add tests for DataHandler->applyFiltersToValues
Browse files Browse the repository at this point in the history
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/+/72636
Tested-by: core-ci <typo3@b13.com>
Tested-by: Stefan Bürk <stefan@buerk.tech>
Tested-by: Benni Mack <benni@typo3.org>
Reviewed-by: Stefan Bürk <stefan@buerk.tech>
Reviewed-by: Benni Mack <benni@typo3.org>
  • Loading branch information
nhovratov authored and bmack committed Dec 13, 2021
1 parent 6c6cb05 commit 2460483
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 3 deletions.
9 changes: 6 additions & 3 deletions typo3/sysext/core/Classes/DataHandling/DataHandler.php
Expand Up @@ -2095,19 +2095,22 @@ protected function checkValueForGroupSelect($res, $value, $tcaFieldConf, $table,
*/
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;
Expand Down
69 changes: 69 additions & 0 deletions typo3/sysext/core/Tests/Unit/DataHandling/DataHandlerTest.php
Expand Up @@ -30,6 +30,7 @@
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;
Expand Down Expand Up @@ -1100,4 +1101,72 @@ public function clearPrefixFromValueRemovesPrefix(string $input, string $expecte
$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);
}
}
@@ -0,0 +1,36 @@
<?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;
}
}

0 comments on commit 2460483

Please sign in to comment.