Skip to content

Commit f69ef7f

Browse files
committed
MCP-806: Prevent opcache invalidation on every request
- Move test from web api to integration because of error on CI builds that domain is not in the list of downloadable domains in env.php
1 parent a497d84 commit f69ef7f

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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+
* Originally this test belonged to \Magento\Downloadable\Api\SampleRepositoryTest
38+
* but it was moved to integration tests
39+
* because of error on CI builds that sample URL's domain is not in the list of downloadable_domains in env.php
40+
*
41+
* @magentoDataFixture Magento/Downloadable/_files/product_downloadable.php
42+
*/
43+
public function testCreateSavesTitleInStoreViewScope(): void
44+
{
45+
/** @var ProductRepositoryInterface $productRepository */
46+
$productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
47+
$product = $this->getTargetProduct(false);
48+
49+
$links = $this->model->getLinks($product);
50+
$this->assertNotEmpty($links);
51+
$samples = $this->model->getSamples($product);
52+
$this->assertEmpty($samples->getData());
53+
$downloadableSampleData = [
54+
'title' => 'Store View Title',
55+
'sort_order' => 1,
56+
'sample_url' => 'http://www.sample.example.com/',
57+
'sample_type' => 'url',
58+
];
59+
60+
$sampleFactory = $this->objectManager->create(SampleInterfaceFactory::class);
61+
$extension = $product->getExtensionAttributes();
62+
$sample = $sampleFactory->create(['data' => $downloadableSampleData]);
63+
$sample->setStoreId($product->getStoreId());
64+
$sample->setSampleType($downloadableSampleData['sample_type']);
65+
$sample->setSampleUrl($downloadableSampleData['sample_url']);
66+
if (!$sample->getSortOrder()) {
67+
$sample->setSortOrder(1);
68+
}
69+
$extension->setDownloadableProductSamples([$sample]);
70+
$product->setExtensionAttributes($extension);
71+
$productRepository->save($product);
72+
73+
$expectedSample = [
74+
'title' => 'Store View Title',
75+
'sort_order' => 1,
76+
'sample_url' => 'http://www.sample.example.com/',
77+
'sample_type' => 'url',
78+
];
79+
80+
$samples = $this->getTargetProduct(false)->getExtensionAttributes()->getDownloadableProductSamples();
81+
$sample = reset($samples);
82+
83+
$this->assertNotEmpty($sample->getData());
84+
$this->assertCount(1, $samples);
85+
86+
/** @var \Magento\Downloadable\Model\Sample $sample */
87+
$sampleData = $sample->getData();
88+
/** @var \Magento\User\Api\Data\UserInterface $testAttribute */
89+
foreach ($expectedSample as $key => $value) {
90+
$this->assertArrayHasKey($key, $sampleData);
91+
$this->assertEquals($value, $sampleData[$key]);
92+
}
93+
94+
$globalScopeSample = $this->getTargetSample(
95+
$this->getTargetProduct(true),
96+
(int)$sampleData['sample_id']
97+
);
98+
$this->assertEmpty($globalScopeSample->getTitle());
99+
}
100+
101+
/**
102+
* Retrieve product that was updated by test
103+
*
104+
* @param bool $isScopeGlobal if true product store ID will be set to 0
105+
* @return Product
106+
*/
107+
private function getTargetProduct(bool $isScopeGlobal): Product
108+
{
109+
if ($isScopeGlobal) {
110+
$product = $this->objectManager->get(ProductFactory::class)
111+
->create()->setStoreId(0)->load(1);
112+
} else {
113+
$product = $this->objectManager->get(ProductFactory::class)
114+
->create()
115+
->load(1);
116+
}
117+
118+
return $product;
119+
}
120+
121+
/**
122+
* Retrieve product sample by its ID (or first sample if ID is not specified)
123+
*
124+
* @param Product $product
125+
* @param int|null $sampleId
126+
* @return Sample|null
127+
*/
128+
private function getTargetSample(Product $product, int $sampleId = null): ?Sample
129+
{
130+
$samples = $product->getExtensionAttributes()->getDownloadableProductSamples();
131+
if ($sampleId) {
132+
if ($samples) {
133+
foreach ($samples as $sample) {
134+
if ((int)$sample->getId() === $sampleId) {
135+
return $sample;
136+
}
137+
}
138+
}
139+
140+
return null;
141+
}
142+
143+
return $samples[0];
144+
}
145+
}

0 commit comments

Comments
 (0)