Skip to content

Commit 967e424

Browse files
authored
ENGCOM-6725: [Csp] Covering the model classes by Unit Tests #26558
2 parents a4f0e96 + 2186c50 commit 967e424

File tree

2 files changed

+199
-0
lines changed

2 files changed

+199
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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;
17+
use Magento\Store\Model\Store;
18+
use PHPUnit\Framework\MockObject\MockObject;
19+
use PHPUnit\Framework\TestCase;
20+
use RuntimeException;
21+
22+
/**
23+
* Test for \Magento\Csp\Model\Mode\ConfigManager
24+
*/
25+
class ConfigManagerTest extends TestCase
26+
{
27+
/**
28+
* @var ConfigManager
29+
*/
30+
private $model;
31+
32+
/**
33+
* @var ScopeConfigInterface|MockObject
34+
*/
35+
private $scopeConfigMock;
36+
37+
/**
38+
* @var Store|MockObject
39+
*/
40+
private $storeMock;
41+
42+
/**
43+
* @var State|MockObject
44+
*/
45+
private $stateMock;
46+
47+
/**
48+
* Set Up
49+
*/
50+
protected function setUp()
51+
{
52+
$objectManager = new ObjectManager($this);
53+
54+
$this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class);
55+
$this->storeMock = $this->createMock(Store::class);
56+
$this->stateMock = $this->createMock(State::class);
57+
58+
$this->model = $objectManager->getObject(
59+
ConfigManager::class,
60+
[
61+
'config' => $this->scopeConfigMock,
62+
'storeModel' => $this->storeMock,
63+
'state' => $this->stateMock
64+
]
65+
);
66+
}
67+
68+
/**
69+
* Test throwing an exception for non storefront or admin areas
70+
*
71+
* @return void
72+
*/
73+
public function testThrownExceptionForCrontabArea()
74+
{
75+
$this->stateMock->expects($this->any())
76+
->method('getAreaCode')
77+
->willReturn(Area::AREA_CRONTAB);
78+
79+
$this->expectExceptionMessage('CSP can only be configured for storefront or admin area');
80+
$this->expectException(RuntimeException::class);
81+
82+
$this->model->getConfigured();
83+
}
84+
85+
/**
86+
* Test returning the configured CSP for admin area
87+
*
88+
* @return void
89+
*/
90+
public function testConfiguredCSPForAdminArea()
91+
{
92+
$this->stateMock->expects($this->any())
93+
->method('getAreaCode')
94+
->willReturn(Area::AREA_ADMINHTML);
95+
$this->scopeConfigMock->expects($this->any())
96+
->method('isSetFlag')
97+
->willReturn(true);
98+
$this->scopeConfigMock->expects($this->any())
99+
->method('getValue')
100+
->willReturn('testReportUri');
101+
$result = $this->model->getConfigured();
102+
103+
$this->assertInstanceOf(ModeConfigured::class, $result);
104+
}
105+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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;
10+
11+
use Magento\Csp\Api\Data\PolicyInterface;
12+
use Magento\Csp\Model\Policy\Renderer\SimplePolicyHeaderRenderer;
13+
use Magento\Csp\Model\PolicyRendererPool;
14+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
15+
use PHPUnit\Framework\MockObject\MockObject;
16+
use PHPUnit\Framework\TestCase;
17+
use RuntimeException;
18+
19+
/**
20+
* Test for \Magento\Csp\Model\PolicyRendererPool
21+
*/
22+
class PolicyRendererPoolTest extends TestCase
23+
{
24+
private const STUB_POLICY_ID = 'header';
25+
26+
/**
27+
* @var PolicyRendererPool
28+
*/
29+
private $model;
30+
31+
/**
32+
* @var SimplePolicyHeaderRenderer|MockObject
33+
*/
34+
private $simplePolicyHeaderRendererMock;
35+
36+
/**
37+
* @var PolicyInterface|MockObject
38+
*/
39+
private $policyMock;
40+
41+
/**
42+
* Set Up
43+
*/
44+
protected function setUp()
45+
{
46+
$objectManager = new ObjectManager($this);
47+
$this->simplePolicyHeaderRendererMock = $this->createPartialMock(
48+
SimplePolicyHeaderRenderer::class,
49+
['canRender']
50+
);
51+
$this->policyMock = $this->createMock(PolicyInterface::class);
52+
53+
$this->model = $objectManager->getObject(
54+
PolicyRendererPool::class,
55+
[
56+
'renderers' => [
57+
$this->simplePolicyHeaderRendererMock
58+
]
59+
]
60+
);
61+
}
62+
63+
/**
64+
* Test throwing an exception for not found policy renders
65+
*
66+
* @return void
67+
*/
68+
public function testThrownExceptionForNotFoundPolicyRenders()
69+
{
70+
$this->policyMock->expects($this->any())
71+
->method('getId')
72+
->willReturn(static::STUB_POLICY_ID);
73+
74+
$this->expectExceptionMessage('Failed to find a renderer for policy');
75+
$this->expectException(RuntimeException::class);
76+
77+
$this->model->getRenderer($this->policyMock);
78+
}
79+
80+
/**
81+
* Test returning a renderer for the given policy
82+
*
83+
* @return void
84+
*/
85+
public function testReturningThePolicyRender()
86+
{
87+
$this->simplePolicyHeaderRendererMock->expects($this->any())
88+
->method('canRender')
89+
->with($this->policyMock)
90+
->willReturn(true);
91+
92+
$this->model->getRenderer($this->policyMock);
93+
}
94+
}

0 commit comments

Comments
 (0)