PageRenderTime 27ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/dev/tests/integration/testsuite/Magento/Downloadable/_files/product_downloadable_with_files.php

https://gitlab.com/axeltizon/magento-demopoweraccess
PHP | 156 lines | 126 code | 10 blank | 20 comment | 11 complexity | 5c8af54684cd87cda96eb88ee53402db MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. \Magento\TestFramework\Helper\Bootstrap::getInstance()->getInstance()->reinitialize();
  7. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  8. /**
  9. * @var \Magento\Catalog\Model\Product $product
  10. */
  11. $product = $objectManager->create('Magento\Catalog\Model\Product');
  12. $sampleFactory = $objectManager->create('Magento\Downloadable\Api\Data\SampleInterfaceFactory');
  13. $linkFactory = $objectManager->create('Magento\Downloadable\Api\Data\LinkInterfaceFactory');
  14. $downloadableData = [
  15. 'sample' => [
  16. [
  17. 'is_delete' => 0,
  18. 'sample_id' => 0,
  19. 'title' => 'Downloadable Product Sample Title',
  20. 'type' => \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE,
  21. 'file' => json_encode(
  22. [
  23. [
  24. 'file' => '/f/u/jellyfish_1_4.jpg',
  25. 'name' => 'jellyfish_1_4.jpg',
  26. 'size' => 1024,
  27. 'status' => 0,
  28. ],
  29. ]
  30. ),
  31. 'sample_url' => null,
  32. 'sort_order' => '0',
  33. ],
  34. ],
  35. ];
  36. $product->setTypeId(
  37. \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE
  38. )->setId(
  39. 1
  40. )->setAttributeSetId(
  41. 4
  42. )->setWebsiteIds(
  43. [1]
  44. )->setName(
  45. 'Downloadable Product'
  46. )->setSku(
  47. 'downloadable-product'
  48. )->setPrice(
  49. 10
  50. )->setVisibility(
  51. \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH
  52. )->setStatus(
  53. \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED
  54. );
  55. $extension = $product->getExtensionAttributes();
  56. $links = [];
  57. $linkData = [
  58. 'product_id' => 1,
  59. 'sort_order' => '0',
  60. 'title' => 'Downloadable Product Link',
  61. 'sample' => [
  62. 'type' => \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE,
  63. 'url' => null,
  64. ],
  65. 'type' => \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE,
  66. 'is_shareable' => \Magento\Downloadable\Model\Link::LINK_SHAREABLE_CONFIG,
  67. 'link_url' => null,
  68. 'is_delete' => 0,
  69. 'number_of_downloads' => 15,
  70. 'price' => 15.00,
  71. ];
  72. $link = $linkFactory->create(['data' => $linkData]);
  73. $link->setId(null);
  74. $link->setSampleType($linkData['sample']['type']);
  75. /**
  76. * @var \Magento\Downloadable\Api\Data\File\ContentInterface $content
  77. */
  78. $content = $objectManager->create('Magento\Downloadable\Api\Data\File\ContentInterfaceFactory')->create();
  79. $content->setFileData(
  80. base64_encode(file_get_contents(__DIR__.DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR.'test_image.jpg'))
  81. );
  82. $content->setName('jellyfish_2_4.jpg');
  83. //$content->setName('');
  84. $link->setLinkFileContent($content);
  85. /**
  86. * @var \Magento\Downloadable\Api\Data\File\ContentInterface $sampleContent
  87. */
  88. $sampleContent = $objectManager->create('Magento\Downloadable\Api\Data\File\ContentInterfaceFactory')->create();
  89. $sampleContent->setFileData(
  90. base64_encode(file_get_contents(__DIR__.DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR.'test_image.jpg'))
  91. );
  92. $sampleContent->setName('jellyfish_1_3.jpg');
  93. $link->setSampleFileContent($sampleContent);
  94. $link->setSampleUrl($linkData['sample']['url']);
  95. $link->setLinkType($linkData['type']);
  96. $link->setStoreId($product->getStoreId());
  97. $link->setWebsiteId($product->getStore()->getWebsiteId());
  98. $link->setProductWebsiteIds($product->getWebsiteIds());
  99. if (!$link->getSortOrder()) {
  100. $link->setSortOrder(1);
  101. }
  102. if (null === $link->getPrice()) {
  103. $link->setPrice(0);
  104. }
  105. if ($link->getIsUnlimited()) {
  106. $link->setNumberOfDownloads(0);
  107. }
  108. $links[] = $link;
  109. $extension->setDownloadableProductLinks($links);
  110. if (isset($downloadableData['sample']) && is_array($downloadableData['sample'])) {
  111. $samples = [];
  112. foreach ($downloadableData['sample'] as $sampleData) {
  113. if (!$sampleData || (isset($sampleData['is_delete']) && (bool)$sampleData['is_delete'])) {
  114. continue;
  115. } else {
  116. unset($sampleData['sample_id']);
  117. /**
  118. * @var \Magento\Downloadable\Api\Data\SampleInterface $sample
  119. */
  120. $sample = $sampleFactory->create(['data' => $sampleData]);
  121. $sample->setId(null);
  122. $sample->setStoreId($product->getStoreId());
  123. $sample->setSampleType($sampleData['type']);
  124. $sample->setSampleUrl($sampleData['sample_url']);
  125. /**
  126. * @var \Magento\Downloadable\Api\Data\File\ContentInterface $content
  127. */
  128. $content = $objectManager->create('Magento\Downloadable\Api\Data\File\ContentInterfaceFactory')->create();
  129. $content->setFileData(
  130. base64_encode(file_get_contents(__DIR__.DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR.'test_image.jpg'))
  131. );
  132. $content->setName('jellyfish_1_4.jpg');
  133. $sample->setSampleFileContent($content);
  134. $sample->setSortOrder($sampleData['sort_order']);
  135. $samples[] = $sample;
  136. }
  137. }
  138. $extension->setDownloadableProductSamples($samples);
  139. }
  140. $product->setExtensionAttributes($extension);
  141. if ($product->getLinksPurchasedSeparately()) {
  142. $product->setTypeHasRequiredOptions(true)->setRequiredOptions(true);
  143. } else {
  144. $product->setTypeHasRequiredOptions(false)->setRequiredOptions(false);
  145. }
  146. $product->save();