Skip to content

Instantly share code, notes, and snippets.

@MichaelThessel
Created May 1, 2020 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichaelThessel/0b0cf69dd20326491115413adf7a94b9 to your computer and use it in GitHub Desktop.
Save MichaelThessel/0b0cf69dd20326491115413adf7a94b9 to your computer and use it in GitHub Desktop.
Patch for Magento issue #19469 (for installations utilizing vendor for core Magento modules)
diff --git a/vendor/magento/module-customer/Model/Customer.php b/vendor/magento/module-customer/Model/Customer.php
index 2692d1edf014..1e4914b152de 100644
--- a/vendor/magento/module-customer/Model/Customer.php
+++ b/vendor/magento/module-customer/Model/Customer.php
@@ -21,6 +21,7 @@
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Math\Random;
+use Magento\Framework\Indexer\IndexerInterface;
/**
* Customer model
@@ -62,8 +63,7 @@ class Customer extends \Magento\Framework\Model\AbstractModel
const XML_PATH_RESET_PASSWORD_TEMPLATE = 'customer/password/reset_password_template';
/**
- * @deprecated
- * @see AccountConfirmation::XML_PATH_IS_CONFIRM
+ * @deprecated @see \Magento\Customer\Model\AccountConfirmation::XML_PATH_IS_CONFIRM
*/
const XML_PATH_IS_CONFIRM = 'customer/create_account/confirm';
@@ -227,6 +227,11 @@ class Customer extends \Magento\Framework\Model\AbstractModel
*/
private $storedAddress;
+ /**
+ * @var IndexerInterface|null
+ */
+ private $indexer;
+
/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
@@ -304,6 +309,19 @@ public function __construct(
);
}
+ /**
+ * Micro-caching optimization
+ *
+ * @return IndexerInterface
+ */
+ private function getIndexer() : IndexerInterface
+ {
+ if ($this->indexer === null) {
+ $this->indexer = $this->indexerRegistry->get(self::CUSTOMER_GRID_INDEXER_ID);
+ }
+ return $this->indexer;
+ }
+
/**
* Initialize customer model
*
@@ -1075,8 +1093,7 @@ public function resetErrors()
*/
public function afterSave()
{
- $indexer = $this->indexerRegistry->get(self::CUSTOMER_GRID_INDEXER_ID);
- if ($indexer->getState()->getStatus() == StateInterface::STATUS_VALID) {
+ if ($this->getIndexer()->getState()->getStatus() == StateInterface::STATUS_VALID) {
$this->_getResource()->addCommitCallback([$this, 'reindex']);
}
return parent::afterSave();
@@ -1100,9 +1117,7 @@ public function afterDeleteCommit()
*/
public function reindex()
{
- /** @var \Magento\Framework\Indexer\IndexerInterface $indexer */
- $indexer = $this->indexerRegistry->get(self::CUSTOMER_GRID_INDEXER_ID);
- $indexer->reindexRow($this->getId());
+ $this->getIndexer()->reindexRow($this->getId());
}
/**
diff --git a/vendor/magento/module-customer/Setup/RecurringData.php b/vendor/magento/module-customer/Setup/RecurringData.php
index fbef4c05d126..76ff818ca7e5 100644
--- a/vendor/magento/module-customer/Setup/RecurringData.php
+++ b/vendor/magento/module-customer/Setup/RecurringData.php
@@ -7,6 +7,7 @@
namespace Magento\Customer\Setup;
use Magento\Framework\Indexer\IndexerRegistry;
+use Magento\Framework\Indexer\StateInterface;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
@@ -27,17 +28,34 @@ class RecurringData implements InstallDataInterface
*
* @param IndexerRegistry $indexerRegistry
*/
- public function __construct(IndexerRegistry $indexerRegistry)
- {
+ public function __construct(
+ IndexerRegistry $indexerRegistry
+ ) {
$this->indexerRegistry = $indexerRegistry;
}
/**
- * {@inheritdoc}
+ * @inheritDoc
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
- $indexer = $this->indexerRegistry->get(Customer::CUSTOMER_GRID_INDEXER_ID);
- $indexer->reindexAll();
+ if ($this->isNeedToDoReindex($setup)) {
+ $indexer = $this->indexerRegistry->get(Customer::CUSTOMER_GRID_INDEXER_ID);
+ $indexer->reindexAll();
+ }
+ }
+
+ /**
+ * Check is re-index needed
+ *
+ * @param ModuleDataSetupInterface $setup
+ * @return bool
+ */
+ private function isNeedToDoReindex(ModuleDataSetupInterface $setup) : bool
+ {
+ return !$setup->tableExists('customer_grid_flat')
+ || $this->indexerRegistry->get(Customer::CUSTOMER_GRID_INDEXER_ID)
+ ->getState()
+ ->getStatus() == StateInterface::STATUS_INVALID;
}
}
diff --git a/vendor/magento/module-customer/Test/Unit/Setup/RecurringDataTest.php b/vendor/magento/module-customer/Test/Unit/Setup/RecurringDataTest.php
new file mode 100644
index 000000000000..ee1af91552f6
--- /dev/null
+++ b/vendor/magento/module-customer/Test/Unit/Setup/RecurringDataTest.php
@@ -0,0 +1,129 @@
+<?php
+/**
+ * Copyright © Magento, Inc. All rights reserved.
+ * See COPYING.txt for license details.
+ */
+declare(strict_types=1);
+
+namespace Magento\Customer\Test\Unit\Setup;
+
+use Magento\Framework\Indexer\IndexerInterface;
+use Magento\Framework\Indexer\StateInterface;
+use Magento\Framework\Indexer\IndexerRegistry;
+use Magento\Framework\Setup\ModuleDataSetupInterface;
+use Magento\Framework\Setup\ModuleContextInterface;
+use Magento\Customer\Model\Customer;
+use Magento\Customer\Setup\RecurringData;
+use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
+
+/**
+ * Test for recurring data
+ */
+class RecurringDataTest extends \PHPUnit\Framework\TestCase
+{
+ /**
+ * @var ObjectManagerHelper
+ */
+ private $objectManagerHelper;
+
+ /**
+ * @var IndexerInterface|\PHPUnit_Framework_MockObject_MockObject
+ */
+ private $indexer;
+
+ /**
+ * @var StateInterface|\PHPUnit_Framework_MockObject_MockObject
+ */
+ private $state;
+
+ /**
+ * @var IndexerRegistry|\PHPUnit_Framework_MockObject_MockObject
+ */
+ private $indexerRegistry;
+
+ /**
+ * @var ModuleDataSetupInterface|\PHPUnit_Framework_MockObject_MockObject
+ */
+ private $setup;
+
+ /**
+ * @var ModuleContextInterface|\PHPUnit_Framework_MockObject_MockObject
+ */
+ private $context;
+
+ /**
+ * @var RecurringData
+ */
+ private $recurringData;
+
+ /**
+ * @inheritdoc
+ */
+ protected function setUp()
+ {
+ $this->objectManagerHelper = new ObjectManagerHelper($this);
+ $this->state = $this->getMockBuilder(StateInterface::class)
+ ->setMethods(['getStatus'])
+ ->getMockForAbstractClass();
+ $this->indexer = $this->getMockBuilder(IndexerInterface::class)
+ ->setMethods(['getState', 'reindexAll'])
+ ->getMockForAbstractClass();
+ $this->indexer->expects($this->any())
+ ->method('getState')
+ ->willReturn($this->state);
+ $this->indexerRegistry = $this->getMockBuilder(IndexerRegistry::class)
+ ->disableOriginalConstructor()
+ ->setMethods(['get'])
+ ->getMock();
+ $this->indexerRegistry->expects($this->any())
+ ->method('get')
+ ->with(Customer::CUSTOMER_GRID_INDEXER_ID)
+ ->willReturn($this->indexer);
+ $this->setup = $this->getMockBuilder(ModuleDataSetupInterface::class)
+ ->setMethods(['tableExists'])
+ ->getMockForAbstractClass();
+ $this->context = $this->getMockBuilder(ModuleContextInterface::class)
+ ->getMockForAbstractClass();
+
+ $this->recurringData = $this->objectManagerHelper->getObject(
+ RecurringData::class,
+ [
+ 'indexerRegistry' => $this->indexerRegistry
+ ]
+ );
+ }
+
+ /**
+ * @param bool $isTableExists
+ * @param string $indexerState
+ * @param int $countReindex
+ * @return void
+ * @dataProvider installDataProvider
+ */
+ public function testInstall(bool $isTableExists, string $indexerState, int $countReindex)
+ {
+ $this->setup->expects($this->any())
+ ->method('tableExists')
+ ->with('customer_grid_flat')
+ ->willReturn($isTableExists);
+ $this->state->expects($this->any())
+ ->method('getStatus')
+ ->willReturn($indexerState);
+ $this->indexer->expects($this->exactly($countReindex))
+ ->method('reindexAll');
+ $this->recurringData->install($this->setup, $this->context);
+ }
+
+ /**
+ * @return array
+ */
+ public function installDataProvider() : array
+ {
+ return [
+ [true, StateInterface::STATUS_INVALID, 1],
+ [false, StateInterface::STATUS_INVALID, 1],
+ [true, StateInterface::STATUS_VALID, 0],
+ [false, StateInterface::STATUS_VALID, 1],
+ ];
+ }
+}
diff --git a/vendor/magento/module-customer/Model/Customer.php b/vendor/magento/module-customer/Model/Customer.php
index 1e4914b152de..ea52994735c6 100644
--- a/vendor/magento/module-customer/Model/Customer.php
+++ b/vendor/magento/module-customer/Model/Customer.php
@@ -1003,6 +1003,7 @@ public function getSharedWebsiteIds()
*/
public function getAttributeSetId()
{
+ // phpstan:ignore "Call to an undefined static method*"
return parent::getAttributeSetId() ?: CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment