Skip to content

Commit f74f65f

Browse files
committed
MAGETWO-66480: [GITHUB] URL key for specified store already exists #6671
- display original DB error in case conflicting URLs are not found
1 parent 4e3bc73 commit f74f65f

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,16 @@ protected function doReplace(array $urls)
121121
$urlConflicted[$urlFound[UrlRewriteData::URL_REWRITE_ID]] = $url->toArray();
122122
}
123123
}
124-
throw new \Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException(
125-
__('URL key for specified store already exists.'),
126-
$e,
127-
$e->getCode(),
128-
$urlConflicted
129-
);
124+
if ($urlConflicted) {
125+
throw new \Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException(
126+
__('URL key for specified store already exists.'),
127+
$e,
128+
$e->getCode(),
129+
$urlConflicted
130+
);
131+
} else {
132+
throw $e->getPrevious() ?: $e;
133+
}
130134
}
131135

132136
return $urls;
@@ -149,7 +153,8 @@ protected function insertMultiple($data)
149153
&& preg_match('#SQLSTATE\[23000\]: [^:]+: 1062[^\d]#', $e->getMessage())
150154
) {
151155
throw new \Magento\Framework\Exception\AlreadyExistsException(
152-
__('URL key for specified store already exists.')
156+
__('URL key for specified store already exists.'),
157+
$e
153158
);
154159
}
155160
throw $e;

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,40 @@ public function testReplace()
246246
/**
247247
* @expectedException \Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException
248248
*/
249-
public function testReplaceIfThrewDuplicateEntryException()
249+
public function testReplaceIfThrewExceptionOnDuplicateUrl()
250+
{
251+
$url = $this->getMock(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class, [], [], '', false);
252+
253+
$url->expects($this->any())
254+
->method('toArray')
255+
->will($this->returnValue(['row1']));
256+
257+
$this->connectionMock->expects($this->once())
258+
->method('insertMultiple')
259+
->will(
260+
$this->throwException(
261+
new \Exception('SQLSTATE[23000]: test: 1062 test', DbStorage::ERROR_CODE_DUPLICATE_ENTRY)
262+
)
263+
);
264+
$conflictingUrl = [
265+
UrlRewrite::URL_REWRITE_ID => 'conflicting-url'
266+
];
267+
$this->connectionMock->expects($this->any())
268+
->method('fetchRow')
269+
->willReturn($conflictingUrl);
270+
271+
$this->storage->replace([$url]);
272+
}
273+
274+
/**
275+
* Validates a case when DB errors on duplicate entry, but calculated URLs are not really duplicated
276+
*
277+
* An example is when URL length exceeds length of the DB field, so URLs are trimmed and become conflicting
278+
*
279+
* @expectedException \Exception
280+
* @expectedExceptionMessage SQLSTATE[23000]: test: 1062 test
281+
*/
282+
public function testReplaceIfThrewExceptionOnDuplicateEntry()
250283
{
251284
$url = $this->getMock(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class, [], [], '', false);
252285

0 commit comments

Comments
 (0)