Skip to content

Commit 599bc7b

Browse files
committed
AC-10686: [PCI] enabled on payment pages.
Added Integration test. Added scenario to skip graphql static test for scenarios not requiring graphql side change.
1 parent 2493562 commit 599bc7b

File tree

7 files changed

+204
-53
lines changed

7 files changed

+204
-53
lines changed

app/code/Magento/Csp/Test/Unit/Model/SubresourceIntegrityRepositoryTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,15 @@ public function testSaveBunch(): void
114114

115115
$bunches = [$bunch1, $bunch2];
116116

117-
$expected = [];
117+
$expected = [];
118118

119-
foreach($bunches as $bunch){
120-
$expected[$bunch->getPath()] = $bunch->getHash();
121-
}
119+
foreach ($bunches as $bunch) {
120+
$expected[$bunch->getPath()] = $bunch->getHash();
121+
}
122122
$serializedBunch = json_encode($expected);
123123
$this->cacheMock->expects($this->once())->method('load')->willReturn(false);
124-
$this->serializerMock->expects($this->once())->method('serialize')->with($expected)->willReturn($serializedBunch);
124+
$this->serializerMock->expects($this->once())->method('serialize')
125+
->with($expected)->willReturn($serializedBunch);
125126
$this->cacheMock->expects($this->once())->method('save')->willReturn(true);
126127
$this->assertTrue($this->subresourceIntegrityRepository->saveBunch($bunches));
127128
}

app/code/Magento/Csp/Test/Unit/Plugin/AddDefaultPropertiesToGroupPluginTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ public function testBeforeGetFilteredProperties(): void
106106
$this->assertEquals(
107107
$expected,
108108
$this->plugin->beforeGetFilteredProperties(
109-
$groupedCollectionMock, $this->assetInterfaceMock
109+
$groupedCollectionMock,
110+
$this->assetInterfaceMock
110111
)
111112
);
112113
}

app/code/Magento/Csp/Test/Unit/Plugin/StoreAssetIntegrityHashesTest.php

Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,21 @@ class StoreAssetIntegrityHashesTest extends TestCase
4242
* @return void
4343
*/
4444
protected function setUp(): void
45-
{
46-
parent::setUp();
47-
$this->integrityRepositoryPoolMock = $this->getMockBuilder(SubresourceIntegrityRepositoryPool::class)
48-
->disableOriginalConstructor()
49-
->onlyMethods(['get'])
50-
->getMock();
51-
$this->integrityCollectorMock = $this->getMockBuilder(SubresourceIntegrityCollector::class)
52-
->disableOriginalConstructor()
53-
->onlyMethods(['release'])
54-
->getMock();
55-
$this->plugin = new StoreAssetIntegrityHashes(
56-
$this->integrityCollectorMock,
57-
$this->integrityRepositoryPoolMock,
58-
);
59-
}
45+
{
46+
parent::setUp();
47+
$this->integrityRepositoryPoolMock = $this->getMockBuilder(SubresourceIntegrityRepositoryPool::class)
48+
->disableOriginalConstructor()
49+
->onlyMethods(['get'])
50+
->getMock();
51+
$this->integrityCollectorMock = $this->getMockBuilder(SubresourceIntegrityCollector::class)
52+
->disableOriginalConstructor()
53+
->onlyMethods(['release'])
54+
->getMock();
55+
$this->plugin = new StoreAssetIntegrityHashes(
56+
$this->integrityCollectorMock,
57+
$this->integrityRepositoryPoolMock,
58+
);
59+
}
6060

