Skip to content

Commit

Permalink
Merge pull request #597 from magento-tsg/2.0-develop-pr1
Browse files Browse the repository at this point in the history
[TSG] Backporting for 2.0
  • Loading branch information
Volodymyr Klymenko authored Nov 16, 2016
2 parents ca68e6e + 62af13f commit 21468d1
Show file tree
Hide file tree
Showing 25 changed files with 664 additions and 210 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
*/
namespace Magento\Catalog\Controller\Adminhtml\Product;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Catalog\Controller\Adminhtml\Product;
use Magento\Catalog\Model\Indexer\Product\Price\Processor;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Framework\Controller\ResultFactory;
use Magento\Ui\Component\MassAction\Filter;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;

class MassStatus extends \Magento\Catalog\Controller\Adminhtml\Product
{
/**
* @var \Magento\Catalog\Model\Indexer\Product\Price\Processor
* @var Processor
*/
protected $_productPriceIndexerProcessor;

Expand All @@ -32,22 +33,23 @@ class MassStatus extends \Magento\Catalog\Controller\Adminhtml\Product
protected $collectionFactory;

/**
* @param Action\Context $context
* @param Context $context
* @param Builder $productBuilder
* @param \Magento\Catalog\Model\Indexer\Product\Price\Processor $productPriceIndexerProcessor
* @param Processor $productPriceIndexerProcessor
* @param Filter $filter
* @param CollectionFactory $collectionFactory
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
Context $context,
Product\Builder $productBuilder,
\Magento\Catalog\Model\Indexer\Product\Price\Processor $productPriceIndexerProcessor,
Processor $productPriceIndexerProcessor,
Filter $filter,
CollectionFactory $collectionFactory
) {
$this->filter = $filter;
$this->collectionFactory = $collectionFactory;
$this->_productPriceIndexerProcessor = $productPriceIndexerProcessor;

parent::__construct($context, $productBuilder);
}

Expand Down Expand Up @@ -82,6 +84,14 @@ public function execute()
$storeId = (int) $this->getRequest()->getParam('store', 0);
$status = (int) $this->getRequest()->getParam('status');

/** @var array $filters */
$filters = (array) $this->getRequest()->getParam('filters', []);

if (isset($filters['store_id'])) {
/** @var int $storeId */
$storeId = (int) $filters['store_id'];
}

try {
$this->_validateMassStatus($productIds, $status);
$this->_objectManager->get('Magento\Catalog\Model\Product\Action')
Expand All @@ -96,6 +106,7 @@ public function execute()

/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);

return $resultRedirect->setPath('catalog/*/', ['store' => $storeId]);
}
}
18 changes: 14 additions & 4 deletions app/code/Magento/Catalog/Model/Product/Option/Type/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\Catalog\Model\Product\Option\Type;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem;
use Magento\Framework\Exception\LocalizedException;
use Magento\Catalog\Model\Product\Exception as ProductException;
Expand Down Expand Up @@ -69,17 +70,23 @@ class File extends \Magento\Catalog\Model\Product\Option\Type\DefaultType
*/
protected $validatorFile;

/**
* @var Filesystem
*/
private $filesystem;

