diff --git a/app/code/Magento/Catalog/Controller/Adminhtml/Product/Edit.php b/app/code/Magento/Catalog/Controller/Adminhtml/Product/Edit.php index 9970a42a3a484..1c75cdd4cfddc 100644 --- a/app/code/Magento/Catalog/Controller/Adminhtml/Product/Edit.php +++ b/app/code/Magento/Catalog/Controller/Adminhtml/Product/Edit.php @@ -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/*/'); } diff --git a/app/code/Magento/Catalog/Cron/RefreshSpecialPrices.php b/app/code/Magento/Catalog/Cron/RefreshSpecialPrices.php index b17c4ba3b24d5..e3a63e0367328 100644 --- a/app/code/Magento/Catalog/Cron/RefreshSpecialPrices.php +++ b/app/code/Magento/Catalog/Cron/RefreshSpecialPrices.php @@ -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 { @@ -44,6 +47,11 @@ class RefreshSpecialPrices */ protected $_connection; + /** + * @var MetadataPool + */ + private $metadataPool; + /** * @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param ResourceConnection $resource @@ -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; } } diff --git a/app/code/Magento/Catalog/Test/Unit/Cron/RefreshSpecialPricesTest.php b/app/code/Magento/Catalog/Test/Unit/Cron/RefreshSpecialPricesTest.php index 672f8f2b39e9d..8cd188541a92f 100644 --- a/app/code/Magento/Catalog/Test/Unit/Cron/RefreshSpecialPricesTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Cron/RefreshSpecialPricesTest.php @@ -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 { @@ -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); @@ -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', [ @@ -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); @@ -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( @@ -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)); diff --git a/app/code/Magento/CatalogRule/Model/ResourceModel/Rule.php b/app/code/Magento/CatalogRule/Model/ResourceModel/Rule.php index cb64f36dd8b5c..5008aba260111 100644 --- a/app/code/Magento/CatalogRule/Model/ResourceModel/Rule.php +++ b/app/code/Magento/CatalogRule/Model/ResourceModel/Rule.php @@ -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) */ @@ -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; @@ -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); } @@ -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; } @@ -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 ); @@ -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; + } } diff --git a/app/code/Magento/CatalogRule/Model/ResourceModel/Rule/Collection.php b/app/code/Magento/CatalogRule/Model/ResourceModel/Rule/Collection.php index 34b9e7b92bd21..9275800024139 100644 --- a/app/code/Magento/CatalogRule/Model/ResourceModel/Rule/Collection.php +++ b/app/code/Magento/CatalogRule/Model/ResourceModel/Rule/Collection.php @@ -15,13 +15,13 @@ 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, @@ -29,18 +29,10 @@ public function __construct( \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(); } /** @@ -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; + } } diff --git a/app/code/Magento/CatalogRule/Model/Rule.php b/app/code/Magento/CatalogRule/Model/Rule.php index 37c190370ca64..4260bd8731f12 100644 --- a/app/code/Magento/CatalogRule/Model/Rule.php +++ b/app/code/Magento/CatalogRule/Model/Rule.php @@ -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 @@ -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, @@ -199,8 +195,6 @@ public function __construct( parent::__construct( $context, $registry, - $extensionFactory, - $customAttributeFactory, $formFactory, $localeDate, $resource, @@ -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; } diff --git a/app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php b/app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php index fc35bd09f7372..b6f895268636c 100644 --- a/app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php +++ b/app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php @@ -129,6 +129,18 @@ protected function setUp() ); $this->objectManagerHelper = new ObjectManagerHelper($this); + + $this->prepareObjectManager([ + [ + 'Magento\Framework\Api\ExtensionAttributesFactory', + $this->getMock('Magento\Framework\Api\ExtensionAttributesFactory', [], [], '', false) + ], + [ + 'Magento\Framework\Api\AttributeValueFactory', + $this->getMock('Magento\Framework\Api\AttributeValueFactory', [], [], '', false) + ], + ]); + $this->rule = $this->objectManagerHelper->getObject( 'Magento\CatalogRule\Model\Rule', [ @@ -363,4 +375,20 @@ public function testGetConditionsFieldSetId() $expectedResult = 'form_namerule_conditions_fieldset_100'; $this->assertEquals($expectedResult, $this->rule->getConditionsFieldSetId($formName)); } + + /** + * @param $map + */ + private function prepareObjectManager($map) + { + $objectManagerMock = $this->getMock('Magento\Framework\ObjectManagerInterface'); + $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($map)); + $reflectionClass = new \ReflectionClass('Magento\Framework\App\ObjectManager'); + $reflectionProperty = $reflectionClass->getProperty('_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue($objectManagerMock); + } } diff --git a/app/code/Magento/CatalogRule/etc/di.xml b/app/code/Magento/CatalogRule/etc/di.xml index b743e38a53533..dfe8bdbe43ddc 100644 --- a/app/code/Magento/CatalogRule/etc/di.xml +++ b/app/code/Magento/CatalogRule/etc/di.xml @@ -9,36 +9,9 @@ Magento\Framework\Event\Manager\Proxy - - - catalogrule_website - rule_id - website_id - - - catalogrule_customer_group - rule_id - customer_group_id - - - - - - - - - catalogrule_website - rule_id - website_id - - - catalogrule_customer_group - rule_id - customer_group_id - - + @@ -123,4 +96,20 @@ + + + + + catalogrule_website + rule_id + website_id + + + catalogrule_customer_group + rule_id + customer_group_id + + + + diff --git a/app/code/Magento/CatalogWidget/Model/Rule.php b/app/code/Magento/CatalogWidget/Model/Rule.php index 94732ec29498a..8258d3f1a8b4f 100644 --- a/app/code/Magento/CatalogWidget/Model/Rule.php +++ b/app/code/Magento/CatalogWidget/Model/Rule.php @@ -19,8 +19,6 @@ class Rule extends \Magento\Rule\Model\AbstractModel /** * @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 Rule\Condition\CombineFactory $conditionsFactory @@ -32,8 +30,6 @@ class Rule extends \Magento\Rule\Model\AbstractModel 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\CatalogWidget\Model\Rule\Condition\CombineFactory $conditionsFactory, @@ -45,8 +41,6 @@ public function __construct( parent::__construct( $context, $registry, - $extensionFactory, - $customAttributeFactory, $formFactory, $localeDate, $resource, diff --git a/app/code/Magento/CatalogWidget/Test/Unit/Model/RuleTest.php b/app/code/Magento/CatalogWidget/Test/Unit/Model/RuleTest.php index dd58200c13a2a..99b9f46fd887d 100644 --- a/app/code/Magento/CatalogWidget/Test/Unit/Model/RuleTest.php +++ b/app/code/Magento/CatalogWidget/Test/Unit/Model/RuleTest.php @@ -28,6 +28,18 @@ protected function setUp() ->getMock(); $objectManagerHelper = new ObjectManagerHelper($this); + + $this->prepareObjectManager([ + [ + 'Magento\Framework\Api\ExtensionAttributesFactory', + $this->getMock('Magento\Framework\Api\ExtensionAttributesFactory', [], [], '', false) + ], + [ + 'Magento\Framework\Api\AttributeValueFactory', + $this->getMock('Magento\Framework\Api\AttributeValueFactory', [], [], '', false) + ], + ]); + $this->rule = $objectManagerHelper->getObject( 'Magento\CatalogWidget\Model\Rule', [ @@ -50,4 +62,20 @@ public function testGetActionsInstance() { $this->assertNull($this->rule->getActionsInstance()); } + + /** + * @param $map + */ + private function prepareObjectManager($map) + { + $objectManagerMock = $this->getMock('Magento\Framework\ObjectManagerInterface'); + $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($map)); + $reflectionClass = new \ReflectionClass('Magento\Framework\App\ObjectManager'); + $reflectionProperty = $reflectionClass->getProperty('_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue($objectManagerMock); + } } diff --git a/app/code/Magento/Reports/view/adminhtml/layout/reports_report_product_viewed.xml b/app/code/Magento/Reports/view/adminhtml/layout/reports_report_product_viewed.xml index 2dc78b068b711..1da38794b3ffa 100644 --- a/app/code/Magento/Reports/view/adminhtml/layout/reports_report_product_viewed.xml +++ b/app/code/Magento/Reports/view/adminhtml/layout/reports_report_product_viewed.xml @@ -8,6 +8,9 @@ + + The report displays the top 5 most viewed products for each interval. + For accurate reporting, be sure to refresh lifetime statistics whenever you change the time zone. diff --git a/app/code/Magento/Rule/Model/AbstractModel.php b/app/code/Magento/Rule/Model/AbstractModel.php index 97f2f085712ec..45f0c092c4aec 100644 --- a/app/code/Magento/Rule/Model/AbstractModel.php +++ b/app/code/Magento/Rule/Model/AbstractModel.php @@ -80,8 +80,6 @@ abstract public function getActionsInstance(); * * @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\Framework\Model\ResourceModel\AbstractResource|null $resource @@ -91,8 +89,6 @@ abstract public function getActionsInstance(); 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\Framework\Model\ResourceModel\AbstractResource $resource = null, @@ -104,8 +100,8 @@ public function __construct( parent::__construct( $context, $registry, - $extensionFactory, - $customAttributeFactory, + $this->getExtensionFactory(), + $this->getCustomAttributeFactory(), $resource, $resourceCollection, $data @@ -464,4 +460,24 @@ public function getWebsiteIds() } return $this->_getData('website_ids'); } + + /** + * @return \Magento\Framework\Api\ExtensionAttributesFactory + * @deprecated + */ + private function getExtensionFactory() + { + return \Magento\Framework\App\ObjectManager::getInstance() + ->get(\Magento\Framework\Api\ExtensionAttributesFactory::class); + } + + /** + * @return \Magento\Framework\Api\AttributeValueFactory + * @deprecated + */ + private function getCustomAttributeFactory() + { + return \Magento\Framework\App\ObjectManager::getInstance() + ->get(\Magento\Framework\Api\AttributeValueFactory::class); + } } diff --git a/app/code/Magento/SalesRule/Model/ResourceModel/Rule.php b/app/code/Magento/SalesRule/Model/ResourceModel/Rule.php index 26a51281c6a3a..92a7487f86145 100644 --- a/app/code/Magento/SalesRule/Model/ResourceModel/Rule.php +++ b/app/code/Magento/SalesRule/Model/ResourceModel/Rule.php @@ -55,22 +55,17 @@ class Rule extends AbstractResource * @param \Magento\Framework\Model\ResourceModel\Db\Context $context * @param \Magento\Framework\Stdlib\StringUtils $string * @param \Magento\SalesRule\Model\ResourceModel\Coupon $resourceCoupon - * @param EntityManager $entityManager - * @param array $associatedEntitiesMap * @param string $connectionName */ public function __construct( \Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Framework\Stdlib\StringUtils $string, \Magento\SalesRule\Model\ResourceModel\Coupon $resourceCoupon, - EntityManager $entityManager, - array $associatedEntitiesMap = [], $connectionName = null ) { $this->string = $string; $this->_resourceCoupon = $resourceCoupon; - $this->entityManager = $entityManager; - $this->_associatedEntitiesMap = $associatedEntitiesMap; + $this->_associatedEntitiesMap = $this->getAssociatedEntitiesMap(); parent::__construct($context, $connectionName); } @@ -138,7 +133,7 @@ public function _beforeSave(AbstractModel $object) */ public function load(AbstractModel $object, $value, $field = null) { - $this->entityManager->load($object, $value, RuleInterface::class); + $this->getEntityManager()->load($object, $value, RuleInterface::class); return $this; } @@ -368,7 +363,7 @@ public function getProductAttributes($serializedString) */ public function save(\Magento\Framework\Model\AbstractModel $object) { - $this->entityManager->save($object, RuleInterface::class); + $this->getEntityManager()->save($object, RuleInterface::class); return $this; } @@ -381,7 +376,34 @@ public function save(\Magento\Framework\Model\AbstractModel $object) */ public function delete(AbstractModel $object) { - $this->entityManager->delete($object, RuleInterface::class); + $this->getEntityManager()->delete($object, RuleInterface::class); return $this; } + + /** + * @return array + * @deprecated + */ + private function getAssociatedEntitiesMap() + { + if (!$this->_associatedEntitiesMap) { + $this->_associatedEntitiesMap = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\SalesRule\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; + } } diff --git a/app/code/Magento/SalesRule/Model/ResourceModel/Rule/Collection.php b/app/code/Magento/SalesRule/Model/ResourceModel/Rule/Collection.php index f74753a142b48..3d814f635f15a 100644 --- a/app/code/Magento/SalesRule/Model/ResourceModel/Rule/Collection.php +++ b/app/code/Magento/SalesRule/Model/ResourceModel/Rule/Collection.php @@ -39,8 +39,6 @@ class Collection extends \Magento\Rule\Model\ResourceModel\Rule\Collection\Abstr * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy * @param \Magento\Framework\Event\ManagerInterface $eventManager * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $date - * @param \Magento\SalesRule\Model\ResourceModel\Rule\DateApplier $dateApplier - * @param array $associatedEntitiesMap * @param mixed $connection * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource */ @@ -50,15 +48,12 @@ public function __construct( \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Framework\Stdlib\DateTime\TimezoneInterface $date, - \Magento\SalesRule\Model\ResourceModel\Rule\DateApplier $dateApplier, - array $associatedEntitiesMap = [], \Magento\Framework\DB\Adapter\AdapterInterface $connection = null, \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null ) { parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource); $this->_date = $date; - $this->_associatedEntitiesMap = $associatedEntitiesMap; - $this->dateApplier = $dateApplier; + $this->_associatedEntitiesMap = $this->getAssociatedEntitiesMap(); } /** @@ -242,7 +237,7 @@ public function addWebsiteGroupDateFilter($websiteId, $customerGroupId, $now = n [] ); - $this->dateApplier->applyDate($this->getSelect(), $now); + $this->getDateApplier()->applyDate($this->getSelect(), $now); $this->addIsActiveFilter(); @@ -321,4 +316,32 @@ public function addCustomerGroupFilter($customerGroupId) } return $this; } + + /** + * @return array + * @deprecated + */ + private function getAssociatedEntitiesMap() + { + if (!$this->_associatedEntitiesMap) { + $this->_associatedEntitiesMap = \Magento\Framework\App\ObjectManager::getInstance() + ->get('Magento\SalesRule\Model\ResourceModel\Rule\AssociatedEntityMap') + ->getData(); + } + return $this->_associatedEntitiesMap; + } + + /** + * @return DateApplier + * @deprecated + */ + private function getDateApplier() + { + if (null === $this->dateApplier) { + $this->dateApplier = \Magento\Framework\App\ObjectManager::getInstance() + ->get(\Magento\SalesRule\Model\ResourceModel\Rule\DateApplier::class); + } + + return $this->dateApplier; + } } diff --git a/app/code/Magento/SalesRule/Model/Rule.php b/app/code/Magento/SalesRule/Model/Rule.php index 9f182fcd377fc..3c94b7059f5d9 100644 --- a/app/code/Magento/SalesRule/Model/Rule.php +++ b/app/code/Magento/SalesRule/Model/Rule.php @@ -173,8 +173,6 @@ class Rule extends \Magento\Rule\Model\AbstractModel /** * @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 CouponFactory $couponFactory @@ -191,8 +189,6 @@ class Rule extends \Magento\Rule\Model\AbstractModel 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\SalesRule\Model\CouponFactory $couponFactory, @@ -214,8 +210,6 @@ public function __construct( parent::__construct( $context, $registry, - $extensionFactory, - $customAttributeFactory, $formFactory, $localeDate, $resource, diff --git a/app/code/Magento/SalesRule/Test/Unit/Model/ResourceModel/RuleTest.php b/app/code/Magento/SalesRule/Test/Unit/Model/ResourceModel/RuleTest.php index 9056a1bf0bec8..c5e623ddd2883 100644 --- a/app/code/Magento/SalesRule/Test/Unit/Model/ResourceModel/RuleTest.php +++ b/app/code/Magento/SalesRule/Test/Unit/Model/ResourceModel/RuleTest.php @@ -114,25 +114,36 @@ protected function setUp() ->disableOriginalConstructor() ->getMock(); - $associatedEntitiesMap = [ - 'customer_group' => [ - 'associations_table' => 'salesrule_customer_group', - 'rule_id_field' => 'rule_id', - 'entity_id_field' => 'customer_group_id' - ], - 'website' => [ - 'associations_table' => 'salesrule_website', - 'rule_id_field' => 'rule_id', - 'entity_id_field' => 'website_id' + $associatedEntitiesMap = $this->getMock('Magento\Framework\DataObject', [], [], '', false); + $associatedEntitiesMap->expects($this->once()) + ->method('getData') + ->willReturn( + [ + 'customer_group' => [ + 'associations_table' => 'salesrule_customer_group', + 'rule_id_field' => 'rule_id', + 'entity_id_field' => 'customer_group_id' + ], + 'website' => [ + 'associations_table' => 'salesrule_website', + 'rule_id_field' => 'rule_id', + 'entity_id_field' => 'website_id' + ], + ] + ); + + $this->prepareObjectManager([ + [ + 'Magento\SalesRule\Model\ResourceModel\Rule\AssociatedEntityMap', + $associatedEntitiesMap ], - ]; + ]); $this->model = $objectManager->getObject( 'Magento\SalesRule\Model\ResourceModel\Rule', [ 'context' => $context, 'connectionName' => $connectionName, - 'associatedEntitiesMap' => $associatedEntitiesMap, 'entityManager' => $this->entityManager, ] ); @@ -170,4 +181,20 @@ public function testDelete() ->with($this->rule, RuleInterface::class); $this->assertEquals($this->model->delete($this->rule), $this->model); } + + /** + * @param $map + */ + private function prepareObjectManager($map) + { + $objectManagerMock = $this->getMock('Magento\Framework\ObjectManagerInterface'); + $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($map)); + $reflectionClass = new \ReflectionClass('Magento\Framework\App\ObjectManager'); + $reflectionProperty = $reflectionClass->getProperty('_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue($objectManagerMock); + } } diff --git a/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php b/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php index d63c1ab6e7c03..a32989bb7af1b 100644 --- a/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php +++ b/app/code/Magento/SalesRule/Test/Unit/Model/RuleTest.php @@ -57,6 +57,17 @@ protected function setUp() ->setMethods(['create']) ->getMock(); + $this->prepareObjectManager([ + [ + 'Magento\Framework\Api\ExtensionAttributesFactory', + $this->getMock('Magento\Framework\Api\ExtensionAttributesFactory', [], [], '', false) + ], + [ + 'Magento\Framework\Api\AttributeValueFactory', + $this->getMock('Magento\Framework\Api\AttributeValueFactory', [], [], '', false) + ], + ]); + $this->model = $objectManager->getObject( 'Magento\SalesRule\Model\Rule', [ @@ -168,4 +179,20 @@ public function testGetActionsFieldSetId() $expectedResult = 'form_namerule_actions_fieldset_100'; $this->assertEquals($expectedResult, $this->model->getActionsFieldSetId($formName)); } + + /** + * @param $map + */ + private function prepareObjectManager($map) + { + $objectManagerMock = $this->getMock('Magento\Framework\ObjectManagerInterface'); + $objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf(); + $objectManagerMock->expects($this->any()) + ->method('get') + ->will($this->returnValueMap($map)); + $reflectionClass = new \ReflectionClass('Magento\Framework\App\ObjectManager'); + $reflectionProperty = $reflectionClass->getProperty('_instance'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue($objectManagerMock); + } } diff --git a/app/code/Magento/SalesRule/etc/di.xml b/app/code/Magento/SalesRule/etc/di.xml index 4498f0b06e038..5698c0bd6d8e6 100644 --- a/app/code/Magento/SalesRule/etc/di.xml +++ b/app/code/Magento/SalesRule/etc/di.xml @@ -42,22 +42,7 @@ - - - - - salesrule_website - rule_id - website_id - - - salesrule_customer_group - rule_id - customer_group_id - - - - + @@ -90,22 +75,6 @@ sales - - - - - salesrule_website - rule_id - website_id - - - salesrule_customer_group - rule_id - customer_group_id - - - - @@ -185,4 +154,20 @@ + + + + + salesrule_website + rule_id + website_id + + + salesrule_customer_group + rule_id + customer_group_id + + + + diff --git a/dev/tests/functional/tests/app/Magento/Backend/Test/Block/FormPageActions.php b/dev/tests/functional/tests/app/Magento/Backend/Test/Block/FormPageActions.php index 19b085b080fd1..1089f6e63cc6b 100644 --- a/dev/tests/functional/tests/app/Magento/Backend/Test/Block/FormPageActions.php +++ b/dev/tests/functional/tests/app/Magento/Backend/Test/Block/FormPageActions.php @@ -17,56 +17,63 @@ class FormPageActions extends PageActions { /** - * "Back" button + * "Back" button. * * @var string */ protected $backButton = '#back'; /** - * "Reset" button + * "Reset" button. * * @var string */ protected $resetButton = '#reset'; /** - * "Save and Continue Edit" button + * "Save and Continue Edit" button. * * @var string */ protected $saveAndContinueButton = '#save_and_continue'; /** - * "Save" button + * "Save" button. * * @var string */ protected $saveButton = '#save'; /** - * "Delete" button + * "Delete" button. * * @var string */ protected $deleteButton = '.delete'; /** - * Magento loader + * Magento new loader. + * + * @var string + */ + protected $spinner = '[data-role="spinner"]'; + + /** + * Magento loader. * * @var string */ protected $loader = '//ancestor::body/div[@data-role="loader"]'; /** - * Magento varienLoader.js loader + * Magento varienLoader.js loader. * * @var string */ protected $loaderOld = '//ancestor::body/div[@id="loading-mask"]'; /** - * Click on "Back" button + * Click "Back" button. */ public function back() { @@ -74,7 +81,7 @@ public function back() } /** - * Click on "Reset" button + * Click "Reset" button. */ public function reset() { @@ -83,7 +90,7 @@ public function reset() } /** - * Click on "Save and Continue Edit" button + * Click "Save and Continue Edit" button. */ public function saveAndContinue() { @@ -94,18 +101,19 @@ public function saveAndContinue() } /** - * Click on "Save" button + * Click "Save" button. */ public function save() { $this->waitBeforeClick(); $this->_rootElement->find($this->saveButton)->click(); + $this->waitForElementNotVisible($this->spinner); $this->waitForElementNotVisible($this->loader, Locator::SELECTOR_XPATH); $this->waitForElementNotVisible($this->loaderOld, Locator::SELECTOR_XPATH); } /** - * Click on "Delete" button + * Click "Delete" button. */ public function delete() { @@ -113,7 +121,7 @@ public function delete() } /** - * Check 'Delete' button availability + * Check 'Delete' button availability. * * @return bool */ @@ -123,7 +131,7 @@ public function checkDeleteButton() } /** - * Wait for User before click on any Button which calls JS validation on correspondent form. + * Wait for User before clicking any Button which calls JS validation on correspondent form. * See details in MAGETWO-31121. * * @return void diff --git a/dev/tests/functional/tests/app/Magento/Backend/Test/Block/Template.php b/dev/tests/functional/tests/app/Magento/Backend/Test/Block/Template.php index f6ced46ad99a2..29361ced38bcc 100644 --- a/dev/tests/functional/tests/app/Magento/Backend/Test/Block/Template.php +++ b/dev/tests/functional/tests/app/Magento/Backend/Test/Block/Template.php @@ -36,7 +36,7 @@ class Template extends Block protected $loaderOld = '#loading-mask #loading_mask_loader'; /** - * Wait until loader will be disappeared. + * Wait until loader disappears. * * @return void */ diff --git a/dev/tests/functional/tests/app/Magento/Backend/Test/Block/Widget/Grid.php b/dev/tests/functional/tests/app/Magento/Backend/Test/Block/Widget/Grid.php index 9007ae355984c..45f4e3427554b 100644 --- a/dev/tests/functional/tests/app/Magento/Backend/Test/Block/Widget/Grid.php +++ b/dev/tests/functional/tests/app/Magento/Backend/Test/Block/Widget/Grid.php @@ -276,13 +276,7 @@ public function searchAndOpen(array $filter) */ protected function waitLoader() { - $this->browser->waitUntil( - function () { - $element = $this->browser->find($this->loader); - return $element->isVisible() == false ? true : null; - } - ); - + $this->waitForElementNotVisible($this->loader); $this->getTemplateBlock()->waitLoader(); } diff --git a/dev/tests/functional/tests/app/Magento/Catalog/Test/Block/Adminhtml/Product/Modal/AddAttribute.php b/dev/tests/functional/tests/app/Magento/Catalog/Test/Block/Adminhtml/Product/Modal/AddAttribute.php index c30ee5ee94428..e1c44942c1b04 100644 --- a/dev/tests/functional/tests/app/Magento/Catalog/Test/Block/Adminhtml/Product/Modal/AddAttribute.php +++ b/dev/tests/functional/tests/app/Magento/Catalog/Test/Block/Adminhtml/Product/Modal/AddAttribute.php @@ -7,6 +7,8 @@ namespace Magento\Catalog\Test\Block\Adminhtml\Product\Modal; use Magento\Ui\Test\Block\Adminhtml\FormSections; +use Magento\Backend\Test\Block\Template; +use Magento\Mtf\Client\Locator; /** * Add new attribute modal. @@ -20,6 +22,26 @@ class AddAttribute extends FormSections */ private $createNewAttribute = 'button[data-index="add_new_attribute_button"]'; + /** + * Xpath selector for "Add Attribute" form. + * + * @var string + */ + private $addAttributeBlock = '//*[@data-role="modal"][.//button[@data-index="add_new_attribute_button"]]'; + + /** + * Get backend abstract block. + * + * @return Template + */ + protected function getTemplateBlock() + { + return $this->blockFactory->create( + 'Magento\Backend\Test\Block\Template', + ['element' => $this->_rootElement->find($this->addAttributeBlock, Locator::SELECTOR_XPATH)] + ); + } + /** * Click on "Create new attribute" button. * @@ -27,6 +49,8 @@ class AddAttribute extends FormSections */ public function createNewAttribute() { + $this->getTemplateBlock()->waitLoader(); $this->_rootElement->find($this->createNewAttribute)->click(); + $this->getTemplateBlock()->waitLoader(); } } diff --git a/dev/tests/functional/tests/app/Magento/Catalog/Test/Block/Adminhtml/Product/Modal/NewAttribute.php b/dev/tests/functional/tests/app/Magento/Catalog/Test/Block/Adminhtml/Product/Modal/NewAttribute.php index 59e999bf791ad..4bedbb1e3152f 100644 --- a/dev/tests/functional/tests/app/Magento/Catalog/Test/Block/Adminhtml/Product/Modal/NewAttribute.php +++ b/dev/tests/functional/tests/app/Magento/Catalog/Test/Block/Adminhtml/Product/Modal/NewAttribute.php @@ -7,6 +7,8 @@ namespace Magento\Catalog\Test\Block\Adminhtml\Product\Modal; use Magento\Ui\Test\Block\Adminhtml\FormSections; +use Magento\Backend\Test\Block\FormPageActions; +use Magento\Mtf\Client\Locator; /** * Product new attribute modal. @@ -14,11 +16,24 @@ class NewAttribute extends FormSections { /** - * Selector for "Save" button. + * Xpath selector for "New Attribute" form. * * @var string */ - private $save = 'button#save'; + private $newAttributeBlock = '//*[@data-role="modal"][.//input[@name="frontend_label[0]"]]'; + + /** + * Get form page actions block. + * + * @return FormPageActions + */ + protected function getFormPageActionsBlock() + { + return $this->blockFactory->create( + 'Magento\Backend\Test\Block\FormPageActions', + ['element' => $this->_rootElement->find($this->newAttributeBlock, Locator::SELECTOR_XPATH)] + ); + } /** * Click "Save Attribute" button on attribute form. @@ -27,6 +42,6 @@ class NewAttribute extends FormSections */ public function saveAttribute() { - $this->_rootElement->find($this->save)->click(); + $this->getFormPageActionsBlock()->save(); } } diff --git a/dev/tests/functional/tests/app/Magento/Catalog/Test/TestCase/ProductAttribute/CreateProductAttributeEntityFromProductPageTest.xml b/dev/tests/functional/tests/app/Magento/Catalog/Test/TestCase/ProductAttribute/CreateProductAttributeEntityFromProductPageTest.xml index 8ac8fc29a2493..fbac539ea7592 100644 --- a/dev/tests/functional/tests/app/Magento/Catalog/Test/TestCase/ProductAttribute/CreateProductAttributeEntityFromProductPageTest.xml +++ b/dev/tests/functional/tests/app/Magento/Catalog/Test/TestCase/ProductAttribute/CreateProductAttributeEntityFromProductPageTest.xml @@ -7,7 +7,7 @@ --> - + Text_Field_Admin_%isolation% Text Field No @@ -32,7 +32,7 @@ - + Dropdown_Admin_%isolation% Dropdown two_options @@ -49,7 +49,7 @@ - + Text_Field_Admin_%isolation% Text Field Yes @@ -58,7 +58,7 @@ attributes - + Text_Field_Admin_%isolation% Text Field No diff --git a/dev/tests/functional/tests/app/Magento/Ui/Test/Block/Adminhtml/DataGrid.php b/dev/tests/functional/tests/app/Magento/Ui/Test/Block/Adminhtml/DataGrid.php index 50c79987b8c09..8fce1bb52b967 100644 --- a/dev/tests/functional/tests/app/Magento/Ui/Test/Block/Adminhtml/DataGrid.php +++ b/dev/tests/functional/tests/app/Magento/Ui/Test/Block/Adminhtml/DataGrid.php @@ -236,6 +236,7 @@ public function searchAndOpen(array $filter) } else { throw new \Exception('Searched item was not found.'); } + $this->waitLoader(); } /** @@ -253,6 +254,7 @@ public function searchAndSelect(array $filter) } else { throw new \Exception('Searched item was not found.'); } + $this->waitLoader(); } /** diff --git a/lib/internal/Magento/Framework/Mview/Test/Unit/View/ChangelogTest.php b/lib/internal/Magento/Framework/Mview/Test/Unit/View/ChangelogTest.php index 44c4e47c6cca7..9134e93fc9060 100644 --- a/lib/internal/Magento/Framework/Mview/Test/Unit/View/ChangelogTest.php +++ b/lib/internal/Magento/Framework/Mview/Test/Unit/View/ChangelogTest.php @@ -185,13 +185,13 @@ public function testCreate() $this->model->create(); } - public function testCreateWithException() + public function testCreateWithExistingTable() { $changelogTableName = 'viewIdtest_cl'; $this->mockIsTableExists($changelogTableName, true); $this->mockGetTableName(); - $this->setExpectedException('Exception', "Table {$changelogTableName} already exist"); + $this->connectionMock->expects($this->never())->method('createTable'); $this->model->setViewId('viewIdtest'); $this->model->create(); } diff --git a/lib/internal/Magento/Framework/Mview/Test/Unit/ViewTest.php b/lib/internal/Magento/Framework/Mview/Test/Unit/ViewTest.php index fdc3e5e34c264..253b6ab6469ce 100644 --- a/lib/internal/Magento/Framework/Mview/Test/Unit/ViewTest.php +++ b/lib/internal/Magento/Framework/Mview/Test/Unit/ViewTest.php @@ -221,15 +221,11 @@ public function testUnsubscribe() $this->stateMock->expects($this->once()) ->method('getMode') ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED)); - $this->stateMock->expects($this->once()) - ->method('setVersionId') - ->with(null) - ->will($this->returnSelf()); $this->stateMock->expects($this->once()) ->method('setMode') ->with(\Magento\Framework\Mview\View\StateInterface::MODE_DISABLED) ->will($this->returnSelf()); - $this->changelogMock->expects($this->once()) + $this->changelogMock->expects($this->never()) ->method('drop'); $subscriptionMock = $this->getMock( \Magento\Framework\Mview\View\Subscription::class, @@ -312,9 +308,6 @@ public function testUpdate() $this->stateMock->expects($this->once()) ->method('setVersionId') ->will($this->returnSelf()); - $this->stateMock->expects($this->once()) - ->method('getMode') - ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED)); $this->stateMock->expects($this->exactly(2)) ->method('getStatus') ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::STATUS_IDLE)); @@ -376,9 +369,6 @@ public function testUpdateWithException() ->will($this->returnValue($lastVersionId)); $this->stateMock->expects($this->never()) ->method('setVersionId'); - $this->stateMock->expects($this->once()) - ->method('getMode') - ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED)); $this->stateMock->expects($this->exactly(2)) ->method('getStatus') ->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::STATUS_IDLE)); diff --git a/lib/internal/Magento/Framework/Mview/View.php b/lib/internal/Magento/Framework/Mview/View.php index 606e6a17655ca..16d88767e508c 100644 --- a/lib/internal/Magento/Framework/Mview/View.php +++ b/lib/internal/Magento/Framework/Mview/View.php @@ -8,6 +8,7 @@ namespace Magento\Framework\Mview; +use Magento\Framework\Mview\View\ChangelogTableNotExistsException; use Magento\Framework\Mview\View\SubscriptionFactory; /** @@ -228,11 +229,8 @@ public function unsubscribe() $subscriptionInstance->remove(); } - // Drop changelog table - $this->getChangelog()->drop(); - // Update view state - $this->getState()->setVersionId(null)->setMode(View\StateInterface::MODE_DISABLED)->save(); + $this->getState()->setMode(View\StateInterface::MODE_DISABLED)->save(); } catch (\Exception $e) { throw $e; } @@ -249,10 +247,12 @@ public function unsubscribe() */ public function update() { - if ($this->getState()->getMode() == View\StateInterface::MODE_ENABLED && - $this->getState()->getStatus() == View\StateInterface::STATUS_IDLE - ) { - $currentVersionId = $this->getChangelog()->getVersion(); + if ($this->getState()->getStatus() == View\StateInterface::STATUS_IDLE) { + try { + $currentVersionId = $this->getChangelog()->getVersion(); + } catch (ChangelogTableNotExistsException $e) { + return; + } $lastVersionId = $this->getState()->getVersionId(); $ids = $this->getChangelog()->getList($lastVersionId, $currentVersionId); if ($ids) { @@ -261,13 +261,15 @@ public function update() try { $action->execute($ids); $this->getState()->loadByView($this->getId()); - $statusToRestore = $this->getState()->getStatus() == - View\StateInterface::STATUS_SUSPENDED ? View\StateInterface::STATUS_SUSPENDED : View\StateInterface::STATUS_IDLE; + $statusToRestore = $this->getState()->getStatus() == View\StateInterface::STATUS_SUSPENDED + ? View\StateInterface::STATUS_SUSPENDED + : View\StateInterface::STATUS_IDLE; $this->getState()->setVersionId($currentVersionId)->setStatus($statusToRestore)->save(); } catch (\Exception $exception) { $this->getState()->loadByView($this->getId()); - $statusToRestore = $this->getState()->getStatus() == - View\StateInterface::STATUS_SUSPENDED ? View\StateInterface::STATUS_SUSPENDED : View\StateInterface::STATUS_IDLE; + $statusToRestore = $this->getState()->getStatus() == View\StateInterface::STATUS_SUSPENDED + ? View\StateInterface::STATUS_SUSPENDED + : View\StateInterface::STATUS_IDLE; $this->getState()->setStatus($statusToRestore)->save(); throw $exception; } diff --git a/lib/internal/Magento/Framework/Mview/View/Changelog.php b/lib/internal/Magento/Framework/Mview/View/Changelog.php index 85d720d67f1e3..a5bf03ea7e7e9 100644 --- a/lib/internal/Magento/Framework/Mview/View/Changelog.php +++ b/lib/internal/Magento/Framework/Mview/View/Changelog.php @@ -6,6 +6,7 @@ namespace Magento\Framework\Mview\View; use Magento\Framework\App\ResourceConnection; +use Magento\Framework\Phrase; class Changelog implements ChangelogInterface { @@ -70,40 +71,37 @@ protected function checkConnection() public function create() { $changelogTableName = $this->resource->getTableName($this->getName()); - if ($this->connection->isTableExists($changelogTableName)) { - throw new \Exception("Table {$changelogTableName} already exist"); + if (!$this->connection->isTableExists($changelogTableName)) { + $table = $this->connection->newTable( + $changelogTableName + )->addColumn( + 'version_id', + \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, + null, + ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true], + 'Version ID' + )->addColumn( + $this->getColumnName(), + \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, + null, + ['unsigned' => true, 'nullable' => false, 'default' => '0'], + 'Entity ID' + ); + $this->connection->createTable($table); } - - $table = $this->connection->newTable( - $changelogTableName - )->addColumn( - 'version_id', - \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, - null, - ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true], - 'Version ID' - )->addColumn( - $this->getColumnName(), - \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, - null, - ['unsigned' => true, 'nullable' => false, 'default' => '0'], - 'Entity ID' - ); - - $this->connection->createTable($table); } /** * Drop changelog table * * @return void - * @throws \Exception + * @throws ChangelogTableNotExistsException */ public function drop() { $changelogTableName = $this->resource->getTableName($this->getName()); if (!$this->connection->isTableExists($changelogTableName)) { - throw new \Exception("Table {$changelogTableName} does not exist"); + throw new ChangelogTableNotExistsException(new Phrase("Table %1 does not exist", [$changelogTableName])); } $this->connection->dropTable($changelogTableName); @@ -114,13 +112,13 @@ public function drop() * * @param int $versionId * @return boolean - * @throws \Exception + * @throws ChangelogTableNotExistsException */ public function clear($versionId) { $changelogTableName = $this->resource->getTableName($this->getName()); if (!$this->connection->isTableExists($changelogTableName)) { - throw new \Exception("Table {$changelogTableName} does not exist"); + throw new ChangelogTableNotExistsException(new Phrase("Table %1 does not exist", [$changelogTableName])); } $this->connection->delete($changelogTableName, ['version_id <= ?' => (int)$versionId]); @@ -134,13 +132,13 @@ public function clear($versionId) * @param int $fromVersionId * @param int $toVersionId * @return int[] - * @throws \Exception + * @throws ChangelogTableNotExistsException */ public function getList($fromVersionId, $toVersionId) { $changelogTableName = $this->resource->getTableName($this->getName()); if (!$this->connection->isTableExists($changelogTableName)) { - throw new \Exception("Table {$changelogTableName} does not exist"); + throw new ChangelogTableNotExistsException(new Phrase("Table %1 does not exist", [$changelogTableName])); } $select = $this->connection->select()->distinct( @@ -161,15 +159,15 @@ public function getList($fromVersionId, $toVersionId) /** * Get maximum version_id from changelog - * * @return int + * @throws ChangelogTableNotExistsException * @throws \Exception */ public function getVersion() { $changelogTableName = $this->resource->getTableName($this->getName()); if (!$this->connection->isTableExists($changelogTableName)) { - throw new \Exception("Table {$changelogTableName} does not exist"); + throw new ChangelogTableNotExistsException(new Phrase("Table %1 does not exist", [$changelogTableName])); } $row = $this->connection->fetchRow('SHOW TABLE STATUS LIKE ?', [$changelogTableName]); if (isset($row['Auto_increment'])) { diff --git a/lib/internal/Magento/Framework/Mview/View/ChangelogTableNotExistsException.php b/lib/internal/Magento/Framework/Mview/View/ChangelogTableNotExistsException.php new file mode 100644 index 0000000000000..bfb50a2c365bd --- /dev/null +++ b/lib/internal/Magento/Framework/Mview/View/ChangelogTableNotExistsException.php @@ -0,0 +1,18 @@ +format('Y-m-d'); } catch (\Exception $e) { - throw new \Exception('Invalid input date format'); + throw new \Exception("Invalid input date format '$value'"); } } } diff --git a/lib/internal/Magento/Framework/Stdlib/DateTime/Filter/DateTime.php b/lib/internal/Magento/Framework/Stdlib/DateTime/Filter/DateTime.php index 8e3b55c2cb45f..b4557134648d1 100644 --- a/lib/internal/Magento/Framework/Stdlib/DateTime/Filter/DateTime.php +++ b/lib/internal/Magento/Framework/Stdlib/DateTime/Filter/DateTime.php @@ -7,15 +7,14 @@ */ namespace Magento\Framework\Stdlib\DateTime\Filter; -use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Phrase; class DateTime extends Date { /** * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate - * - * @deprecated + * + * @deprecated */ public function __construct(\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate) {