Skip to content

Commit 32cba04

Browse files
author
Alex Bomko
committed
MAGETWO-34597: [TD] Unit/Integration tests for UrlRewtites
1 parent fa98e6a commit 32cba04

File tree

3 files changed

+238
-54
lines changed

3 files changed

+238
-54
lines changed

src/Migration/Step/UrlRewrite/Version11410to2000.php

Lines changed: 140 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ class Version11410to2000 extends DatabaseStep implements \Migration\Step\StepInt
2121
*/
2222
protected $duplicateIndex;
2323

24+
/**
25+
* @var array
26+
*/
27+
protected $resolvedDuplicates = [];
28+
2429
/**
2530
* Resource of source
2631
*
@@ -199,8 +204,8 @@ public function run()
199204
$this->migrateRewrites($records, $destinationRecords);
200205
$this->destination->saveRecords($destinationDocument->getName(), $destinationRecords);
201206
}
202-
$this->copyEavData('catalog_category_entity_url_key', 'catalog_category_entity_varchar');
203-
$this->copyEavData('catalog_product_entity_url_key', 'catalog_product_entity_varchar');
207+
$this->copyEavData('catalog_category_entity_url_key', 'catalog_category_entity_varchar', 'category');
208+
$this->copyEavData('catalog_product_entity_url_key', 'catalog_product_entity_varchar', 'product');
204209
$this->progress->finish();
205210
}
206211

@@ -245,6 +250,7 @@ protected function migrateRewrites($source, $destination)
245250
$destinationRecord->setValue('redirect_type', 0);
246251
$destinationRecord->setValue('is_autogenerated', $sourceRecord->getValue('is_system'));
247252
$destinationRecord->setValue('metadata', '');
253+
$destinationRecord->setValue('redirect_type', $sourceRecord->getValue('redirect_type'));
248254
$destinationRecord->setValue('entity_type', $sourceRecord->getValue('entity_type'));
249255
$destinationRecord->setValue('request_path', $sourceRecord->getValue('request_path'));
250256

@@ -260,10 +266,11 @@ protected function migrateRewrites($source, $destination)
260266
$destinationRecord->setValue('entity_id', $productId);
261267
$targetPath = "catalog/product/view/id/$productId/category/$categoryId";
262268
} elseif (!empty($productId) && empty($categoryId)) {
263-
$destinationRecord->setValue('entity_type', 'category');
269+
$destinationRecord->setValue('entity_type', 'product');
264270
$destinationRecord->setValue('entity_id', $productId);
265271
$targetPath = 'catalog/product/view/id/' . $productId;
266272
} elseif (empty($productId) && !empty($categoryId)) {
273+
$destinationRecord->setValue('entity_type', 'category');
267274
$destinationRecord->setValue('entity_id', $categoryId);
268275
$targetPath = 'catalog/category/view/id/' . $categoryId;
269276
} else {
@@ -284,17 +291,24 @@ protected function migrateRewrites($source, $destination)
284291
}
285292
}
286293
if ($shouldResolve) {
287-
$destinationRecord->setValue(
288-
'request_path',
289-
$sourceRecord->getValue('request_path') . '-' . md5(mt_rand())
290-
);
291-
$message = 'Duplicate resolved. ' . sprintf(
292-
'Request path was: %s Target path was: %s Store ID: %s New request path: %s',
293-
$sourceRecord->getValue('request_path'),
294-
$sourceRecord->getValue('target_path'),
295-
$sourceRecord->getValue('store_id'),
296-
$destinationRecord->getValue('request_path')
294+
$key = md5(mt_rand());
295+
$requestPath = preg_replace(
296+
'/^(.*)\.([^\.]+)$/i',
297+
'$1-' . $key . '.$2',
298+
$sourceRecord->getValue('request_path')
297299
);
300+
$this->resolvedDuplicates[$destinationRecord->getValue('entity_type')]
301+
[$destinationRecord->getValue('entity_id')]
302+
[$sourceRecord->getValue('store_id')] = $key;
303+
$destinationRecord->setValue('request_path', $requestPath);
304+
$message = 'Duplicate resolved. '
305+
. sprintf(
306+
'Request path was: %s Target path was: %s Store ID: %s New request path: %s',
307+
$sourceRecord->getValue('request_path'),
308+
$sourceRecord->getValue('target_path'),
309+
$sourceRecord->getValue('store_id'),
310+
$destinationRecord->getValue('request_path')
311+
);
298312
$this->logger->addInfo($message);
299313
}
300314
}
@@ -312,7 +326,7 @@ protected function migrateRewrites($source, $destination)
312326
* @param string $destinationName
313327
* @return void
314328
*/
315-
protected function copyEavData($sourceName, $destinationName)
329+
protected function copyEavData($sourceName, $destinationName, $type)
316330
{
317331
$destinationDocument = $this->destination->getDocument($destinationName);
318332
$pageNumber = 0;
@@ -323,6 +337,21 @@ protected function copyEavData($sourceName, $destinationName)
323337
$this->progress->advance();
324338
unset($row['value_id']);
325339
unset($row['entity_type_id']);
340+
if (!empty($this->resolvedDuplicates[$type][$row['entity_id']][$row['store_id']])) {
341+
$row['value'] = $row['value'] . '-'
342+
. $this->resolvedDuplicates[$type][$row['entity_id']][$row['store_id']];
343+
} elseif (!empty($this->resolvedDuplicates[$type][$row['entity_id']]) && $row['store_id'] == 0) {
344+
foreach ($this->resolvedDuplicates[$type][$row['entity_id']] as $storeId => $urlKey) {
345+
$storeRow = $row;
346+
$storeRow['store_id'] = $storeId;
347+
$storeRow['value'] = $storeRow['value'] . '-' . $urlKey;
348+
$records->addRecord($this->recordFactory->create(['data' => $storeRow]));
349+
if (!isset($this->resolvedDuplicates[$destinationName])) {
350+
$this->resolvedDuplicates[$destinationName] = 0;
351+
}
352+
$this->resolvedDuplicates[$destinationName]++;;
353+
}
354+
}
326355
$records->addRecord($this->recordFactory->create(['data' => $row]));
327356
}
328357
$this->destination->saveRecords($destinationName, $records);
@@ -401,15 +430,23 @@ public function volumeCheck()
401430
$this->progress->start(1);
402431
$this->getRewritesSelect();
403432
$this->progress->advance();
433+
$categoryRecords = $this->source->getRecordsCount('catalog_category_entity_varchar')
434+
+ $this->source->getRecordsCount('catalog_category_entity_url_key')
435+
+ (isset($this->resolvedDuplicates['catalog_category_entity_varchar'])
436+
? $this->resolvedDuplicates['catalog_category_entity_varchar']
437+
: 0
438+
);
439+
$productRecords = $this->source->getRecordsCount('catalog_product_entity_varchar')
440+
+ $this->source->getRecordsCount('catalog_product_entity_url_key')
441+
+ (isset($this->resolvedDuplicates['catalog_product_entity_varchar'])
442+
? $this->resolvedDuplicates['catalog_product_entity_varchar']
443+
: 0
444+
);
404445

