Skip to content

Commit 246a0fd

Browse files
merge magento/2.3-qwerty into magento-tsg/2.3-qwerty-pr49
2 parents 57f82b8 + ca06c47 commit 246a0fd

File tree

10 files changed

+240
-61
lines changed

10 files changed

+240
-61
lines changed

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

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ class ShippingInformationManagement implements \Magento\Checkout\Api\ShippingInf
5353

5454
/**
5555
* @var QuoteAddressValidator
56-
* @deprecated 100.2.0
5756
*/
5857
protected $addressValidator;
5958

@@ -152,35 +151,36 @@ public function saveAddressInformation(
152151
$cartId,
153152
\Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
154153
) {
155-
$address = $addressInformation->getShippingAddress();
156-
$billingAddress = $addressInformation->getBillingAddress();
157-
$carrierCode = $addressInformation->getShippingCarrierCode();
158-
$methodCode = $addressInformation->getShippingMethodCode();
154+
/** @var \Magento\Quote\Model\Quote $quote */
155+
$quote = $this->quoteRepository->getActive($cartId);
156+
$this->validateQuote($quote);
159157

158+
$address = $addressInformation->getShippingAddress();
159+
if (!$address || !$address->getCountryId()) {
160+
throw new StateException(__('The shipping address is missing. Set the address and try again.'));
161+
}
160162
if (!$address->getCustomerAddressId()) {
161163
$address->setCustomerAddressId(null);
162164
}
163165

164-
if ($billingAddress && !$billingAddress->getCustomerAddressId()) {
165-
$billingAddress->setCustomerAddressId(null);
166-
}
167-
168-
if (!$address->getCountryId()) {
169-
throw new StateException(__('The shipping address is missing. Set the address and try again.'));
170-
}
166+
try {
167+
$billingAddress = $addressInformation->getBillingAddress();
168+
if ($billingAddress) {
169+
if (!$billingAddress->getCustomerAddressId()) {
170+
$billingAddress->setCustomerAddressId(null);
171+
}
172+
$this->addressValidator->validateForCart($quote, $billingAddress);
173+
$quote->setBillingAddress($billingAddress);
174+
}
171175

172-
/** @var \Magento\Quote\Model\Quote $quote */
173-
$quote = $this->quoteRepository->getActive($cartId);
174-
$address->setLimitCarrier($carrierCode);
175-
$quote = $this->prepareShippingAssignment($quote, $address, $carrierCode . '_' . $methodCode);
176-
$this->validateQuote($quote);
177-
$quote->setIsMultiShipping(false);
176+
$this->addressValidator->validateForCart($quote, $address);
177+
$carrierCode = $addressInformation->getShippingCarrierCode();
178+
$address->setLimitCarrier($carrierCode);
179+
$methodCode = $addressInformation->getShippingMethodCode();
180+
$quote = $this->prepareShippingAssignment($quote, $address, $carrierCode . '_' . $methodCode);
178181

179-
if ($billingAddress) {
180-
$quote->setBillingAddress($billingAddress);
181-
}
182+
$quote->setIsMultiShipping(false);
182183

183-
try {
184184
$this->quoteRepository->save($quote);
185185
} catch (\Exception $e) {
186186
$this->logger->critical($e);

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

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ class ShippingInformationManagementTest extends \PHPUnit\Framework\TestCase
8282
*/
8383
private $shippingMock;
8484

85+
/**
86+
* @var \PHPUnit_Framework_MockObject_MockObject
87+
*/
88+
private $addressValidatorMock;
89+
8590
protected function setUp()
8691
{
8792
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
@@ -141,6 +146,9 @@ protected function setUp()
141146
$this->createPartialMock(\Magento\Quote\Api\Data\CartExtensionFactory::class, ['create']);
142147
$this->shippingFactoryMock =
143148
$this->createPartialMock(\Magento\Quote\Model\ShippingFactory::class, ['create']);
149+
$this->addressValidatorMock = $this->createMock(
150+
\Magento\Quote\Model\QuoteAddressValidator::class
151+
);
144152

145153
$this->model = $this->objectManager->getObject(
146154
\Magento\Checkout\Model\ShippingInformationManagement::class,
@@ -151,7 +159,8 @@ protected function setUp()
151159
'quoteRepository' => $this->quoteRepositoryMock,
152160
'shippingAssignmentFactory' => $this->shippingAssignmentFactoryMock,
153161
'cartExtensionFactory' => $this->cartExtensionFactoryMock,
154-
'shippingFactory' => $this->shippingFactoryMock
162+
'shippingFactory' => $this->shippingFactoryMock,
163+
'addressValidator' => $this->addressValidatorMock,
155164
]
156165
);
157166
}
@@ -163,22 +172,8 @@ protected function setUp()
163172
public function testSaveAddressInformationIfCartIsEmpty()
164173
{
165174
$cartId = 100;
166-
$carrierCode = 'carrier_code';
167-
$shippingMethod = 'shipping_method';
168175
$addressInformationMock = $this->createMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
169176

170-
$billingAddress = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
171-
$addressInformationMock->expects($this->once())
172-
->method('getShippingAddress')
173-
->willReturn($this->shippingAddressMock);
174-
$addressInformationMock->expects($this->once())->method('getBillingAddress')->willReturn($billingAddress);
175-
$addressInformationMock->expects($this->once())->method('getShippingCarrierCode')->willReturn($carrierCode);
176-
$addressInformationMock->expects($this->once())->method('getShippingMethodCode')->willReturn($shippingMethod);
177-
178-
$this->shippingAddressMock->expects($this->once())->method('getCountryId')->willReturn('USA');
179-
180-
$this->setShippingAssignmentsMocks($carrierCode . '_' . $shippingMethod);
181-
182177
$this->quoteMock->expects($this->once())->method('getItemsCount')->willReturn(0);
183178
$this->quoteRepositoryMock->expects($this->once())
184179
->method('getActive')
@@ -244,21 +239,19 @@ private function setShippingAssignmentsMocks($shippingMethod)
244239
public function testSaveAddressInformationIfShippingAddressNotSet()
245240
{
246241
$cartId = 100;
247-
$carrierCode = 'carrier_code';
248-
$shippingMethod = 'shipping_method';
249242
$addressInformationMock = $this->createMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
250-
251243
$addressInformationMock->expects($this->once())
252244
->method('getShippingAddress')
253245
->willReturn($this->shippingAddressMock);
254-
$addressInformationMock->expects($this->once())->method('getShippingCarrierCode')->willReturn($carrierCode);
255-
$addressInformationMock->expects($this->once())->method('getShippingMethodCode')->willReturn($shippingMethod);
256-
257-
$billingAddress = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
258-
$addressInformationMock->expects($this->once())->method('getBillingAddress')->willReturn($billingAddress);
259246

260247
$this->shippingAddressMock->expects($this->once())->method('getCountryId')->willReturn(null);
261248

249+
$this->quoteRepositoryMock->expects($this->once())
250+
->method('getActive')
251+
->with($cartId)
252+
->willReturn($this->quoteMock);
253+
$this->quoteMock->expects($this->once())->method('getItemsCount')->willReturn(100);
254+
262255
$this->model->saveAddressInformation($cartId, $addressInformationMock);
263256
}
264257

@@ -273,6 +266,9 @@ public function testSaveAddressInformationIfCanNotSaveQuote()
273266
$shippingMethod = 'shipping_method';
274267
$addressInformationMock = $this->createMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
275268

269+
$this->addressValidatorMock->expects($this->exactly(2))
270+
->method('validateForCart');
271+
276272
$this->quoteRepositoryMock->expects($this->once())
277273
->method('getActive')
278274
->with($cartId)
@@ -314,6 +310,9 @@ public function testSaveAddressInformationIfCarrierCodeIsInvalid()
314310
$shippingMethod = 'shipping_method';
315311
$addressInformationMock = $this->createMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
316312

313+
$this->addressValidatorMock->expects($this->exactly(2))
314+
->method('validateForCart');
315+
317316
$this->quoteRepositoryMock->expects($this->once())
318317
->method('getActive')
319318
->with($cartId)
@@ -355,6 +354,9 @@ public function testSaveAddressInformation()
355354
$shippingMethod = 'shipping_method';
356355
$addressInformationMock = $this->createMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
357356

357+
$this->addressValidatorMock->expects($this->exactly(2))
358+
->method('validateForCart');
359+
358360
$this->quoteRepositoryMock->expects($this->once())
359361
->method('getActive')
360362
->with($cartId)

app/code/Magento/Cms/Model/Wysiwyg/Images/Storage.php

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ public function getFilesCollection($path, $type = null)
328328
$item->setName($item->getBasename());
329329
$item->setShortName($this->_cmsWysiwygImages->getShortFilename($item->getBasename()));
330330
$item->setUrl($this->_cmsWysiwygImages->getCurrentUrl() . $item->getBasename());
331+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
331332
$item->setSize(filesize($item->getFilename()));
332333
$item->setMimeType(\mime_content_type($item->getFilename()));
333334

@@ -338,6 +339,7 @@ public function getFilesCollection($path, $type = null)
338339
$thumbUrl = $this->_backendUrl->getUrl('cms/*/thumbnail', ['file' => $item->getId()]);
339340
}
340341

342+
// phpcs:ignore Generic.PHP.NoSilencedErrors
341343
$size = @getimagesize($item->getFilename());
342344

343345
if (is_array($size)) {
@@ -413,6 +415,7 @@ public function createDirectory($name, $path)
413415
'id' => $this->_cmsWysiwygImages->convertPathToId($newPath),
414416
];
415417
return $result;
418+
// phpcs:ignore Magento2.Exceptions.ThrowCatch
416419
} catch (\Magento\Framework\Exception\FileSystemException $e) {
417420
throw new \Magento\Framework\Exception\LocalizedException(__('We cannot create a new directory.'));
418421
}
@@ -421,7 +424,7 @@ public function createDirectory($name, $path)
421424
/**
422425
* Recursively delete directory from storage
423426
*
424-
* @param string $path Target dir
427+
* @param string $path Absolute path to target directory
425428
* @return void
426429
* @throws \Magento\Framework\Exception\LocalizedException
427430
*/
@@ -430,12 +433,20 @@ public function deleteDirectory($path)
430433
if ($this->_coreFileStorageDb->checkDbUsage()) {
431434
$this->_directoryDatabaseFactory->create()->deleteDirectory($path);
432435
}
436+
if (!$this->isPathAllowed($path, $this->getConditionsForExcludeDirs())) {
437+
throw new \Magento\Framework\Exception\LocalizedException(
438+
__('We cannot delete directory %1.', $this->_getRelativePathToRoot($path))
439+
);
440+
}
433441
try {
434442
$this->_deleteByPath($path);
435443
$path = $this->getThumbnailRoot() . $this->_getRelativePathToRoot($path);
436444
$this->_deleteByPath($path);
445+
// phpcs:ignore Magento2.Exceptions.ThrowCatch
437446
} catch (\Magento\Framework\Exception\FileSystemException $e) {
438-
throw new \Magento\Framework\Exception\LocalizedException(__('We cannot delete directory %1.', $path));
447+
throw new \Magento\Framework\Exception\LocalizedException(
448+
__('We cannot delete directory %1.', $this->_getRelativePathToRoot($path))
449+
);
439450
}
440451
}
441452

