Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.

Commit 62124e3

Browse files
Merge pull request #3676 from magento-qwerty/2.3-bugfixes-310119
Fixed issues: - MAGETWO-71993: CMS Image Upload Response Contains Redundant Info - MC-5964: Fixed incorrect behaviour of sync actions - MC-10866: Cart's customer and address mismatch - MC-5947: Email templates breaking rendering
2 parents 384000f + e58caf1 commit 62124e3

File tree

16 files changed

+528
-472
lines changed

16 files changed

+528
-472
lines changed

app/code/Magento/Catalog/Model/Product/ProductFrontendAction/Synchronizer.php

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use Magento\Framework\EntityManager\EntityManager;
1717

1818
/**
19+
* A Product Widget Synchronizer.
20+
*
1921
* Service which allows to sync product widget information, such as product id with db. In order to reuse this info
2022
* on different devices
2123
*/
@@ -85,9 +87,10 @@ public function __construct(
8587
}
8688

8789
/**
88-
* Find lifetime in configuration. Configuration is hold in Stores Configuration
89-
* Also this configuration is generated by:
90-
* @see \Magento\Catalog\Model\Widget\RecentlyViewedStorageConfiguration
90+
* Finds lifetime in configuration.
91+
*
92+
* Configuration is hold in Stores Configuration. Also this configuration is generated by
93+
* {@see Magento\Catalog\Model\Widget\RecentlyViewedStorageConfiguration}
9194
*
9295
* @param string $namespace
9396
* @return int
@@ -108,6 +111,8 @@ private function getLifeTimeByNamespace($namespace)
108111
}
109112

110113
/**
114+
* Filters actions.
115+
*
111116
* In order to avoid suspicious actions, we need to filter them in DESC order, and slice only items that
112117
* can be persisted in database.
113118
*
@@ -138,7 +143,9 @@ private function getProductIdsByActions(array $actions)
138143
$productIds = [];
139144

140145
foreach ($actions as $action) {
141-
$productIds[] = $action['product_id'];
146+
if (isset($action['product_id']) && is_int($action['product_id'])) {
147+
$productIds[] = $action['product_id'];
148+
}
142149
}
143150

144151
return $productIds;
@@ -159,33 +166,37 @@ public function syncActions(array $productsData, $typeId)
159166
$customerId = $this->session->getCustomerId();
160167
$visitorId = $this->visitor->getId();
161168
$collection = $this->getActionsByType($typeId);
162-
$collection->addFieldToFilter('product_id', $this->getProductIdsByActions($productsData));
163-
164-
/**
165-
* Note that collection is also filtered by visitor id and customer id
166-
* This collection shouldn't be flushed when visitor has products and then login
167-
* It can remove only products for visitor, or only products for customer
168-
*
169-
* ['product_id' => 'added_at']
170-
* @var ProductFrontendActionInterface $item
171-
*/
172-
foreach ($collection as $item) {
173-
$this->entityManager->delete($item);
174-
}
175-
176-
foreach ($productsData as $productId => $productData) {
177-
/** @var ProductFrontendActionInterface $action */
178-
$action = $this->productFrontendActionFactory->create([
179-
'data' => [
180-
'visitor_id' => $customerId ? null : $visitorId,
181-
'customer_id' => $this->session->getCustomerId(),
182-
'added_at' => $productData['added_at'],
183-
'product_id' => $productId,
184-
'type_id' => $typeId
185-
]
186-
]);
187-
188-
$this->entityManager->save($action);
169+
$productIds = $this->getProductIdsByActions($productsData);
170+
171+
if ($productIds) {
172+
$collection->addFieldToFilter('product_id', $productIds);
173+
174+
/**
175+
* Note that collection is also filtered by visitor id and customer id
176+
* This collection shouldn't be flushed when visitor has products and then login
177+
* It can remove only products for visitor, or only products for customer
178+
*
179+
* ['product_id' => 'added_at']
180+
* @var ProductFrontendActionInterface $item
181+
*/
182+
foreach ($collection as $item) {
183+
$this->entityManager->delete($item);
184+
}
185+
186+
foreach ($productsData as $productId => $productData) {
187+
/** @var ProductFrontendActionInterface $action */
188+
$action = $this->productFrontendActionFactory->create([
189+
'data' => [
190+
'visitor_id' => $customerId ? null : $visitorId,
191+
'customer_id' => $this->session->getCustomerId(),
192+
'added_at' => $productData['added_at'],
193+
'product_id' => $productId,
194+
'type_id' => $typeId
195+
]
196+
]);
197+
198+
$this->entityManager->save($action);
199+
}
189200
}
190201
}
191202

