Skip to content

Commit 484c853

Browse files
committed
MC-14824: Sample links minor changes
1 parent 0dac78e commit 484c853

File tree

5 files changed

+147
-8
lines changed

5 files changed

+147
-8
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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\Catalog\Model\Product;
9+
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Store\Model\StoreManagerInterface;
12+
13+
/**
14+
* Class to check that product is saleable.
15+
*/
16+
class SalabilityChecker
17+
{
18+
/**
19+
* @var ProductRepositoryInterface
20+
*/
21+
private $productRepository;
22+
23+
/**
24+
* @var StoreManagerInterface
25+
*/
26+
private $storeManager;
27+
28+
/**
29+
* @param ProductRepositoryInterface $productRepository
30+
* @param StoreManagerInterface $storeManager
31+
*/
32+
public function __construct(
33+
ProductRepositoryInterface $productRepository,
34+
StoreManagerInterface $storeManager
35+
) {
36+
$this->productRepository = $productRepository;
37+
$this->storeManager = $storeManager;
38+
}
39+
40+
/**
41+
* Check if product is salable.
42+
*
43+
* @param int|string $productId
44+
* @param int|null $storeId
45+
* @return bool
46+
*/
47+
public function isSalable($productId, $storeId = null): bool
48+
{
49+
if ($storeId === null) {
50+
$storeId = $this->storeManager->getStore()->getId();
51+
}
52+
/** @var \Magento\Catalog\Model\Product $product */
53+
$product = $this->productRepository->getById($productId, false, $storeId);
54+
55+
return $product->isSalable();
56+
}
57+
}

app/code/Magento/Downloadable/Controller/Download/LinkSample.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
namespace Magento\Downloadable\Controller\Download;
99

10+
use Magento\Catalog\Model\Product\SalabilityChecker;
1011
use Magento\Downloadable\Helper\Download as DownloadHelper;
12+
use Magento\Framework\App\Action\Context;
1113
use Magento\Framework\App\ResponseInterface;
1214

1315
/**
@@ -18,7 +20,24 @@
1820
class LinkSample extends \Magento\Downloadable\Controller\Download
1921
{
2022
/**
21-
* Download link's sample action
23+
* @var SalabilityChecker
24+
*/
25+
private $salabilityChecker;
26+
27+
/**
28+
* @param Context $context
29+
* @param SalabilityChecker|null $salabilityChecker
30+
*/
31+
public function __construct(
32+
Context $context,
33+
SalabilityChecker $salabilityChecker = null
34+
) {
35+
parent::__construct($context);
36+
$this->salabilityChecker = $salabilityChecker ?: $this->_objectManager->get(SalabilityChecker::class);
37+
}
38+
39+
/**
40+
* Download link's sample action.
2241
*
2342
* @return ResponseInterface
2443
*/
@@ -27,7 +46,7 @@ public function execute()
2746
$linkId = $this->getRequest()->getParam('link_id', 0);
2847
/** @var \Magento\Downloadable\Model\Link $link */
2948
$link = $this->_objectManager->create(\Magento\Downloadable\Model\Link::class)->load($linkId);
30-
if ($link->getId()) {
49+
if ($link->getId() && $this->salabilityChecker->isSalable($link->getProductId())) {
3150
$resource = '';
3251
$resourceType = '';
3352
if ($link->getSampleType() == DownloadHelper::LINK_TYPE_URL) {
@@ -52,6 +71,7 @@ public function execute()
5271
);
5372
}
5473
}
74+
5575
return $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
5676
}
5777
}

app/code/Magento/Downloadable/Controller/Download/Sample.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
namespace Magento\Downloadable\Controller\Download;
99

10+
use Magento\Catalog\Model\Product\SalabilityChecker;
1011
use Magento\Downloadable\Helper\Download as DownloadHelper;
12+
use Magento\Framework\App\Action\Context;
1113
use Magento\Framework\App\ResponseInterface;
1214

