Skip to content

Commit

Permalink
Merge pull request #576 from magento-troll/Troll-PR
Browse files Browse the repository at this point in the history
[Troll] Bugfixes
  • Loading branch information
kandy committed May 11, 2016
2 parents 5f5bd4f + 72c66ca commit 7a247b7
Show file tree
Hide file tree
Showing 32 changed files with 520 additions and 236 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,15 @@ public function execute()
$productId = (int) $this->getRequest()->getParam('id');
$product = $this->productBuilder->build($this->getRequest());

if ($productId && !$product->getEntityId()) {
$this->messageManager->addError(__('This product no longer exists.'));
if (($productId && !$product->getEntityId())) {
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
$this->messageManager->addError(__('This product doesn\'t exist.'));
return $resultRedirect->setPath('catalog/*/');
} else if ($productId === 0) {
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
$this->messageManager->addError(__('Invalid product id. Should be numeric value greater than 0'));
return $resultRedirect->setPath('catalog/*/');
}

Expand Down
48 changes: 42 additions & 6 deletions app/code/Magento/Catalog/Cron/RefreshSpecialPrices.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
namespace Magento\Catalog\Cron;

use Magento\Framework\App\ResourceConnection;
use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Framework\App\ObjectManager;

class RefreshSpecialPrices
{
Expand Down Expand Up @@ -44,6 +47,11 @@ class RefreshSpecialPrices
*/
protected $_connection;

/**
* @var MetadataPool
*/
private $metadataPool;

/**
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param ResourceConnection $resource
Expand Down Expand Up @@ -131,22 +139,50 @@ protected function _refreshSpecialPriceByStore($storeId, $attrCode, $attrConditi
$attribute = $this->_eavConfig->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $attrCode);
$attributeId = $attribute->getAttributeId();

$linkField = $this->getMetadataPool()->getMetadata(CategoryInterface::class)->getLinkField();
$identifierField = $this->getMetadataPool()->getMetadata(CategoryInterface::class)->getIdentifierField();

$connection = $this->_getConnection();

$select = $connection->select()->from(
$this->_resource->getTableName(['catalog_product_entity', 'datetime']),
['entity_id']
['attr' => $this->_resource->getTableName(['catalog_product_entity', 'datetime'])],
[
$identifierField => 'cat.' . $identifierField,
]
)->joinLeft(
['cat' => $this->_resource->getTableName('catalog_product_entity')],
'cat.' . $linkField . '= attr.' . $linkField,
''
)->where(
'attribute_id = ?',
'attr.attribute_id = ?',
$attributeId
)->where(
'store_id = ?',
'attr.store_id = ?',
$storeId
)->where(
'value = ?',
'attr.value = ?',
$attrConditionValue
);

$this->_processor->getIndexer()->reindexList($connection->fetchCol($select, ['entity_id']));
$selectData = $connection->fetchCol($select, $identifierField);

if (!empty($selectData)) {
$this->_processor->getIndexer()->reindexList($selectData);
}

}

/**
* Get MetadataPool instance
* @return MetadataPool
*
* @deprecated
*/
private function getMetadataPool()
{
if (null === $this->metadataPool) {
$this->metadataPool = ObjectManager::getInstance()->get(MetadataPool::class);
}
return $this->metadataPool;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Magento\Catalog\Test\Unit\Cron;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\EntityManager\MetadataPool;

class RefreshSpecialPricesTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -49,6 +50,16 @@ class RefreshSpecialPricesTest extends \PHPUnit_Framework_TestCase
*/
protected $_priceProcessorMock;

/**
* @var MetadataPool|\PHPUnit_Framework_MockObject_MockObject
*/
protected $metadataPool;

/**
* @var \Magento\Framework\EntityManager\EntityMetadata|\PHPUnit_Framework_MockObject_MockObject
*/
protected $metadataMock;

protected function setUp()
{
$this->_objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
Expand All @@ -72,6 +83,8 @@ protected function setUp()
false
);

$this->metadataMock = $this->getMock(\Magento\Framework\EntityManager\EntityMetadata::class, [], [], '', false);

$this->_model = $this->_objectManager->getObject(
'Magento\Catalog\Cron\RefreshSpecialPrices',
[
Expand All @@ -83,14 +96,30 @@ protected function setUp()
'processor' => $this->_priceProcessorMock
]
);

$this->metadataPool = $this->getMock(MetadataPool::class, [], [], '', false);

$reflection = new \ReflectionClass(get_class($this->_model));
$reflectionProperty = $reflection->getProperty('metadataPool');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($this->_model, $this->metadataPool);
}

public function testRefreshSpecialPrices()
{
$idsToProcess = [1, 2, 3];

$this->metadataPool->expects($this->atLeastOnce())
->method('getMetadata')
->willReturn($this->metadataMock);

$this->metadataMock->expects($this->atLeastOnce())->method('getLinkField')->willReturn('row_id');

$this->metadataMock->expects($this->atLeastOnce())->method('getIdentifierField')->willReturn('entity_id');

$selectMock = $this->getMock('Magento\Framework\DB\Select', [], [], '', false);
$selectMock->expects($this->any())->method('from')->will($this->returnSelf());
$selectMock->expects($this->any())->method('joinLeft')->will($this->returnSelf());
$selectMock->expects($this->any())->method('where')->will($this->returnSelf());

$connectionMock = $this->getMock('Magento\Framework\DB\Adapter\AdapterInterface', [], [], '', false);
Expand All @@ -99,12 +128,10 @@ public function testRefreshSpecialPrices()
$this->any()
)->method(
'fetchCol'
)->with(
$selectMock,
['entity_id']
)->will(
$this->returnValue($idsToProcess)
);

$this->_resourceMock->expects(
$this->once()
)->method(
Expand All @@ -113,6 +140,14 @@ public function testRefreshSpecialPrices()
$this->returnValue($connectionMock)
);

$this->_resourceMock->expects(
$this->any()
)->method(
'getTableName'
)->will(
$this->returnValue('category')
);

$storeMock = $this->getMock('\Magento\Store\Model\Store', [], [], '', false);
$storeMock->expects($this->any())->method('getId')->will($this->returnValue(1));

Expand Down
40 changes: 31 additions & 9 deletions app/code/Magento/CatalogRule/Model/ResourceModel/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ class Rule extends \Magento\Rule\Model\ResourceModel\AbstractResource
* @param \Psr\Log\LoggerInterface $logger
* @param \Magento\Framework\Stdlib\DateTime $dateTime
* @param PriceCurrencyInterface $priceCurrency
* @param \Magento\Framework\EntityManager\EntityManager $entityManager
* @param array $associatedEntitiesMap
* @param null $connectionName
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
Expand All @@ -107,8 +105,6 @@ public function __construct(
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\Stdlib\DateTime $dateTime,
PriceCurrencyInterface $priceCurrency,
\Magento\Framework\EntityManager\EntityManager $entityManager,
array $associatedEntitiesMap = [],
$connectionName = null
) {
$this->_storeManager = $storeManager;
Expand All @@ -120,8 +116,7 @@ public function __construct(
$this->_logger = $logger;
$this->dateTime = $dateTime;
$this->priceCurrency = $priceCurrency;
$this->entityManager = $entityManager;
$this->_associatedEntitiesMap = $associatedEntitiesMap;
$this->_associatedEntitiesMap = $this->getAssociatedEntitiesMap();
parent::__construct($context, $connectionName);
}

Expand Down Expand Up @@ -232,7 +227,7 @@ public function getRulesFromProduct($date, $websiteId, $customerGroupId, $produc
*/
public function load(\Magento\Framework\Model\AbstractModel $object, $value, $field = null)
{
$this->entityManager->load($object, $value, \Magento\CatalogRule\Api\Data\RuleInterface::class);
$this->getEntityManager()->load($object, $value, \Magento\CatalogRule\Api\Data\RuleInterface::class);
return $this;
}

Expand All @@ -243,7 +238,7 @@ public function load(\Magento\Framework\Model\AbstractModel $object, $value, $fi
*/
public function save(\Magento\Framework\Model\AbstractModel $object)
{
$this->entityManager->save(
$this->getEntityManager()->save(
$object,
\Magento\CatalogRule\Api\Data\RuleInterface::class
);
Expand All @@ -259,7 +254,34 @@ public function save(\Magento\Framework\Model\AbstractModel $object)
*/
public function delete(AbstractModel $object)
{
$this->entityManager->delete($object, \Magento\CatalogRule\Api\Data\RuleInterface::class);
$this->getEntityManager()->delete($object, \Magento\CatalogRule\Api\Data\RuleInterface::class);
return $this;
}

/**
* @return array
* @deprecated
*/
private function getAssociatedEntitiesMap()
{
if (!$this->_associatedEntitiesMap) {
$this->_associatedEntitiesMap = \Magento\Framework\App\ObjectManager::getInstance()
->get('Magento\CatalogRule\Model\ResourceModel\Rule\AssociatedEntityMap')
->getData();
}
return $this->_associatedEntitiesMap;
}

/**
* @return \Magento\Framework\EntityManager\EntityManager
* @deprecated
*/
private function getEntityManager()
{
if (null === $this->entityManager) {
$this->entityManager = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\EntityManager\EntityManager::class);
}
return $this->entityManager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,24 @@ class Collection extends \Magento\Rule\Model\ResourceModel\Rule\Collection\Abstr
protected $_associatedEntitiesMap;

/**
* Collection constructor.
* @param \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory
* @param \Psr\Log\LoggerInterface $logger
* @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
* @param \Magento\Framework\Event\ManagerInterface $eventManager
* @param \Magento\Framework\DB\Adapter\AdapterInterface|null $connection
* @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb|null $resource
* @param array $associatedEntitiesMap
* @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
* @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
*/
public function __construct(
\Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
\Magento\Framework\Event\ManagerInterface $eventManager,
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null,
array $associatedEntitiesMap = []
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
) {
parent::__construct(
$entityFactory,
$logger,
$fetchStrategy,
$eventManager,
$connection,
$resource
);
$this->_associatedEntitiesMap = $associatedEntitiesMap;
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
$this->_associatedEntitiesMap = $this->getAssociatedEntitiesMap();
}

/**
Expand Down Expand Up @@ -137,4 +129,18 @@ public function addCustomerGroupFilter($customerGroupId)
}
return $this;
}

/**
* @return array
* @deprecated
*/
private function getAssociatedEntitiesMap()
{
if (!$this->_associatedEntitiesMap) {
$this->_associatedEntitiesMap = \Magento\Framework\App\ObjectManager::getInstance()
->get('Magento\CatalogRule\Model\ResourceModel\Rule\AssociatedEntityMap')
->getData();
}
return $this->_associatedEntitiesMap;
}
}
9 changes: 2 additions & 7 deletions app/code/Magento/CatalogRule/Model/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@ class Rule extends \Magento\Rule\Model\AbstractModel implements \Magento\Catalog
* Rule constructor.
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
* @param \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory
* @param \Magento\Framework\Data\FormFactory $formFactory
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
* @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
Expand All @@ -163,8 +161,6 @@ class Rule extends \Magento\Rule\Model\AbstractModel implements \Magento\Catalog
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
\Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
\Magento\Framework\Data\FormFactory $formFactory,
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
Expand Down Expand Up @@ -199,8 +195,6 @@ public function __construct(
parent::__construct(
$context,
$registry,
$extensionFactory,
$customAttributeFactory,
$formFactory,
$localeDate,
$resource,
Expand Down Expand Up @@ -775,12 +769,13 @@ public function setExtensionAttributes(\Magento\CatalogRule\Api\Data\RuleExtensi

/**
* @return Data\Condition\Converter
* @deprecated
*/
private function getRuleConditionConverter()
{
if (null === $this->ruleConditionConverter) {
$this->ruleConditionConverter = \Magento\Framework\App\ObjectManager::getInstance()
->get('Magento\CatalogRule\Model\Data\Condition\Converter');
->get(\Magento\CatalogRule\Model\Data\Condition\Converter::class);
}
return $this->ruleConditionConverter;
}
Expand Down
Loading

0 comments on commit 7a247b7

Please sign in to comment.