Skip to content

Commit 506c4de

Browse files
authored
ENGCOM-5373: Links to the downloadable_product_samples are direct links #719
2 parents 145535b + dca10d5 commit 506c4de

File tree

3 files changed

+39
-49
lines changed

3 files changed

+39
-49
lines changed

app/code/Magento/DownloadableGraphQl/Model/Resolver/Product/DownloadableOptions.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Magento\Framework\GraphQl\Config\Element\Field;
2020
use Magento\Framework\GraphQl\Query\EnumLookup;
2121
use Magento\Framework\GraphQl\Query\ResolverInterface;
22+
use Magento\Framework\UrlInterface;
2223

2324
/**
2425
* @inheritdoc
@@ -47,22 +48,30 @@ class DownloadableOptions implements ResolverInterface
4748
*/
4849
private $linkCollection;
4950

51+
/**
52+
* @var UrlInterface
53+
*/
54+
private $urlBuilder;
55+
5056
/**
5157
* @param EnumLookup $enumLookup
5258
* @param DownloadableHelper $downloadableHelper
5359
* @param SampleCollection $sampleCollection
5460
* @param LinkCollection $linkCollection
61+
* @param UrlInterface|null $urlBuilder
5562
*/
5663
public function __construct(
5764
EnumLookup $enumLookup,
5865
DownloadableHelper $downloadableHelper,
5966
SampleCollection $sampleCollection,
60-
LinkCollection $linkCollection
67+
LinkCollection $linkCollection,
68+
UrlInterface $urlBuilder
6169
) {
6270
$this->enumLookup = $enumLookup;
6371
$this->downloadableHelper = $downloadableHelper;
6472
$this->sampleCollection = $sampleCollection;
6573
$this->linkCollection = $linkCollection;
74+
$this->urlBuilder = $urlBuilder;
6675
}
6776

6877
/**
@@ -144,7 +153,10 @@ private function formatLinks(LinkCollection $links) : array
144153
}
145154

146155
$resultData[$linkKey]['sample_file'] = $link->getSampleFile();
147-
$resultData[$linkKey]['sample_url'] = $link->getSampleUrl();
156+
$resultData[$linkKey]['sample_url'] = $this->urlBuilder->getUrl(
157+
'downloadable/download/linkSample',
158+
['link_id' => $link->getId()]
159+
);
148160
}
149161
return $resultData;
150162
}
@@ -155,7 +167,7 @@ private function formatLinks(LinkCollection $links) : array
155167
* @param Collection $samples
156168
* @return array
157169
*/
158-
private function formatSamples(Collection $samples) : array
170+
private function formatSamples(Collection $samples): array
159171
{
160172
$resultData = [];
161173
foreach ($samples as $sampleKey => $sample) {
@@ -166,7 +178,10 @@ private function formatSamples(Collection $samples) : array
166178
$resultData[$sampleKey]['sample_type']
167179
= $this->enumLookup->getEnumValueFromField('DownloadableFileTypeEnum', $sample->getSampleType());
168180
$resultData[$sampleKey]['sample_file'] = $sample->getSampleFile();
169-
$resultData[$sampleKey]['sample_url'] = $sample->getSampleUrl();
181+
$resultData[$sampleKey]['sample_url'] = $this->urlBuilder->getUrl(
182+
'downloadable/download/sample',
183+
['sample_id' => $sample->getId()]
184+
);
170185
}
171186
return $resultData;
172187
}

app/code/Magento/DownloadableGraphQl/etc/schema.graphqls

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,29 @@ type DownloadableProduct implements ProductInterface, CustomizableProductInterfa
2424
links_title: String @doc(description: "The heading above the list of downloadable products")
2525
}
2626

