48 $this->urlRewriteFactory = $this->getMockBuilder(\
Magento\
UrlRewrite\Service\V1\Data\UrlRewriteFactory::class)
49 ->setMethods([
'create'])
50 ->disableOriginalConstructor()->getMock();
51 $this->dataObjectHelper = $this->createMock(\
Magento\Framework\Api\DataObjectHelper::class);
52 $this->connectionMock = $this->createMock(\
Magento\Framework\DB\Adapter\AdapterInterface::class);
53 $this->select = $this->getMockBuilder(Select::class)
54 ->disableOriginalConstructor()
56 $this->resource = $this->createMock(\
Magento\Framework\
App\ResourceConnection::class);
58 $this->resource->expects($this->any())
59 ->method(
'getConnection')
60 ->will($this->returnValue($this->connectionMock));
61 $this->connectionMock->expects($this->any())
63 ->will($this->returnValue($this->select));
68 'urlRewriteFactory' => $this->urlRewriteFactory,
69 'dataObjectHelper' => $this->dataObjectHelper,
70 'resource' => $this->resource,
77 $data = [
'col1' =>
'val1',
'col2' =>
'val2'];
79 $this->select->expects($this->at(1))
81 ->with(
'col1 IN (?)',
'val1');
83 $this->select->expects($this->at(2))
85 ->with(
'col2 IN (?)',
'val2');
87 $this->connectionMock->expects($this->any())
88 ->method(
'quoteIdentifier')
89 ->will($this->returnArgument(0));
91 $this->connectionMock->expects($this->once())
94 ->will($this->returnValue([[
'row1'], [
'row2']]));
96 $this->dataObjectHelper->expects($this->at(0))
97 ->method(
'populateWithArray')
98 ->with([
'urlRewrite1'], [
'row1'], \
Magento\
UrlRewrite\Service\V1\Data\UrlRewrite::class)
99 ->will($this->returnSelf());
101 $this->urlRewriteFactory->expects($this->at(0))
103 ->will($this->returnValue([
'urlRewrite1']));
105 $this->dataObjectHelper->expects($this->at(1))
106 ->method(
'populateWithArray')
107 ->with([
'urlRewrite2'], [
'row2'], \
Magento\
UrlRewrite\Service\V1\Data\UrlRewrite::class)
108 ->will($this->returnSelf());
110 $this->urlRewriteFactory->expects($this->at(1))
112 ->will($this->returnValue([
'urlRewrite2']));
114 $this->assertEquals([[
'urlRewrite1'], [
'urlRewrite2']], $this->storage->findAllByData(
$data));
119 $data = [
'col1' =>
'val1',
'col2' =>
'val2'];
121 $this->select->expects($this->at(1))
123 ->with(
'col1 IN (?)',
'val1');
125 $this->select->expects($this->at(2))
127 ->with(
'col2 IN (?)',
'val2');
129 $this->connectionMock->expects($this->any())
130 ->method(
'quoteIdentifier')
131 ->will($this->returnArgument(0));
133 $this->connectionMock->expects($this->once())
135 ->with($this->select)
136 ->will($this->returnValue([
'row1']));
138 $this->connectionMock->expects($this->never())->method(
'fetchAll');
140 $this->dataObjectHelper->expects($this->at(0))
141 ->method(
'populateWithArray')
142 ->with([
'urlRewrite1'], [
'row1'], \
Magento\
UrlRewrite\Service\V1\Data\UrlRewrite::class)
143 ->will($this->returnSelf());
145 $this->urlRewriteFactory->expects($this->at(0))
147 ->will($this->returnValue([
'urlRewrite1']));
149 $this->assertEquals([
'urlRewrite1'], $this->storage->findOneByData(
$data));
154 $origRequestPath =
'page-one';
161 $this->select->expects($this->at(1))
163 ->with(
'col1 IN (?)',
'val1');
165 $this->select->expects($this->at(2))
167 ->with(
'col2 IN (?)',
'val2');
169 $this->select->expects($this->at(3))
171 ->with(
'request_path IN (?)', [$origRequestPath, $origRequestPath .
'/']);
173 $this->connectionMock->expects($this->any())
174 ->method(
'quoteIdentifier')
175 ->will($this->returnArgument(0));
177 $this->connectionMock->expects($this->never())
178 ->method(
'fetchRow');
180 $urlRewriteRowInDb = [
185 $this->connectionMock->expects($this->once())
187 ->with($this->select)
188 ->will($this->returnValue([$urlRewriteRowInDb]));
190 $this->dataObjectHelper->expects($this->at(0))
191 ->method(
'populateWithArray')
192 ->with([
'urlRewrite1'], $urlRewriteRowInDb, \
Magento\
UrlRewrite\Service\V1\Data\UrlRewrite::class)
193 ->will($this->returnSelf());
195 $this->urlRewriteFactory->expects($this->at(0))
197 ->will($this->returnValue([
'urlRewrite1']));
199 $this->assertEquals([
'urlRewrite1'], $this->storage->findOneByData(
$data));
204 $origRequestPath =
'page-one';
211 $this->select->expects($this->at(1))
213 ->with(
'col1 IN (?)',
'val1');
215 $this->select->expects($this->at(2))
217 ->with(
'col2 IN (?)',
'val2');
219 $this->select->expects($this->at(3))
221 ->with(
'request_path IN (?)', [$origRequestPath, $origRequestPath .
'/']);
223 $this->connectionMock->expects($this->any())
224 ->method(
'quoteIdentifier')
225 ->will($this->returnArgument(0));
227 $this->connectionMock->expects($this->never())
228 ->method(
'fetchRow');
230 $urlRewriteRowInDb = [
236 $this->connectionMock->expects($this->once())
238 ->with($this->select)
239 ->will($this->returnValue([$urlRewriteRowInDb]));
241 $urlRewriteRedirect = [
242 'request_path' => $origRequestPath,
243 'redirect_type' => 301,
245 'entity_type' =>
'custom',
247 'target_path' => $origRequestPath .
'/',
248 'description' =>
null,
249 'is_autogenerated' =>
'0',
253 $this->dataObjectHelper->expects($this->at(0))
254 ->method(
'populateWithArray')
255 ->with([
'urlRewrite1'], $urlRewriteRedirect, \
Magento\
UrlRewrite\Service\V1\Data\UrlRewrite::class)
256 ->will($this->returnSelf());
258 $this->urlRewriteFactory->expects($this->at(0))
260 ->will($this->returnValue([
'urlRewrite1']));
262 $this->assertEquals([
'urlRewrite1'], $this->storage->findOneByData(
$data));
267 $origRequestPath =
'page-one/';
274 $this->select->expects($this->at(1))
276 ->with(
'col1 IN (?)',
'val1');
278 $this->select->expects($this->at(2))
280 ->with(
'col2 IN (?)',
'val2');
282 $this->select->expects($this->at(3))
284 ->with(
'request_path IN (?)', [rtrim($origRequestPath,
'/'), rtrim($origRequestPath,
'/') .
'/']);
286 $this->connectionMock->expects($this->any())
287 ->method(
'quoteIdentifier')
288 ->will($this->returnArgument(0));
290 $this->connectionMock->expects($this->never())
291 ->method(
'fetchRow');
293 $urlRewriteRowInDb = [
299 $this->connectionMock->expects($this->once())
301 ->with($this->select)
302 ->will($this->returnValue([$urlRewriteRowInDb]));
304 $urlRewriteRedirect = [
305 'request_path' => $origRequestPath,
306 'redirect_type' => 301,
308 'entity_type' =>
'custom',
310 'target_path' => rtrim($origRequestPath,
'/'),
311 'description' =>
null,
312 'is_autogenerated' =>
'0',
316 $this->dataObjectHelper->expects($this->at(0))
317 ->method(
'populateWithArray')
318 ->with([
'urlRewrite1'], $urlRewriteRedirect, \
Magento\
UrlRewrite\Service\V1\Data\UrlRewrite::class)
319 ->will($this->returnSelf());
321 $this->urlRewriteFactory->expects($this->at(0))
323 ->will($this->returnValue([
'urlRewrite1']));
325 $this->assertEquals([
'urlRewrite1'], $this->storage->findOneByData(
$data));
330 $origRequestPath =
'page-one';
337 $this->select->expects($this->at(1))
339 ->with(
'col1 IN (?)',
'val1');
341 $this->select->expects($this->at(2))
343 ->with(
'col2 IN (?)',
'val2');
345 $this->select->expects($this->at(3))
347 ->with(
'request_path IN (?)', [$origRequestPath, $origRequestPath .
'/']);
349 $this->connectionMock->expects($this->any())
350 ->method(
'quoteIdentifier')
351 ->will($this->returnArgument(0));
353 $this->connectionMock->expects($this->never())
354 ->method(
'fetchRow');
356 $urlRewriteRowInDb = [
363 $this->connectionMock->expects($this->once())
365 ->with($this->select)
366 ->will($this->returnValue([$urlRewriteRowInDb]));
368 $this->dataObjectHelper->expects($this->at(0))
369 ->method(
'populateWithArray')
370 ->with([
'urlRewrite1'], $urlRewriteRowInDb, \
Magento\
UrlRewrite\Service\V1\Data\UrlRewrite::class)
371 ->will($this->returnSelf());
373 $this->urlRewriteFactory->expects($this->at(0))
375 ->will($this->returnValue([
'urlRewrite1']));
377 $this->assertEquals([
'urlRewrite1'], $this->storage->findOneByData(
$data));
382 $origRequestPath =
'page-one';
389 $this->select->expects($this->at(1))
391 ->with(
'col1 IN (?)',
'val1');
393 $this->select->expects($this->at(2))
395 ->with(
'col2 IN (?)',
'val2');
397 $this->select->expects($this->at(3))
399 ->with(
'request_path IN (?)', [$origRequestPath, $origRequestPath .
'/']);
401 $this->connectionMock->expects($this->any())
402 ->method(
'quoteIdentifier')
403 ->will($this->returnArgument(0));
405 $this->connectionMock->expects($this->never())
406 ->method(
'fetchRow');
408 $urlRewriteRowInDb = [
415 $urlRewriteRowInDb2 = [
422 $this->connectionMock->expects($this->once())
424 ->with($this->select)
425 ->will($this->returnValue([$urlRewriteRowInDb, $urlRewriteRowInDb2]));
427 $this->dataObjectHelper->expects($this->at(0))
428 ->method(
'populateWithArray')
429 ->with([
'urlRewrite1'], $urlRewriteRowInDb2, \
Magento\
UrlRewrite\Service\V1\Data\UrlRewrite::class)
430 ->will($this->returnSelf());
432 $this->urlRewriteFactory->expects($this->at(0))
434 ->will($this->returnValue([
'urlRewrite1']));
436 $this->assertEquals([
'urlRewrite1'], $this->storage->findOneByData(
$data));
444 $urlFirst = $this->createMock(\
Magento\
UrlRewrite\Service\V1\Data\UrlRewrite::class);
445 $urlSecond = $this->createMock(\
Magento\
UrlRewrite\Service\V1\Data\UrlRewrite::class);
449 $urlFirst->expects($this->any())
450 ->method(
'getEntityType')
451 ->willReturn(
'product');
452 $urlFirst->expects($this->any())
453 ->method(
'getEntityId')
454 ->willReturn(
'entity_1');
455 $urlFirst->expects($this->any())
456 ->method(
'getStoreId')
457 ->willReturn(
'store_id_1');
459 $urlSecond->expects($this->any())
460 ->method(
'getEntityType')
461 ->willReturn(
'category');
462 $urlSecond->expects($this->any())
463 ->method(
'getEntityId')
464 ->willReturn(
'entity_2');
465 $urlSecond->expects($this->any())
466 ->method(
'getStoreId')
467 ->willReturn(
'store_id_2');
469 $this->connectionMock->expects($this->any())
470 ->method(
'quoteIdentifier')
471 ->will($this->returnArgument(0));
473 $this->select->expects($this->any())
474 ->method($this->anything())
477 $this->resource->expects($this->any())
478 ->method(
'getTableName')
480 ->will($this->returnValue(
'table_name'));
482 $this->connectionMock->expects($this->any())
484 ->with(
'sql delete query');
488 $urlFirst->expects($this->any())
490 ->will($this->returnValue([
'row1']));
491 $urlSecond->expects($this->any())
493 ->will($this->returnValue([
'row2']));
495 $this->resource->expects($this->any())
496 ->method(
'getTableName')
498 ->will($this->returnValue(
'table_name'));
500 $this->connectionMock->expects($this->once())
501 ->method(
'insertMultiple')
502 ->with(
'table_name', [[
'row1'], [
'row2']]);
504 $this->storage->replace([$urlFirst, $urlSecond]);
514 $url->expects($this->any())
516 ->will($this->returnValue([
'row1']));
518 $this->connectionMock->expects($this->once())
519 ->method(
'insertMultiple')
521 $this->throwException(
528 $this->connectionMock->expects($this->any())
530 ->willReturn($conflictingUrl);
532 $this->storage->replace([
$url]);
547 $url->expects($this->any())
549 ->will($this->returnValue([
'row1']));
551 $this->connectionMock->expects($this->once())
552 ->method(
'insertMultiple')
554 $this->throwException(
559 $this->storage->replace([
$url]);
569 $url->expects($this->any())
571 ->will($this->returnValue([
'row1']));
573 $this->connectionMock->expects($this->once())
574 ->method(
'insertMultiple')
575 ->will($this->throwException(
new \RuntimeException()));
577 $this->storage->replace([
$url]);
582 $data = [
'col1' =>
'val1',
'col2' =>
'val2'];
584 $this->connectionMock->expects($this->any())
585 ->method(
'quoteIdentifier')
586 ->will($this->returnArgument(0));
588 $this->select->expects($this->at(1))
590 ->with(
'col1 IN (?)',
'val1');
592 $this->select->expects($this->at(2))
594 ->with(
'col2 IN (?)',
'val2');
596 $this->select->expects($this->at(3))
597 ->method(
'deleteFromSelect')
599 ->will($this->returnValue(
'sql delete query'));
601 $this->resource->expects($this->any())
602 ->method(
'getTableName')
604 ->will($this->returnValue(
'table_name'));
606 $this->connectionMock->expects($this->once())
608 ->with(
'sql delete query');
610 $this->storage->deleteByData(
$data);
testFindOneByDataWithRequestPath()
testReplaceIfThrewCustomException()
testFindOneByDataWithRequestPathIsRedirect()
testFindOneByDataWithRequestPathTwoResults()
testFindOneByDataWithRequestPathIsDifferent2()
testReplaceIfThrewExceptionOnDuplicateEntry()
testReplaceIfThrewExceptionOnDuplicateUrl()
const ERROR_CODE_DUPLICATE_ENTRY
testFindOneByDataWithRequestPathIsDifferent()