RewriteUrlTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\UrlRewrite\Model\StoreSwitcher;
  8. use Magento\Catalog\Api\ProductRepositoryInterface;
  9. use Magento\Framework\App\Config\ReinitableConfigInterface;
  10. use Magento\Framework\App\Config\Value;
  11. use Magento\Store\Api\Data\StoreInterface;
  12. use Magento\Store\Model\ScopeInterface;
  13. use Magento\Store\Model\StoreSwitcher;
  14. use Magento\Framework\ObjectManagerInterface as ObjectManager;
  15. use Magento\TestFramework\Helper\Bootstrap;
  16. class RewriteUrlTest extends \PHPUnit\Framework\TestCase
  17. {
  18. /**
  19. * @var StoreSwitcher
  20. */
  21. private $storeSwitcher;
  22. /**
  23. * @var ObjectManager
  24. */
  25. private $objectManager;
  26. /**
  27. * @var ProductRepositoryInterface
  28. */
  29. private $productRepository;
  30. /**
  31. * Class dependencies initialization
  32. *
  33. * @return void
  34. */
  35. protected function setUp()
  36. {
  37. $this->objectManager = Bootstrap::getObjectManager();
  38. $this->storeSwitcher = $this->objectManager->get(StoreSwitcher::class);
  39. $this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
  40. }
  41. /**
  42. * @magentoDataFixture Magento/UrlRewrite/_files/url_rewrite.php
  43. * @magentoDataFixture Magento/Catalog/_files/category_product.php
  44. * @return void
  45. * @throws StoreSwitcher\CannotSwitchStoreException
  46. * @throws \Magento\Framework\Exception\NoSuchEntityException
  47. */
  48. public function testSwitchToNonExistingPage(): void
  49. {
  50. $fromStoreCode = 'default';
  51. /** @var \Magento\Store\Api\StoreRepositoryInterface $storeRepository */
  52. $storeRepository = $this->objectManager->create(\Magento\Store\Api\StoreRepositoryInterface::class);
  53. $fromStore = $storeRepository->get($fromStoreCode);
  54. $toStoreCode = 'fixture_second_store';
  55. /** @var \Magento\Store\Api\StoreRepositoryInterface $storeRepository */
  56. $storeRepository = $this->objectManager->create(\Magento\Store\Api\StoreRepositoryInterface::class);
  57. $toStore = $storeRepository->get($toStoreCode);
  58. $this->setBaseUrl($toStore);
  59. $product = $this->productRepository->get('simple333');
  60. $redirectUrl = "http://domain.com/{$product->getUrlKey()}.html";
  61. $expectedUrl = $toStore->getBaseUrl();
  62. $this->assertEquals($expectedUrl, $this->storeSwitcher->switch($fromStore, $toStore, $redirectUrl));
  63. }
  64. /**
  65. * @magentoDataFixture Magento/UrlRewrite/_files/url_rewrite.php
  66. * @return void
  67. * @throws StoreSwitcher\CannotSwitchStoreException
  68. * @throws \Magento\Framework\Exception\NoSuchEntityException
  69. */
  70. public function testSwitchToExistingPage(): void
  71. {
  72. $fromStoreCode = 'default';
  73. /** @var \Magento\Store\Api\StoreRepositoryInterface $storeRepository */
  74. $storeRepository = $this->objectManager->create(\Magento\Store\Api\StoreRepositoryInterface::class);
  75. $fromStore = $storeRepository->get($fromStoreCode);
  76. $toStoreCode = 'fixture_second_store';
  77. /** @var \Magento\Store\Api\StoreRepositoryInterface $storeRepository */
  78. $storeRepository = $this->objectManager->create(\Magento\Store\Api\StoreRepositoryInterface::class);
  79. $toStore = $storeRepository->get($toStoreCode);
  80. $redirectUrl = "http://localhost/index.php/page-c/";
  81. $expectedUrl = "http://localhost/index.php/page-c-on-2nd-store";
  82. $this->assertEquals($expectedUrl, $this->storeSwitcher->switch($fromStore, $toStore, $redirectUrl));
  83. }
  84. /**
  85. * Set base url to store.
  86. *
  87. * @param StoreInterface $targetStore
  88. * @return void
  89. */
  90. private function setBaseUrl(StoreInterface $targetStore): void
  91. {
  92. $configValue = $this->objectManager->create(Value::class);
  93. $configValue->load('web/unsecure/base_url', 'path');
  94. $baseUrl = 'http://domain.com/';
  95. if (!$configValue->getPath()) {
  96. $configValue->setPath('web/unsecure/base_url');
  97. }
  98. $configValue->setValue($baseUrl);
  99. $configValue->setScope(ScopeInterface::SCOPE_STORES);
  100. $configValue->setScopeId($targetStore->getId());
  101. $configValue->save();
  102. $reinitibleConfig = $this->objectManager->create(ReinitableConfigInterface::class);
  103. $reinitibleConfig->reinit();
  104. }
  105. }