Skip to content

Commit ad5ba42

Browse files
committed
Merge remote-tracking branch 'origin/2.1.18-develop' into MAGETWO-97671
2 parents e64b3ef + 5cba861 commit ad5ba42

File tree

51 files changed

+1113
-720
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1113
-720
lines changed

app/code/Magento/Captcha/Model/DefaultModel.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
*/
66
namespace Magento\Captcha\Model;
77

8+
use Magento\Framework\Math\Random;
9+
810
/**
911
* Implementation of \Zend_Captcha
1012
*
13+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
14+
*
1115
* @author Magento Core Team <core@magentocommerce.com>
1216
*/
1317
class DefaultModel extends \Zend_Captcha_Image implements \Magento\Captcha\Model\CaptchaInterface
@@ -68,22 +72,30 @@ class DefaultModel extends \Zend_Captcha_Image implements \Magento\Captcha\Model
6872
*/
6973
protected $_session;
7074

75+
/**
76+
* @var Random
77+
*/
78+
private $randomMath;
79+
7180
/**
7281
* @param \Magento\Framework\Session\SessionManagerInterface $session
7382
* @param \Magento\Captcha\Helper\Data $captchaData
7483
* @param \Magento\Captcha\Model\ResourceModel\LogFactory $resLogFactory
7584
* @param string $formId
85+
* @param Random $randomMath
7686
*/
7787
public function __construct(
7888
\Magento\Framework\Session\SessionManagerInterface $session,
7989
\Magento\Captcha\Helper\Data $captchaData,
8090
\Magento\Captcha\Model\ResourceModel\LogFactory $resLogFactory,
81-
$formId
91+
$formId,
92+
Random $randomMath = null
8293
) {
8394
$this->_session = $session;
8495
$this->_captchaData = $captchaData;
8596
$this->_resLogFactory = $resLogFactory;
8697
$this->_formId = $formId;
98+
$this->randomMath = $randomMath ?: \Magento\Framework\App\ObjectManager::getInstance()->get(Random::class);
8799
}
88100

89101
/**
@@ -361,13 +373,9 @@ public function setShowCaptchaInSession($value = true)
361373
*/
362374
protected function _generateWord()
363375
{
364-
$word = '';
365-
$symbols = $this->_getSymbols();
376+
$symbols = (string)$this->_captchaData->getConfig('symbols');
366377
$wordLen = $this->_getWordLen();
367-
for ($i = 0; $i < $wordLen; $i++) {
368-
$word .= $symbols[array_rand($symbols)];
369-
}
370-
return $word;
378+
return $this->randomMath->getRandomString($wordLen, $symbols);
371379
}
372380

373381
/**

app/code/Magento/Captcha/Test/Unit/Model/DefaultTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,4 +376,41 @@ public function isShownToLoggedInUserDataProvider()
376376
[false, 'guest_checkout']
377377
];
378378
}
379+
380+
/**
381+
* @param string $string
382+
* @dataProvider generateWordProvider
383+
* @throws \ReflectionException
384+
*/
385+
public function testGenerateWord($string)
386+
{
387+
$randomMock = $this->getMock('Magento\Framework\Math\Random');
388+
$randomMock->expects($this->once())
389+
->method('getRandomString')
390+
->will($this->returnValue($string));
391+
392+
$captcha = new \Magento\Captcha\Model\DefaultModel(
393+
$this->session,
394+
$this->_getHelperStub(),
395+
$this->_resLogFactory,
396+
'user_create',
397+
$randomMock
398+
);
399+
400+
$method = new \ReflectionMethod($captcha, '_generateWord');
401+
$method->setAccessible(true);
402+
$this->assertEquals($string, $method->invoke($captcha));
403+
}
404+
405+
/**
406+
* @return array
407+
*/
408+
public function generateWordProvider()
409+
{
410+
return [
411+
['ABC123'],
412+
['1234567890'],
413+
['The quick brown fox jumps over the lazy dog.']
414+
];
415+
}
379416
}

app/code/Magento/Checkout/Model/ShippingInformationManagement.php

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use Magento\Framework\App\ObjectManager;
1919

