From a8c0c47939999516bc5e4623d8b0e4099f0734d3 Mon Sep 17 00:00:00 2001 From: Sergey Savchenko Date: Fri, 15 Dec 2017 13:02:48 +0200 Subject: [PATCH 1/2] Grid filtration doesn't work for mysql special characters - added escaping for filter input value --- app/code/Magento/Ui/Component/Filters/Type/Input.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Ui/Component/Filters/Type/Input.php b/app/code/Magento/Ui/Component/Filters/Type/Input.php index cbcb33ffcca04..9cc060ae58172 100644 --- a/app/code/Magento/Ui/Component/Filters/Type/Input.php +++ b/app/code/Magento/Ui/Component/Filters/Type/Input.php @@ -65,7 +65,7 @@ public function prepare() protected function applyFilter() { if (isset($this->filterData[$this->getName()])) { - $value = $this->filterData[$this->getName()]; + $value = str_replace(['%', '_'], ['\%', '\_'], $this->filterData[$this->getName()]); if (!empty($value)) { $filter = $this->filterBuilder->setConditionType('like') From 17a817ce5ea5a05bb767964d7d552b9dcfe93e7f Mon Sep 17 00:00:00 2001 From: Sergey Savchenko Date: Fri, 15 Dec 2017 14:42:52 +0200 Subject: [PATCH 2/2] Grid filtration doesn't work for mysql special characters - test has been fixed. - new test-case has been added. --- .../Unit/Component/Filters/Type/InputTest.php | 41 +++++++++++++++---- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/app/code/Magento/Ui/Test/Unit/Component/Filters/Type/InputTest.php b/app/code/Magento/Ui/Test/Unit/Component/Filters/Type/InputTest.php index ed571f44dd6e9..d814fdcd153da 100644 --- a/app/code/Magento/Ui/Test/Unit/Component/Filters/Type/InputTest.php +++ b/app/code/Magento/Ui/Test/Unit/Component/Filters/Type/InputTest.php @@ -111,8 +111,7 @@ public function testPrepare($name, $filterData, $expectedCondition) ->method('addComponentDefinition') ->with(Input::NAME, ['extends' => Input::NAME]); $this->contextMock->expects($this->any()) - ->method('getRequestParam') - ->with(UiContext::FILTER_VAR) + ->method('getFiltersParams') ->willReturn($filterData); $dataProvider = $this->getMockForAbstractClass( \Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface::class, @@ -120,20 +119,41 @@ public function testPrepare($name, $filterData, $expectedCondition) '', false ); + $this->contextMock->expects($this->any()) ->method('getDataProvider') ->willReturn($dataProvider); - if ($expectedCondition !== null) { - $dataProvider->expects($this->any()) - ->method('addFilter') - ->with($expectedCondition, $name); - } $this->uiComponentFactory->expects($this->any()) ->method('create') ->with($name, Input::COMPONENT, ['context' => $this->contextMock]) ->willReturn($uiComponent); + if ($expectedCondition !== null) { + $this->filterBuilderMock->expects($this->once()) + ->method('setConditionType') + ->with('like') + ->willReturnSelf(); + + $this->filterBuilderMock->expects($this->once()) + ->method('setField') + ->with($name) + ->willReturnSelf(); + + $this->filterBuilderMock->expects($this->once()) + ->method('setValue') + ->with($expectedCondition['like']) + ->willReturnSelf(); + + $filterMock = $this->getMockBuilder(\Magento\Framework\Api\Filter::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->filterBuilderMock->expects($this->once()) + ->method('create') + ->willReturn($filterMock); + } + $date = new Input( $this->contextMock, $this->uiComponentFactory, @@ -160,7 +180,12 @@ public function getPrepareDataProvider() [ 'test_date', ['test_date' => 'some_value'], - ['like' => '%some_value%'], + ['like' => '%some\_value%'], + ], + [ + 'test_date', + ['test_date' => '%'], + ['like' => '%\%%'], ], ]; }