Skip to content

Commit

Permalink
⏫ Forwardport of #12732 to 2.3-develop branch
Browse files Browse the repository at this point in the history
Applied pull request patch https://github.com/magento/magento2/pull/12732.patch (created by @ihor-sviziev) based on commit(s):
  1. ecf27be
  2. 5e4bf42

Fixed GitHub Issues in 2.3-develop branch:
  - #12206: Tracking link returns 404 page in admin panel (reported by @catstantin)
  • Loading branch information
magento-engcom-team committed Jan 24, 2018
1 parent 8e77e2f commit 14f85e9
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 18 deletions.
24 changes: 19 additions & 5 deletions app/code/Magento/Shipping/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
*/
namespace Magento\Shipping\Helper;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\UrlInterface;
use Magento\Store\Model\StoreManagerInterface;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
/**
Expand All @@ -19,19 +23,28 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
protected $_allowedHashKeys = ['ship_id', 'order_id', 'track_id'];

/**
* @var \Magento\Store\Model\StoreManagerInterface
* @var StoreManagerInterface
*/
protected $_storeManager;

/**
* @var UrlInterface|null
*/
private $url;

/**
* @param \Magento\Framework\App\Helper\Context $context
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param StoreManagerInterface $storeManager
* @param UrlInterface|null $url
*/
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Store\Model\StoreManagerInterface $storeManager
StoreManagerInterface $storeManager,
UrlInterface $url = null
) {
$this->_storeManager = $storeManager;
$this->url = $url ?: ObjectManager::getInstance()->get(UrlInterface::class);

parent::__construct($context);
}

Expand Down Expand Up @@ -63,12 +76,13 @@ protected function _getTrackingUrl($key, $model, $method = 'getId')
{
$urlPart = "{$key}:{$model->{$method}()}:{$model->getProtectCode()}";
$params = [
'_scope' => $model->getStoreId(),
'_nosid' => true,
'_direct' => 'shipping/tracking/popup',
'_query' => ['hash' => $this->urlEncoder->encode($urlPart)]
];

$storeModel = $this->_storeManager->getStore($model->getStoreId());
return $storeModel->getUrl('', $params);
return $this->url->getUrl('', $params);
}

/**
Expand Down
7 changes: 7 additions & 0 deletions app/code/Magento/Shipping/etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,11 @@
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Shipping\Model\Shipping" type="Magento\Shipping\Model\Shipping\Labels" />

<type name="Magento\Shipping\Helper\Data">
<arguments>
<!-- Use frontend URL model-->
<argument name="url" xsi:type="object">Magento\Framework\Url</argument>
</arguments>
</type>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
*/
namespace Magento\Shipping\Helper;

use Magento\Store\Model\StoreManagerInterface;

class DataTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Shipping\Helper\Data
*/
protected $_helper = null;
private $helper;

protected function setUp()
{
$this->_helper = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
$this->helper = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
\Magento\Shipping\Helper\Data::class
);
}
Expand All @@ -31,33 +33,77 @@ public function testGetTrackingPopupUrlBySalesModel($modelName, $getIdMethod, $e
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$constructArgs = [];
if (\Magento\Sales\Model\Order\Shipment::class == $modelName) {
$orderRepository = $this->_getMockOrderRepository($code);
if (\Magento\Sales\Model\Order\Shipment::class === $modelName) {
$orderRepository = $this->getMockOrderRepository($code);
$constructArgs['orderRepository'] = $orderRepository;
} elseif (\Magento\Sales\Model\Order\Shipment\Track::class === $modelName) {
$shipmentRepository = $this->getMockShipmentRepository($code);
$constructArgs['shipmentRepository'] = $shipmentRepository;
}

$model = $objectManager->create($modelName, $constructArgs);
$model->{$getIdMethod}($entityId);

if (\Magento\Sales\Model\Order::class === $modelName) {
$model->setProtectCode($code);
}
if (\Magento\Sales\Model\Order\Shipment\Track::class === $modelName) {
$model->setParentId(1);
}

$actual = $this->helper->getTrackingPopupUrlBySalesModel($model);
$this->assertEquals($expected, $actual);
}

/**
* From the admin panel with custom URL we should have generated frontend URL
*
* @param string $modelName
* @param string $getIdMethod
* @param int $entityId
* @param string $code
* @param string $expected
* @magentoAppArea adminhtml
* @magentoConfigFixture admin_store web/unsecure/base_link_url http://admin.localhost/
* @dataProvider getTrackingPopupUrlBySalesModelDataProvider
*/
public function testGetTrackingPopupUrlBySalesModelFromAdmin($modelName, $getIdMethod, $entityId, $code, $expected)
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();

/** @var StoreManagerInterface $storeManager */
$storeManager = $objectManager->create(StoreManagerInterface::class);
$storeManager->reinitStores();

$constructArgs = [];
if (\Magento\Sales\Model\Order\Shipment::class === $modelName) {
$orderRepository = $this->getMockOrderRepository($code);
$constructArgs['orderRepository'] = $orderRepository;
} elseif (\Magento\Sales\Model\Order\Shipment\Track::class == $modelName) {
$shipmentRepository = $this->_getMockShipmentRepository($code);
} elseif (\Magento\Sales\Model\Order\Shipment\Track::class === $modelName) {
$shipmentRepository = $this->getMockShipmentRepository($code);
$constructArgs['shipmentRepository'] = $shipmentRepository;
}

$model = $objectManager->create($modelName, $constructArgs);
$model->{$getIdMethod}($entityId);

if (\Magento\Sales\Model\Order::class == $modelName) {
if (\Magento\Sales\Model\Order::class === $modelName) {
$model->setProtectCode($code);
}
if (\Magento\Sales\Model\Order\Shipment\Track::class == $modelName) {
if (\Magento\Sales\Model\Order\Shipment\Track::class === $modelName) {
$model->setParentId(1);
}

$actual = $this->_helper->getTrackingPopupUrlBySalesModel($model);
//Frontend URL should be used there
$actual = $this->helper->getTrackingPopupUrlBySalesModel($model);
$this->assertEquals($expected, $actual);
}

/**
* @param $code
* @return \Magento\Sales\Api\OrderRepositoryInterface
* @return \Magento\Sales\Api\OrderRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected function _getMockOrderRepository($code)
private function getMockOrderRepository($code)
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$order = $objectManager->create(\Magento\Sales\Model\Order::class);
Expand All @@ -71,10 +117,10 @@ protected function _getMockOrderRepository($code)
* @param $code
* @return \Magento\Sales\Model\Order\ShipmentRepository|\PHPUnit_Framework_MockObject_MockObject
*/
protected function _getMockShipmentRepository($code)
private function getMockShipmentRepository($code)
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$orderRepository = $this->_getMockOrderRepository($code);
$orderRepository = $this->getMockOrderRepository($code);
$shipmentArgs = ['orderRepository' => $orderRepository];

$shipment = $objectManager->create(\Magento\Sales\Model\Order\Shipment::class, $shipmentArgs);
Expand Down

0 comments on commit 14f85e9

Please sign in to comment.