Skip to content

Commit 089efd2

Browse files
committed
MAGETWO-98233: Fix incorrect samples behavior
1 parent e29f4c5 commit 089efd2

File tree

5 files changed

+211
-49
lines changed

5 files changed

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

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,35 @@
66
*/
77
namespace Magento\Downloadable\Controller\Download;
88

9+
use Magento\Catalog\Model\Product\SalabilityChecker;
910
use Magento\Downloadable\Helper\Download as DownloadHelper;
11+
use Magento\Framework\App\Action\Context;
1012
use Magento\Framework\App\ResponseInterface;
1113

14+
/**
15+
* Class LinkSample executes download sample link action.
16+
*/
1217
class LinkSample extends \Magento\Downloadable\Controller\Download
1318
{
1419
/**
15-
* Download link's sample action
20+
* @var SalabilityChecker
21+
*/
22+
private $salabilityChecker;
23+
24+
/**
25+
* @param Context $context
26+
* @param SalabilityChecker|null $salabilityChecker
27+
*/
28+
public function __construct(
29+
Context $context,
30+
SalabilityChecker $salabilityChecker = null
31+
) {
32+
parent::__construct($context);
33+
$this->salabilityChecker = $salabilityChecker ?: $this->_objectManager->get(SalabilityChecker::class);
34+
}
35+
36+
/**
37+
* Download link's sample action.
1638
*
1739
* @return ResponseInterface
1840
* @SuppressWarnings(PHPMD.ExitExpression)
@@ -21,16 +43,16 @@ public function execute()
2143
{
2244
$linkId = $this->getRequest()->getParam('link_id', 0);
2345
/** @var \Magento\Downloadable\Model\Link $link */
24-
$link = $this->_objectManager->create('Magento\Downloadable\Model\Link')->load($linkId);
25-
if ($link->getId()) {
46+
$link = $this->_objectManager->create(\Magento\Downloadable\Model\Link::class)->load($linkId);
47+
if ($link->getId() && $this->salabilityChecker->isSalable($link->getProductId())) {
2648
$resource = '';
2749
$resourceType = '';
2850
if ($link->getSampleType() == DownloadHelper::LINK_TYPE_URL) {
2951
$resource = $link->getSampleUrl();
3052
$resourceType = DownloadHelper::LINK_TYPE_URL;
3153
} elseif ($link->getSampleType() == DownloadHelper::LINK_TYPE_FILE) {
3254
$resource = $this->_objectManager->get(
33-
'Magento\Downloadable\Helper\File'
55+
\Magento\Downloadable\Helper\File::class
3456
)->getFilePath(
3557
$this->_getLink()->getBaseSamplePath(),
3658
$link->getSampleFile()
@@ -46,6 +68,7 @@ public function execute()
4668
);
4769
}
4870
}
71+
4972
return $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
5073
}
5174
}

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,35 @@
66
*/
77
namespace Magento\Downloadable\Controller\Download;
88

9+
use Magento\Catalog\Model\Product\SalabilityChecker;
910
use Magento\Downloadable\Helper\Download as DownloadHelper;
11+
use Magento\Framework\App\Action\Context;
1012
use Magento\Framework\App\ResponseInterface;
1113

14+
/**
15+
* Class Sample executes download sample action.
16+
*/
1217
class Sample extends \Magento\Downloadable\Controller\Download
1318
{
1419
/**
15-
* Download sample action
20+
* @var SalabilityChecker
21+
*/
22+
private $salabilityChecker;
23+
24+
/**
25+
* @param Context $context
26+
* @param SalabilityChecker|null $salabilityChecker
27+
*/
28+
public function __construct(
29+
Context $context,
30+
SalabilityChecker $salabilityChecker = null
31+
) {
32+
parent::__construct($context);
33+
$this->salabilityChecker = $salabilityChecker ?: $this->_objectManager->get(SalabilityChecker::class);
34+
}
35+
36+
/**
37+
* Download sample action.
1638
*
1739
* @return ResponseInterface
1840
* @SuppressWarnings(PHPMD.ExitExpression)
@@ -21,16 +43,16 @@ public function execute()
2143
{
2244
$sampleId = $this->getRequest()->getParam('sample_id', 0);
2345
/** @var \Magento\Downloadable\Model\Sample $sample */
24-
$sample = $this->_objectManager->create('Magento\Downloadable\Model\Sample')->load($sampleId);
25-
if ($sample->getId()) {
46+
$sample = $this->_objectManager->create(\Magento\Downloadable\Model\Sample::class)->load($sampleId);
47+
if ($sample->getId() && $this->salabilityChecker->isSalable($sample->getProductId())) {
2648
$resource = '';
2749
$resourceType = '';
2850
if ($sample->getSampleType() == DownloadHelper::LINK_TYPE_URL) {
2951
$resource = $sample->getSampleUrl();
3052
$resourceType = DownloadHelper::LINK_TYPE_URL;
3153
} elseif ($sample->getSampleType() == DownloadHelper::LINK_TYPE_FILE) {
3254
/** @var \Magento\Downloadable\Helper\File $helper */
33-
$helper = $this->_objectManager->get('Magento\Downloadable\Helper\File');
55+
$helper = $this->_objectManager->get(\Magento\Downloadable\Helper\File::class);
3456
$resource = $helper->getFilePath($sample->getBasePath(), $sample->getSampleFile());
3557
$resourceType = DownloadHelper::LINK_TYPE_FILE;
3658
}
@@ -43,6 +65,7 @@ public function execute()
4365
);
4466
}
4567
}
68+
4669
return $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
4770
}
4871
}

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

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
99

