From a2564bc449d1aa736174da311de09a386c71e77e Mon Sep 17 00:00:00 2001 From: RomanKis Date: Fri, 10 Nov 2017 11:31:32 +0200 Subject: [PATCH] 12073: ProductRepository fails to load product with camelcase SKU --- .../Catalog/Model/ProductRepository.php | 5 +++ .../Test/Unit/Model/ProductRepositoryTest.php | 19 +++++++++ .../Catalog/Model/ProductRepositoryTest.php | 36 ++++++++++++++++ .../_files/product_simple_camel_case_sku.php | 42 +++++++++++++++++++ ...product_simple_camel_case_sku_rollback.php | 26 ++++++++++++ 5 files changed, 128 insertions(+) create mode 100644 dev/tests/integration/testsuite/Magento/Catalog/Model/ProductRepositoryTest.php create mode 100644 dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_camel_case_sku.php create mode 100644 dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_camel_case_sku_rollback.php diff --git a/app/code/Magento/Catalog/Model/ProductRepository.php b/app/code/Magento/Catalog/Model/ProductRepository.php index 4927a19146723..c6ab1afafaf3f 100644 --- a/app/code/Magento/Catalog/Model/ProductRepository.php +++ b/app/code/Magento/Catalog/Model/ProductRepository.php @@ -251,6 +251,11 @@ public function get($sku, $editMode = false, $storeId = null, $forceReload = fal if (!isset($this->instances[$sku])) { $sku = trim($sku); } + + if (!isset($this->instances[$sku])) { + throw new NoSuchEntityException(__('Requested product doesn\'t exist')); + } + return $this->instances[$sku][$cacheKey]; } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php index c98705b4eda63..8d72924a36359 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php @@ -527,6 +527,25 @@ public function testGetBySkuFromCacheInitializedInGetById() $this->assertEquals($this->productMock, $this->model->get($productSku)); } + /** + * @expectedException \Magento\Framework\Exception\NoSuchEntityException + */ + public function testGetBySkuWithCamelCaseException() + { + $productId = 22; + $fakeProductSku = 'testproduct'; + $realProductSku = 'testProduct'; + $this->productFactoryMock->expects($this->once())->method('create') + ->will($this->returnValue($this->productMock)); + $this->resourceModelMock->expects($this->once())->method('getIdBySku') + ->with($fakeProductSku)->willReturn($productId); + $this->productMock->expects($this->once())->method('load')->with($productId); + $this->productMock->expects($this->atLeastOnce())->method('getId')->willReturn($productId); + $this->productMock->expects($this->once())->method('getSku')->willReturn($realProductSku); + + $this->assertEquals($this->productMock, $this->model->get($fakeProductSku)); + } + public function testSaveExisting() { $this->storeManagerMock->expects($this->any())->method('getWebsites')->willReturn([1 => 'default']); diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Model/ProductRepositoryTest.php b/dev/tests/integration/testsuite/Magento/Catalog/Model/ProductRepositoryTest.php new file mode 100644 index 0000000000000..11add388af0ce --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Catalog/Model/ProductRepositoryTest.php @@ -0,0 +1,36 @@ +model = Bootstrap::getObjectManager()->create(ProductRepository::class); + } + + /** + * @magentoDataFixture Magento/Catalog/_files/product_simple_camel_case_sku.php + * @expectedException \Magento\Framework\Exception\NoSuchEntityException + */ + public function testGet() + { + $product = $this->model->get('camelcaseproduct'); + + $this->assertEquals('CamelCaseProduct', $product->getSku()); + } +} diff --git a/dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_camel_case_sku.php b/dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_camel_case_sku.php new file mode 100644 index 0000000000000..3424c71e570e3 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_camel_case_sku.php @@ -0,0 +1,42 @@ +reinitialize(); + +/** @var \Magento\TestFramework\ObjectManager $objectManager */ +$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + +/** @var $product \Magento\Catalog\Model\Product */ +$product = $objectManager->create(\Magento\Catalog\Model\Product::class); +$product->isObjectNew(true); +$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE) + ->setId(1) + ->setAttributeSetId(4) + ->setWebsiteIds([1]) + ->setName('Simple Product') + ->setSku('CamelCaseProduct') + ->setPrice(10) + ->setWeight(1) + ->setShortDescription("Short description") + ->setTaxClassId(0) + ->setDescription('Description with html tag') + ->setMetaTitle('meta title') + ->setMetaKeyword('meta keyword') + ->setMetaDescription('meta description') + ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH) + ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED) + ->setStockData( + [ + 'use_config_manage_stock' => 1, + 'qty' => 100, + 'is_qty_decimal' => 0, + 'is_in_stock' => 1, + ] + ); + +/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepositoryFactory */ +$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class); +$productRepository->save($product); diff --git a/dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_camel_case_sku_rollback.php b/dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_camel_case_sku_rollback.php new file mode 100644 index 0000000000000..80ec2b781c28f --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_camel_case_sku_rollback.php @@ -0,0 +1,26 @@ +getInstance()->reinitialize(); + +/** @var \Magento\Framework\Registry $registry */ +$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class); + +$registry->unregister('isSecureArea'); +$registry->register('isSecureArea', true); + +/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */ +$productRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() + ->get(\Magento\Catalog\Api\ProductRepositoryInterface::class); +try { + $product = $productRepository->get('CamelCaseProduct', false, null, true); + $productRepository->delete($product); +} catch (NoSuchEntityException $e) { +} +$registry->unregister('isSecureArea'); +$registry->register('isSecureArea', false);