Skip to content

Commit 45b2d44

Browse files
🔃 [Magento Community Engineering] Community Contributions - 2.4-develop
Accepted Community Pull Requests: - #29205: #29174 Add save_rewrites_history to categories via API (by @amenk) - #28177: Add collapsible nav in customer account theme blank (by @mrtuvn) - #30217: MFTF: Replace repetitive actions with Action Groups in AdminUpdateStoreGroupAcceptAlertAndVerifyStoreViewFormTest, AdminUpdateStoreGroupAndVerifyStoreViewFormTest and AdminUpdateStoreViewTest (by @kate-kyzyma) - #28676: BUG - Prevent Magento from Creating incorrect URLs for category/products when Store View has modified URL keys (by @BarnyShergold) - #30172: fixing arguments for method in getStatusByState (by @sarron93) - #30108: improve visual counter review with parentheses in blank theme (by @mrtuvn) - #27077: Update FlushCacheByTags to use after plugins instead of around plugins (by @navarr) - #27581: Refactor moveFileFromTmp to not use a static method call (by @dmanners) - #28516: Rewrote Magento\Framework\DB\Adapter\Pdo\Mysql->isTableExists (by @jonashrem) - #27939: magento/magento2#: Remove a redundant getMappedNums from a loop (by @atwixfirster) Fixed GitHub Issues: - #29174: Category API should support save_rewrites_history for consistency reasons (reported by @amenk) has been fixed in #29205 by @amenk in 2.4-develop branch Related commits: 1. 5307bad 2. 00176f5 3. 4c99f0d 4. f0ba72a 5. 0f983b2 6. 7f380b6 7. 3361e72 8. 79c54ae 9. 99da072 10. f61a27a 11. 25111f0 12. c7822ff 13. df69e82 14. 6fde4ba 15. 9f81b49 16. 9583212 17. 1ad4482 - #30240: [Issue] #29174 Add save_rewrites_history to categories via API (reported by @m2-assistant[bot]) has been fixed in #29205 by @amenk in 2.4-develop branch Related commits: 1. 5307bad 2. 00176f5 3. 4c99f0d 4. f0ba72a 5. 0f983b2 6. 7f380b6 7. 3361e72 8. 79c54ae 9. 99da072 10. f61a27a 11. 25111f0 12. c7822ff 13. df69e82 14. 6fde4ba 15. 9f81b49 16. 9583212 17. 1ad4482 - #30237: [Issue] Add collapsible nav in customer account theme blank (reported by @m2-assistant[bot]) has been fixed in #28177 by @mrtuvn in 2.4-develop branch Related commits: 1. b351d50 2. 2b9b090 - #28633: URL Regeneration Fails on Store-Level URL Keys - Products (reported by @BarnyShergold) has been fixed in #28676 by @BarnyShergold in 2.4-develop branch Related commits: 1. 3409af0 2. a6d1976 3. ac1ac06 4. e100822 5. d59d740 6. 0097625 7. 7ee8e09 8. 54457b4 9. aea1d8e 10. 5d9ce4d 11. 87a4fd9 12. ec3e6da 13. 7a6a740 14. 7f41733 15. 4b9f004 - #30173: [Issue] fixing arguments for method in getStatusByState (reported by @m2-assistant[bot]) has been fixed in #30172 by @sarron93 in 2.4-develop branch Related commits: 1. 238ce85 2. 0873cf1 - #29558: [Issue] Update FlushCacheByTags to use after plugins instead of around plugins (reported by @m2-assistant[bot]) has been fixed in #27077 by @navarr in 2.4-develop branch Related commits: 1. 86d8b9b 2. 46c09c6 3. 2ef99f9 4. e8c63a7 5. b7c9c19 6. 5734a6c 7. 13673b2 8. 6b5a983 - #29598: [Issue] Refactor moveFileFromTmp to not use a static method call (reported by @m2-assistant[bot]) has been fixed in #27581 by @dmanners in 2.4-develop branch Related commits: 1. c3d14d1 2. f2f88a4 3. 25a9bdf 4. 7a92462 5. b1676da 6. 69aa7bf 7. e4d45a8 8. 05abef2 9. 5c31521 10. 988f31b 11. 84b22c1 - #29662: [Issue] Rewrote Magento\Framework\DB\Adapter\Pdo\Mysql->isTableExists (reported by @m2-assistant[bot]) has been fixed in #28516 by @jonashrem in 2.4-develop branch Related commits: 1. 2d67fdd 2. c77a8f5 3. 4876d4e 4. 54a39d5 5. 94006bd 6. 582cff8 7. 67c7234 8. aad9480 9. b597c7a - #30031: [Issue] magento/magento2#: Remove a redundant getMappedNums from a loop (reported by @m2-assistant[bot]) has been fixed in #27939 by @atwixfirster in 2.4-develop branch Related commits: 1. 1c87648 2. cd9e834 3. c68c447 4. d2bc662 5. 862609f 6. 740ce6b 7. e113e37 8. 814cf2b 9. d240573 10. 1bb4814 11. 033401b
2 parents 51b53be + 85fd7f5 commit 45b2d44

