Skip to content

Commit 0ca95ed

Browse files
committed
AC-15049 : [CE] PHPUnit 12: Upgrade Security & Authentication related test cases
1 parent 6b7b3df commit 0ca95ed

File tree

30 files changed

+281
-316
lines changed

30 files changed

+281
-316
lines changed

app/code/Magento/Authorization/Test/Unit/Model/Acl/AclRetrieverTest.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use Magento\Framework\Acl\Builder;
2121
use Magento\Framework\Acl\Role\CurrentRoleContext;
2222
use Magento\Framework\Exception\AuthorizationException;
23-
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
23+
use Magento\Framework\TestFramework\Unit\Helper\MockCreationTrait;
2424
use PHPUnit\Framework\MockObject\MockObject;
2525
use PHPUnit\Framework\TestCase;
2626
use Psr\Log\LoggerInterface;
@@ -32,6 +32,8 @@
3232
*/
3333
class AclRetrieverTest extends TestCase
3434
{
35+
use MockCreationTrait;
36+
3537
/**
3638
* @var AclRetriever
3739
*/
@@ -42,14 +44,8 @@ class AclRetrieverTest extends TestCase
4244
*/
4345
private $roleMock;
4446

45-
/**
46-
* @var ObjectManagerHelper
47-
*/
48-
private $objectManagerHelper;
49-
5047
protected function setUp(): void
5148
{
52-
$this->objectManagerHelper = new ObjectManagerHelper($this);
5349
$this->aclRetriever = $this->createAclRetriever();
5450
}
5551

@@ -128,7 +124,7 @@ protected function createAclRetriever()
128124
/**
129125
* @var Rules|MockObject $rulesMock1
130126
*/
131-
$rulesMock1 = $this->objectManagerHelper->createPartialMockWithReflection(
127+
$rulesMock1 = $this->createPartialMockWithReflection(
132128
Rules::class,
133129
['getResourceId', '__wakeup']
134130
);
@@ -138,7 +134,7 @@ protected function createAclRetriever()
138134
/**
139135
* @var Rules|MockObject $rulesMock2
140136
*/
141-
$rulesMock2 = $this->objectManagerHelper->createPartialMockWithReflection(
137+
$rulesMock2 = $this->createPartialMockWithReflection(
142138
Rules::class,
143139
['getResourceId', '__wakeup']
144140
);
@@ -193,7 +189,7 @@ protected function createAclRetriever()
193189
public function testResetAclWhenRoleChanges(): void
194190
{
195191
// Set up rules collection
196-
$rulesMock = $this->objectManagerHelper->createPartialMockWithReflection(
192+
$rulesMock = $this->createPartialMockWithReflection(
197193
Rules::class,
198194
['getResourceId', '__wakeup']
199195
);

app/code/Magento/Authorization/Test/Unit/Model/Acl/Loader/MissingDeclineRuleTest.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Magento\Framework\Acl\RootResource;
1414
use Magento\Framework\App\ResourceConnection;
1515
use Magento\Framework\Serialize\Serializer\Json;
16-
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
16+
use Magento\Framework\TestFramework\Unit\Helper\MockCreationTrait;
1717
use PHPUnit\Framework\MockObject\Exception;
1818
use Magento\Framework\Acl\Role\CurrentRoleContext;
1919
use PHPUnit\Framework\MockObject\MockObject;
@@ -24,6 +24,8 @@
2424
*/
2525
class MissingDeclineRuleTest extends TestCase
2626
{
27+
use MockCreationTrait;
28+
2729
/**
2830
* @var Rule
2931
*/
@@ -49,20 +51,14 @@ class MissingDeclineRuleTest extends TestCase
4951
*/
5052
private $serializerMock;
5153

52-
/**
53-
* @var ObjectManagerHelper
54-
*/
55-
private $objectManagerHelper;
56-
5754
/**
5855
* @inheritDoc
5956
*/
6057
protected function setUp(): void
6158
{
62-
$this->objectManagerHelper = new ObjectManagerHelper($this);
6359
$this->rootResource = new RootResource('Magento_Backend::all');
6460

65-
$this->resourceMock = $this->objectManagerHelper->createPartialMockWithReflection(
61+
$this->resourceMock = $this->createPartialMockWithReflection(
6662
ResourceConnection::class,
6763
['getTable', 'getConnection']
6864
);

app/code/Magento/Authorization/Test/Unit/Model/Acl/Loader/RoleTest.php

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Magento\Framework\DB\Adapter\Pdo\Mysql;
1717
use Magento\Framework\DB\Select;
1818
use Magento\Framework\Serialize\Serializer\Json;
19-
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
19+
use Magento\Framework\TestFramework\Unit\Helper\MockCreationTrait;
2020
use PHPUnit\Framework\MockObject\MockObject;
2121
use PHPUnit\Framework\TestCase;
2222

@@ -25,6 +25,8 @@
2525
*/
2626
class RoleTest extends TestCase
2727
{
28+
use MockCreationTrait;
29+
2830
/**
2931
* @var Role
3032
*/
@@ -65,23 +67,16 @@ class RoleTest extends TestCase
6567
*/
6668
private $adapterMock;
6769

68-
/**
69-
* @var ObjectManager
70-
*/
71-
private $objectManagerHelper;
72-
7370
/**
7471
* @inheritDoc
7572
*/
7673
protected function setUp(): void
7774
{
78-
$this->objectManagerHelper = new ObjectManager($this);
79-
80-
$this->groupFactoryMock = $this->objectManagerHelper->createPartialMockWithReflection(
75+
$this->groupFactoryMock = $this->createPartialMockWithReflection(
8176
GroupFactory::class,
8277
['create', 'getModelInstance']
8378
);
84-
$this->roleFactoryMock = $this->objectManagerHelper->createPartialMockWithReflection(
79+
$this->roleFactoryMock = $this->createPartialMockWithReflection(
8580
UserFactory::class,
8681
['create', 'getModelInstance']
8782
);
@@ -112,15 +107,12 @@ static function ($value) {
112107

113108
$this->adapterMock = $this->createMock(Mysql::class);
114109

115-
$this->model = $this->objectManagerHelper->getObject(
116-
Role::class,
117-
[
118-
'groupFactory' => $this->groupFactoryMock,
119-
'roleFactory' => $this->roleFactoryMock,
120-
'resource' => $this->resourceMock,
121-
'aclDataCache' => $this->aclDataCacheMock,
122-
'serializer' => $this->serializerMock
123-
]
110+
$this->model = new Role(
111+
$this->groupFactoryMock,
112+
$this->roleFactoryMock,
113+
$this->resourceMock,
114+
$this->aclDataCacheMock,
115+
$this->serializerMock
124116
);
125117
}
126118

app/code/Magento/Authorization/Test/Unit/Model/Acl/Loader/RuleTest.php

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Magento\Framework\App\ResourceConnection;
1616
use Magento\Framework\DB\Adapter\AdapterInterface;
1717
use Magento\Framework\Serialize\Serializer\Json;
18-
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
18+
use Magento\Framework\TestFramework\Unit\Helper\MockCreationTrait;
1919
use PHPUnit\Framework\MockObject\MockObject;
2020
use PHPUnit\Framework\TestCase;
2121

@@ -24,6 +24,8 @@
2424
*/
2525
class RuleTest extends TestCase
2626
{
27+
use MockCreationTrait;
28+
2729
/**
2830
* @var Rule
2931
*/
@@ -49,20 +51,14 @@ class RuleTest extends TestCase
4951
*/
5052
private $serializerMock;
5153

52-
/**
53-
* @var ObjectManager
54-
*/
55-
private $objectManagerHelper;
56-
5754
/**
5855
* @inheritDoc
5956
*/
6057
protected function setUp(): void
6158
{
62-
$this->objectManagerHelper = new ObjectManager($this);
6359
$this->rootResource = new RootResource('Magento_Backend::all');
6460

65-
$this->resourceMock = $this->objectManagerHelper->createPartialMockWithReflection(
61+
$this->resourceMock = $this->createPartialMockWithReflection(
6662
ResourceConnection::class,
6763
['getConnection', 'getTableName', 'getTable']
6864
);
@@ -86,14 +82,16 @@ static function ($value) {
8682
}
8783
);
8884

89-
$this->model = $this->objectManagerHelper->getObject(
90-
Rule::class,
91-
[
92-
'rootResource' => $this->rootResource,
93-
'resource' => $this->resourceMock,
94-
'aclDataCache' => $this->aclDataCacheMock,
95-
'serializer' => $this->serializerMock
96-
]
85+
$roleContext = $this->createMock(CurrentRoleContext::class);
86+
87+
$this->model = new Rule(
88+
$this->rootResource,
89+
$this->resourceMock,
90+
$this->aclDataCacheMock,
91+
$this->serializerMock,
92+
null,
93+
null,
94+
$roleContext
9795
);
9896
}
9997

@@ -189,7 +187,7 @@ public function testPopulateAclForSpecificRoleFromCache(): void
189187
$connectionMock = $this->createMock(AdapterInterface::class);
190188
$connectionMock->method('fetchRow')->willReturn([]); // Return empty array for any DB fetchRow() call
191189

192-
$selectMock = $this->objectManagerHelper->createPartialMockWithReflection(
190+
$selectMock = $this->createPartialMockWithReflection(
193191
'stdClass',
194192
['from', 'where', 'limit']
195193
);
@@ -200,15 +198,14 @@ public function testPopulateAclForSpecificRoleFromCache(): void
200198
$this->resourceMock->method('getConnection')->willReturn($connectionMock);
201199
$this->resourceMock->method('getTableName')->willReturn('authorization_role'); // Return dummy table name
202200

203-
$model = $this->objectManagerHelper->getObject(
204-
Rule::class,
205-
[
206-
'rootResource' => $this->rootResource,
207-
'resource' => $this->resourceMock,
208-
'aclDataCache' => $this->aclDataCacheMock,
209-
'serializer' => $this->serializerMock,
210-
'roleContext' => $roleContext,
211-
]
201+
$model = new Rule(
202+
$this->rootResource,
203+
$this->resourceMock,
204+
$this->aclDataCacheMock,
205+
$this->serializerMock,
206+
null,
207+
null,
208+
$roleContext
212209
);
213210

214211
$model->populateAcl($aclMock);

app/code/Magento/Authorization/Test/Unit/Model/ResourceModel/RulesTest.php

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Magento\Framework\Exception\LocalizedException;
1818
use Magento\Framework\Model\ResourceModel\Db\Context;
1919
use Magento\Framework\Phrase;
20-
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
20+
use Magento\Framework\TestFramework\Unit\Helper\MockCreationTrait;
2121
use PHPUnit\Framework\MockObject\MockObject;
2222
use PHPUnit\Framework\TestCase;
2323
use Psr\Log\LoggerInterface;
@@ -32,6 +32,8 @@
3232
*/
3333
class RulesTest extends TestCase
3434
{
35+
use MockCreationTrait;
36+
3537
/**
3638
* Test constants
3739
*/
@@ -82,18 +84,11 @@ class RulesTest extends TestCase
8284
*/
8385
private $rulesModelMock;
8486

85-
/**
86-
* @var ObjectManager
87-
*/
88-
private $objectManagerHelper;
89-
9087
/**
9188
* @inheritDoc
9289
*/
9390
protected function setUp(): void
9491
{
95-
$this->objectManagerHelper = new ObjectManager($this);
96-
9792
$this->contextMock = $this->createPartialMock(Context::class, ['getResources']);
9893

9994
$this->resourceConnectionMock = $this->createPartialMock(
@@ -116,7 +111,7 @@ protected function setUp(): void
116111
->with('authorization_rule', 'default')
117112
->willReturnArgument(0);
118113

119-
$this->aclBuilderMock = $this->objectManagerHelper->createPartialMockWithReflection(
114+
$this->aclBuilderMock = $this->createPartialMockWithReflection(
120115
Builder::class,
121116
['getConfigCache']
122117
);
@@ -130,24 +125,21 @@ protected function setUp(): void
130125
$this->aclBuilderMock->method('getConfigCache')
131126
->willReturn($this->aclDataCacheMock);
132127

133-
$this->rulesModelMock = $this->objectManagerHelper->createPartialMockWithReflection(
128+
$this->rulesModelMock = $this->createPartialMockWithReflection(
134129
RulesModel::class,
135130
['getRoleId']
136131
);
137132

138133
$this->rulesModelMock->method('getRoleId')
139134
->willReturn(self::TEST_ROLE_ID);
140135

141-
$this->model = $this->objectManagerHelper->getObject(
142-
Rules::class,
143-
[
144-
'context' => $this->contextMock,
145-
'aclBuilder' => $this->aclBuilderMock,
146-
'logger' => $this->loggerMock,
147-
'rootResource' => $this->rootResourceMock,
148-
'aclDataCache' => $this->aclDataCacheMock,
149-
'default'
150-
]
136+
$this->model = new Rules(
137+
$this->contextMock,
138+
$this->aclBuilderMock,
139+
$this->loggerMock,
140+
$this->rootResourceMock,
141+
$this->aclDataCacheMock,
142+
'default'
151143
);
152144
}
153145

app/code/Magento/Captcha/Test/Unit/Controller/Refresh/IndexTest.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Magento\Framework\Controller\Result\Json as ResultJson;
1616
use Magento\Framework\Controller\Result\JsonFactory as ResultJsonFactory;
1717
use Magento\Framework\Serialize\Serializer\Json as JsonSerializer;
18-
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
18+
use Magento\Framework\TestFramework\Unit\Helper\MockCreationTrait;
1919
use Magento\Framework\View\Element\AbstractBlock;
2020
use Magento\Framework\View\Element\BlockInterface;
2121
use Magento\Framework\View\LayoutInterface;
@@ -24,6 +24,8 @@
2424

2525
class IndexTest extends TestCase
2626
{
27+
use MockCreationTrait;
28+
2729
private const STUB_FORM_ID = 'StubFormId';
2830
private const STUB_CAPTCHA_SOURCE = '/stub-captcha-source.jpg';
2931

@@ -54,16 +56,11 @@ class IndexTest extends TestCase
5456
/** @var Index */
5557
private $refreshAction;
5658

57-
/** @var ObjectManager */
58-
private $objectManager;
59-
6059
protected function setUp(): void
6160
{
62-
$this->objectManager = new ObjectManager($this);
63-
6461
$this->requestMock = $this->createPartialMock(HttpRequest::class, ['getPost', 'getContent']);
6562
$this->layoutMock = $this->createMock(LayoutInterface::class);
66-
$this->blockMock = $this->objectManager->createPartialMockWithReflection(
63+
$this->blockMock = $this->createPartialMockWithReflection(
6764
AbstractBlock::class,
6865
['setFormId', 'setIsAjax', 'toHtml']
6966
);
@@ -149,7 +146,7 @@ public function testCaptchaFallsBackToRequestContentIfPostMissing()
149146
*/
150147
private function getCaptchaModelMock(string $imageSource): CaptchaInterface
151148
{
152-
$modelMock = $this->objectManager->createPartialMockWithReflection(
149+
$modelMock = $this->createPartialMockWithReflection(
153150
CaptchaInterface::class,
154151
['generate', 'getBlockName', 'getImgSrc', 'isCorrect']
155152
);

0 commit comments

Comments
 (0)