From 589bcb2bb238e0a3a60350f0c069c2a57f290ff6 Mon Sep 17 00:00:00 2001 From: Simon Sprankel Date: Fri, 8 Feb 2019 11:45:11 +0100 Subject: [PATCH 1/3] refactored retrieval of entity ID --- app/code/Magento/Ui/Component/Form.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Ui/Component/Form.php b/app/code/Magento/Ui/Component/Form.php index 4033abba820e0..b560097e490e0 100644 --- a/app/code/Magento/Ui/Component/Form.php +++ b/app/code/Magento/Ui/Component/Form.php @@ -60,7 +60,8 @@ public function getDataSourceData() $dataSource = []; $id = $this->getContext()->getRequestParam($this->getContext()->getDataProvider()->getRequestFieldName(), null); - $filter = $this->filterBuilder->setField($this->getContext()->getDataProvider()->getPrimaryFieldName()) + $idFieldName = $this->getContext()->getDataProvider()->getPrimaryFieldName(); + $filter = $this->filterBuilder->setField($idFieldName) ->setValue($id) ->create(); $this->getContext()->getDataProvider() @@ -74,7 +75,7 @@ public function getDataSourceData() ]; } elseif (isset($data['items'])) { foreach ($data['items'] as $item) { - if ($item[$item['id_field_name']] == $id) { + if ($item[$idFieldName] == $id) { $dataSource = ['data' => ['general' => $item]]; } } From 767c86c9fd55649da52bfc267dea45ed09c38dff Mon Sep 17 00:00:00 2001 From: Simon Sprankel Date: Thu, 21 Feb 2019 09:44:12 +0100 Subject: [PATCH 2/3] implemented UI component form test for usage with AbstractDataProvider Signed-off-by: Simon Sprankel --- .../Ui/Test/Unit/Component/FormTest.php | 62 ++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Ui/Test/Unit/Component/FormTest.php b/app/code/Magento/Ui/Test/Unit/Component/FormTest.php index 6951583291df9..6df69c7d0e48d 100644 --- a/app/code/Magento/Ui/Test/Unit/Component/FormTest.php +++ b/app/code/Magento/Ui/Test/Unit/Component/FormTest.php @@ -9,7 +9,6 @@ use Magento\Framework\Api\FilterBuilder; use Magento\Framework\View\Element\UiComponent\ContextInterface; use Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface; -use Magento\Framework\View\Element\UiComponent\Processor; use Magento\Ui\Component\Form; class FormTest extends \PHPUnit\Framework\TestCase @@ -215,4 +214,65 @@ public function testGetDataSourceDataWithoutId() $this->assertEquals($dataSource, $this->model->getDataSourceData()); } + + public function testGetDataSourceDataWithAbstractDataProvider() + { + $requestFieldName = 'request_id'; + $primaryFieldName = 'primary_id'; + $fieldId = 44; + $row = ['key' => 'value', $primaryFieldName => $fieldId]; + $data = [ + 'items' => [$row], + ]; + $dataSource = [ + 'data' => [ + 'general' => $row + ], + ]; + + /** @var DataProviderInterface|\PHPUnit_Framework_MockObject_MockObject $dataProviderMock */ + $dataProviderMock = + $this->getMockBuilder(\Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface::class) + ->getMock(); + $dataProviderMock->expects($this->once()) + ->method('getRequestFieldName') + ->willReturn($requestFieldName); + $dataProviderMock->expects($this->once()) + ->method('getPrimaryFieldName') + ->willReturn($primaryFieldName); + + $this->contextMock->expects($this->any()) + ->method('getDataProvider') + ->willReturn($dataProviderMock); + $this->contextMock->expects($this->once()) + ->method('getRequestParam') + ->with($requestFieldName) + ->willReturn($fieldId); + + /** @var Filter|\PHPUnit_Framework_MockObject_MockObject $filterMock */ + $filterMock = $this->getMockBuilder(\Magento\Framework\Api\Filter::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->filterBuilderMock->expects($this->once()) + ->method('setField') + ->with($primaryFieldName) + ->willReturnSelf(); + $this->filterBuilderMock->expects($this->once()) + ->method('setValue') + ->with($fieldId) + ->willReturnSelf(); + $this->filterBuilderMock->expects($this->once()) + ->method('create') + ->willReturn($filterMock); + + $dataProviderMock->expects($this->once()) + ->method('addFilter') + ->with($filterMock); + $dataProviderMock->expects($this->once()) + ->method('getData') + ->willReturn($data); + + $this->assertEquals($dataSource, $this->model->getDataSourceData()); + } } From 0c7218b9889af1b11bba56fb8e0e0b6958ea4409 Mon Sep 17 00:00:00 2001 From: Oleksandr Kravchuk Date: Fri, 22 Feb 2019 10:43:48 +0200 Subject: [PATCH 3/3] Fix static tests --- app/code/Magento/Ui/Component/Form.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Ui/Component/Form.php b/app/code/Magento/Ui/Component/Form.php index b560097e490e0..dc6e7b5ca06ab 100644 --- a/app/code/Magento/Ui/Component/Form.php +++ b/app/code/Magento/Ui/Component/Form.php @@ -10,6 +10,7 @@ use Magento\Framework\View\Element\UiComponentInterface; /** + * Ui component Form * @api * @since 100.0.2 */ @@ -53,7 +54,7 @@ public function getComponentName() } /** - * {@inheritdoc} + * @inheritdoc */ public function getDataSourceData() {