File tree

28 files changed

+725
-113
lines changed

28 files changed

+725
-113
lines changed

app/code/Magento/Catalog/Model/ImageUploader.php

Lines changed: 44 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,114 +5,116 @@
55
*/
66
namespace Magento\Catalog\Model;
77

8+
use Magento\Framework\App\Filesystem\DirectoryList;
9+
use Magento\Framework\Exception\LocalizedException;
810
use Magento\Framework\File\Uploader;
11+
use Magento\Framework\App\ObjectManager;
12+
use Magento\Framework\File\Name;
13+
use Magento\Framework\Filesystem;
14+
use Magento\Framework\Filesystem\Directory\WriteInterface;
15+
use Magento\MediaStorage\Helper\File\Storage\Database;
16+
use Magento\MediaStorage\Model\File\UploaderFactory;
17+
use Magento\Store\Model\StoreManagerInterface;
18+
use Psr\Log\LoggerInterface;
919

1020
/**
1121
* Catalog image uploader
1222
*/
1323
class ImageUploader
1424
{
1525
/**
16-
* Core file storage database
17-
*
18-
* @var \Magento\MediaStorage\Helper\File\Storage\Database
26+
* @var Database
1927
*/
2028
protected $coreFileStorageDatabase;
2129

2230
/**
23-
* Media directory object (writable).
24-
*
25-
* @var \Magento\Framework\Filesystem\Directory\WriteInterface
31+
* @var WriteInterface
2632
*/
2733
protected $mediaDirectory;
2834

2935
/**
30-
* Uploader factory
31-
*
32-
* @var \Magento\MediaStorage\Model\File\UploaderFactory
36+
* @var UploaderFactory
3337
*/
3438
private $uploaderFactory;
3539

3640
/**
37-
* Store manager
38-
*
39-
* @var \Magento\Store\Model\StoreManagerInterface
41+
* @var StoreManagerInterface
4042
*/
4143
protected $storeManager;
4244

4345
/**
44-
* @var \Psr\Log\LoggerInterface
46+
* @var LoggerInterface
4547
*/
4648
protected $logger;
4749

4850
/**
49-
* Base tmp path
50-
*
5151
* @var string
5252
*/
5353
protected $baseTmpPath;
5454

5555
/**
56-
* Base path
57-
*
5856
* @var string
5957
*/
6058
protected $basePath;
6159

6260
/**
63-
* Allowed extensions
64-
*
6561
* @var string
6662
*/
6763
protected $allowedExtensions;
6864

6965
/**
70-
* List of allowed image mime types
71-
*
7266
* @var string[]
7367
*/
7468
private $allowedMimeTypes;
7569

7670
/**
77-
* ImageUploader constructor
71+
* @var Name
72+
*/
73+
private $fileNameLookup;
74+
75+
/**
76+
* ImageUploader constructor.
7877
*
79-
* @param \Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDatabase
80-
* @param \Magento\Framework\Filesystem $filesystem
81-
* @param \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory
82-
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
83-
* @param \Psr\Log\LoggerInterface $logger
78+
* @param Database $coreFileStorageDatabase
79+
* @param Filesystem $filesystem
80+
* @param UploaderFactory $uploaderFactory
81+
* @param StoreManagerInterface $storeManager
82+
* @param LoggerInterface $logger
8483
* @param string $baseTmpPath
8584
* @param string $basePath
8685
* @param string[] $allowedExtensions
8786
* @param string[] $allowedMimeTypes
87+
* @param Name|null $fileNameLookup
88+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
8889
*/
8990
public function __construct(
90-
\Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDatabase,
91-
\Magento\Framework\Filesystem $filesystem,
92-
\Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory,
93-
\Magento\Store\Model\StoreManagerInterface $storeManager,
94-
\Psr\Log\LoggerInterface $logger,
91+
Database $coreFileStorageDatabase,
92+
Filesystem $filesystem,
93+
UploaderFactory $uploaderFactory,
94+
StoreManagerInterface $storeManager,
95+
LoggerInterface $logger,
9596
$baseTmpPath,
9697
$basePath,
9798
$allowedExtensions,
98-
$allowedMimeTypes = []
99+
$allowedMimeTypes = [],
100+
Name $fileNameLookup = null
99101
) {
100102
$this->coreFileStorageDatabase = $coreFileStorageDatabase;
101-
$this->mediaDirectory = $filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
103+
$this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
102104
$this->uploaderFactory = $uploaderFactory;
103105
$this->storeManager = $storeManager;
104106
$this->logger = $logger;
105107
$this->baseTmpPath = $baseTmpPath;
106108
$this->basePath = $basePath;
107109
$this->allowedExtensions = $allowedExtensions;
108110
$this->allowedMimeTypes = $allowedMimeTypes;
111+
$this->fileNameLookup = $fileNameLookup ?? ObjectManager::getInstance()->get(Name::class);
109112
}
110113

111114
/**
112115
* Set base tmp path
113116
*
114117
* @param string $baseTmpPath
115-
*
116118
* @return void
117119
*/
118120
public function setBaseTmpPath($baseTmpPath)
@@ -124,7 +126,6 @@ public function setBaseTmpPath($baseTmpPath)
124126
* Set base path
125127
*
126128
* @param string $basePath
127-
*
128129
* @return void
129130
*/
130131
public function setBasePath($basePath)
@@ -136,7 +137,6 @@ public function setBasePath($basePath)
136137
* Set allowed extensions
137138
*
138139
* @param string[] $allowedExtensions
139-
*
140140
* @return void
141141
*/
142142
public function setAllowedExtensions($allowedExtensions)
@@ -179,7 +179,6 @@ public function getAllowedExtensions()
179179
*
180180
* @param string $path
181181
* @param string $imageName
182-
*
183182
* @return string
184183
*/
185184
public function getFilePath($path, $imageName)
@@ -194,7 +193,7 @@ public function getFilePath($path, $imageName)
194193
* @param bool $returnRelativePath
195194
* @return string
196195
*
197-
* @throws \Magento\Framework\Exception\LocalizedException
196+
* @throws LocalizedException
198197
*/
199198
public function moveFileFromTmp($imageName, $returnRelativePath = false)
200199
{
@@ -203,7 +202,7 @@ public function moveFileFromTmp($imageName, $returnRelativePath = false)
203202

204203
$baseImagePath = $this->getFilePath(
205204
$basePath,
206-
Uploader::getNewFileName(
205+
$this->fileNameLookup->getNewFileName(
207206
$this->mediaDirectory->getAbsolutePath(
208207
$this->getFilePath($basePath, $imageName)
209208
)
@@ -222,10 +221,7 @@ public function moveFileFromTmp($imageName, $returnRelativePath = false)
222221
);
223222
} catch (\Exception $e) {
224223
$this->logger->critical($e);
225-
throw new \Magento\Framework\Exception\LocalizedException(
226-
__('Something went wrong while saving the file(s).'),
227-
$e
228-
);
224+
throw new LocalizedException(__('Something went wrong while saving the file(s).'), $e);
229225
}
230226

231227
return $returnRelativePath ? $baseImagePath : $imageName;
@@ -235,10 +231,9 @@ public function moveFileFromTmp($imageName, $returnRelativePath = false)
235231
* Checking file for save and save it to tmp dir
236232
*
237233
* @param string $fileId
238-
*
239234
* @return string[]
240235
*
241-
* @throws \Magento\Framework\Exception\LocalizedException
236+
* @throws LocalizedException
242237
*/
243238
public function saveFileToTmpDir($fileId)
244239
{
@@ -249,15 +244,13 @@ public function saveFileToTmpDir($fileId)
249244
$uploader->setAllowedExtensions($this->getAllowedExtensions());
250245
$uploader->setAllowRenameFiles(true);
251246
if (!$uploader->checkMimeType($this->allowedMimeTypes)) {
252-
throw new \Magento\Framework\Exception\LocalizedException(__('File validation failed.'));
247+
throw new LocalizedException(__('File validation failed.'));
253248
}
254249
$result = $uploader->save($this->mediaDirectory->getAbsolutePath($baseTmpPath));
255250
unset($result['path']);
256251

257252
if (!$result) {
258-
throw new \Magento\Framework\Exception\LocalizedException(
259-
__('File can not be saved to the destination folder.')
260-
);
253+
throw new LocalizedException(__('File can not be saved to the destination folder.'));
261254
}
262255

263256
/**
@@ -277,7 +270,7 @@ public function saveFileToTmpDir($fileId)
277270
$this->coreFileStorageDatabase->saveFile($relativePath);
278271
} catch (\Exception $e) {
279272
$this->logger->critical($e);
280-
throw new \Magento\Framework\Exception\LocalizedException(
273+
throw new LocalizedException(
281274
__('Something went wrong while saving the file(s).'),
282275
$e
283276
);

app/code/Magento/CatalogUrlRewrite/Model/ProductScopeRewriteGenerator.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\CatalogUrlRewrite\Model;
77

88
use Magento\Catalog\Api\CategoryRepositoryInterface;
9+
use Magento\Catalog\Api\Data\CategoryInterface;
910
use Magento\Catalog\Model\Category;
1011
use Magento\Catalog\Model\Product;
1112
use Magento\CatalogUrlRewrite\Model\Product\AnchorUrlRewriteGenerator;
@@ -15,12 +16,14 @@
1516
use Magento\CatalogUrlRewrite\Service\V1\StoreViewService;
1617
use Magento\Framework\App\Config\ScopeConfigInterface;
1718
use Magento\Framework\App\ObjectManager;
19+
use Magento\Framework\Exception\NoSuchEntityException;
1820
use Magento\Store\Model\Store;
1921
use Magento\Store\Model\StoreManagerInterface;
2022
use Magento\UrlRewrite\Model\MergeDataProviderFactory;
2123

2224
/**
23-
* Class ProductScopeRewriteGenerator
25+
* Generates Product/Category URLs for different scopes
26+
*
2427
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2528
*/
2629
class ProductScopeRewriteGenerator
@@ -174,7 +177,6 @@ public function generateForSpecificStoreView($storeId, $productCategories, Produ
174177
continue;
175178
}
176179

177-
// category should be loaded per appropriate store if category's URL key has been changed
178180
$categories[] = $this->getCategoryWithOverriddenUrlKey($storeId, $category);
179181
}
180182

@@ -240,9 +242,15 @@ public function isCategoryProperForGenerating(Category $category, $storeId)
240242
* Checks if URL key has been changed for provided category and returns reloaded category,
241243
* in other case - returns provided category.
242244
*
245+
* Category should be loaded per appropriate store at all times. This is because whilst the URL key on the
246+
* category in focus might be unchanged, parent category URL keys might be. If the category store ID
247+
* and passed store ID are the same then return current category as it is correct but may have changed in memory
248+
*
243249
* @param int $storeId
244250
* @param Category $category
245-
* @return Category
251+
*
252+
* @return CategoryInterface
253+
* @throws NoSuchEntityException
246254
*/
247255
private function getCategoryWithOverriddenUrlKey($storeId, Category $category)
248256
{
@@ -252,9 +260,10 @@ private function getCategoryWithOverriddenUrlKey($storeId, Category $category)
252260
Category::ENTITY
253261
);
254262

255-
if (!$isUrlKeyOverridden) {
263+
if (!$isUrlKeyOverridden && $storeId === $category->getStoreId()) {
256264
return $category;
257265
}
266+
258267
return $this->categoryRepository->get($category->getEntityId(), $storeId);
259268
}
260269

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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\CatalogUrlRewrite\Plugin\Model;
9+
10+
use Magento\Catalog\Model\Category;
11+
use Magento\Framework\Webapi\Rest\Request as RestRequest;
12+
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
13+
14+
class CategorySetSaveRewriteHistory
15+
{
16+
private const SAVE_REWRITES_HISTORY = 'save_rewrites_history';
17+
18+
/**
19+
* @var RestRequest
20+
*/
21+
private $request;
22+
23+
/**
24+
* @param RestRequest $request
25+
*/
26+
public function __construct(RestRequest $request)
27+
{
28+
$this->request = $request;
29+
}
30+
31+
/**
32+
* Add 'save_rewrites_history' param to the category for list
33+
*
34+
* @param CategoryUrlRewriteGenerator $subject
35+
* @param Category $category
36+
* @param bool $overrideStoreUrls
37+
* @param int|null $rootCategoryId
38+
* @return array
39+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
40+
*/
41+
public function beforeGenerate(
42+
CategoryUrlRewriteGenerator $subject,
43+
Category $category,
44+
bool $overrideStoreUrls = false,
45+
?int $rootCategoryId = null
46+
) {
47+
$requestBodyParams = $this->request->getBodyParams();
48+
49+
if ($this->isCustomAttributesExists($requestBodyParams, CategoryUrlRewriteGenerator::ENTITY_TYPE)) {
50+
foreach ($requestBodyParams[CategoryUrlRewriteGenerator::ENTITY_TYPE]['custom_attributes'] as $attribute) {
51+
if ($attribute['attribute_code'] === self::SAVE_REWRITES_HISTORY) {
52+
$category->setData(self::SAVE_REWRITES_HISTORY, (bool)$attribute['value']);
53+
}
54+
}
55+
}
56+
57+
return [$category, $overrideStoreUrls, $rootCategoryId];
58+
}
59+
60+
/**
61+
* Check is any custom options exists in data
62+
*
63+
* @param array $requestBodyParams
64+
* @param string $entityCode
65+
* @return bool
66+
*/
67+
private function isCustomAttributesExists(array $requestBodyParams, string $entityCode): bool
68+
{
69+
return !empty($requestBodyParams[$entityCode]['custom_attributes']);
70+
}
71+
}

0 commit comments

Comments
 (0)