|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Csp\Test\Unit\Plugin; |
| 9 | + |
| 10 | +use Magento\Csp\Model\SubresourceIntegrity; |
| 11 | +use Magento\Csp\Model\SubresourceIntegrity\HashGenerator; |
| 12 | +use Magento\Csp\Model\SubresourceIntegrityFactory; |
| 13 | +use Magento\Csp\Model\SubresourceIntegrityRepository; |
| 14 | +use Magento\Csp\Model\SubresourceIntegrityRepositoryPool; |
| 15 | +use Magento\Framework\App\Area; |
| 16 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 17 | +use Magento\Framework\Filesystem; |
| 18 | +use Magento\Framework\Filesystem\Directory\WriteInterface; |
| 19 | +use Magento\Framework\View\Asset\File; |
| 20 | +use Magento\Framework\View\Asset\MergeStrategy\FileExists; |
| 21 | +use Magento\Csp\Plugin\GenerateMergedAssetIntegrity; |
| 22 | +use PHPUnit\Framework\MockObject\Exception; |
| 23 | +use PHPUnit\Framework\MockObject\MockObject; |
| 24 | +use PHPUnit\Framework\TestCase; |
| 25 | + |
| 26 | +class GenerateMergedAssetIntegrityTest extends TestCase |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @var SubresourceIntegrityRepositoryPool|MockObject |
| 30 | + */ |
| 31 | + private SubresourceIntegrityRepositoryPool $sourceIntegrityRepository; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var HashGenerator|MockObject |
| 35 | + */ |
| 36 | + private HashGenerator $hashGenerator; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var SubresourceIntegrityFactory|MockObject |
| 40 | + */ |
| 41 | + private SubresourceIntegrityFactory $integrityFactory; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var Filesystem|MockObject |
| 45 | + */ |
| 46 | + private Filesystem $filesystem; |
| 47 | + |
| 48 | + /** |
| 49 | + * @return void |
| 50 | + * @throws Exception |
| 51 | + */ |
| 52 | + protected function setUp(): void |
| 53 | + { |
| 54 | + $this->sourceIntegrityRepository = $this->createMock(SubresourceIntegrityRepositoryPool::class); |
| 55 | + $this->hashGenerator = $this->createMock(HashGenerator::class); |
| 56 | + $this->integrityFactory = $this->createMock(SubresourceIntegrityFactory::class); |
| 57 | + $this->filesystem = $this->createMock(Filesystem::class); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @return void |
| 62 | + * @throws Exception |
| 63 | + */ |
| 64 | + public function testAfterMerge(): void |
| 65 | + { |
| 66 | + $subject = $this->createMock(FileExists::class); |
| 67 | + $result = null; |
| 68 | + $assetsToMerge = []; |
| 69 | + $fileExtension = 'js'; |
| 70 | + $filePath = 'path/to/file.js'; |
| 71 | + $hash = '1234567890abcdef'; |
| 72 | + $fileContent = 'some content'; |
| 73 | + $resultAsset = $this->createMock(File::class); |
| 74 | + $resultAsset->expects($this->once())->method('getContentType')->willReturn($fileExtension); |
| 75 | + $resultAsset->expects($this->exactly(2)) |
| 76 | + ->method('getRelativeSourceFilePath') |
| 77 | + ->willReturn($filePath); |
| 78 | + $pubStaticDir = $this->createMock(WriteInterface::class); |
| 79 | + $pubStaticDir->expects($this->once())->method('readFile')->with($filePath)->willReturn($fileContent); |
| 80 | + $this->filesystem->expects($this->once()) |
| 81 | + ->method('getDirectoryWrite') |
| 82 | + ->with(DirectoryList::STATIC_VIEW) |
| 83 | + ->willReturn($pubStaticDir); |
| 84 | + $this->hashGenerator->expects($this->once())->method('generate')->with($fileContent)->willReturn($hash); |
| 85 | + $integrity = $this->createMock(SubresourceIntegrity::class); |
| 86 | + $this->integrityFactory->expects($this->once()) |
| 87 | + ->method('create')->with([ |
| 88 | + 'data' => [ |
| 89 | + 'hash' => $hash, |
| 90 | + 'path' => $filePath |
| 91 | + ] |
| 92 | + ])->willReturn($integrity); |
| 93 | + $repository = $this->createMock(SubresourceIntegrityRepository::class); |
| 94 | + $this->sourceIntegrityRepository->expects($this->once())->method('get') |
| 95 | + ->with(Area::AREA_FRONTEND) |
| 96 | + ->willReturn($repository); |
| 97 | + $repository->expects($this->once())->method('save')->with($integrity); |
| 98 | + |
| 99 | + $plugin = new GenerateMergedAssetIntegrity( |
| 100 | + $this->sourceIntegrityRepository, |
| 101 | + $this->hashGenerator, |
| 102 | + $this->integrityFactory, |
| 103 | + $this->filesystem |
| 104 | + ); |
| 105 | + $plugin->afterMerge($subject, $result, $assetsToMerge, $resultAsset); |
| 106 | + } |
| 107 | +} |
0 commit comments