1315
/**
@@ -18,7 +20,24 @@
1820
class Sample extends \Magento\Downloadable\Controller\Download
1921
{
2022
/**
21-
* Download sample action
23+
* @var SalabilityChecker
24+
*/
25+
private $salabilityChecker;
26+
27+
/**
28+
* @param Context $context
29+
* @param SalabilityChecker|null $salabilityChecker
30+
*/
31+
public function __construct(
32+
Context $context,
33+
SalabilityChecker $salabilityChecker = null
34+
) {
35+
parent::__construct($context);
36+
$this->salabilityChecker = $salabilityChecker ?: $this->_objectManager->get(SalabilityChecker::class);
37+
}
38+
39+
/**
40+
* Download sample action.
2241
*
2342
* @return ResponseInterface
2443
*/
@@ -27,7 +46,7 @@ public function execute()
2746
$sampleId = $this->getRequest()->getParam('sample_id', 0);
2847
/** @var \Magento\Downloadable\Model\Sample $sample */
2948
$sample = $this->_objectManager->create(\Magento\Downloadable\Model\Sample::class)->load($sampleId);
30-
if ($sample->getId()) {
49+
if ($sample->getId() && $this->salabilityChecker->isSalable($sample->getProductId())) {
3150
$resource = '';
3251
$resourceType = '';
3352
if ($sample->getSampleType() == DownloadHelper::LINK_TYPE_URL) {
@@ -49,6 +68,7 @@ public function execute()
4968
);
5069
}
5170
}
71+
5272
return $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
5373
}
5474
}

app/code/Magento/Downloadable/Test/Unit/Controller/Download/LinkSampleTest.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
99

1010
/**
11+
* Unit tests for \Magento\Downloadable\Controller\Download\LinkSample.
12+
*
1113
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1214
*/
1315
class LinkSampleTest extends \PHPUnit\Framework\TestCase
@@ -63,6 +65,11 @@ class LinkSampleTest extends \PHPUnit\Framework\TestCase
6365
*/
6466
protected $urlInterface;
6567

68+
/**
69+
* @var \Magento\Catalog\Model\Product\SalabilityChecker|\PHPUnit_Framework_MockObject_MockObject
70+
*/
71+
private $salabilityCheckerMock;
72+
6673
/**
6774
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
6875
*/
@@ -104,6 +111,7 @@ protected function setUp()
104111
$this->messageManager = $this->createMock(\Magento\Framework\Message\ManagerInterface::class);
105112
$this->redirect = $this->createMock(\Magento\Framework\App\Response\RedirectInterface::class);
106113
$this->urlInterface = $this->createMock(\Magento\Framework\UrlInterface::class);
114+
$this->salabilityCheckerMock = $this->createMock(\Magento\Catalog\Model\Product\SalabilityChecker::class);
107115
$this->objectManager = $this->createPartialMock(\Magento\Framework\ObjectManager\ObjectManager::class, [
108116
'create',
109117
'get'
@@ -115,11 +123,17 @@ protected function setUp()
115123
'request' => $this->request,
116124
'response' => $this->response,
117125
'messageManager' => $this->messageManager,
118-
'redirect' => $this->redirect
126+
'redirect' => $this->redirect,
127+
'salabilityChecker' => $this->salabilityCheckerMock,
119128
]
120129
);
121130
}
122131

132+
/**
133+
* Execute Download link's sample action with Url link.
134+
*
135+
* @return void
136+
*/
123137
public function testExecuteLinkTypeUrl()
124138
{
125139
$linkMock = $this->getMockBuilder(\Magento\Downloadable\Model\Link::class)
@@ -134,6 +148,7 @@ public function testExecuteLinkTypeUrl()
134148
->willReturn($linkMock);
135149
$linkMock->expects($this->once())->method('load')->with('some_link_id')->willReturnSelf();
136150
$linkMock->expects($this->once())->method('getId')->willReturn('some_link_id');
151+
$this->salabilityCheckerMock->expects($this->once())->method('isSalable')->willReturn(true);
137152
$linkMock->expects($this->once())->method('getSampleType')->willReturn(
138153
\Magento\Downloadable\Helper\Download::LINK_TYPE_URL
139154
);
@@ -155,6 +170,11 @@ public function testExecuteLinkTypeUrl()
155170
$this->assertEquals($this->response, $this->linkSample->execute());
156171
}
157172

