Skip to content

Commit 6a6c897

Browse files
committed
Merge remote-tracking branch 'origin/BUG#AC-2797' into Hammer_QaulityBacklog_GraphQL_24052022
2 parents 7a64e50 + 3e5534e commit 6a6c897

File tree

3 files changed

+278
-6
lines changed

3 files changed

+278
-6
lines changed

app/code/Magento/CustomerGraphQl/Model/Resolver/IsSubscribed.php

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
namespace Magento\CustomerGraphQl\Model\Resolver;
99

1010
use Magento\Customer\Api\Data\CustomerInterface;
11+
use Magento\Framework\App\ObjectManager;
1112
use Magento\Framework\Exception\LocalizedException;
12-
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1313
use Magento\Framework\GraphQl\Config\Element\Field;
1414
use Magento\Framework\GraphQl\Query\ResolverInterface;
15+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1516
use Magento\Newsletter\Model\SubscriberFactory;
17+
use Psr\Log\LoggerInterface;
1618

1719
/**
1820
* Customer is_subscribed field resolver
@@ -22,15 +24,23 @@ class IsSubscribed implements ResolverInterface
2224
/**
2325
* @var SubscriberFactory
2426
*/
25-
private $subscriberFactory;
27+
private SubscriberFactory $subscriberFactory;
28+
29+
/**
30+
* @var LoggerInterface
31+
*/
32+
private LoggerInterface $logger;
2633

2734
/**
2835
* @param SubscriberFactory $subscriberFactory
36+
* @param LoggerInterface|null $logger
2937
*/
3038
public function __construct(
31-
SubscriberFactory $subscriberFactory
39+
SubscriberFactory $subscriberFactory,
40+
LoggerInterface $logger = null
3241
) {
3342
$this->subscriberFactory = $subscriberFactory;
43+
$this->logger = $logger ?? ObjectManager::getInstance()->get(LoggerInterface::class);
3444
}
3545

3646
/**
@@ -49,9 +59,34 @@ public function resolve(
4959
/** @var CustomerInterface $customer */
5060
$customer = $value['model'];
5161
$customerId = (int)$customer->getId();
52-
$websiteId = (int)$customer->getWebsiteId();
53-
$status = $this->subscriberFactory->create()->loadByCustomer($customerId, $websiteId)->isSubscribed();
5462

