Skip to content

Commit b63e1c3

Browse files
ACPT-757
Fixing static test failures
1 parent 810402b commit b63e1c3

File tree

2 files changed

+39
-28
lines changed

2 files changed

+39
-28
lines changed

app/code/Magento/UrlRewrite/Model/Storage/DbStorage.php

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ class DbStorage extends AbstractStorage
4848
*/
4949
private $logger;
5050

51-
5251
/**
5352
* @var int
5453
*/
@@ -273,8 +272,8 @@ private function checkDuplicates(array $uniqueEntities): void
273272
foreach ($uniqueEntities as $storeId => $entityTypes) {
274273
$newRequestPaths = [];
275274
foreach ($entityTypes as $entityType => $entities) {
275+
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
276276
$requestPaths = array_merge(...$entities);
277-
$requestPathFilter = '';
278277
if (empty($requestPaths)) {
279278
continue;
280279
}
@@ -289,7 +288,7 @@ private function checkDuplicates(array $uniqueEntities): void
289288
. ') AND ' . $this->connection->quoteIdentifier(UrlRewrite::REQUEST_PATH)
290289
. ' IN (' . $this->connection->quote($requestPaths) . ')'
291290
);
292-
foreach($requestPaths as $requestPath) {
291+
foreach ($requestPaths as $requestPath) {
293292
if (isset($newRequestPaths[$requestPath])) {
294293
throw new \Magento\Framework\Exception\AlreadyExistsException();
295294
}
@@ -340,7 +339,7 @@ protected function doReplace(array $urls): array
340339
foreach ($urls as $url) {
341340
$data[] = $url->toArray();
342341
}
343-
for ($tries = 0; ; $tries++) {
342+
for ($tries = 0;; $tries++) {
344343
$this->connection->beginTransaction();
345344
try {
346345
$this->deleteOldUrls($uniqueEntities);
@@ -355,26 +354,7 @@ protected function doReplace(array $urls): array
355354
continue;
356355
} catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
357356
$this->connection->rollBack();
358-
/** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] $urlConflicted */
359-
$urlConflicted = [];
360-
foreach ($urls as $url) {
361-
$urlFound = $this->doFindOneByData(
362-
[
363-
UrlRewrite::REQUEST_PATH => $url->getRequestPath(),
364-
UrlRewrite::STORE_ID => $url->getStoreId(),
365-
]
366-
);
367-
if (isset($urlFound[UrlRewrite::URL_REWRITE_ID])) {
368-
if (isset($uniqueEntities
369-
[$urlFound[UrlRewrite::STORE_ID]]
370-
[$urlFound[UrlRewrite::ENTITY_TYPE]]
371-
[$urlFound[UrlRewrite::ENTITY_ID]
372-
])) {
373-
continue; // Note: If it's one of the entities we are updating, then it is okay.
374-
}
375-
$urlConflicted[$urlFound[UrlRewrite::URL_REWRITE_ID]] = $url->toArray();
376-
}
377-
}
357+
$urlConflicted = $this->findUrlConflicted($urls, $uniqueEntities);
378358
if ($urlConflicted) {
379359
throw new \Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException(
380360
__('URL key for specified store already exists.'),
@@ -389,8 +369,39 @@ protected function doReplace(array $urls): array
389369
$this->connection->rollBack();
390370
throw $e;
391371
}
392-
return $urls;
372+
break;
393373
}
374+
return $urls;
375+
}
376+
377+
/**
378+
* Searches existing rewrites with same requestPath & store, but ignores ones to be updated.
379+
*
380+
* @param array $urls
381+
* @return array
382+
*/
383+
private function findUrlConflicted(array $urls, array $uniqueEntities): array
384+
{
385+
$urlConflicted = [];
386+
foreach ($urls as $url) {
387+
$urlFound = $this->doFindOneByData(
388+
[
389+
UrlRewrite::REQUEST_PATH => $url->getRequestPath(),
390+
UrlRewrite::STORE_ID => $url->getStoreId(),
391+
]
392+
);
393+
if (isset($urlFound[UrlRewrite::URL_REWRITE_ID])) {
394+
if (isset($uniqueEntities
395+
[$urlFound[UrlRewrite::STORE_ID]]
396+
[$urlFound[UrlRewrite::ENTITY_TYPE]]
397+
[$urlFound[UrlRewrite::ENTITY_ID]
398+
])) {
399+
continue; // Note: If it's one of the entities we are updating, then it is okay.
400+
}
401+
$urlConflicted[$urlFound[UrlRewrite::URL_REWRITE_ID]] = $url->toArray();
402+
}
403+
}
404+
return $urlConflicted;
394405
}
395406

396407
/**
@@ -436,7 +447,7 @@ private function upsertMultiple(array $data): void
436447
* Get filter for url rows deletion due to provided urls
437448
*
438449
* @param UrlRewrite[] $urls
439-
* @return array
450+
* @return array~/tmp/system.log
440451
* @deprecated 101.0.3 Not used anymore.
441452
* @see nothing
442453
*/

app/code/Magento/UrlRewrite/Test/Unit/Model/Storage/DbStorageTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,9 +480,9 @@ public function testReplaceIfThrewExceptionOnDuplicateUrl(): void
480480
'target_path' => 'catalog/product/view/id/1',
481481
'redirect_type' => '0',
482482
'store_id' => '1',
483-
'description' => NULL,
483+
'description' => null,
484484
'is_autogenerated' => '1',
485-
'metadata' => NULL,
485+
'metadata' => null,
486486
]]);
487487
$this->connectionMock->method('fetchOne')->willReturnOnConsecutiveCalls(false, true);
488488
$this->storage->replace([$url]);

0 commit comments

Comments
 (0)