Skip to content

Commit 750d9bd

Browse files
authored
Merge branch '2.4-develop' into B2B-2404
2 parents 08cec73 + 254c17b commit 750d9bd

File tree

6 files changed

+238
-2
lines changed

6 files changed

+238
-2
lines changed

app/code/Magento/GiftMessageGraphQl/Model/Resolver/Order/GiftMessage.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ public function resolve(
7676
throw new GraphQlInputException(__('"id" value should be specified'));
7777
}
7878

79-
$orderId = $this->uidEncoder->decode((string) $this->uidEncoder->encode((string) $value['id']));
79+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
80+
$orderId = (int)base64_decode($value['id']) ?: (int)$value['id'];
8081

8182
try {
8283
$orderGiftMessage = $this->orderRepository->get($orderId);
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GiftMessageGraphQl\Model\Resolver\Order\Item;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\GraphQl\Config\Element\Field;
12+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
13+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
14+
use Magento\Framework\GraphQl\Query\Resolver\Value;
15+
use Magento\Framework\GraphQl\Query\ResolverInterface;
16+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
17+
use Magento\GiftMessage\Api\OrderItemRepositoryInterface;
18+
use Magento\GiftMessage\Helper\Message as GiftMessageHelper;
19+
20+
/**
21+
* Class provides ability to get GiftMessage for order item
22+
*/
23+
class GiftMessage implements ResolverInterface
24+
{
25+
/**
26+
* @var OrderItemRepositoryInterface
27+
*/
28+
private $orderItemRepository;
29+
30+
/**
31+
* @var GiftMessageHelper
32+
*/
33+
private $giftMessageHelper;
34+
35+
/**
36+
* @param OrderItemRepositoryInterface $itemRepository
37+
* @param GiftMessageHelper $giftMessageHelper
38+
*/
39+
public function __construct(
40+
OrderItemRepositoryInterface $itemRepository,
41+
GiftMessageHelper $giftMessageHelper
42+
) {
43+
$this->orderItemRepository = $itemRepository;
44+
$this->giftMessageHelper = $giftMessageHelper;
45+
}
46+
47+
/**
48+
* Return information about Gift message for order item
49+
*
50+
* @param Field $field
51+
* @param ContextInterface $context
52+
* @param ResolveInfo $info
53+
* @param array|null $value
54+
* @param array|null $args
55+
*
56+
* @return array|Value|mixed
57+
* @throws GraphQlInputException
58+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
59+
*/
60+
public function resolve(
61+
Field $field,
62+
$context,
63+
ResolveInfo $info,
64+
array $value = null,
65+
array $args = null
66+
) {
67+
if (!isset($value['model'])) {
68+
throw new GraphQlInputException(__('"model" value must be specified'));
69+
}
70+
71+
$orderItem = $value['model'];
72+
73+
if (!$this->giftMessageHelper->isMessagesAllowed('items', $orderItem)) {
74+
return null;
75+
}
76+
77+
if (!$this->giftMessageHelper->isMessagesAllowed('item', $orderItem)) {
78+
return null;
79+
}
80+
81+
try {
82+
$giftItemMessage = $this->orderItemRepository->get($orderItem->getOrderId(), $orderItem->getItemId());
83+
} catch (LocalizedException $e) {
84+
throw new GraphQlInputException(__('Can\'t load message for order item'));
85+
}
86+
87+
if ($giftItemMessage === null) {
88+
return null;
89+
}
90+
91+
return [
92+
'to' => $giftItemMessage->getRecipient() ?? '',
93+
'from' => $giftItemMessage->getSender() ?? '',
94+
'message'=> $giftItemMessage->getMessage() ?? ''
95+
];
96+
}
97+
}

app/code/Magento/GiftMessageGraphQl/etc/schema.graphqls

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,7 @@ type SalesItemInterface {
4545
type CustomerOrder {
4646
gift_message: GiftMessage @resolver (class: "\\Magento\\GiftMessageGraphQl\\Model\\Resolver\\Order\\GiftMessage") @doc(description: "The entered gift message for the order")
4747
}
48+
49+
interface OrderItemInterface {
50+
gift_message: GiftMessage @resolver(class: "\\Magento\\GiftMessageGraphQl\\Model\\Resolver\\Order\\Item\\GiftMessage") @doc(description: "The selected gift message for the order item")
51+
}

dev/tests/api-functional/testsuite/Magento/GraphQl/GiftMessage/Order/GiftMessageTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,44 @@ public function testGiftMessageForOrder()
4848
}
4949
}
5050

