Skip to content

Commit 5d175a3

Browse files
committed
Resolve Export Coupon Code Grid redirect to DashBoard when create New Cart Price Rule issue24468
1 parent e95cca3 commit 5d175a3

File tree

4 files changed

+206
-28
lines changed

4 files changed

+206
-28
lines changed

app/code/Magento/SalesRule/Controller/Adminhtml/Promo/Quote/ExportCouponsCsv.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,32 @@
44
* Copyright © Magento, Inc. All rights reserved.
55
* See COPYING.txt for license details.
66
*/
7+
8+
declare(strict_types=1);
9+
710
namespace Magento\SalesRule\Controller\Adminhtml\Promo\Quote;
811

912
use Magento\Framework\App\Filesystem\DirectoryList;
13+
use Magento\Framework\Controller\ResultFactory;
14+
use Magento\SalesRule\Controller\Adminhtml\Promo\Quote;
15+
use Magento\SalesRule\Block\Adminhtml\Promo\Quote\Edit\Tab\Coupons\Grid;
16+
use Magento\Framework\View\Result\Layout;
17+
use Magento\Framework\App\ResponseInterface;
1018

11-
class ExportCouponsCsv extends \Magento\SalesRule\Controller\Adminhtml\Promo\Quote
19+
class ExportCouponsCsv extends Quote
1220
{
1321
/**
1422
* Export coupon codes as CSV file
1523
*
16-
* @return \Magento\Framework\App\ResponseInterface|null
24+
* @return ResponseInterface|null
1725
*/
1826
public function execute()
1927
{
2028
$this->_initRule();
21-
$rule = $this->_coreRegistry->registry(\Magento\SalesRule\Model\RegistryConstants::CURRENT_SALES_RULE);
22-
if ($rule->getId()) {
23-
$fileName = 'coupon_codes.csv';
24-
$content = $this->_view->getLayout()->createBlock(
25-
\Magento\SalesRule\Block\Adminhtml\Promo\Quote\Edit\Tab\Coupons\Grid::class
26-
)->getCsvFile();
27-
return $this->_fileFactory->create($fileName, $content, DirectoryList::VAR_DIR);
28-
} else {
29-
$this->_redirect('sales_rule/*/detail', ['_current' => true]);
30-
return;
31-
}
29+
$fileName = 'coupon_codes.csv';
30+
/** @var Layout $resultLayout */
31+
$resultLayout = $this->resultFactory->create(ResultFactory::TYPE_LAYOUT);
32+
$content = $resultLayout->getLayout()->createBlock(Grid::class)->getCsvFile();
33+
return $this->_fileFactory->create($fileName, $content, DirectoryList::VAR_DIR);
3234
}
3335
}

app/code/Magento/SalesRule/Controller/Adminhtml/Promo/Quote/ExportCouponsXml.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,32 @@
44
* Copyright © Magento, Inc. All rights reserved.
55
* See COPYING.txt for license details.
66
*/
7+
8+
declare(strict_types=1);
9+
710
namespace Magento\SalesRule\Controller\Adminhtml\Promo\Quote;
811

912
use Magento\Framework\App\Filesystem\DirectoryList;
13+
use Magento\Framework\Controller\ResultFactory;
14+
use Magento\SalesRule\Controller\Adminhtml\Promo\Quote;
15+
use Magento\SalesRule\Block\Adminhtml\Promo\Quote\Edit\Tab\Coupons\Grid;
16+
use Magento\Framework\View\Result\Layout;
17+
use Magento\Framework\App\ResponseInterface;
1018

