From 156ac9c80b79fe3eac45ecbccaa578b9318b211f Mon Sep 17 00:00:00 2001 From: Yaroslav Rogoza Date: Sun, 3 Nov 2019 14:37:49 +0100 Subject: [PATCH 1/4] Concept of showing error message in the grid's body --- AdobeStockAsset/Model/GetAssetList.php | 2 +- .../Model/Listing/DataProvider.php | 52 +++++++++++++++++-- .../adminhtml/web/css/source/_module.less | 3 +- .../web/js/components/grid/masonry.js | 15 +++++- .../adminhtml/web/template/grid/masonry.html | 5 +- 5 files changed, 69 insertions(+), 8 deletions(-) diff --git a/AdobeStockAsset/Model/GetAssetList.php b/AdobeStockAsset/Model/GetAssetList.php index 248472442eca..9c1e09c91ac2 100644 --- a/AdobeStockAsset/Model/GetAssetList.php +++ b/AdobeStockAsset/Model/GetAssetList.php @@ -74,7 +74,7 @@ public function execute(SearchCriteriaInterface $searchCriteria): SearchResultIn } catch (AuthenticationException $exception) { throw new LocalizedException( __( - 'Failed to authenticate to Adobe Stock API. Please correct the API credentials in ' + 'Failed to authenticate to Adobe Stock API.
Please correct the API credentials in ' . 'Configuration → System → Adobe Stock Integration.', $this->url->getUrl('adminhtml/system_config/edit/section/system') ) diff --git a/AdobeStockImageAdminUi/Model/Listing/DataProvider.php b/AdobeStockImageAdminUi/Model/Listing/DataProvider.php index c6180e12bcc7..4c832b75d80c 100644 --- a/AdobeStockImageAdminUi/Model/Listing/DataProvider.php +++ b/AdobeStockImageAdminUi/Model/Listing/DataProvider.php @@ -13,6 +13,7 @@ use Magento\Framework\Api\Search\SearchCriteriaBuilder; use Magento\Framework\App\RequestInterface; use Magento\AdobeStockImageApi\Api\GetImageListInterface; +use Magento\Framework\Api\Search\SearchResultInterfaceFactory; use Magento\Framework\Exception\LocalizedException; /** @@ -20,11 +21,21 @@ */ class DataProvider extends \Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider { + /** + * @var SearchResultInterfaceFactory + */ + private $searchResultFactory; + /** * @var GetImageListInterface */ private $getImageList; + /** + * @var string|null + */ + private $errorMessage; + /** * DataProvider constructor. * @param string $name @@ -47,6 +58,7 @@ public function __construct( RequestInterface $request, FilterBuilder $filterBuilder, GetImageListInterface $getImageList, + SearchResultInterfaceFactory $searchResultFactory, array $meta = [], array $data = [] ) { @@ -61,16 +73,50 @@ public function __construct( $meta, $data ); + $this->searchResultFactory = $searchResultFactory; $this->getImageList = $getImageList; } + /** + * @return string|null + */ + public function getErrorMessage(): ?string + { + return $this->errorMessage; + } + + /** + * @param string $message + */ + public function setErrorMessage(string $message): void + { + $this->errorMessage = $message; + } + + /** + * @inheritDoc + */ + public function getData() + { + $recordsData = $this->searchResultToOutput($this->getSearchResult()); + $errorMessage = ['errorMessage' => $this->getErrorMessage()]; + + return array_merge($recordsData, $errorMessage); + } + /** * @inheritdoc - * - * @throws LocalizedException */ public function getSearchResult() { - return $this->getImageList->execute($this->getSearchCriteria()); + try { + $searchResult = $this->getImageList->execute($this->getSearchCriteria()); + } catch (LocalizedException $exception) { + $this->setErrorMessage($exception->getMessage()); + $searchResult = $this->searchResultFactory->create(); + $searchResult->setItems([]); + } + + return $searchResult; } } diff --git a/AdobeUi/view/adminhtml/web/css/source/_module.less b/AdobeUi/view/adminhtml/web/css/source/_module.less index 400fb4fe96f5..3bdeebb5856d 100644 --- a/AdobeUi/view/adminhtml/web/css/source/_module.less +++ b/AdobeUi/view/adminhtml/web/css/source/_module.less @@ -11,7 +11,8 @@ margin: @admin__masonry_grid_image__space/2 -(@admin__masonry_grid_image__space/2); overflow: hidden; - .no-data-message-container { + .no-data-message-container, + .error-message-container { font-size: @data-grid__no-records__font-size; padding: @data-grid__no-records__padding; text-align: center; diff --git a/AdobeUi/view/adminhtml/web/js/components/grid/masonry.js b/AdobeUi/view/adminhtml/web/js/components/grid/masonry.js index 3ab91481837f..a113935ed9e7 100644 --- a/AdobeUi/view/adminhtml/web/js/components/grid/masonry.js +++ b/AdobeUi/view/adminhtml/web/js/components/grid/masonry.js @@ -13,7 +13,8 @@ define([ defaults: { template: 'Magento_AdobeUi/grid/masonry', imports: { - rows: '${ $.provider }:data.items' + rows: '${ $.provider }:data.items', + errorMessage: '${ $.provider }:data.errorMessage' }, listens: { 'rows': 'initComponent' @@ -57,7 +58,8 @@ define([ initObservable: function () { this._super() .observe([ - 'rows' + 'rows', + 'errorMessage' ]); return this; @@ -226,6 +228,15 @@ define([ */ hasData: function () { return !!this.rows() && !!this.rows().length; + }, + + /** + * Returns error message returned by the data provider + * + * @returns {String|null} + */ + getErrorMessage: function () { + return this.errorMessage() } }); }); diff --git a/AdobeUi/view/adminhtml/web/template/grid/masonry.html b/AdobeUi/view/adminhtml/web/template/grid/masonry.html index 9510495b4e9a..fdf13f777a7c 100644 --- a/AdobeUi/view/adminhtml/web/template/grid/masonry.html +++ b/AdobeUi/view/adminhtml/web/template/grid/masonry.html @@ -8,7 +8,10 @@
-
+
+
+ +
From 0a30147e517cda1aa3beeed8c270174feb197226 Mon Sep 17 00:00:00 2001 From: Sergii Ivashchenko Date: Mon, 4 Nov 2019 13:53:05 +0000 Subject: [PATCH 2/4] magento/adobe-stock-integration#629: Unit test coverage and fixed static tests --- .../Model/Listing/DataProvider.php | 46 +++++-------------- ...MediaGalleryForPageNoEditorActionGroup.xml | 2 +- .../Mftf/Suite/adobe-stock-integration.xml | 4 +- .../Unit/Model/Listing/DataProviderTest.php | 28 +++++++++++ .../web/js/components/grid/masonry.js | 2 +- 5 files changed, 43 insertions(+), 39 deletions(-) diff --git a/AdobeStockImageAdminUi/Model/Listing/DataProvider.php b/AdobeStockImageAdminUi/Model/Listing/DataProvider.php index 68ea28824dfc..6b569217b0ee 100644 --- a/AdobeStockImageAdminUi/Model/Listing/DataProvider.php +++ b/AdobeStockImageAdminUi/Model/Listing/DataProvider.php @@ -32,11 +32,6 @@ class DataProvider extends \Magento\Framework\View\Element\UiComponent\DataProvi */ private $getImageList; - /** - * @var string|null - */ - private $errorMessage; - /** * DataProvider constructor. * @param string $name @@ -79,30 +74,19 @@ public function __construct( } /** - * @return string|null - */ - public function getErrorMessage(): ?string - { - return $this->errorMessage; - } - - /** - * @param string $message - */ - public function setErrorMessage(string $message): void - { - $this->errorMessage = $message; - } - - /** - * @inheritDoc + * @inheritdoc */ public function getData() { - $recordsData = $this->searchResultToOutput($this->getSearchResult()); - $errorMessage = ['errorMessage' => $this->getErrorMessage()]; - - return array_merge($recordsData, $errorMessage); + try { + return $this->searchResultToOutput($this->getSearchResult()); + } catch (LocalizedException $exception) { + return [ + 'items' => [], + 'totalRecords' => 0, + 'errorMessage' => $exception->getMessage() + ]; + } } /** @@ -110,14 +94,6 @@ public function getData() */ public function getSearchResult(): SearchResultInterface { - try { - $searchResult = $this->getImageList->execute($this->getSearchCriteria()); - } catch (LocalizedException $exception) { - $this->setErrorMessage($exception->getMessage()); - $searchResult = $this->searchResultFactory->create(); - $searchResult->setItems([]); - } - - return $searchResult; + return $this->getImageList->execute($this->getSearchCriteria()); } } diff --git a/AdobeStockImageAdminUi/Test/Mftf/ActionGroup/AdminOpenMediaGalleryForPageNoEditorActionGroup.xml b/AdobeStockImageAdminUi/Test/Mftf/ActionGroup/AdminOpenMediaGalleryForPageNoEditorActionGroup.xml index a577963c973a..efc8ccfb2625 100644 --- a/AdobeStockImageAdminUi/Test/Mftf/ActionGroup/AdminOpenMediaGalleryForPageNoEditorActionGroup.xml +++ b/AdobeStockImageAdminUi/Test/Mftf/ActionGroup/AdminOpenMediaGalleryForPageNoEditorActionGroup.xml @@ -14,7 +14,7 @@ - + diff --git a/AdobeStockImageAdminUi/Test/Mftf/Suite/adobe-stock-integration.xml b/AdobeStockImageAdminUi/Test/Mftf/Suite/adobe-stock-integration.xml index f84b0a6c7ee9..80ef3651a6a3 100644 --- a/AdobeStockImageAdminUi/Test/Mftf/Suite/adobe-stock-integration.xml +++ b/AdobeStockImageAdminUi/Test/Mftf/Suite/adobe-stock-integration.xml @@ -10,8 +10,8 @@ xsi:noNamespaceSchemaLocation="../../../../../../vendor/magento/magento2-functional-testing-framework/src/Magento/FunctionalTestingFramework/Suite/etc/suiteSchema.xsd"> - - + + diff --git a/AdobeStockImageAdminUi/Test/Unit/Model/Listing/DataProviderTest.php b/AdobeStockImageAdminUi/Test/Unit/Model/Listing/DataProviderTest.php index 21db34858520..639eb38b28cb 100644 --- a/AdobeStockImageAdminUi/Test/Unit/Model/Listing/DataProviderTest.php +++ b/AdobeStockImageAdminUi/Test/Unit/Model/Listing/DataProviderTest.php @@ -14,6 +14,7 @@ use Magento\Framework\Api\Search\SearchCriteria; use Magento\Framework\Api\Search\SearchCriteriaBuilder; use Magento\Framework\Api\Search\SearchResultInterface; +use Magento\Framework\Exception\LocalizedException; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -113,6 +114,33 @@ public function testGetData(array $itemsData): void $this->assertEquals($data, $this->dataProvider->getData()); } + /** + * Verify LocalizedExceptions messages are returned in errorMessage data node + */ + public function testGetDataException(): void + { + $searchCriteria = $this->createMock(SearchCriteria::class); + $searchCriteria->expects($this->once()) + ->method('setRequestName') + ->with('adobe_stock_images_listing_data_source'); + + $this->searchCriteriaBuilder->expects($this->once()) + ->method('create') + ->willReturn($searchCriteria); + + $this->getImageListMock->expects($this->once()) + ->method('execute') + ->willThrowException(new LocalizedException(__('Localized error'))); + + $data = [ + 'items' => [], + 'totalRecords' => 0, + 'errorMessage' => 'Localized error' + ]; + + $this->assertEquals($data, $this->dataProvider->getData()); + } + /** * @param array $itemsData * @return SearchResultInterface|MockObject diff --git a/AdobeUi/view/adminhtml/web/js/components/grid/masonry.js b/AdobeUi/view/adminhtml/web/js/components/grid/masonry.js index 85fac8f062b8..5ceaeb4696f2 100644 --- a/AdobeUi/view/adminhtml/web/js/components/grid/masonry.js +++ b/AdobeUi/view/adminhtml/web/js/components/grid/masonry.js @@ -255,7 +255,7 @@ define([ * @returns {String|null} */ getErrorMessage: function () { - return this.errorMessage() + return this.errorMessage(); } }); }); From 1355265575588867b504fa0399ff6e4d7728cec5 Mon Sep 17 00:00:00 2001 From: Sergii Ivashchenko Date: Mon, 4 Nov 2019 13:54:40 +0000 Subject: [PATCH 3/4] Reverted MFTF test suite preconditions --- .../Test/Mftf/Suite/adobe-stock-integration.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/AdobeStockImageAdminUi/Test/Mftf/Suite/adobe-stock-integration.xml b/AdobeStockImageAdminUi/Test/Mftf/Suite/adobe-stock-integration.xml index 80ef3651a6a3..f84b0a6c7ee9 100644 --- a/AdobeStockImageAdminUi/Test/Mftf/Suite/adobe-stock-integration.xml +++ b/AdobeStockImageAdminUi/Test/Mftf/Suite/adobe-stock-integration.xml @@ -10,8 +10,8 @@ xsi:noNamespaceSchemaLocation="../../../../../../vendor/magento/magento2-functional-testing-framework/src/Magento/FunctionalTestingFramework/Suite/etc/suiteSchema.xsd"> - - + + From 0c67fb74c766fc71917eeacc582d287cb5d2e077 Mon Sep 17 00:00:00 2001 From: Sergii Ivashchenko Date: Mon, 4 Nov 2019 14:25:03 +0000 Subject: [PATCH 4/4] magento/adobe-stock-integration#629: Fixed static tests --- .../Model/Listing/DataProvider.php | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/AdobeStockImageAdminUi/Model/Listing/DataProvider.php b/AdobeStockImageAdminUi/Model/Listing/DataProvider.php index 6b569217b0ee..c2b1900591b5 100644 --- a/AdobeStockImageAdminUi/Model/Listing/DataProvider.php +++ b/AdobeStockImageAdminUi/Model/Listing/DataProvider.php @@ -14,7 +14,6 @@ use Magento\Framework\Api\Search\SearchResultInterface; use Magento\Framework\App\RequestInterface; use Magento\AdobeStockImageApi\Api\GetImageListInterface; -use Magento\Framework\Api\Search\SearchResultInterfaceFactory; use Magento\Framework\Exception\LocalizedException; /** @@ -22,11 +21,6 @@ */ class DataProvider extends \Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider { - /** - * @var SearchResultInterfaceFactory - */ - private $searchResultFactory; - /** * @var GetImageListInterface */ @@ -34,16 +28,16 @@ class DataProvider extends \Magento\Framework\View\Element\UiComponent\DataProvi /** * DataProvider constructor. - * @param string $name - * @param string $primaryFieldName - * @param string $requestFieldName - * @param ReportingInterface $reporting + * @param string $name + * @param string $primaryFieldName + * @param string $requestFieldName + * @param ReportingInterface $reporting * @param SearchCriteriaBuilder $searchCriteriaBuilder - * @param RequestInterface $request - * @param FilterBuilder $filterBuilder + * @param RequestInterface $request + * @param FilterBuilder $filterBuilder * @param GetImageListInterface $getImageList - * @param array $meta - * @param array $data + * @param array $meta + * @param array $data */ public function __construct( string $name, @@ -54,7 +48,6 @@ public function __construct( RequestInterface $request, FilterBuilder $filterBuilder, GetImageListInterface $getImageList, - SearchResultInterfaceFactory $searchResultFactory, array $meta = [], array $data = [] ) { @@ -69,7 +62,6 @@ public function __construct( $meta, $data ); - $this->searchResultFactory = $searchResultFactory; $this->getImageList = $getImageList; }