Skip to content

Commit

Permalink
Merge pull request #23 from magento-l3/1.2-develop-pr45
Browse files Browse the repository at this point in the history
[TSG-CSL3] For 1.2 develop (pr45)
  • Loading branch information
viktym authored Nov 10, 2020
2 parents d406eaf + 8bfc18e commit 09081a5
Show file tree
Hide file tree
Showing 9 changed files with 290 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryGroupedProduct\Plugin\InventoryCatalog\Model;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Message\ManagerInterface;
use Magento\InventoryCatalog\Model\UpdateInventory;
use Magento\InventoryCatalog\Model\UpdateInventory\InventoryData;
use Magento\GroupedProduct\Model\Inventory\ChangeParentStockStatus;
use Magento\InventoryCatalogApi\Model\GetProductIdsBySkusInterface;

/**
* Disable Source items management for grouped product type.
*/
class UpdateParentStockStatusPlugin
{
/**
* @var ManagerInterface
*/
private $messageManager;

/**
* @var ChangeParentStockStatus
*/
private $changeParentStockStatus;

/**
* @var GetProductIdsBySkusInterface
*/
private $getProductIdsBySkus;

/**
* @param ManagerInterface $messageManager
* @param ChangeParentStockStatus $changeParentStockStatus
* @param GetProductIdsBySkusInterface $getProductIdsBySkus
*/
public function __construct(
ManagerInterface $messageManager,
ChangeParentStockStatus $changeParentStockStatus,
GetProductIdsBySkusInterface $getProductIdsBySkus
) {
$this->messageManager = $messageManager;
$this->changeParentStockStatus = $changeParentStockStatus;
$this->getProductIdsBySkus = $getProductIdsBySkus;
}

/**
* After execute plugin
*
* @param UpdateInventory $subject
* @param mixed $result
* @param InventoryData $data
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterExecute(UpdateInventory $subject, $result, InventoryData $data)
{
$skus = $data->getSkus();
try {
$productIds = $this->getProductIdsBySkus->execute($skus);
} catch (NoSuchEntityException $e) {
$productIds = [];
}
try {
foreach ($productIds as $productId) {
$this->changeParentStockStatus->execute((int)$productId);
}
} catch (\Exception $e) {
$this->messageManager->addExceptionMessage(
$e,
__('Something went wrong while updating the product(s) stock status.')
);
}
}
}
15 changes: 15 additions & 0 deletions InventoryGroupedProduct/Test/Mftf/Data/QueueConsumerData.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->

<entities xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:DataGenerator/etc/dataProfileSchema.xsd">
<entity name="AdminInventoryMassUpdateConsumerData">
<data key="consumerName">inventory.mass.update</data>
<data key="messageLimit">10</data>
</entity>
</entities>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->

<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
<test name="UpdateStockStatusGroupedProductTest">
<!--Run cron for start consumer -->
<actionGroup ref="CliConsumerStartActionGroup" stepKey="startMessageQueue" after="setProductToOutOfStock">
<argument name="consumerName" value="{{AdminInventoryMassUpdateConsumerData.consumerName}}"/>
<argument name="maxMessages" value="{{AdminInventoryMassUpdateConsumerData.messageLimit}}"/>
</actionGroup>
<!--Run cron for start consumer second-->
<actionGroup ref="CliConsumerStartActionGroup" stepKey="startMessageQueueSecond" after="returnProductToInStock">
<argument name="consumerName" value="{{AdminInventoryMassUpdateConsumerData.consumerName}}"/>
<argument name="maxMessages" value="{{AdminInventoryMassUpdateConsumerData.messageLimit}}"/>
</actionGroup>
</test>
</tests>
4 changes: 3 additions & 1 deletion InventoryGroupedProduct/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"magento/module-grouped-product": "*"
},
"suggest": {
"magento/module-inventory-configuration-api": "*"
"magento/module-inventory-configuration-api": "*",
"magento/module-inventory-catalog": "*",
"magento/module-inventory-catalog-api": "*"
},
"type": "magento2-module",
"license": [
Expand Down
3 changes: 3 additions & 0 deletions InventoryGroupedProduct/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@
<plugin name="disable_grouped_type"
type="Magento\InventoryGroupedProduct\Plugin\InventoryConfigurationApi\IsSourceItemManagementAllowedForProductType\DisableGroupedTypePlugin"/>
</type>
<type name="Magento\InventoryCatalog\Model\UpdateInventory">
<plugin name="updateParentLegacyStockStatus" type="Magento\InventoryGroupedProduct\Plugin\InventoryCatalog\Model\UpdateParentStockStatusPlugin"/>
</type>
</config>
49 changes: 46 additions & 3 deletions InventoryInStorePickup/Model/ExtractPickupLocationAddressData.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

namespace Magento\InventoryInStorePickup\Model;

use Magento\Directory\Model\RegionFactory;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\DataObject\Copy;
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\InventoryInStorePickupApi\Api\Data\PickupLocationInterface;

/**
Expand All @@ -20,12 +23,27 @@ class ExtractPickupLocationAddressData
*/
private $objectCopyService;

/**
* @var RegionFactory
*/
private $regionFactory;

/**
* @var array
*/
private $regions = [];

/**
* @param Copy $copyService
* @param RegionFactory|null $regionFactory
*/
public function __construct(Copy $copyService)
{
public function __construct(
Copy $copyService,
?RegionFactory $regionFactory = null
) {
$this->objectCopyService = $copyService;
$this->regionFactory = $regionFactory ?:
ObjectManager::getInstance()->get(RegionFactory::class);
}

/**
Expand All @@ -37,10 +55,35 @@ public function __construct(Copy $copyService)
*/
public function execute(PickupLocationInterface $pickupLocation): array
{
return $this->objectCopyService->getDataFromFieldset(
$data = $this->objectCopyService->getDataFromFieldset(
'inventory_convert_pickup_location',
'to_in_store_pickup_shipping_address',
$pickupLocation
);
$data = $this->retrieveRegion($pickupLocation, $data);

return $data;
}

/**
* Retrieve region name by current locale
*
* @param PickupLocationInterface $pickupLocation
* @param array $data
* @return array
*/
private function retrieveRegion(PickupLocationInterface $pickupLocation, array $data): array
{
$cacheKey = $pickupLocation->getCountryId() . '_' . $pickupLocation->getRegionId();

if (!isset($this->regions[$cacheKey])) {
$region = $this->regionFactory->create();
$region->loadByName($pickupLocation->getRegion(), $pickupLocation->getCountryId());
$this->regions[$cacheKey] = $region->getName() ?: $pickupLocation->getRegion();
}

$data[SourceInterface::REGION] = $this->regions[$cacheKey];

return $data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryInStorePickup\Test\Unit\Model;

use Magento\Directory\Model\Region;
use Magento\Directory\Model\RegionFactory;
use Magento\Framework\DataObject\Copy;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\InventoryInStorePickup\Model\ExtractPickupLocationAddressData;
use Magento\InventoryInStorePickup\Model\PickupLocation;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* Provide tests for DataResolver
*/
class ExtractPickupLocationAddressDataTest extends TestCase
{
/**
* @var ExtractPickupLocationAddressData
*/
private $model;

/**
* @var Copy|MockObject
*/
private $objectCopyServiceMock;

/**
* @var Region|MockObject
*/
private $regionMock;

/**
* @inheritDoc
*/
protected function setUp(): void
{
$objectManagerHelper = new ObjectManager($this);

$this->regionMock = $this->getMockBuilder(Region::class)
->addMethods(['getCode'])
->onlyMethods(['load', 'getName', 'loadByName'])
->disableOriginalConstructor()
->getMock();
$this->regionMock->method('loadByName')->willReturnSelf();

$this->objectCopyServiceMock = $this->getMockBuilder(Copy::class)
->disableOriginalConstructor()
->setMethods(['getDataFromFieldset'])
->getMockForAbstractClass();

$regionFactoryMock = $this->getMockBuilder(RegionFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMockForAbstractClass();

$regionFactoryMock->expects($this->any())
->method('create')->willReturn($this->regionMock);

$this->model = $objectManagerHelper->getObject(
ExtractPickupLocationAddressData::class,
[
'objectCopyService' => $this->objectCopyServiceMock,
'regionFactory' => $regionFactoryMock,
]
);
}

/**
* Check that region name is replacing correctly
*
* @param string $translatedRegionName
* @param string $expectedRegionName
* @dataProvider executeDataProvider
* @return void
*/
public function testExecute(string $translatedRegionName, string $expectedRegionName): void
{
$this->objectCopyServiceMock->method('getDataFromFieldset')
->willReturn(['region' => 'original_name']);
$this->regionMock->method('getName')->willReturn($translatedRegionName);

$pickupLocation = $this->createMock(PickupLocation::class);
$pickupLocation->method('getCountryId')->willReturn('US');
$pickupLocation->method('getRegionId')->willReturn(1);
$pickupLocation->method('getRegion')->willReturn('original_name');

$result = $this->model->execute($pickupLocation);

$this->assertEquals(
['region' => $expectedRegionName],
$result
);
}

/**
* Provider for testExecute
*
* @return array
*/
public function executeDataProvider(): array
{
return [
[
'translatedRegionName' => '',
'expectedRegionName' => 'original_name',
],
[
'translatedRegionName' => 'region_name_translated',
'expectedRegionName' => 'region_name_translated',
],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ private function getSelectionRequestItems(iterable $invoiceItems): array
$selectionRequestItems = [];
foreach ($invoiceItems as $invoiceItem) {
$orderItem = $invoiceItem->getOrderItem();

if ($orderItem->isDummy() || !$orderItem->getIsVirtual()) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public function __construct(
$this->stockId = $stockId;

//TODO: Temporary fix for resolving issue with webApi (https://github.com/magento-engcom/msi/issues/1524)
$this->items = [];
foreach ($items as $item) {
if (false === $item instanceof ItemRequestInterface) {
$this->items[] = $this->itemRequestFactory->create([
Expand Down

0 comments on commit 09081a5

Please sign in to comment.