diff --git a/app/code/Magento/InventoryInStorePickup/Model/PickupLocation.php b/app/code/Magento/InventoryInStorePickup/Model/PickupLocation.php index 18c59bd35801..60bb17dc00c0 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/Model/PickupLocation/Mapper/CreateFromSource.php b/app/code/Magento/InventoryInStorePickup/Model/PickupLocation/Mapper/CreateFromSource.php index 58a6ef55716c..2f91f5395ec1 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 (array_key_exists($field, $data)) { + $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/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/Model/Source/InitPickupLocationExtensionAttributes.php b/app/code/Magento/InventoryInStorePickup/Model/Source/InitPickupLocationExtensionAttributes.php new file mode 100644 index 000000000000..faa1b53b4762 --- /dev/null +++ b/app/code/Magento/InventoryInStorePickup/Model/Source/InitPickupLocationExtensionAttributes.php @@ -0,0 +1,58 @@ +extensionAttributesFactory = $extensionAttributesFactory; + } + /** + * @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); + + $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..3b58e4d09eef 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\InitPickupLocationExtensionAttributes; class LoadInStorePickupOnGetListPlugin { /** - * @var ExtensionAttributesFactory + * @var InitPickupLocationExtensionAttributes */ - private $extensionAttributesFactory; + private $setExtensionAttributes; /** - * @param ExtensionAttributesFactory $extensionAttributesFactory + * @param InitPickupLocationExtensionAttributes $setExtensionAttributes */ - public function __construct(ExtensionAttributesFactory $extensionAttributesFactory) - { - $this->extensionAttributesFactory = $extensionAttributesFactory; + public function __construct( + InitPickupLocationExtensionAttributes $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..db81d81fa449 100644 --- a/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceRepository/LoadInStorePickupOnGetPlugin.php +++ b/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceRepository/LoadInStorePickupOnGetPlugin.php @@ -7,24 +7,25 @@ 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\InitPickupLocationExtensionAttributes; class LoadInStorePickupOnGetPlugin { /** - * @var \Magento\Framework\Api\ExtensionAttributesFactory + * @var InitPickupLocationExtensionAttributes */ - private $extensionAttributesFactory; + private $setExtensionAttributes; /** - * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionAttributesFactory + * @param InitPickupLocationExtensionAttributes $setExtensionAttributes */ - public function __construct(ExtensionAttributesFactory $extensionAttributesFactory) - { - $this->extensionAttributesFactory = $extensionAttributesFactory; + public function __construct( + InitPickupLocationExtensionAttributes $setExtensionAttributes + ) { + $this->setExtensionAttributes = $setExtensionAttributes; } /** @@ -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..116e24dc056e 100644 --- a/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceRepository/SaveInStorePickupPlugin.php +++ b/app/code/Magento/InventoryInStorePickup/Plugin/InventoryApi/SourceRepository/SaveInStorePickupPlugin.php @@ -7,9 +7,15 @@ 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; +/** + * Set data to Source itself from its extension attributes to save + * these values to `inventory_source` DB table + */ class SaveInStorePickupPlugin { /** @@ -24,11 +30,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 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()); } return [$source]; 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()); } } diff --git a/app/code/Magento/InventoryInStorePickup/Test/Integration/PickupLocation/MapperTest.php b/app/code/Magento/InventoryInStorePickup/Test/Integration/PickupLocation/MapperTest.php index 23ff6a97e05b..0481a4076523 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,19 +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->getExtensionAttributes()->getFrontendDescription(), + $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()); @@ -112,70 +121,19 @@ public function testMapPickupLocation() } /** - * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source.php + * @magentoDataFixture ../../../../app/code/Magento/InventoryInStorePickup/Test/_files/pickup_location.php + * @throws */ - public function testMapPickupLocationWithExtensionAttributes() + public function testEmptyFrontendName() { $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'; + $source->getExtensionAttributes()->setFrontendName(null); /** @var Mapper $mapper */ - $mapper = $this->objectManager->create( - Mapper::class, - ['map' => $map, 'createFromSource' => $createFromSource] - ); - $pickupLocation = $mapper->map($source); + $mapper = $this->objectManager->create(Mapper::class); - $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->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->assertEquals(['open', 'hours'], $pickupLocation->getOpenHours()); + $pickupLocation = $mapper->map($source); + $this->assertEquals($source->getName(), $pickupLocation->getName()); } /** 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/db_schema.xml b/app/code/Magento/InventoryInStorePickup/etc/db_schema.xml index 71b7fa90ede0..9345959f936a 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..fba14b449028 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, + "frontend_description": true } } } diff --git a/app/code/Magento/InventoryInStorePickup/etc/di.xml b/app/code/Magento/InventoryInStorePickup/etc/di.xml index 111570b89e4d..198834e13c49 100644 --- a/app/code/Magento/InventoryInStorePickup/etc/di.xml +++ b/app/code/Magento/InventoryInStorePickup/etc/di.xml @@ -33,6 +33,17 @@ + + + + name + description + + + Magento\InventoryInStorePickup\Model\PickupLocation\Mapper\PreProcessor\FrontendName + + + diff --git a/app/code/Magento/InventoryInStorePickup/etc/extension_attributes.xml b/app/code/Magento/InventoryInStorePickup/etc/extension_attributes.xml index 19a91948a751..915a3cb51aa2 100644 --- a/app/code/Magento/InventoryInStorePickup/etc/extension_attributes.xml +++ b/app/code/Magento/InventoryInStorePickup/etc/extension_attributes.xml @@ -15,5 +15,7 @@ + + 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..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,4 +31,41 @@ +
+ + true + + general + + ${ $.provider }:data.general.extension_attributes.is_pickup_location_active + + + + + 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/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..ac53eb13aa1b --- /dev/null +++ b/app/code/Magento/InventoryInStorePickupAdminUi/view/adminhtml/web/js/form/components/fieldset.js @@ -0,0 +1,46 @@ +/* + * 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(); + + // 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))); + }, + owner: this + }); + this.visible(visible()); + } + } + ); +}); diff --git a/app/code/Magento/InventoryInStorePickupApi/Api/Data/PickupLocationInterface.php b/app/code/Magento/InventoryInStorePickupApi/Api/Data/PickupLocationInterface.php index 11d801ef2147..bd28cfc9f537 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 @@ -125,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. * diff --git a/app/code/Magento/InventoryInStorePickupApi/Model/Mapper.php b/app/code/Magento/InventoryInStorePickupApi/Model/Mapper.php index 3afcd6b02691..a3887a19fcaa 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 InvalidArgumentException; 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,36 @@ class Mapper */ private $createFromSource; + /** + * @var PreProcessorInterface[] + */ + private $preProcessors; + /** * @param CreateFromSourceInterface $createFromSource - * @param array $map + * @param string[] $map + * @param PreProcessorInterface[] $preProcessors + * @throws InvalidArgumentException */ 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 InvalidArgumentException( + sprintf( + 'Source Data PreProcessor must implement %s.', + PreProcessorInterface::class + ) + ); + } + } + $this->preProcessors = $preProcessors; } /** @@ -50,6 +72,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 @@ +