Skip to content

Commit 8658b46

Browse files
Merge pull request #6246 from magento-borg/2.3-bugfixes-101620
[CIA] Bugfixes
2 parents 70297dd + 07660fb commit 8658b46

File tree

6 files changed

+396
-16
lines changed

6 files changed

+396
-16
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Csp\Model\Collector\CspWhitelistXml;
10+
11+
use Magento\Framework\Config\FileResolverInterface;
12+
use Magento\Framework\Filesystem;
13+
use Magento\Framework\View\Design\ThemeInterface;
14+
use Magento\Framework\View\DesignInterface;
15+
use Magento\Framework\View\Design\Theme\CustomizationInterface;
16+
use Magento\Framework\View\Design\Theme\CustomizationInterfaceFactory;
17+
use Magento\Framework\App\Filesystem\DirectoryList;
18+
use Magento\Framework\Filesystem\Directory\ReadInterface as DirectoryRead;
19+
use Magento\Framework\Config\CompositeFileIteratorFactory;
20+
21+
/**
22+
* Combines configuration files from both modules and current theme.
23+
*/
24+
class FileResolver implements FileResolverInterface
25+
{
26+
/**
27+
* @var FileResolverInterface
28+
*/
29+
private $moduleFileResolver;
30+
31+
/**
32+
* @var ThemeInterface
33+
*/
34+
private $theme;
35+
36+
/**
37+
* @var CustomizationInterfaceFactory
38+
*/
39+
private $themeInfoFactory;
40+
41+
/**
42+
* @var DirectoryRead
43+
*/
44+
private $rootDir;
45+
46+
/**
47+
* @var CompositeFileIteratorFactory
48+
*/
49+
private $iteratorFactory;
50+
51+
/**
52+
* @param FileResolverInterface $moduleFileResolver
53+
* @param DesignInterface $design
54+
* @param CustomizationInterfaceFactory $customizationFactory
55+
* @param Filesystem $filesystem
56+
* @param CompositeFileIteratorFactory $iteratorFactory
57+
*/
58+
public function __construct(
59+
FileResolverInterface $moduleFileResolver,
60+
DesignInterface $design,
61+
CustomizationInterfaceFactory $customizationFactory,
62+
Filesystem $filesystem,
63+
CompositeFileIteratorFactory $iteratorFactory
64+
) {
65+
$this->moduleFileResolver = $moduleFileResolver;
66+
$this->theme = $design->getDesignTheme();
67+
$this->themeInfoFactory = $customizationFactory;
68+
$this->rootDir = $filesystem->getDirectoryRead(DirectoryList::ROOT);
69+
$this->iteratorFactory = $iteratorFactory;
70+
}
71+
72+
/**
73+
* @inheritDoc
74+
*/
75+
public function get($filename, $scope)
76+
{
77+
$configs = $this->moduleFileResolver->get($filename, $scope);
78+
if ($scope === 'global') {
79+
$files = [];
80+
$theme = $this->theme;
81+
while ($theme) {
82+
/** @var CustomizationInterface $info */
83+
$info = $this->themeInfoFactory->create(['theme' => $theme]);
84+
$file = $info->getThemeFilesPath() .'/etc/' .$filename;
85+
if ($this->rootDir->isExist($file)) {
86+
$files[] = $file;
87+
}
88+
$theme = $theme->getParentTheme();
89+
}
90+
$configs = $this->iteratorFactory->create(
91+
['paths' => array_reverse($files), 'existingIterator' => $configs]
92+
);
93+
}
94+
95+
return $configs;
96+
}
97+
}

app/code/Magento/Csp/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
<arguments>
4747
<argument name="converter" xsi:type="object">Magento\Csp\Model\Collector\CspWhitelistXml\Converter</argument>
4848
<argument name="schemaLocator" xsi:type="object">Magento\Csp\Model\Collector\CspWhitelistXml\SchemaLocator</argument>
49+
<argument name="fileResolver" xsi:type="object">Magento\Csp\Model\Collector\CspWhitelistXml\FileResolver</argument>
4950
<argument name="fileName" xsi:type="string">csp_whitelist.xml</argument>
5051
</arguments>
5152
</type>

dev/tests/integration/testsuite/Magento/Framework/Filesystem/Directory/WriteTest.php

Lines changed: 99 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@
77
*/
88
namespace Magento\Framework\Filesystem\Directory;
99

10+
use Magento\Framework\Exception\FileSystemException;
1011
use Magento\Framework\Exception\ValidatorException;
1112
use Magento\Framework\Filesystem\DriverPool;
1213
use Magento\TestFramework\Helper\Bootstrap;
14+
use PHPUnit\Framework\TestCase;
1315

