From c6e755420b4be6278c6c30778a1b2507c22f3d92 Mon Sep 17 00:00:00 2001
From: Pavel Bystritsky
Date: Thu, 29 Mar 2018 15:57:18 +0300
Subject: [PATCH 01/10] MSI-757: Add Inventory Index invalidation when Source
is enabled or disabled.
---
.../Model/GetSourceEnabled.php | 49 +++++
.../Model/GetSourceLinked.php | 64 ++++++
.../InvalidateAfterSourceSetEnabledPlugin.php | 77 ++++++++
...alidateAfterSourceSetEnabledPluginTest.php | 186 ++++++++++++++++++
app/code/Magento/InventoryIndexer/etc/di.xml | 3 +
5 files changed, 379 insertions(+)
create mode 100644 app/code/Magento/InventoryIndexer/Model/GetSourceEnabled.php
create mode 100644 app/code/Magento/InventoryIndexer/Model/GetSourceLinked.php
create mode 100644 app/code/Magento/InventoryIndexer/Plugin/InventoryApi/InvalidateAfterSourceSetEnabledPlugin.php
create mode 100644 app/code/Magento/InventoryIndexer/Test/Integration/Plugin/InventoryApi/InvalidateAfterSourceSetEnabledPluginTest.php
diff --git a/app/code/Magento/InventoryIndexer/Model/GetSourceEnabled.php b/app/code/Magento/InventoryIndexer/Model/GetSourceEnabled.php
new file mode 100644
index 0000000000000..bed28d4ec04fe
--- /dev/null
+++ b/app/code/Magento/InventoryIndexer/Model/GetSourceEnabled.php
@@ -0,0 +1,49 @@
+resource = $resource;
+ }
+
+ /**
+ * Get Enabled field value for Source by Source Code.
+ * @param string $sourceCode
+ * @return int|null
+ */
+ public function execute(string $sourceCode)
+ {
+ $connection = $this->resource->getConnection();
+ $select = $connection->select()
+ ->from(
+ Source::TABLE_NAME_SOURCE,
+ [SourceInterface::ENABLED]
+ )
+ ->where(SourceInterface::SOURCE_CODE . ' = ?', $sourceCode);
+ return $connection->fetchOne($select) ?: null;
+ }
+}
diff --git a/app/code/Magento/InventoryIndexer/Model/GetSourceLinked.php b/app/code/Magento/InventoryIndexer/Model/GetSourceLinked.php
new file mode 100644
index 0000000000000..df9d776d04c87
--- /dev/null
+++ b/app/code/Magento/InventoryIndexer/Model/GetSourceLinked.php
@@ -0,0 +1,64 @@
+resource = $resource;
+ }
+
+ /**
+ * Is Source linked with Stock or Source Items.
+ *
+ * @param string $sourceCode
+ * @return bool
+ */
+ public function execute(string $sourceCode): bool
+ {
+ return $this->isSourceInTable(SourceItem::TABLE_NAME_SOURCE_ITEM, $sourceCode)
+ && $this->isSourceInTable(StockSourceLink::TABLE_NAME_STOCK_SOURCE_LINK, $sourceCode);
+ }
+
+ /**
+ * Returns true if in Table exist some records related to Source.
+ *
+ * @param string $tableName
+ * @param string $sourceCode
+ * @return bool
+ */
+ private function isSourceInTable(string $tableName, string $sourceCode): bool
+ {
+ $connection = $this->resource->getConnection();
+ $select = $connection->select()
+ ->from(
+ $tableName,
+ 'COUNT(*)'
+ )
+ ->where(SourceInterface::SOURCE_CODE . ' = ?', $sourceCode);
+
+ return (bool)$connection->fetchOne($select);
+ }
+}
diff --git a/app/code/Magento/InventoryIndexer/Plugin/InventoryApi/InvalidateAfterSourceSetEnabledPlugin.php b/app/code/Magento/InventoryIndexer/Plugin/InventoryApi/InvalidateAfterSourceSetEnabledPlugin.php
new file mode 100644
index 0000000000000..ffb68f607eb1b
--- /dev/null
+++ b/app/code/Magento/InventoryIndexer/Plugin/InventoryApi/InvalidateAfterSourceSetEnabledPlugin.php
@@ -0,0 +1,77 @@
+indexerRegistry = $indexerRegistry;
+ $this->getSourceEnabled = $getSourceEnabled;
+ $this->getSourceLinked = $getSourceLinked;
+ }
+
+ /**
+ * Invalidate Inventory Indexer after Source was enabled or disabled.
+ *
+ * @param SourceRepositoryInterface $subject
+ * @param callable $proceed
+ * @param SourceInterface $source
+ * @return void
+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)
+ */
+ public function aroundSave(
+ SourceRepositoryInterface $subject,
+ callable $proceed,
+ SourceInterface $source
+ ) {
+ $sourceCode = $source->getSourceCode();
+ $oldStatus = (int)$this->getSourceEnabled->execute($sourceCode);
+ $proceed($source);
+ $status = (int)$source->isEnabled();
+ if (($oldStatus !== $status) && $this->getSourceLinked->execute($sourceCode)) {
+ $indexer = $this->indexerRegistry->get(InventoryIndexer::INDEXER_ID);
+ if ($indexer->isValid()) {
+ $indexer->invalidate();
+ }
+ }
+ }
+}
diff --git a/app/code/Magento/InventoryIndexer/Test/Integration/Plugin/InventoryApi/InvalidateAfterSourceSetEnabledPluginTest.php b/app/code/Magento/InventoryIndexer/Test/Integration/Plugin/InventoryApi/InvalidateAfterSourceSetEnabledPluginTest.php
new file mode 100644
index 0000000000000..b0f460ea0c1db
--- /dev/null
+++ b/app/code/Magento/InventoryIndexer/Test/Integration/Plugin/InventoryApi/InvalidateAfterSourceSetEnabledPluginTest.php
@@ -0,0 +1,186 @@
+sourceRepository = Bootstrap::getObjectManager()->get(SourceRepositoryInterface::class);
+ $this->indexerRegistry = Bootstrap::getObjectManager()->get(IndexerRegistry::class);
+ }
+
+ /**
+ * Tests Source enabling and disabling when no Stocks or Source Items are connected to current Source.
+ *
+ * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
+ * @dataProvider IndexerInvalidationOnlySourcesDataProvider
+ * @param string $sourceCode
+ * @param bool $enable
+ * @param bool $expectedValid
+ */
+ public function testIndexerInvalidationOnlySources(string $sourceCode, bool $enable, bool $expectedValid)
+ {
+ $this->indexerInvalidationBase($sourceCode, $enable, $expectedValid);
+ }
+
+ /**
+ * Tests Source enabling and disabling when no Stocks are connected to current Source.
+ *
+ * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
+ * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
+ * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
+ *
+ * @dataProvider IndexerInvalidationNoStockLinksDataProvider
+ * @param string $sourceCode
+ * @param bool $enable
+ * @param bool $expectedValid
+ */
+ public function testIndexerInvalidationNoStockLinks(string $sourceCode, bool $enable, bool $expectedValid)
+ {
+ $this->indexerInvalidationBase($sourceCode, $enable, $expectedValid);
+ }
+
+ /**
+ * Tests Source enabling and disabling when no Source Items are connected to current Source.
+ *
+ * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
+ * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
+ * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
+ * @dataProvider IndexerInvalidationNoSourceItemsDataProvider
+ * @param string $sourceCode
+ * @param bool $enable
+ * @param bool $expectedValid
+ */
+ public function testIndexerInvalidationNoSourceItems(string $sourceCode, bool $enable, bool $expectedValid)
+ {
+ $this->indexerInvalidationBase($sourceCode, $enable, $expectedValid);
+ }
+
+ /**
+ * Tests Source enabling and disabling when both Stocks and Source Items are connected to current Source.
+ *
+ * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
+ * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
+ * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
+ * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
+ * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
+ * @dataProvider IndexerInvalidationFullDataProvider
+ * @param string $sourceCode
+ * @param bool $enable
+ * @param bool $expectedValid
+ */
+ public function testIndexerInvalidationFull(string $sourceCode, bool $enable, bool $expectedValid)
+ {
+ $this->indexerInvalidationBase($sourceCode, $enable, $expectedValid);
+ }
+
+ /**
+ * Contains generic logic for all Indexer invalidation after Source enabled or disabled tests.
+ *
+ * @param string $sourceCode
+ * @param bool $enable
+ * @param bool $expectedValid
+ */
+ private function indexerInvalidationBase(string $sourceCode, bool $enable, bool $expectedValid)
+ {
+ $indexer = $this->indexerRegistry->get(InventoryIndexer::INDEXER_ID);
+ $indexer->reindexAll();
+
+ $this->assertTrue($indexer->isValid());
+
+ $source = $this->sourceRepository->get($sourceCode);
+ $source->setEnabled($enable);
+ $this->sourceRepository->save($source);
+ $actualValid = $indexer->isValid();
+
+ $this->assertEquals(
+ $expectedValid,
+ $actualValid
+ );
+ }
+
+ /**
+ * Data provider for testIndexerInvalidationOnlySources.
+ *
+ * @return array
+ */
+ public function indexerInvalidationOnlySourcesDataProvider()
+ {
+ return [
+ ['eu-1', true, true],
+ ['eu-1', false, true],
+ ['eu-disabled', true, true],
+ ['eu-disabled', false, true],
+ ];
+ }
+
+ /**
+ * Data provider for testIndexerInvalidationNoStockLinks.
+ *
+ * @return array
+ */
+ public function indexerInvalidationNoStockLinksDataProvider()
+ {
+ return [
+ ['eu-1', true, true],
+ ['eu-1', false, true],
+ ['eu-disabled', true, true],
+ ['eu-disabled', false, true],
+ ];
+ }
+
+ /**
+ * Data provider for testIndexerInvalidationNoSourceItems.
+ *
+ * @return array
+ */
+ public function indexerInvalidationNoSourceItemsDataProvider()
+ {
+ return [
+ ['eu-1', true, true],
+ ['eu-1', false, true],
+ ['eu-disabled', true, true],
+ ['eu-disabled', false, true],
+ ];
+ }
+
+ /**
+ * Data provider for testIndexerInvalidationFull.
+ *
+ * @return array
+ */
+ public function indexerInvalidationFullDataProvider()
+ {
+ return [
+ ['eu-1', true, true],
+ ['eu-1', false, false],
+ ['eu-disabled', true, false],
+ ['eu-disabled', false, true],
+ ];
+ }
+}
diff --git a/app/code/Magento/InventoryIndexer/etc/di.xml b/app/code/Magento/InventoryIndexer/etc/di.xml
index 118d4b59e81d0..c8fe10f8bb80d 100644
--- a/app/code/Magento/InventoryIndexer/etc/di.xml
+++ b/app/code/Magento/InventoryIndexer/etc/di.xml
@@ -36,6 +36,9 @@
+
+
+
From aa75b9c3b216fb2ae07c02d518d8994a01fc87e7 Mon Sep 17 00:00:00 2001
From: Pavel Bystritsky
Date: Fri, 30 Mar 2018 15:49:23 +0300
Subject: [PATCH 02/10] MSI-757: Add Inventory Index invalidation when Source
is enabled or disabled.
---
.../Model/GetInvalidationRequired.php | 66 +++++++++++++++++++
.../Model/GetSourceEnabled.php | 49 --------------
.../Model/GetSourceLinked.php | 64 ------------------
.../InvalidateAfterSourceSetEnabledPlugin.php | 27 +++-----
4 files changed, 75 insertions(+), 131 deletions(-)
create mode 100644 app/code/Magento/InventoryIndexer/Model/GetInvalidationRequired.php
delete mode 100644 app/code/Magento/InventoryIndexer/Model/GetSourceEnabled.php
delete mode 100644 app/code/Magento/InventoryIndexer/Model/GetSourceLinked.php
diff --git a/app/code/Magento/InventoryIndexer/Model/GetInvalidationRequired.php b/app/code/Magento/InventoryIndexer/Model/GetInvalidationRequired.php
new file mode 100644
index 0000000000000..5b7fa7845b605
--- /dev/null
+++ b/app/code/Magento/InventoryIndexer/Model/GetInvalidationRequired.php
@@ -0,0 +1,66 @@
+resource = $resource;
+ }
+
+ /**
+ * Is Inventory Indexer invalidation required after Source enabling or disabling.
+ *
+ * Returns 'true' only if Source 'enabled' value is changed, Source is linked to Stock and contains at least one
+ * Stock Item.
+ *
+ * @param string $sourceCode
+ * @param int $enabled
+ * @return bool
+ */
+ public function execute(string $sourceCode, int $enabled): bool
+ {
+ $connection = $this->resource->getConnection();
+ $select = $connection->select()
+ ->from(
+ ['tis' => Source::TABLE_NAME_SOURCE],
+ 'IF(tis.' . SourceInterface::ENABLED . '=' . $enabled . ', 0, 1)'
+ )
+ ->joinInner(
+ ['isi' => SourceItem::TABLE_NAME_SOURCE_ITEM],
+ 'tis.' . SourceInterface::SOURCE_CODE . '=' . 'isi.' . SourceInterface::SOURCE_CODE,
+ ''
+ )->joinInner(
+ ['issl' => StockSourceLink::TABLE_NAME_STOCK_SOURCE_LINK],
+ 'tis.' . SourceInterface::SOURCE_CODE . '=' . 'issl.' . SourceInterface::SOURCE_CODE,
+ ''
+ )
+ ->where('tis.' . SourceInterface::SOURCE_CODE . ' = ?', $sourceCode);
+
+ return (bool)$connection->fetchOne($select);
+ }
+}
diff --git a/app/code/Magento/InventoryIndexer/Model/GetSourceEnabled.php b/app/code/Magento/InventoryIndexer/Model/GetSourceEnabled.php
deleted file mode 100644
index bed28d4ec04fe..0000000000000
--- a/app/code/Magento/InventoryIndexer/Model/GetSourceEnabled.php
+++ /dev/null
@@ -1,49 +0,0 @@
-resource = $resource;
- }
-
- /**
- * Get Enabled field value for Source by Source Code.
- * @param string $sourceCode
- * @return int|null
- */
- public function execute(string $sourceCode)
- {
- $connection = $this->resource->getConnection();
- $select = $connection->select()
- ->from(
- Source::TABLE_NAME_SOURCE,
- [SourceInterface::ENABLED]
- )
- ->where(SourceInterface::SOURCE_CODE . ' = ?', $sourceCode);
- return $connection->fetchOne($select) ?: null;
- }
-}
diff --git a/app/code/Magento/InventoryIndexer/Model/GetSourceLinked.php b/app/code/Magento/InventoryIndexer/Model/GetSourceLinked.php
deleted file mode 100644
index df9d776d04c87..0000000000000
--- a/app/code/Magento/InventoryIndexer/Model/GetSourceLinked.php
+++ /dev/null
@@ -1,64 +0,0 @@
-resource = $resource;
- }
-
- /**
- * Is Source linked with Stock or Source Items.
- *
- * @param string $sourceCode
- * @return bool
- */
- public function execute(string $sourceCode): bool
- {
- return $this->isSourceInTable(SourceItem::TABLE_NAME_SOURCE_ITEM, $sourceCode)
- && $this->isSourceInTable(StockSourceLink::TABLE_NAME_STOCK_SOURCE_LINK, $sourceCode);
- }
-
- /**
- * Returns true if in Table exist some records related to Source.
- *
- * @param string $tableName
- * @param string $sourceCode
- * @return bool
- */
- private function isSourceInTable(string $tableName, string $sourceCode): bool
- {
- $connection = $this->resource->getConnection();
- $select = $connection->select()
- ->from(
- $tableName,
- 'COUNT(*)'
- )
- ->where(SourceInterface::SOURCE_CODE . ' = ?', $sourceCode);
-
- return (bool)$connection->fetchOne($select);
- }
-}
diff --git a/app/code/Magento/InventoryIndexer/Plugin/InventoryApi/InvalidateAfterSourceSetEnabledPlugin.php b/app/code/Magento/InventoryIndexer/Plugin/InventoryApi/InvalidateAfterSourceSetEnabledPlugin.php
index ffb68f607eb1b..6061500733519 100644
--- a/app/code/Magento/InventoryIndexer/Plugin/InventoryApi/InvalidateAfterSourceSetEnabledPlugin.php
+++ b/app/code/Magento/InventoryIndexer/Plugin/InventoryApi/InvalidateAfterSourceSetEnabledPlugin.php
@@ -11,8 +11,7 @@
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\InventoryApi\Api\SourceRepositoryInterface;
use Magento\InventoryIndexer\Indexer\InventoryIndexer;
-use Magento\InventoryIndexer\Model\GetSourceEnabled;
-use Magento\InventoryIndexer\Model\GetSourceLinked;
+use Magento\InventoryIndexer\Model\GetInvalidationRequired;
/**
* Invalidate Inventory Indexer after Source was enabled or disabled.
@@ -25,28 +24,20 @@ class InvalidateAfterSourceSetEnabledPlugin
private $indexerRegistry;
/**
- * @var GetSourceEnabled
+ * @var GetInvalidationRequired
*/
- private $getSourceEnabled;
-
- /**
- * @var GetSourceLinked
- */
- private $getSourceLinked;
+ private $getInvalidationRequired;
/**
* @param IndexerRegistry $indexerRegistry
- * @param GetSourceEnabled $getSourceEnabled
- * @param GetSourceLinked $getSourceLinked
+ * @param GetInvalidationRequired $getInvalidationRequired
*/
public function __construct(
IndexerRegistry $indexerRegistry,
- GetSourceEnabled $getSourceEnabled,
- GetSourceLinked $getSourceLinked
+ GetInvalidationRequired $getInvalidationRequired
) {
$this->indexerRegistry = $indexerRegistry;
- $this->getSourceEnabled = $getSourceEnabled;
- $this->getSourceLinked = $getSourceLinked;
+ $this->getInvalidationRequired = $getInvalidationRequired;
}
/**
@@ -64,10 +55,10 @@ public function aroundSave(
SourceInterface $source
) {
$sourceCode = $source->getSourceCode();
- $oldStatus = (int)$this->getSourceEnabled->execute($sourceCode);
+ $enabled = (int)$source->isEnabled();
+ $invalidationRequired = $this->getInvalidationRequired->execute($sourceCode, $enabled);
$proceed($source);
- $status = (int)$source->isEnabled();
- if (($oldStatus !== $status) && $this->getSourceLinked->execute($sourceCode)) {
+ if ($invalidationRequired) {
$indexer = $this->indexerRegistry->get(InventoryIndexer::INDEXER_ID);
if ($indexer->isValid()) {
$indexer->invalidate();
From a0eaec538e635f7e6b6d32d5238c66f66cce4c59 Mon Sep 17 00:00:00 2001
From: Pavel Bystritsky
Date: Mon, 2 Apr 2018 14:30:22 +0300
Subject: [PATCH 03/10] MSI-757: Add Inventory Index invalidation when Source
is enabled or disabled.
---
.../InventoryIndexer/Model/GetInvalidationRequired.php | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/app/code/Magento/InventoryIndexer/Model/GetInvalidationRequired.php b/app/code/Magento/InventoryIndexer/Model/GetInvalidationRequired.php
index 5b7fa7845b605..6267737fe8357 100644
--- a/app/code/Magento/InventoryIndexer/Model/GetInvalidationRequired.php
+++ b/app/code/Magento/InventoryIndexer/Model/GetInvalidationRequired.php
@@ -47,15 +47,15 @@ public function execute(string $sourceCode, int $enabled): bool
$connection = $this->resource->getConnection();
$select = $connection->select()
->from(
- ['tis' => Source::TABLE_NAME_SOURCE],
+ ['tis' => $connection->getTableName(Source::TABLE_NAME_SOURCE)],
'IF(tis.' . SourceInterface::ENABLED . '=' . $enabled . ', 0, 1)'
)
->joinInner(
- ['isi' => SourceItem::TABLE_NAME_SOURCE_ITEM],
+ ['isi' => $connection->getTableName(SourceItem::TABLE_NAME_SOURCE_ITEM)],
'tis.' . SourceInterface::SOURCE_CODE . '=' . 'isi.' . SourceInterface::SOURCE_CODE,
''
)->joinInner(
- ['issl' => StockSourceLink::TABLE_NAME_STOCK_SOURCE_LINK],
+ ['issl' => $connection->getTableName(StockSourceLink::TABLE_NAME_STOCK_SOURCE_LINK)],
'tis.' . SourceInterface::SOURCE_CODE . '=' . 'issl.' . SourceInterface::SOURCE_CODE,
''
)
From 2848806a59642e8a45f7ae33efa3a70049b62885 Mon Sep 17 00:00:00 2001
From: Pavel Bystritsky
Date: Mon, 2 Apr 2018 17:09:09 +0300
Subject: [PATCH 04/10] MSI-757: Add Inventory Index invalidation when Source
is enabled or disabled.
---
.../InventoryIndexer/Model/GetInvalidationRequired.php | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/app/code/Magento/InventoryIndexer/Model/GetInvalidationRequired.php b/app/code/Magento/InventoryIndexer/Model/GetInvalidationRequired.php
index 6267737fe8357..aab41cdb8592d 100644
--- a/app/code/Magento/InventoryIndexer/Model/GetInvalidationRequired.php
+++ b/app/code/Magento/InventoryIndexer/Model/GetInvalidationRequired.php
@@ -47,15 +47,15 @@ public function execute(string $sourceCode, int $enabled): bool
$connection = $this->resource->getConnection();
$select = $connection->select()
->from(
- ['tis' => $connection->getTableName(Source::TABLE_NAME_SOURCE)],
+ ['tis' => $this->resource->getTableName(Source::TABLE_NAME_SOURCE)],
'IF(tis.' . SourceInterface::ENABLED . '=' . $enabled . ', 0, 1)'
)
->joinInner(
- ['isi' => $connection->getTableName(SourceItem::TABLE_NAME_SOURCE_ITEM)],
+ ['isi' => $this->resource->getTableName(SourceItem::TABLE_NAME_SOURCE_ITEM)],
'tis.' . SourceInterface::SOURCE_CODE . '=' . 'isi.' . SourceInterface::SOURCE_CODE,
''
)->joinInner(
- ['issl' => $connection->getTableName(StockSourceLink::TABLE_NAME_STOCK_SOURCE_LINK)],
+ ['issl' => $this->resource->getTableName(StockSourceLink::TABLE_NAME_STOCK_SOURCE_LINK)],
'tis.' . SourceInterface::SOURCE_CODE . '=' . 'issl.' . SourceInterface::SOURCE_CODE,
''
)
From 0ea3d2ee0fc7114921c4e77b9ae04680457ccb18 Mon Sep 17 00:00:00 2001
From: Pavel Bystritsky
Date: Wed, 4 Apr 2018 17:09:14 +0300
Subject: [PATCH 05/10] MSI-757: Add Inventory Index invalidation when Source
is enabled or disabled.
---
.../Model/{ => ResourceModel}/GetInvalidationRequired.php | 2 +-
.../InventoryApi/InvalidateAfterSourceSetEnabledPlugin.php | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
rename app/code/Magento/InventoryIndexer/Model/{ => ResourceModel}/GetInvalidationRequired.php (97%)
diff --git a/app/code/Magento/InventoryIndexer/Model/GetInvalidationRequired.php b/app/code/Magento/InventoryIndexer/Model/ResourceModel/GetInvalidationRequired.php
similarity index 97%
rename from app/code/Magento/InventoryIndexer/Model/GetInvalidationRequired.php
rename to app/code/Magento/InventoryIndexer/Model/ResourceModel/GetInvalidationRequired.php
index aab41cdb8592d..644dfa61a09a3 100644
--- a/app/code/Magento/InventoryIndexer/Model/GetInvalidationRequired.php
+++ b/app/code/Magento/InventoryIndexer/Model/ResourceModel/GetInvalidationRequired.php
@@ -5,7 +5,7 @@
*/
declare(strict_types=1);
-namespace Magento\InventoryIndexer\Model;
+namespace Magento\InventoryIndexer\Model\ResourceModel;
use Magento\Framework\App\ResourceConnection;
use Magento\Inventory\Model\ResourceModel\Source;
diff --git a/app/code/Magento/InventoryIndexer/Plugin/InventoryApi/InvalidateAfterSourceSetEnabledPlugin.php b/app/code/Magento/InventoryIndexer/Plugin/InventoryApi/InvalidateAfterSourceSetEnabledPlugin.php
index 6061500733519..5d08724fc6e40 100644
--- a/app/code/Magento/InventoryIndexer/Plugin/InventoryApi/InvalidateAfterSourceSetEnabledPlugin.php
+++ b/app/code/Magento/InventoryIndexer/Plugin/InventoryApi/InvalidateAfterSourceSetEnabledPlugin.php
@@ -11,7 +11,7 @@
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\InventoryApi\Api\SourceRepositoryInterface;
use Magento\InventoryIndexer\Indexer\InventoryIndexer;
-use Magento\InventoryIndexer\Model\GetInvalidationRequired;
+use Magento\InventoryIndexer\Model\ResourceModel\GetInvalidationRequired;
/**
* Invalidate Inventory Indexer after Source was enabled or disabled.
From 11060a9a260ce22e357ba98c2115465c9ee3f27e Mon Sep 17 00:00:00 2001
From: Valeriy Nayda
Date: Wed, 4 Apr 2018 17:16:28 +0300
Subject: [PATCH 06/10] MSI-757: Add Inventory Index invalidation when Source
is enabled or disabled
---
.../ResourceModel/GetInvalidationRequired.php | 66 --------
.../{ => ResourceModel}/GetStockItemData.php | 3 +-
.../IsInvalidationRequiredForSource.php | 68 ++++++++
...eAfterEnablingOrDisablingSourcePlugin.php} | 23 +--
...dateAfterStockSourceLinksDeletePlugin.php} | 2 +-
...lidateAfterStockSourceLinksSavePlugin.php} | 2 +-
.../Integration/Indexer/SourceIndexerTest.php | 2 +-
.../Indexer/SourceItemIndexerTest.php | 2 +-
.../Integration/Indexer/StockIndexerTest.php | 2 +-
...ateAfterEnablingOrDisablingSourceTest.php} | 145 +++++++++---------
app/code/Magento/InventoryIndexer/etc/di.xml | 12 +-
11 files changed, 166 insertions(+), 161 deletions(-)
delete mode 100644 app/code/Magento/InventoryIndexer/Model/ResourceModel/GetInvalidationRequired.php
rename app/code/Magento/InventoryIndexer/Model/{ => ResourceModel}/GetStockItemData.php (93%)
create mode 100644 app/code/Magento/InventoryIndexer/Model/ResourceModel/IsInvalidationRequiredForSource.php
rename app/code/Magento/InventoryIndexer/Plugin/InventoryApi/{InvalidateAfterSourceSetEnabledPlugin.php => InvalidateAfterEnablingOrDisablingSourcePlugin.php} (70%)
rename app/code/Magento/InventoryIndexer/Plugin/InventoryApi/{ReindexAfterStockSourceLinksDeletePlugin.php => InvalidateAfterStockSourceLinksDeletePlugin.php} (97%)
rename app/code/Magento/InventoryIndexer/Plugin/InventoryApi/{ReindexAfterStockSourceLinksSavePlugin.php => InvalidateAfterStockSourceLinksSavePlugin.php} (97%)
rename app/code/Magento/InventoryIndexer/Test/Integration/{Plugin/InventoryApi/InvalidateAfterSourceSetEnabledPluginTest.php => InvalidateAfterEnablingOrDisablingSourceTest.php} (75%)
diff --git a/app/code/Magento/InventoryIndexer/Model/ResourceModel/GetInvalidationRequired.php b/app/code/Magento/InventoryIndexer/Model/ResourceModel/GetInvalidationRequired.php
deleted file mode 100644
index 644dfa61a09a3..0000000000000
--- a/app/code/Magento/InventoryIndexer/Model/ResourceModel/GetInvalidationRequired.php
+++ /dev/null
@@ -1,66 +0,0 @@
-resource = $resource;
- }
-
- /**
- * Is Inventory Indexer invalidation required after Source enabling or disabling.
- *
- * Returns 'true' only if Source 'enabled' value is changed, Source is linked to Stock and contains at least one
- * Stock Item.
- *
- * @param string $sourceCode
- * @param int $enabled
- * @return bool
- */
- public function execute(string $sourceCode, int $enabled): bool
- {
- $connection = $this->resource->getConnection();
- $select = $connection->select()
- ->from(
- ['tis' => $this->resource->getTableName(Source::TABLE_NAME_SOURCE)],
- 'IF(tis.' . SourceInterface::ENABLED . '=' . $enabled . ', 0, 1)'
- )
- ->joinInner(
- ['isi' => $this->resource->getTableName(SourceItem::TABLE_NAME_SOURCE_ITEM)],
- 'tis.' . SourceInterface::SOURCE_CODE . '=' . 'isi.' . SourceInterface::SOURCE_CODE,
- ''
- )->joinInner(
- ['issl' => $this->resource->getTableName(StockSourceLink::TABLE_NAME_STOCK_SOURCE_LINK)],
- 'tis.' . SourceInterface::SOURCE_CODE . '=' . 'issl.' . SourceInterface::SOURCE_CODE,
- ''
- )
- ->where('tis.' . SourceInterface::SOURCE_CODE . ' = ?', $sourceCode);
-
- return (bool)$connection->fetchOne($select);
- }
-}
diff --git a/app/code/Magento/InventoryIndexer/Model/GetStockItemData.php b/app/code/Magento/InventoryIndexer/Model/ResourceModel/GetStockItemData.php
similarity index 93%
rename from app/code/Magento/InventoryIndexer/Model/GetStockItemData.php
rename to app/code/Magento/InventoryIndexer/Model/ResourceModel/GetStockItemData.php
index f7b7adf3b8d6d..beeadcbefbecc 100644
--- a/app/code/Magento/InventoryIndexer/Model/GetStockItemData.php
+++ b/app/code/Magento/InventoryIndexer/Model/ResourceModel/GetStockItemData.php
@@ -5,10 +5,11 @@
*/
declare(strict_types=1);
-namespace Magento\InventoryIndexer\Model;
+namespace Magento\InventoryIndexer\Model\ResourceModel;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Exception\LocalizedException;
+use Magento\InventoryIndexer\Model\StockIndexTableNameResolverInterface;
use Magento\InventorySales\Model\GetStockItemDataInterface;
use Magento\InventoryIndexer\Indexer\IndexStructure;
diff --git a/app/code/Magento/InventoryIndexer/Model/ResourceModel/IsInvalidationRequiredForSource.php b/app/code/Magento/InventoryIndexer/Model/ResourceModel/IsInvalidationRequiredForSource.php
new file mode 100644
index 0000000000000..d53024593b306
--- /dev/null
+++ b/app/code/Magento/InventoryIndexer/Model/ResourceModel/IsInvalidationRequiredForSource.php
@@ -0,0 +1,68 @@
+resourceConnection = $resourceConnection;
+ }
+
+ /**
+ * Returns 'true' only if Source 'enabled' value is changed, Source is linked to Stock and contains at least one
+ * Source Item.
+ *
+ * @param string $sourceCode
+ * @param bool $enabled
+ * @return bool
+ */
+ public function execute(string $sourceCode, bool $enabled): bool
+ {
+ $connection = $this->resourceConnection->getConnection();
+ $sourceTable = $this->resourceConnection->getTableName(Source::TABLE_NAME_SOURCE);
+ $sourceItemTable = $this->resourceConnection->getTableName(SourceItem::TABLE_NAME_SOURCE_ITEM);
+ $stockSourceLinkTable = $this->resourceConnection->getTableName(StockSourceLink::TABLE_NAME_STOCK_SOURCE_LINK);
+
+ $select = $connection->select()
+ ->from(
+ ['sources' => $sourceTable],
+ '(sources.' . SourceInterface::ENABLED . ' != ' . (int)$enabled . ')'
+ )
+ ->joinInner(
+ ['source_item' => $sourceItemTable],
+ 'sources.' . SourceInterface::SOURCE_CODE . '=' . 'source_item.' . SourceInterface::SOURCE_CODE,
+ null
+ )->joinInner(
+ ['stock_source_link' => $stockSourceLinkTable],
+ 'sources.' . SourceInterface::SOURCE_CODE . '=' . 'stock_source_link.' . SourceInterface::SOURCE_CODE,
+ null
+ )
+ ->where('sources.' . SourceInterface::SOURCE_CODE . ' = ?', $sourceCode);
+
+ return (bool)$connection->fetchOne($select);
+ }
+}
diff --git a/app/code/Magento/InventoryIndexer/Plugin/InventoryApi/InvalidateAfterSourceSetEnabledPlugin.php b/app/code/Magento/InventoryIndexer/Plugin/InventoryApi/InvalidateAfterEnablingOrDisablingSourcePlugin.php
similarity index 70%
rename from app/code/Magento/InventoryIndexer/Plugin/InventoryApi/InvalidateAfterSourceSetEnabledPlugin.php
rename to app/code/Magento/InventoryIndexer/Plugin/InventoryApi/InvalidateAfterEnablingOrDisablingSourcePlugin.php
index 5d08724fc6e40..4d98ce7f1c2cb 100644
--- a/app/code/Magento/InventoryIndexer/Plugin/InventoryApi/InvalidateAfterSourceSetEnabledPlugin.php
+++ b/app/code/Magento/InventoryIndexer/Plugin/InventoryApi/InvalidateAfterEnablingOrDisablingSourcePlugin.php
@@ -11,12 +11,12 @@
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\InventoryApi\Api\SourceRepositoryInterface;
use Magento\InventoryIndexer\Indexer\InventoryIndexer;
-use Magento\InventoryIndexer\Model\ResourceModel\GetInvalidationRequired;
+use Magento\InventoryIndexer\Model\ResourceModel\IsInvalidationRequiredForSource;
/**
* Invalidate Inventory Indexer after Source was enabled or disabled.
*/
-class InvalidateAfterSourceSetEnabledPlugin
+class InvalidateAfterEnablingOrDisablingSourcePlugin
{
/**
* @var IndexerRegistry
@@ -24,20 +24,20 @@ class InvalidateAfterSourceSetEnabledPlugin
private $indexerRegistry;
/**
- * @var GetInvalidationRequired
+ * @var IsInvalidationRequiredForSource
*/
- private $getInvalidationRequired;
+ private $isInvalidationRequiredForSource;
/**
* @param IndexerRegistry $indexerRegistry
- * @param GetInvalidationRequired $getInvalidationRequired
+ * @param IsInvalidationRequiredForSource $isInvalidationRequiredForSource
*/
public function __construct(
IndexerRegistry $indexerRegistry,
- GetInvalidationRequired $getInvalidationRequired
+ IsInvalidationRequiredForSource $isInvalidationRequiredForSource
) {
$this->indexerRegistry = $indexerRegistry;
- $this->getInvalidationRequired = $getInvalidationRequired;
+ $this->isInvalidationRequiredForSource = $isInvalidationRequiredForSource;
}
/**
@@ -54,10 +54,13 @@ public function aroundSave(
callable $proceed,
SourceInterface $source
) {
- $sourceCode = $source->getSourceCode();
- $enabled = (int)$source->isEnabled();
- $invalidationRequired = $this->getInvalidationRequired->execute($sourceCode, $enabled);
+ $invalidationRequired = $this->isInvalidationRequiredForSource->execute(
+ $source->getSourceCode(),
+ $source->isEnabled()
+ );
+
$proceed($source);
+
if ($invalidationRequired) {
$indexer = $this->indexerRegistry->get(InventoryIndexer::INDEXER_ID);
if ($indexer->isValid()) {
diff --git a/app/code/Magento/InventoryIndexer/Plugin/InventoryApi/ReindexAfterStockSourceLinksDeletePlugin.php b/app/code/Magento/InventoryIndexer/Plugin/InventoryApi/InvalidateAfterStockSourceLinksDeletePlugin.php
similarity index 97%
rename from app/code/Magento/InventoryIndexer/Plugin/InventoryApi/ReindexAfterStockSourceLinksDeletePlugin.php
rename to app/code/Magento/InventoryIndexer/Plugin/InventoryApi/InvalidateAfterStockSourceLinksDeletePlugin.php
index 6b400269a5b83..8953352499871 100644
--- a/app/code/Magento/InventoryIndexer/Plugin/InventoryApi/ReindexAfterStockSourceLinksDeletePlugin.php
+++ b/app/code/Magento/InventoryIndexer/Plugin/InventoryApi/InvalidateAfterStockSourceLinksDeletePlugin.php
@@ -16,7 +16,7 @@
/**
* Invalidate InventoryIndexer
*/
-class ReindexAfterStockSourceLinksDeletePlugin
+class InvalidateAfterStockSourceLinksDeletePlugin
{
/**
* @var IndexerRegistry
diff --git a/app/code/Magento/InventoryIndexer/Plugin/InventoryApi/ReindexAfterStockSourceLinksSavePlugin.php b/app/code/Magento/InventoryIndexer/Plugin/InventoryApi/InvalidateAfterStockSourceLinksSavePlugin.php
similarity index 97%
rename from app/code/Magento/InventoryIndexer/Plugin/InventoryApi/ReindexAfterStockSourceLinksSavePlugin.php
rename to app/code/Magento/InventoryIndexer/Plugin/InventoryApi/InvalidateAfterStockSourceLinksSavePlugin.php
index e9073fb69a19e..80730d3d06ca9 100644
--- a/app/code/Magento/InventoryIndexer/Plugin/InventoryApi/ReindexAfterStockSourceLinksSavePlugin.php
+++ b/app/code/Magento/InventoryIndexer/Plugin/InventoryApi/InvalidateAfterStockSourceLinksSavePlugin.php
@@ -16,7 +16,7 @@
/**
* Invalidate InventoryIndexer
*/
-class ReindexAfterStockSourceLinksSavePlugin
+class InvalidateAfterStockSourceLinksSavePlugin
{
/**
* @var IndexerRegistry
diff --git a/app/code/Magento/InventoryIndexer/Test/Integration/Indexer/SourceIndexerTest.php b/app/code/Magento/InventoryIndexer/Test/Integration/Indexer/SourceIndexerTest.php
index 0fd12b3ccedff..58c8ca64f787b 100644
--- a/app/code/Magento/InventoryIndexer/Test/Integration/Indexer/SourceIndexerTest.php
+++ b/app/code/Magento/InventoryIndexer/Test/Integration/Indexer/SourceIndexerTest.php
@@ -8,7 +8,7 @@
namespace Magento\InventoryIndexer\Test\Integration\Indexer;
use Magento\InventoryIndexer\Indexer\Source\SourceIndexer;
-use Magento\InventoryIndexer\Model\GetStockItemData;
+use Magento\InventoryIndexer\Model\ResourceModel\GetStockItemData;
use Magento\InventorySales\Model\GetStockItemDataInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
diff --git a/app/code/Magento/InventoryIndexer/Test/Integration/Indexer/SourceItemIndexerTest.php b/app/code/Magento/InventoryIndexer/Test/Integration/Indexer/SourceItemIndexerTest.php
index 2f1ca9748c533..d85ae9550764e 100644
--- a/app/code/Magento/InventoryIndexer/Test/Integration/Indexer/SourceItemIndexerTest.php
+++ b/app/code/Magento/InventoryIndexer/Test/Integration/Indexer/SourceItemIndexerTest.php
@@ -12,7 +12,7 @@
use Magento\InventoryApi\Api\SourceItemRepositoryInterface;
use Magento\InventoryIndexer\Indexer\SourceItem\GetSourceItemIds;
use Magento\InventoryIndexer\Indexer\SourceItem\SourceItemIndexer;
-use Magento\InventoryIndexer\Model\GetStockItemData;
+use Magento\InventoryIndexer\Model\ResourceModel\GetStockItemData;
use Magento\InventorySales\Model\GetStockItemDataInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
diff --git a/app/code/Magento/InventoryIndexer/Test/Integration/Indexer/StockIndexerTest.php b/app/code/Magento/InventoryIndexer/Test/Integration/Indexer/StockIndexerTest.php
index 8f2896a577a7c..5f90baef29a1b 100644
--- a/app/code/Magento/InventoryIndexer/Test/Integration/Indexer/StockIndexerTest.php
+++ b/app/code/Magento/InventoryIndexer/Test/Integration/Indexer/StockIndexerTest.php
@@ -8,7 +8,7 @@
namespace Magento\InventoryIndexer\Test\Integration\Indexer;
use Magento\InventoryIndexer\Indexer\Stock\StockIndexer;
-use Magento\InventoryIndexer\Model\GetStockItemData;
+use Magento\InventoryIndexer\Model\ResourceModel\GetStockItemData;
use Magento\InventorySales\Model\GetStockItemDataInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
diff --git a/app/code/Magento/InventoryIndexer/Test/Integration/Plugin/InventoryApi/InvalidateAfterSourceSetEnabledPluginTest.php b/app/code/Magento/InventoryIndexer/Test/Integration/InvalidateAfterEnablingOrDisablingSourceTest.php
similarity index 75%
rename from app/code/Magento/InventoryIndexer/Test/Integration/Plugin/InventoryApi/InvalidateAfterSourceSetEnabledPluginTest.php
rename to app/code/Magento/InventoryIndexer/Test/Integration/InvalidateAfterEnablingOrDisablingSourceTest.php
index b0f460ea0c1db..1fb922e42062d 100644
--- a/app/code/Magento/InventoryIndexer/Test/Integration/Plugin/InventoryApi/InvalidateAfterSourceSetEnabledPluginTest.php
+++ b/app/code/Magento/InventoryIndexer/Test/Integration/InvalidateAfterEnablingOrDisablingSourceTest.php
@@ -5,8 +5,9 @@
*/
declare(strict_types=1);
-namespace Magento\InventoryIndexer\Test\Integration\Plugin\InventoryApi;
+namespace Magento\InventoryIndexer\Test\Integration;
+use Magento\Framework\Indexer\IndexerInterface;
use Magento\Framework\Indexer\IndexerRegistry;
use Magento\InventoryApi\Api\SourceRepositoryInterface;
use Magento\InventoryIndexer\Indexer\InventoryIndexer;
@@ -16,7 +17,7 @@
/**
* Tests Indexer invalidation after Source enabled or disabled.
*/
-class InvalidateAfterSourceSetEnabledPluginTest extends TestCase
+class InvalidateAfterEnablingOrDisablingSourceTest extends TestCase
{
/**
* @var SourceRepositoryInterface
@@ -24,20 +25,24 @@ class InvalidateAfterSourceSetEnabledPluginTest extends TestCase
private $sourceRepository;
/**
- * @var IndexerRegistry
+ * @var IndexerInterface
*/
- private $indexerRegistry;
+ private $indexer;
protected function setUp()
{
$this->sourceRepository = Bootstrap::getObjectManager()->get(SourceRepositoryInterface::class);
- $this->indexerRegistry = Bootstrap::getObjectManager()->get(IndexerRegistry::class);
+ /** @var IndexerRegistry $indexerRegistry */
+ $indexerRegistry = Bootstrap::getObjectManager()->get(IndexerRegistry::class);
+ $this->indexer = $indexerRegistry->get(InventoryIndexer::INDEXER_ID);
}
/**
* Tests Source enabling and disabling when no Stocks or Source Items are connected to current Source.
*
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
+ * @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
+ *
* @dataProvider IndexerInvalidationOnlySourcesDataProvider
* @param string $sourceCode
* @param bool $enable
@@ -45,7 +50,22 @@ protected function setUp()
*/
public function testIndexerInvalidationOnlySources(string $sourceCode, bool $enable, bool $expectedValid)
{
- $this->indexerInvalidationBase($sourceCode, $enable, $expectedValid);
+ $this->setSourceEnabledStatus($sourceCode, $enable);
+
+ $this->assertEquals($expectedValid, $this->indexer->isValid());
+ }
+
+ /**
+ * @return array
+ */
+ public function indexerInvalidationOnlySourcesDataProvider(): array
+ {
+ return [
+ ['eu-1', true, true],
+ ['eu-1', false, true],
+ ['eu-disabled', true, true],
+ ['eu-disabled', false, true],
+ ];
}
/**
@@ -54,6 +74,7 @@ public function testIndexerInvalidationOnlySources(string $sourceCode, bool $ena
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
+ * @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
*
* @dataProvider IndexerInvalidationNoStockLinksDataProvider
* @param string $sourceCode
@@ -62,74 +83,48 @@ public function testIndexerInvalidationOnlySources(string $sourceCode, bool $ena
*/
public function testIndexerInvalidationNoStockLinks(string $sourceCode, bool $enable, bool $expectedValid)
{
- $this->indexerInvalidationBase($sourceCode, $enable, $expectedValid);
+ $this->setSourceEnabledStatus($sourceCode, $enable);
+
+ $this->assertEquals($expectedValid, $this->indexer->isValid());
}
/**
- * Tests Source enabling and disabling when no Source Items are connected to current Source.
- *
- * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
- * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
- * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
- * @dataProvider IndexerInvalidationNoSourceItemsDataProvider
- * @param string $sourceCode
- * @param bool $enable
- * @param bool $expectedValid
+ * @return array
*/
- public function testIndexerInvalidationNoSourceItems(string $sourceCode, bool $enable, bool $expectedValid)
+ public function indexerInvalidationNoStockLinksDataProvider(): array
{
- $this->indexerInvalidationBase($sourceCode, $enable, $expectedValid);
+ return [
+ ['eu-1', true, true],
+ ['eu-1', false, true],
+ ['eu-disabled', true, true],
+ ['eu-disabled', false, true],
+ ];
}
/**
- * Tests Source enabling and disabling when both Stocks and Source Items are connected to current Source.
+ * Tests Source enabling and disabling when no Source Items are connected to current Source.
*
- * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
- * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
- * @dataProvider IndexerInvalidationFullDataProvider
- * @param string $sourceCode
- * @param bool $enable
- * @param bool $expectedValid
- */
- public function testIndexerInvalidationFull(string $sourceCode, bool $enable, bool $expectedValid)
- {
- $this->indexerInvalidationBase($sourceCode, $enable, $expectedValid);
- }
-
- /**
- * Contains generic logic for all Indexer invalidation after Source enabled or disabled tests.
+ * @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
*
+ * @dataProvider IndexerInvalidationNoSourceItemsDataProvider
* @param string $sourceCode
* @param bool $enable
* @param bool $expectedValid
*/
- private function indexerInvalidationBase(string $sourceCode, bool $enable, bool $expectedValid)
+ public function testIndexerInvalidationNoSourceItems(string $sourceCode, bool $enable, bool $expectedValid)
{
- $indexer = $this->indexerRegistry->get(InventoryIndexer::INDEXER_ID);
- $indexer->reindexAll();
-
- $this->assertTrue($indexer->isValid());
-
- $source = $this->sourceRepository->get($sourceCode);
- $source->setEnabled($enable);
- $this->sourceRepository->save($source);
- $actualValid = $indexer->isValid();
+ $this->setSourceEnabledStatus($sourceCode, $enable);
- $this->assertEquals(
- $expectedValid,
- $actualValid
- );
+ $this->assertEquals($expectedValid, $this->indexer->isValid());
}
/**
- * Data provider for testIndexerInvalidationOnlySources.
- *
* @return array
*/
- public function indexerInvalidationOnlySourcesDataProvider()
+ public function indexerInvalidationNoSourceItemsDataProvider(): array
{
return [
['eu-1', true, true],
@@ -140,47 +135,49 @@ public function indexerInvalidationOnlySourcesDataProvider()
}
/**
- * Data provider for testIndexerInvalidationNoStockLinks.
+ * Tests Source enabling and disabling when both Stocks and Source Items are connected to current Source.
*
- * @return array
+ * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
+ * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
+ * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
+ * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
+ * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
+ * @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
+ *
+ * @dataProvider IndexerInvalidationFullDataProvider
+ * @param string $sourceCode
+ * @param bool $enable
+ * @param bool $expectedValid
*/
- public function indexerInvalidationNoStockLinksDataProvider()
+ public function testIndexerInvalidationFull(string $sourceCode, bool $enable, bool $expectedValid)
{
- return [
- ['eu-1', true, true],
- ['eu-1', false, true],
- ['eu-disabled', true, true],
- ['eu-disabled', false, true],
- ];
+ $this->setSourceEnabledStatus($sourceCode, $enable);
+
+ $this->assertEquals($expectedValid, $this->indexer->isValid());
}
/**
- * Data provider for testIndexerInvalidationNoSourceItems.
- *
* @return array
*/
- public function indexerInvalidationNoSourceItemsDataProvider()
+ public function indexerInvalidationFullDataProvider(): array
{
return [
['eu-1', true, true],
- ['eu-1', false, true],
- ['eu-disabled', true, true],
+ ['eu-1', false, false],
+ ['eu-disabled', true, false],
['eu-disabled', false, true],
];
}
/**
- * Data provider for testIndexerInvalidationFull.
- *
- * @return array
+ * @param string $sourceCode
+ * @param bool $enable
+ * @return void
*/
- public function indexerInvalidationFullDataProvider()
+ private function setSourceEnabledStatus(string $sourceCode, bool $enable)
{
- return [
- ['eu-1', true, true],
- ['eu-1', false, false],
- ['eu-disabled', true, false],
- ['eu-disabled', false, true],
- ];
+ $source = $this->sourceRepository->get($sourceCode);
+ $source->setEnabled($enable);
+ $this->sourceRepository->save($source);
}
}
diff --git a/app/code/Magento/InventoryIndexer/etc/di.xml b/app/code/Magento/InventoryIndexer/etc/di.xml
index 9f57453ac2274..30aec2ce542af 100644
--- a/app/code/Magento/InventoryIndexer/etc/di.xml
+++ b/app/code/Magento/InventoryIndexer/etc/di.xml
@@ -30,24 +30,26 @@
2000
+
+
-
+
-
+
-
+
-
-
+
+
From 56e63a91b725f4a21d3f14dbbc490cfcef0568d4 Mon Sep 17 00:00:00 2001
From: Valeriy Nayda
Date: Wed, 4 Apr 2018 17:52:36 +0300
Subject: [PATCH 07/10] MSI-757: Add Inventory Index invalidation when Source
is enabled or disabled
---
...dateAfterEnablingOrDisablingSourceTest.php | 95 ++++---------------
1 file changed, 16 insertions(+), 79 deletions(-)
diff --git a/app/code/Magento/InventoryIndexer/Test/Integration/InvalidateAfterEnablingOrDisablingSourceTest.php b/app/code/Magento/InventoryIndexer/Test/Integration/InvalidateAfterEnablingOrDisablingSourceTest.php
index 1fb922e42062d..23041e47cfb92 100644
--- a/app/code/Magento/InventoryIndexer/Test/Integration/InvalidateAfterEnablingOrDisablingSourceTest.php
+++ b/app/code/Magento/InventoryIndexer/Test/Integration/InvalidateAfterEnablingOrDisablingSourceTest.php
@@ -38,50 +38,21 @@ protected function setUp()
}
/**
- * Tests Source enabling and disabling when no Stocks or Source Items are connected to current Source.
- *
- * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
- * @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
- *
- * @dataProvider IndexerInvalidationOnlySourcesDataProvider
- * @param string $sourceCode
- * @param bool $enable
- * @param bool $expectedValid
- */
- public function testIndexerInvalidationOnlySources(string $sourceCode, bool $enable, bool $expectedValid)
- {
- $this->setSourceEnabledStatus($sourceCode, $enable);
-
- $this->assertEquals($expectedValid, $this->indexer->isValid());
- }
-
- /**
- * @return array
- */
- public function indexerInvalidationOnlySourcesDataProvider(): array
- {
- return [
- ['eu-1', true, true],
- ['eu-1', false, true],
- ['eu-disabled', true, true],
- ['eu-disabled', false, true],
- ];
- }
-
- /**
- * Tests Source enabling and disabling when no Stocks are connected to current Source.
+ * Tests Source enabling and disabling when both Stocks and Source Items are connected to current Source.
*
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
+ * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
+ * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
*
- * @dataProvider IndexerInvalidationNoStockLinksDataProvider
+ * @dataProvider indexerInvalidationDataProvider
* @param string $sourceCode
* @param bool $enable
* @param bool $expectedValid
*/
- public function testIndexerInvalidationNoStockLinks(string $sourceCode, bool $enable, bool $expectedValid)
+ public function testIndexerInvalidation(string $sourceCode, bool $enable, bool $expectedValid)
{
$this->setSourceEnabledStatus($sourceCode, $enable);
@@ -91,31 +62,32 @@ public function testIndexerInvalidationNoStockLinks(string $sourceCode, bool $en
/**
* @return array
*/
- public function indexerInvalidationNoStockLinksDataProvider(): array
+ public function indexerInvalidationDataProvider(): array
{
return [
['eu-1', true, true],
- ['eu-1', false, true],
- ['eu-disabled', true, true],
+ ['eu-1', false, false],
+ ['eu-disabled', true, false],
['eu-disabled', false, true],
];
}
/**
- * Tests Source enabling and disabling when no Source Items are connected to current Source.
+ * Tests Source enabling and disabling when no Stocks or Source Items are connected to Source.
*
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
- * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
- * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
*
- * @dataProvider IndexerInvalidationNoSourceItemsDataProvider
+ * @dataProvider sourceHasNotAnyRelationsDataProvider
* @param string $sourceCode
* @param bool $enable
* @param bool $expectedValid
*/
- public function testIndexerInvalidationNoSourceItems(string $sourceCode, bool $enable, bool $expectedValid)
- {
+ public function testIndexerInvalidationIfSourceHasNotAnyRelations(
+ string $sourceCode,
+ bool $enable,
+ bool $expectedValid
+ ) {
$this->setSourceEnabledStatus($sourceCode, $enable);
$this->assertEquals($expectedValid, $this->indexer->isValid());
@@ -124,7 +96,7 @@ public function testIndexerInvalidationNoSourceItems(string $sourceCode, bool $e
/**
* @return array
*/
- public function indexerInvalidationNoSourceItemsDataProvider(): array
+ public function sourceHasNotAnyRelationsDataProvider(): array
{
return [
['eu-1', true, true],
@@ -134,41 +106,6 @@ public function indexerInvalidationNoSourceItemsDataProvider(): array
];
}
- /**
- * Tests Source enabling and disabling when both Stocks and Source Items are connected to current Source.
- *
- * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
- * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
- * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
- * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
- * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
- * @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
- *
- * @dataProvider IndexerInvalidationFullDataProvider
- * @param string $sourceCode
- * @param bool $enable
- * @param bool $expectedValid
- */
- public function testIndexerInvalidationFull(string $sourceCode, bool $enable, bool $expectedValid)
- {
- $this->setSourceEnabledStatus($sourceCode, $enable);
-
- $this->assertEquals($expectedValid, $this->indexer->isValid());
- }
-
- /**
- * @return array
- */
- public function indexerInvalidationFullDataProvider(): array
- {
- return [
- ['eu-1', true, true],
- ['eu-1', false, false],
- ['eu-disabled', true, false],
- ['eu-disabled', false, true],
- ];
- }
-
/**
* @param string $sourceCode
* @param bool $enable
From f7161c93312f34387acb1ac4f4a647cbbf7662ba Mon Sep 17 00:00:00 2001
From: Valeriy Nayda
Date: Wed, 4 Apr 2018 17:55:19 +0300
Subject: [PATCH 08/10] MSI-757: Add Inventory Index invalidation when Source
is enabled or disabled
---
.../InvalidateAfterEnablingOrDisablingSourceTest.php | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/app/code/Magento/InventoryIndexer/Test/Integration/InvalidateAfterEnablingOrDisablingSourceTest.php b/app/code/Magento/InventoryIndexer/Test/Integration/InvalidateAfterEnablingOrDisablingSourceTest.php
index 23041e47cfb92..76b37a4f63dc5 100644
--- a/app/code/Magento/InventoryIndexer/Test/Integration/InvalidateAfterEnablingOrDisablingSourceTest.php
+++ b/app/code/Magento/InventoryIndexer/Test/Integration/InvalidateAfterEnablingOrDisablingSourceTest.php
@@ -83,7 +83,7 @@ public function indexerInvalidationDataProvider(): array
* @param bool $enable
* @param bool $expectedValid
*/
- public function testIndexerInvalidationIfSourceHasNotAnyRelations(
+ public function testIndexerInvalidationIfSourceDoesNotHaveAnyRelations(
string $sourceCode,
bool $enable,
bool $expectedValid
@@ -96,7 +96,7 @@ public function testIndexerInvalidationIfSourceHasNotAnyRelations(
/**
* @return array
*/
- public function sourceHasNotAnyRelationsDataProvider(): array
+ public function sourceDoesNotHaveAnyRelationsDataProvider(): array
{
return [
['eu-1', true, true],
From f06aeedaedb317e20d7441453c49668b2a231e46 Mon Sep 17 00:00:00 2001
From: Valeriy Nayda
Date: Wed, 4 Apr 2018 17:56:54 +0300
Subject: [PATCH 09/10] MSI-757: Add Inventory Index invalidation when Source
is enabled or disabled
---
.../InvalidateAfterEnablingOrDisablingSourceTest.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/app/code/Magento/InventoryIndexer/Test/Integration/InvalidateAfterEnablingOrDisablingSourceTest.php b/app/code/Magento/InventoryIndexer/Test/Integration/InvalidateAfterEnablingOrDisablingSourceTest.php
index 76b37a4f63dc5..0501464323066 100644
--- a/app/code/Magento/InventoryIndexer/Test/Integration/InvalidateAfterEnablingOrDisablingSourceTest.php
+++ b/app/code/Magento/InventoryIndexer/Test/Integration/InvalidateAfterEnablingOrDisablingSourceTest.php
@@ -78,7 +78,7 @@ public function indexerInvalidationDataProvider(): array
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
*
- * @dataProvider sourceHasNotAnyRelationsDataProvider
+ * @dataProvider sourceDoesNotHaveAnyRelationsDataProvider
* @param string $sourceCode
* @param bool $enable
* @param bool $expectedValid
From e2374a666b31373c4dd21271583179fd95db42d7 Mon Sep 17 00:00:00 2001
From: Pavel Bystritsky
Date: Wed, 4 Apr 2018 18:55:36 +0300
Subject: [PATCH 10/10] MSI-757: Add Inventory Index invalidation when Source
is enabled or disabled.
---
...teAfterEnablingOrDisablingSourcePlugin.php | 2 +-
...dateAfterEnablingOrDisablingSourceTest.php | 50 ++++++++++++++++++-
.../Test/_files/reindex_inventory.php | 8 +--
3 files changed, 53 insertions(+), 7 deletions(-)
diff --git a/app/code/Magento/InventoryIndexer/Plugin/InventoryApi/InvalidateAfterEnablingOrDisablingSourcePlugin.php b/app/code/Magento/InventoryIndexer/Plugin/InventoryApi/InvalidateAfterEnablingOrDisablingSourcePlugin.php
index 4d98ce7f1c2cb..2536df43b9740 100644
--- a/app/code/Magento/InventoryIndexer/Plugin/InventoryApi/InvalidateAfterEnablingOrDisablingSourcePlugin.php
+++ b/app/code/Magento/InventoryIndexer/Plugin/InventoryApi/InvalidateAfterEnablingOrDisablingSourcePlugin.php
@@ -56,7 +56,7 @@ public function aroundSave(
) {
$invalidationRequired = $this->isInvalidationRequiredForSource->execute(
$source->getSourceCode(),
- $source->isEnabled()
+ (bool)$source->isEnabled()
);
$proceed($source);
diff --git a/app/code/Magento/InventoryIndexer/Test/Integration/InvalidateAfterEnablingOrDisablingSourceTest.php b/app/code/Magento/InventoryIndexer/Test/Integration/InvalidateAfterEnablingOrDisablingSourceTest.php
index 0501464323066..8b70f398a3a4b 100644
--- a/app/code/Magento/InventoryIndexer/Test/Integration/InvalidateAfterEnablingOrDisablingSourceTest.php
+++ b/app/code/Magento/InventoryIndexer/Test/Integration/InvalidateAfterEnablingOrDisablingSourceTest.php
@@ -78,7 +78,7 @@ public function indexerInvalidationDataProvider(): array
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
*
- * @dataProvider sourceDoesNotHaveAnyRelationsDataProvider
+ * @dataProvider sourceDoesNotHaveAllRelationsDataProvider
* @param string $sourceCode
* @param bool $enable
* @param bool $expectedValid
@@ -93,10 +93,56 @@ public function testIndexerInvalidationIfSourceDoesNotHaveAnyRelations(
$this->assertEquals($expectedValid, $this->indexer->isValid());
}
+ /**
+ * Tests Source enabling and disabling when no Stocks are connected to current Source.
+ *
+ * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
+ * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
+ * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
+ * @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
+ *
+ * @dataProvider sourceDoesNotHaveAllRelationsDataProvider
+ * @param string $sourceCode
+ * @param bool $enable
+ * @param bool $expectedValid
+ */
+ public function testIndexerInvalidationIfSourceDoesNotHaveStockLinks(
+ string $sourceCode,
+ bool $enable,
+ bool $expectedValid
+ ) {
+ $this->setSourceEnabledStatus($sourceCode, $enable);
+
+ $this->assertEquals($expectedValid, $this->indexer->isValid());
+ }
+
+ /**
+ * Tests Source enabling and disabling when no Source Items are connected to current Source.
+ *
+ * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
+ * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
+ * @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
+ * @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
+ *
+ * @dataProvider sourceDoesNotHaveAllRelationsDataProvider
+ * @param string $sourceCode
+ * @param bool $enable
+ * @param bool $expectedValid
+ */
+ public function testIndexerInvalidationIfSourceDoesNotHaveSourceItems(
+ string $sourceCode,
+ bool $enable,
+ bool $expectedValid
+ ) {
+ $this->setSourceEnabledStatus($sourceCode, $enable);
+
+ $this->assertEquals($expectedValid, $this->indexer->isValid());
+ }
+
/**
* @return array
*/
- public function sourceDoesNotHaveAnyRelationsDataProvider(): array
+ public function sourceDoesNotHaveAllRelationsDataProvider(): array
{
return [
['eu-1', true, true],
diff --git a/app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php b/app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
index 6ebcdc0e8287b..69b81d5adf1d6 100644
--- a/app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
+++ b/app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
@@ -5,11 +5,11 @@
*/
declare(strict_types=1);
-use Magento\Framework\Indexer\IndexerInterface;
+use Magento\Framework\Indexer\IndexerRegistry;
use Magento\InventoryIndexer\Indexer\InventoryIndexer;
use Magento\TestFramework\Helper\Bootstrap;
-/** @var IndexerInterface $indexer */
-$indexer = Bootstrap::getObjectManager()->create(IndexerInterface::class);
-$indexer->load(InventoryIndexer::INDEXER_ID);
+/** @var IndexerRegistry $indexerRegistry */
+$indexerRegistry = Bootstrap::getObjectManager()->get(IndexerRegistry::class);
+$indexer = $indexerRegistry->get(InventoryIndexer::INDEXER_ID);
$indexer->reindexAll();