173+
/**
174+
* Execute Download link's sample action with File link.
175+
*
176+
* @return void
177+
*/
158178
public function testExecuteLinkTypeFile()
159179
{
160180
$linkMock = $this->getMockBuilder(\Magento\Downloadable\Model\Link::class)
@@ -173,6 +193,7 @@ public function testExecuteLinkTypeFile()
173193
->willReturn($linkMock);
174194
$linkMock->expects($this->once())->method('load')->with('some_link_id')->willReturnSelf();
175195
$linkMock->expects($this->once())->method('getId')->willReturn('some_link_id');
196+
$this->salabilityCheckerMock->expects($this->once())->method('isSalable')->willReturn(true);
176197
$linkMock->expects($this->any())->method('getSampleType')->willReturn(
177198
\Magento\Downloadable\Helper\Download::LINK_TYPE_FILE
178199
);

app/code/Magento/Downloadable/Test/Unit/Controller/Download/SampleTest.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
99

1010
/**
11+
* Unit tests for \Magento\Downloadable\Controller\Download\Sample.
12+
*
1113
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1214
*/
1315
class SampleTest extends \PHPUnit\Framework\TestCase
@@ -63,6 +65,11 @@ class SampleTest extends \PHPUnit\Framework\TestCase
6365
*/
6466
protected $urlInterface;
6567

68+
/**
69+
* @var \Magento\Catalog\Model\Product\SalabilityChecker|\PHPUnit_Framework_MockObject_MockObject
70+
*/
71+
private $salabilityCheckerMock;
72+
6673
/**
6774
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
6875
*/
@@ -104,6 +111,7 @@ protected function setUp()
104111
$this->messageManager = $this->createMock(\Magento\Framework\Message\ManagerInterface::class);
105112
$this->redirect = $this->createMock(\Magento\Framework\App\Response\RedirectInterface::class);
106113
$this->urlInterface = $this->createMock(\Magento\Framework\UrlInterface::class);
114+
$this->salabilityCheckerMock = $this->createMock(\Magento\Catalog\Model\Product\SalabilityChecker::class);
107115
$this->objectManager = $this->createPartialMock(\Magento\Framework\ObjectManager\ObjectManager::class, [
108116
'create',
109117
'get'
@@ -115,12 +123,18 @@ protected function setUp()
115123
'request' => $this->request,
116124
'response' => $this->response,
117125
'messageManager' => $this->messageManager,
118-
'redirect' => $this->redirect
126+
'redirect' => $this->redirect,
127+
'salabilityChecker' => $this->salabilityCheckerMock,
119128
]
120129
);
121130
}
122131

123-
public function testExecuteLinkTypeUrl()
132+
/**
133+
* Execute Download sample action with Sample Url.
134+
*
135+
* @return void
136+
*/
137+
public function testExecuteSampleWithUrlType()
124138
{
125139
$sampleMock = $this->getMockBuilder(\Magento\Downloadable\Model\Sample::class)
126140
->disableOriginalConstructor()
@@ -134,6 +148,7 @@ public function testExecuteLinkTypeUrl()
134148
->willReturn($sampleMock);
135149
$sampleMock->expects($this->once())->method('load')->with('some_sample_id')->willReturnSelf();
136150
$sampleMock->expects($this->once())->method('getId')->willReturn('some_link_id');
151+
$this->salabilityCheckerMock->expects($this->once())->method('isSalable')->willReturn(true);
137152
$sampleMock->expects($this->once())->method('getSampleType')->willReturn(
138153
\Magento\Downloadable\Helper\Download::LINK_TYPE_URL
139154
);
@@ -155,7 +170,12 @@ public function testExecuteLinkTypeUrl()
155170
$this->assertEquals($this->response, $this->sample->execute());
156171
}
157172

158-
public function testExecuteLinkTypeFile()
173+
/**
174+
* Execute Download sample action with Sample File.
175+
*
176+
* @return void
177+
*/
178+
public function testExecuteSampleWithFileType()
159179
{
160180
$sampleMock = $this->getMockBuilder(\Magento\Downloadable\Model\Sample::class)
161181
->disableOriginalConstructor()
@@ -173,6 +193,7 @@ public function testExecuteLinkTypeFile()
173193
->willReturn($sampleMock);
174194
$sampleMock->expects($this->once())->method('load')->with('some_sample_id')->willReturnSelf();
175195
$sampleMock->expects($this->once())->method('getId')->willReturn('some_sample_id');
196+
$this->salabilityCheckerMock->expects($this->once())->method('isSalable')->willReturn(true);
176197
$sampleMock->expects($this->any())->method('getSampleType')->willReturn(
177198
\Magento\Downloadable\Helper\Download::LINK_TYPE_FILE
178199
);

0 commit comments

Comments
 (0)