51+
/**
52+
* @magentoConfigFixture default_store sales/gift_options/allow_order 1
53+
* @magentoConfigFixture default_store sales/gift_options/allow_items 1
54+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
55+
* @magentoApiDataFixture Magento/GiftMessage/_files/customer/order_with_message.php
56+
* @throws AuthenticationException
57+
* @throws Exception
58+
*/
59+
public function testGiftMessageForCustomerOrder()
60+
{
61+
$query = <<<QUERY
62+
query {
63+
customer {
64+
orders(filter:{number:{eq:"999999991"}}){
65+
total_count
66+
items
67+
{
68+
gift_message {
69+
from
70+
to
71+
message
72+
}
73+
}
74+
}
75+
}
76+
}
77+
QUERY;
78+
$currentEmail = 'customer@example.com';
79+
$currentPassword = 'password';
80+
$response = $this->graphQlQuery($query, [], '', $this->getCustomerAuthHeaders($currentEmail, $currentPassword));
81+
foreach ($response['customer']['orders']['items'] as $order) {
82+
self::assertArrayHasKey('gift_message', $order);
83+
self::assertSame('Jane Roe', $order['gift_message']['to']);
84+
self::assertSame('John Doe', $order['gift_message']['from']);
85+
self::assertSame('Gift Message Text', $order['gift_message']['message']);
86+
}
87+
}
88+
5189
/**
5290
* @magentoConfigFixture default_store sales/gift_options/allow_order 0
5391
* @magentoConfigFixture default_store sales/gift_options/allow_items 0
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\GiftMessage\Order\Item;
9+
10+
use Exception;
11+
use Magento\Framework\Exception\AuthenticationException;
12+
use Magento\Integration\Api\CustomerTokenServiceInterface;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
use Magento\TestFramework\TestCase\GraphQlAbstract;
15+
16+
class GiftMessageTest extends GraphQlAbstract
17+
{
18+
/**
19+
* @var CustomerTokenServiceInterface
20+
*/
21+
private $customerTokenService;
22+
23+
protected function setUp(): void
24+
{
25+
parent::setUp();
26+
$this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class);
27+
}
28+
29+
/**
30+
* @magentoConfigFixture default_store sales/gift_options/allow_order 1
31+
* @magentoConfigFixture default_store sales/gift_options/allow_items 1
32+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
33+
* @magentoApiDataFixture Magento/GiftMessage/_files/customer/order_with_message.php
34+
* @throws AuthenticationException
35+
* @throws Exception
36+
*/
37+
public function testGiftMessageForOrderItem()
38+
{
39+
$query = <<<QUERY
40+
query {
41+
customer {
42+
orders(filter:{number:{eq:"999999991"}}){
43+
total_count
44+
items {
45+
id
46+
items{
47+
gift_message {
48+
from
49+
to
50+
message
51+
}
52+
}
53+
gift_message {
54+
from
55+
to
56+
message
57+
}
58+
}
59+
}
60+
}
61+
}
62+
QUERY;
63+
$currentEmail = 'customer@example.com';
64+
$currentPassword = 'password';
65+
$response = $this->graphQlQuery($query, [], '', $this->getCustomerAuthHeaders($currentEmail, $currentPassword));
66+
foreach ($response['customer']['orders']['items'][0]['items'] as $item) {
67+
self::assertArrayHasKey('gift_message', $item);
68+
self::assertSame('Luci', $item['gift_message']['to']);
69+
self::assertSame('Jack', $item['gift_message']['from']);
70+
self::assertSame('Good Job!', $item['gift_message']['message']);
71+
}
72+
}
73+
74+
/**
75+
* @param string $email
76+
* @param string $password
77+
* @return array
78+
* @throws AuthenticationException
79+
*/
80+
private function getCustomerAuthHeaders(string $email, string $password): array
81+
{
82+
$customerToken = $this->customerTokenService->createCustomerAccessToken($email, $password);
83+
return ['Authorization' => 'Bearer ' . $customerToken];
84+
}
85+
}

dev/tests/integration/testsuite/Magento/GiftMessage/_files/customer/order_with_message.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,25 @@
7979
$messageModel->setMessage('Gift Message Text');
8080
$message->save($messageModel);
8181

82+
/** @var MessageResource $productMessage */
83+
$productMessage = $objectManager->create(MessageResource::class);
84+
/** @var Message $productMessageModel */
85+
$productMessageModel = $objectManager->create(Message::class);
86+
87+
$productMessageModel->setSender('Jack');
88+
$productMessageModel->setRecipient('Luci');
89+
$productMessageModel->setMessage('Good Job!');
90+
$productMessage->save($productMessageModel);
91+
8292
/** @var Order\Item $orderItem */
8393
$orderItem = $objectManager->create(Order\Item::class);
8494
$orderItem->setProductId($product->getId())
8595
->setQtyOrdered(2)
8696
->setBasePrice($product->getPrice())
8797
->setPrice($product->getPrice())
8898
->setRowTotal($product->getPrice())
89-
->setProductType('simple');
99+
->setProductType('simple')
100+
->setGiftMessageId($productMessageModel->getId());
90101

91102
$order
92103
->setData($orderData)

0 commit comments

Comments
 (0)