Skip to content

Commit d7c3b5b

Browse files
committed
Added Unit Test Cases for CSP FileResolver
1 parent 35ed4a0 commit d7c3b5b

File tree

1 file changed

+221
-0
lines changed

1 file changed

+221
-0
lines changed
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Csp\Test\Unit\Model\Collector\CspWhitelistXml;
10+
11+
use Magento\Framework\Filesystem;
12+
use Magento\Framework\View\Design\Theme\CustomizationInterface;
13+
use Magento\Framework\View\Design\ThemeInterface;
14+
use PHPUnit\Framework\TestCase;
15+
use Magento\Framework\Config\FileResolverInterface;
16+
use Magento\Csp\Model\Collector\CspWhitelistXml\FileResolver;
17+
use Magento\Framework\View\DesignInterface;
18+
use Magento\Framework\Config\CompositeFileIteratorFactory;
19+
use Magento\Framework\View\Design\Theme\CustomizationInterfaceFactory;
20+
use Magento\Framework\Filesystem\Directory\ReadInterface;
21+
use Magento\Framework\App\Filesystem\DirectoryList;
22+
23+
class FileResolverTest extends TestCase
24+
{
25+
/**
26+
* @var FileResolver
27+
*/
28+
private $model;
29+
30+
/**
31+
* @var FileResolverInterface
32+
*/
33+
private $moduleFileResolverMock;
34+
35+
/**
36+
* @var DesignInterface
37+
*/
38+
private $designMock;
39+
40+
/**
41+
* @var CustomizationInterfaceFactory
42+
*/
43+
private $customizationFactoryMock;
44+
45+
/**
46+
* @var Filesystem
47+
*/
48+
private $filesystemMock;
49+
50+
/**
51+
* @var CompositeFileIteratorFactory
52+
*/
53+
private $iteratorFactoryMock;
54+
55+
/**
56+
* @var ReadInterface
57+
*/
58+
private $readInterfaceMock;
59+
60+
/**
61+
* @var ThemeInterface
62+
*/
63+
private $themeInterFaceMock;
64+
65+
/**
66+
* @var CustomizationInterface
67+
*/
68+
private $customizationInterfaceMock;
69+
70+
protected function setUp(): void
71+
{
72+
$this->moduleFileResolverMock = $this->getMockBuilder(FileResolverInterface::class)
73+
->disableOriginalConstructor()
74+
->onlyMethods(['get'])
75+
->getMockForAbstractClass();
76+
77+
$this->designMock = $this->getMockBuilder(DesignInterface::class)
78+
->disableOriginalConstructor()
79+
->onlyMethods(['getDesignTheme'])
80+
->getMockForAbstractClass();
81+
82+
$this->themeInterFaceMock = $this->getMockBuilder(ThemeInterface::class)
83+
->getMockForAbstractClass();
84+
85+
$this->designMock->expects($this->once())
86+
->method('getDesignTheme')
87+
->willReturn($this->themeInterFaceMock);
88+
89+
$this->customizationFactoryMock = $this->getMockBuilder(CustomizationInterfaceFactory::class)
90+
->disableOriginalConstructor()
91+
->onlyMethods(['create'])
92+
->getMock();
93+
94+
$this->customizationInterfaceMock = $this->getMockBuilder(CustomizationInterface::class)
95+
->disableOriginalConstructor()
96+
->getMockForAbstractClass();
97+
98+
$this->filesystemMock = $this->createPartialMock(Filesystem::class, ['getDirectoryRead']);
99+
100+
$this->readInterfaceMock = $this->getMockBuilder(ReadInterface::class)
101+
->disableOriginalConstructor()
102+
->getMockForAbstractClass();
103+
104+
$this->filesystemMock->expects($this->once())
105+
->method('getDirectoryRead')
106+
->with(DirectoryList::ROOT)
107+
->willReturn($this->readInterfaceMock);
108+
109+
$this->iteratorFactoryMock = $this->getMockBuilder(CompositeFileIteratorFactory::class)
110+
->disableOriginalConstructor()
111+
->getMock();
112+
113+
$this->model = new FileResolver(
114+
$this->moduleFileResolverMock,
115+
$this->designMock,
116+
$this->customizationFactoryMock,
117+
$this->filesystemMock,
118+
$this->iteratorFactoryMock
119+
);
120+
}
121+
122+
/**
123+
* Test for get method with frontend scope.
124+
*
125+
* @param string $filename
126+
* @param array $fileList
127+
*
128+
* @return void
129+
* @dataProvider providerGetFrontend
130+
*/
131+
public function testGetFrontend(string $scope, string $filename, array $fileList, string $themeFilesPath): void
132+
{
133+
$this->moduleFileResolverMock->expects($this->once())
134+
->method('get')
135+
->with($filename, $scope)
136+
->willReturn($fileList);
137+
138+
$this->customizationFactoryMock->expects($this->any())
139+
->method('create')
140+
->with(['theme' => $this->themeInterFaceMock])
141+
->willReturn($this->customizationInterfaceMock);
142+
143+
$this->customizationInterfaceMock->expects($this->once())
144+
->method('getThemeFilesPath')
145+
->willReturn($themeFilesPath);
146+
147+
$this->readInterfaceMock->expects($this->once())
148+
->method('isExist')
149+
->with($themeFilesPath.'/etc/'.$filename)
150+
->willReturn(true);
151+
152+
$this->iteratorFactoryMock->expects($this->once())
153+
->method('create')
154+
->with(
155+
[
156+
'paths' => array_reverse([$themeFilesPath.'/etc/'.$filename]),
157+
'existingIterator' => $fileList
158+
]
159+
)
160+
->willReturn($fileList);
161+
162+
$this->assertEquals($fileList, $this->model->get($filename, $scope));
163+
}
164+
165+
/**
166+
* Test for get method with global scope.
167+
*
168+
* @param string $filename
169+
* @param array $fileList
170+
*
171+
* @return void
172+
* @dataProvider providerGetGlobal
173+
*/
174+
public function testGetGlobal(string $scope, string $fileName, array $fileList): void
175+
{
176+
$this->moduleFileResolverMock->expects($this->once())
177+
->method('get')
178+
->with($fileName, $scope)
179+
->willReturn($fileList);
180+
$this->assertEquals($fileList, $this->model->get($fileName, $scope));
181+
}
182+
183+
/**
184+
* Data provider for get glocal scope tests.
185+
*
186+
* @return array
187+
*/
188+
public static function providerGetGlobal(): array
189+
{
190+
return [
191+
[
192+
'global',
193+
'csp_whitelist.xml',
194+
['anyvendor/anymodule/etc/csp_whitelist.xml']
195+
]
196+
];
197+
}
198+
199+
/**
200+
* Data provider for get frontend & adminhtml scope tests.
201+
*
202+
* @return array
203+
*/
204+
public static function providerGetFrontend(): array
205+
{
206+
return [
207+
[
208+
'frontend',
209+
'csp_whitelist.xml',
210+
['themevendor/theme/etc/csp_whitelist.xml'],
211+
'themevendor/theme'
212+
],
213+
[
214+
'adminhtml',
215+
'csp_whitelist.xml',
216+
['adminthemevendor/admintheme/etc/csp_whitelist.xml'],
217+
'adminthemevendor/admintheme'
218+
]
219+
];
220+
}
221+
}

0 commit comments

Comments
 (0)