|
| 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\CatalogUrlRewrite\Model; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 11 | +use Magento\CatalogUrlRewrite\Model\Map\DataProductUrlRewriteDatabaseMap; |
| 12 | +use Magento\Store\Model\ScopeInterface; |
| 13 | +use Magento\TestFramework\Fixture\DataFixture; |
| 14 | +use Magento\Catalog\Model\Product\Visibility; |
| 15 | +use Magento\Catalog\Test\Fixture\Product as ProductFixture; |
| 16 | + |
| 17 | +/** |
| 18 | + * Class for product url rewrites tests |
| 19 | + * |
| 20 | + */ |
| 21 | +class ProductUrlRewriteVisibilityTest extends AbstractUrlRewriteTest |
| 22 | +{ |
| 23 | + private const URL_KEY_EMPTY_MESSAGE = 'Failed asserting URL key is empty for the given product'; |
| 24 | + |
| 25 | + /** @var string */ |
| 26 | + private $suffix; |
| 27 | + |
| 28 | + /** @var ProductRepositoryInterface */ |
| 29 | + private $productRepository; |
| 30 | + |
| 31 | + /** |
| 32 | + * @inheritdoc |
| 33 | + */ |
| 34 | + protected function setUp(): void |
| 35 | + { |
| 36 | + parent::setUp(); |
| 37 | + |
| 38 | + $this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class); |
| 39 | + $this->suffix = $this->config->getValue( |
| 40 | + ProductUrlPathGenerator::XML_PATH_PRODUCT_URL_SUFFIX, |
| 41 | + ScopeInterface::SCOPE_STORE |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 1 |
| 47 | + * @dataProvider invisibleProductDataProvider |
| 48 | + * @param array $expectedData |
| 49 | + * @return void |
| 50 | + */ |
| 51 | + #[ |
| 52 | + DataFixture(ProductFixture::class, ['sku' => 'simple', 'name' => 'Simple Url Test Product', |
| 53 | + 'visibility' => Visibility::VISIBILITY_NOT_VISIBLE]), |
| 54 | + ] |
| 55 | + public function testUrlRewriteOnInvisibleProductEdit(array $expectedData): void |
| 56 | + { |
| 57 | + $product = $this->productRepository->get('simple', true, 0, true); |
| 58 | + $this->assertUrlKeyEmpty($product, self::URL_KEY_EMPTY_MESSAGE); |
| 59 | + |
| 60 | + //Update visibility and check the database entry |
| 61 | + $product->setVisibility(Visibility::VISIBILITY_BOTH); |
| 62 | + $product = $this->productRepository->save($product); |
| 63 | + |
| 64 | + $productUrlRewriteCollection = $this->getEntityRewriteCollection($product->getId()); |
| 65 | + $this->assertRewrites( |
| 66 | + $productUrlRewriteCollection, |
| 67 | + $this->prepareData($expectedData, (int)$product->getId()) |
| 68 | + ); |
| 69 | + |
| 70 | + //Update visibility and check if the entry is removed from the database |
| 71 | + $product = $this->productRepository->get('simple', true, 0, true); |
| 72 | + $product->setVisibility(Visibility::VISIBILITY_NOT_VISIBLE); |
| 73 | + $product = $this->productRepository->save($product); |
| 74 | + |
| 75 | + $this->assertUrlKeyEmpty($product, self::URL_KEY_EMPTY_MESSAGE); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @return array |
| 80 | + */ |
| 81 | + public function invisibleProductDataProvider(): array |
| 82 | + { |
| 83 | + return [ |
| 84 | + [ |
| 85 | + 'expected_data' => [ |
| 86 | + [ |
| 87 | + 'request_path' => 'simple-url-test-product%suffix%', |
| 88 | + 'target_path' => 'catalog/product/view/id/%id%', |
| 89 | + ], |
| 90 | + ], |
| 91 | + ], |
| 92 | + ]; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Assert URL key is empty in database for the given product |
| 97 | + * |
| 98 | + * @param $product |
| 99 | + * @param string $message |
| 100 | + * |
| 101 | + * @return void |
| 102 | + */ |
| 103 | + public function assertUrlKeyEmpty($product, $message = ''): void |
| 104 | + { |
| 105 | + $productUrlRewriteItems = $this->getEntityRewriteCollection($product->getId())->getItems(); |
| 106 | + $this->assertEmpty($productUrlRewriteItems, $message); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @inheritdoc |
| 111 | + */ |
| 112 | + protected function getUrlSuffix(): string |
| 113 | + { |
| 114 | + return $this->suffix; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @inheritdoc |
| 119 | + */ |
| 120 | + protected function getEntityType(): string |
| 121 | + { |
| 122 | + return DataProductUrlRewriteDatabaseMap::ENTITY_TYPE; |
| 123 | + } |
| 124 | +} |
0 commit comments