Skip to content

Commit

Permalink
backport dca field eval
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Feb 5, 2025
1 parent e16cdf0 commit 2508936
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 30 deletions.
12 changes: 6 additions & 6 deletions src/Dca/AuthorFieldConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
class AuthorFieldConfiguration extends DcaFieldConfiguration
{
/** @var string */
protected $type = AuthorField::TYPE_USER;
protected string $type = AuthorField::TYPE_USER;
/** @var string */
protected $fieldNamePrefix = '';
protected string $fieldNamePrefix = '';
/** @var bool */
protected $useDefaultLabel = true;
protected bool $useDefaultLabel = true;
/** @var bool */
protected $exclude = true;
protected bool $exclude = true;
/** @var bool */
protected $search = true;
protected bool $search = true;
/** @var bool */
protected $filter = true;
protected bool $filter = true;

public function setType(string $type): AuthorFieldConfiguration
{
Expand Down
48 changes: 34 additions & 14 deletions src/Dca/DcaFieldConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,20 @@

class DcaFieldConfiguration
{
/**
* @var string
*/
private $table;

/** @var null|int */
private $flag = null;
private string $table;

private ?int $flag = null;

/** @var bool */
protected $exclude = false;
protected bool $exclude = false;

/** @var bool */
protected $search = false;
protected bool $search = false;

/** @var bool */
protected $filter = false;
protected bool $filter = false;

/** @var bool */
protected $sorting = false;
protected bool $sorting = false;

protected array $eval = [];

/**
* @param string $table
Expand Down Expand Up @@ -91,4 +86,29 @@ public function setFilter(bool $filter): DcaFieldConfiguration
$this->filter = $filter;
return $this;
}

/**
* @param string $key
* @param mixed $value
* @return $this
*/
public function setEvalValue(string $key, $value): DcaFieldConfiguration
{
$this->eval[$key] = $value;
return $this;
}

/**
* @param string $key
* @return mixed|null
*/
public function getEvalValue(string $key)
{
return $this->eval[$key] ?? null;
}

public function getEval(): array
{
return $this->eval;
}
}
9 changes: 6 additions & 3 deletions src/EventListener/DcaField/AbstractDcaFieldListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

abstract class AbstractDcaFieldListener implements ServiceSubscriberInterface
{
/** @var ContainerInterface */
protected $container;
protected ContainerInterface $container;

public function __construct(ContainerInterface $container)
{
Expand All @@ -25,7 +24,7 @@ protected function getModelInstance(string $table, int $id): ?Model
return $framework->getAdapter($modelClass)->findByPk($id);
}

protected function applyDefaultFieldAdjustments(array &$field, DcaFieldConfiguration $configuration)
protected function applyDefaultFieldAdjustments(array &$field, DcaFieldConfiguration $configuration): void
{
if ($configuration->isFilter()) {
$field['filter'] = true;
Expand All @@ -46,6 +45,10 @@ protected function applyDefaultFieldAdjustments(array &$field, DcaFieldConfigura
if ($configuration->getFlag() !== null) {
$field['flag'] = $configuration->getFlag();
}

if (!empty($configuration->getEval())) {
$field['eval'] = \array_merge($field['eval'] ?? [], $configuration->getEval());
}
}

public static function getSubscribedServices(): array
Expand Down
39 changes: 38 additions & 1 deletion tests/Dca/DcaFieldOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,46 @@

class DcaFieldOptionsTest extends TestCase
{
public function testGetTable()
public function testAllOptions()
{
$dcaFieldOptions = new DcaFieldConfiguration('test_table');
$this->assertEquals('test_table', $dcaFieldOptions->getTable());

$dcaFieldOptions
->setFlag(69)
->setExclude(true)
->setSearch(true)
->setFilter(true)
->setSorting(true)
->setEvalValue('test_eval_1', 'test_value_1')
->setEvalValue('test_eval_2', 'test_value_2');
$this->assertEquals(69, $dcaFieldOptions->getFlag());
$this->assertTrue($dcaFieldOptions->isExclude());
$this->assertTrue($dcaFieldOptions->isSearch());
$this->assertTrue($dcaFieldOptions->isFilter());
$this->assertTrue($dcaFieldOptions->isSorting());
$this->assertEquals(
[
'test_eval_1' => 'test_value_1',
'test_eval_2' => 'test_value_2',
],
$dcaFieldOptions->getEval()
);

$dcaFieldOptions->setEvalValue('test_eval_2', 'test_value_new2');
$this->assertEquals(
[
'test_eval_1' => 'test_value_1',
'test_eval_2' => 'test_value_new2',
],
$dcaFieldOptions->getEval()
);

$dcaFieldOptions->setEvalValue('test_eval_1', 'test_value_single1');
$dcaFieldOptions->setEvalValue('test_eval_2', 'test_value_single2');
$dcaFieldOptions->setEvalValue('test_eval_3', 'test_value_single3');
$this->assertEquals('test_value_single1', $dcaFieldOptions->getEvalValue('test_eval_1'));
$this->assertEquals('test_value_single2', $dcaFieldOptions->getEvalValue('test_eval_2'));
$this->assertEquals('test_value_single3', $dcaFieldOptions->getEvalValue('test_eval_3'));
}
}
25 changes: 19 additions & 6 deletions tests/EventListener/DcaField/DateAddedFieldListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,22 @@ public function testOnLoadDataContainer()
public function dateAddedConfig():array
{
return [
[null, false, false, false, false],
[1, true, false, false, false],
[2, false, true, false, false],
[null, false, false, true, false],
[null, false, false, false, true],
[null, false, false, false, false, null],
[1, true, false, false, false, []],
[2, false, true, false, false, ['testProp' => 'testValue']],
[null, false, false, true, false, ['a' => 'x', 'b' => 'y', 'c' => 99]],
[null, false, false, false, true, ['noSubmissionField' => true]],
];
}

/**
* @dataProvider dateAddedConfig
* @runInSeparateProcess
*/
public function testConfig(?int $flag, bool $sorting, bool $exclude, bool $filter, bool $search)
public function testConfig(?int $flag, bool $sorting, bool $exclude, bool $filter, bool $search, ?array $eval)
{
$eval ??= [];

$container = $this->createMock(ContainerInterface::class);

$listener = new DateAddedFieldListener($container);
Expand All @@ -76,6 +78,9 @@ public function testConfig(?int $flag, bool $sorting, bool $exclude, bool $filte
$config->setExclude($exclude);
$config->setFilter($filter);
$config->setSearch($search);
foreach ($eval as $key => $value) {
$config->setEvalValue($key, $value);
}

$listener->onLoadDataContainer($table);

Expand All @@ -101,6 +106,14 @@ public function testConfig(?int $flag, bool $sorting, bool $exclude, bool $filte
$this->assertArrayHasKey('search', $field);
$this->assertTrue($field['search']);
}
if (!empty($eval)) {
$this->assertArrayHasKey('eval', $field);
$this->assertIsArray($field['eval']);
foreach ($eval as $key => $value) {
$this->assertArrayHasKey($key, $field['eval']);
$this->assertEquals($value, $field['eval'][$key]);
}
}
}

public function testOnLoadCallback()
Expand Down

0 comments on commit 2508936

Please sign in to comment.