2020
/**
21+
* Class ShippingInformationManagement
22+
*
2123
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2224
*/
2325
class ShippingInformationManagement implements \Magento\Checkout\Api\ShippingInformationManagementInterface
@@ -149,31 +151,32 @@ public function saveAddressInformation(
149151
$cartId,
150152
\Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
151153
) {
152-
$address = $addressInformation->getShippingAddress();
153-
$billingAddress = $addressInformation->getBillingAddress();
154-
$carrierCode = $addressInformation->getShippingCarrierCode();
155-
$methodCode = $addressInformation->getShippingMethodCode();
156-
157-
if (!$address->getCustomerAddressId()) {
158-
$address->setCustomerAddressId(null);
159-
}
160-
161-
if (!$address->getCountryId()) {
162-
throw new StateException(__('Shipping address is not set'));
163-
}
164-
165154
/** @var \Magento\Quote\Model\Quote $quote */
166155
$quote = $this->quoteRepository->getActive($cartId);
167-
$address->setLimitCarrier($carrierCode);
168-
$quote = $this->prepareShippingAssignment($quote, $address, $carrierCode . '_' . $methodCode);
169156
$this->validateQuote($quote);
170-
$quote->setIsMultiShipping(false);
171157

172-
if ($billingAddress) {
173-
$quote->setBillingAddress($billingAddress);
158+
$address = $addressInformation->getShippingAddress();
159+
if (!$address || !$address->getCountryId()) {
160+
throw new StateException(__('Shipping address is not set'));
161+
}
162+
if (!$address->getCustomerAddressId()) {
163+
$address->setCustomerAddressId(null);
174164
}
175165

176166
try {
167+
$billingAddress = $addressInformation->getBillingAddress();
168+
if ($billingAddress) {
169+
$this->addressValidator->validateForCart($quote, $billingAddress);
170+
$quote->setBillingAddress($billingAddress);
171+
}
172+
173+
$this->addressValidator->validateForCart($quote, $address);
174+
$carrierCode = $addressInformation->getShippingCarrierCode();
175+
$address->setLimitCarrier($carrierCode);
176+
$methodCode = $addressInformation->getShippingMethodCode();
177+
$quote = $this->prepareShippingAssignment($quote, $address, $carrierCode . '_' . $methodCode);
178+
$quote->setIsMultiShipping(false);
179+
177180
$this->quoteRepository->save($quote);
178181
} catch (\Exception $e) {
179182
$this->logger->critical($e);
@@ -211,6 +214,8 @@ protected function validateQuote(\Magento\Quote\Model\Quote $quote)
211214
}
212215

213216
/**
217+
* Prepare shipping assignment.
218+
*
214219
* @param CartInterface $quote
215220
* @param AddressInterface $address
216221
* @param string $method

app/code/Magento/Checkout/Test/Unit/Model/ShippingInformationManagementTest.php

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\Checkout\Test\Unit\Model;
77

8+
use Magento\Quote\Model\QuoteAddressValidator;
9+
810
/**
911
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1012
* @SuppressWarnings(PHPMD.TooManyFields)
@@ -149,6 +151,7 @@ protected function setUp()
149151
$this->getMock(\Magento\Quote\Api\Data\CartExtensionFactory::class, ['create'], [], '', false);
150152
$this->shippingFactoryMock =
151153
$this->getMock(\Magento\Quote\Model\ShippingFactory::class, ['create'], [], '', false);
154+
$this->addressValidatorMock = $this->getMock(QuoteAddressValidator::class, [], [], '', false);
152155

153156
$this->model = $this->objectManager->getObject(
154157
\Magento\Checkout\Model\ShippingInformationManagement::class,
@@ -157,6 +160,7 @@ protected function setUp()
157160
'paymentDetailsFactory' => $this->paymentDetailsFactoryMock,
158161
'cartTotalsRepository' => $this->cartTotalsRepositoryMock,
159162
'quoteRepository' => $this->quoteRepositoryMock,
163+
'addressValidator' => $this->addressValidatorMock,
160164
]
161165
);
162166
$this->objectManager->setBackwardCompatibleProperty(
@@ -183,22 +187,8 @@ protected function setUp()
183187
public function testSaveAddressInformationIfCartIsEmpty()
184188
{
185189
$cartId = 100;
186-
$carrierCode = 'carrier_code';
187-
$shippingMethod = 'shipping_method';
188190
$addressInformationMock = $this->getMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
189191

190-
$billingAddress = $this->getMock(\Magento\Quote\Api\Data\AddressInterface::class);
191-
$addressInformationMock->expects($this->once())
192-
->method('getShippingAddress')
193-
->willReturn($this->shippingAddressMock);
194-
$addressInformationMock->expects($this->once())->method('getBillingAddress')->willReturn($billingAddress);
195-
$addressInformationMock->expects($this->once())->method('getShippingCarrierCode')->willReturn($carrierCode);
196-
$addressInformationMock->expects($this->once())->method('getShippingMethodCode')->willReturn($shippingMethod);
197-
198-
$this->shippingAddressMock->expects($this->once())->method('getCountryId')->willReturn('USA');
199-
200-
$this->setShippingAssignmentsMocks($carrierCode . '_' . $shippingMethod);
201-
202192
$this->quoteMock->expects($this->once())->method('getItemsCount')->willReturn(0);
203193
$this->quoteRepositoryMock->expects($this->once())
204194
->method('getActive')
@@ -271,21 +261,19 @@ private function setShippingAssignmentsMocks($shippingMethod)
271261
public function testSaveAddressInformationIfShippingAddressNotSet()
272262
{
273263
$cartId = 100;
274-
$carrierCode = 'carrier_code';
275-
$shippingMethod = 'shipping_method';
276264
$addressInformationMock = $this->getMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
277-
278265
$addressInformationMock->expects($this->once())
279266
->method('getShippingAddress')
280267
->willReturn($this->shippingAddressMock);
281-
$addressInformationMock->expects($this->once())->method('getShippingCarrierCode')->willReturn($carrierCode);
282-
$addressInformationMock->expects($this->once())->method('getShippingMethodCode')->willReturn($shippingMethod);
283-
284-
$billingAddress = $this->getMock(\Magento\Quote\Api\Data\AddressInterface::class);
285-
$addressInformationMock->expects($this->once())->method('getBillingAddress')->willReturn($billingAddress);
286268

287269
$this->shippingAddressMock->expects($this->once())->method('getCountryId')->willReturn(null);
288270

271+
$this->quoteRepositoryMock->expects($this->once())
272+
->method('getActive')
273+
->with($cartId)
274+
->willReturn($this->quoteMock);
275+
$this->quoteMock->expects($this->once())->method('getItemsCount')->willReturn(100);
276+
289277
$this->model->saveAddressInformation($cartId, $addressInformationMock);
290278
}
291279

@@ -300,6 +288,9 @@ public function testSaveAddressInformationIfCanNotSaveQuote()
300288
$shippingMethod = 'shipping_method';
301289
$addressInformationMock = $this->getMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
302290

291+
$this->addressValidatorMock->expects($this->exactly(2))
292+
->method('validateForCart');
293+
303294
$this->quoteRepositoryMock->expects($this->once())
304295
->method('getActive')
305296
->with($cartId)
@@ -341,6 +332,9 @@ public function testSaveAddressInformationIfCarrierCodeIsInvalid()
341332
$shippingMethod = 'shipping_method';
342333
$addressInformationMock = $this->getMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
343334

335+
$this->addressValidatorMock->expects($this->exactly(2))
336+
->method('validateForCart');
337+
344338
$this->quoteRepositoryMock->expects($this->once())
345339
->method('getActive')
346340
->with($cartId)
@@ -382,6 +376,9 @@ public function testSaveAddressInformation()
382376
$shippingMethod = 'shipping_method';
383377
$addressInformationMock = $this->getMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
384378

379+
$this->addressValidatorMock->expects($this->exactly(2))
380+
->method('validateForCart');
381+
385382
$this->quoteRepositoryMock->expects($this->once())
386383
->method('getActive')
387384
->with($cartId)

app/code/Magento/Cms/Helper/Wysiwyg/Images.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ class Images extends \Magento\Framework\App\Helper\AbstractHelper
2727
protected $_currentUrl;
2828

2929
/**
30-
* Currenty selected store ID if applicable
30+
* Currently selected store ID if applicable
3131
*
3232
* @var int
3333
*/
34-
protected $_storeId = null;
34+
protected $_storeId;
3535

3636
/**
3737
* @var \Magento\Framework\Filesystem\Directory\Write
@@ -71,7 +71,7 @@ public function __construct(
7171
$this->_storeManager = $storeManager;
7272

7373
$this->_directory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
74-
$this->_directory->create(\Magento\Cms\Model\Wysiwyg\Config::IMAGE_DIRECTORY);
74+
$this->_directory->create($this->getStorageRoot());
7575
}
7676

7777
/**
@@ -93,7 +93,17 @@ public function setStoreId($store)
9393
*/
9494
public function getStorageRoot()
9595
{
96-
return $this->_directory->getAbsolutePath(\Magento\Cms\Model\Wysiwyg\Config::IMAGE_DIRECTORY);
96+
return $this->_directory->getAbsolutePath($this->getStorageRootSubpath());
97+
}
98+
99+
/**
100+
* Get image storage root subpath. User is unable to traverse outside of this subpath in media gallery
101+
*
102+
* @return string
103+
*/
104+
public function getStorageRootSubpath()
105+
{
106+
return '';
97107
}
98108

99109
/**
@@ -141,7 +151,7 @@ public function convertIdToPath($id)
141151
return $this->getStorageRoot();
142152
} else {
143153
$path = $this->getStorageRoot() . $this->idDecode($id);
144-
if (strpos($path, DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR) !== false) {
154+
if (preg_match('/\.\.(\\\|\/)/', $path)) {
145155
throw new \InvalidArgumentException('Path is invalid');
146156
}
147157

@@ -208,7 +218,7 @@ public function getImageHtmlDeclaration($filename, $renderAsTag = false)
208218
public function getCurrentPath()
209219
{
210220
if (!$this->_currentPath) {
211-
$currentPath = $this->_directory->getAbsolutePath() . \Magento\Cms\Model\Wysiwyg\Config::IMAGE_DIRECTORY;
221+
$currentPath = $this->getStorageRoot();
212222
$path = $this->_getRequest()->getParam($this->getTreeNodeName());
213223
if ($path) {
214224
$path = $this->convertIdToPath($path);
@@ -244,7 +254,7 @@ public function getCurrentUrl()
244254
)->getBaseUrl(
245255
\Magento\Framework\UrlInterface::URL_TYPE_MEDIA
246256
);
247-
$this->_currentUrl = $mediaUrl . $this->_directory->getRelativePath($path) . '/';
257+
$this->_currentUrl = rtrim($mediaUrl . $this->_directory->getRelativePath($path), '/') . '/';
248258
}
249259
return $this->_currentUrl;
250260
}

0 commit comments

Comments
 (0)