Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RewriteUrlTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
18 
19 class RewriteUrlTest extends \PHPUnit\Framework\TestCase
20 {
24  private $storeSwitcher;
25 
29  private $objectManager;
30 
34  private $productRepository;
35 
41  protected function setUp()
42  {
43  $this->objectManager = Bootstrap::getObjectManager();
44  $this->storeSwitcher = $this->objectManager->get(StoreSwitcher::class);
45  $this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
46  }
47 
55  public function testSwitchToNonExistingPage(): void
56  {
57  $fromStoreCode = 'default';
59  $storeRepository = $this->objectManager->create(\Magento\Store\Api\StoreRepositoryInterface::class);
60  $fromStore = $storeRepository->get($fromStoreCode);
61 
62  $toStoreCode = 'fixture_second_store';
64  $storeRepository = $this->objectManager->create(\Magento\Store\Api\StoreRepositoryInterface::class);
65  $toStore = $storeRepository->get($toStoreCode);
66 
67  $this->setBaseUrl($toStore);
68 
69  $product = $this->productRepository->get('simple333');
70 
71  $redirectUrl = "http://domain.com/{$product->getUrlKey()}.html";
72  $expectedUrl = $toStore->getBaseUrl();
73 
74  $this->assertEquals($expectedUrl, $this->storeSwitcher->switch($fromStore, $toStore, $redirectUrl));
75  }
76 
83  public function testSwitchToExistingPage(): void
84  {
85  $fromStoreCode = 'default';
87  $storeRepository = $this->objectManager->create(\Magento\Store\Api\StoreRepositoryInterface::class);
88  $fromStore = $storeRepository->get($fromStoreCode);
89 
90  $toStoreCode = 'fixture_second_store';
92  $storeRepository = $this->objectManager->create(\Magento\Store\Api\StoreRepositoryInterface::class);
93  $toStore = $storeRepository->get($toStoreCode);
94 
95  $redirectUrl = $expectedUrl = "http://localhost/page-c";
96 
97  $this->assertEquals($expectedUrl, $this->storeSwitcher->switch($fromStore, $toStore, $redirectUrl));
98  }
99 
106  private function setBaseUrl(StoreInterface $targetStore): void
107  {
108  $configValue = $this->objectManager->create(Value::class);
109  $configValue->load('web/unsecure/base_url', 'path');
110  $baseUrl = 'http://domain.com/';
111  if (!$configValue->getPath()) {
112  $configValue->setPath('web/unsecure/base_url');
113  }
114  $configValue->setValue($baseUrl);
115  $configValue->setScope(ScopeInterface::SCOPE_STORES);
116  $configValue->setScopeId($targetStore->getId());
117  $configValue->save();
118 
119  $reinitibleConfig = $this->objectManager->create(ReinitableConfigInterface::class);
120  $reinitibleConfig->reinit();
121  }
122 }