UrlRewritesTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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\GraphQl\Catalog;
  8. use Magento\Catalog\Api\ProductRepositoryInterface;
  9. use Magento\TestFramework\ObjectManager;
  10. use Magento\TestFramework\TestCase\GraphQlAbstract;
  11. use Magento\UrlRewrite\Model\UrlFinderInterface;
  12. use Magento\UrlRewrite\Service\V1\Data\UrlRewrite as UrlRewriteDTO;
  13. /**
  14. * Test of getting URL rewrites data from products
  15. */
  16. class UrlRewritesTest extends GraphQlAbstract
  17. {
  18. /**
  19. *
  20. * @magentoApiDataFixture Magento/Catalog/_files/product_virtual.php
  21. * @throws \Magento\Framework\Exception\NoSuchEntityException
  22. */
  23. public function testProductWithNoCategoriesAssigned()
  24. {
  25. $productSku = 'virtual-product';
  26. $query
  27. = <<<QUERY
  28. {
  29. products (filter: {sku: {eq: "{$productSku}"}}) {
  30. items {
  31. name,
  32. sku,
  33. description {
  34. html
  35. }
  36. url_rewrites {
  37. url,
  38. parameters {
  39. name,
  40. value
  41. }
  42. }
  43. }
  44. }
  45. }
  46. QUERY;
  47. $response = $this->graphQlQuery($query);
  48. /** @var ProductRepositoryInterface $productRepository */
  49. $productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
  50. $product = $productRepository->get('virtual-product', false, null, true);
  51. $urlFinder = ObjectManager::getInstance()->get(UrlFinderInterface::class);
  52. $rewritesCollection = $urlFinder->findAllByData([UrlRewriteDTO::ENTITY_ID => $product->getId()]);
  53. /* There should be only one rewrite */
  54. /** @var UrlRewriteDTO $urlRewrite */
  55. $urlRewrite = current($rewritesCollection);
  56. $this->assertArrayHasKey('url_rewrites', $response['products']['items'][0]);
  57. $this->assertCount(1, $response['products']['items'][0]['url_rewrites']);
  58. $this->assertResponseFields(
  59. $response['products']['items'][0]['url_rewrites'][0],
  60. [
  61. "url" => $urlRewrite->getRequestPath(),
  62. "parameters" => $this->getUrlParameters($urlRewrite->getTargetPath())
  63. ]
  64. );
  65. }
  66. /**
  67. *
  68. * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
  69. * @throws \Magento\Framework\Exception\NoSuchEntityException
  70. */
  71. public function testProductWithOneCategoryAssigned()
  72. {
  73. $productSku = 'simple';
  74. $query
  75. = <<<QUERY
  76. {
  77. products (filter: {sku: {eq: "{$productSku}"}}) {
  78. items {
  79. name,
  80. sku,
  81. description {
  82. html
  83. }
  84. url_rewrites {
  85. url,
  86. parameters {
  87. name,
  88. value
  89. }
  90. }
  91. }
  92. }
  93. }
  94. QUERY;
  95. $response = $this->graphQlQuery($query);
  96. /** @var ProductRepositoryInterface $productRepository */
  97. $productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
  98. $product = $productRepository->get('simple', false, null, true);
  99. $urlFinder = ObjectManager::getInstance()->get(UrlFinderInterface::class);
  100. $rewritesCollection = $urlFinder->findAllByData([UrlRewriteDTO::ENTITY_ID => $product->getId()]);
  101. $rewritesCount = count($rewritesCollection);
  102. $this->assertArrayHasKey('url_rewrites', $response['products']['items'][0]);
  103. $this->assertCount($rewritesCount, $response['products']['items'][0]['url_rewrites']);
  104. for ($i = 0; $i < $rewritesCount; $i++) {
  105. $urlRewrite = $rewritesCollection[$i];
  106. $this->assertResponseFields(
  107. $response['products']['items'][0]['url_rewrites'][$i],
  108. [
  109. "url" => $urlRewrite->getRequestPath(),
  110. "parameters" => $this->getUrlParameters($urlRewrite->getTargetPath())
  111. ]
  112. );
  113. }
  114. }
  115. /**
  116. * Parses target path and extracts parameters
  117. *
  118. * @param string $targetPath
  119. * @return array
  120. */
  121. private function getUrlParameters(string $targetPath): array
  122. {
  123. $urlParameters = [];
  124. $targetPathParts = explode('/', trim($targetPath, '/'));
  125. for ($i = 3; ($i < sizeof($targetPathParts) - 1); $i += 2) {
  126. $urlParameters[] = [
  127. 'name' => $targetPathParts[$i],
  128. 'value' => $targetPathParts[$i + 1]
  129. ];
  130. }
  131. return $urlParameters;
  132. }
  133. }