This commit is contained in:
2026-09-15 10:26:34 +08:00
commit ecf36afa56
10493 changed files with 1404888 additions and 0 deletions
@@ -0,0 +1,329 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Downloadable\Api;
use Magento\TestFramework\TestCase\WebapiAbstract;
class CartItemRepositoryTest extends WebapiAbstract
{
const SERVICE_VERSION = 'V1';
const SERVICE_NAME = 'quoteCartItemRepositoryV1';
/**
* @var \Magento\TestFramework\ObjectManager
*/
protected $objectManager;
protected function setUp(): void
{
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
}
/**
* @magentoApiDataFixture Magento/Quote/_files/empty_quote.php
* @magentoApiDataFixture Magento/Downloadable/_files/product_downloadable.php
*/
public function testAddItem()
{
/** @var \Magento\Catalog\Model\Product $product */
$product = $this->objectManager->create(\Magento\Catalog\Model\Product::class)->load(1);
$productSku = $product->getSku();
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
$quote->load('reserved_order_id', 'reserved_order_id');
$cartId = $quote->getId();
$serviceInfo = [
'rest' => [
'resourcePath' => '/V1/carts/' . $cartId . '/items',
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'Save',
],
];
// use ID of the first downloadable link
$linkId = array_values($product->getDownloadableLinks())[0]->getId();
$requestData = [
'cartItem' => [
'sku' => $productSku,
'qty' => 1,
'quote_id' => $cartId,
'product_option' => [
'extension_attributes' => [
'downloadable_option' => [
'downloadable_links' => [$linkId]
]
]
]
],
];
$response = $this->_webApiCall($serviceInfo, $requestData);
$this->assertNotEmpty($response);
$this->assertEquals('downloadable-product', $response['sku']);
$this->assertEquals(1, $response['qty']);
$this->assertCount(
1,
$response['product_option']['extension_attributes']['downloadable_option']['downloadable_links']
);
$this->assertContainsEquals(
$linkId,
$response['product_option']['extension_attributes']['downloadable_option']['downloadable_links']
);
}
/**
* @magentoApiDataFixture Magento/Quote/_files/empty_quote.php
* @magentoApiDataFixture Magento/Downloadable/_files/product_downloadable.php
*/
public function testAddItemWithInvalidLinkId()
{
$this->expectException(\Exception::class);
/** @var \Magento\Catalog\Model\Product $product */
$product = $this->objectManager->create(\Magento\Catalog\Model\Product::class)->load(1);
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
$quote->load('reserved_order_id', 'reserved_order_id');
$cartId = $quote->getId();
$productSku = $product->getSku();
$serviceInfo = [
'rest' => [
'resourcePath' => '/V1/carts/' . $cartId . '/items',
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'Save',
],
];
$linkId = 9999;
$requestData = [
'cartItem' => [
'sku' => $productSku,
'qty' => 1,
'quote_id' => $cartId,
'product_option' => [
'extension_attributes' => [
'downloadable_option' => [
'downloadable_links' => [$linkId]
]
]
]
],
];
$this->_webApiCall($serviceInfo, $requestData);
}
/**
* @magentoApiDataFixture Magento/Downloadable/_files/quote_with_downloadable_product.php
*/
public function testUpdateItem()
{
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
$quote->load('reserved_order_id_1', 'reserved_order_id');
$cartId = $quote->getId();
$product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
$product->load($product->getIdBySku('downloadable-product'));
// use ID of the first quote item
$itemId = $quote->getAllItems()[0]->getId();
$serviceInfo = [
'rest' => [
'resourcePath' => '/V1/carts/' . $cartId . '/items/' . $itemId,
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'Save',
],
];
// use ID of the first downloadable link
$linkId = array_values($product->getDownloadableLinks())[0]->getId();
$requestData = [
'cartItem' => [
'qty' => 2,
'quote_id' => $cartId,
'item_id' => $itemId,
'sku' => 'downloadable-product',
'product_option' => [
'extension_attributes' => [
'downloadable_option' => [
'downloadable_links' => [$linkId]
]
]
]
],
];
$response = $this->_webApiCall($serviceInfo, $requestData);
$this->assertNotEmpty($response);
$this->assertEquals('downloadable-product', $response['sku']);
$this->assertEquals(2, $response['qty']);
$this->assertCount(
1,
$response['product_option']['extension_attributes']['downloadable_option']['downloadable_links']
);
$this->assertContainsEquals(
$linkId,
$response['product_option']['extension_attributes']['downloadable_option']['downloadable_links']
);
}
/**
* @magentoApiDataFixture Magento/Downloadable/_files/quote_with_downloadable_product.php
*/
public function testUpdateItemWithInvalidLinkId()
{
$this->expectException(\Exception::class);
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
$quote->load('reserved_order_id_1', 'reserved_order_id');
$cartId = $quote->getId();
$product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
$product->load($product->getIdBySku('downloadable-product'));
// use ID of the first quote item
$itemId = $quote->getAllItems()[0]->getId();
$serviceInfo = [
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'Save',
],
'rest' => [
'resourcePath' => '/V1/carts/' . $cartId . '/items/' . $itemId,
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
],
];
$linkId = 9999;
$requestData = [
'cartItem' => [
'qty' => 2,
'quote_id' => $cartId,
'item_id' => $itemId,
'sku' => 'downloadable-product',
'product_option' => [
'extension_attributes' => [
'downloadable_option' => [
'downloadable_links' => [$linkId]
]
]
]
],
];
$this->_webApiCall($serviceInfo, $requestData);
}
/**
* @magentoApiDataFixture Magento/Downloadable/_files/quote_with_downloadable_product.php
*/
public function testGetList()
{
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
$quote->load('reserved_order_id_1', 'reserved_order_id');
$cartId = $quote->getId();
$product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
$product->load($product->getIdBySku('downloadable-product'));
// use ID of the first downloadable link
$linkId = array_values($product->getDownloadableLinks())[0]->getId();
/** @var \Magento\Quote\Api\Data\CartItemInterface $item */
$item = $quote->getAllItems()[0];
$expectedResult = [[
'item_id' => $item->getItemId(),
'sku' => $item->getSku(),
'name' => $item->getName(),
'price' => $item->getPrice(),
'qty' => $item->getQty(),
'product_type' => $item->getProductType(),
'quote_id' => $item->getQuoteId(),
'product_option' => [
'extension_attributes' => [
'downloadable_option' => [
'downloadable_links' => [$linkId]
]
]
]
]];
$serviceInfo = [
'rest' => [
'resourcePath' => '/V1/carts/' . $cartId . '/items',
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'GetList',
],
];
$requestData = ["cartId" => $cartId];
$this->assertEquals($expectedResult, $this->_webApiCall($serviceInfo, $requestData));
}
/**
* @magentoApiDataFixture Magento/Downloadable/_files/quote_with_downloadable_product.php
*/
public function testUpdateItemQty()
{
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
$quote->load('reserved_order_id_1', 'reserved_order_id');
$product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
$product->load($product->getIdBySku('downloadable-product'));
$cartId = $quote->getId();
// use ID of the first quote item
$itemId = $quote->getAllItems()[0]->getId();
$serviceInfo = [
'rest' => [
'resourcePath' => '/V1/carts/' . $cartId . '/items/' . $itemId,
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'Save',
],
];
// use ID of the first downloadable link
$linkId = array_values($product->getDownloadableLinks())[0]->getId();
$requestData = [
'cartItem' => [
'qty' => 2,
'quote_id' => $cartId,
'item_id' => $itemId,
'sku' => 'downloadable-product',
],
];
$response = $this->_webApiCall($serviceInfo, $requestData);
$this->assertNotEmpty($response);
$this->assertEquals('downloadable-product', $response['sku']);
$this->assertEquals(2, $response['qty']);
$this->assertCount(
1,
$response['product_option']['extension_attributes']['downloadable_option']['downloadable_links']
);
$this->assertContainsEquals(
$linkId,
$response['product_option']['extension_attributes']['downloadable_option']['downloadable_links']
);
}
}
@@ -0,0 +1,120 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Downloadable\Api;
use Magento\TestFramework\TestCase\WebapiAbstract;
class OrderItemRepositoryTest extends WebapiAbstract
{
const RESOURCE_PATH = '/V1/orders/items';
const SERVICE_VERSION = 'V1';
const SERVICE_NAME = 'salesOrderItemRepositoryV1';
const ORDER_INCREMENT_ID = '100000001';
/**
* @var \Magento\TestFramework\ObjectManager
*/
protected $objectManager;
protected function setUp(): void
{
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
}
/**
* @magentoApiDataFixture Magento/Downloadable/_files/order_item_with_downloadable_and_options.php
*/
public function testGet()
{
/** @var \Magento\Sales\Model\Order $order */
$order = $this->objectManager->create(\Magento\Sales\Model\Order::class);
$order->loadByIncrementId(self::ORDER_INCREMENT_ID);
$orderItem = current($order->getItems());
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . '/' . $orderItem->getId(),
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'get',
],
];
$response = $this->_webApiCall($serviceInfo, ['id' => $orderItem->getId()]);
$this->assertIsArray($response);
$this->assertOrderItem($orderItem, $response);
}
/**
* @magentoApiDataFixture Magento/Downloadable/_files/order_item_with_downloadable_and_options.php
*/
public function testGetList()
{
/** @var \Magento\Sales\Model\Order $order */
$order = $this->objectManager->create(\Magento\Sales\Model\Order::class);
$order->loadByIncrementId(self::ORDER_INCREMENT_ID);
/** @var $searchCriteriaBuilder \Magento\Framework\Api\SearchCriteriaBuilder */
$searchCriteriaBuilder = $this->objectManager->create(\Magento\Framework\Api\SearchCriteriaBuilder::class);
/** @var $filterBuilder \Magento\Framework\Api\FilterBuilder */
$filterBuilder = $this->objectManager->create(\Magento\Framework\Api\FilterBuilder::class);
$searchCriteriaBuilder->addFilters(
[
$filterBuilder->setField('order_id')
->setValue($order->getId())
->create(),
]
);
$requestData = ['searchCriteria' => $searchCriteriaBuilder->create()->__toArray()];
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . '?' . http_build_query($requestData),
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'getList',
],
];
$response = $this->_webApiCall($serviceInfo, $requestData);
$this->assertIsArray($response);
$this->assertArrayHasKey('items', $response);
$this->assertCount(1, $response['items']);
$this->assertIsArray($response['items'][0]);
$this->assertOrderItem(current($order->getItems()), $response['items'][0]);
}
/**
* @param \Magento\Sales\Model\Order\Item $orderItem
* @param array $response
* @return void
*/
protected function assertOrderItem(\Magento\Sales\Model\Order\Item $orderItem, array $response)
{
$expected = $orderItem->getBuyRequest()->getLinks();
$this->assertArrayHasKey('product_option', $response);
$this->assertArrayHasKey('extension_attributes', $response['product_option']);
$this->assertArrayHasKey('downloadable_option', $response['product_option']['extension_attributes']);
$actualOptions = $response['product_option']['extension_attributes']['downloadable_option'];
$this->assertArrayHasKey('downloadable_links', $actualOptions);
$this->assertEquals($expected, $actualOptions['downloadable_links']);
}
}
@@ -0,0 +1,700 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Downloadable\Api;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Framework\Api\ExtensibleDataInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\WebapiAbstract;
/**
* Class ProductRepositoryTest for testing ProductRepository interface with Downloadable Product
*/
class ProductRepositoryTest extends WebapiAbstract
{
private const SERVICE_NAME = 'catalogProductRepositoryV1';
private const SERVICE_VERSION = 'V1';
private const RESOURCE_PATH = '/V1/products';
private const PRODUCT_SKU = 'sku-test-product-downloadable';
private const PRODUCT_SAMPLES = 'downloadable_product_samples';
private const PRODUCT_LINKS = 'downloadable_product_links';
/**
* @var string
*/
private $testImagePath;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->testImagePath = __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'test_image.jpg';
/** @var DomainManagerInterface $domainManager */
$domainManager = $objectManager->get(DomainManagerInterface::class);
$domainManager->addDomains(['www.example.com']);
}
/**
* Execute per test cleanup
*/
protected function tearDown(): void
{
$this->deleteProductBySku(self::PRODUCT_SKU);
parent::tearDown();
$objectManager = Bootstrap::getObjectManager();
/** @var DomainManagerInterface $domainManager */
$domainManager = $objectManager->get(DomainManagerInterface::class);
$domainManager->removeDomains(['www.example.com']);
}
protected function getLinkData()
{
return [
'link1' => [
'title' => "link1",
'sort_order'=> 10,
'is_shareable' => 1,
'price' => 2.0,
'number_of_downloads' => 0,
'link_type' => 'file',
'link_file_content' => [
'name' => 'link1_content.jpg',
// phpcs:ignore Magento2.Functions.DiscouragedFunction
'file_data' => base64_encode(file_get_contents($this->testImagePath)),
],
'sample_type' => 'file',
'sample_file_content' => [
'name' => 'link1_sample.jpg',
// phpcs:ignore Magento2.Functions.DiscouragedFunction
'file_data' => base64_encode(file_get_contents($this->testImagePath)),
],
],
'link2' => [
'title' => 'link2',
'sort_order'=> 20,
'is_shareable' => 0,
'price' => 3.0,
'number_of_downloads' => 100,
'link_type' => "url",
'link_url' => 'http://www.example.com/link2.jpg',
'sample_type' => 'url',
'sample_url' => 'http://www.example.com/link2.jpg',
],
];
}
protected function getExpectedLinkData()
{
return [
[
'title' => 'link1',
'sort_order' => 10,
'is_shareable' => 1,
'price' => 2,
'number_of_downloads' => 0,
'link_type' => 'file',
'sample_type' => 'file',
],
[
'title' => 'link2',
'sort_order' => 20,
'is_shareable' => 0,
'price' => 3,
'number_of_downloads' => 100,
'link_type' => 'url',
'link_url' => 'http://www.example.com/link2.jpg',
'sample_type' => 'url',
'sample_url' => 'http://www.example.com/link2.jpg',
],
];
}
protected function getSampleData()
{
return [
'sample1' => [
'title' => 'sample1',
'sort_order' => 10,
'sample_type' => 'url',
'sample_url' => 'http://www.example.com/sample1.jpg',
],
'sample2' => [
'title' => 'sample2',
'sort_order' => 20,
'sample_type' => 'file',
'sample_file_content' => [
'name' => 'sample2.jpg',
// phpcs:ignore Magento2.Functions.DiscouragedFunction
'file_data' => base64_encode(file_get_contents($this->testImagePath)),
],
],
];
}
protected function getExpectedSampleData()
{
return [
[
'title' => 'sample1',
'sort_order' => 10,
'sample_type' => 'url',
'sample_url' => 'http://www.example.com/sample1.jpg',
],
[
'title' => 'sample2',
'sort_order' => 20,
'sample_type' => 'file',
],
];
}
protected function createDownloadableProduct()
{
$product = [
"sku" => self::PRODUCT_SKU,
"name" => self::PRODUCT_SKU,
"type_id" => \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE,
"price" => 10,
'attribute_set_id' => 4,
"extension_attributes" => [
// phpcs:ignore Magento2.Functions.DiscouragedFunction
"downloadable_product_links" => array_values($this->getLinkData()),
// phpcs:ignore Magento2.Functions.DiscouragedFunction
"downloadable_product_samples" => array_values($this->getSampleData()),
],
];
$response = $this->createProduct($product);
$this->assertEquals(self::PRODUCT_SKU, $response[ProductInterface::SKU]);
$this->assertEquals(10, $response['price']);
$this->assertEquals(
\Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE,
$response['type_id']
);
return $response;
}
/**
* Create a downloadable product with two links and two samples
*/
public function testCreateDownloadableProduct()
{
$response = $this->createDownloadableProduct();
$this->assertTrue(
isset($response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["downloadable_product_links"])
);
$this->assertTrue(
isset($response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["downloadable_product_samples"])
);
$resultLinks
= $response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["downloadable_product_links"];
$this->assertCount(2, $resultLinks);
$this->assertTrue(isset($resultLinks[0]['id']));
$this->assertTrue(isset($resultLinks[0]['link_file']));
$this->assertTrue(isset($resultLinks[0]['sample_file']));
unset($resultLinks[0]['id']);
unset($resultLinks[0]['link_file']);
unset($resultLinks[0]['sample_file']);
$this->assertTrue(isset($resultLinks[1]['id']));
unset($resultLinks[1]['id']);
$expectedLinkData = $this->getExpectedLinkData();
$this->assertEquals($expectedLinkData, $resultLinks);
$resultSamples = $response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["downloadable_product_samples"];
$this->assertCount(2, $resultSamples);
$this->assertTrue(isset($resultSamples[0]['id']));
unset($resultSamples[0]['id']);
$this->assertTrue(isset($resultSamples[1]['id']));
$this->assertTrue(isset($resultSamples[1]['sample_file']));
unset($resultSamples[1]['sample_file']);
unset($resultSamples[1]['id']);
$expectedSampleData = $this->getExpectedSampleData();
$this->assertEquals($expectedSampleData, $resultSamples);
}
/**
* Update downloadable product, update a link, add two link, delete a link
*/
public function testUpdateDownloadableProductLinks()
{
$response = $this->createDownloadableProduct();
$resultLinks
= $response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["downloadable_product_links"];
$link1Id = $resultLinks[0]['id'];
$link2Id = $resultLinks[1]['id'];
$linkFile = $resultLinks[0]['link_file'];
$sampleFile = $resultLinks[0]['sample_file'];
$updatedLink1Data = [
'id' => $link1Id,
'title' => 'link1_updated',
'sort_order' => 1, //the sort order needs to be smaller than 10
'is_shareable' => 0,
'price' => 5.0,
'number_of_downloads' => 999,
'link_type' => 'file',
'link_file' => $linkFile,
'sample_type' => 'file',
'sample_file' => $sampleFile,
];
$linkData = $this->getLinkData();
$response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["downloadable_product_links"] =
[$updatedLink1Data, $linkData['link1'], $linkData['link2']];
$response = $this->saveProduct($response);
$this->assertTrue(
isset($response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["downloadable_product_links"])
);
$this->assertTrue(
isset($response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["downloadable_product_samples"])
);
$resultLinks
= $response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["downloadable_product_links"];
$this->assertCount(3, $resultLinks);
$this->assertTrue(isset($resultLinks[0]['id']));
$this->assertEquals($link1Id, $resultLinks[0]['id']);
$this->assertTrue(isset($resultLinks[0]['link_file']));
$this->assertEquals($linkFile, $resultLinks[0]['link_file']);
$this->assertTrue(isset($resultLinks[0]['sample_file']));
$this->assertEquals($sampleFile, $resultLinks[0]['sample_file']);
unset($resultLinks[0]['id']);
unset($resultLinks[0]['link_file']);
unset($resultLinks[0]['sample_file']);
$this->assertTrue(isset($resultLinks[1]['id']));
$this->assertGreaterThan($link2Id, $resultLinks[1]['id']);
$this->assertTrue(isset($resultLinks[1]['link_file']));
$this->assertTrue(isset($resultLinks[1]['sample_file']));
unset($resultLinks[1]['id']);
unset($resultLinks[1]['link_file']);
unset($resultLinks[1]['sample_file']);
$this->assertTrue(isset($resultLinks[2]['id']));
$this->assertGreaterThan($link2Id, $resultLinks[2]['id']);
unset($resultLinks[2]['id']);
$expectedLinkData[] = [
'title' => 'link1_updated',
'sort_order' => 1, //the sort order needs to be smaller than 10
'is_shareable' => 0,
'price' => 5.0,
'number_of_downloads' => 999,
'link_type' => 'file',
'sample_type' => 'file',
];
$expectedLinkData = array_merge($expectedLinkData, $this->getExpectedLinkData());
$this->assertEquals($expectedLinkData, $resultLinks);
$resultSamples = $response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["downloadable_product_samples"];
$this->assertCount(2, $resultSamples);
}
/**
* Update downloadable product extension attribute and check data
*
* @return void
*/
public function testUpdateDownloadableProductData(): void
{
$productResponce = $this->createDownloadableProduct();
$stockItemData = $productResponce[ProductInterface::EXTENSION_ATTRIBUTES_KEY]['stock_item'];
$stockItemData = TESTS_WEB_API_ADAPTER === self::ADAPTER_SOAP
? $stockItemData['manage_stock'] = false
: ['stock_item' => ['manage_stock' => false]];
$productData = [
ProductInterface::SKU => self::PRODUCT_SKU,
ProductInterface::EXTENSION_ATTRIBUTES_KEY => $stockItemData,
];
$response = $this->saveProduct($productData);
$this->assertArrayHasKey(ProductInterface::EXTENSION_ATTRIBUTES_KEY, $response);
$this->assertArrayHasKey(self::PRODUCT_SAMPLES, $response[ProductInterface::EXTENSION_ATTRIBUTES_KEY]);
$this->assertArrayHasKey(self::PRODUCT_LINKS, $response[ProductInterface::EXTENSION_ATTRIBUTES_KEY]);
$this->assertCount(2, $response[ProductInterface::EXTENSION_ATTRIBUTES_KEY][self::PRODUCT_SAMPLES]);
$this->assertCount(2, $response[ProductInterface::EXTENSION_ATTRIBUTES_KEY][self::PRODUCT_LINKS]);
}
/**
* Update downloadable product, update two links and change file content
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function testUpdateDownloadableProductLinksWithNewFile()
{
$response = $this->createDownloadableProduct();
$resultLinks
= $response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["downloadable_product_links"];
$link1Id = $resultLinks[0]['id'];
$link2Id = $resultLinks[1]['id'];
$linkFile = 'link1_content_updated';
$sampleFile = 'link1_sample_updated';
$extension = '.jpg';
$updatedLink1Data = [
'id' => $link1Id,
'title' => 'link1_updated',
'sort_order' => 1, //the sort order needs to be smaller than 10
'is_shareable' => 0,
'price' => 5.0,
'number_of_downloads' => 999,
'link_type' => 'file',
'link_file_content' => [
'name' => $linkFile . $extension,
// phpcs:ignore Magento2.Functions.DiscouragedFunction
'file_data' => base64_encode(file_get_contents($this->testImagePath)),
],
'sample_type' => 'file',
'sample_file_content' => [
'name' => $sampleFile . $extension,
// phpcs:ignore Magento2.Functions.DiscouragedFunction
'file_data' => base64_encode(file_get_contents($this->testImagePath)),
],
];
$updatedLink2Data = [
'id' => $link2Id,
'title' => 'link2_updated',
'sort_order' => 2,
'is_shareable' => 0,
'price' => 6.0,
'number_of_downloads' => 0,
'link_type' => 'file',
'link_file_content' => [
'name' => 'link2_content.jpg',
// phpcs:ignore Magento2.Functions.DiscouragedFunction
'file_data' => base64_encode(file_get_contents($this->testImagePath)),
],
'sample_type' => 'file',
'sample_file_content' => [
'name' => 'link2_sample.jpg',
// phpcs:ignore Magento2.Functions.DiscouragedFunction
'file_data' => base64_encode(file_get_contents($this->testImagePath)),
],
];
$response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["downloadable_product_links"] =
[$updatedLink1Data, $updatedLink2Data];
$response = $this->saveProduct($response);
$this->assertTrue(
isset($response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["downloadable_product_links"])
);
$this->assertTrue(
isset($response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["downloadable_product_samples"])
);
$resultLinks
= $response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["downloadable_product_links"];
$this->assertCount(2, $resultLinks);
$this->assertTrue(isset($resultLinks[0]['id']));
$this->assertEquals($link1Id, $resultLinks[0]['id']);
$this->assertTrue(isset($resultLinks[0]['link_file']));
$this->assertGreaterThan(0, strpos($resultLinks[0]['link_file'], $linkFile));
$this->assertStringEndsWith($extension, $resultLinks[0]['link_file']);
$this->assertTrue(isset($resultLinks[0]['sample_file']));
$this->assertGreaterThan(0, strpos($resultLinks[0]['sample_file'], $sampleFile));
$this->assertStringEndsWith($extension, $resultLinks[0]['sample_file']);
unset($resultLinks[0]['id']);
unset($resultLinks[0]['link_file']);
unset($resultLinks[0]['sample_file']);
$this->assertTrue(isset($resultLinks[1]['id']));
$this->assertEquals($link2Id, $resultLinks[1]['id']);
$this->assertTrue(isset($resultLinks[1]['link_file']));
$this->assertTrue(isset($resultLinks[1]['sample_file']));
unset($resultLinks[1]['id']);
unset($resultLinks[1]['link_file']);
unset($resultLinks[1]['sample_file']);
$expectedLinkData = [
[
'title' => 'link1_updated',
'sort_order' => 1, //the sort order needs to be smaller than 10
'is_shareable' => 0,
'price' => 5.0,
'number_of_downloads' => 999,
'link_type' => 'file',
'sample_type' => 'file',
],
[
'title' => 'link2_updated',
'sort_order' => 2,
'is_shareable' => 0,
'price' => 6.0,
'number_of_downloads' => 0,
'link_type' => 'file',
'sample_type' => 'file',
'link_url' => 'http://www.example.com/link2.jpg', //urls are still saved, just not used
'sample_url' => 'http://www.example.com/link2.jpg',
]
];
$this->assertEquals($expectedLinkData, $resultLinks);
$resultSamples = $response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["downloadable_product_samples"];
$this->assertCount(2, $resultSamples);
}
public function testUpdateDownloadableProductSamples()
{
$response = $this->createDownloadableProduct();
$resultSample
= $response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["downloadable_product_samples"];
$sample1Id = $resultSample[0]['id'];
$sample2Id = $resultSample[1]['id'];
$updatedSample1Data = [
'id' => $sample1Id,
'title' => 'sample1_updated',
'sort_order' => 1,
'sample_type' => 'url',
'sample_url' => 'http://www.example.com/sample1.jpg',
];
$sampleData = $this->getSampleData();
$response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["downloadable_product_samples"] =
[$updatedSample1Data, $sampleData['sample1'], $sampleData['sample2']];
$response = $this->saveProduct($response);
$this->assertTrue(
isset($response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["downloadable_product_links"])
);
$this->assertTrue(
isset($response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["downloadable_product_samples"])
);
$resultLinks
= $response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["downloadable_product_links"];
$this->assertCount(2, $resultLinks);
$resultSamples = $response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["downloadable_product_samples"];
$this->assertCount(3, $resultSamples);
$this->assertTrue(isset($resultSamples[0]['id']));
$this->assertEquals($sample1Id, $resultSamples[0]['id']);
unset($resultSamples[0]['id']);
$this->assertTrue(isset($resultSamples[1]['id']));
$this->assertGreaterThan($sample2Id, $resultSamples[1]['id']);
unset($resultSamples[1]['id']);
$this->assertTrue(isset($resultSamples[2]['id']));
$this->assertGreaterThan($sample2Id, $resultSamples[2]['id']);
$this->assertTrue(isset($resultSamples[2]['sample_file']));
unset($resultSamples[2]['sample_file']);
unset($resultSamples[2]['id']);
$expectedSampleData[] = [
'title' => 'sample1_updated',
'sort_order' => 1,
'sample_type' => 'url',
'sample_url' => 'http://www.example.com/sample1.jpg',
];
$expectedSampleData = array_merge($expectedSampleData, $this->getExpectedSampleData());
$this->assertEquals($expectedSampleData, $resultSamples);
}
public function testUpdateDownloadableProductSamplesWithNewFile()
{
$response = $this->createDownloadableProduct();
$resultSample
= $response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["downloadable_product_samples"];
$sample1Id = $resultSample[0]['id'];
$sample2Id = $resultSample[1]['id'];
//upload a file for sample 1
$updatedSample1Data = [
'id' => $sample1Id,
'title' => 'sample1_updated',
'sort_order' => 1,
'sample_type' => 'file',
'sample_file_content' => [
'name' => 'sample1.jpg',
// phpcs:ignore Magento2.Functions.DiscouragedFunction
'file_data' => base64_encode(file_get_contents($this->testImagePath)),
],
];
//change title for sample 2
$updatedSamp2e1Data = [
'id' => $sample2Id,
'title' => 'sample2_updated',
'sort_order' => 2,
'sample_type' => 'file',
'sample_file_content' => [
'name' => 'sample2.jpg',
// phpcs:ignore Magento2.Functions.DiscouragedFunction
'file_data' => base64_encode(file_get_contents($this->testImagePath)),
],
];
$response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["downloadable_product_samples"] =
[$updatedSample1Data, $updatedSamp2e1Data];
$response = $this->saveProduct($response);
$this->assertTrue(
isset($response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["downloadable_product_links"])
);
$this->assertTrue(
isset($response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["downloadable_product_samples"])
);
$resultLinks
= $response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["downloadable_product_links"];
$this->assertCount(2, $resultLinks);
$resultSamples = $response[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]["downloadable_product_samples"];
$this->assertCount(2, $resultSamples);
$this->assertTrue(isset($resultSamples[0]['id']));
$this->assertEquals($sample1Id, $resultSamples[0]['id']);
unset($resultSamples[0]['id']);
$this->assertTrue(isset($resultSamples[0]['sample_file']));
$this->assertStringContainsString('sample1', $resultSamples[0]['sample_file']);
$this->assertStringEndsWith('.jpg', $resultSamples[0]['sample_file']);
unset($resultSamples[0]['sample_file']);
$this->assertTrue(isset($resultSamples[1]['id']));
$this->assertEquals($sample2Id, $resultSamples[1]['id']);
unset($resultSamples[1]['id']);
$this->assertTrue(isset($resultSamples[1]['sample_file']));
$this->assertStringContainsString('sample2', $resultSamples[1]['sample_file']);
$this->assertStringEndsWith('.jpg', $resultSamples[1]['sample_file']);
unset($resultSamples[1]['sample_file']);
$expectedSampleData = [
[
'title' => 'sample1_updated',
'sort_order' => 1,
'sample_type' => 'file',
'sample_url' => 'http://www.example.com/sample1.jpg',
],
[
'title' => 'sample2_updated',
'sort_order' => 2,
'sample_type' => 'file',
],
];
$this->assertEquals($expectedSampleData, $resultSamples);
}
/**
* Get product
*
* @param string $productSku
* @return array the product data
*/
protected function getProduct($productSku)
{
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . '/' . $productSku,
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'Get',
],
];
$response = (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) ?
$this->_webApiCall($serviceInfo, ['sku' => $productSku]) : $this->_webApiCall($serviceInfo);
return $response;
}
/**
* Create product
*
* @param array $product
* @return array the created product data
*/
protected function createProduct($product)
{
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH,
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'Save',
],
];
$requestData = ['product' => $product];
$response = $this->_webApiCall($serviceInfo, $requestData);
return $response;
}
/**
* Delete a product by sku
*
* @param $productSku
* @return bool
*/
protected function deleteProductBySku($productSku)
{
$resourcePath = self::RESOURCE_PATH . '/' . $productSku;
$serviceInfo = [
'rest' => [
'resourcePath' => $resourcePath,
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'deleteById',
],
];
$requestData = ["sku" => $productSku];
$response = $this->_webApiCall($serviceInfo, $requestData);
return $response;
}
/**
* Save product
*
* @param array $product
* @return array the created product data
*/
protected function saveProduct($product)
{
if (isset($product['custom_attributes'])) {
for ($i = 0, $iMax = count($product['custom_attributes']); $i < $iMax; $i++) {
if ($product['custom_attributes'][$i]['attribute_code'] == 'category_ids'
&& !is_array($product['custom_attributes'][$i]['value'])
) {
$product['custom_attributes'][$i]['value'] = [""];
}
}
}
$resourcePath = self::RESOURCE_PATH . '/' . $product['sku'];
$serviceInfo = [
'rest' => [
'resourcePath' => $resourcePath,
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'Save',
],
];
$requestData = ['product' => $product];
$response = $this->_webApiCall($serviceInfo, $requestData);
return $response;
}
}
@@ -0,0 +1,593 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Downloadable\Api;
use Magento\Catalog\Model\Product;
use Magento\Downloadable\Model\Sample;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\WebapiAbstract;
/**
* API tests for Magento\Downloadable\Model\SampleRepository.
*/
class SampleRepositoryTest extends WebapiAbstract
{
/**
* @var array
*/
protected $createServiceInfo;
/**
* @var string
*/
protected $testImagePath;
/**
* @var array
*/
protected $updateServiceInfo;
/**
* @var array
*/
protected $deleteServiceInfo;
/**
* @var DomainManagerInterface
*/
private $domainManager;
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->domainManager = $objectManager->get(DomainManagerInterface::class);
$this->domainManager->addDomains(['example.com']);
$this->createServiceInfo = [
'rest' => [
'resourcePath' => '/V1/products/downloadable-product/downloadable-links/samples',
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
],
'soap' => [
'service' => 'downloadableSampleRepositoryV1',
'serviceVersion' => 'V1',
'operation' => 'downloadableSampleRepositoryV1Save',
],
];
$this->updateServiceInfo = [
'rest' => [
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
],
'soap' => [
'service' => 'downloadableSampleRepositoryV1',
'serviceVersion' => 'V1',
'operation' => 'downloadableSampleRepositoryV1Save',
],
];
$this->deleteServiceInfo = [
'rest' => [
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE,
],
'soap' => [
'service' => 'downloadableSampleRepositoryV1',
'serviceVersion' => 'V1',
'operation' => 'downloadableSampleRepositoryV1Delete',
],
];
$this->testImagePath = __DIR__ . str_replace('/', DIRECTORY_SEPARATOR, '/_files/test_image.jpg');
}
/**
* Remove example domain from whitelist and call parent restore configuration
*/
protected function tearDown(): void
{
parent::tearDown();
$this->domainManager->removeDomains(['example.com']);
}
/**
* Retrieve product that was updated by test
*
* @param bool $isScopeGlobal if true product store ID will be set to 0
* @return Product
*/
protected function getTargetProduct($isScopeGlobal = false)
{
if ($isScopeGlobal) {
$product = Bootstrap::getObjectManager()->get(\Magento\Catalog\Model\ProductFactory::class)
->create()->setStoreId(0)->load(1);
} else {
$product = Bootstrap::getObjectManager()->get(\Magento\Catalog\Model\ProductFactory::class)
->create()
->load(1);
}
return $product;
}
/**
* Retrieve product sample by its ID (or first sample if ID is not specified)
*
* @param Product $product
* @param int|null $sampleId
* @return Sample|null
*/
protected function getTargetSample(Product $product, $sampleId = null)
{
$samples = $product->getExtensionAttributes()->getDownloadableProductSamples();
if ($sampleId) {
/* @var $sample \Magento\Downloadable\Model\Sample */
if ($samples) {
foreach ($samples as $sample) {
if ($sample->getId() == $sampleId) {
return $sample;
}
}
}
return null;
}
// return first sample
return $samples[0];
}
/**
* @magentoApiDataFixture Magento/Downloadable/_files/product_downloadable.php
*/
public function testCreateUploadsProvidedFileContent()
{
$requestData = [
'isGlobalScopeContent' => true,
'sku' => 'downloadable-product',
'sample' => [
'title' => 'Title',
'sort_order' => 1,
'sample_file_content' => [
//phpcs:ignore Magento2.Functions.DiscouragedFunction
'file_data' => base64_encode(file_get_contents($this->testImagePath)),
'name' => 'image.jpg',
],
'sample_type' => 'file',
],
];
$newSampleId = $this->_webApiCall($this->createServiceInfo, $requestData);
$globalScopeSample = $this->getTargetSample($this->getTargetProduct(true), $newSampleId);
$sample = $this->getTargetSample($this->getTargetProduct(), $newSampleId);
$this->assertNotNull($sample);
$this->assertNotNull($sample->getId());
$this->assertEquals($requestData['sample']['title'], $sample->getTitle());
$this->assertEquals($requestData['sample']['title'], $globalScopeSample->getTitle());
$this->assertEquals($requestData['sample']['sort_order'], $sample->getSortOrder());
$this->assertEquals($requestData['sample']['sample_type'], $sample->getSampleType());
$this->assertStringEndsWith('.jpg', $sample->getSampleFile());
$this->assertNull($sample->getSampleUrl());
}
/**
* @magentoApiDataFixture Magento/Downloadable/_files/product_downloadable.php
*/
public function testCreateThrowsExceptionIfSampleTypeIsInvalid()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('The sample type is invalid. Verify the sample type and try again.');
$requestData = [
'isGlobalScopeContent' => false,
'sku' => 'downloadable-product',
'sample' => [
'title' => 'Sample with URL resource',
'sort_order' => 1,
'sample_type' => 'invalid',
],
];
$this->_webApiCall($this->createServiceInfo, $requestData);
}
/**
* Check that error appears when sample file not existing in filesystem.
*
* @magentoApiDataFixture Magento/Downloadable/_files/product_downloadable.php
* @return void
*/
public function testCreateSampleWithMissingFileThrowsException(): void
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Sample file not found. Please try again.');
$requestData = [
'isGlobalScopeContent' => false,
'sku' => 'downloadable-product',
'sample' => [
'title' => 'Link Title',
'sort_order' => 1,
'sample_type' => 'file',
'sample_file' => '/n/o/nexistfile.png',
],
];
$this->_webApiCall($this->createServiceInfo, $requestData);
}
/**
* @magentoApiDataFixture Magento/Downloadable/_files/product_downloadable.php
*/
public function testCreateThrowsExceptionIfSampleFileContentIsNotAValidBase64EncodedString()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Provided content must be valid base64 encoded data.');
$requestData = [
'isGlobalScopeContent' => false,
'sku' => 'downloadable-product',
'sample' => [
'title' => 'Sample Title',
'sort_order' => 1,
'sample_type' => 'file',
'sample_file_content' => [
'file_data' => 'not_a_base64_encoded_content',
'name' => 'image.jpg',
],
],
];
$this->_webApiCall($this->createServiceInfo, $requestData);
}
/**
* @magentoApiDataFixture Magento/Downloadable/_files/product_downloadable.php
*/
public function testCreateThrowsExceptionIfSampleFileNameContainsForbiddenCharacters()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Provided file name contains forbidden characters.');
$requestData = [
'isGlobalScopeContent' => false,
'sku' => 'downloadable-product',
'sample' => [
'title' => 'Title',
'sort_order' => 15,
'sample_type' => 'file',
'sample_file_content' => [
//phpcs:ignore Magento2.Functions.DiscouragedFunction
'file_data' => base64_encode(file_get_contents($this->testImagePath)),
'name' => 'name/with|forbidden{characters',
],
],
];
$this->_webApiCall($this->createServiceInfo, $requestData);
}
/**
* @magentoApiDataFixture Magento/Downloadable/_files/product_downloadable.php
*/
public function testCreateThrowsExceptionIfSampleUrlHasWrongFormat()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Sample URL must have valid format.');
$requestData = [
'isGlobalScopeContent' => false,
'sku' => 'downloadable-product',
'sample' => [
'title' => 'Sample Title',
'sort_order' => 1,
'sample_type' => 'url',
'sample_url' => 'http://example<.>com/',
],
];
$this->_webApiCall($this->createServiceInfo, $requestData);
}
/**
* @magentoApiDataFixture Magento/Downloadable/_files/product_downloadable.php
* @dataProvider getInvalidSortOrder
*/
public function testCreateThrowsExceptionIfSortOrderIsInvalid($sortOrder)
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Sort order must be a positive integer.');
$requestData = [
'isGlobalScopeContent' => false,
'sku' => 'downloadable-product',
'sample' => [
'title' => 'Sample Title',
'sort_order' => $sortOrder,
'sample_type' => 'url',
'sample_url' => 'http://example.com/',
],
];
$this->_webApiCall($this->createServiceInfo, $requestData);
}
/**
* @return array
*/
public function getInvalidSortOrder()
{
return [
[-1],
];
}
/**
* @magentoApiDataFixture Magento/Downloadable/_files/product_downloadable_with_files.php
*/
public function testUpdate()
{
$sampleId = $this->getTargetSample($this->getTargetProduct())->getId();
$this->updateServiceInfo['rest']['resourcePath']
= "/V1/products/downloadable-product/downloadable-links/samples/{$sampleId}";
$requestData = [
'isGlobalScopeContent' => false,
'sku' => 'downloadable-product',
'sample' => [
'id' => $sampleId,
'title' => 'Updated Title',
'sort_order' => 2,
'sample_type' => 'url',
'sample_url' => 'http://google.com',
],
];
$this->assertEquals($sampleId, $this->_webApiCall($this->updateServiceInfo, $requestData));
$sample = $this->getTargetSample($this->getTargetProduct(), $sampleId);
$this->assertNotNull($sample);
$this->assertEquals($requestData['sample']['id'], $sample->getId());
$this->assertEquals($requestData['sample']['title'], $sample->getTitle());
$this->assertEquals($requestData['sample']['sort_order'], $sample->getSortOrder());
}
/**
* @magentoApiDataFixture Magento/Downloadable/_files/product_downloadable_with_files.php
*/
public function testUpdateSavesDataInGlobalScopeAndDoesNotAffectValuesStoredInStoreViewScope()
{
$originalSample = $this->getTargetSample($this->getTargetProduct());
$sampleId = $originalSample->getId();
$this->updateServiceInfo['rest']['resourcePath']
= "/V1/products/downloadable-product/downloadable-links/samples/{$sampleId}";
$requestData = [
'isGlobalScopeContent' => true,
'sku' => 'downloadable-product',
'sample' => [
'id' => $sampleId,
'title' => 'Updated Title',
'sort_order' => 2,
'sample_type' => 'url',
'sample_url' => 'http://google.com',
],
];
$this->assertEquals($sampleId, $this->_webApiCall($this->updateServiceInfo, $requestData));
$sample = $this->getTargetSample($this->getTargetProduct(), $sampleId);
$globalScopeSample = $this->getTargetSample($this->getTargetProduct(true), $sampleId);
$this->assertNotNull($sample);
// Title was set on store view level in fixture so it must be the same
$this->assertEquals($originalSample->getTitle(), $sample->getTitle());
$this->assertEquals($requestData['sample']['title'], $globalScopeSample->getTitle());
$this->assertEquals($requestData['sample']['sort_order'], $sample->getSortOrder());
}
/**
* @magentoApiDataFixture Magento/Downloadable/_files/product_downloadable_with_files.php
*/
public function testUpdateThrowsExceptionIfThereIsNoDownloadableSampleWithGivenId()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage(
'No downloadable sample with the provided ID was found. Verify the ID and try again.'
);
$sampleId = 9999;
$this->updateServiceInfo['rest']['resourcePath']
= "/V1/products/downloadable-product/downloadable-links/samples/{$sampleId}";
$requestData = [
'isGlobalScopeContent' => true,
'sku' => 'downloadable-product',
'sample' => [
'id' => $sampleId,
'title' => 'Title',
'sort_order' => 2,
'sample_type' => 'url',
],
];
$this->_webApiCall($this->updateServiceInfo, $requestData);
}
/**
* @magentoApiDataFixture Magento/Downloadable/_files/product_downloadable_with_files.php
* @dataProvider getInvalidSortOrder
*/
public function testUpdateThrowsExceptionIfSortOrderIsInvalid($sortOrder)
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Sort order must be a positive integer.');
$sampleId = $this->getTargetSample($this->getTargetProduct())->getId();
$this->updateServiceInfo['rest']['resourcePath']
= "/V1/products/downloadable-product/downloadable-links/samples/{$sampleId}";
$requestData = [
'isGlobalScopeContent' => false,
'sku' => 'downloadable-product',
'sample' => [
'id' => $sampleId,
'title' => 'Updated Sample Title',
'sort_order' => $sortOrder,
'sample_type' => 'url',
],
];
$this->_webApiCall($this->updateServiceInfo, $requestData);
}
/**
* @magentoApiDataFixture Magento/Downloadable/_files/product_downloadable_with_files.php
*/
public function testDelete()
{
$sampleId = $this->getTargetSample($this->getTargetProduct())->getId();
$this->deleteServiceInfo['rest']['resourcePath'] = "/V1/products/downloadable-links/samples/{$sampleId}";
$requestData = [
'id' => $sampleId,
];
$this->assertTrue($this->_webApiCall($this->deleteServiceInfo, $requestData));
$sample = $this->getTargetSample($this->getTargetProduct(), $sampleId);
$this->assertNull($sample);
}
/**
* @magentoApiDataFixture Magento/Downloadable/_files/product_downloadable_with_files.php
* @dataProvider getListForAbsentProductProvider
*/
public function testGetList($urlTail, $method, $expectations)
{
$sku = 'downloadable-product';
$serviceInfo = [
'rest' => [
'resourcePath' => '/V1/products/' . $sku . $urlTail,
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => 'downloadableSampleRepositoryV1',
'serviceVersion' => 'V1',
'operation' => 'downloadableSampleRepositoryV1' . $method,
],
];
$requestData = ['sku' => $sku];
$list = $this->_webApiCall($serviceInfo, $requestData);
$this->assertCount(1, $list);
$link = reset($list);
foreach ($expectations['fields'] as $index => $value) {
$this->assertEquals($value, $link[$index]);
}
$this->assertNotEmpty($link['sample_file']);
}
public function getListForAbsentProductProvider()
{
$sampleExpectation = [
'fields' => [
'title' => 'Downloadable Product Sample Title',
'sort_order' => 0,
'sample_type' => 'file'
]
];
return [
'samples' => [
'/downloadable-links/samples',
'GetList',
$sampleExpectation,
],
];
}
/**
*/
public function testDeleteThrowsExceptionIfThereIsNoDownloadableSampleWithGivenId()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage(
'No downloadable sample with the provided ID was found. Verify the ID and try again.'
);
$sampleId = 9999;
$this->deleteServiceInfo['rest']['resourcePath'] = "/V1/products/downloadable-links/samples/{$sampleId}";
$requestData = [
'id' => $sampleId,
];
$this->_webApiCall($this->deleteServiceInfo, $requestData);
}
/**
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
*/
public function testCreateThrowsExceptionIfTargetProductTypeIsNotDownloadable()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage(
'The product needs to be the downloadable type. Verify the product and try again.'
);
$this->createServiceInfo['rest']['resourcePath'] = '/V1/products/simple/downloadable-links/samples';
$requestData = [
'isGlobalScopeContent' => false,
'sku' => 'simple',
'sample' => [
'title' => 'Sample Title',
'sort_order' => 50,
'sample_type' => 'url',
'sample_url' => 'http://example.com/',
],
];
$this->_webApiCall($this->createServiceInfo, $requestData);
}
/**
*/
public function testCreateThrowsExceptionIfTargetProductDoesNotExist()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage(
'The product that was requested doesn\'t exist. Verify the product and try again.'
);
$this->createServiceInfo['rest']['resourcePath'] = '/V1/products/wrong-sku/downloadable-links/samples';
$requestData = [
'isGlobalScopeContent' => false,
'sku' => 'wrong-sku',
'sample' => [
'title' => 'Title',
'sort_order' => 15,
'sample_type' => 'url',
'sample_url' => 'http://example.com/',
],
];
$this->_webApiCall($this->createServiceInfo, $requestData);
}
/**
*/
public function testUpdateThrowsExceptionIfTargetProductDoesNotExist()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage(
'The product that was requested doesn\'t exist. Verify the product and try again.'
);
$this->updateServiceInfo['rest']['resourcePath'] = '/V1/products/wrong-sku/downloadable-links/samples/1';
$requestData = [
'isGlobalScopeContent' => true,
'sku' => 'wrong-sku',
'sample' => [
'id' => 1,
'title' => 'Updated Title',
'sort_order' => 2,
'sample_type' => 'url',
],
];
$this->_webApiCall($this->updateServiceInfo, $requestData);
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB