|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Downloadable\Model; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 11 | +use Magento\Catalog\Model\Product; |
| 12 | +use Magento\Catalog\Model\ProductFactory; |
| 13 | +use Magento\Downloadable\Api\Data\SampleInterfaceFactory; |
| 14 | + |
| 15 | +/** |
| 16 | + * Test class for \Magento\Downloadable\Model\SampleRepository |
| 17 | + */ |
| 18 | +class SampleRepositoryTest extends \PHPUnit\Framework\TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var \Magento\Downloadable\Model\Product\Type |
| 22 | + */ |
| 23 | + private $model; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var \Magento\Framework\ObjectManagerInterface |
| 27 | + */ |
| 28 | + private $objectManager; |
| 29 | + |
| 30 | + protected function setUp(): void |
| 31 | + { |
| 32 | + $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
| 33 | + $this->model = $this->objectManager->create(\Magento\Downloadable\Model\Product\Type::class); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @magentoDataFixture Magento/Downloadable/_files/product_downloadable.php |
| 38 | + */ |
| 39 | + public function testCreateSavesProvidedUrls() |
| 40 | + { |
| 41 | + /** @var ProductRepositoryInterface $productRepository */ |
| 42 | + $productRepository = $this->objectManager->create(ProductRepositoryInterface::class); |
| 43 | + $product = $this->getTargetProduct(false); |
| 44 | + |
| 45 | + $links = $this->model->getLinks($product); |
| 46 | + $this->assertNotEmpty($links); |
| 47 | + $samples = $this->model->getSamples($product); |
| 48 | + $this->assertEmpty($samples->getData()); |
| 49 | + $downloadableSampleData = [ |
| 50 | + 'title' => 'Sample with URL resource', |
| 51 | + 'sort_order' => 1, |
| 52 | + 'sample_url' => 'http://www.sample.example.com/', |
| 53 | + 'sample_type' => 'url', |
| 54 | + ]; |
| 55 | + |
| 56 | + $sampleFactory = $this->objectManager->create(SampleInterfaceFactory::class); |
| 57 | + $extension = $product->getExtensionAttributes(); |
| 58 | + $sample = $sampleFactory->create(['data' => $downloadableSampleData]); |
| 59 | + $sample->setStoreId($product->getStoreId()); |
| 60 | + $sample->setSampleType($downloadableSampleData['sample_type']); |
| 61 | + $sample->setSampleUrl($downloadableSampleData['sample_url']); |
| 62 | + if (!$sample->getSortOrder()) { |
| 63 | + $sample->setSortOrder(1); |
| 64 | + } |
| 65 | + $extension->setDownloadableProductSamples([$sample]); |
| 66 | + $product->setExtensionAttributes($extension); |
| 67 | + $productRepository->save($product); |
| 68 | + |
| 69 | + $samples = $this->getTargetProduct(false)->getExtensionAttributes()->getDownloadableProductSamples(); |
| 70 | + $sample = reset($samples); |
| 71 | + |
| 72 | + $this->assertNotEmpty($sample->getData()); |
| 73 | + $this->assertCount(1, $samples); |
| 74 | + |
| 75 | + /** @var \Magento\Downloadable\Model\Sample $sample */ |
| 76 | + $sampleData = $sample->getData(); |
| 77 | + /** @var \Magento\User\Api\Data\UserInterface $testAttribute */ |
| 78 | + foreach ($downloadableSampleData as $key => $value) { |
| 79 | + $this->assertArrayHasKey($key, $sampleData); |
| 80 | + $this->assertEquals($value, $sampleData[$key]); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @magentoDataFixture Magento/Downloadable/_files/product_downloadable.php |
| 86 | + */ |
| 87 | + public function testCreateSavesTitleInStoreViewScope(): void |
| 88 | + { |
| 89 | + /** @var ProductRepositoryInterface $productRepository */ |
| 90 | + $productRepository = $this->objectManager->create(ProductRepositoryInterface::class); |
| 91 | + $product = $this->getTargetProduct(false); |
| 92 | + |
| 93 | + $links = $this->model->getLinks($product); |
| 94 | + $this->assertNotEmpty($links); |
| 95 | + $samples = $this->model->getSamples($product); |
| 96 | + $this->assertEmpty($samples->getData()); |
| 97 | + $downloadableSampleData = [ |
| 98 | + 'title' => 'Store View Title', |
| 99 | + 'sort_order' => 1, |
| 100 | + 'sample_url' => 'http://www.sample.example.com/', |
| 101 | + 'sample_type' => 'url' |
| 102 | + ]; |
| 103 | + |
| 104 | + $sampleFactory = $this->objectManager->create(SampleInterfaceFactory::class); |
| 105 | + $extension = $product->getExtensionAttributes(); |
| 106 | + $sample = $sampleFactory->create(['data' => $downloadableSampleData]); |
| 107 | + $sample->setStoreId($product->getStoreId()); |
| 108 | + $sample->setSampleType($downloadableSampleData['sample_type']); |
| 109 | + $sample->setSampleUrl($downloadableSampleData['sample_url']); |
| 110 | + if (!$sample->getSortOrder()) { |
| 111 | + $sample->setSortOrder(1); |
| 112 | + } |
| 113 | + $extension->setDownloadableProductSamples([$sample]); |
| 114 | + $product->setExtensionAttributes($extension); |
| 115 | + $productRepository->save($product); |
| 116 | + |
| 117 | + $samples = $this->getTargetProduct(false)->getExtensionAttributes()->getDownloadableProductSamples(); |
| 118 | + $sample = reset($samples); |
| 119 | + |
| 120 | + $this->assertNotEmpty($sample->getData()); |
| 121 | + $this->assertCount(1, $samples); |
| 122 | + |
| 123 | + /** @var \Magento\Downloadable\Model\Sample $sample */ |
| 124 | + $sampleData = $sample->getData(); |
| 125 | + /** @var \Magento\User\Api\Data\UserInterface $testAttribute */ |
| 126 | + foreach ($downloadableSampleData as $key => $value) { |
| 127 | + $this->assertArrayHasKey($key, $sampleData); |
| 128 | + $this->assertEquals($value, $sampleData[$key]); |
| 129 | + } |
| 130 | + |
| 131 | + $globalScopeSample = $this->getTargetSample( |
| 132 | + $this->getTargetProduct(true), |
| 133 | + (int)$sampleData['sample_id'] |
| 134 | + ); |
| 135 | + $this->assertEmpty($globalScopeSample->getTitle()); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Retrieve product that was updated by test |
| 140 | + * |
| 141 | + * @param bool $isScopeGlobal if true product store ID will be set to 0 |
| 142 | + * @return Product |
| 143 | + */ |
| 144 | + private function getTargetProduct(bool $isScopeGlobal): Product |
| 145 | + { |
| 146 | + if ($isScopeGlobal) { |
| 147 | + $product = $this->objectManager->get(ProductFactory::class) |
| 148 | + ->create()->setStoreId(0)->load(1); |
| 149 | + } else { |
| 150 | + $product = $this->objectManager->get(ProductFactory::class) |
| 151 | + ->create() |
| 152 | + ->load(1); |
| 153 | + } |
| 154 | + |
| 155 | + return $product; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Retrieve product sample by its ID (or first sample if ID is not specified) |
| 160 | + * |
| 161 | + * @param Product $product |
| 162 | + * @param int|null $sampleId |
| 163 | + * @return Sample|null |
| 164 | + */ |
| 165 | + private function getTargetSample(Product $product, int $sampleId = null): ?Sample |
| 166 | + { |
| 167 | + $samples = $product->getExtensionAttributes()->getDownloadableProductSamples(); |
| 168 | + if ($sampleId) { |
| 169 | + if ($samples) { |
| 170 | + foreach ($samples as $sample) { |
| 171 | + if ((int)$sample->getId() === $sampleId) { |
| 172 | + return $sample; |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + return null; |
| 178 | + } |
| 179 | + |
| 180 | + return $samples[0]; |
| 181 | + } |
| 182 | +} |
0 commit comments