|
| 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\Test\Unit\Model\Mode; |
| 10 | + |
| 11 | +use Magento\Csp\Model\Mode\ConfigManager; |
| 12 | +use Magento\Csp\Model\Mode\Data\ModeConfigured; |
| 13 | +use Magento\Framework\App\Area; |
| 14 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 15 | +use Magento\Framework\App\State; |
| 16 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 17 | +use Magento\Store\Model\Store; |
| 18 | +use PHPUnit\Framework\MockObject\MockObject; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | + |
| 21 | +/** |
| 22 | + * Unit Test for \Magento\Csp\Model\Mode\ConfigManager |
| 23 | + */ |
| 24 | +class ConfigManagerTest extends TestCase |
| 25 | +{ |
| 26 | + const STUB_REPORT_ONLY = true; |
| 27 | + const STUB_AREA_CODE_OTHER = 'other'; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var MockObject|ScopeConfigInterface |
| 31 | + */ |
| 32 | + private $configMock; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var MockObject|Store |
| 36 | + */ |
| 37 | + private $storeModelMock; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var MockObject|State |
| 41 | + */ |
| 42 | + private $stateMock; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var ConfigManager |
| 46 | + */ |
| 47 | + private $model; |
| 48 | + |
| 49 | + /** |
| 50 | + * @inheritDoc |
| 51 | + */ |
| 52 | + protected function setUp() |
| 53 | + { |
| 54 | + $this->configMock = $this->createMock(ScopeConfigInterface::class); |
| 55 | + $this->storeModelMock = $this->createMock(Store::class); |
| 56 | + $this->stateMock = $this->createMock(State::class); |
| 57 | + |
| 58 | + $objectManagerHelper = new ObjectManagerHelper($this); |
| 59 | + $this->model = $objectManagerHelper->getObject( |
| 60 | + ConfigManager::class, |
| 61 | + [ |
| 62 | + 'config' => $this->configMock, |
| 63 | + 'storeModel' => $this->storeModelMock, |
| 64 | + 'state' => $this->stateMock |
| 65 | + ] |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Test case with correct Area codes. |
| 71 | + * |
| 72 | + * @param string $area |
| 73 | + * @param string $pathReportOnly |
| 74 | + * @param string $pathReportUri |
| 75 | + * @dataProvider dataProviderGetConfiguredWithCorrectArea |
| 76 | + */ |
| 77 | + public function testGetConfiguredWithCorrectArea(string $area, string $pathReportOnly, string $pathReportUri) |
| 78 | + { |
| 79 | + $this->stateMock->expects($this->once())->method('getAreaCode')->willReturn($area); |
| 80 | + |
| 81 | + $this->configMock->expects($this->once())->method('getValue')->with($pathReportUri); |
| 82 | + $this->configMock->expects($this->once()) |
| 83 | + ->method('isSetFlag') |
| 84 | + ->with($pathReportOnly) |
| 85 | + ->willReturn(self::STUB_REPORT_ONLY); |
| 86 | + |
| 87 | + $this->assertInstanceOf(ModeConfigured::class, $this->model->getConfigured()); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Data Provider with appropriate areas. |
| 92 | + * |
| 93 | + * @return array |
| 94 | + */ |
| 95 | + public function dataProviderGetConfiguredWithCorrectArea(): array |
| 96 | + { |
| 97 | + return [ |
| 98 | + [ |
| 99 | + 'area' => Area::AREA_ADMINHTML, |
| 100 | + 'pathReportOnly' => 'csp/mode/admin/report_only', |
| 101 | + 'pathReportUri' => 'csp/mode/admin/report_uri' |
| 102 | + ], |
| 103 | + [ |
| 104 | + 'area' => Area::AREA_FRONTEND, |
| 105 | + 'pathReportOnly' => 'csp/mode/storefront/report_only', |
| 106 | + 'pathReportUri' => 'csp/mode/storefront/report_uri' |
| 107 | + ] |
| 108 | + ]; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Test case with an inappropriate Area code. |
| 113 | + */ |
| 114 | + public function testGetConfiguredWithWrongArea() |
| 115 | + { |
| 116 | + $this->stateMock->expects($this->once()) |
| 117 | + ->method('getAreaCode') |
| 118 | + ->willReturn(self::STUB_AREA_CODE_OTHER); |
| 119 | + |
| 120 | + $this->configMock->expects($this->never())->method('isSetFlag'); |
| 121 | + $this->configMock->expects($this->never())->method('getValue'); |
| 122 | + $this->expectException(\RuntimeException::class); |
| 123 | + $this->expectExceptionMessage('CSP can only be configured for storefront or admin area'); |
| 124 | + |
| 125 | + $this->model->getConfigured(); |
| 126 | + } |
| 127 | +} |
0 commit comments