405446
$result = $this->source->getRecordsCount($this->tableName)
406447
== $this->destination->getRecordsCount('url_rewrite')
407-
&& $this->source->getRecordsCount('catalog_category_entity_varchar')
408-
+ $this->source->getRecordsCount('catalog_category_entity_url_key')
409-
== $this->destination->getRecordsCount('catalog_category_entity_varchar')
410-
&& $this->source->getRecordsCount('catalog_product_entity_varchar')
411-
+ $this->source->getRecordsCount('catalog_product_entity_url_key')
412-
== $this->destination->getRecordsCount('catalog_product_entity_varchar');
448+
&& $categoryRecords == $this->destination->getRecordsCount('catalog_category_entity_varchar')
449+
&& $productRecords == $this->destination->getRecordsCount('catalog_product_entity_varchar');
413450
$this->progress->finish();
414451
return $result;
415452
}
@@ -430,8 +467,8 @@ public function getTitle()
430467
protected function getIterationsCount()
431468
{
432469
return $this->source->getRecordsCount($this->tableName)
433-
+ $this->source->getRecordsCount('catalog_category_entity_url_key')
434-
+ $this->source->getRecordsCount('catalog_product_entity_url_key');
470+
+ $this->source->getRecordsCount('catalog_category_entity_url_key')
471+
+ $this->source->getRecordsCount('catalog_product_entity_url_key');
435472
}
436473

