Skip to content

Commit

Permalink
Merge branch '2.1-develop' of github.com:magento/magento2ce into pr-2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Shvets committed Oct 19, 2016
2 parents 8b82897 + 05619f7 commit db02994
Show file tree
Hide file tree
Showing 24 changed files with 460 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ class MassStatus extends \Magento\Catalog\Controller\Adminhtml\Product
/**
* MassActions filter
*
* @var Filter
* @var \Magento\Ui\Component\MassAction\Filter
*/
protected $filter;

/**
* @var CollectionFactory
* @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
*/
protected $collectionFactory;

/**
* @param Action\Context $context
* @param Builder $productBuilder
* @param \Magento\Catalog\Model\Indexer\Product\Price\Processor $productPriceIndexerProcessor
* @param Filter $filter
* @param CollectionFactory $collectionFactory
* @param \Magento\Ui\Component\MassAction\Filter $filter
* @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
Expand Down Expand Up @@ -81,6 +81,11 @@ public function execute()
$productIds = $collection->getAllIds();
$storeId = (int) $this->getRequest()->getParam('store', 0);
$status = (int) $this->getRequest()->getParam('status');
$filters = (array)$this->getRequest()->getParam('filters', []);

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

try {
$this->_validateMassStatus($productIds, $status);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,12 @@ protected function _generateUniqueSku($object)
{
$attribute = $this->getAttribute();
$entity = $attribute->getEntity();
$increment = $this->_getLastSimilarAttributeValueIncrement($attribute, $object);
$attributeValue = $object->getData($attribute->getAttributeCode());
$increment = null;
while (!$entity->checkAttributeUniqueValue($attribute, $object)) {
if ($increment === null) {
$increment = $this->_getLastSimilarAttributeValueIncrement($attribute, $object);
}
$sku = trim($attributeValue);
if (strlen($sku . '-' . ++$increment) > self::SKU_MAX_LENGTH) {
$sku = substr($sku, 0, -strlen($increment) - 1);
Expand Down
24 changes: 22 additions & 2 deletions app/code/Magento/Catalog/Model/Product/Gallery/CreateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ class CreateHandler implements ExtensionInterface
*/
protected $fileStorageDb;

/**
* @var array
*/
private $mediaAttributeCodes;

/**
* @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
* @param \Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository
Expand Down Expand Up @@ -145,9 +150,11 @@ public function execute($product, $arguments = [])
}

/* @var $mediaAttribute \Magento\Catalog\Api\Data\ProductAttributeInterface */
foreach ($this->mediaConfig->getMediaAttributeCodes() as $mediaAttrCode) {
foreach ($this->getMediaAttributeCodes() as $mediaAttrCode) {
$attrData = $product->getData($mediaAttrCode);

if (empty($attrData) && empty($clearImages) && empty($newImages) && empty($existImages)) {
continue;
}
if (in_array($attrData, $clearImages)) {
$product->setData($mediaAttrCode, 'no_selection');
}
Expand Down Expand Up @@ -393,4 +400,17 @@ protected function copyImage($file)
);
}
}

/**
* Get Media Attribute Codes cached value
*
* @return array
*/
private function getMediaAttributeCodes()
{
if ($this->mediaAttributeCodes === null) {
$this->mediaAttributeCodes = $this->mediaConfig->getMediaAttributeCodes();
}
return $this->mediaAttributeCodes;
}
}
17 changes: 11 additions & 6 deletions app/code/Magento/Catalog/Model/Product/Link/SaveHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public function __construct(
Link $linkResource,
ProductLinkRepositoryInterface $productLinkRepository
) {

$this->metadataPool = $metadataPool;
$this->linkResource = $linkResource;
$this->productLinkRepository = $productLinkRepository;
Expand All @@ -54,12 +53,18 @@ public function __construct(
*/
public function execute($entityType, $entity)
{
/** @var \Magento\Catalog\Api\Data\ProductInterface $entity*/
foreach ($this->productLinkRepository->getList($entity) as $link) {
$this->productLinkRepository->delete($link);
$link = $entity->getData($this->metadataPool->getMetadata($entityType)->getLinkField());
if ($this->linkResource->hasProductLinks($link)) {
/** @var \Magento\Catalog\Api\Data\ProductInterface $entity*/
foreach ($this->productLinkRepository->getList($entity) as $link) {
$this->productLinkRepository->delete($link);
}
}
foreach ($entity->getProductLinks() as $link) {
$this->productLinkRepository->save($link);
$productLinks = $entity->getProductLinks();
if (count($productLinks) > 0) {
foreach ($entity->getProductLinks() as $link) {
$this->productLinkRepository->save($link);
}
}
return $entity;
}
Expand Down
4 changes: 3 additions & 1 deletion app/code/Magento/Catalog/Model/ResourceModel/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,9 @@ protected function _beforeSave(\Magento\Framework\DataObject $object)
if (!$object->getChildrenCount()) {
$object->setChildrenCount(0);
}

$object->setAttributeSetId(
$object->getAttributeSetId() ?: $this->getEntityType()->getDefaultAttributeSetId()
);
if ($object->isObjectNew()) {
if ($object->getPosition() === null) {
$object->setPosition($this->_getMaxPosition($object->getPath()) + 1);
Expand Down
11 changes: 4 additions & 7 deletions app/code/Magento/Catalog/Model/ResourceModel/Eav/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,14 +351,11 @@ public function getStoreId()
*/
public function getApplyTo()
{
if ($this->getData(self::APPLY_TO)) {
if (is_array($this->getData(self::APPLY_TO))) {
return $this->getData(self::APPLY_TO);
}
return explode(',', $this->getData(self::APPLY_TO));
} else {
return [];
$applyTo = $this->_getData(self::APPLY_TO) ?: [];
if (!is_array($applyTo)) {
$applyTo = explode(',', $applyTo);
}
return $applyTo;
}

/**
Expand Down
24 changes: 24 additions & 0 deletions app/code/Magento/Catalog/Model/ResourceModel/Product/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,30 @@ public function getProductLinkId($parentId, $linkedProductId, $typeId)
return $connection->fetchOne($select, $bind);
}

/**
* Check if product has links.
*
* @param int $parentId ID of product
* @return bool
*/
public function hasProductLinks($parentId)
{
$connection = $this->getConnection();
$select = $connection->select()->from(
$this->getMainTable(),
['count' => new \Zend_Db_Expr('COUNT(*)')]
)->where(
'product_id = :product_id'
) ;

return $connection->fetchOne(
$select,
[
'product_id' => $parentId
]
) > 0;
}

/**
* Save Product Links process
*
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\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Ui\Component\MassAction\Filter;
use Magento\Backend\Model\View\Result\Redirect;
use Magento\Catalog\Model\Indexer\Product\Price\Processor;
use Magento\Catalog\Controller\Adminhtml\Product\Builder;
use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Catalog\Model\Product\Action;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Catalog\Controller\Adminhtml\Product\MassStatus;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\Request\Http;
use Magento\Catalog\Model\Product;
use Magento\Framework\ObjectManager\ObjectManager as Manager;

/**
* @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([
$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(CollectionFactory::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();
}
}
Loading

0 comments on commit db02994

Please sign in to comment.