6161
/**
6262
* Test After Deploy method of plugin
@@ -65,34 +65,32 @@ protected function setUp(): void
6565
* @doesNotPerformAssertions
6666
*/
6767
public function testAfterDeploy(): void
68-
{
69-
$bunch1 = new SubresourceIntegrity(
70-
[
71-
'hash' => 'testhash',
72-
'path' => 'adminhtml/js/jquery.js'
73-
]
74-
);
68+
{
69+
$bunch1 = new SubresourceIntegrity(
70+
[
71+
'hash' => 'testhash',
72+
'path' => 'adminhtml/js/jquery.js'
73+
]
74+
);
7575

76-
$bunch2 = new SubresourceIntegrity(
77-
[
78-
'hash' => 'testhash2',
79-
'path' => 'frontend/js/test.js'
80-
]
81-
);
76+
$bunch2 = new SubresourceIntegrity(
77+
[
78+
'hash' => 'testhash2',
79+
'path' => 'frontend/js/test.js'
80+
]
81+
);
8282

83-
$bunches = [$bunch1, $bunch2];
84-
$deployStaticContentMock = $this->getMockBuilder(DeployStaticContent::class)
85-
->disableOriginalConstructor()
86-
->getMock();
87-
$subResourceIntegrityMock = $this->getMockBuilder(SubresourceIntegrityRepository::class)
88-
->disableOriginalConstructor()
89-
->onlyMethods(['saveBunch'])
90-
->getMock();
91-
$this->integrityCollectorMock->expects($this->once())->method('release')->willReturn($bunches);
92-
$this->integrityRepositoryPoolMock->expects($this->any())->method('get')->willReturn($subResourceIntegrityMock);
93-
$subResourceIntegrityMock->expects($this->any())->method('saveBunch')->willReturn(true);
94-
$this->plugin->afterDeploy($deployStaticContentMock, null, []);
95-
}
83+
$bunches = [$bunch1, $bunch2];
84+
$deployStaticContentMock = $this->getMockBuilder(DeployStaticContent::class)
85+
->disableOriginalConstructor()
86+
->getMock();
87+
$subResourceIntegrityMock = $this->getMockBuilder(SubresourceIntegrityRepository::class)
88+
->disableOriginalConstructor()
89+
->onlyMethods(['saveBunch'])
90+
->getMock();
91+
$this->integrityCollectorMock->expects($this->once())->method('release')->willReturn($bunches);
92+
$this->integrityRepositoryPoolMock->expects($this->any())->method('get')->willReturn($subResourceIntegrityMock);
93+
$subResourceIntegrityMock->expects($this->any())->method('saveBunch')->willReturn(true);
94+
$this->plugin->afterDeploy($deployStaticContentMock, null, []);
95+
}
9696
}
97-
98-

app/code/Magento/Csp/view/base/templates/sri/hashes.phtml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@
66

77
/** @var \Magento\Csp\Block\Sri\Hashes $block */
88
/** @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer */
9-
//phpcs:ignore Magento2.GraphQL
109
?>
1110

1211
<?php $sriHashes = /* @noEscape */ $block->getSerialized();
13-
//@codingStandardsIgnoreStart
1412
$scriptString = <<<script
1513
window.sriHashes = {$sriHashes};
1614
script;
17-
//@codingStandardsIgnoreStart
1815
?>
1916

2017
<?= /* @noEscape */ $secureRenderer->renderTag('script', [], $scriptString, false) ?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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\Csp\Model\SubresourceIntegrity;
9+
10+
use Magento\Csp\Model\SubresourceIntegrityRepositoryPool;
11+
use Magento\Deploy\Service\DeployStaticContent;
12+
use Magento\Deploy\Strategy\DeployStrategyFactory;
13+
use Magento\Framework\Exception\FileSystemException;
14+
use Magento\Framework\Exception\LocalizedException;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
use Magento\Framework\App\Filesystem\DirectoryList;
17+
use Magento\Framework\Filesystem;
18+
use Magento\Deploy\Console\DeployStaticOptions as Options;
19+
use Magento\Framework\Filesystem\Directory\WriteInterface;
20+
use Magento\Csp\Model\SubresourceIntegrityRepository;
21+
use Magento\Csp\Model\SubresourceIntegrity\HashGenerator;
22+
23+
/**
24+
* Integration test to cover end to end SRI Generation
25+
*/
26+
class SubresourceIntegrityRepositoryTest extends \PHPUnit\Framework\TestCase
27+
{
28+
/**
29+
* @var Filesystem|null
30+
*/
31+
private ?Filesystem $filesystem = null;
32+
33+
/**
34+
* @var WriteInterface|null
35+
*/
36+
private ?WriteInterface $staticDir = null;
37+
38+
/**
39+
* @var DeployStaticContent|null
40+
*/
41+
private ?DeployStaticContent $deployService = null;
42+
43+
/**
44+
* @var HashGenerator|null
45+
*/
46+
private ?HashGenerator $hashGenerator = null;
47+
48+
/**
49+
* @var array
50+
*/
51+
private $options = [
52+
Options::NO_JAVASCRIPT => false,
53+
Options::NO_JS_BUNDLE => false,
54+
Options::NO_CSS => false,
55+
Options::NO_IMAGES => false,
56+
Options::NO_FONTS => false,
57+
Options::NO_HTML => false,
58+
Options::NO_MISC => false,
59+
Options::NO_HTML_MINIFY => false,
60+
Options::AREA => ['frontend'],
61+
Options::EXCLUDE_AREA => ['none'],
62+
Options::THEME => ['Magento/zoom1', 'Magento/zoom2', 'Magento/zoom3'],
63+
Options::EXCLUDE_THEME => ['Magento/backend', 'Magento/luma'],
64+
Options::LANGUAGE => ['en_US'],
65+
Options::EXCLUDE_LANGUAGE => ['none'],
66+
Options::JOBS_AMOUNT => 0,
67+
Options::SYMLINK_LOCALE => false,
68+
Options::NO_PARENT => false,
69+
Options::STRATEGY => DeployStrategyFactory::DEPLOY_STRATEGY_QUICK,
70+
];
71+
72+
/**
73+
* @var SubresourceIntegrityRepository|null
74+
*/
75+
private ?SubresourceIntegrityRepository $integrityRepository = null;
76+
77+
/**
78+
* @var SubresourceIntegrityRepositoryPool|null
79+
*/
80+
private ?SubresourceIntegrityRepositoryPool $integrityRepositoryPool = null;
81+
82+
/**
83+
* Initialize Dependencies
84+
*
85+
* @return void
86+
* @throws FileSystemException
87+
*/
88+
protected function setUp(): void
89+
{
90+
$objectManager = Bootstrap::getObjectManager();
91+
92+
$this->filesystem = $objectManager->get(Filesystem::class);
93+
$this->staticDir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
94+
$this->integrityRepository = $objectManager->get(SubresourceIntegrityRepository::class);
95+
$this->integrityRepositoryPool = $objectManager->get(SubresourceIntegrityRepositoryPool::class);
96+
$this->hashGenerator = $objectManager->get(HashGenerator::class);
97+
98+
$logger = $objectManager->get(\Psr\Log\LoggerInterface::class);
99+
$this->deployService = $objectManager->create(
100+
DeployStaticContent::class,
101+
['logger' => $logger]
102+
);
103+
$this->filesystem->getDirectoryWrite(DirectoryList::PUB)->delete(DirectoryList::STATIC_VIEW);
104+
$this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR)->delete(DirectoryList::TMP_MATERIALIZATION_DIR);
105+
}
106+
107+
/**
108+
* Integration test to check caches save hash value after static content deploy
109+
*
110+
* @magentoDataFixture Magento/Deploy/_files/theme.php
111+
* @covers \Magento\Csp\Model\SubresourceIntegrityRepositoryPool::get
112+
* @covers \Magento\Csp\Model\SubresourceIntegrityRepository::getAll
113+
* @covers \Magento\Csp\Model\SubresourceIntegrityRepository::getByPath
114+
* @covers \Magento\Csp\Model\SubresourceIntegrity\HashGenerator::generate
115+
* @return void
116+
* @throws LocalizedException
117+
*/
118+
public function testDeploy(): void
119+
{
120+
$this->assertEmpty($this->integrityRepository->getAll());
121+
$this->deployService->deploy($this->options);
122+
$repository = $this->integrityRepositoryPool->get('frontend');
123+
$this->assertNotEmpty($repository->getAll());
124+
$integrity = $repository->getByPath('frontend/Magento/zoom3/en_US/js/file3.js');
125+
$filePath = $this->staticDir->getAbsolutePath('frontend/Magento/zoom3/en_US/js/file3.js');
126+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
127+
$fileContents = file_get_contents($filePath);
128+
$hash = $this->hashGenerator->generate($fileContents);
129+
$this->assertEquals($hash, $integrity->getHash());
130+
}
131+
}

