Skip to content

Commit bd033a6

Browse files
committed
AC-2797:GraphQL Customer Newsletter Subscriptions are inconsistent in multi website setups - Added Unit test coverage
1 parent 62186a1 commit bd033a6

File tree

2 files changed

+215
-2
lines changed

2 files changed

+215
-2
lines changed

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

Lines changed: 14 additions & 2 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;
1213
use Magento\Framework\GraphQl\Config\Element\Field;
1314
use Magento\Framework\GraphQl\Query\ResolverInterface;
1415
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
/**
@@ -57,6 +67,8 @@ public function resolve(
5767

5868
$store = $extensionAttributes->getStore();
5969
if (!$store) {
70+
$this->logger->error(__('Store not found'));
71+
6072
return false;
6173
}
6274

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
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 $subscriber;
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+
->getMockForAbstractClass();
102+
103+
$this->customerMock = $this->getMockBuilder(CustomerInterface::class)
104+
->disableOriginalConstructor()
105+
->getMockForAbstractClass();
106+
107+
$this->storeMock = $this->getMockBuilder(StoreInterface::class)
108+
->disableOriginalConstructor()
109+
->getMockForAbstractClass();
110+
111+
$this->fieldMock = $this->getMockBuilder(Field::class)
112+
->disableOriginalConstructor()
113+
->getMock();
114+
115+
$this->subscriberFactory = $this->createMock(SubscriberFactory::class);
116+
$this->subscriber = $this->createMock(Subscriber::class);
117+
118+
$this->resolveInfoMock = $this->getMockBuilder(ResolveInfo::class)
119+
->disableOriginalConstructor()
120+
->getMock();
121+
122+
$this->resolver = $this->objectManager->getObject(
123+
IsSubscribed::class,
124+
[
125+
'subscriberFactory' => $this->subscriberFactory
126+
]
127+
);
128+
}
129+
130+
/**
131+
* Test customer is subscribed
132+
*/
133+
public function testCustomerIsSubscribed()
134+
{
135+
$subscriber = $this->createMock(Subscriber::class);
136+
$this->valueMock = ['model' => $this->customerMock];
137+
$this->contextMock
138+
->expects($this->once())
139+
->method('getExtensionAttributes')
140+
->willReturn($this->contextExtensionMock);
141+
142+
$this->contextExtensionMock
143+
->expects($this->once())
144+
->method('getStore')
145+
->willReturn($this->storeMock);
146+
147+
$this->customerMock->expects($this->once())
148+
->method('getId')
149+
->willReturn(1);
150+
151+
$this->storeMock->expects($this->any())
152+
->method('getWebsiteId')
153+
->willReturn(1);
154+
155+
$this->assertNotNull($this->storeMock->getWebsiteId());
156+
157+
$this->subscriberFactory->expects($this->once())
158+
->method('create')
159+
->willReturn($subscriber);
160+
161+
$subscriber->expects($this->once())
162+
->method('loadByCustomer')
163+
->willReturn($this->subscriber);
164+
165+
$this->subscriber->expects($this->once())
166+
->method('isSubscribed')
167+
->willReturn(true);
168+
169+
$this->assertEquals(
170+
true,
171+
$this->resolver->resolve(
172+
$this->fieldMock,
173+
$this->contextMock,
174+
$this->resolveInfoMock,
175+
$this->valueMock
176+
)
177+
);
178+
}
179+
180+
/**
181+
* Test subscription status will return false if store not found
182+
*/
183+
public function testCustomerIsSubscribedWithoutStore()
184+
{
185+
$this->valueMock = ['model' => $this->customerMock];
186+
$this->contextMock
187+
->expects($this->once())
188+
->method('getExtensionAttributes')
189+
->willReturn($this->contextExtensionMock);
190+
191+
$this->assertEquals(
192+
false,
193+
$this->resolver->resolve(
194+
$this->fieldMock,
195+
$this->contextMock,
196+
$this->resolveInfoMock,
197+
$this->valueMock
198+
)
199+
);
200+
}
201+
}

0 commit comments

Comments
 (0)