app/code/Magento/Cms/Controller/Adminhtml/Wysiwyg/Images/Upload.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
* Copyright © Magento, Inc. All rights reserved.
55
* See COPYING.txt for license details.
66
*/
7+
8+
declare(strict_types=1);
9+
710
namespace Magento\Cms\Controller\Adminhtml\Wysiwyg\Images;
811

912
use Magento\Framework\App\Action\HttpPostActionInterface;
@@ -58,13 +61,20 @@ public function execute()
5861
__('Directory %1 is not under storage root path.', $path)
5962
);
6063
}
61-
$result = $this->getStorage()->uploadFile($path, $this->getRequest()->getParam('type'));
64+
$uploaded = $this->getStorage()->uploadFile($path, $this->getRequest()->getParam('type'));
65+
$response = [
66+
'name' => $uploaded['name'],
67+
'type' => $uploaded['type'],
68+
'error' => $uploaded['error'],
69+
'size' => $uploaded['size'],
70+
'file' => $uploaded['file']
71+
];
6272
} catch (\Exception $e) {
63-
$result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
73+
$response = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
6474
}
6575
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
6676
$resultJson = $this->resultJsonFactory->create();
6777

68-
return $resultJson->setData($result);
78+
return $resultJson->setData($response);
6979
}
7080
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,24 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
7+
declare(strict_types=1);
8+
69
namespace Magento\Cms\Model\Wysiwyg\Images;
710

811
use Magento\Cms\Helper\Wysiwyg\Images;
912
use Magento\Framework\App\Filesystem\DirectoryList;
1013

