Skip to content

Commit 993a1e5

Browse files
#23972: Refactoring, static and unit tests fix.
1 parent 9f37b2f commit 993a1e5

File tree

2 files changed

+81
-40
lines changed

2 files changed

+81
-40
lines changed

app/code/Magento/Downloadable/Observer/IsAllowedGuestCheckoutObserver.php

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,86 +3,100 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Downloadable\Observer;
78

9+
use Magento\Downloadable\Model\Product\Type;
10+
use Magento\Downloadable\Model\ResourceModel\Link\CollectionFactory;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use Magento\Framework\Event\Observer;
813
use Magento\Framework\Event\ObserverInterface;
14+
use Magento\Quote\Api\Data\CartItemInterface;
15+
use Magento\Quote\Model\Quote;
916
use Magento\Store\Model\ScopeInterface;
1017

18+
/**
19+
* Checks if guest checkout is allowed then quote contains downloadable products.
20+
*/
1121
class IsAllowedGuestCheckoutObserver implements ObserverInterface
1222
{
1323
/**
1424
* Xml path to disable checkout
1525
*/
16-
const XML_PATH_DISABLE_GUEST_CHECKOUT = 'catalog/downloadable/disable_guest_checkout';
26+
private const XML_PATH_DISABLE_GUEST_CHECKOUT = 'catalog/downloadable/disable_guest_checkout';
1727

1828
/**
1929
* Xml path to get downloadable Shareable setting
2030
*/
21-
const XML_PATH_DOWNLOADABLE_SHAREABLE = 'catalog/downloadable/shareable';
31+
private const XML_PATH_DOWNLOADABLE_SHAREABLE = 'catalog/downloadable/shareable';
2232

2333
/**
2434
* Core store config
2535
*
26-
* @var \Magento\Framework\App\Config\ScopeConfigInterface
36+
* @var ScopeConfigInterface
2737
*/
28-
protected $_scopeConfig;
38+
private $scopeConfig;
2939

3040
/**
3141
* Downloadable link collection factory
3242
*
33-
* @var \Magento\Downloadable\Model\ResourceModel\Link\CollectionFactory
43+
* @var CollectionFactory
3444
*/
35-
protected $_linksFactory;
45+
private $linksFactory;
3646

3747
/**
38-
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
48+
* @param ScopeConfigInterface $scopeConfig
49+
* @param CollectionFactory $linksFactory
3950
*/
4051
public function __construct(
41-
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
42-
\Magento\Downloadable\Model\ResourceModel\Link\CollectionFactory $linksFactory
52+
ScopeConfigInterface $scopeConfig,
53+
CollectionFactory $linksFactory
4354
) {
44-
$this->_scopeConfig = $scopeConfig;
45-
$this->_linksFactory = $linksFactory;
55+
$this->scopeConfig = $scopeConfig;
56+
$this->linksFactory = $linksFactory;
4657
}
4758

4859
/**
4960
* Check is allowed guest checkout if quote contain downloadable product(s)
5061
*
51-
* @param \Magento\Framework\Event\Observer $observer
62+
* @param Observer $observer
5263
* @return $this
5364
*/
54-
public function execute(\Magento\Framework\Event\Observer $observer)
65+
public function execute(Observer $observer)
5566
{
5667
$store = $observer->getEvent()->getStore();
5768
$result = $observer->getEvent()->getResult();
5869

59-
/* @var $quote \Magento\Quote\Model\Quote */
70+
/* @var $quote Quote */
6071
$quote = $observer->getEvent()->getQuote();
6172

6273
foreach ($quote->getAllItems() as $item) {
6374
if (($product = $item->getProduct())
64-
&& $product->getTypeId() == \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE
75+
&& $product->getTypeId() == Type::TYPE_DOWNLOADABLE
6576
) {
66-
if ($this->_scopeConfig->isSetFlag(
77+
if ($this->scopeConfig->isSetFlag(
6778
self::XML_PATH_DISABLE_GUEST_CHECKOUT,
6879
ScopeInterface::SCOPE_STORE,
6980
$store
70-
) || !$this->checkForShareableLinks($item)) {
81+
)
82+
|| !$this->checkForShareableLinks($item, $store)) {
7183
$result->setIsAllowed(false);
7284
break;
7385
}
7486
}
7587
}
88+
7689
return $this;
7790
}
7891

7992
/**
8093
* Check for shareable link
8194
*
82-
* @param \Magento\Quote\Api\Data\CartItemInterface $item
95+
* @param CartItemInterface $item
96+
* @param int $store
8397
* @return boolean
8498
*/
85-
private function checkForShareableLinks($item)
99+
private function checkForShareableLinks(CartItemInterface $item, int $store): bool
86100
{
87101
$isSharable = true;
88102
$option = $item->getOptionByCode('downloadable_link_ids');
@@ -91,17 +105,19 @@ private function checkForShareableLinks($item)
91105
$links = $this->linksFactory->create()->addFieldToFilter("link_id", ["in" => $downloadableLinkIds]);
92106
foreach ($links as $link) {
93107
if (!$link->getIsShareable() ||
94-
($link->getIsShareable() == 2 && !$this->_scopeConfig->isSetFlag(
95-
self::XML_PATH_DOWNLOADABLE_SHAREABLE,
96-
ScopeInterface::SCOPE_STORE,
97-
$store
98-
)
108+
(
109+
$link->getIsShareable() == 2 && !$this->scopeConfig->isSetFlag(
110+
self::XML_PATH_DOWNLOADABLE_SHAREABLE,
111+
ScopeInterface::SCOPE_STORE,
112+
$store
113+
)
99114
)
100115
) {
101116
$isSharable = false;
102117
}
103118
}
104119
}
120+
105121
return $isSharable;
106122
}
107123
}

app/code/Magento/Downloadable/Test/Unit/Observer/IsAllowedGuestCheckoutObserverTest.php

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Downloadable\Test\Unit\Observer;
78

89
use Magento\Downloadable\Observer\IsAllowedGuestCheckoutObserver;
@@ -136,10 +137,10 @@ public function testIsAllowedGuestCheckoutConfigSetToTrue($productType, $isAllow
136137
->method('getQuote')
137138
->will($this->returnValue($quote));
138139

139-
$this->scopeConfig->expects($this->once())
140+
$this->scopeConfig->expects($this->any())
140141
->method('isSetFlag')
141142
->with(
142-
IsAllowedGuestCheckoutObserver::XML_PATH_DISABLE_GUEST_CHECKOUT,
143+
'catalog/downloadable/disable_guest_checkout',
143144
ScopeInterface::SCOPE_STORE,
144145
$this->storeMock
145146
)
@@ -168,33 +169,57 @@ public function dataProviderForTestisAllowedGuestCheckoutConfigSetToTrue()
168169

169170
public function testIsAllowedGuestCheckoutConfigSetToFalse()
170171
{
172+
$storeCode = 1;
173+
174+
$product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
175+
->disableOriginalConstructor()
176+
->setMethods(['getTypeId'])
177+
->getMock();
178+
179+
$product->expects($this->once())
180+
->method('getTypeId')
181+
->willReturn(Type::TYPE_DOWNLOADABLE);
182+
183+
$item = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item::class)
184+
->disableOriginalConstructor()
185+
->setMethods(['getProduct'])
186+
->getMock();
187+
188+
$item->expects($this->once())
189+
->method('getProduct')
190+
->willReturn($product);
191+
192+
$quote = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
193+
->disableOriginalConstructor()
194+
->setMethods(['getAllItems'])
195+
->getMock();
196+
197+
$quote->expects($this->once())
198+
->method('getAllItems')
199+
->willReturn([$item]);
200+
171201
$this->eventMock->expects($this->once())
172202
->method('getStore')
173-
->will($this->returnValue($this->storeMock));
203+
->willReturn($storeCode);
174204

175205
$this->eventMock->expects($this->once())
176206
->method('getResult')
177207
->will($this->returnValue($this->resultMock));
178208

179-
$this->scopeConfig->expects($this->at(0))
180-
->method('isSetFlag')
181-
->with(
182-
IsAllowedGuestCheckoutObserver::XML_PATH_DISABLE_GUEST_CHECKOUT,
183-
ScopeInterface::SCOPE_STORE,
184-
$this->storeMock
185-
)
186-
->willReturn(false);
209+
$this->eventMock->expects($this->once())
210+
->method('getQuote')
211+
->will($this->returnValue($quote));
187212

188-
$this->scopeConfig->expects($this->at(1))
213+
$this->scopeConfig->expects($this->once())
189214
->method('isSetFlag')
190215
->with(
191-
IsAllowedGuestCheckoutObserver::XML_PATH_DOWNLOADABLE_SHAREABLE,
216+
'catalog/downloadable/disable_guest_checkout',
192217
ScopeInterface::SCOPE_STORE,
193-
$this->storeMock
218+
$storeCode
194219
)
195-
->willReturn(true);
220+
->willReturn(false);
196221

197-
$this->observerMock->expects($this->exactly(2))
222+
$this->observerMock->expects($this->exactly(3))
198223
->method('getEvent')
199224
->will($this->returnValue($this->eventMock));
200225

0 commit comments

Comments
 (0)