/**
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Quote\Model\Quote\Item\OptionFactory $itemOptionFactory
* @param \Magento\Catalog\Model\Product\Option\UrlBuilder $urlBuilder
* @param \Magento\Framework\Escaper $escaper
* @param \Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDatabase
* @param File\ValidatorInfo $validatorInfo
* @param File\ValidatorFile $validatorFile
* @param \Magento\Catalog\Model\Product\Option\UrlBuilder $urlBuilder
* @param \Magento\Framework\Escaper $escaper
* @param array $data
* @throws \Magento\Framework\Exception\FileSystemException
* @param Filesystem $filesystem
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
\Magento\Checkout\Model\Session $checkoutSession,
Expand All @@ -90,12 +97,15 @@ public function __construct(
\Magento\Catalog\Model\Product\Option\Type\File\ValidatorFile $validatorFile,
\Magento\Catalog\Model\Product\Option\UrlBuilder $urlBuilder,
\Magento\Framework\Escaper $escaper,
array $data = []
array $data = [],
Filesystem $filesystem = null
) {
$this->_itemOptionFactory = $itemOptionFactory;
$this->_urlBuilder = $urlBuilder;
$this->_escaper = $escaper;
$this->_coreFileStorageDatabase = $coreFileStorageDatabase;
$this->filesystem = $filesystem ?: \Magento\Framework\App\ObjectManager::getInstance()->get(Filesystem::class);
$this->_rootDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
$this->validatorInfo = $validatorInfo;
$this->validatorFile = $validatorFile;
parent::__construct($checkoutSession, $scopeConfig, $data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,80 +6,165 @@
*/
namespace Magento\Catalog\Test\Unit\Controller\Adminhtml\Product;

class MassStatusTest extends \Magento\Catalog\Test\Unit\Controller\Adminhtml\ProductTest
use Magento\Backend\Model\View\Result\Redirect;
use Magento\Catalog\Controller\Adminhtml\Product\Builder;
use Magento\Catalog\Controller\Adminhtml\Product\MassStatus;
use Magento\Catalog\Model\Indexer\Product\Price\Processor;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Action;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
use Magento\Framework\App\Request\Http;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\ObjectManager\ObjectManager as Manager;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Ui\Component\MassAction\Filter;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class MassStatusTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $priceProcessor;
private $priceProcessorMock;

/**
* @var \Magento\Catalog\Controller\Adminhtml\Product\MassStatus
*/
private $action;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $abstractDbMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $filterMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $requestMock;

/** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Backend\Model\View\Result\Redirect */
protected $resultRedirect;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $actionMock;

