Skip to content

Commit

Permalink
Merge branch '2.4-develop' of https://github.com/mage-os/mirror-magento2
Browse files Browse the repository at this point in the history
 into 2.4-develop
  • Loading branch information
mage-os-ci committed Aug 23, 2024
2 parents a3ae7c3 + 71432ae commit 9fbf386
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 28 deletions.
11 changes: 11 additions & 0 deletions app/code/Magento/Indexer/Setup/Recurring.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ public function __construct(
*/
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
foreach ($this->config->getIndexers() as $index) {
$indexerId = $index['indexer_id'];
$state = $this->stateFactory->create();
$state->loadByIndexer($indexerId);
// If state does not exist, create default index mode to scheduled
if (empty($state->getData('state_id'))) {
$indexer = $this->indexerFactory->create()->load($indexerId);
$indexer->setScheduled(true);
}
}

/** @var State[] $stateIndexers */
$stateIndexers = [];
$states = $this->statesFactory->create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
use Magento\Framework\Autoload\AutoloaderInterface;
use Magento\Framework\Config\ConfigOptionsListConstants;
use Magento\Framework\Filesystem\Glob;
use Magento\Framework\Indexer\IndexerRegistry;
use Magento\Framework\Mail;
use Magento\Indexer\Model\Indexer\Collection;
use Magento\TestFramework;
use Magento\TestFramework\Fixture\Data\ProcessorInterface;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -567,9 +569,11 @@ public function install($cleanup)
);

$this->runPostInstallCommands();
$this->makeIndexStatusRealtime();

// enable only specified list of caches
$initParamsQuery = $this->getInitParamsQuery();

$this->_shell->execute(
PHP_BINARY . ' -f %s cache:disable -vvv --bootstrap=%s',
[BP . '/bin/magento', $initParamsQuery]
Expand Down Expand Up @@ -745,7 +749,7 @@ protected function _ensureDirExists($dir)
// phpcs:ignore Magento2.Functions.DiscouragedFunction
mkdir($dir, 0777, true);
umask($old);
// phpcs:ignore Magento2.Functions.DiscouragedFunction
// phpcs:ignore Magento2.Functions.DiscouragedFunction
} elseif (!is_dir($dir)) {
throw new \Magento\Framework\Exception\LocalizedException(__("'%1' is not a directory.", $dir));
}
Expand Down Expand Up @@ -825,4 +829,90 @@ protected function getCustomDirs()
];
return $customDirs;
}

/**
* Initialize the application and set the index status to realtime.
*
* @return void
*/
private function makeIndexStatusRealtime(): void
{
$overriddenParams = $this->getOverriddenParams();
$directoryList = $this->getDirectoryList($overriddenParams);
$objectManager = $this->getObjectManagers($overriddenParams, $directoryList);
Helper\Bootstrap::setObjectManager($objectManager);
$objectManagerConfiguration = [
'preferences' => [
\Magento\Framework\App\State::class => TestFramework\App\State::class
]
];
$objectManager->configure($objectManagerConfiguration);
$this->setIndexerToRealtime($objectManager);
}

/**
* Retrieves the overridden parameters.
*
* @return array
*/
private function getOverriddenParams(): array
{
$overriddenParams[\Magento\Framework\App\State::PARAM_MODE] = $this->_appMode;
return $this->_customizeParams($overriddenParams);
}

/**
* Retrieves the directory list.
*
* @param array $overriddenParams
* @return DirectoryList
*/
private function getDirectoryList($overriddenParams): DirectoryList
{
$directories = isset($overriddenParams[\Magento\Framework\App\Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS])
? $overriddenParams[\Magento\Framework\App\Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS]
: [];
return new DirectoryList(BP, $directories);
}

