diff --git a/app/code/Magento/Rule/Model/Condition/Product/AbstractProduct.php b/app/code/Magento/Rule/Model/Condition/Product/AbstractProduct.php index 2206dbef38640..7d40dbfe652f4 100644 --- a/app/code/Magento/Rule/Model/Condition/Product/AbstractProduct.php +++ b/app/code/Magento/Rule/Model/Condition/Product/AbstractProduct.php @@ -12,6 +12,7 @@ /** * Abstract Rule product condition data model * + * phpcs:disable Magento2.Classes.AbstractApi * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) * @SuppressWarnings(PHPMD.CouplingBetweenObjects) * @api @@ -661,19 +662,7 @@ public function validateByEntityId($productId) */ protected function _getAvailableInCategories($productId) { - return $this->_productResource->getConnection() - ->fetchCol( - $this->_productResource->getConnection() - ->select() - ->distinct() - ->from( - $this->_productResource->getTable('catalog_category_product'), - ['category_id'] - )->where( - 'product_id = ?', - $productId - ) - ); + return $this->productCategoryList->getCategoryIds($productId); } /** diff --git a/app/code/Magento/SalesRule/Test/Unit/Model/Rule/Condition/ProductTest.php b/app/code/Magento/SalesRule/Test/Unit/Model/Rule/Condition/ProductTest.php index b0d3a203977ef..22627717a47f2 100644 --- a/app/code/Magento/SalesRule/Test/Unit/Model/Rule/Condition/ProductTest.php +++ b/app/code/Magento/SalesRule/Test/Unit/Model/Rule/Condition/ProductTest.php @@ -9,6 +9,7 @@ use Magento\Backend\Helper\Data; use Magento\Catalog\Api\ProductRepositoryInterface; +use Magento\Catalog\Model\ProductCategoryList; use Magento\Catalog\Model\ProductFactory; use Magento\Catalog\Model\ResourceModel\Product; use Magento\Directory\Model\CurrencyFactory; @@ -34,6 +35,7 @@ */ class ProductTest extends TestCase { + const STUB_CATEGORY_ID = 5; /** @var SalesRuleProduct */ protected $model; @@ -70,6 +72,9 @@ class ProductTest extends TestCase /** @var Select|MockObject */ protected $selectMock; + /** @var MockObject|ProductCategoryList */ + private $productCategoryListMock; + /** * Setup the test */ @@ -138,6 +143,10 @@ protected function setUp(): void $this->collectionMock = $this->getMockBuilder(Collection::class) ->disableOriginalConstructor() ->getMock(); + $this->productCategoryListMock = $this->getMockBuilder(ProductCategoryList::class) + ->disableOriginalConstructor() + ->onlyMethods(['getCategoryIds']) + ->getMock(); $this->format = new Format( $this->getMockBuilder(ScopeResolverInterface::class) ->disableOriginalConstructor() @@ -158,7 +167,9 @@ protected function setUp(): void $this->productRepositoryMock, $this->productMock, $this->collectionMock, - $this->format + $this->format, + [], + $this->productCategoryListMock ); } @@ -228,28 +239,22 @@ public function testValidateCategoriesIgnoresVisibility(): void ->setMethods(['getAttribute', 'getId', 'setQuoteItemQty', 'setQuoteItemPrice']) ->getMock(); $product - ->expects($this->any()) ->method('setQuoteItemQty') ->willReturnSelf(); $product - ->expects($this->any()) ->method('setQuoteItemPrice') ->willReturnSelf(); /* @var AbstractItem|MockObject $item */ $item = $this->getMockBuilder(AbstractItem::class) ->disableOriginalConstructor() - ->setMethods(['getProduct']) + ->onlyMethods(['getProduct']) ->getMockForAbstractClass(); $item->expects($this->any()) ->method('getProduct') ->willReturn($product); $this->model->setAttribute('category_ids'); - - $this->selectMock - ->expects($this->once()) - ->method('where') - ->with($this->logicalNot($this->stringContains('visibility')), $this->anything(), $this->anything()); - + $this->productCategoryListMock->method('getCategoryIds') + ->willReturn([self::STUB_CATEGORY_ID]); $this->model->validate($item); }