|
13 | 13 | use Magento\Catalog\Model\Product\Visibility;
|
14 | 14 | use Magento\Catalog\Model\ProductFactory;
|
15 | 15 | use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
|
| 16 | +use Magento\CatalogImportExport\Model\Import\Product; |
16 | 17 | use Magento\CatalogUrlRewrite\Model\Map\DataProductUrlRewriteDatabaseMap;
|
| 18 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 19 | +use Magento\Framework\Filesystem; |
| 20 | +use Magento\ImportExport\Model\Import; |
| 21 | +use Magento\ImportExport\Model\Import\Source\Csv; |
17 | 22 | use Magento\Store\Model\ScopeInterface;
|
18 | 23 | use Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException;
|
19 | 24 | use Magento\UrlRewrite\Model\OptionProvider;
|
| 25 | +use Psr\Log\LoggerInterface; |
20 | 26 |
|
21 | 27 | /**
|
22 | 28 | * Class for product url rewrites tests
|
@@ -275,6 +281,77 @@ public function testProductUrlRewritePerStoreViews(): void
|
275 | 281 | }
|
276 | 282 | }
|
277 | 283 |
|
| 284 | + /** |
| 285 | + * Check if redirects are generated correctly while product urls are changed during import process. |
| 286 | + * |
| 287 | + * @magentoDataFixture Magento/Catalog/_files/multiple_products.php |
| 288 | + * @magentoAppIsolation enabled |
| 289 | + * @magentoDbIsolation enabled |
| 290 | + */ |
| 291 | + public function testImportProductRewrites() |
| 292 | + { |
| 293 | + $data = [ |
| 294 | + ['sku' => 'simple1', 'request_path' => 'simple-product1', 'target_path' => 'product-1-updated'], |
| 295 | + ['sku' => 'simple2', 'request_path' => 'simple-product2', 'target_path' => 'product-2-updated'], |
| 296 | + ['sku' => 'simple3', 'request_path' => 'simple-product3', 'target_path' => 'product-3-updated'], |
| 297 | + ]; |
| 298 | + |
| 299 | + $logger = $this->getMockBuilder(LoggerInterface::class) |
| 300 | + ->disableOriginalConstructor() |
| 301 | + ->getMockForAbstractClass(); |
| 302 | + $productImport = $this->objectManager->create( |
| 303 | + Product::class, |
| 304 | + ['logger' => $logger] |
| 305 | + ); |
| 306 | + $filesystem = $this->objectManager->get(Filesystem::class); |
| 307 | + |
| 308 | + foreach ($data as $datum) { |
| 309 | + $this->assertEquals( |
| 310 | + $datum['request_path'], |
| 311 | + $this->productRepository->get($datum['sku'], false, null, true)->getUrlKey() |
| 312 | + ); |
| 313 | + } |
| 314 | + |
| 315 | + $directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT); |
| 316 | + $source = $this->objectManager->create( |
| 317 | + Csv::class, |
| 318 | + [ |
| 319 | + 'file' => __DIR__ . '/../_files/products_to_import_with_rewrites.csv', |
| 320 | + 'directory' => $directory |
| 321 | + ] |
| 322 | + ); |
| 323 | + $errors = $productImport->setParameters( |
| 324 | + ['behavior' => Import::BEHAVIOR_APPEND, 'entity' => 'catalog_product'] |
| 325 | + )->setSource( |
| 326 | + $source |
| 327 | + )->validateData(); |
| 328 | + $this->assertTrue($errors->getErrorsCount() === 0); |
| 329 | + $productImport->importData(); |
| 330 | + |
| 331 | + foreach ($data as $datum) { |
| 332 | + $product = $this->productRepository->get($datum['sku'], false, null, true); |
| 333 | + $this->assertEquals( |
| 334 | + $datum['target_path'], |
| 335 | + $product->getUrlKey() |
| 336 | + ); |
| 337 | + |
| 338 | + $productUrlRewriteCollection = $this->getEntityRewriteCollection($product->getId()); |
| 339 | + $rewriteExists = false; |
| 340 | + foreach ($productUrlRewriteCollection as $item) { |
| 341 | + if ( |
| 342 | + $item->getTargetPath() === $datum['target_path'] . $this->suffix && |
| 343 | + $item->getRequestPath() === $datum['request_path'] . $this->suffix |
| 344 | + ) { |
| 345 | + $rewriteExists = true; |
| 346 | + break; |
| 347 | + } |
| 348 | + |
| 349 | + } |
| 350 | + |
| 351 | + $this->assertTrue($rewriteExists); |
| 352 | + } |
| 353 | + } |
| 354 | + |
278 | 355 | /**
|
279 | 356 | * Save product with data using resource model directly
|
280 | 357 | *
|
|
0 commit comments