Skip to content

Commit 4a6f60d

Browse files
[Magento Community Engineering] Community Contributions - 2.4-develop-fast-lane-prs
- merged with '2.4-develop-temporary-five-prs' branch
2 parents 70a467a + ed84144 commit 4a6f60d

File tree

4 files changed

+218
-30
lines changed

4 files changed

+218
-30
lines changed
Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,40 @@
11
<?php
22
/**
3-
*
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
6+
7+
declare(strict_types=1);
8+
79
namespace Magento\SalesRule\Controller\Adminhtml\Promo\Quote;
810

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

11-
class ExportCouponsCsv extends \Magento\SalesRule\Controller\Adminhtml\Promo\Quote
19+
/**
20+
* Export Coupons to csv file
21+
*
22+
* Class \Magento\SalesRule\Controller\Adminhtml\Promo\Quote\ExportCouponsCsv
23+
*/
24+
class ExportCouponsCsv extends Quote implements HttpGetActionInterface
1225
{
1326
/**
1427
* Export coupon codes as CSV file
1528
*
16-
* @return \Magento\Framework\App\ResponseInterface|null
29+
* @return ResponseInterface|null
1730
*/
1831
public function execute()
1932
{
2033
$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-
}
34+
$fileName = 'coupon_codes.csv';
35+
/** @var Layout $resultLayout */
36+
$resultLayout = $this->resultFactory->create(ResultFactory::TYPE_LAYOUT);
37+
$content = $resultLayout->getLayout()->createBlock(Grid::class)->getCsvFile();
38+
return $this->_fileFactory->create($fileName, $content, DirectoryList::VAR_DIR);
3239
}
3340
}
Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
11
<?php
22
/**
3-
*
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
6+
7+
declare(strict_types=1);
8+
79
namespace Magento\SalesRule\Controller\Adminhtml\Promo\Quote;
810

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

11-
class ExportCouponsXml extends \Magento\SalesRule\Controller\Adminhtml\Promo\Quote
19+
/**
20+
* Export coupons to xml file
21+
*
22+
* Class \Magento\SalesRule\Controller\Adminhtml\Promo\Quote\ExportCouponsXml
23+
*/
24+
class ExportCouponsXml extends Quote implements HttpGetActionInterface
1225
{
1326
/**
1427
* Export coupon codes as excel xml file
1528
*
16-
* @return \Magento\Framework\App\ResponseInterface|null
29+
* @return ResponseInterface|null
1730
*/
1831
public function execute()
1932
{
2033
$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-
}
34+
$fileName = 'coupon_codes.xml';
35+
/** @var Layout $resultLayout */
36+
$resultLayout = $this->resultFactory->create(ResultFactory::TYPE_LAYOUT);
37+
$content = $resultLayout->getLayout()->createBlock(Grid::class)->getExcelFile($fileName);
38+
return $this->_fileFactory->create($fileName, $content, DirectoryList::VAR_DIR);
3439
}
3540
}
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)