Skip to content

Commit ee50e37

Browse files
committed
Merge branch 'feature-256' of github.com:Usik2203/magento2 into 2.4-develop-expedited-prs
2 parents 574a32c + b059e51 commit ee50e37

File tree

8 files changed

+259
-0
lines changed

8 files changed

+259
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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\Cart;
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\CartRepositoryInterface;
18+
use Magento\GiftMessage\Helper\Message as GiftMessageHelper;
19+
20+
/**
21+
* Class provides ability to get GiftMessage for cart
22+
*/
23+
class GiftMessage implements ResolverInterface
24+
{
25+
/**
26+
* @var CartRepositoryInterface
27+
*/
28+
private $cartRepository;
29+
30+
/**
31+
* @var GiftMessageHelper
32+
*/
33+
private $giftMessageHelper;
34+
35+
/**
36+
* @param CartRepositoryInterface $cartRepository
37+
* @param GiftMessageHelper $giftMessageHelper
38+
*/
39+
public function __construct(
40+
CartRepositoryInterface $cartRepository,
41+
GiftMessageHelper $giftMessageHelper
42+
) {
43+
$this->cartRepository = $cartRepository;
44+
$this->giftMessageHelper = $giftMessageHelper;
45+
}
46+
47+
/**
48+
* Return information about Gift message of cart
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+
*
58+
* @throws GraphQlInputException
59+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
60+
*/
61+
public function resolve(
62+
Field $field,
63+
$context,
64+
ResolveInfo $info,
65+
array $value = null,
66+
array $args = null
67+
) {
68+
if (!isset($value['model'])) {
69+
throw new GraphQlInputException(__('"model" value should be specified'));
70+
}
71+
72+
$cart = $value['model'];
73+
74+
if (!$this->giftMessageHelper->isMessagesAllowed('order', $cart)) {
75+
return null;
76+
}
77+
78+
try {
79+
$giftCartMessage = $this->cartRepository->get($cart->getId());
80+
} catch (LocalizedException $e) {
81+
throw new GraphQlInputException(__('Can\'t load cart.'));
82+
}
83+
84+
if (!isset($giftCartMessage)) {
85+
return null;
86+
}
87+
88+
return [
89+
'to' => $giftCartMessage->getRecipient() ?? '',
90+
'from' => $giftCartMessage->getSender() ?? '',
91+
'message'=> $giftCartMessage->getMessage() ?? ''
92+
];
93+
}
94+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# GiftMessageGraphQl
2+
3+
**GiftMessageGraphQl** provides information about gift messages for cart, cart items, order and order items.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "magento/module-gift-message-graph-ql",
3+
"description": "N/A",
4+
"type": "magento2-module",
5+
"require": {
6+
"php": "~7.3.0||~7.4.0",
7+
"magento/framework": "*",
8+
"magento/module-gift-message": "*"
9+
},
10+
"suggest": {
11+
"magento/module-graph-ql": "*"
12+
},
13+
"license": [
14+
"OSL-3.0",
15+
"AFL-3.0"
16+
],
17+
"autoload": {
18+
"files": [
19+
"registration.php"
20+
],
21+
"psr-4": {
22+
"Magento\\GiftMessageGraphQl\\": ""
23+
}
24+
}
25+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
10+
<module name="Magento_GiftMessageGraphQl">
11+
<sequence>
12+
<module name="Magento_GiftMessage"/>
13+
</sequence>
14+
</module>
15+
</config>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright © Magento, Inc. All rights reserved.
2+
# See COPYING.txt for license details.
3+
4+
type Cart {
5+
gift_message: GiftMessage @resolver (class: "\\Magento\\GiftMessageGraphQl\\Model\\Resolver\\Cart\\GiftMessage") @doc(description: "The entered gift message for the cart")
6+
}
7+
8+
type GiftMessage {
9+
to: String! @doc(description: "Recepient name")
10+
from: String! @doc(description: "Sender name")
11+
message: String! @doc(description: "Gift message text")
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\Framework\Component\ComponentRegistrar;
8+
9+
ComponentRegistrar::register(
10+
ComponentRegistrar::MODULE,
11+
'Magento_GiftMessageGraphQl',
12+
__DIR__
13+
);

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@
164164
"magento/module-encryption-key": "*",
165165
"magento/module-fedex": "*",
166166
"magento/module-gift-message": "*",
167+
"magento/module-gift-message-graph-ql": "*",
167168
"magento/module-google-adwords": "*",
168169
"magento/module-google-analytics": "*",
169170
"magento/module-google-optimizer": "*",
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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\Cart;
9+
10+
use Exception;
11+
use Magento\Framework\Exception\NoSuchEntityException;
12+
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
use Magento\TestFramework\TestCase\GraphQlAbstract;
15+
16+
class GiftMessageTest extends GraphQlAbstract
17+
{
18+
/**
19+
* @var GetMaskedQuoteIdByReservedOrderId
20+
*/
21+
private $getMaskedQuoteIdByReservedOrderId;
22+
23+
protected function setUp(): void
24+
{
25+
$objectManager = Bootstrap::getObjectManager();
26+
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
27+
}
28+
29+
/**
30+
* @magentoConfigFixture default_store sales/gift_options/allow_order 1
31+
* @magentoApiDataFixture Magento/GiftMessage/_files/quote_with_message.php
32+
* @throws NoSuchEntityException
33+
* @throws Exception
34+
*/
35+
public function testGiftMessageForCart()
36+
{
37+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('message_order_21');
38+
$response = $this->requestCartAndAssertResult($maskedQuoteId);
39+
self::assertArrayHasKey('gift_message', $response['cart']);
40+
self::assertSame('Mercutio', $response['cart']['gift_message']['to']);
41+
self::assertSame('Romeo', $response['cart']['gift_message']['from']);
42+
self::assertSame('I thought all for the best.', $response['cart']['gift_message']['message']);
43+
}
44+
45+
/**
46+
* @magentoConfigFixture default_store sales/gift_options/allow_order 0
47+
* @magentoApiDataFixture Magento/GiftMessage/_files/quote_with_message.php
48+
* @throws NoSuchEntityException
49+
* @throws Exception
50+
*/
51+
public function testGiftMessageForCartWithNotAllow()
52+
{
53+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('message_order_21');
54+
$response = $this->requestCartAndAssertResult($maskedQuoteId);
55+
self::assertArrayHasKey('gift_message', $response['cart']);
56+
self::assertNull($response['cart']['gift_message']);
57+
}
58+
59+
/**
60+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
61+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
62+
* @throws NoSuchEntityException
63+
* @throws Exception
64+
*/
65+
public function testGiftMessageForCartWithoutMessage()
66+
{
67+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
68+
$response = $this->requestCartAndAssertResult($maskedQuoteId);
69+
self::assertArrayHasKey('gift_message', $response['cart']);
70+
self::assertNull($response['cart']['gift_message']);
71+
}
72+
73+
/**
74+
* Get Gift Message Assertion
75+
*
76+
* @param string $quoteId
77+
*
78+
* @return array
79+
* @throws Exception
80+
*/
81+
private function requestCartAndAssertResult(string $quoteId)
82+
{
83+
$query = <<<QUERY
84+
{
85+
cart(cart_id: "$quoteId") {
86+
gift_message {
87+
to
88+
from
89+
message
90+
}
91+
}
92+
}
93+
QUERY;
94+
return $this->graphQlQuery($query);
95+
}
96+
}

0 commit comments

Comments
 (0)