Skip to content

Commit

Permalink
ENGCOM-6869: issue/26384 Fix store switcher when using different base…
Browse files Browse the repository at this point in the history
… url on stores #26548
  • Loading branch information
slavvka authored Feb 27, 2020
2 parents 5eb2a6a + 0213f14 commit a998b2f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 23 deletions.
32 changes: 24 additions & 8 deletions app/code/Magento/Store/Controller/Store/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\App\ActionInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Session\Generic;
use Magento\Framework\Session\SidResolverInterface;
use Magento\Store\Api\StoreRepositoryInterface;
use Magento\Store\Api\StoreResolverInterface;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Store\Model\StoreResolver;
use Magento\Store\Model\StoreSwitcher\HashGenerator;

Expand All @@ -38,27 +43,35 @@ class Redirect extends Action implements HttpGetActionInterface, HttpPostActionI
*/
private $hashGenerator;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @param Context $context
* @param StoreRepositoryInterface $storeRepository
* @param StoreResolverInterface $storeResolver
* @param \Magento\Framework\Session\Generic $session
* @param \Magento\Framework\Session\SidResolverInterface $sidResolver
* @param Generic $session
* @param SidResolverInterface $sidResolver
* @param HashGenerator $hashGenerator
* @param StoreManagerInterface $storeManager
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function __construct(
Context $context,
StoreRepositoryInterface $storeRepository,
StoreResolverInterface $storeResolver,
\Magento\Framework\Session\Generic $session,
\Magento\Framework\Session\SidResolverInterface $sidResolver,
HashGenerator $hashGenerator
Generic $session,
SidResolverInterface $sidResolver,
HashGenerator $hashGenerator,
StoreManagerInterface $storeManager = null
) {
parent::__construct($context);
$this->storeRepository = $storeRepository;
$this->storeResolver = $storeResolver;
$this->hashGenerator = $hashGenerator;
$this->storeManager = $storeManager ?: ObjectManager::getInstance()->get(StoreManagerInterface::class);
}

/**
Expand All @@ -81,6 +94,9 @@ public function execute()
try {
/** @var Store $fromStore */
$fromStore = $this->storeRepository->get($fromStoreCode);
/** @var Store $targetStore */
$targetStore = $this->storeRepository->get($targetStoreCode);
$this->storeManager->setCurrentStore($targetStore);
} catch (NoSuchEntityException $e) {
$error = __("Requested store is not found ({$fromStoreCode})");
}
Expand All @@ -89,12 +105,11 @@ public function execute()
$this->messageManager->addErrorMessage($error);
$this->_redirect->redirect($this->_response, $currentStore->getBaseUrl());
} else {
$encodedUrl = $this->_request->getParam(\Magento\Framework\App\ActionInterface::PARAM_NAME_URL_ENCODED);

$encodedUrl = $this->_request->getParam(ActionInterface::PARAM_NAME_URL_ENCODED);
$query = [
'___from_store' => $fromStore->getCode(),
StoreResolverInterface::PARAM_NAME => $targetStoreCode,
\Magento\Framework\App\ActionInterface::PARAM_NAME_URL_ENCODED => $encodedUrl,
ActionInterface::PARAM_NAME_URL_ENCODED => $encodedUrl,
];

$customerHash = $this->hashGenerator->generateHash($fromStore);
Expand All @@ -104,6 +119,7 @@ public function execute()
'_nosid' => true,
'_query' => $query
];

$this->_redirect->redirect($this->_response, 'stores/store/switch', $arguments);
}

