Skip to content

Commit

Permalink
Resolve Export Coupon Code Grid redirect to DashBoard when create New…
Browse files Browse the repository at this point in the history
… Cart Price Rule issue24468
  • Loading branch information
edenduong committed Nov 27, 2019
1 parent e95cca3 commit 5d175a3
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,32 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\SalesRule\Controller\Adminhtml\Promo\Quote;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Controller\ResultFactory;
use Magento\SalesRule\Controller\Adminhtml\Promo\Quote;
use Magento\SalesRule\Block\Adminhtml\Promo\Quote\Edit\Tab\Coupons\Grid;
use Magento\Framework\View\Result\Layout;
use Magento\Framework\App\ResponseInterface;

class ExportCouponsCsv extends \Magento\SalesRule\Controller\Adminhtml\Promo\Quote
class ExportCouponsCsv extends Quote
{
/**
* Export coupon codes as CSV file
*
* @return \Magento\Framework\App\ResponseInterface|null
* @return ResponseInterface|null
*/
public function execute()
{
$this->_initRule();
$rule = $this->_coreRegistry->registry(\Magento\SalesRule\Model\RegistryConstants::CURRENT_SALES_RULE);
if ($rule->getId()) {
$fileName = 'coupon_codes.csv';
$content = $this->_view->getLayout()->createBlock(
\Magento\SalesRule\Block\Adminhtml\Promo\Quote\Edit\Tab\Coupons\Grid::class
)->getCsvFile();
return $this->_fileFactory->create($fileName, $content, DirectoryList::VAR_DIR);
} else {
$this->_redirect('sales_rule/*/detail', ['_current' => true]);
return;
}
$fileName = 'coupon_codes.csv';
/** @var Layout $resultLayout */
$resultLayout = $this->resultFactory->create(ResultFactory::TYPE_LAYOUT);
$content = $resultLayout->getLayout()->createBlock(Grid::class)->getCsvFile();
return $this->_fileFactory->create($fileName, $content, DirectoryList::VAR_DIR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,32 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\SalesRule\Controller\Adminhtml\Promo\Quote;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Controller\ResultFactory;
use Magento\SalesRule\Controller\Adminhtml\Promo\Quote;
use Magento\SalesRule\Block\Adminhtml\Promo\Quote\Edit\Tab\Coupons\Grid;
use Magento\Framework\View\Result\Layout;
use Magento\Framework\App\ResponseInterface;

class ExportCouponsXml extends \Magento\SalesRule\Controller\Adminhtml\Promo\Quote
class ExportCouponsXml extends Quote
{
/**
* Export coupon codes as excel xml file
*
* @return \Magento\Framework\App\ResponseInterface|null
* @return ResponseInterface|null
*/
public function execute()
{
$this->_initRule();
$rule = $this->_coreRegistry->registry(\Magento\SalesRule\Model\RegistryConstants::CURRENT_SALES_RULE);
if ($rule->getId()) {
$fileName = 'coupon_codes.xml';
$content = $this->_view->getLayout()->createBlock(
\Magento\SalesRule\Block\Adminhtml\Promo\Quote\Edit\Tab\Coupons\Grid::class
)->getExcelFile(
$fileName
);
return $this->_fileFactory->create($fileName, $content, DirectoryList::VAR_DIR);
} else {
$this->_redirect('sales_rule/*/detail', ['_current' => true]);
return;
}
$fileName = 'coupon_codes.xml';
/** @var Layout $resultLayout */
$resultLayout = $this->resultFactory->create(ResultFactory::TYPE_LAYOUT);
$content = $resultLayout->getLayout()->createBlock(Grid::class)->getExcelFile($fileName);
return $this->_fileFactory->create($fileName, $content, DirectoryList::VAR_DIR);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\SalesRule\Test\Unit\Controller\Adminhtml\Promo\Quote;

use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use Magento\SalesRule\Controller\Adminhtml\Promo\Quote\ExportCouponsCsv;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\Response\Http\FileFactory;
use Magento\Framework\View\Result\Layout;
use Magento\Framework\View\LayoutInterface;
use Magento\Framework\View\Element\AbstractBlock;
use Magento\SalesRule\Block\Adminhtml\Promo\Quote\Edit\Tab\Coupons\Grid;
use PHPUnit\Framework\TestCase;

class ExportCouponsCsvTest extends TestCase
{
/**
* @var ExportCouponsCsv
*/
private $controller;

/**
* @var FileFactory|\PHPUnit_Framework_MockObject_MockObject
*/
private $fileFactoryMock;

/**
* @var ObjectManagerHelper
*/
private $objectManagerHelper;

/**
* @var ResultFactory|\PHPUnit_Framework_MockObject_MockObject
*/
private $resultFactoryMock;

/**
* Setup environment
*/
protected function setUp()
{
$this->objectManagerHelper = new ObjectManagerHelper($this);
$this->fileFactoryMock = $this->createMock(FileFactory::class);
$this->resultFactoryMock = $this->createMock(ResultFactory::class);

$this->controller = $this->objectManagerHelper->getObject(
ExportCouponsCsv::class,
[
'fileFactory' => $this->fileFactoryMock,
'resultFactory' => $this->resultFactoryMock
]
);
}

/**
* Test execute function
*/
public function testExecute()
{
$fileName = 'coupon_codes.csv';

$resultLayoutMock = $this->createMock(Layout::class);
$layoutMock = $this->createMock(LayoutInterface::class);
$contentMock = $this->createPartialMock(AbstractBlock::class, ['getCsvFile']);
$this->resultFactoryMock
->expects($this->once())
->method('create')
->with(ResultFactory::TYPE_LAYOUT)->willReturn($resultLayoutMock);
$resultLayoutMock->expects($this->once())->method('getLayout')->willReturn($layoutMock);
$layoutMock->expects($this->once())->method('createBlock')->with(Grid::class)
->willReturn($contentMock);
$contentMock->expects($this->once())->method('getCsvFile')->willReturn('csvFile');
$this->fileFactoryMock
->expects($this->once())
->method('create')
->with($fileName, 'csvFile', DirectoryList::VAR_DIR);

$this->controller->execute();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\SalesRule\Test\Unit\Controller\Adminhtml\Promo\Quote;

use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use Magento\SalesRule\Controller\Adminhtml\Promo\Quote\ExportCouponsXml;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\Response\Http\FileFactory;
use Magento\Framework\View\Result\Layout;
use Magento\Framework\View\LayoutInterface;
use Magento\Framework\View\Element\AbstractBlock;
use Magento\SalesRule\Block\Adminhtml\Promo\Quote\Edit\Tab\Coupons\Grid;
use PHPUnit\Framework\TestCase;

class ExportCouponsXmlTest extends TestCase
{
/**
* @var ExportCouponsXml
*/
private $controller;

/**
* @var FileFactory|\PHPUnit_Framework_MockObject_MockObject
*/
private $fileFactoryMock;

/**
* @var ObjectManagerHelper
*/
private $objectManagerHelper;

/**
* @var ResultFactory|\PHPUnit_Framework_MockObject_MockObject
*/
private $resultFactoryMock;

/**
* Setup environment
*/
protected function setUp()
{
$this->objectManagerHelper = new ObjectManagerHelper($this);
$this->fileFactoryMock = $this->createMock(FileFactory::class);
$this->resultFactoryMock = $this->createMock(ResultFactory::class);

$this->controller = $this->objectManagerHelper->getObject(
ExportCouponsXml::class,
[
'fileFactory' => $this->fileFactoryMock,
'resultFactory' => $this->resultFactoryMock
]
);
}

/**
* Test execute function
*/
public function testExecute()
{
$fileName = 'coupon_codes.xml';

$resultLayoutMock = $this->createMock(Layout::class);
$layoutMock = $this->createMock(LayoutInterface::class);
$contentMock = $this->createPartialMock(AbstractBlock::class, ['getExcelFile']);
$this->resultFactoryMock
->expects($this->once())
->method('create')
->with(ResultFactory::TYPE_LAYOUT)->willReturn($resultLayoutMock);
$resultLayoutMock->expects($this->once())->method('getLayout')->willReturn($layoutMock);
$layoutMock->expects($this->once())->method('createBlock')->with(Grid::class)
->willReturn($contentMock);
$contentMock->expects($this->once())->method('getExcelFile')
->with($fileName)
->willReturn('xmlFile');
$this->fileFactoryMock
->expects($this->once())
->method('create')
->with($fileName, 'xmlFile', DirectoryList::VAR_DIR);

$this->controller->execute();
}
}

0 comments on commit 5d175a3

Please sign in to comment.