27-
enum DownloadableFileTypeEnum @doc(description: "This enumeration specifies whether a link or sample is a file or URL") {
27+
enum DownloadableFileTypeEnum @deprecated(reason: "`sample_url` serves to get the downloadable sample") {
2828
FILE
2929
URL
3030
}
3131

3232
type DownloadableProductLinks @doc(description: "DownloadableProductLinks defines characteristics of a downloadable product") {
33-
id: Int @doc(description: "The unique ID for the link to the downloadable product")
33+
id: Int @deprecated(reason: "This information shoud not be exposed on frontend")
3434
title: String @doc(description: "The display name of the link")
3535
sort_order: Int @doc(description: "A number indicating the sort order")
36-
is_shareable: Boolean @doc(description: "Indicates whether the link is shareable")
36+
is_shareable: Boolean @deprecated(reason: "This information shoud not be exposed on frontend")
3737
price: Float @doc(description: "The price of the downloadable product")
38-
number_of_downloads: Int @doc(description: "The maximum number of times the product can be downloaded. A value of 0 means unlimited.")
39-
link_type: DownloadableFileTypeEnum @doc(description: "Either FILE or URL")
40-
sample_type: DownloadableFileTypeEnum @doc(description: "Either FILE or URL")
41-
sample_file: String @doc(description: "The relative path to the downloadable sample")
42-
sample_url: String @doc(description: "The relative URL to the downloadable sample")
38+
number_of_downloads: Int @deprecated(reason: "This information shoud not be exposed on frontend")
39+
link_type: DownloadableFileTypeEnum @deprecated(reason: "`sample_url` serves to get the downloadable sample")
40+
sample_type: DownloadableFileTypeEnum @deprecated(reason: "`sample_url` serves to get the downloadable sample")
41+
sample_file: String @deprecated(reason: "`sample_url` serves to get the downloadable sample")
42+
sample_url: String @doc(description: "URL to the downloadable sample")
4343
}
4444

4545
type DownloadableProductSamples @doc(description: "DownloadableProductSamples defines characteristics of a downloadable product") {
46-
id: Int @doc(description: "The unique ID for the downloadable product sample")
46+
id: Int @deprecated(reason: "This information shoud not be exposed on frontend")
4747
title: String @doc(description: "The display name of the sample")
4848
sort_order: Int @doc(description: "A number indicating the sort order")
49-
sample_type: DownloadableFileTypeEnum @doc(description: "Either FILE or URL")
50-
sample_file: String @doc(description: "The relative path to the downloadable sample")
51-
sample_url: String @doc(description: "The relative URL to the downloadable sample")
49+
sample_type: DownloadableFileTypeEnum @deprecated(reason: "`sample_url` serves to get the downloadable sample")
50+
sample_file: String @deprecated(reason: "`sample_url` serves to get the downloadable sample")
51+
sample_url: String @doc(description: "URL to the downloadable sample")
5252
}

dev/tests/api-functional/testsuite/Magento/GraphQl/DownloadableProduct/DownloadableProductViewTest.php

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
use Magento\TestFramework\ObjectManager;
1616
use Magento\TestFramework\TestCase\GraphQlAbstract;
1717

18+
/**
19+
* Test for downloadable product.
20+
*/
1821
class DownloadableProductViewTest extends GraphQlAbstract
1922
{
2023
/**
@@ -53,24 +56,16 @@ public function testQueryAllFieldsDownloadableProductsWithDownloadableFileAndSam
5356
links_purchased_separately
5457
5558
downloadable_product_links{
56-
id
5759
sample_url
58-
sample_type
59-
60-
is_shareable
61-
number_of_downloads
6260
sort_order
6361
title
64-
link_type
65-
6662
price
6763
}
6864
downloadable_product_samples{
6965
title
7066
sort_order
7167
sort_order
72-
sample_type
73-
sample_file
68+
sample_url
7469
}
7570
}
7671
}
@@ -82,8 +77,6 @@ public function testQueryAllFieldsDownloadableProductsWithDownloadableFileAndSam
8277
$config = ObjectManager::getInstance()->get(\Magento\Config\Model\ResourceModel\Config::class);
8378
$config->saveConfig(
8479
\Magento\Downloadable\Model\Link::XML_PATH_CONFIG_IS_SHAREABLE,
85-
0,
86-
ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
8780
0
8881
);
8982
$response = $this->graphQlQuery($query);
@@ -143,22 +136,15 @@ public function testDownloadableProductQueryWithNoSample()
143136
links_purchased_separately
144137
145138
downloadable_product_links{
146-
id
147139
sample_url
148-
sample_type
149-
is_shareable
150-
number_of_downloads
151140
sort_order
152141
title
153-
link_type
154142
price
155143
}
156144
downloadable_product_samples{
157145
title
158146
sort_order
159-
sort_order
160-
sample_type
161-
sample_file
147+
sample_url
162148
}
163149
}
164150
}
@@ -197,12 +183,8 @@ public function testDownloadableProductQueryWithNoSample()
197183
$this->assertResponseFields(
198184
$response['products']['items'][0]['downloadable_product_links'][0],
199185
[
200-
'id' => $downloadableProductLink->getId(),
201-
'is_shareable' => false,
202-
'number_of_downloads' => $downloadableProductLink->getNumberOfDownloads(),
203186
'sort_order' => $downloadableProductLink->getSortOrder(),
204187
'title' => $downloadableProductLink->getTitle(),
205-
'link_type' => strtoupper($downloadableProductLink->getLinkType()),
206188
'price' => $downloadableProductLink->getPrice()
207189
]
208190
);
@@ -221,18 +203,12 @@ private function assertDownloadableProductLinks($product, $actualResponse)
221203
/** @var LinkInterface $downloadableProductLinks */
222204
$downloadableProductLinks = $product->getExtensionAttributes()->getDownloadableProductLinks();
223205
$downloadableProductLink = $downloadableProductLinks[1];
224-
206+
$this->assertNotEmpty('sample_url', $actualResponse['downloadable_product_links'][1]);
225207
$this->assertResponseFields(
226208
$actualResponse['downloadable_product_links'][1],
227209
[
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(),
233210
'sort_order' => $downloadableProductLink->getSortOrder(),
234211
'title' => $downloadableProductLink->getTitle(),
235-
'link_type' => strtoupper($downloadableProductLink->getLinkType()),
236212
'price' => $downloadableProductLink->getPrice()
237213
]
238214
);
@@ -251,13 +227,12 @@ private function assertDownloadableProductSamples($product, $actualResponse)
251227
/** @var SampleInterface $downloadableProductSamples */
252228
$downloadableProductSamples = $product->getExtensionAttributes()->getDownloadableProductSamples();
253229
$downloadableProductSample = $downloadableProductSamples[0];
230+
$this->assertNotEmpty('sample_url', $actualResponse['downloadable_product_samples'][0]);
254231
$this->assertResponseFields(
255232
$actualResponse['downloadable_product_samples'][0],
256233
[
257234
'title' => $downloadableProductSample->getTitle(),
258-
'sort_order' =>$downloadableProductSample->getSortOrder(),
259-
'sample_type' => strtoupper($downloadableProductSample->getSampleType()),
260-
'sample_file' => $downloadableProductSample->getSampleFile()
235+
'sort_order' => $downloadableProductSample->getSortOrder()
261236
]
262237
);
263238
}

0 commit comments

Comments
 (0)