UrlRewriteHandlerTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\CatalogUrlRewrite\Observer;
  8. use Magento\Catalog\Api\CategoryListInterface;
  9. use Magento\Catalog\Api\Data\CategoryInterface;
  10. use Magento\Catalog\Api\Data\ProductInterface;
  11. use Magento\Catalog\Api\ProductRepositoryInterface;
  12. use Magento\Framework\Api\SearchCriteriaBuilder;
  13. use Magento\TestFramework\Helper\Bootstrap;
  14. use Magento\TestFramework\ObjectManager;
  15. use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
  16. use PHPUnit\Framework\TestCase;
  17. /**
  18. * @magentoAppArea adminhtml
  19. */
  20. class UrlRewriteHandlerTest extends TestCase
  21. {
  22. /**
  23. * @var UrlRewriteHandler
  24. */
  25. private $handler;
  26. /**
  27. * @var ObjectManager
  28. */
  29. private $objectManager;
  30. /**
  31. * @inheritdoc
  32. */
  33. protected function setUp()
  34. {
  35. $this->objectManager = Bootstrap::getObjectManager();
  36. $this->handler = $this->objectManager->get(UrlRewriteHandler::class);
  37. }
  38. /**
  39. * Checks category URLs rewrites generation with enabled `Use Categories Path for Product URLs` option and
  40. * store's specific product URL key.
  41. *
  42. * @magentoDbIsolation disabled
  43. * @magentoDataFixture Magento/CatalogUrlRewrite/Fixtures/product_custom_url_key.php
  44. * @magentoConfigFixture admin_store catalog/seo/product_use_categories 1
  45. */
  46. public function testGenerateProductUrlRewrites()
  47. {
  48. $product = $this->getProduct('p002');
  49. $category = $this->getCategory('category 1');
  50. // change the category scope to the global
  51. $category->setStoreId(0)
  52. ->setChangedProductIds([$product->getId()])
  53. ->setAffectedProductIds([$product->getId()])
  54. ->setAnchorsAbove(false);
  55. $generatedUrls = $this->handler->generateProductUrlRewrites($category);
  56. $actual = array_values(array_map(function (UrlRewrite $urlRewrite) {
  57. return $urlRewrite->getRequestPath();
  58. }, $generatedUrls));
  59. $expected = [
  60. 'store-1-key.html', // the Default store
  61. 'cat-1/store-1-key.html', // the Default store with Category URL key
  62. '/store-1-key.html', // an anchor URL the Default store
  63. 'p002.html', // the Secondary store
  64. 'cat-1-2/p002.html', // the Secondary store with Category URL key
  65. '/p002.html', // an anchor URL the Secondary store
  66. ];
  67. self::assertEquals($expected, $actual, 'Generated URLs rewrites do not match.');
  68. }
  69. /**
  70. * Gets category by name.
  71. *
  72. * @param string $name
  73. * @return CategoryInterface
  74. */
  75. private function getCategory(string $name): CategoryInterface
  76. {
  77. /** @var SearchCriteriaBuilder $searchCriteriaBuilder */
  78. $searchCriteriaBuilder = $this->objectManager->get(SearchCriteriaBuilder::class);
  79. $searchCriteria = $searchCriteriaBuilder->addFilter('name', $name)
  80. ->create();
  81. /** @var CategoryListInterface $repository */
  82. $repository = $this->objectManager->get(CategoryListInterface::class);
  83. $items = $repository->getList($searchCriteria)
  84. ->getItems();
  85. return array_pop($items);
  86. }
  87. /**
  88. * Gets product by SKU.
  89. *
  90. * @param string $sku
  91. * @return ProductInterface
  92. * @throws \Magento\Framework\Exception\NoSuchEntityException
  93. */
  94. private function getProduct(string $sku): ProductInterface
  95. {
  96. /** @var ProductRepositoryInterface $productRepository */
  97. $productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
  98. return $productRepository->get($sku);
  99. }
  100. }