Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DownloadableProductViewTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
17 
19 {
24  public function testQueryAllFieldsDownloadableProductsWithDownloadableFileAndSample()
25  {
26  $productSku = 'downloadable-product';
27  $query = <<<QUERY
28 {
29  products(filter:{sku: {eq:"{$productSku}"}})
30  {
31  items{
32  id
33  attribute_set_id
34  created_at
35  name
36  sku
37  type_id
38  updated_at
39  price{
40  regularPrice{
41  amount{
42  value
43  currency
44  }
45  adjustments{
46  code
47  description
48  }
49  }
50  }
51  ... on DownloadableProduct {
52  links_title
53  links_purchased_separately
54 
55  downloadable_product_links{
56  id
57  sample_url
58  sample_type
59 
60  is_shareable
61  number_of_downloads
62  sort_order
63  title
64  link_type
65 
66  price
67  }
68  downloadable_product_samples{
69  title
70  sort_order
71  sort_order
72  sample_type
73  sample_file
74  }
75  }
76  }
77  }
78 }
79 QUERY;
80 
82  $config = ObjectManager::getInstance()->get(\Magento\Config\Model\ResourceModel\Config::class);
83  $config->saveConfig(
84  \Magento\Downloadable\Model\Link::XML_PATH_CONFIG_IS_SHAREABLE,
85  0,
87  0
88  );
89  $response = $this->graphQlQuery($query);
93  $productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
94  $downloadableProduct = $productRepository->get($productSku, false, null, true);
95  $this->assertNull($downloadableProduct->getWeight());
96  $IsLinksPurchasedSeparately = $downloadableProduct->getLinksPurchasedSeparately();
97  $linksTitle = $downloadableProduct->getLinksTitle();
98  $this->assertEquals(
99  $IsLinksPurchasedSeparately,
100  $response['products']['items'][0]['links_purchased_separately']
101  );
102  $this->assertEquals($linksTitle, $response['products']['items'][0]['links_title']);
103  $this->assertDownloadableProductLinks($downloadableProduct, $response['products']['items'][0]);
104  $this->assertDownloadableProductSamples($downloadableProduct, $response['products']['items'][0]);
105  }
106 
111  public function testDownloadableProductQueryWithNoSample()
112  {
113  $productSku = 'downloadable-product';
114  $query = <<<QUERY
115 {
116  products(filter:{sku: {eq:"{$productSku}"}})
117  {
118  items{
119  id
120  attribute_set_id
121  created_at
122  name
123  sku
124  type_id
125  updated_at
126  ...on PhysicalProductInterface{
127  weight
128  }
129  price{
130  regularPrice{
131  amount{
132  value
133  currency
134  }
135  adjustments{
136  code
137  description
138  }
139  }
140  }
141  ... on DownloadableProduct {
142  links_title
143  links_purchased_separately
144 
145  downloadable_product_links{
146  id
147  sample_url
148  sample_type
149  is_shareable
150  number_of_downloads
151  sort_order
152  title
153  link_type
154  price
155  }
156  downloadable_product_samples{
157  title
158  sort_order
159  sort_order
160  sample_type
161  sample_file
162  }
163  }
164  }
165  }
166 }
167 QUERY;
168  $response = $this->graphQlQuery($query);
172  $productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
173  $downloadableProduct = $productRepository->get($productSku, false, null, true);
175  $config = ObjectManager::getInstance()->get(\Magento\Config\Model\ResourceModel\Config::class);
176  $config->saveConfig(
177  \Magento\Downloadable\Model\Link::XML_PATH_CONFIG_IS_SHAREABLE,
178  0,
180  1
181  );
182  $IsLinksPurchasedSeparately = $downloadableProduct->getLinksPurchasedSeparately();
183  $linksTitle = $downloadableProduct->getLinksTitle();
184  $this->assertEquals(
185  $IsLinksPurchasedSeparately,
186  $response['products']['items'][0]['links_purchased_separately']
187  );
188  $this->assertEquals($linksTitle, $response['products']['items'][0]['links_title']);
189  $this->assertEmpty($response['products']['items'][0]['downloadable_product_samples']);
190  $this->assertNotEmpty(
191  $response['products']['items'][0]['downloadable_product_links'],
192  "Precondition failed: 'downloadable_product_links' must not be empty"
193  );
195  $downloadableProductLinks = $downloadableProduct->getExtensionAttributes()->getDownloadableProductLinks();
196  $downloadableProductLink = $downloadableProductLinks[0];
197  $this->assertResponseFields(
198  $response['products']['items'][0]['downloadable_product_links'][0],
199  [
200  'id' => $downloadableProductLink->getId(),
201  'is_shareable' => false,
202  'number_of_downloads' => $downloadableProductLink->getNumberOfDownloads(),
203  'sort_order' => $downloadableProductLink->getSortOrder(),
204  'title' => $downloadableProductLink->getTitle(),
205  'link_type' => strtoupper($downloadableProductLink->getLinkType()),
206  'price' => $downloadableProductLink->getPrice()
207  ]
208  );
209  }
210 
215  private function assertDownloadableProductLinks($product, $actualResponse)
216  {
217  $this->assertNotEmpty(
218  $actualResponse['downloadable_product_links'],
219  "Precondition failed: 'downloadable_product_links' must not be empty"
220  );
222  $downloadableProductLinks = $product->getExtensionAttributes()->getDownloadableProductLinks();
223  $downloadableProductLink = $downloadableProductLinks[1];
224 
225  $this->assertResponseFields(
226  $actualResponse['downloadable_product_links'][1],
227  [
228  'id' => $downloadableProductLink->getId(),
229  'sample_url' => $downloadableProductLink->getSampleUrl(),
230  'sample_type' => strtoupper($downloadableProductLink->getSampleType()),
231  'is_shareable' => false,
232  'number_of_downloads' => $downloadableProductLink->getNumberOfDownloads(),
233  'sort_order' => $downloadableProductLink->getSortOrder(),
234  'title' => $downloadableProductLink->getTitle(),
235  'link_type' => strtoupper($downloadableProductLink->getLinkType()),
236  'price' => $downloadableProductLink->getPrice()
237  ]
238  );
239  }
240 
245  private function assertDownloadableProductSamples($product, $actualResponse)
246  {
247  $this->assertNotEmpty(
248  $actualResponse['downloadable_product_samples'],
249  "Precondition failed: 'downloadable_product_samples' must not be empty"
250  );
252  $downloadableProductSamples = $product->getExtensionAttributes()->getDownloadableProductSamples();
253  $downloadableProductSample = $downloadableProductSamples[0];
254  $this->assertResponseFields(
255  $actualResponse['downloadable_product_samples'][0],
256  [
257  'title' => $downloadableProductSample->getTitle(),
258  'sort_order' =>$downloadableProductSample->getSortOrder(),
259  'sample_type' => strtoupper($downloadableProductSample->getSampleType()),
260  'sample_file' => $downloadableProductSample->getSampleFile()
261  ]
262  );
263  }
264 }
$response
Definition: 404.php:11
$block setTitle( 'CMS Block Title') -> setIdentifier('fixture_block') ->setContent('< h1 >Fixture Block Title</h1 >< a href=" store url</a><p> Config value
Definition: block.php:9
if( $block->displayPriceExclTax()||$block->displayBothPrices())(__('Excl. Tax')) ?>"> <?php if ($block -> displayPriceWithWeeeDetails()): ?> <span class="cart-tax-total" data-mage-init=' Magento Weee Helper Data Magento Weee Helper Data title amount
Definition: unit.phtml:68
$config
Definition: fraud_order.php:17
assertResponseFields($actualResponse, $assertionMap)
taxRateField this edit on("click.mselect-delete", ".mselect-delete", function() { if(!confirm('<?=/*@escapeNotVerified */__( 'Do you really want to delete this tax rate?') ?>')) { return;} var that=$(this), select=that.closest('.mselect-list').prev(), rateValue=that.parent().find( 'input[type="checkbox"]').val();$( 'body').trigger( 'processStart');var ajaxOptions={ type:'POST', data:{ tax_calculation_rate_id:rateValue, form_key:$( 'input[name="form_key"]').val() }, dataType:'json', url:'<?=/*@escapeNotVerified */$block->getTaxRateDeleteUrl() ?>', success:function(result, status) { $( 'body').trigger( 'processStop');if(result.success) { that.parent().remove();select.find( 'option').each(function() { if(this.value===rateValue) { $(this).remove();} });select.trigger( 'change.hiddenSelect');} else { if(result.error_message) alert({ content:result.error_message });else alert({ content:'<?=/*@escapeNotVerified */__( 'An error occurred') ?>' });} }, error:function() { $( 'body').trigger( 'processStop');alert({ content:'<?=/*@escapeNotVerified */__( 'An error occurred') ?>' });} };$.ajax(ajaxOptions);}) .on( 'click.mselectAdd'
Definition: edit.phtml:164
graphQlQuery(string $query, array $variables=[], string $operationName='', array $headers=[])