Skip to content

Commit

Permalink
Merge pull request #2696 from magento-tsg/2.3-develop-pr23
Browse files Browse the repository at this point in the history
[TSG] Upporting for 2.3 (pr23) (2.3.0)
  • Loading branch information
Alexander Akimov authored Jun 13, 2018
2 parents 285a39c + 012d0a3 commit c67893b
Show file tree
Hide file tree
Showing 62 changed files with 1,891 additions and 371 deletions.
3 changes: 2 additions & 1 deletion app/code/Magento/Bundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
},
"suggest": {
"magento/module-webapi": "*",
"magento/module-bundle-sample-data": "*"
"magento/module-bundle-sample-data": "*",
"magento/module-sales-rule": "*"
},
"type": "magento2-module",
"license": [
Expand Down
7 changes: 7 additions & 0 deletions app/code/Magento/Bundle/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,11 @@
</argument>
</arguments>
</type>
<type name="Magento\SalesRule\Model\Quote\ChildrenValidationLocator">
<arguments>
<argument name="productTypeChildrenValidationMap" xsi:type="array">
<item name="bundle" xsi:type="boolean">false</item>
</argument>
</arguments>
</type>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
*/
interface StockStatusInterface extends ExtensibleDataInterface
{
/**#@+
* Stock Status values.
*/
const STATUS_OUT_OF_STOCK = 0;

const STATUS_IN_STOCK = 1;
/**#@-*/

/**#@+
* Stock status object data keys
*/
Expand Down
8 changes: 0 additions & 8 deletions app/code/Magento/CatalogInventory/Model/Stock/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@
*/
class Status extends AbstractExtensibleModel implements StockStatusInterface
{
/**#@+
* Stock Status values
*/
const STATUS_OUT_OF_STOCK = 0;

const STATUS_IN_STOCK = 1;
/**#@-*/

/**#@+
* Field name
*/
Expand Down
19 changes: 14 additions & 5 deletions app/code/Magento/Elasticsearch/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
namespace Magento\Elasticsearch\Model;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Search\EngineResolverInterface;
use Magento\Search\Model\EngineResolver;
use Magento\Store\Model\ScopeInterface;
use Magento\AdvancedSearch\Model\Client\ClientOptionsInterface;
use Magento\AdvancedSearch\Model\Client\ClientResolver;
Expand Down Expand Up @@ -54,21 +56,28 @@ class Config implements ClientOptionsInterface
*/
private $clientResolver;

/**
* @var EngineResolverInterface
*/
private $engineResolver;

/**
* Constructor
*
* @param ScopeConfigInterface $scopeConfig
* @param ClientResolver $clientResolver
* @param string $prefix
* @param ClientResolver|null $clientResolver
* @param EngineResolverInterface|null $engineResolver
* @param string|null $prefix
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
ClientResolver $clientResolver = null,
EngineResolverInterface $engineResolver = null,
$prefix = null
) {
$this->scopeConfig = $scopeConfig;
$this->clientResolver = $clientResolver ?:
ObjectManager::getInstance()->get(ClientResolver::class);
$this->clientResolver = $clientResolver ?: ObjectManager::getInstance()->get(ClientResolver::class);
$this->engineResolver = $engineResolver ?: ObjectManager::getInstance()->get(EngineResolverInterface::class);
$this->prefix = $prefix ?: $this->clientResolver->getCurrentEngine();
}

Expand Down Expand Up @@ -126,7 +135,7 @@ public function getSearchConfigData($field, $storeId = null)
*/
public function isElasticsearchEnabled()
{
return $this->getSearchConfigData('engine') == self::ENGINE_NAME;
return $this->engineResolver->getCurrentSearchEngine() === self::ENGINE_NAME;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Elasticsearch\Model\Indexer\Plugin;

use Magento\Elasticsearch\Model\Config;
use Magento\Framework\Indexer\Config\DependencyInfoProvider as Provider;
use Magento\CatalogSearch\Model\Indexer\Fulltext as CatalogSearchFulltextIndexer;
use Magento\CatalogInventory\Model\Indexer\Stock\Processor as CatalogInventoryStockIndexer;

/**
* Plugin for maintenance catalog search index dependency on stock index.
* If elasticsearch is used as search engine catalog search index becomes dependent on stock index. Elasticsearch
* module declares this dependence. But in case when elasticsearch module is enabled and elasticsearch engine isn`t
* used as search engine other search engines don`t need this dependency.
* This plugin remove catalog search index dependency on stock index when elasticsearch isn`t used as search engine
* except full reindexing. During full reindexing this dependency doesn`t make overhead.
*/
class DependencyUpdaterPlugin
{
/**
* @var Config
*/
private $config;

/**
* @param Config $config
*/
public function __construct(Config $config)
{
$this->config = $config;
}

/**
* Remove index dependency, if it needed, on run reindexing by specifics indexes.
*
* @param Provider $provider
* @param array $dependencies
* @param string $indexerId
* @return array
* @see \Magento\Indexer\Console\Command\IndexerReindexCommand::getIndexers()
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterGetIndexerIdsToRunBefore(Provider $provider, array $dependencies, string $indexerId): array
{
if ($this->isFilteringNeeded($indexerId, CatalogSearchFulltextIndexer::INDEXER_ID)) {
$dependencies = array_diff($dependencies, [CatalogInventoryStockIndexer::INDEXER_ID]);
}

return $dependencies;
}

/**
* Remove index dependency, if it needed, on reindex triggers.
*
* @param Provider $provider
* @param array $dependencies
* @param string $indexerId
* @return array
* @see \Magento\Indexer\Model\Indexer\DependencyDecorator
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterGetIndexerIdsToRunAfter(Provider $provider, array $dependencies, string $indexerId): array
{
if ($this->isFilteringNeeded($indexerId, CatalogInventoryStockIndexer::INDEXER_ID)) {
$dependencies = array_diff($dependencies, [CatalogSearchFulltextIndexer::INDEXER_ID]);
}

return $dependencies;
}

/**
* @param string $currentIndexerId
* @param string $targetIndexerId
* @return bool
*/
private function isFilteringNeeded(string $currentIndexerId, string $targetIndexerId): bool
{
return (!$this->config->isElasticsearchEnabled() && $targetIndexerId === $currentIndexerId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Elasticsearch\Model\Indexer\Plugin;

use Magento\Elasticsearch\Model\Config;
use Magento\CatalogInventory\Api\StockConfigurationInterface;
use Magento\CatalogInventory\Api\StockStatusRepositoryInterface;
use Magento\CatalogInventory\Api\StockStatusCriteriaInterfaceFactory;
use Magento\CatalogInventory\Api\Data\StockStatusInterface;
use Magento\CatalogSearch\Model\Indexer\Fulltext\Action\DataProvider;

/**
* Plugin for filtering child products that are out of stock for preventing their saving to catalog search index.
*/
class StockedProductsFilterPlugin
{
/**
* @var Config
*/
private $config;

/**
* @var StockConfigurationInterface
*/
private $stockConfiguration;

/**
* @var StockStatusRepositoryInterface
*/
private $stockStatusRepository;

/**
* @var StockStatusCriteriaInterfaceFactory
*/
private $stockStatusCriteriaFactory;

/**
* @param Config $config
* @param StockConfigurationInterface $stockConfiguration
* @param StockStatusRepositoryInterface $stockStatusRepository
* @param StockStatusCriteriaInterfaceFactory $stockStatusCriteriaFactory
*/
public function __construct(
Config $config,
StockConfigurationInterface $stockConfiguration,
StockStatusRepositoryInterface $stockStatusRepository,
StockStatusCriteriaInterfaceFactory $stockStatusCriteriaFactory
) {
$this->config = $config;
$this->stockConfiguration = $stockConfiguration;
$this->stockStatusRepository = $stockStatusRepository;
$this->stockStatusCriteriaFactory = $stockStatusCriteriaFactory;
}

/**
* Filter out of stock options for configurable product.
*
* @param DataProvider $dataProvider
* @param array $indexData
* @param array $productData
* @param int $storeId
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function beforePrepareProductIndex(
DataProvider $dataProvider,
array $indexData,
array $productData,
int $storeId
): array {
if ($this->config->isElasticsearchEnabled() && !$this->stockConfiguration->isShowOutOfStock($storeId)) {
$productIds = array_keys($indexData);
$stockStatusCriteria = $this->stockStatusCriteriaFactory->create();
$stockStatusCriteria->setProductsFilter($productIds);
$stockStatusCollection = $this->stockStatusRepository->getList($stockStatusCriteria);
$stockStatuses = $stockStatusCollection->getItems();
$stockStatuses = array_filter($stockStatuses, function (StockStatusInterface $stockStatus) {
return StockStatusInterface::STATUS_IN_STOCK == $stockStatus->getStockStatus();
});
$indexData = array_intersect_key($indexData, $stockStatuses);
}

return [
$indexData,
$productData,
$storeId,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Elasticsearch\Test\Unit\Model\Indexer\Plugin;

use Magento\Elasticsearch\Model\Config;
use Magento\Elasticsearch\Model\Indexer\Plugin\DependencyUpdaterPlugin;
use Magento\Framework\Indexer\Config\DependencyInfoProvider;
use Magento\CatalogSearch\Model\Indexer\Fulltext as CatalogSearchFulltextIndexer;
use Magento\CatalogInventory\Model\Indexer\Stock\Processor as CatalogInventoryStockIndexer;

/**
* Test for Magento\Elasticsearch\Model\Indexer\Plugin\DependencyUpdaterPlugin class.
*/
class DependencyUpdaterPluginTest extends \PHPUnit\Framework\TestCase
{
/**
* @var Config|\PHPUnit_Framework_MockObject_MockObject
*/
private $configMock;

/**
* @var DependencyUpdaterPlugin
*/
private $plugin;

/**
* @var DependencyInfoProvider|\PHPUnit_Framework_MockObject_MockObject
*/
private $providerMock;

/**
* {@inheritdoc}
*/
protected function setUp()
{
$this->configMock = $this->getMockBuilder(Config::class)
->disableOriginalConstructor()
->getMock();
$this->configMock->expects($this->exactly(2))
->method('isElasticsearchEnabled')
->willReturnOnConsecutiveCalls(true, false);
$this->providerMock = $this->getMockBuilder(DependencyInfoProvider::class)
->disableOriginalConstructor()
->getMock();

$this->plugin = new DependencyUpdaterPlugin($this->configMock);
}

/**
* @return void
*/
public function testAfterGetIndexerIdsToRunBefore(): void
{
$dependencies = [
CatalogInventoryStockIndexer::INDEXER_ID,
];
$indexerId = CatalogSearchFulltextIndexer::INDEXER_ID;

$indexerIds = $this->plugin->afterGetIndexerIdsToRunBefore($this->providerMock, $dependencies, $indexerId);
$this->assertContains(CatalogInventoryStockIndexer::INDEXER_ID, $indexerIds);

$indexerIds = $this->plugin->afterGetIndexerIdsToRunBefore($this->providerMock, $dependencies, $indexerId);
$this->assertNotContains(CatalogInventoryStockIndexer::INDEXER_ID, $indexerIds);
}

/**
* @return void
*/
public function testAfterGetIndexerIdsToRunAfter(): void
{
$dependencies = [
CatalogSearchFulltextIndexer::INDEXER_ID,
];
$indexerId = CatalogInventoryStockIndexer::INDEXER_ID;

$indexerIds = $this->plugin->afterGetIndexerIdsToRunAfter($this->providerMock, $dependencies, $indexerId);
$this->assertContains(CatalogSearchFulltextIndexer::INDEXER_ID, $indexerIds);

$indexerIds = $this->plugin->afterGetIndexerIdsToRunAfter($this->providerMock, $dependencies, $indexerId);
$this->assertNotContains(CatalogSearchFulltextIndexer::INDEXER_ID, $indexerIds);
}
}
Loading

0 comments on commit c67893b

Please sign in to comment.