Expand Down
60 changes: 45 additions & 15 deletions app/code/Magento/Store/Test/Unit/Controller/Store/RedirectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Magento\Store\Api\StoreResolverInterface;
use Magento\Store\Controller\Store\Redirect;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Store\Model\StoreResolver;
use Magento\Store\Model\StoreSwitcher\HashGenerator;
use PHPUnit\Framework\MockObject\MockObject;
Expand All @@ -31,8 +32,20 @@
*/
class RedirectTest extends TestCase
{
private const DEFAULT_STORE_VIEW_CODE = 'default';
private const STORE_CODE = 'sv1';
/**
* Stub for default store view code
*/
private const STUB_DEFAULT_STORE_VIEW_CODE = 'default';

/**
* Stub for default store code
*/
private const STUB_STORE_CODE = 'sv1';

/**
* @var StoreManagerInterface|MockObject
*/
private $storeManagerMock;

/**
* @var StoreRepositoryInterface|MockObject
Expand Down Expand Up @@ -67,7 +80,12 @@ class RedirectTest extends TestCase
/**
* @var Store|MockObject
*/
private $formStoreMock;
private $fromStoreMock;

/**
* @var Store|MockObject
*/
private $targetStoreMock;

/**
* @var Store|MockObject
Expand All @@ -87,13 +105,14 @@ class RedirectTest extends TestCase
/**
* @var Redirect
*/
private $redirectController;
private $model;

/**
* @inheritDoc
*/
protected function setUp()
{
$this->storeManagerMock = $this->createMock(StoreManagerInterface::class);
$this->requestMock = $this->getMockBuilder(RequestInterface::class)
->disableOriginalConstructor()
->setMethods(['getParam'])
Expand All @@ -117,7 +136,11 @@ protected function setUp()
$this->responseMock = $this->getMockBuilder(ResponseInterface::class)
->disableOriginalConstructor()
->getMockForAbstractClass();
$this->formStoreMock = $this->getMockBuilder(Store::class)
$this->fromStoreMock = $this->getMockBuilder(Store::class)
->disableOriginalConstructor()
->setMethods(['getCode'])
->getMockForAbstractClass();
$this->targetStoreMock = $this->getMockBuilder(Store::class)
->disableOriginalConstructor()
->setMethods(['getCode'])
->getMockForAbstractClass();
Expand Down Expand Up @@ -150,9 +173,10 @@ protected function setUp()
'messageManager' => $this->messageManagerMock,
]
);
$this->redirectController = $objectManager->getObject(
$this->model = $objectManager->getObject(
Redirect::class,
[
'storeManager' => $this->storeManagerMock,
'storeRepository' => $this->storeRepositoryMock,
'storeResolver' => $this->storeResolverMock,
'sidResolver' => $this->sidResolverMock,
Expand Down Expand Up @@ -186,19 +210,25 @@ public function testRedirect(string $defaultStoreViewCode, string $storeCode): v
$defaultStoreViewCode
);
$this->storeRepositoryMock
->expects($this->once())
->expects($this->exactly(2))
->method('get')
->with($defaultStoreViewCode)
->willReturn($this->formStoreMock);
$this->formStoreMock
->willReturnMap([
[$defaultStoreViewCode, $this->fromStoreMock],
[$storeCode, $this->targetStoreMock],
]);
$this->fromStoreMock
->expects($this->once())
->method('getCode')
->willReturn($defaultStoreViewCode);
$this->hashGeneratorMock
->expects($this->once())
->method('generateHash')
->with($this->formStoreMock)
->with($this->fromStoreMock)
->willReturn([]);
$this->storeManagerMock
->expects($this->once())
->method('setCurrentStore')
->with($this->targetStoreMock);
$this->redirectMock
->expects($this->once())
->method('redirect')
Expand All @@ -214,7 +244,7 @@ public function testRedirect(string $defaultStoreViewCode, string $storeCode): v
]
);

$this->assertEquals(null, $this->redirectController->execute());
$this->assertEquals(null, $this->model->execute());
}

/**
Expand Down Expand Up @@ -257,7 +287,7 @@ public function testRedirectWithThrowsException(string $defaultStoreViewCode, st
->with($this->responseMock, $this->currentStoreMock)
->willReturnSelf();

$this->assertEquals(null, $this->redirectController->execute());
$this->assertEquals(null, $this->model->execute());
}

/**
Expand All @@ -281,7 +311,7 @@ public function testRedirectTargetIsNull(): void
->expects($this->never())
->method('get');

$this->assertEquals($this->responseMock, $this->redirectController->execute());
$this->assertEquals($this->responseMock, $this->model->execute());
}

/**
Expand All @@ -292,7 +322,7 @@ public function testRedirectTargetIsNull(): void
public function getConfigDataProvider(): array
{
return [
[self::DEFAULT_STORE_VIEW_CODE, self::STORE_CODE]
[self::STUB_DEFAULT_STORE_VIEW_CODE, self::STUB_STORE_CODE]
];
}
}

0 comments on commit a998b2f

Please sign in to comment.