Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class RouteParamsResolverTest extends \PHPUnit\Framework\TestCase
*/
protected $queryParamsResolverMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Store\Model\Store
*/
protected $storeMock;

/**
* @var \Magento\Store\Url\Plugin\RouteParamsResolver
*/
Expand All @@ -30,7 +35,19 @@ class RouteParamsResolverTest extends \PHPUnit\Framework\TestCase
protected function setUp()
{
$this->scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);

$this->storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
->setMethods(['getCode'])
->disableOriginalConstructor()
->getMock();
$this->storeMock->expects($this->any())->method('getCode')->willReturn('custom_store');

$this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
$this->storeManagerMock
->expects($this->once())
->method('getStore')
->willReturn($this->storeMock);

$this->queryParamsResolverMock = $this->createMock(\Magento\Framework\Url\QueryParamsResolverInterface::class);
$this->model = new \Magento\Store\Url\Plugin\RouteParamsResolver(
$this->scopeConfigMock,
Expand All @@ -42,6 +59,8 @@ protected function setUp()
public function testBeforeSetRouteParamsScopeInParams()
{
$storeCode = 'custom_store';
$data = ['_scope' => $storeCode, '_scope_to_url' => true];

$this->scopeConfigMock
->expects($this->once())
->method('getValue')
Expand All @@ -52,7 +71,7 @@ public function testBeforeSetRouteParamsScopeInParams()
)
->will($this->returnValue(false));
$this->storeManagerMock->expects($this->any())->method('hasSingleStore')->willReturn(false);
$data = ['_scope' => $storeCode, '_scope_to_url' => true];

/** @var \PHPUnit_Framework_MockObject_MockObject $routeParamsResolverMock */
$routeParamsResolverMock = $this->getMockBuilder(\Magento\Framework\Url\RouteParamsResolver::class)
->setMethods(['setScope', 'getScope'])
Expand All @@ -61,7 +80,8 @@ public function testBeforeSetRouteParamsScopeInParams()
$routeParamsResolverMock->expects($this->once())->method('setScope')->with($storeCode);
$routeParamsResolverMock->expects($this->once())->method('getScope')->willReturn($storeCode);

$this->queryParamsResolverMock->expects($this->once())->method('setQueryParam')->with('___store', $storeCode);
$this->queryParamsResolverMock->expects($this->never())->method('setQueryParam');


$this->model->beforeSetRouteParams(
$routeParamsResolverMock,
Expand All @@ -72,6 +92,8 @@ public function testBeforeSetRouteParamsScopeInParams()
public function testBeforeSetRouteParamsScopeUseStoreInUrl()
{
$storeCode = 'custom_store';
$data = ['_scope' => $storeCode, '_scope_to_url' => true];

$this->scopeConfigMock
->expects($this->once())
->method('getValue')
Expand All @@ -81,8 +103,9 @@ public function testBeforeSetRouteParamsScopeUseStoreInUrl()
$storeCode
)
->will($this->returnValue(true));

$this->storeManagerMock->expects($this->any())->method('hasSingleStore')->willReturn(false);
$data = ['_scope' => $storeCode, '_scope_to_url' => true];

/** @var \PHPUnit_Framework_MockObject_MockObject $routeParamsResolverMock */
$routeParamsResolverMock = $this->getMockBuilder(\Magento\Framework\Url\RouteParamsResolver::class)
->setMethods(['setScope', 'getScope'])
Expand All @@ -91,7 +114,7 @@ public function testBeforeSetRouteParamsScopeUseStoreInUrl()
$routeParamsResolverMock->expects($this->once())->method('setScope')->with($storeCode);
$routeParamsResolverMock->expects($this->once())->method('getScope')->willReturn($storeCode);

$this->queryParamsResolverMock->expects($this->never())->method('setQueryParam');
$this->queryParamsResolverMock->expects($this->once())->method('setQueryParam')->with('___store', $storeCode);

$this->model->beforeSetRouteParams(
$routeParamsResolverMock,
Expand All @@ -102,6 +125,8 @@ public function testBeforeSetRouteParamsScopeUseStoreInUrl()
public function testBeforeSetRouteParamsSingleStore()
{
$storeCode = 'custom_store';
$data = ['_scope' => $storeCode, '_scope_to_url' => true];

$this->scopeConfigMock
->expects($this->once())
->method('getValue')
Expand All @@ -112,7 +137,7 @@ public function testBeforeSetRouteParamsSingleStore()
)
->will($this->returnValue(false));
$this->storeManagerMock->expects($this->any())->method('hasSingleStore')->willReturn(true);
$data = ['_scope' => $storeCode, '_scope_to_url' => true];

/** @var \PHPUnit_Framework_MockObject_MockObject $routeParamsResolverMock */
$routeParamsResolverMock = $this->getMockBuilder(\Magento\Framework\Url\RouteParamsResolver::class)
->setMethods(['setScope', 'getScope'])
Expand All @@ -132,6 +157,8 @@ public function testBeforeSetRouteParamsSingleStore()
public function testBeforeSetRouteParamsNoScopeInParams()
{
$storeCode = 'custom_store';
$data = ['_scope_to_url' => true];

$this->scopeConfigMock
->expects($this->once())
->method('getValue')
Expand All @@ -140,17 +167,10 @@ public function testBeforeSetRouteParamsNoScopeInParams()
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$storeCode
)
->will($this->returnValue(false));
->will($this->returnValue(true));

$this->storeManagerMock->expects($this->any())->method('hasSingleStore')->willReturn(false);
/** @var \PHPUnit_Framework_MockObject_MockObject| $routeParamsResolverMock */
$storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
->setMethods(['getCode'])
->disableOriginalConstructor()
->getMock();
$storeMock->expects($this->any())->method('getCode')->willReturn($storeCode);
$this->storeManagerMock->expects($this->any())->method('getStore')->willReturn($storeMock);

$data = ['_scope_to_url' => true];
/** @var \PHPUnit_Framework_MockObject_MockObject $routeParamsResolverMock */
$routeParamsResolverMock = $this->getMockBuilder(\Magento\Framework\Url\RouteParamsResolver::class)
->setMethods(['setScope', 'getScope'])
Expand Down
12 changes: 10 additions & 2 deletions app/code/Magento/Store/Url/Plugin/RouteParamsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public function __construct(
* @param \Magento\Framework\Url\RouteParamsResolver $subject
* @param array $data
* @param bool $unsetOldParams
* @throws \Magento\Framework\Exception\NoSuchEntityException
*
* @return array
*/
public function beforeSetRouteParams(
Expand All @@ -63,13 +65,19 @@ public function beforeSetRouteParams(
unset($data['_scope']);
}
if (isset($data['_scope_to_url']) && (bool)$data['_scope_to_url'] === true) {
$storeCode = $subject->getScope() ?: $this->storeManager->getStore()->getCode();
/** @var Store $currentScope */
$currentScope = $subject->getScope();
$storeCode = $currentScope && $currentScope instanceof Store ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to check for StoreInterface in this case?

$currentScope->getCode() :
$this->storeManager->getStore()->getCode();

$useStoreInUrl = $this->scopeConfig->getValue(
Store::XML_PATH_STORE_IN_URL,
StoreScopeInterface::SCOPE_STORE,
$storeCode
);
if (!$useStoreInUrl && !$this->storeManager->hasSingleStore()) {

if ($useStoreInUrl && !$this->storeManager->hasSingleStore()) {
$this->queryParamsResolver->setQueryParam('___store', $storeCode);
}
}
Expand Down