437474
/**
@@ -525,64 +562,109 @@ protected function getSuffix($suffixFor, $mainTable = 's')
525562
* Initialize temporary table and insert UrlRewrite data
526563
*
527564
* @codeCoverageIgnore
565+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
528566
* @return void
529567
*/
530568
protected function initTemporaryTable()
531569
{
532570
/** @var \Migration\Resource\Adapter\Mysql $adapter */
533571
$adapter = $this->source->getAdapter();
534572
$select = $adapter->getSelect();
535-
$select->getAdapter()->query('DROP TABLE IF EXISTS ' . $this->source->addDocumentPrefix($this->tableName));
536-
$select->getAdapter()->query(
537-
'CREATE TABLE ' . $this->source->addDocumentPrefix($this->tableName) . ' (
538-
id INT NOT NULL AUTO_INCREMENT,
539-
request_path VARCHAR(255) NOT NULL,
540-
target_path VARCHAR(255) NOT NULL,
541-
is_system SMALLINT NOT NULL DEFAULT 0,
542-
store_id INT NOT NULL,
543-
entity_type VARCHAR(32) NOT NULL,
544-
product_id INT NOT NULL DEFAULT 0,
545-
category_id INT NOT NULL DEFAULT 0,
546-
priority INT NOT NULL DEFAULT 0,
547-
PRIMARY KEY (id),
548-
UNIQUE (request_path, target_path, store_id, entity_type)
549-
)'
550-
);
573+
$select->getAdapter()->dropTable($this->source->addDocumentPrefix($this->tableName));
574+
/** @var \Magento\Framework\DB\Ddl\Table $table */
575+
$table = $select->getAdapter()->newTable($this->source->addDocumentPrefix($this->tableName))
576+
->addColumn(
577+
'id',
578+
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
579+
null,
580+
['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true]
581+
)
582+
->addColumn(
583+
'request_path',
584+
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
585+
255
586+
)
587+
->addColumn(
588+
'target_path',
589+
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
590+
255
591+
)
592+
->addColumn(
593+
'is_system',
594+
\Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
595+
null,
596+
['nullable' => false, 'default' => '0']
597+
)
598+
->addColumn(
599+
'store_id',
600+
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER
601+
)
602+
->addColumn(
603+
'entity_type',
604+
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
605+
32
606+
)
607+
->addColumn(
608+
'redirect_type',
609+
\Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
610+
null,
611+
['unsigned' => true, 'nullable' => false, 'default' => '0']
612+
)
613+
->addColumn(
614+
'product_id',
615+
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER
616+
)
617+
->addColumn(
618+
'category_id',
619+
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER
620+
)
621+
->addColumn(
622+
'priority',
623+
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER
624+
)
625+
->addIndex(
626+
'url_rewrite',
627+
['request_path', 'store_id', 'entity_type', 'target_path'],
628+
['type' => \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE]
629+
) ;
630+
$select->getAdapter()->createTable($table);
551631

552632
$select->from(
553-
['r' => $this->source->addDocumentPrefix('enterprise_url_rewrite_redirect')],
633+
['r' => $this->source->addDocumentPrefix('enterprise_url_rewrite')],
554634
[
555635
'id' => 'IFNULL(NULL, NULL)',
556-
'request_path' => 'r.identifier',
636+
'request_path' => 'r.request_path',
557637
'target_path' => 'r.target_path',
558-
'is_system' => "trim('0')",
638+
'is_system' => 'r.is_system',
559639
'store_id' => 'r.store_id',
560640
'entity_type' => "trim('custom')",
561-
'product_id' => "r.product_id",
562-
'category_id' => "r.category_id",
563-
'priority' => "trim('1')"
641+
'redirect_type' => "trim('0')",
642+
'product_id' => "trim('0')",
643+
'category_id' => "trim('0')",
644+
'priority' => "trim('2')"
564645
]
565646
);
566-
$query = $select->insertFromSelect($this->source->addDocumentPrefix($this->tableName));
647+
$query = $select->where('`r`.`entity_type` = 1')
648+
->insertFromSelect($this->source->addDocumentPrefix($this->tableName));
567649
$select->getAdapter()->query($query);
568650

569651
$select = $adapter->getSelect();
570652
$select->from(
571-
['r' => $this->source->addDocumentPrefix('enterprise_url_rewrite')],
653+
['r' => $this->source->addDocumentPrefix('enterprise_url_rewrite_redirect')],
572654
[
573655
'id' => 'IFNULL(NULL, NULL)',
574-
'request_path' => 'r.request_path',
656+
'request_path' => 'r.identifier',
575657
'target_path' => 'r.target_path',
576-
'is_system' => 'r.is_system',
658+
'is_system' => "trim('0')",
577659
'store_id' => 'r.store_id',
578660
'entity_type' => "trim('custom')",
579-
'product_id' => "trim('0')",
580-
'category_id' => "trim('0')",
581-
'priority' => "trim('2')"
661+
'redirect_type' => "(SELECT CASE r.options WHEN 'RP' THEN 301 WHEN 'R' THEN 302 ELSE 0 END)",
662+
'product_id' => "r.product_id",
663+
'category_id' => "r.category_id",
664+
'priority' => "trim('1')"
582665
]
583666
);
584-
$query = $select->where('`r`.`entity_type` = 1')
585-
->insertFromSelect($this->source->addDocumentPrefix($this->tableName));
667+
$query = $select->insertFromSelect($this->source->addDocumentPrefix($this->tableName));
586668
$select->getAdapter()->query($query);
587669

588670
$select = $adapter->getSelect();
@@ -595,6 +677,7 @@ protected function initTemporaryTable()
595677
'is_system' => 'r.is_system',
596678
'store_id' => 'r.store_id',
597679
'entity_type' => "trim('category')",
680+
'redirect_type' => "trim('0')",
598681
'product_id' => "trim('0')",
599682
'category_id' => "c.entity_id",
600683
'priority' => "trim('3')"
@@ -647,6 +730,7 @@ protected function initTemporaryTable()
647730
'is_system' => 'r.is_system',
648731
'store_id' => 's.store_id',
649732
'entity_type' => "trim('product')",
733+
'redirect_type' => "trim('0')",
650734
'product_id' => "p.entity_id",
651735
'category_id' => "c.category_id",
652736
'priority' => "trim('4')"
@@ -698,6 +782,7 @@ protected function initTemporaryTable()
698782
'is_system' => 'r.is_system',
699783
'store_id' => 's.store_id',
700784
'entity_type' => "trim('product')",
785+
'redirect_type' => "trim('0')",
701786
'product_id' => "p.entity_id",
702787
'category_id' => "trim('0')",
703788
'priority' => "trim('4')"
@@ -740,6 +825,7 @@ protected function initTemporaryTable()
740825
'is_system' => 's.is_system',
741826
'store_id' => 's.store_id',
742827
'entity_type' => "trim('product')",
828+
'redirect_type' => "trim('0')",
743829
'product_id' => "p.entity_id",
744830
'category_id' => "c.category_id",
745831
'priority' => "trim('4')"
@@ -780,6 +866,7 @@ protected function initTemporaryTable()
780866
'is_system' => 's.is_system',
781867
'store_id' => 's.store_id',
782868
'entity_type' => "trim('product')",
869+
'redirect_type' => "trim('0')",
783870
'product_id' => "p.entity_id",
784871
'category_id' => "trim('0')",
785872
'priority' => "trim('4')"

0 commit comments

Comments
 (0)