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 Feb 25, 2024
2 parents d2eeef2 + a2519ca commit 49bd991
Show file tree
Hide file tree
Showing 44 changed files with 1,477 additions and 387 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private function markUserNotified(): ResultInterface
public function execute()
{
$this->enableAdminUsage();
$this->markUserNotified();
return $this->markUserNotified();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\AdminAnalytics\Test\Unit\Controller\Adminhtml\Config;

use Magento\AdminAnalytics\Controller\Adminhtml\Config\EnableAdminUsage;
use Magento\AdminAnalytics\Model\ResourceModel\Viewer\Logger as NotificationLogger;
use Magento\Config\Model\Config;
use Magento\Config\Model\Config\Factory as ConfigFactory;
use Magento\Framework\App\ProductMetadataInterface;
use Magento\Framework\Controller\Result\Json as JsonResult;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use PHPUnit\Framework\MockObject\MockObject;

/**
* @covers \Magento\AdminAnalytics\Controller\Adminhtml\Config\EnableAdminUsage
*/
class EnableAdminUsageTest extends \PHPUnit\Framework\TestCase
{
private const STUB_PRODUCT_VERSION = 'Product Version';

/** @var EnableAdminUsage */
private $controller;

/** @var MockObject|Config */
private $configMock;

/** @var MockObject|ProductMetadataInterface */
private $productMetadataMock;

/** @var MockObject|NotificationLogger */
private $notificationLoggerMock;

/** @var MockObject|ResultFactory */
private $resultFactoryMock;

/** @var JsonResult|MockObject */
private $resultMock;

protected function setUp(): void
{
$objectManager = new ObjectManager($this);

$this->configMock = $this->getMockBuilder(Config::class)
->disableOriginalConstructor()
->onlyMethods(['setDataByPath', 'save'])
->getMock();

$configFactory = $this->getMockBuilder(ConfigFactory::class)
->disableOriginalConstructor()
->onlyMethods(['create'])
->getMock();

$configFactory->method('create')
->willReturn($this->configMock);

$this->productMetadataMock = $this->getMockBuilder(ProductMetadataInterface::class)
->onlyMethods(['getVersion'])
->getMockForAbstractClass();

$this->productMetadataMock->method('getVersion')
->willReturn(self::STUB_PRODUCT_VERSION);

$this->notificationLoggerMock = $this->getMockBuilder(NotificationLogger::class)
->disableOriginalConstructor()
->onlyMethods(['log'])
->getMock();

$this->resultFactoryMock = $this->getMockBuilder(ResultFactory::class)
->disableOriginalConstructor()
->onlyMethods(['create'])
->getMock();

$this->resultMock = $this->getMockBuilder(JsonResult::class)
->disableOriginalConstructor()
->onlyMethods(['setData'])
->getMock();

$this->resultFactoryMock->method('create')
->with(ResultFactory::TYPE_JSON)
->willReturn($this->resultMock);

$this->controller = $objectManager->getObject(EnableAdminUsage::class, [
'configFactory' => $configFactory,
'productMetadata' => $this->productMetadataMock,
'notificationLogger' => $this->notificationLoggerMock,
'resultFactory' => $this->resultFactoryMock
]);
}

/**
* If Controller returns `null`, no data is passed to the browser
*/
public function testResponseAfterAdminUsageChange()
{
// Given
$this->resultMock->method('setData')->willReturnSelf();

// When
$response = $this->controller->execute();

// Then
$this->assertInstanceOf(ResultInterface::class, $response);
}

public function testResponseWhenExceptionThrown()
{
$this->markTestSkipped('magento/magento2#31393 Lack of exception handling');

$this->configMock->method('setDataByPath')
->willThrowException(
new \Exception('System Exception')
);

// When
$response = $this->controller->execute();

// Then
$this->assertInstanceOf(ResultInterface::class, $response);
}
}
8 changes: 5 additions & 3 deletions app/code/Magento/Backend/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<admin>
<dashboard>
<enable_charts>0</enable_charts>
</dashboard>
</admin>
<dev>
<template>
<minify_html>0</minify_html>
Expand All @@ -22,9 +27,6 @@
<forgot_email_template>system_emails_forgot_email_template</forgot_email_template>
<forgot_email_identity>general</forgot_email_identity>
</emails>
<dashboard>
<enable_charts>1</enable_charts>
</dashboard>
<upload_configuration>
<enable_resize>1</enable_resize>
<max_width>1920</max_width>
Expand Down
4 changes: 2 additions & 2 deletions app/code/Magento/CacheInvalidate/Model/PurgeCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ class PurgeCache
/**
* Batch size of the purge request.
*
* Based on default Varnish 4 http_req_hdr_len size minus a 512 bytes margin for method,
* Based on default Varnish 6 http_req_hdr_len size minus a 512 bytes margin for method,
* header name, line feeds etc.
*
* @see https://varnish-cache.org/docs/4.1/reference/varnishd.html
* @see https://varnish-cache.org/docs/6.0/reference/varnishd.html
*
* @var int
*/
Expand Down
46 changes: 35 additions & 11 deletions app/code/Magento/CatalogRule/Model/Indexer/IndexBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace Magento\CatalogRule\Model\Indexer;

use Exception;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ProductFactory;
use Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher;
Expand All @@ -28,6 +29,7 @@
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
use Psr\Log\LoggerInterface;
use Zend_Db_Statement_Exception;

/**
* Catalog rule index builder
Expand Down Expand Up @@ -181,6 +183,16 @@ class IndexBuilder
*/
private $productCollectionFactory;

/**
* @var ReindexRuleProductsPrice
*/
private $reindexRuleProductsPrice;

/**
* @var int
*/
private $productBatchSize;

/**
* @param RuleCollectionFactory $ruleCollectionFactory
* @param PriceCurrencyInterface $priceCurrency
Expand All @@ -204,6 +216,8 @@ class IndexBuilder
* @param TimezoneInterface|null $localeDate
* @param ProductCollectionFactory|null $productCollectionFactory
* @param IndexerRegistry|null $indexerRegistry
* @param ReindexRuleProductsPrice|null $reindexRuleProductsPrice
* @param int $productBatchSize
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
Expand All @@ -229,7 +243,9 @@ public function __construct(
TableSwapper $tableSwapper = null,
TimezoneInterface $localeDate = null,
ProductCollectionFactory $productCollectionFactory = null,
IndexerRegistry $indexerRegistry = null
IndexerRegistry $indexerRegistry = null,
ReindexRuleProductsPrice $reindexRuleProductsPrice = null,
int $productBatchSize = 1000
) {
$this->resource = $resource;
$this->connection = $resource->getConnection();
Expand All @@ -242,6 +258,7 @@ public function __construct(
$this->dateTime = $dateTime;
$this->productFactory = $productFactory;
$this->batchCount = $batchCount;
$this->productBatchSize = $productBatchSize;

$this->productPriceCalculator = $productPriceCalculator ?? ObjectManager::getInstance()->get(
ProductPriceCalculator::class
Expand Down Expand Up @@ -275,6 +292,8 @@ public function __construct(
ObjectManager::getInstance()->get(IndexerRegistry::class);
$this->productCollectionFactory = $productCollectionFactory ??
ObjectManager::getInstance()->get(ProductCollectionFactory::class);
$this->reindexRuleProductsPrice = $reindexRuleProductsPrice ??
ObjectManager::getInstance()->get(ReindexRuleProductsPrice::class);
}

/**
Expand All @@ -296,7 +315,7 @@ public function reindexById($id)
}

$this->reindexRuleGroupWebsite->execute();
} catch (\Exception $e) {
} catch (Exception $e) {
$this->critical($e);
throw new LocalizedException(
__('Catalog rule indexing failed. See details in exception log.')
Expand All @@ -315,7 +334,7 @@ public function reindexByIds(array $ids)
{
try {
$this->doReindexByIds($ids);
} catch (\Exception $e) {
} catch (Exception $e) {
$this->critical($e);
throw new LocalizedException(
__("Catalog rule indexing failed. See details in exception log.")
Expand All @@ -328,6 +347,8 @@ public function reindexByIds(array $ids)
*
* @param array $ids
* @return void
* @throws LocalizedException
* @throws Zend_Db_Statement_Exception
*/
protected function doReindexByIds($ids)
{
Expand All @@ -340,9 +361,10 @@ protected function doReindexByIds($ids)
$this->reindexRuleProduct->execute($rule, $this->batchCount);
}

foreach ($ids as $productId) {
$this->cleanProductPriceIndex([$productId]);
$this->reindexRuleProductPrice->execute($this->batchCount, $productId);
// batch products together, using configurable batch size parameter
foreach (array_chunk($ids, $this->productBatchSize) as $productIds) {
$this->cleanProductPriceIndex($productIds);
$this->reindexRuleProductsPrice->execute($this->batchCount, $productIds);
}

//the case was not handled via indexer dependency decorator or via mview configuration
Expand All @@ -367,7 +389,7 @@ public function reindexFull()
{
try {
$this->doReindexFull();
} catch (\Exception $e) {
} catch (Exception $e) {
$this->critical($e);
throw new LocalizedException(
__("Catalog rule indexing failed. See details in exception log.")
Expand Down Expand Up @@ -441,6 +463,7 @@ protected function cleanByIds($productIds)
* @param int $productEntityId
* @param array $websiteIds
* @return void
* @throws Exception
*/
private function assignProductToRule(Rule $rule, int $productEntityId, array $websiteIds): void
{
Expand Down Expand Up @@ -502,7 +525,7 @@ private function assignProductToRule(Rule $rule, int $productEntityId, array $we
* @param Rule $rule
* @param Product $product
* @return $this
* @throws \Exception
* @throws Exception
* @deprecated 101.1.5
* @see ReindexRuleProduct::execute
* @SuppressWarnings(PHPMD.NPathComplexity)
Expand All @@ -525,6 +548,7 @@ protected function applyRule(Rule $rule, $product)
* @param RuleCollection $ruleCollection
* @param Product $product
* @return void
* @throws LocalizedException
*/
private function applyRules(RuleCollection $ruleCollection, Product $product): void
{
Expand Down Expand Up @@ -590,7 +614,7 @@ protected function updateRuleProductData(Rule $rule)
* Apply all rules
*
* @param Product|null $product
* @throws \Exception
* @throws Exception
* @return $this
* @deprecated 101.0.0
* @see ReindexRuleProductPrice::execute
Expand Down Expand Up @@ -661,7 +685,7 @@ protected function getRuleProductsStmt($websiteId, Product $product = null)
*
* @param array $arrData
* @return $this
* @throws \Exception
* @throws Exception
* @deprecated 101.0.0
* @see RuleProductPricesPersistor::execute
*/
Expand Down Expand Up @@ -708,7 +732,7 @@ protected function getProduct($productId)
/**
* Log critical exception
*
* @param \Exception $e
* @param Exception $e
* @return void
*/
protected function critical($e)
Expand Down
Loading

0 comments on commit 49bd991

Please sign in to comment.