Skip to content

Commit

Permalink
ENGCOM-2627: #16273: [Backport] Fix bug in method getUrlInStore() of …
Browse files Browse the repository at this point in the history
…product model #16310

 - Merge Pull Request #16310 from vasilii-b/magento2:#16273-product-geturlinstore-wrong-link_mage21
 - Merged commits:
   1. 2f8f469
   2. ecfbe24
   3. 7833f43
   4. 7c33686
   5. 726b6e9
  • Loading branch information
magento-engcom-team committed Aug 14, 2018
2 parents 3e387ea + 726b6e9 commit 9ff8ee2
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,36 @@ 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
*/
protected $model;

/**
* @return void
*/
protected function setUp()
{
$this->scopeConfigMock = $this->getMock('Magento\Framework\App\Config\ScopeConfigInterface');
$this->storeManagerMock = $this->getMock('Magento\Store\Model\StoreManagerInterface');
$this->queryParamsResolverMock = $this->getMock('Magento\Framework\Url\QueryParamsResolverInterface');
$this->scopeConfigMock = $this->getMock(\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->getMock(\Magento\Store\Model\StoreManagerInterface::class);
$this->storeManagerMock
->expects($this->once())
->method('getStore')
->willReturn($this->storeMock);

$this->queryParamsResolverMock = $this->getMock(\Magento\Framework\Url\QueryParamsResolverInterface::class);
$this->model = new \Magento\Store\Url\Plugin\RouteParamsResolver(
$this->scopeConfigMock,
$this->storeManagerMock,
Expand All @@ -40,11 +60,15 @@ protected function setUp()
}

/**
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
* @throws \Magento\Framework\Exception\NoSuchEntityException
*
* @return void
*/
public function testAroundSetRouteParamsScopeInParams()
public function testBeforeSetRouteParamsScopeInParams()
{
$storeCode = 'custom_store';
$data = ['_scope' => $storeCode, '_scope_to_url' => true];

$this->scopeConfigMock
->expects($this->once())
->method('getValue')
Expand All @@ -55,33 +79,33 @@ public function testAroundSetRouteParamsScopeInParams()
)
->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')

/** @var \PHPUnit_Framework_MockObject_MockObject $routeResolverMock */
$routeResolverMock = $this->getMockBuilder(\Magento\Framework\Url\RouteParamsResolver::class)
->setMethods(['setScope', 'getScope'])
->disableOriginalConstructor()
->getMock();
$routeParamsResolverMock->expects($this->once())->method('setScope')->with($storeCode);
$routeParamsResolverMock->expects($this->once())->method('getScope')->willReturn($storeCode);
$routeResolverMock->expects($this->once())->method('setScope')->with($storeCode);
$routeResolverMock->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->aroundSetRouteParams(
$routeParamsResolverMock,
function ($data, $unsetOldParams) {
$this->assertArrayNotHasKey('_scope_to_url', $data, 'This data item should have been unset.');
$this->assertArrayNotHasKey('_scope', $data, 'This data item should have been unset.');
},
$this->model->beforeSetRouteParams(
$routeResolverMock,
$data
);
}

/**
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
* @throws \Magento\Framework\Exception\NoSuchEntityException
*
* @return void
*/
public function testAroundSetRouteParamsScopeUseStoreInUrl()
public function testBeforeSetRouteParamsScopeUseStoreInUrl()
{
$storeCode = 'custom_store';
$data = ['_scope' => $storeCode, '_scope_to_url' => true];

$this->scopeConfigMock
->expects($this->once())
->method('getValue')
Expand All @@ -91,34 +115,35 @@ public function testAroundSetRouteParamsScopeUseStoreInUrl()
$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')

/** @var \PHPUnit_Framework_MockObject_MockObject $routeResolverMock */
$routeResolverMock = $this->getMockBuilder(\Magento\Framework\Url\RouteParamsResolver::class)
->setMethods(['setScope', 'getScope'])
->disableOriginalConstructor()
->getMock();
$routeParamsResolverMock->expects($this->once())->method('setScope')->with($storeCode);
$routeParamsResolverMock->expects($this->once())->method('getScope')->willReturn($storeCode);
$routeResolverMock->expects($this->once())->method('setScope')->with($storeCode);
$routeResolverMock->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->aroundSetRouteParams(
$routeParamsResolverMock,
function ($data, $unsetOldParams) {
$this->assertArrayNotHasKey('_scope_to_url', $data, 'This data item should have been unset.');
$this->assertArrayNotHasKey('_scope', $data, 'This data item should have been unset.');
},
$this->model->beforeSetRouteParams(
$routeResolverMock,
$data
);
}

/**
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
* @throws \Magento\Framework\Exception\NoSuchEntityException
*
* @return void
*/
public function testAroundSetRouteParamsSingleStore()
public function testBeforeSetRouteParamsSingleStore()
{
$storeCode = 'custom_store';
$data = ['_scope' => $storeCode, '_scope_to_url' => true];

$this->scopeConfigMock
->expects($this->once())
->method('getValue')
Expand All @@ -129,33 +154,33 @@ public function testAroundSetRouteParamsSingleStore()
)
->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')

/** @var \PHPUnit_Framework_MockObject_MockObject $routeResolverMock */
$routeResolverMock = $this->getMockBuilder(\Magento\Framework\Url\RouteParamsResolver::class)
->setMethods(['setScope', 'getScope'])
->disableOriginalConstructor()
->getMock();
$routeParamsResolverMock->expects($this->once())->method('setScope')->with($storeCode);
$routeParamsResolverMock->expects($this->once())->method('getScope')->willReturn($storeCode);
$routeResolverMock->expects($this->once())->method('setScope')->with($storeCode);
$routeResolverMock->expects($this->once())->method('getScope')->willReturn($storeCode);

$this->queryParamsResolverMock->expects($this->never())->method('setQueryParam');

$this->model->aroundSetRouteParams(
$routeParamsResolverMock,
function ($data, $unsetOldParams) {
$this->assertArrayNotHasKey('_scope_to_url', $data, 'This data item should have been unset.');
$this->assertArrayNotHasKey('_scope', $data, 'This data item should have been unset.');
},
$this->model->beforeSetRouteParams(
$routeResolverMock,
$data
);
}

/**
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
* @throws \Magento\Framework\Exception\NoSuchEntityException
*
* @return void
*/
public function testAroundSetRouteParamsNoScopeInParams()
public function testBeforeSetRouteParamsNoScopeInParams()
{
$storeCode = 'custom_store';
$data = ['_scope_to_url' => true];

$this->scopeConfigMock
->expects($this->once())
->method('getValue')
Expand All @@ -164,32 +189,22 @@ public function testAroundSetRouteParamsNoScopeInParams()
\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')
->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')
/** @var \PHPUnit_Framework_MockObject_MockObject $routeResolverMock */
$routeResolverMock = $this->getMockBuilder(\Magento\Framework\Url\RouteParamsResolver::class)
->setMethods(['setScope', 'getScope'])
->disableOriginalConstructor()
->getMock();
$routeParamsResolverMock->expects($this->never())->method('setScope');
$routeParamsResolverMock->expects($this->once())->method('getScope')->willReturn(false);
$routeResolverMock->expects($this->never())->method('setScope');
$routeResolverMock->expects($this->once())->method('getScope')->willReturn(false);

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

$this->model->aroundSetRouteParams(
$routeParamsResolverMock,
function ($data, $unsetOldParams) {
$this->assertArrayNotHasKey('_scope_to_url', $data, 'This data item should have been unset.');
},
$this->model->beforeSetRouteParams(
$routeResolverMock,
$data
);
}
Expand Down
17 changes: 12 additions & 5 deletions app/code/Magento/Store/Url/Plugin/RouteParamsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ public function __construct(
* @param callable $proceed
* @param array $data
* @param bool $unsetOldParams
* @throws \Magento\Framework\Exception\NoSuchEntityException
*
* @return \Magento\Framework\Url\RouteParamsResolver
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function aroundSetRouteParams(
public function beforeSetRouteParams(
\Magento\Framework\Url\RouteParamsResolver $subject,
\Closure $proceed,
array $data,
$unsetOldParams = true
) {
Expand All @@ -66,18 +67,24 @@ public function aroundSetRouteParams(
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 ?
$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);
}
}
unset($data['_scope_to_url']);

return $proceed($data, $unsetOldParams);
return [$data, $unsetOldParams];
}
}

0 comments on commit 9ff8ee2

Please sign in to comment.