|
| 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 | + ->getMock(); |
| 75 | + |
| 76 | + $this->designMock = $this->getMockBuilder(DesignInterface::class) |
| 77 | + ->disableOriginalConstructor() |
| 78 | + ->getMock(); |
| 79 | + |
| 80 | + $this->themeInterFaceMock = $this->getMockBuilder(ThemeInterface::class) |
| 81 | + ->disableOriginalConstructor() |
| 82 | + ->getMock(); |
| 83 | + |
| 84 | + $this->designMock->expects($this->once()) |
| 85 | + ->method('getDesignTheme') |
| 86 | + ->willReturn($this->themeInterFaceMock); |
| 87 | + |
| 88 | + $this->customizationFactoryMock = $this->getMockBuilder(CustomizationInterfaceFactory::class) |
| 89 | + ->disableOriginalConstructor() |
| 90 | + ->onlyMethods(['create']) |
| 91 | + ->getMock(); |
| 92 | + |
| 93 | + $this->customizationInterfaceMock = $this->getMockBuilder(CustomizationInterface::class) |
| 94 | + ->disableOriginalConstructor() |
| 95 | + ->getMock(); |
| 96 | + |
| 97 | + $this->filesystemMock = $this->createPartialMock(Filesystem::class, ['getDirectoryRead']); |
| 98 | + |
| 99 | + $this->readInterfaceMock = $this->getMockBuilder(ReadInterface::class) |
| 100 | + ->disableOriginalConstructor() |
| 101 | + ->getMock(); |
| 102 | + |
| 103 | + $this->filesystemMock->expects($this->once()) |
| 104 | + ->method('getDirectoryRead') |
| 105 | + ->with(DirectoryList::ROOT) |
| 106 | + ->willReturn($this->readInterfaceMock); |
| 107 | + |
| 108 | + $this->iteratorFactoryMock = $this->getMockBuilder(CompositeFileIteratorFactory::class) |
| 109 | + ->disableOriginalConstructor() |
| 110 | + ->getMock(); |
| 111 | + |
| 112 | + $this->model = new FileResolver( |
| 113 | + $this->moduleFileResolverMock, |
| 114 | + $this->designMock, |
| 115 | + $this->customizationFactoryMock, |
| 116 | + $this->filesystemMock, |
| 117 | + $this->iteratorFactoryMock |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Test for get method with frontend scope. |
| 123 | + * |
| 124 | + * @param string $scope |
| 125 | + * @param string $fileName |
| 126 | + * @param array $fileList |
| 127 | + * @param string $themeFilesPath |
| 128 | + * |
| 129 | + * @return void |
| 130 | + * @dataProvider providerGetFrontend |
| 131 | + */ |
| 132 | + public function testGetFrontend(string $scope, string $fileName, array $fileList, string $themeFilesPath): void |
| 133 | + { |
| 134 | + $this->moduleFileResolverMock->expects($this->once()) |
| 135 | + ->method('get') |
| 136 | + ->with($fileName, $scope) |
| 137 | + ->willReturn($fileList); |
| 138 | + |
| 139 | + $this->customizationFactoryMock->expects($this->any()) |
| 140 | + ->method('create') |
| 141 | + ->with(['theme' => $this->themeInterFaceMock]) |
| 142 | + ->willReturn($this->customizationInterfaceMock); |
| 143 | + |
| 144 | + $this->customizationInterfaceMock->expects($this->once()) |
| 145 | + ->method('getThemeFilesPath') |
| 146 | + ->willReturn($themeFilesPath); |
| 147 | + |
| 148 | + $this->readInterfaceMock->expects($this->once()) |
| 149 | + ->method('isExist') |
| 150 | + ->with($themeFilesPath.'/etc/'.$fileName) |
| 151 | + ->willReturn(true); |
| 152 | + |
| 153 | + $this->iteratorFactoryMock->expects($this->once()) |
| 154 | + ->method('create') |
| 155 | + ->with( |
| 156 | + [ |
| 157 | + 'paths' => array_reverse([$themeFilesPath.'/etc/'.$fileName]), |
| 158 | + 'existingIterator' => $fileList |
| 159 | + ] |
| 160 | + ) |
| 161 | + ->willReturn($fileList); |
| 162 | + |
| 163 | + $this->assertEquals($fileList, $this->model->get($fileName, $scope)); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Test for get method with global scope. |
| 168 | + * |
| 169 | + * @param string $scope |
| 170 | + * @param string $fileName |
| 171 | + * @param array $fileList |
| 172 | + * |
| 173 | + * @return void |
| 174 | + * @dataProvider providerGetGlobal |
| 175 | + */ |
| 176 | + public function testGetGlobal(string $scope, string $fileName, array $fileList): void |
| 177 | + { |
| 178 | + $this->moduleFileResolverMock->expects($this->once()) |
| 179 | + ->method('get') |
| 180 | + ->with($fileName, $scope) |
| 181 | + ->willReturn($fileList); |
| 182 | + $this->assertEquals($fileList, $this->model->get($fileName, $scope)); |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Data provider for get global scope tests. |
| 187 | + * |
| 188 | + * @return array |
| 189 | + */ |
| 190 | + public static function providerGetGlobal(): array |
| 191 | + { |
| 192 | + return [ |
| 193 | + [ |
| 194 | + 'global', |
| 195 | + 'csp_whitelist.xml', |
| 196 | + ['anyvendor/anymodule/etc/csp_whitelist.xml'] |
| 197 | + ] |
| 198 | + ]; |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Data provider for get frontend & adminhtml scope tests. |
| 203 | + * |
| 204 | + * @return array |
| 205 | + */ |
| 206 | + public static function providerGetFrontend(): array |
| 207 | + { |
| 208 | + return [ |
| 209 | + [ |
| 210 | + 'frontend', |
| 211 | + 'csp_whitelist.xml', |
| 212 | + ['themevendor/theme/etc/csp_whitelist.xml'], |
| 213 | + 'themevendor/theme' |
| 214 | + ], |
| 215 | + [ |
| 216 | + 'adminhtml', |
| 217 | + 'csp_whitelist.xml', |
| 218 | + ['adminthemevendor/admintheme/etc/csp_whitelist.xml'], |
| 219 | + 'adminthemevendor/admintheme' |
| 220 | + ] |
| 221 | + ]; |
| 222 | + } |
| 223 | +} |
0 commit comments