|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Store\Test\Unit\Controller\Store; |
| 9 | + |
| 10 | +use Magento\Framework\App\ActionInterface; |
| 11 | +use Magento\Framework\App\RequestInterface; |
| 12 | +use Magento\Framework\App\Response\RedirectInterface; |
| 13 | +use Magento\Framework\App\ResponseInterface; |
| 14 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 15 | +use Magento\Framework\Message\ManagerInterface; |
| 16 | +use Magento\Framework\Session\SidResolverInterface; |
| 17 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 18 | +use Magento\Store\Api\StoreRepositoryInterface; |
| 19 | +use Magento\Store\Api\StoreResolverInterface; |
| 20 | +use Magento\Store\Controller\Store\Redirect; |
| 21 | +use Magento\Store\Model\Store; |
| 22 | +use Magento\Store\Model\StoreResolver; |
| 23 | +use Magento\Store\Model\StoreSwitcher\HashGenerator; |
| 24 | +use PHPUnit\Framework\MockObject\MockObject; |
| 25 | +use PHPUnit\Framework\TestCase; |
| 26 | + |
| 27 | +/** |
| 28 | + * Test class for redirect controller |
| 29 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 30 | + */ |
| 31 | +class RedirectTest extends TestCase |
| 32 | +{ |
| 33 | + private const DEFAULT_STORE_VIEW_CODE = 'default'; |
| 34 | + private const STORE_CODE = 'sv1'; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var StoreRepositoryInterface|MockObject |
| 38 | + */ |
| 39 | + private $storeRepositoryMock; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var RequestInterface|MockObject |
| 43 | + */ |
| 44 | + private $requestMock; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var StoreResolverInterface|MockObject |
| 48 | + */ |
| 49 | + private $storeResolverMock; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var RedirectInterface|MockObject |
| 53 | + */ |
| 54 | + private $redirectMock; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var ResponseInterface|MockObject |
| 58 | + */ |
| 59 | + private $responseMock; |
| 60 | + |
| 61 | + /** |
| 62 | + * @var ManagerInterface|MockObject |
| 63 | + */ |
| 64 | + private $messageManagerMock; |
| 65 | + |
| 66 | + /** |
| 67 | + * @var Store|MockObject |
| 68 | + */ |
| 69 | + private $formStoreMock; |
| 70 | + |
| 71 | + /** |
| 72 | + * @var Store|MockObject |
| 73 | + */ |
| 74 | + private $currentStoreMock; |
| 75 | + |
| 76 | + /** |
| 77 | + * @var SidResolverInterface|MockObject |
| 78 | + */ |
| 79 | + private $sidResolverMock; |
| 80 | + |
| 81 | + /** |
| 82 | + * @var HashGenerator|MockObject |
| 83 | + */ |
| 84 | + private $hashGeneratorMock; |
| 85 | + |
| 86 | + /** |
| 87 | + * @var Redirect |
| 88 | + */ |
| 89 | + private $redirectController; |
| 90 | + |
| 91 | + /** |
| 92 | + * @inheritDoc |
| 93 | + */ |
| 94 | + protected function setUp() |
| 95 | + { |
| 96 | + $this->requestMock = $this->createMock(RequestInterface::class); |
| 97 | + $this->redirectMock = $this->createMock(RedirectInterface::class); |
| 98 | + $this->storeResolverMock = $this->createMock(StoreResolverInterface::class); |
| 99 | + $this->storeRepositoryMock = $this->createMock(StoreRepositoryInterface::class); |
| 100 | + $this->messageManagerMock = $this->createMock(ManagerInterface::class); |
| 101 | + $this->responseMock = $this->createMock(ResponseInterface::class); |
| 102 | + $this->formStoreMock = $this->createMock(Store::class); |
| 103 | + $this->sidResolverMock = $this->createMock(SidResolverInterface::class); |
| 104 | + $this->hashGeneratorMock = $this->createMock(HashGenerator::class); |
| 105 | + |
| 106 | + $this->currentStoreMock = $this->getMockBuilder(Store::class) |
| 107 | + ->disableOriginalConstructor() |
| 108 | + ->setMethods(['getBaseUrl']) |
| 109 | + ->getMock(); |
| 110 | + $this->storeRepositoryMock |
| 111 | + ->expects($this->once()) |
| 112 | + ->method('getById') |
| 113 | + ->willReturn($this->currentStoreMock); |
| 114 | + $this->storeResolverMock |
| 115 | + ->expects($this->once()) |
| 116 | + ->method('getCurrentStoreId') |
| 117 | + ->willReturnSelf(); |
| 118 | + |
| 119 | + $objectManager = new ObjectManagerHelper($this); |
| 120 | + |
| 121 | + $this->redirectController = $objectManager->getObject( |
| 122 | + Redirect::class, |
| 123 | + [ |
| 124 | + 'storeRepository' => $this->storeRepositoryMock, |
| 125 | + 'storeResolver' => $this->storeResolverMock, |
| 126 | + 'messageManager' => $this->messageManagerMock, |
| 127 | + '_request' => $this->requestMock, |
| 128 | + '_redirect' => $this->redirectMock, |
| 129 | + '_response' => $this->responseMock, |
| 130 | + 'sidResolver' => $this->sidResolverMock, |
| 131 | + 'hashGenerator' => $this->hashGeneratorMock |
| 132 | + ] |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Verify redirect controller |
| 138 | + * |
| 139 | + * @param string $defaultStoreViewCode |
| 140 | + * @param string $storeCode |
| 141 | + * |
| 142 | + * @dataProvider getConfigDataProvider |
| 143 | + * @return void |
| 144 | + * @throws NoSuchEntityException |
| 145 | + */ |
| 146 | + public function testRedirect(string $defaultStoreViewCode, string $storeCode): void |
| 147 | + { |
| 148 | + $this->requestMock |
| 149 | + ->expects($this->at(0)) |
| 150 | + ->method('getParam') |
| 151 | + ->with(StoreResolver::PARAM_NAME) |
| 152 | + ->willReturn($storeCode); |
| 153 | + $this->requestMock |
| 154 | + ->expects($this->at(1)) |
| 155 | + ->method('getParam') |
| 156 | + ->with('___from_store') |
| 157 | + ->willReturn($defaultStoreViewCode); |
| 158 | + $this->requestMock |
| 159 | + ->expects($this->at(2)) |
| 160 | + ->method('getParam') |
| 161 | + ->with(ActionInterface::PARAM_NAME_URL_ENCODED) |
| 162 | + ->willReturn($defaultStoreViewCode); |
| 163 | + $this->storeRepositoryMock |
| 164 | + ->expects($this->once()) |
| 165 | + ->method('get') |
| 166 | + ->with($defaultStoreViewCode) |
| 167 | + ->willReturn($this->formStoreMock); |
| 168 | + $this->formStoreMock |
| 169 | + ->expects($this->once()) |
| 170 | + ->method('getCode') |
| 171 | + ->willReturnSelf(); |
| 172 | + $this->sidResolverMock |
| 173 | + ->expects($this->once()) |
| 174 | + ->method('getUseSessionInUrl') |
| 175 | + ->willReturn(false); |
| 176 | + $this->hashGeneratorMock |
| 177 | + ->expects($this->once()) |
| 178 | + ->method('generateHash') |
| 179 | + ->with($this->formStoreMock) |
| 180 | + ->willReturn([]); |
| 181 | + |
| 182 | + $this->redirectMock |
| 183 | + ->expects($this->once()) |
| 184 | + ->method('redirect') |
| 185 | + ->with( |
| 186 | + $this->responseMock, |
| 187 | + 'stores/store/switch', |
| 188 | + ['_nosid' => true, |
| 189 | + '_query' => [ |
| 190 | + 'uenc' => $defaultStoreViewCode, |
| 191 | + '___from_store' => $this->formStoreMock, |
| 192 | + '___store' => $storeCode |
| 193 | + ] |
| 194 | + ] |
| 195 | + ) |
| 196 | + ->willReturnSelf(); |
| 197 | + |
| 198 | + $this->assertEquals(null, $this->redirectController->execute()); |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Verify execute with exception |
| 203 | + * |
| 204 | + * @param string $defaultStoreViewCode |
| 205 | + * @param string $storeCode |
| 206 | + * @return void |
| 207 | + * @dataProvider getConfigDataProvider |
| 208 | + * @throws NoSuchEntityException |
| 209 | + */ |
| 210 | + public function testRedirectWithThrowsException(string $defaultStoreViewCode, string $storeCode): void |
| 211 | + { |
| 212 | + $this->requestMock |
| 213 | + ->expects($this->at(0)) |
| 214 | + ->method('getParam') |
| 215 | + ->with(StoreResolver::PARAM_NAME) |
| 216 | + ->willReturn($storeCode); |
| 217 | + $this->requestMock |
| 218 | + ->expects($this->at(1)) |
| 219 | + ->method('getParam') |
| 220 | + ->with('___from_store') |
| 221 | + ->willReturn($defaultStoreViewCode); |
| 222 | + $this->storeRepositoryMock |
| 223 | + ->expects($this->once()) |
| 224 | + ->method('get') |
| 225 | + ->with($defaultStoreViewCode) |
| 226 | + ->willThrowException(new NoSuchEntityException()); |
| 227 | + $this->messageManagerMock |
| 228 | + ->expects($this->once()) |
| 229 | + ->method('addErrorMessage') |
| 230 | + ->willReturnSelf(); |
| 231 | + $this->currentStoreMock |
| 232 | + ->expects($this->once()) |
| 233 | + ->method('getBaseUrl') |
| 234 | + ->willReturnSelf(); |
| 235 | + $this->redirectMock |
| 236 | + ->expects($this->once()) |
| 237 | + ->method('redirect') |
| 238 | + ->with($this->responseMock, $this->currentStoreMock) |
| 239 | + ->willReturnSelf(); |
| 240 | + |
| 241 | + $this->assertEquals(null, $this->redirectController->execute()); |
| 242 | + } |
| 243 | + |
| 244 | + /** |
| 245 | + * Verify redirect target is null |
| 246 | + * |
| 247 | + * @return void |
| 248 | + * @throws NoSuchEntityException |
| 249 | + */ |
| 250 | + public function testRedirectTargetIsNull(): void |
| 251 | + { |
| 252 | + $this->requestMock |
| 253 | + ->expects($this->at(0)) |
| 254 | + ->method('getParam') |
| 255 | + ->with(StoreResolver::PARAM_NAME) |
| 256 | + ->willReturn(null); |
| 257 | + $this->requestMock |
| 258 | + ->expects($this->at(1)) |
| 259 | + ->method('getParam') |
| 260 | + ->with('___from_store') |
| 261 | + ->willReturnSelf(); |
| 262 | + $this->storeRepositoryMock |
| 263 | + ->expects($this->never()) |
| 264 | + ->method('get'); |
| 265 | + |
| 266 | + $this->assertEquals($this->responseMock, $this->redirectController->execute()); |
| 267 | + } |
| 268 | + |
| 269 | + /** |
| 270 | + * @inheritDoc |
| 271 | + * |
| 272 | + * @return array |
| 273 | + */ |
| 274 | + public function getConfigDataProvider(): array |
| 275 | + { |
| 276 | + return [ |
| 277 | + [ self::DEFAULT_STORE_VIEW_CODE, self::STORE_CODE ] |
| 278 | + ]; |
| 279 | + } |
| 280 | +} |
0 commit comments