protected function setUp()
{
$this->priceProcessor = $this->getMockBuilder('Magento\Catalog\Model\Indexer\Product\Price\Processor')
$objectManagerMock = $this->getMockBuilder(Manager::class)
->disableOriginalConstructor()
->getMock();

$objectManagerHelper = new ObjectManager($this);
$this->priceProcessorMock = $this->getMockBuilder(Processor::class)
->disableOriginalConstructor()->getMock();

$productBuilder = $this->getMockBuilder('Magento\Catalog\Controller\Adminhtml\Product\Builder')->setMethods([
'build',
])->disableOriginalConstructor()->getMock();
$productBuilderMock = $this->getMockBuilder(Builder::class)->setMethods([
'build',
])->disableOriginalConstructor()->getMock();

$product = $this->getMockBuilder('\Magento\Catalog\Model\Product')->disableOriginalConstructor()
$this->requestMock = $this->getMockBuilder(Http::class)->setMethods(
['getParam', 'getPost', 'getFullActionName', 'getPostValue']
)->disableOriginalConstructor()->getMock();

$productMock = $this->getMockBuilder(Product::class)->disableOriginalConstructor()
->setMethods(['getTypeId', 'getStoreId', '__sleep', '__wakeup'])->getMock();
$product->expects($this->any())->method('getTypeId')->will($this->returnValue('simple'));
$product->expects($this->any())->method('getStoreId')->will($this->returnValue('1'));
$productBuilder->expects($this->any())->method('build')->will($this->returnValue($product));
$productMock->expects($this->any())->method('getTypeId')->will($this->returnValue('simple'));
$productMock->expects($this->any())->method('getStoreId')->will($this->returnValue('1'));
$productBuilderMock->expects($this->any())->method('build')->will($this->returnValue($productMock));

$this->resultRedirect = $this->getMockBuilder('Magento\Backend\Model\View\Result\Redirect')
$resultRedirectMock = $this->getMockBuilder(Redirect::class)
->disableOriginalConstructor()
->getMock();
$resultFactory = $this->getMockBuilder('Magento\Framework\Controller\ResultFactory')
$resultFactory = $this->getMockBuilder(ResultFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();
$resultFactory->expects($this->atLeastOnce())
->method('create')
->with(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT)
->willReturn($this->resultRedirect);
->with(ResultFactory::TYPE_REDIRECT)
->willReturn($resultRedirectMock);

$abstractDbMock = $this->getMockBuilder('Magento\Framework\Data\Collection\AbstractDb')
$this->abstractDbMock = $this->getMockBuilder(AbstractDb::class)
->disableOriginalConstructor()
->setMethods(['getAllIds', 'getResource'])
->getMock();
$abstractDbMock->expects($this->any())
$this->abstractDbMock->expects($this->any())
->method('getAllIds')
->willReturn([]);

$filterMock = $this->getMockBuilder('Magento\Ui\Component\MassAction\Filter')
$this->filterMock = $this->getMockBuilder(Filter::class)
->disableOriginalConstructor()
->setMethods(['getCollection'])
->getMock();
$filterMock->expects($this->any())
$this->filterMock->expects($this->any())
->method('getCollection')
->willReturn($abstractDbMock);

$collectionFactoryMock = $this->getMockBuilder('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory')
->willReturn($this->abstractDbMock);
$this->actionMock = $this->getMockBuilder(Action::class)
->disableOriginalConstructor()
->getMock();
$objectManagerMock->expects($this->any())->method('get')->willReturn($this->actionMock);
$collectionFactoryMock = $this->getMockBuilder(ProductCollectionFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();
$collectionFactoryMock->expects($this->any())
->method('create')
->willReturn($abstractDbMock);

$additionalParams = ['resultFactory' => $resultFactory];
$this->action = new \Magento\Catalog\Controller\Adminhtml\Product\MassStatus(
$this->initContext($additionalParams),
$productBuilder,
$this->priceProcessor,
$filterMock,
$collectionFactoryMock
->willReturn($this->abstractDbMock);
$this->requestMock = $this->getMockBuilder(Http::class)->setMethods(
['getParam', 'getPost', 'getFullActionName', 'getPostValue']
)->disableOriginalConstructor()->getMock();

$this->action = $objectManagerHelper->getObject(
MassStatus::class,
[
'objectManager' => $objectManagerMock,
'request' => $this->requestMock,
'productBuilder' => $productBuilderMock,
'filter' => $this->filterMock,
'productPriceIndexerProcessor' => $this->priceProcessorMock,
'collectionFactory' => $collectionFactoryMock,
'resultFactory' => $resultFactory
]
);

}

public function testMassStatusAction()
{
$this->priceProcessor->expects($this->once())->method('reindexList');
$storeId = 1;
$status = Status::STATUS_DISABLED;
$filters = [
'store_id' => 2,
];

$productIds = [3];

$this->filterMock->expects($this->once())
->method('getCollection')
->willReturn($this->abstractDbMock);
$this->abstractDbMock->expects($this->once())
->method('getAllIds')
->willReturn($productIds);
$this->requestMock->expects($this->exactly(3))
->method('getParam')
->willReturnMap([
['store', 0, $storeId],
['status', null, $status],
['filters', [], $filters]
]);
$this->actionMock->expects($this->once())
->method('updateAttributes');
$this->priceProcessorMock->expects($this->once())
->method('reindexList');

$this->action->execute();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ abstract class ProductTest extends \PHPUnit_Framework_TestCase
{
/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $context;

/** @var \Magento\Catalog\Controller\Product */
protected $action;

/** @var \Magento\Framework\View\Layout */
protected $layout;

/** @var \Magento\Backend\Model\Session|\PHPUnit_Framework_MockObject_MockObject */
protected $session;

/** @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject */
protected $request;

Expand Down
Loading

0 comments on commit 21468d1

Please sign in to comment.