Skip to content

Commit 226bcb6

Browse files
committed
Filter processing should not ask for values of whole form
1 parent 0574c71 commit 226bcb6

File tree

4 files changed

+68
-13
lines changed

4 files changed

+68
-13
lines changed

src/DataGrid.php

+8-11
Original file line numberDiff line numberDiff line change
@@ -1498,11 +1498,6 @@ public function filterSucceeded(NetteForm $form): void
14981498
return;
14991499
}
15001500

1501-
$values = (array) $form->getUnsafeValues(null, [
1502-
$form['perPage'],
1503-
$form['filter'],
1504-
]);
1505-
15061501
if ($this->getPresenterInstance()->isAjax()) {
15071502
if (isset($form['group_action']['submit']) && $form['group_action']['submit']->isSubmittedBy()) {
15081503
return;
@@ -1512,9 +1507,11 @@ public function filterSucceeded(NetteForm $form): void
15121507
/**
15131508
* Per page
15141509
*/
1515-
if (isset($values['perPage'])) {
1516-
$this->saveSessionData('_grid_perPage', $values['perPage']);
1517-
$this->perPage = $values['perPage'];
1510+
$perPage = $form['perPage']->getValue();
1511+
1512+
if (isset($perPage)) {
1513+
$this->saveSessionData('_grid_perPage', $perPage);
1514+
$this->perPage = $perPage;
15181515
}
15191516

15201517
/**
@@ -1541,7 +1538,7 @@ public function filterSucceeded(NetteForm $form): void
15411538
$primaryWhereColumn = $form->getHttpData(Form::DATA_LINE, 'inline_edit[_primary_where_column]');
15421539

15431540
if ($edit['submit']->isSubmittedBy() && $edit->getErrors() === []) {
1544-
$this->inlineEdit->onSubmit($id, $form['inline_edit']->getUnsafeValues(null));
1541+
$this->inlineEdit->onSubmit($id, $form['inline_edit']->getValues());
15451542
$this->getPresenterInstance()->payload->_datagrid_inline_edited = $id;
15461543
$this->getPresenterInstance()->payload->_datagrid_name = $this->getFullName();
15471544
} else {
@@ -1583,7 +1580,7 @@ public function filterSucceeded(NetteForm $form): void
15831580

15841581
if ($add['submit']->isSubmittedBy() || $add['cancel']->isSubmittedBy()) {
15851582
if ($add['submit']->isSubmittedBy() && $add->getErrors() === []) {
1586-
$this->inlineAdd->onSubmit($form['inline_add']->getUnsafeValues(null));
1583+
$this->inlineAdd->onSubmit($form['inline_add']->getValues());
15871584
}
15881585

15891586
$this->redrawControl('tbody');
@@ -1597,7 +1594,7 @@ public function filterSucceeded(NetteForm $form): void
15971594
/**
15981595
* Filter itself
15991596
*/
1600-
$values = $values['filter'];
1597+
$values = $form['filter']->getValues();;
16011598

16021599
if (!$values instanceof ArrayHash) {
16031600
throw new \UnexpectedValueException;

tests/Cases/FilterTest.phpt

+35
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ namespace Ublaboo\DataGrid\Tests\Cases;
77
require __DIR__ . '/../bootstrap.php';
88

99
use Nette\Application\AbortException;
10+
use Nette\Forms\Container;
1011
use Tester\Assert;
1112
use Tester\TestCase;
1213
use Ublaboo\DataGrid\DataGrid;
14+
use Ublaboo\DataGrid\Tests\Files\FormValueObject;
1315
use Ublaboo\DataGrid\Tests\Files\TestingDataGridFactoryRouter;
1416

1517
final class FilterTest extends TestCase
@@ -27,6 +29,39 @@ final class FilterTest extends TestCase
2729
}, AbortException::class);
2830
}
2931

32+
33+
/**
34+
* This case is testing grid filter processing to not cause side effects by unnecessarily instantiating
35+
* value object {@see FormValueObject} of inline add form container. This value object is crafted
36+
* to fail on constructor argument type check due to inline add form container not being validated in this case.
37+
*/
38+
public function testFilterSubmitWithInvalidInlineAddOpen(): void
39+
{
40+
$factory = new TestingDataGridFactoryRouter();
41+
/** @var DataGrid $grid */
42+
$grid = $factory->createTestingDataGrid()->getComponent('grid');
43+
44+
$grid->addColumnText('status', 'Status');
45+
46+
$grid->addInlineAdd()->onControlAdd[] = function (Container $container) {
47+
$container->setMappedType(FormValueObject::class);
48+
$container->addSelect('status', '', [
49+
// items are irrelevant, case is testing control returning null value
50+
1 => 'Concept',
51+
2 => 'Active',
52+
3 => 'Unpublished',
53+
])
54+
->setPrompt('---')
55+
->setRequired();
56+
};
57+
58+
$filterForm = $grid->createComponentFilter();
59+
60+
Assert::exception(function() use ($grid, $filterForm): void {
61+
$grid->filterSucceeded($filterForm);
62+
}, AbortException::class);
63+
}
64+
3065
}
3166

3267
(new FilterTest)->run();

tests/Files/FormValueObject.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Ublaboo\DataGrid\Tests\Files;
5+
6+
class FormValueObject
7+
{
8+
9+
private $status;
10+
11+
12+
public function __construct(int $status)
13+
{
14+
$this->status = $status;
15+
}
16+
17+
18+
public function getStatus(): int
19+
{
20+
return $this->status;
21+
}
22+
23+
}

tests/Files/TestingDataGridFactoryRouter.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616
class TestingDataGridFactoryRouter
1717
{
1818

19-
public function createTestingDataGrid(): ?IComponent
19+
public function createTestingDataGrid(?array $post = null): ?IComponent
2020
{
2121
$presenterFactory = new PresenterFactory();
2222
$presenterFactory->setMapping(['*' => 'Ublaboo\DataGrid\Tests\Files\*Presenter']);
2323

2424
$presenter = $presenterFactory->createPresenter('Test');
2525

2626
$url = new UrlScript('http://localhost/index.php');
27-
$request = new HttpRequest($url);
27+
$request = new HttpRequest($url, $post);
2828
$response = new Response();
2929
$session = new Session($request, $response);
3030

0 commit comments

Comments
 (0)