1114
/**
12-
* Wysiwyg Images model
15+
* Wysiwyg Images model.
16+
*
17+
* Tightly connected with controllers responsible for managing files so it uses session and is (sort of) a part
18+
* of the presentation layer.
1319
*
1420
* @SuppressWarnings(PHPMD.LongVariable)
1521
* @SuppressWarnings(PHPMD.TooManyFields)
1622
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
23+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
1724
*
1825
* @api
1926
* @since 100.0.2

app/code/Magento/Quote/Model/Quote/Address/BillingAddressPersister.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
use Magento\Quote\Model\QuoteAddressValidator;
1313
use Magento\Customer\Api\AddressRepositoryInterface;
1414

15+
/**
16+
* Saves billing address for quotes.
17+
*/
1518
class BillingAddressPersister
1619
{
1720
/**
@@ -37,6 +40,8 @@ public function __construct(
3740
}
3841

3942
/**
43+
* Save address for billing.
44+
*
4045
* @param CartInterface $quote
4146
* @param AddressInterface $address
4247
* @param bool $useForShipping
@@ -47,7 +52,7 @@ public function __construct(
4752
public function save(CartInterface $quote, AddressInterface $address, $useForShipping = false)
4853
{
4954
/** @var \Magento\Quote\Model\Quote $quote */
50-
$this->addressValidator->validate($address);
55+
$this->addressValidator->validateForCart($quote, $address);
5156
$customerAddressId = $address->getCustomerAddressId();
5257
$shippingAddress = null;
5358
$addressData = [];

app/code/Magento/Quote/Model/QuoteAddressValidator.php

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
namespace Magento\Quote\Model;
77

88
use Magento\Framework\Exception\NoSuchEntityException;
9+
use Magento\Quote\Api\Data\AddressInterface;
10+
use Magento\Quote\Api\Data\CartInterface;
911

1012
/**
1113
* Quote shipping/billing address validator service.
1214
*
15+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
1316
*/
1417
class QuoteAddressValidator
1518
{
@@ -28,7 +31,7 @@ class QuoteAddressValidator
2831
protected $customerRepository;
2932

3033
/**
31-
* @var \Magento\Customer\Model\Session
34+
* @deprecated This class is not a part of HTML presentation layer and should not use sessions.
3235
*/
3336
protected $customerSession;
3437

@@ -50,44 +53,80 @@ public function __construct(
5053
}
5154

5255
/**
53-
* Validates the fields in a specified address data object.
56+
* Validate address.
5457
*
55-
* @param \Magento\Quote\Api\Data\AddressInterface $addressData The address data object.
56-
* @return bool
58+
* @param AddressInterface $address
59+
* @param int|null $customerId Cart belongs to
60+
* @return void
5761
* @throws \Magento\Framework\Exception\InputException The specified address belongs to another customer.
5862
* @throws \Magento\Framework\Exception\NoSuchEntityException The specified customer ID or address ID is not valid.
5963
*/
60-
public function validate(\Magento\Quote\Api\Data\AddressInterface $addressData)
64+
private function doValidate(AddressInterface $address, ?int $customerId): void
6165
{
6266
//validate customer id
63-
if ($addressData->getCustomerId()) {
64-
$customer = $this->customerRepository->getById($addressData->getCustomerId());
67+
if ($customerId) {
68+
$customer = $this->customerRepository->getById($customerId);
6569
if (!$customer->getId()) {
6670
throw new \Magento\Framework\Exception\NoSuchEntityException(
67-
__('Invalid customer id %1', $addressData->getCustomerId())
71+
__('Invalid customer id %1', $customerId)
6872
);
6973
}
7074
}
7175

72-
if ($addressData->getCustomerAddressId()) {
76+
if ($address->getCustomerAddressId()) {
77+
//Existing address cannot belong to a guest
78+
if (!$customerId) {
79+
throw new \Magento\Framework\Exception\NoSuchEntityException(
80+
__('Invalid customer address id %1', $address->getCustomerAddressId())
81+
);
82+
}
83+
//Validating address ID
7384
try {
74-
$this->addressRepository->getById($addressData->getCustomerAddressId());
85+
$this->addressRepository->getById($address->getCustomerAddressId());
7586
} catch (NoSuchEntityException $e) {
7687
throw new \Magento\Framework\Exception\NoSuchEntityException(
77-
__('Invalid address id %1', $addressData->getId())
88+
__('Invalid address id %1', $address->getId())
7889
);
7990
}
80-
91+
//Finding available customer's addresses
8192
$applicableAddressIds = array_map(function ($address) {
8293
/** @var \Magento\Customer\Api\Data\AddressInterface $address */
8394
return $address->getId();
84-
}, $this->customerRepository->getById($addressData->getCustomerId())->getAddresses());
85-
if (!in_array($addressData->getCustomerAddressId(), $applicableAddressIds)) {
95+
}, $this->customerRepository->getById($customerId)->getAddresses());
96+
if (!in_array($address->getCustomerAddressId(), $applicableAddressIds)) {
8697
throw new \Magento\Framework\Exception\NoSuchEntityException(
87-
__('Invalid customer address id %1', $addressData->getCustomerAddressId())
98+
__('Invalid customer address id %1', $address->getCustomerAddressId())
8899
);
89100
}
90101
}
102+
}
103+
104+
/**
105+
* Validates the fields in a specified address data object.
106+
*
107+
* @param \Magento\Quote\Api\Data\AddressInterface $addressData The address data object.
108+
* @return bool
109+
* @throws \Magento\Framework\Exception\InputException The specified address belongs to another customer.
110+
* @throws \Magento\Framework\Exception\NoSuchEntityException The specified customer ID or address ID is not valid.
111+
*/
112+
public function validate(AddressInterface $addressData)
113+
{
114+
$this->doValidate($addressData, $addressData->getCustomerId());
115+
91116
return true;
92117
}
118+
119+
/**
120+
* Validate address to be used for cart.
121+
*
122+
* @param CartInterface $cart
123+
* @param AddressInterface $address
124+
* @return void
125+
* @throws \Magento\Framework\Exception\InputException The specified address belongs to another customer.
126+
* @throws \Magento\Framework\Exception\NoSuchEntityException The specified customer ID or address ID is not valid.
127+
*/
128+
public function validateForCart(CartInterface $cart, AddressInterface $address): void
129+
{
130+
$this->doValidate($address, $cart->getCustomerIsGuest() ? null : $cart->getCustomer()->getId());
131+
}
93132
}

app/code/Magento/Quote/Model/ShippingAddressManagement.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function __construct(
7979
}
8080

8181
/**
82-
* {@inheritDoc}
82+
* @inheritDoc
8383
* @SuppressWarnings(PHPMD.NPathComplexity)
8484
*/
8585
public function assign($cartId, \Magento\Quote\Api\Data\AddressInterface $address)
@@ -95,7 +95,7 @@ public function assign($cartId, \Magento\Quote\Api\Data\AddressInterface $addres
9595
$saveInAddressBook = $address->getSaveInAddressBook() ? 1 : 0;
9696
$sameAsBilling = $address->getSameAsBilling() ? 1 : 0;
9797
$customerAddressId = $address->getCustomerAddressId();
98-
$this->addressValidator->validate($address);
98+
$this->addressValidator->validateForCart($quote, $address);
9999
$quote->setShippingAddress($address);
100100
$address = $quote->getShippingAddress();
101101

@@ -123,7 +123,7 @@ public function assign($cartId, \Magento\Quote\Api\Data\AddressInterface $addres
123123
}
124124

125125
/**
126-
* {@inheritDoc}
126+
* @inheritDoc
127127
*/
128128
public function get($cartId)
129129
{

0 commit comments

Comments
 (0)