From 4da7b8b4e71214222f974f191431ab95f1019eb9 Mon Sep 17 00:00:00 2001 From: "al.kravchuk" Date: Tue, 16 Apr 2019 18:00:51 +0300 Subject: [PATCH 01/13] MSI-2182 Source Entity expansion with Pickup Location attributes. Add rows and extension attributes to Source. --- app/code/Magento/InventoryInStorePickup/etc/db_schema.xml | 2 ++ .../InventoryInStorePickup/etc/db_schema_whitelist.json | 4 +++- .../InventoryInStorePickup/etc/extension_attributes.xml | 2 ++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/InventoryInStorePickup/etc/db_schema.xml b/app/code/Magento/InventoryInStorePickup/etc/db_schema.xml index 71b7fa90ede0..340496832752 100644 --- a/app/code/Magento/InventoryInStorePickup/etc/db_schema.xml +++ b/app/code/Magento/InventoryInStorePickup/etc/db_schema.xml @@ -17,5 +17,7 @@ + +
diff --git a/app/code/Magento/InventoryInStorePickup/etc/db_schema_whitelist.json b/app/code/Magento/InventoryInStorePickup/etc/db_schema_whitelist.json index 74f66636c5ed..1a531c21bc15 100644 --- a/app/code/Magento/InventoryInStorePickup/etc/db_schema_whitelist.json +++ b/app/code/Magento/InventoryInStorePickup/etc/db_schema_whitelist.json @@ -11,7 +11,9 @@ }, "inventory_source": { "column": { - "is_pickup_location_active": true + "is_pickup_location_active": true, + "frontend_name": true, + "open_hours": true } } } diff --git a/app/code/Magento/InventoryInStorePickup/etc/extension_attributes.xml b/app/code/Magento/InventoryInStorePickup/etc/extension_attributes.xml index 19a91948a751..3633410dcc7b 100644 --- a/app/code/Magento/InventoryInStorePickup/etc/extension_attributes.xml +++ b/app/code/Magento/InventoryInStorePickup/etc/extension_attributes.xml @@ -15,5 +15,7 @@ + + From f6d9bb57916662a254e52baad84bb26cc85b9f25 Mon Sep 17 00:00:00 2001 From: Maksym Novik Date: Sat, 18 May 2019 16:25:29 +0300 Subject: [PATCH 02/13] Source Entity expansion with Pickup Location attributes #2182. open_hours -> frontend_description --- app/code/Magento/InventoryInStorePickup/etc/db_schema.xml | 4 ++-- .../InventoryInStorePickup/etc/db_schema_whitelist.json | 2 +- .../InventoryInStorePickup/etc/extension_attributes.xml | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/InventoryInStorePickup/etc/db_schema.xml b/app/code/Magento/InventoryInStorePickup/etc/db_schema.xml index 340496832752..9345959f936a 100644 --- a/app/code/Magento/InventoryInStorePickup/etc/db_schema.xml +++ b/app/code/Magento/InventoryInStorePickup/etc/db_schema.xml @@ -17,7 +17,7 @@ - - + +
diff --git a/app/code/Magento/InventoryInStorePickup/etc/db_schema_whitelist.json b/app/code/Magento/InventoryInStorePickup/etc/db_schema_whitelist.json index 1a531c21bc15..fba14b449028 100644 --- a/app/code/Magento/InventoryInStorePickup/etc/db_schema_whitelist.json +++ b/app/code/Magento/InventoryInStorePickup/etc/db_schema_whitelist.json @@ -13,7 +13,7 @@ "column": { "is_pickup_location_active": true, "frontend_name": true, - "open_hours": true + "frontend_description": true } } } diff --git a/app/code/Magento/InventoryInStorePickup/etc/extension_attributes.xml b/app/code/Magento/InventoryInStorePickup/etc/extension_attributes.xml index 3633410dcc7b..915a3cb51aa2 100644 --- a/app/code/Magento/InventoryInStorePickup/etc/extension_attributes.xml +++ b/app/code/Magento/InventoryInStorePickup/etc/extension_attributes.xml @@ -15,7 +15,7 @@ - - + + From 2314c9fe5263cc3e2bdc55c50782c09bd463c139 Mon Sep 17 00:00:00 2001 From: Maksym Novik Date: Sat, 18 May 2019 18:41:54 +0300 Subject: [PATCH 03/13] Source Entity expansion with Pickup Location attributes #2182. Frontend name and frontend description admin UI. Some refactoring. --- .../Model/Source/SetExtensionAttributes.php | 55 +++++++++++++++++++ .../LoadInStorePickupOnGetListPlugin.php | 35 +++++------- .../LoadInStorePickupOnGetPlugin.php | 32 ++++------- .../SaveInStorePickupPlugin.php | 12 ++-- .../ui_component/inventory_source_form.xml | 34 ++++++++++++ .../Api/Data/PickupLocationInterface.php | 2 + 6 files changed, 125 insertions(+), 45 deletions(-) create mode 100644 app/code/Magento/InventoryInStorePickup/Model/Source/SetExtensionAttributes.php diff --git a/app/code/Magento/InventoryInStorePickup/Model/Source/SetExtensionAttributes.php b/app/code/Magento/InventoryInStorePickup/Model/Source/SetExtensionAttributes.php new file mode 100644 index 000000000000..c53f3f3de3e0 --- /dev/null +++ b/app/code/Magento/InventoryInStorePickup/Model/Source/SetExtensionAttributes.php @@ -0,0 +1,55 @@ +extensionAttributesFactory = $extensionAttributesFactory; + } + /** + * @param SourceInterface|DataObject $source + */ + public function execute(SourceInterface $source): void + { + $pickupAvailable = $source->getData(PickupLocationInterface::IS_PICKUP_LOCATION_ACTIVE); + $frontendName = $source->getData(PickupLocationInterface::FRONTEND_NAME); + $frontendDescription = $source->getData(PickupLocationInterface::FRONTEND_DESCRIPTION); + + $extensionAttributes = $source->getExtensionAttributes(); + + if ($extensionAttributes === null) { + $extensionAttributes = $this->extensionAttributesFactory->create(SourceInterface::class); + /** @noinspection PhpParamsInspection */ + $source->setExtensionAttributes($extensionAttributes); + } + + $extensionAttributes + ->setIsPickupLocationActive((bool)$pickupAvailable) + ->setFrontendName($frontendName) + ->setFrontendDescription($frontendDescription); + } +} diff --git a/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceRepository/LoadInStorePickupOnGetListPlugin.php b/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceRepository/LoadInStorePickupOnGetListPlugin.php index 637bc1765803..4fe9ae96d1df 100644 --- a/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceRepository/LoadInStorePickupOnGetListPlugin.php +++ b/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceRepository/LoadInStorePickupOnGetListPlugin.php @@ -7,25 +7,24 @@ namespace Magento\InventoryInStorePickup\Plugin\InventoryApi\SourceRepository; -use Magento\Framework\Api\ExtensionAttributesFactory; -use Magento\InventoryApi\Api\Data\SourceInterface; use Magento\InventoryApi\Api\Data\SourceSearchResultsInterface; use Magento\InventoryApi\Api\SourceRepositoryInterface; -use Magento\InventoryInStorePickupApi\Api\Data\PickupLocationInterface; +use Magento\InventoryInStorePickup\Model\Source\SetExtensionAttributes; class LoadInStorePickupOnGetListPlugin { /** - * @var ExtensionAttributesFactory + * @var SetExtensionAttributes */ - private $extensionAttributesFactory; + private $setExtensionAttributes; /** - * @param ExtensionAttributesFactory $extensionAttributesFactory + * @param SetExtensionAttributes $setExtensionAttributes */ - public function __construct(ExtensionAttributesFactory $extensionAttributesFactory) - { - $this->extensionAttributesFactory = $extensionAttributesFactory; + public function __construct( + SetExtensionAttributes $setExtensionAttributes + ) { + $this->setExtensionAttributes = $setExtensionAttributes; } /** @@ -40,18 +39,12 @@ public function __construct(ExtensionAttributesFactory $extensionAttributesFacto public function afterGetList( SourceRepositoryInterface $subject, SourceSearchResultsInterface $sourceSearchResults - ):SourceSearchResultsInterface { - foreach ($sourceSearchResults->getItems() as $source) { - $extensionAttributes = $source->getExtensionAttributes(); - - if ($extensionAttributes === null) { - $extensionAttributes = $this->extensionAttributesFactory->create(SourceInterface::class); - $source->setExtensionAttributes($extensionAttributes); - } - - $pickupAvailable = $source->getData(PickupLocationInterface::IS_PICKUP_LOCATION_ACTIVE); - $extensionAttributes->setIsPickupLocationActive((bool)$pickupAvailable); - } + ): SourceSearchResultsInterface { + $items = $sourceSearchResults->getItems(); + array_walk( + $items, + [$this->setExtensionAttributes, 'execute'] + ); return $sourceSearchResults; } diff --git a/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceRepository/LoadInStorePickupOnGetPlugin.php b/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceRepository/LoadInStorePickupOnGetPlugin.php index e4e947154402..ef02eeb7b74e 100644 --- a/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceRepository/LoadInStorePickupOnGetPlugin.php +++ b/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceRepository/LoadInStorePickupOnGetPlugin.php @@ -7,31 +7,32 @@ namespace Magento\InventoryInStorePickup\Plugin\InventoryApi\SourceRepository; -use Magento\Framework\Api\ExtensionAttributesFactory; +use Magento\Framework\DataObject; use Magento\InventoryApi\Api\Data\SourceInterface; use Magento\InventoryApi\Api\SourceRepositoryInterface; -use Magento\InventoryInStorePickupApi\Api\Data\PickupLocationInterface; +use Magento\InventoryInStorePickup\Model\Source\SetExtensionAttributes; class LoadInStorePickupOnGetPlugin { /** - * @var \Magento\Framework\Api\ExtensionAttributesFactory + * @var SetExtensionAttributes */ - private $extensionAttributesFactory; + private $setExtensionAttributes; /** - * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionAttributesFactory + * @param SetExtensionAttributes $setExtensionAttributes */ - public function __construct(ExtensionAttributesFactory $extensionAttributesFactory) - { - $this->extensionAttributesFactory = $extensionAttributesFactory; + public function __construct( + SetExtensionAttributes $setExtensionAttributes + ) { + $this->setExtensionAttributes = $setExtensionAttributes; } /** * Enrich the given Source Objects with the In-Store pickup attribute * * @param SourceRepositoryInterface $subject - * @param SourceInterface $source + * @param SourceInterface|DataObject $source * * @return SourceInterface * @SuppressWarnings(PHPMD.UnusedFormalParameter) @@ -39,17 +40,8 @@ public function __construct(ExtensionAttributesFactory $extensionAttributesFacto public function afterGet( SourceRepositoryInterface $subject, SourceInterface $source - ):SourceInterface { - $pickupAvailable = $source->getData(PickupLocationInterface::IS_PICKUP_LOCATION_ACTIVE); - - $extensionAttributes = $source->getExtensionAttributes(); - - if ($extensionAttributes === null) { - $extensionAttributes = $this->extensionAttributesFactory->create(SourceInterface::class); - $source->setExtensionAttributes($extensionAttributes); - } - - $extensionAttributes->setIsPickupLocationActive((bool)$pickupAvailable); + ): SourceInterface { + $this->setExtensionAttributes->execute($source); return $source; } diff --git a/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceRepository/SaveInStorePickupPlugin.php b/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceRepository/SaveInStorePickupPlugin.php index fd4fd5bac1f9..eb74bf1b2ba8 100644 --- a/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceRepository/SaveInStorePickupPlugin.php +++ b/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceRepository/SaveInStorePickupPlugin.php @@ -7,8 +7,10 @@ namespace Magento\InventoryInStorePickup\Plugin\InventoryApi\SourceRepository; +use Magento\Framework\DataObject; use Magento\InventoryApi\Api\Data\SourceInterface; use Magento\InventoryApi\Api\SourceRepositoryInterface; +use Magento\InventoryInStorePickupApi\Api\Data\PickupLocationInterface as Location; class SaveInStorePickupPlugin { @@ -16,7 +18,7 @@ class SaveInStorePickupPlugin * Persist the In-Store pickup attribute on Source save * * @param SourceRepositoryInterface $subject - * @param SourceInterface $source + * @param SourceInterface|DataObject $source * * @return array * @SuppressWarnings(PHPMD.UnusedFormalParameter) @@ -24,11 +26,13 @@ class SaveInStorePickupPlugin public function beforeSave( SourceRepositoryInterface $subject, SourceInterface $source - ):array { + ): array { $extensionAttributes = $source->getExtensionAttributes(); - if ($extensionAttributes !== null && $extensionAttributes->getIsPickupLocationActive() !== null) { - $source->setIsPickupLocationActive($extensionAttributes->getIsPickupLocationActive()); + if ($extensionAttributes !== null) { + $source->setData(Location::IS_PICKUP_LOCATION_ACTIVE, $extensionAttributes->getIsPickupLocationActive()); + $source->setData(Location::FRONTEND_NAME, $extensionAttributes->getFrontendName()); + $source->setData(Location::FRONTEND_DESCRIPTION, $extensionAttributes->getFrontendDescription()); } return [$source]; diff --git a/app/code/Magento/InventoryInStorePickupAdminUi/view/adminhtml/ui_component/inventory_source_form.xml b/app/code/Magento/InventoryInStorePickupAdminUi/view/adminhtml/ui_component/inventory_source_form.xml index 2126e728a410..22429152d806 100644 --- a/app/code/Magento/InventoryInStorePickupAdminUi/view/adminhtml/ui_component/inventory_source_form.xml +++ b/app/code/Magento/InventoryInStorePickupAdminUi/view/adminhtml/ui_component/inventory_source_form.xml @@ -31,4 +31,38 @@ +
+ + true + + general + + + + extension_attributes.frontend_name + string + + + Pickup Location name visible for the customer on checkout + + + + + + extension_attributes.frontend_description + string + + + Pickup Location description + + + + + + true + + + + +
\ No newline at end of file diff --git a/app/code/Magento/InventoryInStorePickupApi/Api/Data/PickupLocationInterface.php b/app/code/Magento/InventoryInStorePickupApi/Api/Data/PickupLocationInterface.php index 11d801ef2147..312c2e4da2a9 100644 --- a/app/code/Magento/InventoryInStorePickupApi/Api/Data/PickupLocationInterface.php +++ b/app/code/Magento/InventoryInStorePickupApi/Api/Data/PickupLocationInterface.php @@ -19,6 +19,8 @@ interface PickupLocationInterface extends ExtensibleDataInterface { const IS_PICKUP_LOCATION_ACTIVE = 'is_pickup_location_active'; + const FRONTEND_NAME = 'frontend_name'; + const FRONTEND_DESCRIPTION = 'frontend_description'; /** * Get source code of Pickup Location From 748fd787c71362600d187a82e5e698428750f731 Mon Sep 17 00:00:00 2001 From: Maksym Novik Date: Sat, 18 May 2019 19:06:22 +0300 Subject: [PATCH 04/13] Source Entity expansion with Pickup Location attributes #2182. Improved tests coverage --- .../InventorySourceExtensionTest.php | 31 ++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/app/code/Magento/InventoryInStorePickup/Test/Integration/Extension/InventorySourceExtensionTest.php b/app/code/Magento/InventoryInStorePickup/Test/Integration/Extension/InventorySourceExtensionTest.php index af2ef3f42817..d6adcab7b002 100644 --- a/app/code/Magento/InventoryInStorePickup/Test/Integration/Extension/InventorySourceExtensionTest.php +++ b/app/code/Magento/InventoryInStorePickup/Test/Integration/Extension/InventorySourceExtensionTest.php @@ -34,21 +34,22 @@ protected function setUp() public function testGetListOfSourcesWithPickupLocationExtensionAfterSave() { $pickupLocationConfig = [ - 'default' => false, - 'eu-1' => true, - 'eu-2' => true, - 'eu-3' => false, - 'eu-disabled' => false, - 'us-1' => true, + 'default' => ['active' => false, 'name' => 'default', 'desc' => 'default'], + 'eu-1' => ['active' => true, 'name' => '', 'desc' => ''], + 'eu-2' => ['active' => true, 'name' => 'zzz', 'desc' => ''], + 'eu-3' => ['active' => false, 'name' => '', 'desc' => 'zzz1'], + 'eu-disabled' => ['active' => false, 'name' => '', 'desc' => ''], + 'us-1' => ['active' => true, 'name' => '666', 'desc' => ''], ]; $searchResult = $this->sourceRepository->getList(); /** @var SourceInterface $item */ foreach ($searchResult->getItems() as $item) { - $item->getExtensionAttributes()->setIsPickupLocationActive( - $pickupLocationConfig[$item->getSourceCode()] - ); + $item->getExtensionAttributes() + ->setIsPickupLocationActive($pickupLocationConfig[$item->getSourceCode()]['active']) + ->setFrontendDescription($pickupLocationConfig[$item->getSourceCode()]['desc']) + ->setFrontendName($pickupLocationConfig[$item->getSourceCode()]['name']); $this->sourceRepository->save($item); } @@ -57,7 +58,10 @@ public function testGetListOfSourcesWithPickupLocationExtensionAfterSave() $pickupLocationsStatus = []; foreach ($searchResult->getItems() as $item) { - $pickupLocationsStatus[$item->getSourceCode()] = $item->getExtensionAttributes()->getIsPickupLocationActive(); + $extension = $item->getExtensionAttributes(); + $pickupLocationsStatus[$item->getSourceCode()]['active'] = $extension->getIsPickupLocationActive(); + $pickupLocationsStatus[$item->getSourceCode()]['name'] = $extension->getFrontendName(); + $pickupLocationsStatus[$item->getSourceCode()]['desc'] = $extension->getFrontendDescription(); } $this->assertEquals($pickupLocationConfig, $pickupLocationsStatus); @@ -71,10 +75,15 @@ public function testGetSourceWithPickupLocationExtensionAfterSave() $sourceCode = 'source-code-1'; $source = $this->sourceRepository->get($sourceCode); - $source->getExtensionAttributes()->setIsPickupLocationActive(true); + $source->getExtensionAttributes() + ->setIsPickupLocationActive(true) + ->setFrontendName('zzz') + ->setFrontendDescription('666'); $this->sourceRepository->save($source); $source = $this->sourceRepository->get($sourceCode); $this->assertEquals(true, $source->getExtensionAttributes()->getIsPickupLocationActive()); + $this->assertEquals('zzz', $source->getExtensionAttributes()->getFrontendName()); + $this->assertEquals('666', $source->getExtensionAttributes()->getFrontendDescription()); } } From 742675602f2582415879b1963c86b32de3eb7908 Mon Sep 17 00:00:00 2001 From: Maksym Novik Date: Sat, 18 May 2019 21:20:07 +0300 Subject: [PATCH 05/13] Source Entity expansion with Pickup Location attributes #2182. Show/Hide the pickup location fieldset depending from `is_pickup_location_active` value --- .../ui_component/inventory_source_form.xml | 5 ++- .../web/js/form/components/fieldset.js | 38 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 app/code/Magento/InventoryInStorePickupAdminUi/view/adminhtml/web/js/form/components/fieldset.js diff --git a/app/code/Magento/InventoryInStorePickupAdminUi/view/adminhtml/ui_component/inventory_source_form.xml b/app/code/Magento/InventoryInStorePickupAdminUi/view/adminhtml/ui_component/inventory_source_form.xml index 22429152d806..cb45a1b99cc1 100644 --- a/app/code/Magento/InventoryInStorePickupAdminUi/view/adminhtml/ui_component/inventory_source_form.xml +++ b/app/code/Magento/InventoryInStorePickupAdminUi/view/adminhtml/ui_component/inventory_source_form.xml @@ -31,11 +31,14 @@ -
+
true general + + ${ $.provider }:data.general.extension_attributes.is_pickup_location_active + diff --git a/app/code/Magento/InventoryInStorePickupAdminUi/view/adminhtml/web/js/form/components/fieldset.js b/app/code/Magento/InventoryInStorePickupAdminUi/view/adminhtml/web/js/form/components/fieldset.js new file mode 100644 index 000000000000..3d1a4ec3f824 --- /dev/null +++ b/app/code/Magento/InventoryInStorePickupAdminUi/view/adminhtml/web/js/form/components/fieldset.js @@ -0,0 +1,38 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ +define([ + 'Magento_Ui/js/form/components/fieldset', + 'ko' +], function (Fieldset, ko) { + 'use strict'; + + /** + * @TODO Remove when issue will be resolved in core. + * @see Please check issue in core for more details: https://github.com/magento/magento2/issues/22067. + */ + return Fieldset.extend(ko).extend( + { + /** + * Convert visible value from string ('1', '0') to bool (true, false) + */ + initialize: function () { + this._super(); + + let visible = ko.observable(this.visible()); + + this.visible = ko.computed({ + read: function () { + return visible(); + }, + write: function (value) { + visible(Boolean(parseInt(value))); + }, + owner: this + }); + this.visible(visible()); + } + } + ); +}); From 755963096b07e51ae116a30477d9b7379c761312 Mon Sep 17 00:00:00 2001 From: Maksym Novik Date: Sat, 18 May 2019 22:35:17 +0300 Subject: [PATCH 06/13] Source Entity expansion with Pickup Location attributes #2182. Adjusted Pickup Location entity --- .../Model/PickupLocation.php | 16 --- .../Integration/PickupLocation/MapperTest.php | 108 ++++-------------- .../Test/_files/pickup_location.php | 54 +++++++++ .../Test/_files/pickup_location_rollback.php | 16 +++ .../Magento/InventoryInStorePickup/etc/di.xml | 8 ++ .../Api/Data/PickupLocationInterface.php | 7 -- 6 files changed, 103 insertions(+), 106 deletions(-) create mode 100644 app/code/Magento/InventoryInStorePickup/Test/_files/pickup_location.php create mode 100644 app/code/Magento/InventoryInStorePickup/Test/_files/pickup_location_rollback.php diff --git a/app/code/Magento/InventoryInStorePickup/Model/PickupLocation.php b/app/code/Magento/InventoryInStorePickup/Model/PickupLocation.php index 6969e74a553c..fb5b8667bf55 100644 --- a/app/code/Magento/InventoryInStorePickup/Model/PickupLocation.php +++ b/app/code/Magento/InventoryInStorePickup/Model/PickupLocation.php @@ -91,11 +91,6 @@ class PickupLocation implements PickupLocationInterface */ private $phone; - /** - * @var string[]|null - */ - private $openHours; - /** * @var string|null */ @@ -117,7 +112,6 @@ class PickupLocation implements PickupLocationInterface * @param string|null $street * @param string|null $postcode * @param string|null $phone - * @param string[]|null $openHours * @param PickupLocationExtensionInterface|null $extensionAttributes */ public function __construct( @@ -136,7 +130,6 @@ public function __construct( ?string $street = null, ?string $postcode = null, ?string $phone = null, - ?array $openHours = null, ?PickupLocationExtensionInterface $extensionAttributes = null ) { $this->sourceCode = $sourceCode; @@ -154,7 +147,6 @@ public function __construct( $this->street = $street; $this->postcode = $postcode; $this->phone = $phone; - $this->openHours = $openHours; $this->extensionAttributes = $extensionAttributes; } @@ -278,14 +270,6 @@ public function getPhone(): ?string return $this->phone; } - /** - * @inheritdoc - */ - public function getOpenHours(): ?array - { - return $this->openHours; - } - /** * @inheritdoc */ diff --git a/app/code/Magento/InventoryInStorePickup/Test/Integration/PickupLocation/MapperTest.php b/app/code/Magento/InventoryInStorePickup/Test/Integration/PickupLocation/MapperTest.php index 23ff6a97e05b..ec353dd5179a 100644 --- a/app/code/Magento/InventoryInStorePickup/Test/Integration/PickupLocation/MapperTest.php +++ b/app/code/Magento/InventoryInStorePickup/Test/Integration/PickupLocation/MapperTest.php @@ -7,23 +7,22 @@ namespace Magento\InventoryInStorePickup\Test\Integration\PickupLocation; -use Magento\Framework\Api\ExtensionAttributesFactory; -use Magento\InventoryApi\Api\Data\SourceExtensionInterface; +use Magento\Framework\ObjectManagerInterface; use Magento\InventoryApi\Api\SourceRepositoryInterface; -use Magento\InventoryInStorePickupApi\Model\Mapper\CreateFromSourceInterface; use Magento\InventoryInStorePickupApi\Model\Mapper; use Magento\InventoryInStorePickupApi\Api\Data\PickupLocationExtensionInterface; use Magento\TestFramework\Helper\Bootstrap; +use PHPUnit\Framework\TestCase; -class MapperTest extends \PHPUnit\Framework\TestCase +class MapperTest extends TestCase { /** - * @var \Magento\Framework\ObjectManagerInterface + * @var ObjectManagerInterface */ private $objectManager; /** - * @var \Magento\InventoryApi\Api\SourceRepositoryInterface + * @var SourceRepositoryInterface */ private $sourceRepository; @@ -36,13 +35,14 @@ protected function setUp() { $this->objectManager = Bootstrap::getObjectManager(); $this->sourceRepository = $this->objectManager->create(SourceRepositoryInterface::class); - $this->sourceCode = 'source-code-1'; + $this->sourceCode = 'pickup'; } /** - * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source.php + * @magentoDataFixture ../../../../app/code/Magento/InventoryInStorePickup/Test/_files/pickup_location.php * @expectedException \InvalidArgumentException - * @expectedExceptionMessage Wrong mapping provided for Magento\InventoryApi\Api\Data\SourceInterface. Field 'source_fail_field' is not found. + * @expectedExceptionMessage Wrong mapping provided for Magento\InventoryApi\Api\Data\SourceInterface. Field + * 'source_fail_field' is not found. */ public function testWrongMappingForSource() { @@ -55,9 +55,11 @@ public function testWrongMappingForSource() } /** - * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source.php + * @magentoDataFixture ../../../../app/code/Magento/InventoryInStorePickup/Test/_files/pickup_location.php * @expectedException \InvalidArgumentException - * @expectedExceptionMessage Wrong mapping provided for Magento\InventoryInStorePickupApi\Api\Data\PickupLocationInterface. Field 'extension_attributes.fail_field' is not found. + * @expectedExceptionMessage Wrong mapping provided for + * Magento\InventoryInStorePickupApi\Api\Data\PickupLocationInterface. Field 'extension_attributes.fail_field' + * is not found. */ public function testWrongMappingForPickupLocationExtensionAttributes() { @@ -70,9 +72,10 @@ public function testWrongMappingForPickupLocationExtensionAttributes() } /** - * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source.php + * @magentoDataFixture ../../../../app/code/Magento/InventoryInStorePickup/Test/_files/pickup_location.php * @expectedException \InvalidArgumentException - * @expectedExceptionMessage Wrong mapping provided for Magento\InventoryInStorePickupApi\Api\Data\PickupLocationInterface. Field 'fail_field' is not found. + * @expectedExceptionMessage Wrong mapping provided for + * Magento\InventoryInStorePickupApi\Api\Data\PickupLocationInterface. Field 'fail_field' is not found. */ public function testWrongMappingForPickupLocation() { @@ -85,86 +88,25 @@ public function testWrongMappingForPickupLocation() } /** - * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source.php + * @magentoDataFixture ../../../../app/code/Magento/InventoryInStorePickup/Test/_files/pickup_location.php */ public function testMapPickupLocation() { $source = $this->sourceRepository->get($this->sourceCode); /** @var Mapper $mapper */ - $mapper = $this->objectManager->create(Mapper::class, ['map' => $this->getMap()]); + $mapper = $this->objectManager->create(Mapper::class); $pickupLocation = $mapper->map($source); $this->assertEquals($source->getSourceCode(), $pickupLocation->getSourceCode()); + $this->assertEquals($source->getExtensionAttributes()->getFrontendName(), $pickupLocation->getName()); + $this->assertNotEquals($source->getDescription(), $pickupLocation->getDescription()); $this->assertEquals($source->getEmail(), $pickupLocation->getEmail()); $this->assertEquals($source->getContactName(), $pickupLocation->getContactName()); - $this->assertEquals($source->getDescription(), $pickupLocation->getDescription()); - $this->assertEquals($source->getLatitude(), $pickupLocation->getLatitude()); - $this->assertEquals($source->getLongitude(), $pickupLocation->getLongitude()); - $this->assertEquals($source->getCountryId(), $pickupLocation->getCountryId()); - $this->assertEquals($source->getRegionId(), $pickupLocation->getRegionId()); - $this->assertEquals($source->getRegion(), $pickupLocation->getRegion()); - $this->assertEquals($source->getCity(), $pickupLocation->getCity()); - $this->assertEquals($source->getStreet(), $pickupLocation->getStreet()); - $this->assertEquals($source->getPostcode(), $pickupLocation->getPostcode()); - $this->assertEquals($source->getPhone(), $pickupLocation->getPhone()); - $this->assertEquals($source->getFax(), $pickupLocation->getFax()); - $this->assertInstanceOf(PickupLocationExtensionInterface::class, $pickupLocation->getExtensionAttributes()); - } - - /** - * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source.php - */ - public function testMapPickupLocationWithExtensionAttributes() - { - $source = $this->sourceRepository->get($this->sourceCode); - - $sourceExtensionAttributes = $this->getMockBuilder(SourceExtensionInterface::class) - ->disableOriginalConstructor() - ->setMethods(['getOpenHours', 'getSomeAttribute']) - ->getMockForAbstractClass(); - $sourceExtensionAttributes->expects($this->once()) - ->method('getOpenHours') - ->willReturn(['open', 'hours']); - $sourceExtensionAttributes->expects($this->once()) - ->method('getSomeAttribute') - ->willReturn('some_value'); - $source->setExtensionAttributes($sourceExtensionAttributes); - - $pickupLocationExtension = $this->getMockBuilder(PickupLocationExtensionInterface::class) - ->disableOriginalConstructor() - ->setMethods(['setPickupLocationAttribute']) - ->getMock(); - $pickupLocationExtension->expects($this->once()) - ->method('setPickupLocationAttribute') - ->with('some_value'); - - $extensionAttributesFactory = $this->getMockBuilder(ExtensionAttributesFactory::class) - ->disableOriginalConstructor() - ->getMock(); - $extensionAttributesFactory->expects($this->once()) - ->method('create') - ->willReturn($pickupLocationExtension); - - $createFromSource = $this->objectManager->create( - CreateFromSourceInterface::class, - ['extensionAttributesFactory' => $extensionAttributesFactory] - ); - - $map = $this->getMap(); - $map['extension_attributes.open_hours'] = 'open_hours'; - $map['extension_attributes.some_attribute'] = 'extension_attributes.pickup_location_attribute'; - - /** @var Mapper $mapper */ - $mapper = $this->objectManager->create( - Mapper::class, - ['map' => $map, 'createFromSource' => $createFromSource] + $this->assertEquals( + $source->getExtensionAttributes()->getFrontendDescription(), + $pickupLocation->getDescription() ); - $pickupLocation = $mapper->map($source); - - $this->assertEquals($source->getSourceCode(), $pickupLocation->getSourceCode()); - $this->assertEquals($source->getEmail(), $pickupLocation->getEmail()); - $this->assertEquals($source->getContactName(), $pickupLocation->getContactName()); - $this->assertEquals($source->getDescription(), $pickupLocation->getDescription()); + $this->assertNotEquals($source->getName(), $pickupLocation->getName()); $this->assertEquals($source->getLatitude(), $pickupLocation->getLatitude()); $this->assertEquals($source->getLongitude(), $pickupLocation->getLongitude()); $this->assertEquals($source->getCountryId(), $pickupLocation->getCountryId()); @@ -175,7 +117,7 @@ public function testMapPickupLocationWithExtensionAttributes() $this->assertEquals($source->getPostcode(), $pickupLocation->getPostcode()); $this->assertEquals($source->getPhone(), $pickupLocation->getPhone()); $this->assertEquals($source->getFax(), $pickupLocation->getFax()); - $this->assertEquals(['open', 'hours'], $pickupLocation->getOpenHours()); + $this->assertInstanceOf(PickupLocationExtensionInterface::class, $pickupLocation->getExtensionAttributes()); } /** diff --git a/app/code/Magento/InventoryInStorePickup/Test/_files/pickup_location.php b/app/code/Magento/InventoryInStorePickup/Test/_files/pickup_location.php new file mode 100644 index 000000000000..6088fca7ff20 --- /dev/null +++ b/app/code/Magento/InventoryInStorePickup/Test/_files/pickup_location.php @@ -0,0 +1,54 @@ +get(SourceInterfaceFactory::class); +/** @var DataObjectHelper $dataObjectHelper */ +$dataObjectHelper = Bootstrap::getObjectManager()->get(DataObjectHelper::class); +/** @var SourceRepositoryInterface $sourceRepository */ +$sourceRepository = Bootstrap::getObjectManager()->get(SourceRepositoryInterface::class); + +/** @var SourceInterface $source */ +$source = $sourceFactory->create(); +$dataObjectHelper->populateWithArray( + $source, + [ + SourceInterface::SOURCE_CODE => 'pickup', + SourceInterface::NAME => 'source-name-1', + SourceInterface::CONTACT_NAME => 'source-contact-name', + SourceInterface::EMAIL => 'source-email', + SourceInterface::ENABLED => true, + SourceInterface::DESCRIPTION => 'source-description', + SourceInterface::LATITUDE => 11.123456, + SourceInterface::LONGITUDE => 12.123456, + SourceInterface::COUNTRY_ID => 'US', + SourceInterface::REGION_ID => 10, + SourceInterface::CITY => 'source-city', + SourceInterface::STREET => 'source-street', + SourceInterface::POSTCODE => 'source-postcode', + SourceInterface::PHONE => 'source-phone', + SourceInterface::FAX => 'source-fax', + SourceInterface::USE_DEFAULT_CARRIER_CONFIG => 0, + SourceInterface::CARRIER_LINKS => [], + ], + SourceInterface::class +); +$sourceRepository->save($source); + +$source = $sourceRepository->get('pickup'); +$source->getExtensionAttributes() + ->setFrontendDescription('frontend-description') + ->setFrontendName('frontend-name') + ->setIsPickupLocationActive(true); + +$sourceRepository->save($source); diff --git a/app/code/Magento/InventoryInStorePickup/Test/_files/pickup_location_rollback.php b/app/code/Magento/InventoryInStorePickup/Test/_files/pickup_location_rollback.php new file mode 100644 index 000000000000..e59fca5bcd61 --- /dev/null +++ b/app/code/Magento/InventoryInStorePickup/Test/_files/pickup_location_rollback.php @@ -0,0 +1,16 @@ +get(ResourceConnection::class); +$connection->getConnection()->delete($connection->getTableName('inventory_source'), [ + SourceInterface::SOURCE_CODE . ' IN (?)' => ['pickup'] +]); diff --git a/app/code/Magento/InventoryInStorePickup/etc/di.xml b/app/code/Magento/InventoryInStorePickup/etc/di.xml index 2c49b2118556..8d9516c46d37 100644 --- a/app/code/Magento/InventoryInStorePickup/etc/di.xml +++ b/app/code/Magento/InventoryInStorePickup/etc/di.xml @@ -32,4 +32,12 @@ + + + + name + description + + + diff --git a/app/code/Magento/InventoryInStorePickupApi/Api/Data/PickupLocationInterface.php b/app/code/Magento/InventoryInStorePickupApi/Api/Data/PickupLocationInterface.php index 312c2e4da2a9..bd28cfc9f537 100644 --- a/app/code/Magento/InventoryInStorePickupApi/Api/Data/PickupLocationInterface.php +++ b/app/code/Magento/InventoryInStorePickupApi/Api/Data/PickupLocationInterface.php @@ -127,13 +127,6 @@ public function getPostcode(): ?string; */ public function getPhone(): ?string; - /** - * Get Pickup Location open hours. - * - * @return string[]|null - */ - public function getOpenHours(): ?array; - /** * Set Extension Attributes for Pickup Location. * From 551568b84ce6936ba4919c80861ec5f8619ef688 Mon Sep 17 00:00:00 2001 From: Maksym Novik Date: Mon, 20 May 2019 22:05:43 +0300 Subject: [PATCH 07/13] Msi 2182 source entity expansion with pickup location attributes. Fixed static tests --- .../view/adminhtml/web/js/form/components/fieldset.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/InventoryInStorePickupAdminUi/view/adminhtml/web/js/form/components/fieldset.js b/app/code/Magento/InventoryInStorePickupAdminUi/view/adminhtml/web/js/form/components/fieldset.js index 3d1a4ec3f824..2ef0e184ea0e 100644 --- a/app/code/Magento/InventoryInStorePickupAdminUi/view/adminhtml/web/js/form/components/fieldset.js +++ b/app/code/Magento/InventoryInStorePickupAdminUi/view/adminhtml/web/js/form/components/fieldset.js @@ -15,19 +15,20 @@ define([ return Fieldset.extend(ko).extend( { /** - * Convert visible value from string ('1', '0') to bool (true, false) + * Convert `visible` value from string ('1', '0') to bool (true, false) */ initialize: function () { this._super(); - - let visible = ko.observable(this.visible()); + + // eslint-disable-next-line vars-on-top + var visible = ko.observable(this.visible()); this.visible = ko.computed({ read: function () { return visible(); }, write: function (value) { - visible(Boolean(parseInt(value))); + visible(Boolean(parseInt(value, 0))); }, owner: this }); From 143b67fc73e6792c975c955ab78d0a4a6ecc3b2a Mon Sep 17 00:00:00 2001 From: Maksym Novik Date: Mon, 20 May 2019 23:58:38 +0300 Subject: [PATCH 08/13] Msi 2182 source entity expansion with pickup location attributes. Set frontend name default value. --- .../SourceValidatorChain/SetFrontendName.php | 40 +++++++++++++++++++ .../Magento/InventoryInStorePickup/etc/di.xml | 4 ++ 2 files changed, 44 insertions(+) create mode 100644 app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceValidatorChain/SetFrontendName.php diff --git a/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceValidatorChain/SetFrontendName.php b/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceValidatorChain/SetFrontendName.php new file mode 100644 index 000000000000..4d07e31f5286 --- /dev/null +++ b/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceValidatorChain/SetFrontendName.php @@ -0,0 +1,40 @@ +getExtensionAttributes() === null) { + return; + } + + $frontendName = trim($source->getExtensionAttributes()->getFrontendName()); + + if ($frontendName === '') { + $source->getExtensionAttributes()->setFrontendName($source->getName()); + if ($source instanceof DataObject) { + $source->setData('frontend_name', $source->getExtensionAttributes()->getFrontendName()); + } + } + } +} diff --git a/app/code/Magento/InventoryInStorePickup/etc/di.xml b/app/code/Magento/InventoryInStorePickup/etc/di.xml index 8d9516c46d37..0134e1c4ecd8 100644 --- a/app/code/Magento/InventoryInStorePickup/etc/di.xml +++ b/app/code/Magento/InventoryInStorePickup/etc/di.xml @@ -40,4 +40,8 @@ + + + From 28c8ddaba93a027482f27a2878dace2ebf75f9a9 Mon Sep 17 00:00:00 2001 From: "al.kravchuk" Date: Wed, 22 May 2019 14:35:25 +0300 Subject: [PATCH 09/13] Msi 2182 source entity expansion with pickup location attributes. Refactor Pickup Locations mapper. Add possibility to pre-process Source data for projection to Pickup Location. --- .../Mapper/CreateFromSource.php | 44 ++++++++++++++----- .../SourceValidatorChain/SetFrontendName.php | 2 +- .../Model/Mapper.php | 31 +++++++++++-- .../Mapper/CreateFromSourceInterface.php | 11 ++++- .../Model/Mapper/PreProcessorInterface.php | 28 ++++++++++++ .../InventoryInStorePickupApi/i18n/en_US.csv | 1 + 6 files changed, 99 insertions(+), 18 deletions(-) create mode 100644 app/code/Magento/InventoryInStorePickupApi/Model/Mapper/PreProcessorInterface.php create mode 100644 app/code/Magento/InventoryInStorePickupApi/i18n/en_US.csv diff --git a/app/code/Magento/InventoryInStorePickup/Model/PickupLocation/Mapper/CreateFromSource.php b/app/code/Magento/InventoryInStorePickup/Model/PickupLocation/Mapper/CreateFromSource.php index 58a6ef55716c..90a8a8af6706 100644 --- a/app/code/Magento/InventoryInStorePickup/Model/PickupLocation/Mapper/CreateFromSource.php +++ b/app/code/Magento/InventoryInStorePickup/Model/PickupLocation/Mapper/CreateFromSource.php @@ -13,6 +13,7 @@ use Magento\InventoryInStorePickupApi\Api\Data\PickupLocationInterface; use Magento\InventoryInStorePickupApi\Api\Data\PickupLocationInterfaceFactory; use Magento\InventoryInStorePickupApi\Model\Mapper\CreateFromSourceInterface; +use Magento\InventoryInStorePickupApi\Model\Mapper\PreProcessorInterface; /** * @inheritdoc @@ -47,20 +48,40 @@ public function __construct( * @inheritdoc * @throws \InvalidArgumentException */ - public function execute(SourceInterface $source, array $map): PickupLocationInterface + public function execute(SourceInterface $source, array $map, array $preProcessors): PickupLocationInterface { - $mappedData = $this->extractDataFromSource($source, $map); - $data = $this->preparePickupLocationFields($mappedData); + $data = $this->extractDataFromSource($source, $map); + $data = $this->preProcessData($source, $data, $preProcessors); + $data = $this->preparePickupLocationFields($data, $map); return $this->pickupLocationFactory->create($data); } /** - * @param array $mappedData + * @param SourceInterface $source + * @param array $data + * @param PreProcessorInterface[] $preProcessors * * @return array */ - private function preparePickupLocationFields(array $mappedData): array + private function preProcessData(SourceInterface $source, array $data, array $preProcessors) + { + foreach ($preProcessors as $field => $processor) { + if (isset($data[$field])) { + $data[$field] = $processor->process($source, $data[$field]); + } + } + + return $data; + } + + /** + * @param array $sourceData + * @param array $map + * + * @return array + */ + private function preparePickupLocationFields(array $sourceData, array $map): array { $pickupLocationExtension = $this->extensionAttributesFactory->create(PickupLocationInterface::class); $pickupLocationMethods = get_class_methods(PickupLocationInterface::class); @@ -68,7 +89,8 @@ private function preparePickupLocationFields(array $mappedData): array 'extensionAttributes' => $pickupLocationExtension ]; - foreach ($mappedData as $pickupLocationField => $value) { + foreach ($sourceData as $sourceField => $value) { + $pickupLocationField = $map[$sourceField]; if ($this->isExtensionAttributeField($pickupLocationField)) { $methodName = $this->getSetterMethodName($this->getExtensionAttributeFieldName($pickupLocationField)); @@ -91,15 +113,15 @@ private function preparePickupLocationFields(array $mappedData): array /** * Extract values from Source according to the provided map. * - * @param \Magento\InventoryApi\Api\Data\SourceInterface $source + * @param SourceInterface $source * @param string[] $map * * @return array */ private function extractDataFromSource(SourceInterface $source, array $map): array { - $mappedData = []; - foreach ($map as $sourceField => $pickupLocationField) { + $sourceData = []; + foreach (array_keys($map) as $sourceField) { if ($this->isExtensionAttributeField($sourceField)) { $methodName = $this->getGetterMethodName($this->getExtensionAttributeFieldName($sourceField)); $entity = $source->getExtensionAttributes(); @@ -112,10 +134,10 @@ private function extractDataFromSource(SourceInterface $source, array $map): arr $this->throwException(SourceInterface::class, $sourceField); } - $mappedData[$pickupLocationField] = $entity->{$methodName}(); + $sourceData[$sourceField] = $entity->{$methodName}(); } - return $mappedData; + return $sourceData; } /** diff --git a/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceValidatorChain/SetFrontendName.php b/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceValidatorChain/SetFrontendName.php index 4d07e31f5286..2a11e5ac3c6a 100644 --- a/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceValidatorChain/SetFrontendName.php +++ b/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceValidatorChain/SetFrontendName.php @@ -28,7 +28,7 @@ public function beforeValidate(SourceValidatorChain $chain, SourceInterface $sou return; } - $frontendName = trim($source->getExtensionAttributes()->getFrontendName()); + $frontendName = trim($source->getExtensionAttributes()->getFrontendName() ?? ''); if ($frontendName === '') { $source->getExtensionAttributes()->setFrontendName($source->getName()); diff --git a/app/code/Magento/InventoryInStorePickupApi/Model/Mapper.php b/app/code/Magento/InventoryInStorePickupApi/Model/Mapper.php index 3afcd6b02691..1729b203164f 100644 --- a/app/code/Magento/InventoryInStorePickupApi/Model/Mapper.php +++ b/app/code/Magento/InventoryInStorePickupApi/Model/Mapper.php @@ -7,9 +7,11 @@ namespace Magento\InventoryInStorePickupApi\Model; +use Magento\Framework\Exception\LocalizedException; use Magento\InventoryApi\Api\Data\SourceInterface; use Magento\InventoryInStorePickupApi\Api\Data\PickupLocationInterface; use Magento\InventoryInStorePickupApi\Model\Mapper\CreateFromSourceInterface; +use Magento\InventoryInStorePickupApi\Model\Mapper\PreProcessorInterface; /** * Create projection of sources on In-Store Pickup context. @@ -31,16 +33,37 @@ class Mapper */ private $createFromSource; + /** + * @var PreProcessorInterface[] + */ + private $preProcessors; + /** * @param CreateFromSourceInterface $createFromSource - * @param array $map + * @param string[] $map + * @param PreProcessorInterface[] $preProcessors + * + * @throws \Magento\Framework\Exception\LocalizedException */ public function __construct( CreateFromSourceInterface $createFromSource, - array $map = [] + array $map = [], + array $preProcessors = [] ) { - $this->map = $map; $this->createFromSource = $createFromSource; + $this->map = $map; + + foreach ($preProcessors as $preProcessor) { + if (!$preProcessor instanceof PreProcessorInterface) { + throw new LocalizedException( + __( + 'Source Data PreProcessor must implement %1.', + PreProcessorInterface::class + ) + ); + } + } + $this->preProcessors = $preProcessors; } /** @@ -50,6 +73,6 @@ public function __construct( */ public function map(SourceInterface $source): PickupLocationInterface { - return $this->createFromSource->execute($source, $this->map); + return $this->createFromSource->execute($source, $this->map, $this->preProcessors); } } diff --git a/app/code/Magento/InventoryInStorePickupApi/Model/Mapper/CreateFromSourceInterface.php b/app/code/Magento/InventoryInStorePickupApi/Model/Mapper/CreateFromSourceInterface.php index ad079014a131..35204949b699 100644 --- a/app/code/Magento/InventoryInStorePickupApi/Model/Mapper/CreateFromSourceInterface.php +++ b/app/code/Magento/InventoryInStorePickupApi/Model/Mapper/CreateFromSourceInterface.php @@ -20,13 +20,20 @@ interface CreateFromSourceInterface { /** * @param SourceInterface $source - * @param array $map May contains references to fields in extension attributes. + * @param string[] $map May contains references to fields in extension attributes. * Please use format 'extension_attributes.field_name' to do so. E.g. * [ * "extension_attributes.source_field" => "pickup_location_field" * "extension_attributes.source_field" => "extension_attributes.pickup_location_extension_field", * ] + * @param \Magento\InventoryInStorePickupApi\Model\Mapper\PreProcessorInterface[] $preProcessors + * Map for Source Fields pre-processing. E.g. + * [ + * "source_field" => PreProcessorInterface, + * "extension_attributes.source_field" => PreProcessorInterface + * ] + * * @return PickupLocationInterface */ - public function execute(SourceInterface $source, array $map): PickupLocationInterface; + public function execute(SourceInterface $source, array $map, array $preProcessors): PickupLocationInterface; } diff --git a/app/code/Magento/InventoryInStorePickupApi/Model/Mapper/PreProcessorInterface.php b/app/code/Magento/InventoryInStorePickupApi/Model/Mapper/PreProcessorInterface.php new file mode 100644 index 000000000000..f50d17a7c864 --- /dev/null +++ b/app/code/Magento/InventoryInStorePickupApi/Model/Mapper/PreProcessorInterface.php @@ -0,0 +1,28 @@ + Date: Thu, 23 May 2019 13:03:43 +0300 Subject: [PATCH 10/13] Msi 2182 source entity expansion with pickup location attributes. LocalizedException -> InvalidArgumentException --- .../InventoryInStorePickupApi/Model/Mapper.php | 11 +++++------ .../Magento/InventoryInStorePickupApi/i18n/en_US.csv | 1 - 2 files changed, 5 insertions(+), 7 deletions(-) delete mode 100644 app/code/Magento/InventoryInStorePickupApi/i18n/en_US.csv diff --git a/app/code/Magento/InventoryInStorePickupApi/Model/Mapper.php b/app/code/Magento/InventoryInStorePickupApi/Model/Mapper.php index 1729b203164f..a3887a19fcaa 100644 --- a/app/code/Magento/InventoryInStorePickupApi/Model/Mapper.php +++ b/app/code/Magento/InventoryInStorePickupApi/Model/Mapper.php @@ -7,7 +7,7 @@ namespace Magento\InventoryInStorePickupApi\Model; -use Magento\Framework\Exception\LocalizedException; +use InvalidArgumentException; use Magento\InventoryApi\Api\Data\SourceInterface; use Magento\InventoryInStorePickupApi\Api\Data\PickupLocationInterface; use Magento\InventoryInStorePickupApi\Model\Mapper\CreateFromSourceInterface; @@ -42,8 +42,7 @@ class Mapper * @param CreateFromSourceInterface $createFromSource * @param string[] $map * @param PreProcessorInterface[] $preProcessors - * - * @throws \Magento\Framework\Exception\LocalizedException + * @throws InvalidArgumentException */ public function __construct( CreateFromSourceInterface $createFromSource, @@ -55,9 +54,9 @@ public function __construct( foreach ($preProcessors as $preProcessor) { if (!$preProcessor instanceof PreProcessorInterface) { - throw new LocalizedException( - __( - 'Source Data PreProcessor must implement %1.', + throw new InvalidArgumentException( + sprintf( + 'Source Data PreProcessor must implement %s.', PreProcessorInterface::class ) ); diff --git a/app/code/Magento/InventoryInStorePickupApi/i18n/en_US.csv b/app/code/Magento/InventoryInStorePickupApi/i18n/en_US.csv deleted file mode 100644 index be3e1c2ee00d..000000000000 --- a/app/code/Magento/InventoryInStorePickupApi/i18n/en_US.csv +++ /dev/null @@ -1 +0,0 @@ -"Source Data Pre Processor must implement %1.","Source Data Pre Processor must implement %1." From 3b6401801e642016ff89317d116a0f7fb48611a3 Mon Sep 17 00:00:00 2001 From: Maksym Novik Date: Thu, 23 May 2019 13:43:15 +0300 Subject: [PATCH 11/13] Msi 2182 source entity expansion with pickup location attributes. If frontend name is empty, pickup location uses source regular name now. --- .../Mapper/CreateFromSource.php | 2 +- .../Mapper/PreProcessor/FrontendName.php | 33 +++++++++++++++ .../SourceValidatorChain/SetFrontendName.php | 40 ------------------- .../Integration/PickupLocation/MapperTest.php | 16 ++++++++ .../Magento/InventoryInStorePickup/etc/di.xml | 7 ++-- 5 files changed, 53 insertions(+), 45 deletions(-) create mode 100644 app/code/Magento/InventoryInStorePickup/Model/PickupLocation/Mapper/PreProcessor/FrontendName.php delete mode 100644 app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceValidatorChain/SetFrontendName.php diff --git a/app/code/Magento/InventoryInStorePickup/Model/PickupLocation/Mapper/CreateFromSource.php b/app/code/Magento/InventoryInStorePickup/Model/PickupLocation/Mapper/CreateFromSource.php index 90a8a8af6706..2f91f5395ec1 100644 --- a/app/code/Magento/InventoryInStorePickup/Model/PickupLocation/Mapper/CreateFromSource.php +++ b/app/code/Magento/InventoryInStorePickup/Model/PickupLocation/Mapper/CreateFromSource.php @@ -67,7 +67,7 @@ public function execute(SourceInterface $source, array $map, array $preProcessor private function preProcessData(SourceInterface $source, array $data, array $preProcessors) { foreach ($preProcessors as $field => $processor) { - if (isset($data[$field])) { + if (array_key_exists($field, $data)) { $data[$field] = $processor->process($source, $data[$field]); } } diff --git a/app/code/Magento/InventoryInStorePickup/Model/PickupLocation/Mapper/PreProcessor/FrontendName.php b/app/code/Magento/InventoryInStorePickup/Model/PickupLocation/Mapper/PreProcessor/FrontendName.php new file mode 100644 index 000000000000..a4f1b8bc436c --- /dev/null +++ b/app/code/Magento/InventoryInStorePickup/Model/PickupLocation/Mapper/PreProcessor/FrontendName.php @@ -0,0 +1,33 @@ +getName(); + } + + return $value; + } +} diff --git a/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceValidatorChain/SetFrontendName.php b/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceValidatorChain/SetFrontendName.php deleted file mode 100644 index 2a11e5ac3c6a..000000000000 --- a/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceValidatorChain/SetFrontendName.php +++ /dev/null @@ -1,40 +0,0 @@ -getExtensionAttributes() === null) { - return; - } - - $frontendName = trim($source->getExtensionAttributes()->getFrontendName() ?? ''); - - if ($frontendName === '') { - $source->getExtensionAttributes()->setFrontendName($source->getName()); - if ($source instanceof DataObject) { - $source->setData('frontend_name', $source->getExtensionAttributes()->getFrontendName()); - } - } - } -} diff --git a/app/code/Magento/InventoryInStorePickup/Test/Integration/PickupLocation/MapperTest.php b/app/code/Magento/InventoryInStorePickup/Test/Integration/PickupLocation/MapperTest.php index ec353dd5179a..0481a4076523 100644 --- a/app/code/Magento/InventoryInStorePickup/Test/Integration/PickupLocation/MapperTest.php +++ b/app/code/Magento/InventoryInStorePickup/Test/Integration/PickupLocation/MapperTest.php @@ -120,6 +120,22 @@ public function testMapPickupLocation() $this->assertInstanceOf(PickupLocationExtensionInterface::class, $pickupLocation->getExtensionAttributes()); } + /** + * @magentoDataFixture ../../../../app/code/Magento/InventoryInStorePickup/Test/_files/pickup_location.php + * @throws + */ + public function testEmptyFrontendName() + { + $source = $this->sourceRepository->get($this->sourceCode); + $source->getExtensionAttributes()->setFrontendName(null); + + /** @var Mapper $mapper */ + $mapper = $this->objectManager->create(Mapper::class); + + $pickupLocation = $mapper->map($source); + $this->assertEquals($source->getName(), $pickupLocation->getName()); + } + /** * @return array */ diff --git a/app/code/Magento/InventoryInStorePickup/etc/di.xml b/app/code/Magento/InventoryInStorePickup/etc/di.xml index 63a9bb91cb68..b8abb4c70197 100644 --- a/app/code/Magento/InventoryInStorePickup/etc/di.xml +++ b/app/code/Magento/InventoryInStorePickup/etc/di.xml @@ -38,12 +38,11 @@ name description + + Magento\InventoryInStorePickup\Model\PickupLocation\Mapper\PreProcessor\FrontendName + - - - From e276996bf8e1f692bbebf971bb175eb0ce2762d8 Mon Sep 17 00:00:00 2001 From: Maksym Novik Date: Thu, 23 May 2019 14:43:41 +0300 Subject: [PATCH 12/13] Msi 2182 source entity expansion with pickup location attributes. Fixed jscs tests --- .../view/adminhtml/web/js/form/components/fieldset.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/InventoryInStorePickupAdminUi/view/adminhtml/web/js/form/components/fieldset.js b/app/code/Magento/InventoryInStorePickupAdminUi/view/adminhtml/web/js/form/components/fieldset.js index 2ef0e184ea0e..ac53eb13aa1b 100644 --- a/app/code/Magento/InventoryInStorePickupAdminUi/view/adminhtml/web/js/form/components/fieldset.js +++ b/app/code/Magento/InventoryInStorePickupAdminUi/view/adminhtml/web/js/form/components/fieldset.js @@ -9,7 +9,7 @@ define([ 'use strict'; /** - * @TODO Remove when issue will be resolved in core. + * TODO Remove when issue will be resolved in core. * @see Please check issue in core for more details: https://github.com/magento/magento2/issues/22067. */ return Fieldset.extend(ko).extend( @@ -19,14 +19,21 @@ define([ */ initialize: function () { this._super(); - + // eslint-disable-next-line vars-on-top var visible = ko.observable(this.visible()); this.visible = ko.computed({ + /** + * @returns {Boolean} + */ read: function () { return visible(); }, + + /** + * @param {String} value + */ write: function (value) { visible(Boolean(parseInt(value, 0))); }, From 86f5f3a8c25473c16bd183acfe31e2c385f2097b Mon Sep 17 00:00:00 2001 From: Maksym Novik Date: Thu, 23 May 2019 15:17:52 +0300 Subject: [PATCH 13/13] Msi 2182 source entity expansion with pickup location attributes. Some refactoring: added explicit DataObject check --- ...s.php => InitPickupLocationExtensionAttributes.php} | 7 +++++-- .../LoadInStorePickupOnGetListPlugin.php | 8 ++++---- .../SourceRepository/LoadInStorePickupOnGetPlugin.php | 10 +++++----- .../SourceRepository/SaveInStorePickupPlugin.php | 8 ++++++-- 4 files changed, 20 insertions(+), 13 deletions(-) rename app/code/Magento/InventoryInStorePickup/Model/Source/{SetExtensionAttributes.php => InitPickupLocationExtensionAttributes.php} (91%) diff --git a/app/code/Magento/InventoryInStorePickup/Model/Source/SetExtensionAttributes.php b/app/code/Magento/InventoryInStorePickup/Model/Source/InitPickupLocationExtensionAttributes.php similarity index 91% rename from app/code/Magento/InventoryInStorePickup/Model/Source/SetExtensionAttributes.php rename to app/code/Magento/InventoryInStorePickup/Model/Source/InitPickupLocationExtensionAttributes.php index c53f3f3de3e0..faa1b53b4762 100644 --- a/app/code/Magento/InventoryInStorePickup/Model/Source/SetExtensionAttributes.php +++ b/app/code/Magento/InventoryInStorePickup/Model/Source/InitPickupLocationExtensionAttributes.php @@ -16,7 +16,7 @@ /** * Set store-pickup related source extension attributes */ -class SetExtensionAttributes +class InitPickupLocationExtensionAttributes { /** * @var ExtensionAttributesFactory @@ -31,10 +31,13 @@ public function __construct(ExtensionAttributesFactory $extensionAttributesFacto $this->extensionAttributesFactory = $extensionAttributesFactory; } /** - * @param SourceInterface|DataObject $source + * @param SourceInterface $source */ public function execute(SourceInterface $source): void { + if (!$source instanceof DataObject) { + return; + } $pickupAvailable = $source->getData(PickupLocationInterface::IS_PICKUP_LOCATION_ACTIVE); $frontendName = $source->getData(PickupLocationInterface::FRONTEND_NAME); $frontendDescription = $source->getData(PickupLocationInterface::FRONTEND_DESCRIPTION); diff --git a/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceRepository/LoadInStorePickupOnGetListPlugin.php b/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceRepository/LoadInStorePickupOnGetListPlugin.php index 4fe9ae96d1df..3b58e4d09eef 100644 --- a/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceRepository/LoadInStorePickupOnGetListPlugin.php +++ b/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceRepository/LoadInStorePickupOnGetListPlugin.php @@ -9,20 +9,20 @@ use Magento\InventoryApi\Api\Data\SourceSearchResultsInterface; use Magento\InventoryApi\Api\SourceRepositoryInterface; -use Magento\InventoryInStorePickup\Model\Source\SetExtensionAttributes; +use Magento\InventoryInStorePickup\Model\Source\InitPickupLocationExtensionAttributes; class LoadInStorePickupOnGetListPlugin { /** - * @var SetExtensionAttributes + * @var InitPickupLocationExtensionAttributes */ private $setExtensionAttributes; /** - * @param SetExtensionAttributes $setExtensionAttributes + * @param InitPickupLocationExtensionAttributes $setExtensionAttributes */ public function __construct( - SetExtensionAttributes $setExtensionAttributes + InitPickupLocationExtensionAttributes $setExtensionAttributes ) { $this->setExtensionAttributes = $setExtensionAttributes; } diff --git a/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceRepository/LoadInStorePickupOnGetPlugin.php b/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceRepository/LoadInStorePickupOnGetPlugin.php index ef02eeb7b74e..db81d81fa449 100644 --- a/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceRepository/LoadInStorePickupOnGetPlugin.php +++ b/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceRepository/LoadInStorePickupOnGetPlugin.php @@ -10,20 +10,20 @@ use Magento\Framework\DataObject; use Magento\InventoryApi\Api\Data\SourceInterface; use Magento\InventoryApi\Api\SourceRepositoryInterface; -use Magento\InventoryInStorePickup\Model\Source\SetExtensionAttributes; +use Magento\InventoryInStorePickup\Model\Source\InitPickupLocationExtensionAttributes; class LoadInStorePickupOnGetPlugin { /** - * @var SetExtensionAttributes + * @var InitPickupLocationExtensionAttributes */ private $setExtensionAttributes; /** - * @param SetExtensionAttributes $setExtensionAttributes + * @param InitPickupLocationExtensionAttributes $setExtensionAttributes */ public function __construct( - SetExtensionAttributes $setExtensionAttributes + InitPickupLocationExtensionAttributes $setExtensionAttributes ) { $this->setExtensionAttributes = $setExtensionAttributes; } @@ -32,7 +32,7 @@ public function __construct( * Enrich the given Source Objects with the In-Store pickup attribute * * @param SourceRepositoryInterface $subject - * @param SourceInterface|DataObject $source + * @param SourceInterface $source * * @return SourceInterface * @SuppressWarnings(PHPMD.UnusedFormalParameter) diff --git a/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceRepository/SaveInStorePickupPlugin.php b/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceRepository/SaveInStorePickupPlugin.php index eb74bf1b2ba8..116e24dc056e 100644 --- a/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceRepository/SaveInStorePickupPlugin.php +++ b/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceRepository/SaveInStorePickupPlugin.php @@ -12,13 +12,17 @@ use Magento\InventoryApi\Api\SourceRepositoryInterface; use Magento\InventoryInStorePickupApi\Api\Data\PickupLocationInterface as Location; +/** + * Set data to Source itself from its extension attributes to save + * these values to `inventory_source` DB table + */ class SaveInStorePickupPlugin { /** * Persist the In-Store pickup attribute on Source save * * @param SourceRepositoryInterface $subject - * @param SourceInterface|DataObject $source + * @param SourceInterface $source * * @return array * @SuppressWarnings(PHPMD.UnusedFormalParameter) @@ -29,7 +33,7 @@ public function beforeSave( ): array { $extensionAttributes = $source->getExtensionAttributes(); - if ($extensionAttributes !== null) { + if ($extensionAttributes !== null && $source instanceof DataObject) { $source->setData(Location::IS_PICKUP_LOCATION_ACTIVE, $extensionAttributes->getIsPickupLocationActive()); $source->setData(Location::FRONTEND_NAME, $extensionAttributes->getFrontendName()); $source->setData(Location::FRONTEND_DESCRIPTION, $extensionAttributes->getFrontendDescription());