dev/tests/static/testsuite/Magento/Test/GraphQl/LiveCodeTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Magento\Test\GraphQl;
99

10+
use Exception;
11+
use Magento\Framework\App\Utility\Files;
1012
use Magento\TestFramework\CodingStandard\Tool\CodeSniffer;
1113
use Magento\TestFramework\CodingStandard\Tool\CodeSniffer\Wrapper;
1214
use Magento\Test\Php\LiveCodeTest as PHPCodeTest;
@@ -86,6 +88,7 @@ public function testModulesRequireGraphQLChange(): void
8688
* returns a array with the list of graphql modules which require changes
8789
*
8890
* @return array
91+
* @throws Exception
8992
*/
9093
private static function getModulesRequiringGraphQLChange(): array
9194
{
@@ -95,10 +98,11 @@ private static function getModulesRequiringGraphQLChange(): array
9598
'',
9699
'/_files/whitelist/graphql.txt'
97100
);
101+
$fileList = self::filterFiles($whitelistFiles);
98102

99103
$updatedGraphQlModules = [];
100104
$requireGraphQLChanges = [];
101-
foreach ($whitelistFiles as $whitelistFile) {
105+
foreach ($fileList as $whitelistFile) {
102106
$moduleName = self::getModuleName($whitelistFile);
103107

104108
if (!$moduleName) {
@@ -209,4 +213,22 @@ private static function filterUiComponents(array $uIComponentClasses, string $mo
209213
}
210214
return $frontendUIComponent;
211215
}
216+
217+
/**
218+
* Skip files not requiring graphql side changes
219+
*
220+
* @param array $fileList
221+
* @return array
222+
* @throws Exception
223+
*/
224+
private static function filterFiles(array $fileList): array
225+
{
226+
$denyListFiles = Files::init()->readLists(__DIR__ . '/_files/denylist/*.txt');
227+
228+
$filter = function ($value) use ($denyListFiles) {
229+
return !in_array($value, $denyListFiles);
230+
};
231+
232+
return array_filter($fileList, $filter);
233+
}
212234
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
app/code/Magento/Csp/Block/Sri/Hashes.php

0 commit comments

Comments
 (0)