|
5 | 5 | */
|
6 | 6 | namespace Magento\Store\Controller\Store;
|
7 | 7 |
|
| 8 | +use Magento\Framework\App\ActionInterface; |
| 9 | +use Magento\Framework\Encryption\UrlCoder; |
| 10 | +use Magento\Store\Api\StoreResolverInterface; |
| 11 | +use Magento\Store\Model\Store; |
| 12 | +use Magento\Store\Model\StoreManagerInterface; |
| 13 | +use Magento\Store\Model\StoreSwitcher\ContextInterface; |
| 14 | +use Magento\Store\Model\StoreSwitcher\ContextInterfaceFactory; |
| 15 | +use Magento\Store\Model\StoreSwitcher\RedirectDataGenerator; |
| 16 | +use Magento\Store\Model\StoreSwitcher\RedirectDataPostprocessorInterface; |
| 17 | +use Magento\Store\Model\StoreSwitcher\RedirectDataPreprocessorInterface; |
| 18 | +use Magento\TestFramework\TestCase\AbstractController; |
| 19 | +use PHPUnit\Framework\MockObject\MockObject; |
| 20 | + |
8 | 21 | /**
|
9 | 22 | * Test for store switch controller.
|
10 | 23 | */
|
11 |
| -class SwitchActionTest extends \Magento\TestFramework\TestCase\AbstractController |
| 24 | +class SwitchActionTest extends AbstractController |
12 | 25 | {
|
| 26 | + /** |
| 27 | + * @var RedirectDataPreprocessorInterface |
| 28 | + */ |
| 29 | + private $preprocessor; |
| 30 | + /** |
| 31 | + * @var MockObject |
| 32 | + */ |
| 33 | + private $preprocessorMock; |
| 34 | + /** |
| 35 | + * @var RedirectDataPostprocessorInterface |
| 36 | + */ |
| 37 | + private $postprocessor; |
| 38 | + /** |
| 39 | + * @var MockObject |
| 40 | + */ |
| 41 | + private $postprocessorMock; |
| 42 | + |
| 43 | + /** |
| 44 | + * @inheritDoc |
| 45 | + */ |
| 46 | + protected function setUp(): void |
| 47 | + { |
| 48 | + parent::setUp(); |
| 49 | + $this->preprocessor = $this->_objectManager->get(RedirectDataPreprocessorInterface::class); |
| 50 | + $this->preprocessorMock = $this->createMock(RedirectDataPreprocessorInterface::class); |
| 51 | + $this->_objectManager->addSharedInstance($this->preprocessorMock, get_class($this->preprocessor)); |
| 52 | + |
| 53 | + $this->postprocessor = $this->_objectManager->get(RedirectDataPostprocessorInterface::class); |
| 54 | + $this->postprocessorMock = $this->createMock(RedirectDataPostprocessorInterface::class); |
| 55 | + $this->_objectManager->addSharedInstance($this->postprocessorMock, get_class($this->postprocessor)); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @inheritDoc |
| 60 | + */ |
| 61 | + protected function tearDown(): void |
| 62 | + { |
| 63 | + if ($this->preprocessor) { |
| 64 | + $this->_objectManager->addSharedInstance($this->preprocessor, get_class($this->preprocessor)); |
| 65 | + } |
| 66 | + if ($this->postprocessor) { |
| 67 | + $this->_objectManager->addSharedInstance($this->postprocessor, get_class($this->postprocessor)); |
| 68 | + } |
| 69 | + parent::tearDown(); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @magentoDataFixture Magento/Store/_files/second_store.php |
| 74 | + * @magentoConfigFixture web/url/use_store 0 |
| 75 | + * @magentoConfigFixture fixture_second_store_store web/unsecure/base_url http://second_store.test/ |
| 76 | + * @magentoConfigFixture fixture_second_store_store web/unsecure/base_link_url http://second_store.test/ |
| 77 | + * @magentoConfigFixture fixture_second_store_store web/secure/base_url http://second_store.test/ |
| 78 | + * @magentoConfigFixture fixture_second_store_store web/secure/base_link_url http://second_store.test/ |
| 79 | + */ |
| 80 | + public function testSwitch() |
| 81 | + { |
| 82 | + $data = ['key1' => 'value1', 'key2' => 1]; |
| 83 | + $this->preprocessorMock->method('process') |
| 84 | + ->willReturn($data); |
| 85 | + $this->postprocessorMock->expects($this->once()) |
| 86 | + ->method('process') |
| 87 | + ->with($this->isInstanceOf(ContextInterface::class), $data); |
| 88 | + |
| 89 | + $redirectDataGenerator = $this->_objectManager->get(RedirectDataGenerator::class); |
| 90 | + $contextFactory = $this->_objectManager->get(ContextInterfaceFactory::class); |
| 91 | + $storeManager = $this->_objectManager->get(StoreManagerInterface::class); |
| 92 | + $urlEncoder = $this->_objectManager->get(UrlCoder::class); |
| 93 | + $fromStore = $storeManager->getStore('fixture_second_store'); |
| 94 | + $targetStore = $storeManager->getStore('default'); |
| 95 | + $redirectData = $redirectDataGenerator->generate( |
| 96 | + $contextFactory->create( |
| 97 | + [ |
| 98 | + 'fromStore' => $fromStore, |
| 99 | + 'targetStore' => $targetStore, |
| 100 | + 'redirectUrl' => '/', |
| 101 | + ] |
| 102 | + ) |
| 103 | + ); |
| 104 | + $this->getRequest()->setParams( |
| 105 | + [ |
| 106 | + '___from_store' => $fromStore->getCode(), |
| 107 | + StoreResolverInterface::PARAM_NAME => $targetStore->getCode(), |
| 108 | + ActionInterface::PARAM_NAME_URL_ENCODED => $urlEncoder->encode('/'), |
| 109 | + 'data' => $redirectData->getData(), |
| 110 | + 'time_stamp' => $redirectData->getTimestamp(), |
| 111 | + 'signature' => $redirectData->getSignature(), |
| 112 | + ] |
| 113 | + ); |
| 114 | + $this->dispatch('stores/store/switch'); |
| 115 | + $this->assertRedirect($this->equalTo('http://localhost/index.php/')); |
| 116 | + } |
| 117 | + |
13 | 118 | /**
|
14 | 119 | * Ensure that proper default store code is calculated.
|
15 | 120 | *
|
@@ -41,10 +146,10 @@ public function testExecuteWithCustomDefaultStore()
|
41 | 146 | * @param string $from
|
42 | 147 | * @param string $to
|
43 | 148 | */
|
44 |
| - protected function changeStoreCode($from, $to) |
| 149 | + private function changeStoreCode($from, $to) |
45 | 150 | {
|
46 |
| - /** @var \Magento\Store\Model\Store $store */ |
47 |
| - $store = $this->_objectManager->create(\Magento\Store\Model\Store::class); |
| 151 | + /** @var Store $store */ |
| 152 | + $store = $this->_objectManager->create(Store::class); |
48 | 153 | $store->load($from, 'code');
|
49 | 154 | $store->setCode($to);
|
50 | 155 | $store->save();
|
|
0 commit comments