Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Forwardport] #16273: Fix bug in method getUrlInStore() of product model #17261

Merged
merged 4 commits into from
Aug 2, 2018
Merged
Show file tree
Hide file tree
Changes from all 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,7 @@ 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 +91,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 +102,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 +113,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 +124,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 +136,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 +156,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 +166,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
13 changes: 11 additions & 2 deletions app/code/Magento/Store/Url/Plugin/RouteParamsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Magento\Store\Url\Plugin;

use \Magento\Store\Model\Store;
use \Magento\Store\Api\Data\StoreInterface;
use \Magento\Store\Model\ScopeInterface as StoreScopeInterface;

/**
Expand Down Expand Up @@ -51,6 +52,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 +66,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 StoreInterface $currentScope */
$currentScope = $subject->getScope();
$storeCode = $currentScope && $currentScope instanceof StoreInterface ?
$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