@@ -482,13 +493,18 @@ public function deleteFile($target)
482493
/**
483494
* Upload and resize new file
484495
*
485-
* @param string $targetPath Target directory
496+
* @param string $targetPath Absolute path to target directory
486497
* @param string $type Type of storage, e.g. image, media etc.
487498
* @return array File info Array
488499
* @throws \Magento\Framework\Exception\LocalizedException
489500
*/
490501
public function uploadFile($targetPath, $type = null)
491502
{
503+
if (!$this->isPathAllowed($targetPath, $this->getConditionsForExcludeDirs())) {
504+
throw new \Magento\Framework\Exception\LocalizedException(
505+
__('We can\'t upload the file to current folder right now. Please try another folder.')
506+
);
507+
}
492508
/** @var \Magento\MediaStorage\Model\File\Uploader $uploader */
493509
$uploader = $this->_uploaderFactory->create(['fileId' => 'image']);
494510
$allowed = $this->getAllowedExtensions($type);
@@ -589,6 +605,7 @@ public function resizeFile($source, $keepRatio = true)
589605
$image->open($source);
590606
$image->keepAspectRatio($keepRatio);
591607
$image->resize($this->_resizeParameters['width'], $this->_resizeParameters['height']);
608+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
592609
$dest = $targetDir . '/' . pathinfo($source, PATHINFO_BASENAME);
593610
$image->save($dest);
594611
if ($this->_directory->isFile($this->_directory->getRelativePath($dest))) {
@@ -624,6 +641,7 @@ public function getThumbsPath($filePath = false)
624641
$thumbnailDir = $this->getThumbnailRoot();
625642

626643
if ($filePath && strpos($filePath, $mediaRootDir) === 0) {
644+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
627645
$thumbnailDir .= dirname(substr($filePath, strlen($mediaRootDir)));
628646
}
629647

@@ -674,6 +692,7 @@ public function isImage($filename)
674692
if (!$this->hasData('_image_extensions')) {
675693
$this->setData('_image_extensions', $this->getAllowedExtensions('image'));
676694
}
695+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
677696
$ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
678697
return in_array($ext, $this->_getData('_image_extensions'));
679698
}
@@ -784,4 +803,29 @@ private function getExtensionsList($type = null): array
784803

785804
return $allowed;
786805
}
806+
807+
/**
808+
* Check if path is not in excluded dirs.
809+
*
810+
* @param string $path Absolute path
811+
* @param array $conditions Exclude conditions
812+
* @return bool
813+
*/
814+
private function isPathAllowed($path, array $conditions): bool
815+
{
816+
$isAllowed = true;
817+
$regExp = $conditions['reg_exp'] ? '~' . implode('|', array_keys($conditions['reg_exp'])) . '~i' : null;
818+
$storageRoot = $this->_cmsWysiwygImages->getStorageRoot();
819+
$storageRootLength = strlen($storageRoot);
820+
821+
$mediaSubPathname = substr($path, $storageRootLength);
822+
$rootChildParts = explode('/', '/' . ltrim($mediaSubPathname, '/'));
823+
824+
if (array_key_exists($rootChildParts[1], $conditions['plain'])
825+
|| ($regExp && preg_match($regExp, $path))) {
826+
$isAllowed = false;
827+
}
828+
829+
return $isAllowed;
830+
}
787831
}

