DownloadableProductViewTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\GraphQl\DownloadableProduct;
  8. use Magento\Catalog\Api\Data\ProductInterface;
  9. use Magento\Catalog\Api\ProductRepositoryInterface;
  10. use Magento\Downloadable\Api\Data\LinkInterface;
  11. use Magento\Downloadable\Api\Data\SampleInterface;
  12. use Magento\Framework\App\Config\ScopeConfigInterface;
  13. use Magento\TestFramework\ObjectManager;
  14. use Magento\TestFramework\TestCase\GraphQlAbstract;
  15. class DownloadableProductViewTest extends GraphQlAbstract
  16. {
  17. /**
  18. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  19. * @magentoApiDataFixture Magento/Downloadable/_files/downloadable_product_with_files_and_sample_url.php
  20. */
  21. public function testQueryAllFieldsDownloadableProductsWithDownloadableFileAndSample()
  22. {
  23. $productSku = 'downloadable-product';
  24. $query = <<<QUERY
  25. {
  26. products(filter:{sku: {eq:"{$productSku}"}})
  27. {
  28. items{
  29. id
  30. attribute_set_id
  31. created_at
  32. name
  33. sku
  34. type_id
  35. updated_at
  36. price{
  37. regularPrice{
  38. amount{
  39. value
  40. currency
  41. }
  42. adjustments{
  43. code
  44. description
  45. }
  46. }
  47. }
  48. ... on DownloadableProduct {
  49. links_title
  50. links_purchased_separately
  51. downloadable_product_links{
  52. id
  53. sample_url
  54. sample_type
  55. is_shareable
  56. number_of_downloads
  57. sort_order
  58. title
  59. link_type
  60. price
  61. }
  62. downloadable_product_samples{
  63. title
  64. sort_order
  65. sort_order
  66. sample_type
  67. sample_file
  68. }
  69. }
  70. }
  71. }
  72. }
  73. QUERY;
  74. /** @var \Magento\Config\Model\ResourceModel\Config $config */
  75. $config = ObjectManager::getInstance()->get(\Magento\Config\Model\ResourceModel\Config::class);
  76. $config->saveConfig(
  77. \Magento\Downloadable\Model\Link::XML_PATH_CONFIG_IS_SHAREABLE,
  78. 0,
  79. ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
  80. 0
  81. );
  82. $response = $this->graphQlQuery($query);
  83. /**
  84. * @var ProductRepositoryInterface $productRepository
  85. */
  86. $productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
  87. $downloadableProduct = $productRepository->get($productSku, false, null, true);
  88. $this->assertNull($downloadableProduct->getWeight());
  89. $IsLinksPurchasedSeparately = $downloadableProduct->getLinksPurchasedSeparately();
  90. $linksTitle = $downloadableProduct->getLinksTitle();
  91. $this->assertEquals(
  92. $IsLinksPurchasedSeparately,
  93. $response['products']['items'][0]['links_purchased_separately']
  94. );
  95. $this->assertEquals($linksTitle, $response['products']['items'][0]['links_title']);
  96. $this->assertDownloadableProductLinks($downloadableProduct, $response['products']['items'][0]);
  97. $this->assertDownloadableProductSamples($downloadableProduct, $response['products']['items'][0]);
  98. }
  99. /**
  100. * @magentoApiDataFixture Magento/Downloadable/_files/product_downloadable.php
  101. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  102. */
  103. public function testDownloadableProductQueryWithNoSample()
  104. {
  105. $productSku = 'downloadable-product';
  106. $query = <<<QUERY
  107. {
  108. products(filter:{sku: {eq:"{$productSku}"}})
  109. {
  110. items{
  111. id
  112. attribute_set_id
  113. created_at
  114. name
  115. sku
  116. type_id
  117. updated_at
  118. ...on PhysicalProductInterface{
  119. weight
  120. }
  121. price{
  122. regularPrice{
  123. amount{
  124. value
  125. currency
  126. }
  127. adjustments{
  128. code
  129. description
  130. }
  131. }
  132. }
  133. ... on DownloadableProduct {
  134. links_title
  135. links_purchased_separately
  136. downloadable_product_links{
  137. id
  138. sample_url
  139. sample_type
  140. is_shareable
  141. number_of_downloads
  142. sort_order
  143. title
  144. link_type
  145. price
  146. }
  147. downloadable_product_samples{
  148. title
  149. sort_order
  150. sort_order
  151. sample_type
  152. sample_file
  153. }
  154. }
  155. }
  156. }
  157. }
  158. QUERY;
  159. $response = $this->graphQlQuery($query);
  160. /**
  161. * @var ProductRepositoryInterface $productRepository
  162. */
  163. $productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
  164. $downloadableProduct = $productRepository->get($productSku, false, null, true);
  165. /** @var \Magento\Config\Model\ResourceModel\Config $config */
  166. $config = ObjectManager::getInstance()->get(\Magento\Config\Model\ResourceModel\Config::class);
  167. $config->saveConfig(
  168. \Magento\Downloadable\Model\Link::XML_PATH_CONFIG_IS_SHAREABLE,
  169. 0,
  170. ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
  171. 1
  172. );
  173. $IsLinksPurchasedSeparately = $downloadableProduct->getLinksPurchasedSeparately();
  174. $linksTitle = $downloadableProduct->getLinksTitle();
  175. $this->assertEquals(
  176. $IsLinksPurchasedSeparately,
  177. $response['products']['items'][0]['links_purchased_separately']
  178. );
  179. $this->assertEquals($linksTitle, $response['products']['items'][0]['links_title']);
  180. $this->assertEmpty($response['products']['items'][0]['downloadable_product_samples']);
  181. $this->assertNotEmpty(
  182. $response['products']['items'][0]['downloadable_product_links'],
  183. "Precondition failed: 'downloadable_product_links' must not be empty"
  184. );
  185. /** @var LinkInterface $downloadableProductLinks */
  186. $downloadableProductLinks = $downloadableProduct->getExtensionAttributes()->getDownloadableProductLinks();
  187. $downloadableProductLink = $downloadableProductLinks[0];
  188. $this->assertResponseFields(
  189. $response['products']['items'][0]['downloadable_product_links'][0],
  190. [
  191. 'id' => $downloadableProductLink->getId(),
  192. 'is_shareable' => false,
  193. 'number_of_downloads' => $downloadableProductLink->getNumberOfDownloads(),
  194. 'sort_order' => $downloadableProductLink->getSortOrder(),
  195. 'title' => $downloadableProductLink->getTitle(),
  196. 'link_type' => strtoupper($downloadableProductLink->getLinkType()),
  197. 'price' => $downloadableProductLink->getPrice()
  198. ]
  199. );
  200. }
  201. /**
  202. * @param ProductInterface $product
  203. * @param array $actualResponse
  204. */
  205. private function assertDownloadableProductLinks($product, $actualResponse)
  206. {
  207. $this->assertNotEmpty(
  208. $actualResponse['downloadable_product_links'],
  209. "Precondition failed: 'downloadable_product_links' must not be empty"
  210. );
  211. /** @var LinkInterface $downloadableProductLinks */
  212. $downloadableProductLinks = $product->getExtensionAttributes()->getDownloadableProductLinks();
  213. $downloadableProductLink = $downloadableProductLinks[1];
  214. $this->assertResponseFields(
  215. $actualResponse['downloadable_product_links'][1],
  216. [
  217. 'id' => $downloadableProductLink->getId(),
  218. 'sample_url' => $downloadableProductLink->getSampleUrl(),
  219. 'sample_type' => strtoupper($downloadableProductLink->getSampleType()),
  220. 'is_shareable' => false,
  221. 'number_of_downloads' => $downloadableProductLink->getNumberOfDownloads(),
  222. 'sort_order' => $downloadableProductLink->getSortOrder(),
  223. 'title' => $downloadableProductLink->getTitle(),
  224. 'link_type' => strtoupper($downloadableProductLink->getLinkType()),
  225. 'price' => $downloadableProductLink->getPrice()
  226. ]
  227. );
  228. }
  229. /**
  230. * @param ProductInterface $product
  231. * @param $actualResponse
  232. */
  233. private function assertDownloadableProductSamples($product, $actualResponse)
  234. {
  235. $this->assertNotEmpty(
  236. $actualResponse['downloadable_product_samples'],
  237. "Precondition failed: 'downloadable_product_samples' must not be empty"
  238. );
  239. /** @var SampleInterface $downloadableProductSamples */
  240. $downloadableProductSamples = $product->getExtensionAttributes()->getDownloadableProductSamples();
  241. $downloadableProductSample = $downloadableProductSamples[0];
  242. $this->assertResponseFields(
  243. $actualResponse['downloadable_product_samples'][0],
  244. [
  245. 'title' => $downloadableProductSample->getTitle(),
  246. 'sort_order' =>$downloadableProductSample->getSortOrder(),
  247. 'sample_type' => strtoupper($downloadableProductSample->getSampleType()),
  248. 'sample_file' => $downloadableProductSample->getSampleFile()
  249. ]
  250. );
  251. }
  252. }