/**
* Retrieves the object manager.
*
* @param array $overriddenParams
* @param DirectoryList $directoryList The directory list.
* @return ObjectManager
*/
private function getObjectManagers($overriddenParams, $directoryList): ObjectManager
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
if (!$objectManager) {
$objectManager = $this->_factory->create($overriddenParams);
$objectManager->addSharedInstance($directoryList, DirectoryList::class);
$objectManager->addSharedInstance($directoryList, \Magento\Framework\Filesystem\DirectoryList::class);
} else {
$objectManager = $this->_factory->restore($objectManager, $directoryList, $overriddenParams);
}
return $objectManager;
}

/**
* Sets the indexer mode to realtime.
*
* @param ObjectManager $objectManager
* @return void
*/
private function setIndexerToRealtime($objectManager): void
{
/** @var Collection $indexCollection */
$indexCollection = $objectManager->get(Collection::class);
$indexerIds = $indexCollection->getAllIds();
if (!empty($indexerIds)) {
foreach ($indexerIds as $indexerId) {
/** @var IndexerInterface $model */
$model = $objectManager->get(IndexerRegistry::class)
->get($indexerId);
$model->setScheduled(false);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ class ApplicationTest extends \PHPUnit\Framework\TestCase
*/
private $appMode;

/**
* @var \Magento\TestFramework\ObjectManagerFactory|\PHPUnit\Framework\MockObject\MockObject
*/
private $factoryMock;

/**
* @var \Magento\TestFramework\ObjectManagerFactory
*/
private $_factory;

/**
* @var \Magento\Indexer\Model\Indexer\Collection | \PHPUnit\Framework\MockObject\MockObject
*/
private $collectionMock;

/**
* @var \Magento\TestFramework\ObjectManager | \PHPUnit\Framework\MockObject\MockObject
*/
private $objectManager;

/**
* @inheritdoc
*/
Expand All @@ -78,6 +98,19 @@ protected function setUp(): void
$this->appMode,
$this->autoloadWrapper
);

$this->factoryMock = $this->getMockBuilder(\Magento\TestFramework\ObjectManagerFactory::class)
->disableOriginalConstructor()
->onlyMethods(['create', 'restore'])
->getMock();

$this->collectionMock = $this->getMockBuilder(\Magento\Indexer\Model\Indexer\Collection::class)
->disableOriginalConstructor()
->getMock();

$this->objectManager = $this->getMockBuilder(\Magento\TestFramework\ObjectManager::class)
->disableOriginalConstructor()
->getMock();
}

/**
Expand Down Expand Up @@ -136,12 +169,18 @@ public function testInstall(
);

// bypass db dump logic
$reflectionProperty = new \ReflectionProperty($subject, '_factory');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($subject, $this->factoryMock);
$this->_factory = $this->factoryMock;
$dbMock = $this->getMockBuilder(Mysql::class)->disableOriginalConstructor()->getMock();

$reflectionSubject = new ReflectionClass($subject);
$dbProperty = $reflectionSubject->getProperty('_db');
$dbProperty->setAccessible(true);
$dbProperty->setValue($subject, $dbMock);
$property = $reflectionSubject->getProperty('canLoadArea');
$property->setValue($subject, false);

$dbMock
->expects($this->any())
Expand All @@ -150,7 +189,6 @@ public function testInstall(
false,
true
);

$withArgs = [];
// Add expected shell execution calls
foreach ($expectedShellExecutionCalls as $expectedShellExecutionArguments) {
Expand All @@ -174,6 +212,21 @@ public function testInstall(
}
});

$this->objectManager->expects($this->any())
->method('configure')
->willReturnSelf();
TestFrameworkBootstrap::setObjectManager($this->objectManager);

$this->_factory->expects($this->any())
->method('restore')
->willReturn($this->objectManager);
$this->objectManager->expects($this->any())
->method('get')
->willReturnCallback(fn($param) => match ([$param]) {
[\Magento\Indexer\Model\Indexer\Collection::class] => $this->collectionMock,
default => ''
});

$subject->install(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
namespace Magento\Customer\Model\ResourceModel\Grid;

use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Model\Customer;
use Magento\Customer\Model\CustomerRegistry;
use Magento\Framework\Exception\InvalidEmailOrPasswordException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Indexer\IndexerRegistry;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Indexer\TestCase;

Expand All @@ -20,6 +22,19 @@
*/
class CollectionReindexOnAccountLockTest extends TestCase
{
/** Set Up
*
* @return void
*
*/
protected function setUp(): void
{
parent::setUp();
$indexerRegistry = Bootstrap::getObjectManager()->create(IndexerRegistry::class);
$indexer = $indexerRegistry->get(Customer::CUSTOMER_GRID_INDEXER_ID);
$indexer->reindexAll();
}

/**
* Trigger customer account lock by making 10 failed authentication attempts
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Customer\Model\Customer;
use Magento\Framework\Indexer\IndexerRegistry;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Indexer\TestCase;
Expand All @@ -17,6 +19,7 @@
*/
class CollectionTest extends TestCase
{

public static function setUpBeforeClass(): void
{
$db = Bootstrap::getInstance()
Expand All @@ -28,6 +31,9 @@ public static function setUpBeforeClass(): void
}
$db->restoreFromDbDump();

$indexerRegistry = Bootstrap::getObjectManager()->create(IndexerRegistry::class);
$indexer = $indexerRegistry->get(Customer::CUSTOMER_GRID_INDEXER_ID);
$indexer->reindexAll();
parent::setUpBeforeClass();
}

Expand Down Expand Up @@ -88,12 +94,4 @@ public function testAddFieldToFilter(): void

$this->assertStringContainsString($expectedSelect, $collection->getSelectSql(true));
}

/**
* @inheritDoc
*/
protected function tearDown(): void
{
parent::tearDown();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
namespace Magento\Customer\Ui\Component;

use Magento\Backend\Model\Locale\Resolver;
use Magento\Customer\Model\Customer;
use Magento\Framework\Api\Filter;
use Magento\Framework\Indexer\IndexerRegistry;
use Magento\Framework\Locale\ResolverInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\MockObject\MockObject;
Expand Down Expand Up @@ -37,6 +39,9 @@ class DataProviderTest extends TestCase
protected function setUp(): void
{
$this->initLocaleResolverMock();
$indexerRegistry = Bootstrap::getObjectManager()->create(IndexerRegistry::class);
$indexer = $indexerRegistry->get(Customer::CUSTOMER_GRID_INDEXER_ID);
$indexer->reindexAll();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
namespace Magento\Customer\Ui\Component\Listing\Address;

use Magento\Backend\Model\Locale\Resolver;
use Magento\Customer\Model\Customer;
use Magento\Framework\Api\Filter;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Indexer\IndexerRegistry;
use Magento\Framework\Locale\ResolverInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\MockObject\MockObject;
Expand Down Expand Up @@ -52,6 +54,9 @@ protected function setUp(): void
'request' => $this->requestMock,
]
);
$indexerRegistry = Bootstrap::getObjectManager()->create(IndexerRegistry::class);
$indexer = $indexerRegistry->get(Customer::CUSTOMER_GRID_INDEXER_ID);
$indexer->reindexAll();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace Magento\Customer\Ui\Component\Listing;

use Magento\Customer\Model\Customer;
use Magento\Framework\Indexer\IndexerRegistry;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

Expand All @@ -28,6 +30,9 @@ class AttributeRepositoryTest extends TestCase
protected function setUp(): void
{
$this->model = Bootstrap::getObjectManager()->create(AttributeRepository::class);
$indexerRegistry = Bootstrap::getObjectManager()->create(IndexerRegistry::class);
$indexer = $indexerRegistry->get(Customer::CUSTOMER_GRID_INDEXER_ID);
$indexer->reindexAll();
}

/**
Expand Down
Loading

0 comments on commit 9fbf386

Please sign in to comment.