|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Catalog\Controller\Adminhtml\Product; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 11 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 12 | +use Magento\Framework\App\Request\Http as HttpRequest; |
| 13 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 14 | +use Magento\Framework\Message\MessageInterface; |
| 15 | +use Magento\TestFramework\TestCase\AbstractBackendController; |
| 16 | + |
| 17 | +/** |
| 18 | + * Test class for Product duplicate action |
| 19 | + * |
| 20 | + * @magentoAppArea adminhtml |
| 21 | + * @see \Magento\Catalog\Controller\Adminhtml\Product\Duplicate |
| 22 | + */ |
| 23 | +class DuplicateTest extends AbstractBackendController |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var ProductRepositoryInterface |
| 27 | + */ |
| 28 | + private $productRepository; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var string |
| 32 | + */ |
| 33 | + private $duplicatedProductSku; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var array |
| 37 | + */ |
| 38 | + private $dataKeys = ['name', 'description', 'short_description', 'price', 'weight', 'attribute_set_id']; |
| 39 | + |
| 40 | + /** |
| 41 | + * @inheritdoc |
| 42 | + */ |
| 43 | + protected function setUp(): void |
| 44 | + { |
| 45 | + parent::setUp(); |
| 46 | + |
| 47 | + $this->productRepository = $this->_objectManager->get(ProductRepositoryInterface::class); |
| 48 | + $this->productRepository->cleanCache(); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @inheritdoc |
| 53 | + */ |
| 54 | + protected function tearDown(): void |
| 55 | + { |
| 56 | + try { |
| 57 | + $this->productRepository->deleteById($this->duplicatedProductSku); |
| 58 | + } catch (NoSuchEntityException $e) { |
| 59 | + // product already deleted |
| 60 | + } |
| 61 | + |
| 62 | + parent::tearDown(); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @magentoDataFixture Magento/Catalog/_files/product_simple.php |
| 67 | + * |
| 68 | + * @return void |
| 69 | + */ |
| 70 | + public function testDuplicateAction(): void |
| 71 | + { |
| 72 | + $product = $this->productRepository->get('simple'); |
| 73 | + $this->getRequest()->setMethod(HttpRequest::METHOD_GET); |
| 74 | + $this->getRequest()->setParams( |
| 75 | + [ |
| 76 | + 'id' => $product->getId(), |
| 77 | + 'attribute_set_id' => $product->getAttributeSetId(), |
| 78 | + ] |
| 79 | + ); |
| 80 | + $this->dispatch('backend/catalog/product/duplicate'); |
| 81 | + $this->assertSessionMessages( |
| 82 | + $this->containsEqual((string)__('You duplicated the product.')), |
| 83 | + MessageInterface::TYPE_SUCCESS |
| 84 | + ); |
| 85 | + $this->assertRedirect($this->stringContains('catalog/product/edit/')); |
| 86 | + $productId = $this->getIdFromRedirectedUrl(); |
| 87 | + $this->assertNotEmpty($productId, 'Id not found'); |
| 88 | + $duplicatedProduct = $this->productRepository->getById((int)$productId); |
| 89 | + $this->duplicatedProductSku = $duplicatedProduct->getSku(); |
| 90 | + $this->assertProductDuplicated($product, $duplicatedProduct); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Get id value from redirected url |
| 95 | + * |
| 96 | + * @return string |
| 97 | + */ |
| 98 | + private function getIdFromRedirectedUrl(): string |
| 99 | + { |
| 100 | + $url = $this->getResponse() |
| 101 | + ->getHeader('Location') |
| 102 | + ->getFieldValue(); |
| 103 | + $pattern = '!/id/(.*?)/!'; |
| 104 | + $result = preg_match($pattern, $url, $matches); |
| 105 | + |
| 106 | + return $result ? $matches[1] : ''; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Checks that duplicate product was created from the first product |
| 111 | + * |
| 112 | + * @param ProductInterface $product |
| 113 | + * @param ProductInterface $duplicatedProduct |
| 114 | + * @return void |
| 115 | + */ |
| 116 | + private function assertProductDuplicated(ProductInterface $product, ProductInterface $duplicatedProduct): void |
| 117 | + { |
| 118 | + foreach ($this->dataKeys as $key) { |
| 119 | + $this->assertEquals($product->getData($key), $duplicatedProduct->getData($key)); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments