Skip to content

Commit d4e8a0a

Browse files
committed
Merge remote-tracking branch 'tango/MC-36835' into MC-37006
2 parents 9bc9bd7 + 424a131 commit d4e8a0a

File tree

2 files changed

+82
-49
lines changed

2 files changed

+82
-49
lines changed

app/code/Magento/Catalog/Model/Product.php

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Catalog\Api\Data\ProductInterface;
1111
use Magento\Catalog\Api\ProductLinkRepositoryInterface;
1212
use Magento\Catalog\Model\Product\Attribute\Backend\Media\EntryConverterPool;
13+
use Magento\Catalog\Model\Product\Attribute\Source\Status;
1314
use Magento\Catalog\Model\Product\Configuration\Item\Option\OptionInterface;
1415
use Magento\Framework\Api\AttributeValueFactory;
1516
use Magento\Framework\App\Filesystem\DirectoryList;
@@ -202,7 +203,7 @@ class Product extends \Magento\Catalog\Model\AbstractModel implements
202203
/**
203204
* Catalog product status
204205
*
205-
* @var \Magento\Catalog\Model\Product\Attribute\Source\Status
206+
* @var Status
206207
*/
207208
protected $_catalogProductStatus;
208209

@@ -408,7 +409,7 @@ public function __construct(
408409
\Magento\CatalogInventory\Api\Data\StockItemInterfaceFactory $stockItemFactory,
409410
\Magento\Catalog\Model\Product\OptionFactory $catalogProductOptionFactory,
410411
\Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
411-
\Magento\Catalog\Model\Product\Attribute\Source\Status $catalogProductStatus,
412+
Status $catalogProductStatus,
412413
\Magento\Catalog\Model\Product\Media\Config $catalogProductMediaConfig,
413414
Product\Type $catalogProductType,
414415
\Magento\Framework\Module\Manager $moduleManager,
@@ -668,7 +669,7 @@ public function getTypeId()
668669
public function getStatus()
669670
{
670671
$status = $this->_getData(self::STATUS);
671-
return $status !== null ? $status : \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED;
672+
return $status !== null ? $status : Status::STATUS_ENABLED;
672673
}
673674

674675
/**
@@ -1033,7 +1034,7 @@ public function priceReindexCallback()
10331034
*/
10341035
public function eavReindexCallback()
10351036
{
1036-
if ($this->isObjectNew() || $this->isDataChanged($this)) {
1037+
if ($this->isObjectNew() || $this->isDataChanged()) {
10371038
$this->_productEavIndexerProcessor->reindexRow($this->getEntityId());
10381039
}
10391040
}
@@ -1103,7 +1104,7 @@ public function afterDeleteCommit()
11031104
protected function _afterLoad()
11041105
{
11051106
if (!$this->hasData(self::STATUS)) {
1106-
$this->setData(self::STATUS, \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
1107+
$this->setData(self::STATUS, Status::STATUS_ENABLED);
11071108
}
11081109
parent::_afterLoad();
11091110
return $this;
@@ -1179,7 +1180,7 @@ public function getTierPrice($qty = null)
11791180
/**
11801181
* Get formatted by currency product price
11811182
*
1182-
* @return array|double
1183+
* @return array|double
11831184
* @since 102.0.6
11841185
*/
11851186
public function getFormattedPrice()
@@ -1780,7 +1781,7 @@ public function isSaleable()
17801781
*/
17811782
public function isInStock()
17821783
{
1783-
return $this->getStatus() == \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED;
1784+
return $this->getStatus() == Status::STATUS_ENABLED;
17841785
}
17851786

17861787
/**
@@ -2341,7 +2342,7 @@ public function getProductEntitiesInfo($columns = null)
23412342
*/
23422343
public function isDisabled()
23432344
{
2344-
return $this->getStatus() == \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED;
2345+
return $this->getStatus() == Status::STATUS_DISABLED;
23452346
}
23462347

23472348
/**
@@ -2365,17 +2366,21 @@ public function getImage()
23652366
public function getIdentities()
23662367
{
23672368
$identities = [self::CACHE_TAG . '_' . $this->getId()];
2368-
if ($this->getIsChangedCategories()) {
2369-
foreach ($this->getAffectedCategoryIds() as $categoryId) {
2370-
$identities[] = self::CACHE_PRODUCT_CATEGORY_TAG . '_' . $categoryId;
2369+
2370+
if (!$this->isObjectNew() || $this->getStatus() == Status::STATUS_ENABLED) {
2371+
if ($this->getIsChangedCategories()) {
2372+
foreach ($this->getAffectedCategoryIds() as $categoryId) {
2373+
$identities[] = self::CACHE_PRODUCT_CATEGORY_TAG . '_' . $categoryId;
2374+
}
23712375
}
2372-
}
23732376

2374-
if (($this->getOrigData('status') != $this->getData('status')) || $this->isStockStatusChanged()) {
2375-
foreach ($this->getCategoryIds() as $categoryId) {
2376-
$identities[] = self::CACHE_PRODUCT_CATEGORY_TAG . '_' . $categoryId;
2377+
if (($this->getOrigData('status') != $this->getData('status')) || $this->isStockStatusChanged()) {
2378+
foreach ($this->getCategoryIds() as $categoryId) {
2379+
$identities[] = self::CACHE_PRODUCT_CATEGORY_TAG . '_' . $categoryId;
2380+
}
23772381
}
23782382
}
2383+
23792384
if ($this->_appState->getAreaCode() == \Magento\Framework\App\Area::AREA_FRONTEND) {
23802385
$identities[] = self::CACHE_TAG;
23812386
}

app/code/Magento/Catalog/Test/Unit/Model/ProductTest.php

Lines changed: 62 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,7 @@ protected function setUp(): void
313313

314314
$contextMock = $this->createPartialMock(
315315
Context::class,
316-
['getEventDispatcher', 'getCacheManager', 'getAppState', 'getActionValidator'],
317-
[],
318-
'',
319-
false
316+
['getEventDispatcher', 'getCacheManager', 'getAppState', 'getActionValidator']
320317
);
321318
$contextMock->expects($this->any())->method('getAppState')->willReturn($this->appStateMock);
322319
$contextMock->expects($this->any())
@@ -541,7 +538,7 @@ public function testGetStoreSingleSiteModelIds(
541538
/**
542539
* @return array
543540
*/
544-
public function getSingleStoreIds()
541+
public function getSingleStoreIds(): array
545542
{
546543
return [
547544
[
@@ -619,15 +616,15 @@ public function testGetCategoryCollectionCollectionNull($initCategoryCollection,
619616

620617
$result = $product->getCategoryCollection();
621618

622-
$productIdCachedActual = $this->getPropertyValue($product, '_productIdCached', $productIdCached);
619+
$productIdCachedActual = $this->getPropertyValue($product, '_productIdCached');
623620
$this->assertEquals($getIdResult, $productIdCachedActual);
624621
$this->assertEquals($initCategoryCollection, $result);
625622
}
626623

627624
/**
628625
* @return array
629626
*/
630-
public function getCategoryCollectionCollectionNullDataProvider()
627+
public function getCategoryCollectionCollectionNullDataProvider(): array
631628
{
632629
return [
633630
[
@@ -742,7 +739,7 @@ public function testReindex($productChanged, $isScheduled, $productFlatCount, $c
742739
/**
743740
* @return array
744741
*/
745-
public function getProductReindexProvider()
742+
public function getProductReindexProvider(): array
746743
{
747744
return [
748745
'set 1' => [true, false, 1, 1],
@@ -774,12 +771,18 @@ public function testPriceReindexCallback()
774771
/**
775772
* @dataProvider getIdentitiesProvider
776773
* @param array $expected
777-
* @param array $origData
774+
* @param array|null $origData
778775
* @param array $data
779776
* @param bool $isDeleted
780-
*/
781-
public function testGetIdentities($expected, $origData, $data, $isDeleted = false)
782-
{
777+
* @param bool $isNew
778+
*/
779+
public function testGetIdentities(
780+
array $expected,
781+
?array $origData,
782+
array $data,
783+
bool $isDeleted = false,
784+
bool $isNew = false
785+
) {
783786
$this->model->setIdFieldName('id');
784787
if (is_array($origData)) {
785788
foreach ($origData as $key => $value) {
@@ -790,13 +793,14 @@ public function testGetIdentities($expected, $origData, $data, $isDeleted = fals
790793
$this->model->setData($key, $value);
791794
}
792795
$this->model->isDeleted($isDeleted);
796+
$this->model->isObjectNew($isNew);
793797
$this->assertEquals($expected, $this->model->getIdentities());
794798
}
795799

796800
/**
797801
* @return array
798802
*/
799-
public function getIdentitiesProvider()
803+
public function getIdentitiesProvider(): array
800804
{
801805
$extensionAttributesMock = $this->getMockBuilder(ExtensionAttributesInterface::class)
802806
->disableOriginalConstructor()
@@ -814,60 +818,61 @@ public function getIdentitiesProvider()
814818
['id' => 1, 'name' => 'value', 'category_ids' => [1]],
815819
],
816820
'new product' => $this->getNewProductProviderData(),
821+
'new disabled product' => $this->getNewDisabledProductProviderData(),
817822
'status and category change' => [
818823
[0 => 'cat_p_1', 1 => 'cat_c_p_1', 2 => 'cat_c_p_2'],
819-
['id' => 1, 'name' => 'value', 'category_ids' => [1], 'status' => 2],
824+
['id' => 1, 'name' => 'value', 'category_ids' => [1], 'status' => Status::STATUS_DISABLED],
820825
[
821826
'id' => 1,
822827
'name' => 'value',
823828
'category_ids' => [2],
824-
'status' => 1,
829+
'status' => Status::STATUS_ENABLED,
825830
'affected_category_ids' => [1, 2],
826831
'is_changed_categories' => true
827832
],
828833
],
829834
'status change only' => [
830835
[0 => 'cat_p_1', 1 => 'cat_c_p_7'],
831-
['id' => 1, 'name' => 'value', 'category_ids' => [7], 'status' => 1],
832-
['id' => 1, 'name' => 'value', 'category_ids' => [7], 'status' => 2],
836+
['id' => 1, 'name' => 'value', 'category_ids' => [7], 'status' => Status::STATUS_ENABLED],
837+
['id' => 1, 'name' => 'value', 'category_ids' => [7], 'status' => Status::STATUS_DISABLED],
833838
],
834839
'status changed, category unassigned' => $this->getStatusAndCategoryChangesData(),
835840
'no status changes' => [
836841
[0 => 'cat_p_1'],
837-
['id' => 1, 'name' => 'value', 'category_ids' => [1], 'status' => 1],
838-
['id' => 1, 'name' => 'value', 'category_ids' => [1], 'status' => 1],
842+
['id' => 1, 'name' => 'value', 'category_ids' => [1], 'status' => Status::STATUS_ENABLED],
843+
['id' => 1, 'name' => 'value', 'category_ids' => [1], 'status' => Status::STATUS_ENABLED],
839844
],
840845
'no stock status changes' => [
841846
[0 => 'cat_p_1'],
842-
['id' => 1, 'name' => 'value', 'category_ids' => [1], 'status' => 1],
847+
['id' => 1, 'name' => 'value', 'category_ids' => [1], 'status' => Status::STATUS_ENABLED],
843848
[
844849
'id' => 1,
845850
'name' => 'value',
846851
'category_ids' => [1],
847-
'status' => 1,
852+
'status' => Status::STATUS_ENABLED,
848853
'stock_data' => ['is_in_stock' => true],
849854
ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY => $extensionAttributesMock,
850855
],
851856
],
852857
'no stock status data 1' => [
853858
[0 => 'cat_p_1'],
854-
['id' => 1, 'name' => 'value', 'category_ids' => [1], 'status' => 1],
859+
['id' => 1, 'name' => 'value', 'category_ids' => [1], 'status' => Status::STATUS_ENABLED],
855860
[
856861
'id' => 1,
857862
'name' => 'value',
858863
'category_ids' => [1],
859-
'status' => 1,
864+
'status' => Status::STATUS_ENABLED,
860865
ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY => $extensionAttributesMock,
861866
],
862867
],
863868
'no stock status data 2' => [
864869
[0 => 'cat_p_1'],
865-
['id' => 1, 'name' => 'value', 'category_ids' => [1], 'status' => 1],
870+
['id' => 1, 'name' => 'value', 'category_ids' => [1], 'status' => Status::STATUS_ENABLED],
866871
[
867872
'id' => 1,
868873
'name' => 'value',
869874
'category_ids' => [1],
870-
'status' => 1,
875+
'status' => Status::STATUS_ENABLED,
871876
'stock_data' => ['is_in_stock' => true],
872877
],
873878
],
@@ -878,16 +883,16 @@ public function getIdentitiesProvider()
878883
/**
879884
* @return array
880885
*/
881-
private function getStatusAndCategoryChangesData()
886+
private function getStatusAndCategoryChangesData(): array
882887
{
883888
return [
884889
[0 => 'cat_p_1', 1 => 'cat_c_p_5'],
885-
['id' => 1, 'name' => 'value', 'category_ids' => [5], 'status' => 2],
890+
['id' => 1, 'name' => 'value', 'category_ids' => [5], 'status' => Status::STATUS_DISABLED],
886891
[
887892
'id' => 1,
888893
'name' => 'value',
889894
'category_ids' => [],
890-
'status' => 1,
895+
'status' => Status::STATUS_ENABLED,
891896
'is_changed_categories' => true,
892897
'affected_category_ids' => [5]
893898
],
@@ -897,7 +902,7 @@ private function getStatusAndCategoryChangesData()
897902
/**
898903
* @return array
899904
*/
900-
private function getNewProductProviderData()
905+
private function getNewProductProviderData(): array
901906
{
902907
return [
903908
['cat_p_1', 'cat_c_p_1'],
@@ -908,24 +913,47 @@ private function getNewProductProviderData()
908913
'category_ids' => [1],
909914
'affected_category_ids' => [1],
910915
'is_changed_categories' => true
911-
]
916+
],
917+
false,
918+
true,
919+
];
920+
}
921+
922+
/**
923+
* @return array
924+
*/
925+
private function getNewDisabledProductProviderData(): array
926+
{
927+
return [
928+
['cat_p_1'],
929+
null,
930+
[
931+
'id' => 1,
932+
'name' => 'value',
933+
'category_ids' => [1],
934+
'status' => Status::STATUS_DISABLED,
935+
'affected_category_ids' => [1],
936+
'is_changed_categories' => true
937+
],
938+
false,
939+
true,
912940
];
913941
}
914942

915943
/**
916944
* @param MockObject $extensionAttributesMock
917945
* @return array
918946
*/
919-
private function getStatusStockProviderData($extensionAttributesMock)
947+
private function getStatusStockProviderData($extensionAttributesMock): array
920948
{
921949
return [
922950
[0 => 'cat_p_1', 1 => 'cat_c_p_1'],
923-
['id' => 1, 'name' => 'value', 'category_ids' => [1], 'status' => 1],
951+
['id' => 1, 'name' => 'value', 'category_ids' => [1], 'status' => Status::STATUS_ENABLED],
924952
[
925953
'id' => 1,
926954
'name' => 'value',
927955
'category_ids' => [1],
928-
'status' => 1,
956+
'status' => Status::STATUS_ENABLED,
929957
'stock_data' => ['is_in_stock' => false],
930958
ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY => $extensionAttributesMock,
931959
],
@@ -1440,7 +1468,7 @@ public function testGetCustomAttributes()
14401468
/**
14411469
* @return array
14421470
*/
1443-
public function priceDataProvider()
1471+
public function priceDataProvider(): array
14441472
{
14451473
return [
14461474
'receive empty array' => [[]],

0 commit comments

Comments
 (0)