1416
/**
1517
* Class ReadTest
1618
* Test for Magento\Framework\Filesystem\Directory\Read class
1719
*/
18-
class WriteTest extends \PHPUnit\Framework\TestCase
20+
class WriteTest extends TestCase
1921
{
2022
/**
2123
* Test data to be cleaned
@@ -41,6 +43,8 @@ public function testInstance()
4143
* @param string $basePath
4244
* @param int $permissions
4345
* @param string $path
46+
* @throws FileSystemException
47+
* @throws ValidatorException
4448
*/
4549
public function testCreate($basePath, $permissions, $path)
4650
{
@@ -64,6 +68,11 @@ public function createProvider()
6468
];
6569
}
6670

71+
/**
72+
* Test for create outside
73+
*
74+
* @throws FileSystemException
75+
*/
6776
public function testCreateOutside()
6877
{
6978
$exceptions = 0;
@@ -91,6 +100,8 @@ public function testCreateOutside()
91100
*
92101
* @dataProvider deleteProvider
93102
* @param string $path
103+
* @throws FileSystemException
104+
* @throws ValidatorException
94105
*/
95106
public function testDelete($path)
96107
{
@@ -111,6 +122,11 @@ public function deleteProvider()
111122
return [['subdir'], ['subdir/subsubdir']];
112123
}
113124

125+
/**
126+
* Test for delete outside
127+
*
128+
* @throws FileSystemException
129+
*/
114130
public function testDeleteOutside()
115131
{
116132
$exceptions = 0;
@@ -141,6 +157,8 @@ public function testDeleteOutside()
141157
* @param int $permissions
142158
* @param string $name
143159
* @param string $newName
160+
* @throws FileSystemException
161+
* @throws ValidatorException
144162
*/
145163
public function testRename($basePath, $permissions, $name, $newName)
146164
{
@@ -164,6 +182,11 @@ public function renameProvider()
164182
return [['newDir1', 0777, 'first_name.txt', 'second_name.txt']];
165183
}
166184

185+
/**
186+
* Test for rename outside
187+
*
188+
* @throws FileSystemException
189+
*/
167190
public function testRenameOutside()
168191
{
169192
$exceptions = 0;
@@ -198,6 +221,8 @@ public function testRenameOutside()
198221
* @param int $permission
199222
* @param string $name
200223
* @param string $newName
224+
* @throws FileSystemException
225+
* @throws ValidatorException
201226
*/
202227
public function testRenameTargetDir($firstDir, $secondDir, $permission, $name, $newName)
203228
{
@@ -231,6 +256,8 @@ public function renameTargetDirProvider()
231256
* @param int $permissions
232257
* @param string $name
233258
* @param string $newName
259+
* @throws FileSystemException
260+
* @throws ValidatorException
234261
*/
235262
public function testCopy($basePath, $permissions, $name, $newName)
236263
{
@@ -255,6 +282,12 @@ public function copyProvider()
255282
];
256283
}
257284

285+
/**
286+
* Test for copy outside
287+
*
288+
* @throws FileSystemException
289+
* @throws ValidatorException
290+
*/
258291
public function testCopyOutside()
259292
{
260293
$exceptions = 0;
@@ -298,6 +331,8 @@ public function testCopyOutside()
298331
* @param int $permission
299332
* @param string $name
300333
* @param string $newName
334+
* @throws FileSystemException
335+
* @throws ValidatorException
301336
*/
302337
public function testCopyTargetDir($firstDir, $secondDir, $permission, $name, $newName)
303338
{
@@ -327,6 +362,8 @@ public function copyTargetDirProvider()
327362

328363
/**
329364
* Test for changePermissions method
365+
*
366+
* @throws FileSystemException|ValidatorException
330367
*/
331368
public function testChangePermissions()
332369
{
@@ -335,6 +372,11 @@ public function testChangePermissions()
335372
$this->assertTrue($directory->changePermissions('test_directory', 0644));
336373
}
337374

375+
/**
376+
* Test change permissions outside
377+
*
378+
* @throws FileSystemException
379+
*/
338380
public function testChangePermissionsOutside()
339381
{
340382
$exceptions = 0;
@@ -359,6 +401,8 @@ public function testChangePermissionsOutside()
359401

360402
/**
361403
* Test for changePermissionsRecursively method
404+
*
405+
* @throws FileSystemException|ValidatorException
362406
*/
363407
public function testChangePermissionsRecursively()
364408
{
@@ -370,6 +414,11 @@ public function testChangePermissionsRecursively()
370414
$this->assertTrue($directory->changePermissionsRecursively('test_directory', 0777, 0644));
371415
}
372416

417+
/**
418+
* Test writable for outside
419+
*
420+
* @throws FileSystemException
421+
*/
373422
public function testChangePermissionsRecursivelyOutside()
374423
{
375424
$exceptions = 0;
@@ -400,6 +449,8 @@ public function testChangePermissionsRecursivelyOutside()
400449
* @param int $permissions
401450
* @param string $path
402451
* @param int $time
452+
* @throws FileSystemException
453+
* @throws ValidatorException
403454
*/
404455
public function testTouch($basePath, $permissions, $path, $time)
405456
{
@@ -422,6 +473,11 @@ public function touchProvider()
422473
];
423474
}
424475

476+
/**
477+
* Test touch for outside
478+
*
479+
* @throws FileSystemException
480+
*/
425481
public function testTouchOutside()
426482
{
427483
$exceptions = 0;
@@ -446,6 +502,8 @@ public function testTouchOutside()
446502

447503
/**
448504
* Test isWritable method
505+
*
506+
* @throws FileSystemException|ValidatorException
449507
*/
450508
public function testIsWritable()
451509
{
@@ -455,6 +513,11 @@ public function testIsWritable()
455513
$this->assertTrue($directory->isWritable('bar'));
456514
}
457515

516+
/**
517+
* Test writable for outside
518+
*
519+
* @throws FileSystemException
520+
*/
458521
public function testIsWritableOutside()
459522
{
460523
$exceptions = 0;
@@ -485,6 +548,8 @@ public function testIsWritableOutside()
485548
* @param int $permissions
486549
* @param string $path
487550
* @param string $mode
551+
* @throws FileSystemException
552+
* @throws ValidatorException
488553
*/
489554
public function testOpenFile($basePath, $permissions, $path, $mode)
490555
{
@@ -507,6 +572,11 @@ public function openFileProvider()
507572
];
508573
}
509574

575+
/**
576+
* Test open file outside
577+
*
578+
* @throws FileSystemException
579+
*/
510580
public function testOpenFileOutside()
511581
{
512582
$exceptions = 0;
@@ -536,6 +606,8 @@ public function testOpenFileOutside()
536606
* @param string $path
537607
* @param string $content
538608
* @param string $extraContent
609+
* @throws FileSystemException
610+
* @throws ValidatorException
539611
*/
540612
public function testWriteFile($path, $content, $extraContent)
541613
{
@@ -553,6 +625,8 @@ public function testWriteFile($path, $content, $extraContent)
553625
* @param string $path
554626
* @param string $content
555627
* @param string $extraContent
628+
* @throws FileSystemException
629+
* @throws ValidatorException
556630
*/
557631
public function testWriteFileAppend($path, $content, $extraContent)
558632
{
@@ -573,6 +647,11 @@ public function writeFileProvider()
573647
return [['file1', '123', '456'], ['folder1/file1', '123', '456']];
574648
}
575649

650+
/**
651+
* Test writing file to outside
652+
*
653+
* @throws FileSystemException
654+
*/
576655
public function testWriteFileOutside()
577656
{
578657
$exceptions = 0;
@@ -595,8 +674,25 @@ public function testWriteFileOutside()
595674
$this->assertEquals(3, $exceptions);
596675
}
597676

677+
/**
678+
* Test for invalid delete path
679+
*
680+
* @throws ValidatorException
681+
*/
682+
public function testInvalidDeletePath()
683+
{
684+
$this->expectException(FileSystemException::class);
685+
$directory = $this->getDirectoryInstance('newDir', 0777);
686+
$invalidPath = 'invalidPath/../';
687+
$directory->create($invalidPath);
688+
$directory->delete($invalidPath);
689+
}
690+
598691
/**
599692
* Tear down
693+
*
694+
* @throws FileSystemException
695+
* @throws ValidatorException
600696
*/
601697
public function tearDown()
602698
{
@@ -620,8 +716,8 @@ private function getDirectoryInstance($path, $permissions)
620716
{
621717
$fullPath = __DIR__ . '/../_files/' . $path;
622718
$objectManager = Bootstrap::getObjectManager();
623-
/** @var \Magento\Framework\Filesystem\Directory\WriteFactory $directoryFactory */
624-
$directoryFactory = $objectManager->create(\Magento\Framework\Filesystem\Directory\WriteFactory::class);
719+
/** @var WriteFactory $directoryFactory */
720+
$directoryFactory = $objectManager->create(WriteFactory::class);
625721
$directory = $directoryFactory->create($fullPath, DriverPool::FILE, $permissions);
626722
$this->testDirectories[] = $directory;
627723
return $directory;

0 commit comments

Comments
 (0)