55-
return (bool)$status;
63+
$extensionAttributes = $context->getExtensionAttributes();
64+
if (!$extensionAttributes) {
65+
return false;
66+
}
67+
68+
$store = $extensionAttributes->getStore();
69+
if (!$store) {
70+
$this->logger->error(__('Store not found'));
71+
72+
return false;
73+
}
74+
75+
return $this->isSubscribed($customerId, (int)$store->getWebsiteId());
76+
}
77+
78+
/**
79+
* Get customer subscription status
80+
*
81+
* @param int $customerId
82+
* @param int $websiteId
83+
* @return bool
84+
*/
85+
public function isSubscribed(int $customerId, int $websiteId): bool
86+
{
87+
$subscriberFactory = $this->subscriberFactory->create();
88+
$subscriptionData = $subscriberFactory->loadByCustomer($customerId, $websiteId);
89+
90+
return $subscriptionData->isSubscribed() ?? false;
5691
}
5792
}
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\CustomerGraphQl\Test\Unit\Model\Resolver;
10+
11+
use Magento\Customer\Api\Data\CustomerInterface;
12+
use Magento\CustomerGraphQl\Model\Resolver\IsSubscribed;
13+
use Magento\Framework\GraphQl\Config\Element\Field;
14+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
15+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
16+
use Magento\GraphQl\Model\Query\ContextExtensionInterface;
17+
use Magento\GraphQl\Model\Query\ContextInterface;
18+
use Magento\Newsletter\Model\Subscriber;
19+
use Magento\Newsletter\Model\SubscriberFactory;
20+
use Magento\Store\Api\Data\StoreInterface;
21+
use PHPUnit\Framework\MockObject\MockObject;
22+
use PHPUnit\Framework\TestCase;
23+
24+
/**
25+
* Test class for \Magento\CustomerGraphQl\Model\Resolver\IsSubscribed
26+
*/
27+
class IsSubscribedTest extends TestCase
28+
{
29+
/**
30+
* Object Manager Instance
31+
*
32+
* @var ObjectManager
33+
*/
34+
private $objectManager;
35+
36+
/**
37+
* Testable Object
38+
*
39+
* @var IsSubscribed
40+
*/
41+
private $resolver;
42+
43+
/**
44+
* @var ContextInterface|MockObject
45+
*/
46+
private $contextMock;
47+
48+
/**
49+
* @var ContextExtensionInterface|MockObject
50+
*/
51+
private $contextExtensionMock;
52+
53+
/**
54+
* @var CustomerInterface|MockObject
55+
*/
56+
private $customerMock;
57+
58+
/**
59+
* @var SubscriberFactory|MockObject
60+
*/
61+
private $subscriberFactory;
62+
63+
/**
64+
* @var Subscriber|MockObject
65+
*/
66+
private $subscriberMock;
67+
68+
/**
69+
* @var StoreInterface|MockObject
70+
*/
71+
private $storeMock;
72+
73+
/**
74+
* @var Field|MockObject
75+
*/
76+
private $fieldMock;
77+
78+
/**
79+
* @var ResolveInfo|MockObject
80+
*/
81+
private $resolveInfoMock;
82+
83+
/**
84+
* @var array
85+
*/
86+
private array $valueMock = [];
87+
88+
/**
89+
* @inheritdoc
90+
*/
91+
protected function setUp(): void
92+
{
93+
$this->objectManager = new ObjectManager($this);
94+
95+
$this->contextMock = $this->getMockBuilder(ContextInterface::class)
96+
->disableOriginalConstructor()
97+
->getMockForAbstractClass();
98+
99+
$this->contextExtensionMock = $this->getMockBuilder(ContextExtensionInterface::class)
100+
->disableOriginalConstructor()
101+
->setMethods(['getStore'])
102+
->getMockForAbstractClass();
103+
104+
$this->customerMock = $this->getMockBuilder(CustomerInterface::class)
105+
->disableOriginalConstructor()
106+
->setMethods(['getId'])
107+
->getMockForAbstractClass();
108+
109+
$this->storeMock = $this->getMockBuilder(StoreInterface::class)
110+
->disableOriginalConstructor()
111+
->setMethods(['getWebsiteId'])
112+
->getMockForAbstractClass();
113+
114+
$this->fieldMock = $this->getMockBuilder(Field::class)
115+
->disableOriginalConstructor()
116+
->getMock();
117+
118+
$this->subscriberFactory = $this->createMock(SubscriberFactory::class);
119+
$this->subscriberMock = $this->createMock(Subscriber::class);
120+
121+
$this->resolveInfoMock = $this->getMockBuilder(ResolveInfo::class)
122+
->disableOriginalConstructor()
123+
->getMock();
124+
125+
$this->resolver = $this->objectManager->getObject(
126+
IsSubscribed::class,
127+
[
128+
'subscriberFactory' => $this->subscriberFactory
129+
]
130+
);
131+
}
132+
133+
/**
134+
* Test customer is subscribed
135+
*/
136+
public function testCustomerIsSubscribed()
137+
{
138+
$subscriber = $this->createMock(Subscriber::class);
139+
$this->valueMock = ['model' => $this->customerMock];
140+
$this->contextMock
141+
->expects($this->once())
142+
->method('getExtensionAttributes')
143+
->willReturn($this->contextExtensionMock);
144+
145+
$this->contextExtensionMock
146+
->expects($this->once())
147+
->method('getStore')
148+
->willReturn($this->storeMock);
149+
150+
$this->customerMock->expects($this->once())
151+
->method('getId')
152+
->willReturn(1);
153+
154+
$this->storeMock->expects($this->any())
155+
->method('getWebsiteId')
156+
->willReturn(1);
157+
158+
$this->assertNotNull($this->storeMock->getWebsiteId());
159+
160+
$this->subscriberFactory->expects($this->once())
161+
->method('create')
162+
->willReturn($subscriber);
163+
164+
$subscriber->expects($this->once())
165+
->method('loadByCustomer')
166+
->willReturn($this->subscriberMock);
167+
168+
$this->subscriberMock->expects($this->once())
169+
->method('isSubscribed')
170+
->willReturn(true);
171+
172+
$this->assertEquals(
173+
true,
174+
$this->resolver->resolve(
175+
$this->fieldMock,
176+
$this->contextMock,
177+
$this->resolveInfoMock,
178+
$this->valueMock
179+
)
180+
);
181+
}
182+
183+
/**
184+
* Test subscription status will return false if store not found
185+
*/
186+
public function testCustomerIsSubscribedWithoutStore()
187+
{
188+
$this->valueMock = ['model' => $this->customerMock];
189+
$this->contextMock
190+
->expects($this->once())
191+
->method('getExtensionAttributes')
192+
->willReturn($this->contextExtensionMock);
193+
194+
$this->assertEquals(
195+
false,
196+
$this->resolver->resolve(
197+
$this->fieldMock,
198+
$this->contextMock,
199+
$this->resolveInfoMock,
200+
$this->valueMock
201+
)
202+
);
203+
}
204+
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/SubscriptionStatusTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,39 @@ public function testUnsubscribeCustomer()
151151
$this->assertFalse($response['updateCustomer']['customer']['is_subscribed']);
152152
}
153153

154+
/**
155+
* @magentoConfigFixture default_store customer/account_share/scope 0
156+
* @magentoApiDataFixture Magento/Customer/_files/customer_for_second_website_with_address.php
157+
*/
158+
public function testSubscriptionStatusInMultiWebsiteSetup(): void
159+
{
160+
$currentEmail = 'customer_second_ws_with_addr@example.com';
161+
$currentPassword = 'Apassword1';
162+
163+
$query = <<<QUERY
164+
mutation {
165+
updateCustomer(
166+
input: {
167+
is_subscribed: true
168+
}
169+
) {
170+
customer {
171+
is_subscribed
172+
}
173+
}
174+
}
175+
QUERY;
176+
$headers = [
177+
'Store' => 'default',
178+
'Authorization' => sprintf(
179+
'Bearer %s',
180+
$this->customerTokenService->createCustomerAccessToken($currentEmail, $currentPassword)
181+
),
182+
];
183+
$response = $this->graphQlMutation($query, [], '', $headers);
184+
$this->assertTrue($response['updateCustomer']['customer']['is_subscribed']);
185+
}
186+
154187
/**
155188
* @param string $email
156189
* @param string $password

0 commit comments

Comments
 (0)