diff --git a/app/code/Magento/Catalog/Model/ProductRepository.php b/app/code/Magento/Catalog/Model/ProductRepository.php index 4343917f250a0..e0d8dab0e80ae 100644 --- a/app/code/Magento/Catalog/Model/ProductRepository.php +++ b/app/code/Magento/Catalog/Model/ProductRepository.php @@ -285,7 +285,7 @@ public function get($sku, $editMode = false, $storeId = null, $forceReload = fal $productId = $this->resourceModel->getIdBySku($sku); if (!$productId) { throw new NoSuchEntityException( - __("The product that was requested doesn't exist. Verify the product and try again.") + __("The product with SKU \"%1\" does not exist.", $sku) ); } if ($editMode) { @@ -319,7 +319,7 @@ public function getById($productId, $editMode = false, $storeId = null, $forceRe $product->load($productId); if (!$product->getId()) { throw new NoSuchEntityException( - __("The product that was requested doesn't exist. Verify the product and try again.") + __("The product with ID \"%1\" does not exist.", $productId) ); } $this->cacheProduct($cacheKey, $product); diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ProductLink/ManagementTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ProductLink/ManagementTest.php index e98c129fa9884..907a0c533eb47 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ProductLink/ManagementTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ProductLink/ManagementTest.php @@ -24,11 +24,11 @@ */ class ManagementTest extends TestCase { - const STUB_PRODUCT_SKU_1 = 'Simple Product 1'; - const STUB_PRODUCT_SKU_2 = 'Simple Product 2'; - const STUB_PRODUCT_TYPE = 'simple'; - const STUB_LINK_TYPE = 'related'; - const STUB_BAD_TYPE = 'bad type'; + private const STUB_PRODUCT_SKU_1 = 'Simple Product 1'; + private const STUB_PRODUCT_SKU_2 = 'Simple Product 2'; + private const STUB_PRODUCT_TYPE = 'simple'; + private const STUB_LINK_TYPE = 'related'; + private const STUB_BAD_TYPE = 'bad type'; /** * @var Management @@ -289,13 +289,13 @@ public function testSetProductLinksNoProductException() ->method('get') ->willThrowException( new NoSuchEntityException( - __("The product that was requested doesn't exist. Verify the product and try again.") + __('The product with SKU "' . $productSku . '" does not exist.') ) ); $this->expectException(NoSuchEntityException::class); $this->expectExceptionMessage( - "The product that was requested doesn't exist. Verify the product and try again." + 'The product with SKU "' . $productSku . '" does not exist.' ); $this->model->setProductLinks($productSku, $links); diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php index e201f46287d67..7b9b4f11d1c07 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php @@ -440,7 +440,7 @@ public function testGetAbsentProduct(): void { $this->expectException('Magento\Framework\Exception\NoSuchEntityException'); $this->expectExceptionMessage( - 'The product that was requested doesn\'t exist. Verify the product and try again.' + 'The product with SKU "test_sku" does not exist.' ); $this->productFactory->expects($this->once())->method('create') ->willReturn($this->product); @@ -521,7 +521,7 @@ public function testGetByIdAbsentProduct(): void { $this->expectException('Magento\Framework\Exception\NoSuchEntityException'); $this->expectExceptionMessage( - 'The product that was requested doesn\'t exist. Verify the product and try again.' + 'The product with ID "product_id" does not exist.' ); $this->productFactory->expects($this->once())->method('create') ->willReturn($this->product); diff --git a/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductAttributeMediaGalleryManagementInterfaceTest.php b/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductAttributeMediaGalleryManagementInterfaceTest.php index 85a4f91fdb962..f98d9e3fdcf5a 100644 --- a/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductAttributeMediaGalleryManagementInterfaceTest.php +++ b/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductAttributeMediaGalleryManagementInterfaceTest.php @@ -492,18 +492,22 @@ public function testCreateThrowsExceptionIfProvidedImageHasWrongMimeType() $this->_webApiCall($this->createServiceInfo, ['sku' => 'simple', 'entry' => $requestData]); } + protected function assertProductNotFoundException(\Exception $e, string $sku): void + { + $decoded = json_decode($e->getMessage(), true); + $this->assertEquals('The product with SKU "%1" does not exist.', $decoded['message']); + $this->assertContains($sku, $decoded['parameters']); + $this->assertEquals(404, $e->getCode()); + } + /** * Test create method if target product does not exist - * */ public function testCreateThrowsExceptionIfTargetProductDoesNotExist() { - $this->expectException(\Exception::class); - $this->expectExceptionMessage( - 'The product that was requested doesn\'t exist. Verify the product and try again.' - ); + $sku = 'wrong_product_sku'; - $this->createServiceInfo['rest']['resourcePath'] = '/V1/products/wrong_product_sku/media'; + $this->createServiceInfo['rest']['resourcePath'] = '/V1/products/' . $sku . '/media'; $requestData = [ 'id' => null, @@ -519,7 +523,12 @@ public function testCreateThrowsExceptionIfTargetProductDoesNotExist() ] ]; - $this->_webApiCall($this->createServiceInfo, ['sku' => 'simple', 'entry' => $requestData]); + try { + $this->_webApiCall($this->createServiceInfo, ['sku' => $sku, 'entry' => $requestData]); + $this->fail('Expected exception was not thrown.'); + } catch (\Exception $e) { + $this->assertProductNotFoundException($e, $sku); + } } /** @@ -551,19 +560,14 @@ public function testCreateThrowsExceptionIfProvidedImageNameContainsForbiddenCha /** * Test update() method if target product does not exist - * */ public function testUpdateThrowsExceptionIfTargetProductDoesNotExist() { - $this->expectException(\Exception::class); - $this->expectExceptionMessage( - 'The product that was requested doesn\'t exist. Verify the product and try again.' - ); + $sku = 'wrong_product_sku'; - $this->updateServiceInfo['rest']['resourcePath'] = '/V1/products/wrong_product_sku/media' - . '/' . 'wrong-sku'; + $this->updateServiceInfo['rest']['resourcePath'] = '/V1/products/' . $sku . '/media/' . 'wrong-sku'; $requestData = [ - 'sku' => 'wrong_product_sku', + 'sku' => $sku, 'entry' => [ 'id' => 9999, 'media_type' => 'image', @@ -574,9 +578,15 @@ public function testUpdateThrowsExceptionIfTargetProductDoesNotExist() ], ]; - $this->_webApiCall($this->updateServiceInfo, $requestData, null, 'all'); + try { + $this->_webApiCall($this->updateServiceInfo, $requestData, null, 'all'); + $this->fail('Expected exception was not thrown.'); + } catch (\Exception $e) { + $this->assertProductNotFoundException($e, $sku); + } } + /** * Test update() method if there is no image with given id * @@ -607,22 +617,23 @@ public function testUpdateThrowsExceptionIfThereIsNoImageWithGivenId() /** * Test delete() method if target product does not exist - * */ public function testDeleteThrowsExceptionIfTargetProductDoesNotExist() { - $this->expectException(\Exception::class); - $this->expectExceptionMessage( - 'The product that was requested doesn\'t exist. Verify the product and try again.' - ); + $sku = 'wrong_product_sku'; - $this->deleteServiceInfo['rest']['resourcePath'] = '/V1/products/wrong_product_sku/media/9999'; + $this->deleteServiceInfo['rest']['resourcePath'] = '/V1/products/' . $sku . '/media/9999'; $requestData = [ - 'sku' => 'wrong_product_sku', + 'sku' => $sku, 'entryId' => 9999, ]; - $this->_webApiCall($this->deleteServiceInfo, $requestData); + try { + $this->_webApiCall($this->deleteServiceInfo, $requestData); + $this->fail('Expected exception was not thrown.'); + } catch (\Exception $e) { + $this->assertProductNotFoundException($e, $sku); + } } /** @@ -778,7 +789,7 @@ public function testGetListForAbsentSku() if (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) { $this->expectException('SoapFault'); $this->expectExceptionMessage( - "The product that was requested doesn't exist. Verify the product and try again." + "The product with SKU \"%1\" does not exist." ); } else { $this->expectException('Exception'); diff --git a/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductCustomOptionRepositoryTest.php b/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductCustomOptionRepositoryTest.php index 6d12a10775df3..922724a57ec5a 100644 --- a/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductCustomOptionRepositoryTest.php +++ b/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductCustomOptionRepositoryTest.php @@ -421,6 +421,7 @@ public function testUpdateNegative($optionData, $message, $exceptionCode) { $this->_markTestAsRestOnly(); $productSku = 'simple'; + /** @var ProductRepository $productRepository */ $productRepository = $this->objectManager->create(ProductRepository::class); $options = $productRepository->get($productSku, true)->getOptions(); @@ -434,10 +435,14 @@ public function testUpdateNegative($optionData, $message, $exceptionCode) ], ]; - $this->expectException('Exception'); - $this->expectExceptionMessage($message); - $this->expectExceptionCode($exceptionCode); - $this->_webApiCall($serviceInfo, ['option' => $optionData]); + try { + $this->_webApiCall($serviceInfo, ['option' => $optionData]); + $this->fail('Expected exception was not thrown.'); + } catch (\Exception $e) { + $this->assertEquals($exceptionCode, $e->getCode()); + $this->assertStringContainsString($message, $e->getMessage()); + $this->assertStringContainsString($productSku, $e->getMessage()); + } } /** diff --git a/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductRepositoryCategoryLinksTest.php b/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductRepositoryCategoryLinksTest.php new file mode 100644 index 0000000000000..400b5b1fa5290 --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductRepositoryCategoryLinksTest.php @@ -0,0 +1,104 @@ +getSimpleProductData(); + $productData[ProductInterface::EXTENSION_ATTRIBUTES_KEY] = [ + self::KEY_CATEGORY_LINKS => [['category_id' => 333, 'position' => 0]], + ]; + $response = $this->saveProduct($productData); + $this->assertEquals( + [['category_id' => 333, 'position' => 0]], + $response[ProductInterface::EXTENSION_ATTRIBUTES_KEY][self::KEY_CATEGORY_LINKS] + ); + $response = $this->getProduct($productData[ProductInterface::SKU]); + $this->assertArrayHasKey(ProductInterface::EXTENSION_ATTRIBUTES_KEY, $response); + $extensionAttributes = $response[ProductInterface::EXTENSION_ATTRIBUTES_KEY]; + $this->assertArrayHasKey(self::KEY_CATEGORY_LINKS, $extensionAttributes); + $this->assertEquals([['category_id' => 333, 'position' => 0]], $extensionAttributes[self::KEY_CATEGORY_LINKS]); + } + + /** + * Test update product category without categories + * + * @magentoApiDataFixture Magento/Catalog/_files/category_product.php + */ + public function testUpdateProductCategoryLinksNullOrNotExists() + { + $response = $this->getProduct('simple333'); + // update product without category_link or category_link is null + $response[ProductInterface::EXTENSION_ATTRIBUTES_KEY][self::KEY_CATEGORY_LINKS] = null; + $response = $this->updateProduct($response); + $this->assertEquals( + [['category_id' => 333, 'position' => 0]], + $response[ProductInterface::EXTENSION_ATTRIBUTES_KEY][self::KEY_CATEGORY_LINKS] + ); + unset($response[ProductInterface::EXTENSION_ATTRIBUTES_KEY][self::KEY_CATEGORY_LINKS]); + $response = $this->updateProduct($response); + $this->assertEquals( + [['category_id' => 333, 'position' => 0]], + $response[ProductInterface::EXTENSION_ATTRIBUTES_KEY][self::KEY_CATEGORY_LINKS] + ); + } + + /** + * Test update product category links position + * + * @magentoApiDataFixture Magento/Catalog/_files/category_product.php + */ + public function testUpdateProductCategoryLinksPosition() + { + $response = $this->getProduct('simple333'); + // update category_link position + $response[ProductInterface::EXTENSION_ATTRIBUTES_KEY][self::KEY_CATEGORY_LINKS] = [ + ['category_id' => 333, 'position' => 10], + ]; + $response = $this->updateProduct($response); + $this->assertEquals( + [['category_id' => 333, 'position' => 10]], + $response[ProductInterface::EXTENSION_ATTRIBUTES_KEY][self::KEY_CATEGORY_LINKS] + ); + } + + /** + * Test update product category links unassing + * + * @magentoApiDataFixture Magento/Catalog/_files/category_product.php + */ + public function testUpdateProductCategoryLinksUnassign() + { + $response = $this->getProduct('simple333'); + // unassign category_links from product + $response[ProductInterface::EXTENSION_ATTRIBUTES_KEY][self::KEY_CATEGORY_LINKS] = []; + $response = $this->updateProduct($response); + $this->assertArrayNotHasKey( + self::KEY_CATEGORY_LINKS, + $response[ProductInterface::EXTENSION_ATTRIBUTES_KEY] + ); + } +} diff --git a/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductRepositoryInterfaceTest.php b/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductRepositoryInterfaceTest.php index 4d82910557625..c7fa16fb01596 100644 --- a/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductRepositoryInterfaceTest.php +++ b/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductRepositoryInterfaceTest.php @@ -1,8 +1,7 @@ _webApiCall($serviceInfo, ['sku' => $invalidSku]); @@ -540,18 +537,23 @@ public function testCreateInvalidPriceFormat() * @dataProvider productCreationProvider * @magentoApiDataFixture Magento/Store/_files/fixture_store_with_catalogsearch_index.php */ - public function testDeleteAllStoreCode($fixtureProduct) + public function testDeleteAllStoreCode(array $fixtureProduct) { - $sku = $fixtureProduct[ProductInterface::SKU]; + $sku = $fixtureProduct[\Magento\Catalog\Api\Data\ProductInterface::SKU]; $this->saveProduct($fixtureProduct); - $this->expectException('Exception'); - $this->expectExceptionMessage( - "The product that was requested doesn't exist. Verify the product and try again." - ); // Delete all with 'all' store code $this->deleteProduct($sku); - $this->getProduct($sku); + + try { + $this->getProduct($sku); + $this->fail('Expected exception was not thrown.'); + } catch (\Exception $e) { + $expectedMessage = 'The product with SKU "%1" does not exist.'; + $this->assertEquals(404, $e->getCode()); + $this->assertStringContainsString($expectedMessage, $e->getMessage()); + $this->assertStringContainsString($sku, $e->getMessage()); + } } /** @@ -1662,86 +1664,6 @@ private function getStockItemData() ]; } - /** - * Test product category links - * - * @magentoApiDataFixture Magento/Catalog/_files/category_product.php - */ - public function testProductCategoryLinks() - { - // Create simple product - $productData = $this->getSimpleProductData(); - $productData[ProductInterface::EXTENSION_ATTRIBUTES_KEY] = [ - self::KEY_CATEGORY_LINKS => [['category_id' => 333, 'position' => 0]], - ]; - $response = $this->saveProduct($productData); - $this->assertEquals( - [['category_id' => 333, 'position' => 0]], - $response[ProductInterface::EXTENSION_ATTRIBUTES_KEY][self::KEY_CATEGORY_LINKS] - ); - $response = $this->getProduct($productData[ProductInterface::SKU]); - $this->assertArrayHasKey(ProductInterface::EXTENSION_ATTRIBUTES_KEY, $response); - $extensionAttributes = $response[ProductInterface::EXTENSION_ATTRIBUTES_KEY]; - $this->assertArrayHasKey(self::KEY_CATEGORY_LINKS, $extensionAttributes); - $this->assertEquals([['category_id' => 333, 'position' => 0]], $extensionAttributes[self::KEY_CATEGORY_LINKS]); - } - - /** - * Test update product category without categories - * - * @magentoApiDataFixture Magento/Catalog/_files/category_product.php - */ - public function testUpdateProductCategoryLinksNullOrNotExists() - { - $response = $this->getProduct('simple333'); - // update product without category_link or category_link is null - $response[ProductInterface::EXTENSION_ATTRIBUTES_KEY][self::KEY_CATEGORY_LINKS] = null; - $response = $this->updateProduct($response); - $this->assertEquals( - [['category_id' => 333, 'position' => 0]], - $response[ProductInterface::EXTENSION_ATTRIBUTES_KEY][self::KEY_CATEGORY_LINKS] - ); - unset($response[ProductInterface::EXTENSION_ATTRIBUTES_KEY][self::KEY_CATEGORY_LINKS]); - $response = $this->updateProduct($response); - $this->assertEquals( - [['category_id' => 333, 'position' => 0]], - $response[ProductInterface::EXTENSION_ATTRIBUTES_KEY][self::KEY_CATEGORY_LINKS] - ); - } - - /** - * Test update product category links position - * - * @magentoApiDataFixture Magento/Catalog/_files/category_product.php - */ - public function testUpdateProductCategoryLinksPosistion() - { - $response = $this->getProduct('simple333'); - // update category_link position - $response[ProductInterface::EXTENSION_ATTRIBUTES_KEY][self::KEY_CATEGORY_LINKS] = [ - ['category_id' => 333, 'position' => 10], - ]; - $response = $this->updateProduct($response); - $this->assertEquals( - [['category_id' => 333, 'position' => 10]], - $response[ProductInterface::EXTENSION_ATTRIBUTES_KEY][self::KEY_CATEGORY_LINKS] - ); - } - - /** - * Test update product category links unassing - * - * @magentoApiDataFixture Magento/Catalog/_files/category_product.php - */ - public function testUpdateProductCategoryLinksUnassign() - { - $response = $this->getProduct('simple333'); - // unassign category_links from product - $response[ProductInterface::EXTENSION_ATTRIBUTES_KEY][self::KEY_CATEGORY_LINKS] = []; - $response = $this->updateProduct($response); - $this->assertArrayNotHasKey(self::KEY_CATEGORY_LINKS, $response[ProductInterface::EXTENSION_ATTRIBUTES_KEY]); - } - /** * Get media gallery data * diff --git a/dev/tests/api-functional/testsuite/Magento/Catalog/Api/_files/product_options_update_negative.php b/dev/tests/api-functional/testsuite/Magento/Catalog/Api/_files/product_options_update_negative.php index b10957dcb7ed2..de20b158ba191 100644 --- a/dev/tests/api-functional/testsuite/Magento/Catalog/Api/_files/product_options_update_negative.php +++ b/dev/tests/api-functional/testsuite/Magento/Catalog/Api/_files/product_options_update_negative.php @@ -1,7 +1,7 @@ 'sku1', 'max_characters' => 10, ], - 'The product that was requested doesn\'t exist. Verify the product and try again.', + 'The product with SKU "%1" does not exist.', 404, ], ]; diff --git a/dev/tests/api-functional/testsuite/Magento/ConfigurableProduct/Api/OptionRepositoryTest.php b/dev/tests/api-functional/testsuite/Magento/ConfigurableProduct/Api/OptionRepositoryTest.php index 8c9d6f35075e0..d65a774482e97 100644 --- a/dev/tests/api-functional/testsuite/Magento/ConfigurableProduct/Api/OptionRepositoryTest.php +++ b/dev/tests/api-functional/testsuite/Magento/ConfigurableProduct/Api/OptionRepositoryTest.php @@ -1,8 +1,7 @@ getMessage(), true); + $this->assertEquals('The product with SKU "%1" does not exist.', $decoded['message']); + $this->assertContains($sku, $decoded['parameters']); + $this->assertEquals(404, $e->getCode()); + } + /** * @return void */ public function testGetUndefinedProduct(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage( - 'The product that was requested doesn\'t exist. Verify the product and try again.' - ); - $productSku = 'product_not_exist'; - $this->getList($productSku); + + try { + $this->getList($productSku); + $this->fail('Expected exception was not thrown.'); + } catch (\Exception $e) { + $this->assertProductNotFoundException($e, $productSku); + } } /** diff --git a/dev/tests/api-functional/testsuite/Magento/ConfigurableProduct/Api/ProductRepositoryTest.php b/dev/tests/api-functional/testsuite/Magento/ConfigurableProduct/Api/ProductRepositoryTest.php index e1fdf4a1a42f6..78318cebd166e 100644 --- a/dev/tests/api-functional/testsuite/Magento/ConfigurableProduct/Api/ProductRepositoryTest.php +++ b/dev/tests/api-functional/testsuite/Magento/ConfigurableProduct/Api/ProductRepositoryTest.php @@ -1,7 +1,7 @@ saveProduct($response); $this->fail("Expected exception"); @@ -461,7 +461,7 @@ public function testUpdateConfigurableProductLinksWithWithoutVariationAttributes $productId2, ]; - $expectedMessage = 'The product that was requested doesn\'t exist. Verify the product and try again.'; + $expectedMessage = "The product with ID \"%1\" does not exist."; try { $this->saveProduct($response); $this->fail("Expected exception"); diff --git a/dev/tests/api-functional/testsuite/Magento/Downloadable/Api/LinkRepositoryTest.php b/dev/tests/api-functional/testsuite/Magento/Downloadable/Api/LinkRepositoryTest.php index 3f7ccca9236da..1dcd69c047189 100644 --- a/dev/tests/api-functional/testsuite/Magento/Downloadable/Api/LinkRepositoryTest.php +++ b/dev/tests/api-functional/testsuite/Magento/Downloadable/Api/LinkRepositoryTest.php @@ -1,7 +1,7 @@ assertEquals($requestData['link']['number_of_downloads'], $link->getNumberOfDownloads()); } + protected function assertProductNotFoundException(\Exception $e, string $sku): void + { + $expectedTemplate = 'The product with SKU "%1" does not exist.'; + if ($e instanceof \SoapFault) { + $expectedMessage = str_replace('%1', $sku, $expectedTemplate); + $this->assertEquals($expectedMessage, $e->getMessage()); + } else { + $decoded = json_decode($e->getMessage(), true); + $this->assertEquals($expectedTemplate, $decoded['message']); + $this->assertContains($sku, $decoded['parameters']); + $this->assertEquals(404, $e->getCode()); + } + } + /** */ public function testUpdateThrowsExceptionIfTargetProductDoesNotExist() { - $this->expectException(\Exception::class); - $this->expectExceptionMessage( - 'The product that was requested doesn\'t exist. Verify the product and try again.' - ); + $sku = 'wrong-sku'; - $this->updateServiceInfo['rest']['resourcePath'] = '/V1/products/wrong-sku/downloadable-links/1'; + $this->updateServiceInfo['rest']['resourcePath'] = "/V1/products/{$sku}/downloadable-links/1"; $requestData = [ 'isGlobalScopeContent' => true, - 'sku' => 'wrong-sku', + 'sku' => $sku, 'link' => [ 'id' => 1, 'title' => 'Updated Title', @@ -769,7 +780,13 @@ public function testUpdateThrowsExceptionIfTargetProductDoesNotExist() 'sample_type' => 'url', ], ]; - $this->_webApiCall($this->updateServiceInfo, $requestData); + + try { + $this->_webApiCall($this->updateServiceInfo, $requestData); + $this->fail('Expected exception was not thrown.'); + } catch (\Exception $e) { + $this->assertProductNotFoundException($e, $sku); + } } /** @@ -1001,13 +1018,11 @@ public function testGetListForAbsentProduct($urlTail, $method) $requestData = ['sku' => $sku]; - $expectedMessage = "The product that was requested doesn't exist. Verify the product and try again."; try { $this->_webApiCall($serviceInfo, $requestData); - } catch (\SoapFault $e) { - $this->assertEquals($expectedMessage, $e->getMessage()); + $this->fail('Expected exception was not thrown.'); } catch (\Exception $e) { - $this->assertStringContainsString($expectedMessage, $e->getMessage()); + $this->assertProductNotFoundException($e, $sku); } } @@ -1070,15 +1085,12 @@ public function testCreateThrowsExceptionIfTargetProductTypeIsNotDownloadable() */ public function testCreateThrowsExceptionIfTargetProductDoesNotExist() { - $this->expectException(\Exception::class); - $this->expectExceptionMessage( - 'The product that was requested doesn\'t exist. Verify the product and try again.' - ); + $sku = 'wrong-sku'; - $this->createServiceInfo['rest']['resourcePath'] = '/V1/products/wrong-sku/downloadable-links'; + $this->createServiceInfo['rest']['resourcePath'] = "/V1/products/{$sku}/downloadable-links"; $requestData = [ 'isGlobalScopeContent' => false, - 'sku' => 'wrong-sku', + 'sku' => $sku, 'link' => [ 'title' => 'Link Title', 'sort_order' => 15, @@ -1091,6 +1103,12 @@ public function testCreateThrowsExceptionIfTargetProductDoesNotExist() 'link_url' => 'http://example.com/', ], ]; - $this->_webApiCall($this->createServiceInfo, $requestData); + + try { + $this->_webApiCall($this->createServiceInfo, $requestData); + $this->fail('Expected exception was not thrown.'); + } catch (\Exception $e) { + $this->assertProductNotFoundException($e, $sku); + } } } diff --git a/dev/tests/api-functional/testsuite/Magento/Downloadable/Api/SampleRepositoryTest.php b/dev/tests/api-functional/testsuite/Magento/Downloadable/Api/SampleRepositoryTest.php index 0c286719feeec..266778c4d99e5 100644 --- a/dev/tests/api-functional/testsuite/Magento/Downloadable/Api/SampleRepositoryTest.php +++ b/dev/tests/api-functional/testsuite/Magento/Downloadable/Api/SampleRepositoryTest.php @@ -1,7 +1,7 @@ _webApiCall($this->createServiceInfo, $requestData); } + protected function assertProductNotFoundException(\Exception $e, string $sku): void + { + $decoded = json_decode($e->getMessage(), true); + $this->assertEquals('The product with SKU "%1" does not exist.', $decoded['message']); + $this->assertContains($sku, $decoded['parameters']); + $this->assertEquals(404, $e->getCode()); + } + /** + * Test create() method on downloadable sample when target product does not exist */ public function testCreateThrowsExceptionIfTargetProductDoesNotExist() { - $this->expectException(\Exception::class); - $this->expectExceptionMessage( - 'The product that was requested doesn\'t exist. Verify the product and try again.' - ); + $sku = 'wrong-sku'; - $this->createServiceInfo['rest']['resourcePath'] = '/V1/products/wrong-sku/downloadable-links/samples'; + $this->createServiceInfo['rest']['resourcePath'] = '/V1/products/' . $sku . '/downloadable-links/samples'; $requestData = [ 'isGlobalScopeContent' => false, - 'sku' => 'wrong-sku', + 'sku' => $sku, 'sample' => [ 'title' => 'Title', 'sort_order' => 15, @@ -565,22 +571,26 @@ public function testCreateThrowsExceptionIfTargetProductDoesNotExist() 'sample_url' => 'http://example.com/', ], ]; - $this->_webApiCall($this->createServiceInfo, $requestData); + + try { + $this->_webApiCall($this->createServiceInfo, $requestData); + $this->fail('Expected exception was not thrown.'); + } catch (\Exception $e) { + $this->assertProductNotFoundException($e, $sku); + } } /** + * Test update() method on downloadable sample when target product does not exist */ public function testUpdateThrowsExceptionIfTargetProductDoesNotExist() { - $this->expectException(\Exception::class); - $this->expectExceptionMessage( - 'The product that was requested doesn\'t exist. Verify the product and try again.' - ); + $sku = 'wrong-sku'; - $this->updateServiceInfo['rest']['resourcePath'] = '/V1/products/wrong-sku/downloadable-links/samples/1'; + $this->updateServiceInfo['rest']['resourcePath'] = '/V1/products/' . $sku . '/downloadable-links/samples/1'; $requestData = [ 'isGlobalScopeContent' => true, - 'sku' => 'wrong-sku', + 'sku' => $sku, 'sample' => [ 'id' => 1, 'title' => 'Updated Title', @@ -588,6 +598,12 @@ public function testUpdateThrowsExceptionIfTargetProductDoesNotExist() 'sample_type' => 'url', ], ]; - $this->_webApiCall($this->updateServiceInfo, $requestData); + + try { + $this->_webApiCall($this->updateServiceInfo, $requestData); + $this->fail('Expected exception was not thrown.'); + } catch (\Exception $e) { + $this->assertProductNotFoundException($e, $sku); + } } } diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/SendFriend/SendFriendTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/SendFriend/SendFriendTest.php index 0db7c72561e33..56ba0c8114f10 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/SendFriend/SendFriendTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/SendFriend/SendFriendTest.php @@ -1,7 +1,7 @@ expectException(\Exception::class); $this->expectExceptionMessage( - 'The product that was requested doesn\'t exist. Verify the product and try again.' + 'The product with ID "2018" does not exist.' ); $productId = 2018; diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/Product/Set/DeleteTest.php b/dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/Product/Set/DeleteTest.php index dfe1115f1d167..059b85c0149d1 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/Product/Set/DeleteTest.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/Product/Set/DeleteTest.php @@ -1,7 +1,7 @@ deleteAttributeSetByNameAndAssert('new_attribute_set'); $this->expectExceptionObject( new NoSuchEntityException( - __('The product that was requested doesn\'t exist. Verify the product and try again.') + __('The product with SKU "simple" does not exist.') ) ); $this->productRepository->get('simple'); diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Helper/Product/CompositeTest.php b/dev/tests/integration/testsuite/Magento/Catalog/Helper/Product/CompositeTest.php index ef68a1ededc1f..70f676855c59f 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/Helper/Product/CompositeTest.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/Helper/Product/CompositeTest.php @@ -1,7 +1,7 @@ [ 'ok' => true, ], - 'expectedErrorMessage' => 'The product that was requested doesn\'t exist.' - . ' Verify the product and try again.', + 'expectedErrorMessage' => 'The product with ID "" does not exist.', ], ]; } diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Model/ProductRepositoryTest.php b/dev/tests/integration/testsuite/Magento/Catalog/Model/ProductRepositoryTest.php index 71a293c69246b..e90fd116fee74 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/Model/ProductRepositoryTest.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/Model/ProductRepositoryTest.php @@ -1,7 +1,7 @@ expectExceptionObject(new NoSuchEntityException( - __("The product that was requested doesn't exist. Verify the product and try again.") + __('The product with SKU "' . $sku . '" does not exist.') )); $this->productRepository->get($sku); } diff --git a/dev/tests/integration/testsuite/Magento/Quote/Model/Quote/Item/CartItemPersisterTest.php b/dev/tests/integration/testsuite/Magento/Quote/Model/Quote/Item/CartItemPersisterTest.php index 42a786e48a61e..0cf38f32488fb 100644 --- a/dev/tests/integration/testsuite/Magento/Quote/Model/Quote/Item/CartItemPersisterTest.php +++ b/dev/tests/integration/testsuite/Magento/Quote/Model/Quote/Item/CartItemPersisterTest.php @@ -1,7 +1,7 @@ setSku('not_existing_product_sku')->setQty(1); $this->expectExceptionObject( new NoSuchEntityException( - __('The product that was requested doesn\'t exist. Verify the product and try again.') + __('The product with SKU "not_existing_product_sku" does not exist.') ) ); $this->model->save($quote, $item);