-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENGCOM-6540: Add plugin for SalesOrderItemRepository gift message (#1…
…9093) #25946 - Merge Pull Request #25946 from lfolco/magento2:orderitem-giftmessage - Merged commits: 1. 30b5818 2. f076e55
- Loading branch information
Showing
4 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
app/code/Magento/GiftMessage/Model/Plugin/OrderItemGet.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\GiftMessage\Model\Plugin; | ||
|
||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\GiftMessage\Api\OrderItemRepositoryInterface as GiftMessageItemRepositoryInterface; | ||
use Magento\Sales\Api\Data\OrderItemExtensionFactory; | ||
use Magento\Sales\Api\Data\OrderItemInterface; | ||
use Magento\Sales\Api\OrderItemRepositoryInterface; | ||
|
||
/** | ||
* Plugin for adding gift message to order item | ||
*/ | ||
class OrderItemGet | ||
{ | ||
|
||
/** | ||
* @var OrderItemExtensionFactory | ||
*/ | ||
private $orderItemExtensionFactory; | ||
|
||
/** | ||
* @var GiftMessageItemRepositoryInterface | ||
*/ | ||
private $giftMessageItemRepository; | ||
|
||
/** | ||
* OrderItemGet constructor. | ||
* | ||
* @param GiftMessageItemRepositoryInterface $giftMessageItemRepository | ||
* @param OrderItemExtensionFactory $orderItemExtensionFactory | ||
*/ | ||
public function __construct( | ||
GiftMessageItemRepositoryInterface $giftMessageItemRepository, | ||
OrderItemExtensionFactory $orderItemExtensionFactory | ||
) { | ||
$this->giftMessageItemRepository = $giftMessageItemRepository; | ||
$this->orderItemExtensionFactory = $orderItemExtensionFactory; | ||
} | ||
|
||
/** | ||
* Add gift message for order item | ||
* | ||
* @param OrderItemRepositoryInterface $subject | ||
* @param OrderItemInterface $orderItem | ||
* @return OrderItemInterface | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function afterGet(OrderItemRepositoryInterface $subject, OrderItemInterface $orderItem) | ||
{ | ||
$extensionAttributes = $orderItem->getExtensionAttributes(); | ||
if ($extensionAttributes && $extensionAttributes->getGiftMessage()) { | ||
return $orderItem; | ||
} | ||
try { | ||
/* @var \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage */ | ||
$giftMessage = $this->giftMessageItemRepository->get( | ||
$orderItem->getOrderId(), | ||
$orderItem->getItemId() | ||
); | ||
} catch (NoSuchEntityException $e) { | ||
return $orderItem; | ||
} | ||
/** @var \Magento\Sales\Api\Data\OrderItemExtension $orderItemExtension */ | ||
$orderItemExtension = $extensionAttributes ?: $this->orderItemExtensionFactory->create(); | ||
$orderItemExtension->setGiftMessage($giftMessage); | ||
$orderItem->setExtensionAttributes($orderItemExtension); | ||
|
||
return $orderItem; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
dev/tests/api-functional/testsuite/Magento/GiftMessage/Api/OrderGetRepositoryTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\GiftMessage\Api; | ||
|
||
use Magento\TestFramework\TestCase\WebapiAbstract; | ||
|
||
class OrderGetRepositoryTest extends WebapiAbstract | ||
{ | ||
const SERVICE_VERSION = 'V1'; | ||
const SERVICE_NAME = 'salesOrderRepositoryV1'; | ||
const RESOURCE_PATH = '/V1/orders/'; | ||
|
||
/** | ||
* @magentoDataFixture Magento/GiftMessage/_files/order_with_message.php | ||
* @magentoConfigFixture default_store sales/gift_options/allow_order 1 | ||
* @magentoAppIsolation enabled | ||
* @magentoDbIsolation disabled | ||
*/ | ||
public function testGet() | ||
{ | ||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); | ||
/** @var \Magento\Sales\Model\Order $order */ | ||
$order = $objectManager->create(\Magento\Sales\Model\Order::class)->loadByIncrementId('100000001'); | ||
$orderId = $order->getId(); | ||
$serviceInfo = [ | ||
'rest' => [ | ||
'resourcePath' => self::RESOURCE_PATH . $orderId, | ||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET, | ||
], | ||
'soap' => [ | ||
'service' => self::SERVICE_NAME, | ||
'serviceVersion' => self::SERVICE_VERSION, | ||
'operation' => self::SERVICE_NAME . 'Get', | ||
], | ||
]; | ||
$expectedMessage = [ | ||
'recipient' => 'Mercutio', | ||
'sender' => 'Romeo', | ||
'message' => 'I thought all for the best.', | ||
]; | ||
$requestData = ['id' => $orderId]; | ||
$result = $this->_webApiCall($serviceInfo, $requestData); | ||
$resultMessage = $result['extension_attributes']['gift_message']; | ||
static::assertCount(5, $resultMessage); | ||
unset($resultMessage['gift_message_id']); | ||
unset($resultMessage['customer_id']); | ||
static::assertEquals($expectedMessage, $resultMessage); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
dev/tests/api-functional/testsuite/Magento/GiftMessage/Api/OrderItemGetRepositoryTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\GiftMessage\Api; | ||
|
||
use Magento\TestFramework\TestCase\WebapiAbstract; | ||
|
||
class OrderItemGetRepositoryTest extends WebapiAbstract | ||
{ | ||
const SERVICE_VERSION = 'V1'; | ||
const SERVICE_NAME = 'salesOrderItemRepositoryV1'; | ||
const RESOURCE_PATH = '/V1/orders/items/'; | ||
|
||
/** | ||
* @magentoDataFixture Magento/GiftMessage/_files/order_with_message.php | ||
* @magentoConfigFixture default_store sales/gift_options/allow_items 1 | ||
* @magentoAppIsolation enabled | ||
* @magentoDbIsolation disabled | ||
*/ | ||
public function testGet() | ||
{ | ||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); | ||
/** @var \Magento\Sales\Model\Order $order */ | ||
$order = $objectManager->create(\Magento\Sales\Model\Order::class)->loadByIncrementId('100000001'); | ||
$items = $order->getItems(); | ||
/** @var \Magento\Sales\Api\Data\OrderItemInterface $orderItem */ | ||
$orderItem = array_shift($items); | ||
$itemId = $orderItem->getItemId(); | ||
$serviceInfo = [ | ||
'rest' => [ | ||
'resourcePath' => self::RESOURCE_PATH . $itemId, | ||
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET, | ||
], | ||
'soap' => [ | ||
'service' => self::SERVICE_NAME, | ||
'serviceVersion' => self::SERVICE_VERSION, | ||
'operation' => self::SERVICE_NAME . 'Get', | ||
], | ||
]; | ||
$expectedMessage = [ | ||
'recipient' => 'Mercutio', | ||
'sender' => 'Romeo', | ||
'message' => 'I thought all for the best.', | ||
]; | ||
$requestData = ['id' => $itemId]; | ||
$result = $this->_webApiCall($serviceInfo, $requestData); | ||
$resultMessage = $result['extension_attributes']['gift_message']; | ||
static::assertCount(5, $resultMessage); | ||
unset($resultMessage['gift_message_id']); | ||
unset($resultMessage['customer_id']); | ||
static::assertEquals($expectedMessage, $resultMessage); | ||
} | ||
} |