diff --git a/app/code/Magento/Catalog/Model/ProductRepository.php b/app/code/Magento/Catalog/Model/ProductRepository.php index ce6c2657836bb..3979a6566966a 100644 --- a/app/code/Magento/Catalog/Model/ProductRepository.php +++ b/app/code/Magento/Catalog/Model/ProductRepository.php @@ -30,7 +30,8 @@ use Magento\Framework\Exception\ValidatorException; /** - * Product Repository. + * @inheritdoc + * * @SuppressWarnings(PHPMD.CouplingBetweenObjects) * @SuppressWarnings(PHPMD.TooManyFields) */ @@ -737,6 +738,7 @@ private function getCollectionProcessor() { if (!$this->collectionProcessor) { $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get( + // phpstan:ignore "Class Magento\Catalog\Model\Api\SearchCriteria\ProductCollectionProcessor not found." \Magento\Catalog\Model\Api\SearchCriteria\ProductCollectionProcessor::class ); } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php index 0dc294e139d3e..792557f14a5bc 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php @@ -46,14 +46,24 @@ use PHPUnit_Framework_MockObject_MockObject as MockObject; /** - * Class ProductRepositoryTest - * @package Magento\Catalog\Test\Unit\Model + * Test for \Magento\Catalog\Model\ProductRepository. + * * @SuppressWarnings(PHPMD.TooManyFields) * @SuppressWarnings(PHPMD.CouplingBetweenObjects) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ class ProductRepositoryTest extends TestCase { + private const STUB_STORE_ID = 1; + + private const STUB_STORE_ID_GLOBAL = 0; + + private const STUB_PRODUCT_ID = 100; + + private const STUB_PRODUCT_NAME = 'name'; + + private const STUB_PRODUCT_SKU = 'sku'; + /** * @var Product|MockObject */ @@ -291,6 +301,7 @@ protected function setUp() ->disableOriginalConstructor() ->setMethods([]) ->getMockForAbstractClass(); + $storeMock->method('getId')->willReturn(self::STUB_STORE_ID); $storeMock->expects($this->any())->method('getWebsiteId')->willReturn('1'); $storeMock->expects($this->any())->method('getCode')->willReturn(Store::ADMIN_CODE); $this->storeManager->expects($this->any())->method('getStore')->willReturn($storeMock); @@ -345,6 +356,68 @@ function ($value) { $this->objectManager->setBackwardCompatibleProperty($this->model, 'mediaProcessor', $mediaProcessor); } + /** + * Test save product with global store id + * + * @param array $productData + * @return void + * @dataProvider getProductData + */ + public function testSaveForAllStoreViewScope(array $productData): void + { + $this->productFactory->method('create')->willReturn($this->product); + $this->product->method('getSku')->willReturn($productData['sku']); + $this->extensibleDataObjectConverter + ->expects($this->once()) + ->method('toNestedArray') + ->willReturn($productData); + $this->resourceModel->method('getIdBySku')->willReturn(self::STUB_PRODUCT_ID); + $this->resourceModel->expects($this->once())->method('validate')->willReturn(true); + $this->product->expects($this->at(14))->method('setData') + ->with('store_id', $productData['store_id']); + + $this->model->save($this->product); + } + + /** + * Product data provider + * + * @return array + */ + public function getProductData(): array + { + return [ + [ + [ + 'sku' => self::STUB_PRODUCT_SKU, + 'name' => self::STUB_PRODUCT_NAME, + 'store_id' => self::STUB_STORE_ID_GLOBAL, + ], + ], + ]; + } + + /** + * Test save product without store + * + * @return void + */ + public function testSaveWithoutStoreId(): void + { + $this->productFactory->method('create')->willReturn($this->product); + $this->product->method('getSku')->willReturn($this->productData['sku']); + $this->extensibleDataObjectConverter + ->expects($this->once()) + ->method('toNestedArray') + ->willReturn($this->productData); + $this->resourceModel->method('getIdBySku')->willReturn(self::STUB_PRODUCT_ID); + $this->resourceModel->expects($this->once())->method('validate')->willReturn(true); + $this->product->expects($this->at(15))->method('setData') + ->with('store_id', self::STUB_STORE_ID); + + $this->model->save($this->product); + } + /** * @expectedException \Magento\Framework\Exception\NoSuchEntityException * @expectedExceptionMessage The product that was requested doesn't exist. Verify the product and try again. diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Model/ProductRepositoryTest.php b/dev/tests/integration/testsuite/Magento/Catalog/Model/ProductRepositoryTest.php index a1b869dcf785d..a63d4c5f02d7e 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/Model/ProductRepositoryTest.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/Model/ProductRepositoryTest.php @@ -7,15 +7,12 @@ namespace Magento\Catalog\Model; -use Magento\Backend\Model\Auth; use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\Catalog\Model\ResourceModel\Product as ProductResource; use Magento\Framework\Api\SearchCriteriaBuilder; use Magento\Framework\Exception\LocalizedException; use Magento\TestFramework\Catalog\Model\ProductLayoutUpdateManager; use Magento\TestFramework\Helper\Bootstrap; -use Magento\TestFramework\Bootstrap as TestBootstrap; -use Magento\Framework\Acl\Builder; /** * Provide tests for ProductRepository model. @@ -26,6 +23,16 @@ */ class ProductRepositoryTest extends \PHPUnit\Framework\TestCase { + private const STUB_STORE_ID = 1; + + private const STUB_STORE_ID_GLOBAL = 0; + + private const STUB_PRODUCT_NAME = 'Simple Product'; + + private const STUB_UPDATED_PRODUCT_NAME = 'updated'; + + private const STUB_PRODUCT_SKU = 'simple'; + /** * Test subject. * @@ -235,22 +242,53 @@ public function testCustomLayout(): void } /** - * Tests product repository update should use provided store code. + * Tests product repository update * + * @dataProvider productUpdateDataProvider * @magentoDataFixture Magento/Catalog/_files/product_simple.php + * @param int $storeId + * @param int $checkStoreId + * @param string $expectedNameStore + * @param string $expectedNameCheckedStore */ - public function testProductUpdate(): void - { - $sku = 'simple'; - $nameUpdated = 'updated'; + public function testProductUpdate( + int $storeId, + int $checkStoreId, + string $expectedNameStore, + string $expectedNameCheckedStore + ): void { + $sku = self::STUB_PRODUCT_SKU; - $product = $this->productRepository->get($sku, false, 0); - $product->setName($nameUpdated); + $product = $this->productRepository->get($sku, false, $storeId); + $product->setName(self::STUB_UPDATED_PRODUCT_NAME); $this->productRepository->save($product); - $product = $this->productRepository->get($sku, false, 0); - self::assertEquals( - $nameUpdated, - $product->getName() - ); + $productNameStoreId = $this->productRepository->get($sku, false, $storeId)->getName(); + $productNameCheckedStoreId = $this->productRepository->get($sku, false, $checkStoreId)->getName(); + + $this->assertEquals($expectedNameStore, $productNameStoreId); + $this->assertEquals($expectedNameCheckedStore, $productNameCheckedStoreId); + } + + /** + * Product update data provider + * + * @return array + */ + public function productUpdateDataProvider(): array + { + return [ + 'Updating for global store' => [ + self::STUB_STORE_ID_GLOBAL, + self::STUB_STORE_ID, + self::STUB_UPDATED_PRODUCT_NAME, + self::STUB_UPDATED_PRODUCT_NAME, + ], + 'Updating for store' => [ + self::STUB_STORE_ID, + self::STUB_STORE_ID_GLOBAL, + self::STUB_UPDATED_PRODUCT_NAME, + self::STUB_PRODUCT_NAME, + ], + ]; } }