app/code/Magento/Cms/Test/Unit/Model/Wysiwyg/Images/StorageTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class StorageTest extends \PHPUnit\Framework\TestCase
1818
/**
1919
* Directory paths samples
2020
*/
21-
const STORAGE_ROOT_DIR = '/storage/root/dir';
21+
const STORAGE_ROOT_DIR = '/storage/root/dir/';
2222

2323
const INVALID_DIRECTORY_OVER_ROOT = '/storage/some/another/dir';
2424

@@ -437,10 +437,11 @@ protected function generalTestGetDirsCollection($path, $collectionArray = [], $e
437437

438438
public function testUploadFile()
439439
{
440-
$targetPath = '/target/path';
440+
$path = 'target/path';
441+
$targetPath = self::STORAGE_ROOT_DIR . $path;
441442
$fileName = 'image.gif';
442443
$realPath = $targetPath . '/' . $fileName;
443-
$thumbnailTargetPath = self::STORAGE_ROOT_DIR . '/.thumbs';
444+
$thumbnailTargetPath = self::STORAGE_ROOT_DIR . '/.thumbs' . $path;
444445
$thumbnailDestination = $thumbnailTargetPath . '/' . $fileName;
445446
$type = 'image';
446447
$result = [

app/code/Magento/Email/Model/Template/Filter.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ public function setDesignParams(array $designParams)
321321
}
322322

323323
/**
324+
* Get Css processor
325+
*
324326
* @deprecated 100.1.2
325327
* @return Css\Processor
326328
*/
@@ -333,6 +335,8 @@ private function getCssProcessor()
333335
}
334336

335337
/**
338+
* Get pub directory
339+
*
336340
* @deprecated 100.1.2
337341
* @param string $dirType
338342
* @return ReadInterface
@@ -523,6 +527,7 @@ public function mediaDirective($construction)
523527

524528
/**
525529
* Retrieve store URL directive
530+
*
526531
* Support url and direct_url properties
527532
*
528533
* @param string[] $construction
@@ -849,7 +854,7 @@ public function cssDirective($construction)
849854
return $css;
850855
} else {
851856
// Return CSS comment for debugging purposes
852-
return '/* ' . sprintf(__('Contents of %s could not be loaded or is empty'), $file) . ' */';
857+
return '/* ' . __('Contents of the specified CSS file could not be loaded or is empty') . ' */';
853858
}
854859
}
855860

@@ -958,6 +963,8 @@ public function getCssFilesContent(array $files)
958963
}
959964

960965
/**
966+
* Apply inline css
967+
*
961968
* Merge HTML and CSS and return HTML that has CSS styles applied "inline" to the HTML tags. This is necessary
962969
* in order to support all email clients.
963970
*

0 commit comments

Comments
 (0)