10+
/**
11+
* Unit tests for \Magento\Downloadable\Controller\Download\LinkSample.
12+
*
13+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
14+
*/
1015
class LinkSampleTest extends \PHPUnit_Framework_TestCase
1116
{
1217
/** @var \Magento\Downloadable\Controller\Download\LinkSample */
@@ -60,16 +65,21 @@ class LinkSampleTest extends \PHPUnit_Framework_TestCase
6065
*/
6166
protected $urlInterface;
6267

68+
/**
69+
* @var \Magento\Catalog\Model\Product\SalabilityChecker|\PHPUnit_Framework_MockObject_MockObject
70+
*/
71+
private $salabilityCheckerMock;
72+
6373
/**
6474
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
6575
*/
6676
protected function setUp()
6777
{
6878
$this->objectManagerHelper = new ObjectManagerHelper($this);
6979

70-
$this->request = $this->getMock('Magento\Framework\App\RequestInterface');
80+
$this->request = $this->getMock(\Magento\Framework\App\RequestInterface::class);
7181
$this->response = $this->getMock(
72-
'\Magento\Framework\App\ResponseInterface',
82+
\Magento\Framework\App\ResponseInterface::class,
7383
[
7484
'setHttpResponseCode',
7585
'clearBody',
@@ -81,7 +91,7 @@ protected function setUp()
8191
);
8292

8393
$this->helperData = $this->getMock(
84-
'Magento\Downloadable\Helper\Data',
94+
\Magento\Downloadable\Helper\Data::class,
8595
[
8696
'getIsShareable'
8797
],
@@ -90,7 +100,7 @@ protected function setUp()
90100
false
91101
);
92102
$this->downloadHelper = $this->getMock(
93-
'Magento\Downloadable\Helper\Download',
103+
\Magento\Downloadable\Helper\Download::class,
94104
[
95105
'setResource',
96106
'getFilename',
@@ -104,7 +114,7 @@ protected function setUp()
104114
false
105115
);
106116
$this->product = $this->getMock(
107-
'Magento\Catalog\Model\Product',
117+
\Magento\Catalog\Model\Product::class,
108118
[
109119
'_wakeup',
110120
'load',
@@ -117,28 +127,28 @@ protected function setUp()
117127
false
118128
);
119129
$this->messageManager = $this->getMock(
120-
'Magento\Framework\Message\ManagerInterface',
130+
\Magento\Framework\Message\ManagerInterface::class,
121131
[],
122132
[],
123133
'',
124134
false
125135
);
126136
$this->redirect = $this->getMock(
127-
'Magento\Framework\App\Response\RedirectInterface',
137+
\Magento\Framework\App\Response\RedirectInterface::class,
128138
[],
129139
[],
130140
'',
131141
false
132142
);
133143
$this->urlInterface = $this->getMock(
134-
'Magento\Framework\UrlInterface',
144+
\Magento\Framework\UrlInterface::class,
135145
[],
136146
[],
137147
'',
138148
false
139149
);
140150
$this->objectManager = $this->getMock(
141-
'\Magento\Framework\ObjectManager\ObjectManager',
151+
\Magento\Framework\ObjectManager\ObjectManager::class,
142152
[
143153
'create',
144154
'get'
@@ -147,39 +157,53 @@ protected function setUp()
147157
'',
148158
false
149159
);
160+
$this->salabilityCheckerMock = $this->getMock(
161+
\Magento\Catalog\Model\Product\SalabilityChecker::class,
162+
['isSalable'],
163+
[],
164+
'',
165+
false
166+
);
150167
$this->linkSample = $this->objectManagerHelper->getObject(
151-
'Magento\Downloadable\Controller\Download\LinkSample',
168+
\Magento\Downloadable\Controller\Download\LinkSample::class,
152169
[
153170
'objectManager' => $this->objectManager,
154171
'request' => $this->request,
155172
'response' => $this->response,
156173
'messageManager' => $this->messageManager,
157-
'redirect' => $this->redirect
174+
'redirect' => $this->redirect,
175+
'salabilityChecker' => $this->salabilityCheckerMock,
158176
]
159177
);
160178
}
161179

180+
/**
181+
* Execute Download link's sample action with Url link.
182+
*
183+
* @return void
184+
*/
162185
public function testExecuteLinkTypeUrl()
163186
{
164-
$linkMock = $this->getMockBuilder('Magento\Downloadable\Model\Link')
187+
$linkMock = $this->getMockBuilder(\Magento\Downloadable\Model\Link::class)
165188
->disableOriginalConstructor()
166189
->setMethods(['getId', 'load', 'getSampleType', 'getSampleUrl'])
167190
->getMock();
168191

169192
$this->request->expects($this->once())->method('getParam')->with('link_id', 0)->willReturn('some_link_id');
170193
$this->objectManager->expects($this->once())
171194
->method('create')
172-
->with('Magento\Downloadable\Model\Link')
195+
->with(\Magento\Downloadable\Model\Link::class)
173196
->willReturn($linkMock);
174197
$linkMock->expects($this->once())->method('load')->with('some_link_id')->willReturnSelf();
175198
$linkMock->expects($this->once())->method('getId')->willReturn('some_link_id');
199+
$this->salabilityCheckerMock->expects($this->once())->method('isSalable')->willReturn(true);
176200
$linkMock->expects($this->once())->method('getSampleType')->willReturn(
177201
\Magento\Downloadable\Helper\Download::LINK_TYPE_URL
178202
);
179203
$linkMock->expects($this->once())->method('getSampleUrl')->willReturn('sample_url');
180204
$this->objectManager->expects($this->at(1))
181205
->method('get')
182-
->with('Magento\Downloadable\Helper\Download')
206+
->with(\Magento\Downloadable\Helper\Download::class)
183207
->willReturn($this->downloadHelper);
184208
$this->response->expects($this->once())->method('setHttpResponseCode')->with(200)->willReturnSelf();
185209
$this->response->expects($this->any())->method('setHeader')->willReturnSelf();
@@ -194,39 +218,45 @@ public function testExecuteLinkTypeUrl()
194218
$this->assertEquals($this->response, $this->linkSample->execute());
195219
}
196220

221+
/**
222+
* Execute Download link's sample action with File link.
223+
*
224+
* @return void
225+
*/
197226
public function testExecuteLinkTypeFile()
198227
{
199-
$linkMock = $this->getMockBuilder('Magento\Downloadable\Model\Link')
228+
$linkMock = $this->getMockBuilder(\Magento\Downloadable\Model\Link::class)
200229
->disableOriginalConstructor()
201230
->setMethods(['getId', 'load', 'getSampleType', 'getSampleUrl', 'getBaseSamplePath'])
202231
->getMock();
203-
$fileMock = $this->getMockBuilder('Magento\Downloadable\Helper\File')
232+
$fileMock = $this->getMockBuilder(\Magento\Downloadable\Helper\File::class)
204233
->disableOriginalConstructor()
205234
->setMethods(['getFilePath', 'load', 'getSampleType', 'getSampleUrl'])
206235
->getMock();
207236

208237
$this->request->expects($this->once())->method('getParam')->with('link_id', 0)->willReturn('some_link_id');
209238
$this->objectManager->expects($this->at(0))
210239
->method('create')
211-
->with('Magento\Downloadable\Model\Link')
240+
->with(\Magento\Downloadable\Model\Link::class)
212241
->willReturn($linkMock);
213242
$linkMock->expects($this->once())->method('load')->with('some_link_id')->willReturnSelf();
214243
$linkMock->expects($this->once())->method('getId')->willReturn('some_link_id');
244+
$this->salabilityCheckerMock->expects($this->once())->method('isSalable')->willReturn(true);
215245
$linkMock->expects($this->any())->method('getSampleType')->willReturn(
216246
\Magento\Downloadable\Helper\Download::LINK_TYPE_FILE
217247
);
218248
$this->objectManager->expects($this->at(1))
219249
->method('get')
220-
->with('Magento\Downloadable\Helper\File')
250+
->with(\Magento\Downloadable\Helper\File::class)
221251
->willReturn($fileMock);
222252
$this->objectManager->expects($this->at(2))
223253
->method('get')
224-
->with('Magento\Downloadable\Model\Link')
254+
->with(\Magento\Downloadable\Model\Link::class)
225255
->willReturn($linkMock);
226256
$linkMock->expects($this->once())->method('getBaseSamplePath')->willReturn('downloadable/files/link_samples');
227257
$this->objectManager->expects($this->at(3))
228258
->method('get')
229-
->with('Magento\Downloadable\Helper\Download')
259+
->with(\Magento\Downloadable\Helper\Download::class)
230260
->willReturn($this->downloadHelper);
231261
$this->response->expects($this->once())->method('setHttpResponseCode')->with(200)->willReturnSelf();
232262
$this->response->expects($this->any())->method('setHeader')->willReturnSelf();

0 commit comments

Comments
 (0)