11-
class ExportCouponsXml extends \Magento\SalesRule\Controller\Adminhtml\Promo\Quote
19+
class ExportCouponsXml extends Quote
1220
{
1321
/**
1422
* Export coupon codes as excel xml file
1523
*
16-
* @return \Magento\Framework\App\ResponseInterface|null
24+
* @return ResponseInterface|null
1725
*/
1826
public function execute()
1927
{
2028
$this->_initRule();
21-
$rule = $this->_coreRegistry->registry(\Magento\SalesRule\Model\RegistryConstants::CURRENT_SALES_RULE);
22-
if ($rule->getId()) {
23-
$fileName = 'coupon_codes.xml';
24-
$content = $this->_view->getLayout()->createBlock(
25-
\Magento\SalesRule\Block\Adminhtml\Promo\Quote\Edit\Tab\Coupons\Grid::class
26-
)->getExcelFile(
27-
$fileName
28-
);
29-
return $this->_fileFactory->create($fileName, $content, DirectoryList::VAR_DIR);
30-
} else {
31-
$this->_redirect('sales_rule/*/detail', ['_current' => true]);
32-
return;
33-
}
29+
$fileName = 'coupon_codes.xml';
30+
/** @var Layout $resultLayout */
31+
$resultLayout = $this->resultFactory->create(ResultFactory::TYPE_LAYOUT);
32+
$content = $resultLayout->getLayout()->createBlock(Grid::class)->getExcelFile($fileName);
33+
return $this->_fileFactory->create($fileName, $content, DirectoryList::VAR_DIR);
3434
}
3535
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\SalesRule\Test\Unit\Controller\Adminhtml\Promo\Quote;
10+
11+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
12+
use Magento\SalesRule\Controller\Adminhtml\Promo\Quote\ExportCouponsCsv;
13+
use Magento\Framework\Controller\ResultFactory;
14+
use Magento\Framework\App\Filesystem\DirectoryList;
15+
use Magento\Framework\App\Response\Http\FileFactory;
16+
use Magento\Framework\View\Result\Layout;
17+
use Magento\Framework\View\LayoutInterface;
18+
use Magento\Framework\View\Element\AbstractBlock;
19+
use Magento\SalesRule\Block\Adminhtml\Promo\Quote\Edit\Tab\Coupons\Grid;
20+
use PHPUnit\Framework\TestCase;
21+
22+
class ExportCouponsCsvTest extends TestCase
23+
{
24+
/**
25+
* @var ExportCouponsCsv
26+
*/
27+
private $controller;
28+
29+
/**
30+
* @var FileFactory|\PHPUnit_Framework_MockObject_MockObject
31+
*/
32+
private $fileFactoryMock;
33+
34+
/**
35+
* @var ObjectManagerHelper
36+
*/
37+
private $objectManagerHelper;
38+
39+
/**
40+
* @var ResultFactory|\PHPUnit_Framework_MockObject_MockObject
41+
*/
42+
private $resultFactoryMock;
43+
44+
/**
45+
* Setup environment
46+
*/
47+
protected function setUp()
48+
{
49+
$this->objectManagerHelper = new ObjectManagerHelper($this);
50+
$this->fileFactoryMock = $this->createMock(FileFactory::class);
51+
$this->resultFactoryMock = $this->createMock(ResultFactory::class);
52+
53+
$this->controller = $this->objectManagerHelper->getObject(
54+
ExportCouponsCsv::class,
55+
[
56+
'fileFactory' => $this->fileFactoryMock,
57+
'resultFactory' => $this->resultFactoryMock
58+
]
59+
);
60+
}
61+
62+
/**
63+
* Test execute function
64+
*/
65+
public function testExecute()
66+
{
67+
$fileName = 'coupon_codes.csv';
68+
69+
$resultLayoutMock = $this->createMock(Layout::class);
70+
$layoutMock = $this->createMock(LayoutInterface::class);
71+
$contentMock = $this->createPartialMock(AbstractBlock::class, ['getCsvFile']);
72+
$this->resultFactoryMock
73+
->expects($this->once())
74+
->method('create')
75+
->with(ResultFactory::TYPE_LAYOUT)->willReturn($resultLayoutMock);
76+
$resultLayoutMock->expects($this->once())->method('getLayout')->willReturn($layoutMock);
77+
$layoutMock->expects($this->once())->method('createBlock')->with(Grid::class)
78+
->willReturn($contentMock);
79+
$contentMock->expects($this->once())->method('getCsvFile')->willReturn('csvFile');
80+
$this->fileFactoryMock
81+
->expects($this->once())
82+
->method('create')
83+
->with($fileName, 'csvFile', DirectoryList::VAR_DIR);
84+
85+
$this->controller->execute();
86+
}
87+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\SalesRule\Test\Unit\Controller\Adminhtml\Promo\Quote;
10+
11+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
12+
use Magento\SalesRule\Controller\Adminhtml\Promo\Quote\ExportCouponsXml;
13+
use Magento\Framework\Controller\ResultFactory;
14+
use Magento\Framework\App\Filesystem\DirectoryList;
15+
use Magento\Framework\App\Response\Http\FileFactory;
16+
use Magento\Framework\View\Result\Layout;
17+
use Magento\Framework\View\LayoutInterface;
18+
use Magento\Framework\View\Element\AbstractBlock;
19+
use Magento\SalesRule\Block\Adminhtml\Promo\Quote\Edit\Tab\Coupons\Grid;
20+
use PHPUnit\Framework\TestCase;
21+
22+
class ExportCouponsXmlTest extends TestCase
23+
{
24+
/**
25+
* @var ExportCouponsXml
26+
*/
27+
private $controller;
28+
29+
/**
30+
* @var FileFactory|\PHPUnit_Framework_MockObject_MockObject
31+
*/
32+
private $fileFactoryMock;
33+
34+
/**
35+
* @var ObjectManagerHelper
36+
*/
37+
private $objectManagerHelper;
38+
39+
/**
40+
* @var ResultFactory|\PHPUnit_Framework_MockObject_MockObject
41+
*/
42+
private $resultFactoryMock;
43+
44+
/**
45+
* Setup environment
46+
*/
47+
protected function setUp()
48+
{
49+
$this->objectManagerHelper = new ObjectManagerHelper($this);
50+
$this->fileFactoryMock = $this->createMock(FileFactory::class);
51+
$this->resultFactoryMock = $this->createMock(ResultFactory::class);
52+
53+
$this->controller = $this->objectManagerHelper->getObject(
54+
ExportCouponsXml::class,
55+
[
56+
'fileFactory' => $this->fileFactoryMock,
57+
'resultFactory' => $this->resultFactoryMock
58+
]
59+
);
60+
}
61+
62+
/**
63+
* Test execute function
64+
*/
65+
public function testExecute()
66+
{
67+
$fileName = 'coupon_codes.xml';
68+
69+
$resultLayoutMock = $this->createMock(Layout::class);
70+
$layoutMock = $this->createMock(LayoutInterface::class);
71+
$contentMock = $this->createPartialMock(AbstractBlock::class, ['getExcelFile']);
72+
$this->resultFactoryMock
73+
->expects($this->once())
74+
->method('create')
75+
->with(ResultFactory::TYPE_LAYOUT)->willReturn($resultLayoutMock);
76+
$resultLayoutMock->expects($this->once())->method('getLayout')->willReturn($layoutMock);
77+
$layoutMock->expects($this->once())->method('createBlock')->with(Grid::class)
78+
->willReturn($contentMock);
79+
$contentMock->expects($this->once())->method('getExcelFile')
80+
->with($fileName)
81+
->willReturn('xmlFile');
82+
$this->fileFactoryMock
83+
->expects($this->once())
84+
->method('create')
85+
->with($fileName, 'xmlFile', DirectoryList::VAR_DIR);
86+
87+
$this->controller->execute();
88+
}
89+
}

0 commit comments

Comments
 (0)