Skip to content

Commit 5c5b75a

Browse files
author
Krishna Kumar
committed
Changed the admin integration file with custom role
1 parent 2eb6c59 commit 5c5b75a

File tree

3 files changed

+55
-36
lines changed

3 files changed

+55
-36
lines changed

dev/tests/api-functional/testsuite/Magento/GraphQl/LoginAsCustomer/GenerateLoginCustomerTokenTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function testGenerateCustomerValidTokenLoginAsCustomerEnabled()
5656
$mutation,
5757
[],
5858
'',
59-
$this->getAdminHeaderAuthentication('TestAdmin1', 'Magento777')
59+
$this->getAdminHeaderAuthentication('TestAdmin1', \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD)
6060
);
6161
$this->assertArrayHasKey('generateCustomerTokenAsAdmin', $response);
6262
$this->assertIsArray($response['generateCustomerTokenAsAdmin']);
@@ -82,7 +82,7 @@ public function testGenerateCustomerValidTokenLoginAsCustomerDisabled()
8282
$mutation,
8383
[],
8484
'',
85-
$this->getAdminHeaderAuthentication('TestAdmin1', 'Magento777')
85+
$this->getAdminHeaderAuthentication('TestAdmin1', \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD)
8686
);
8787
}
8888

@@ -151,7 +151,7 @@ public function dataProviderInvalidInfo(): array
151151
return [
152152
'invalid_admin_user_name' => [
153153
'TestAdmin(^%',
154-
'Magento777',
154+
\Magento\TestFramework\Bootstrap::ADMIN_PASSWORD,
155155
156156
'The account sign-in was incorrect or your account is disabled temporarily. ' .
157157
'Please wait and try again later.'
@@ -195,7 +195,7 @@ private function getQuery(string $customerEmail) : string
195195
*/
196196
public function getCustomerHeaderAuthentication(
197197
string $username = '[email protected]',
198-
string $password = 'Magento777'
198+
string $password = \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
199199
): array {
200200
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
201201

dev/tests/integration/testsuite/Magento/LoginAsCustomer/_files/admin.php

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,38 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
use Magento\User\Model\UserFactory;
7-
use Magento\User\Model\ResourceModel\User as UserResource;
86

9-
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
7+
declare(strict_types=1);
108

11-
/** @var UserFactory $userFactory */
12-
$userFactory = $objectManager->get(UserFactory::class);
13-
/** @var UserResource $userResource */
14-
$userResource = $objectManager->get(UserResource::class);
9+
use Magento\Authorization\Model\RoleFactory;
10+
use Magento\Authorization\Model\Role;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
use Magento\User\Model\User;
13+
use Magento\Authorization\Model\RulesFactory;
14+
use Magento\Authorization\Model\Rules;
1515

16-
$adminInfo = [
17-
'username' => 'TestAdmin1',
18-
'firstname' => 'test',
19-
'lastname' => 'test',
20-
'email' => '[email protected]',
21-
'password' =>'Magento777',
22-
'interface_locale' => 'en_US',
23-
'is_active' => 1
24-
];
16+
//Creating a new admin user with a custom role to safely change role settings without affecting the main user's role.
17+
/** @var Role $role */
18+
$role = Bootstrap::getObjectManager()->get(RoleFactory::class)->create();
19+
$role->setName('test_custom_role');
20+
$role->setData('role_name', $role->getName());
21+
$role->setRoleType(\Magento\Authorization\Model\Acl\Role\Group::ROLE_TYPE);
22+
$role->setUserType((string)\Magento\Authorization\Model\UserContextInterface::USER_TYPE_ADMIN);
23+
$role->save();
24+
/** @var Rules $rules */
25+
$rules = Bootstrap::getObjectManager()->get(RulesFactory::class)->create();
26+
$rules->setRoleId($role->getId());
27+
//Granted all permissions.
28+
$rules->setResources([Bootstrap::getObjectManager()->get(\Magento\Framework\Acl\RootResource::class)->getId()]);
29+
$rules->saveRel();
2530

26-
$userModel = $userFactory->create();
27-
try {
28-
$userModel->setData($adminInfo);
29-
$userModel->setRoleId(1);
30-
$userResource->save($userModel);
31-
} catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
32-
}
31+
/** @var User $user */
32+
$user = Bootstrap::getObjectManager()->create(User::class);
33+
$user->setFirstname("John")
34+
->setLastname("Doe")
35+
->setUsername('TestAdmin1')
36+
->setPassword(\Magento\TestFramework\Bootstrap::ADMIN_PASSWORD)
37+
->setEmail('[email protected]')
38+
->setIsActive(1)
39+
->setRoleId($role->getId());
40+
$user->save();

dev/tests/integration/testsuite/Magento/LoginAsCustomer/_files/admin_rollback.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,26 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
use Magento\User\Model\UserFactory;
76

8-
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
7+
declare(strict_types=1);
98

10-
/** @var UserFactory $userFactory */
11-
$userFactory = $objectManager->get(UserFactory::class);
9+
use Magento\Authorization\Model\RoleFactory;
10+
use Magento\Authorization\Model\Role;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
use Magento\User\Model\User;
13+
use Magento\Authorization\Model\RulesFactory;
14+
use Magento\Authorization\Model\Rules;
1215

13-
$userModel = $userFactory->create();
14-
$userModel->loadByUsername('TestAdmin1');
15-
if ($userModel->getId()) {
16-
$userModel->delete();
17-
}
16+
//Deleting the user and the role.
17+
/** @var User $user */
18+
$user = Bootstrap::getObjectManager()->create(User::class);
19+
$user->load('TestAdmin1', 'username');
20+
$user->delete();
21+
/** @var Role $role */
22+
$role = Bootstrap::getObjectManager()->get(RoleFactory::class)->create();
23+
$role->load('test_custom_role', 'role_name');
24+
/** @var Rules $rules */
25+
$rules = Bootstrap::getObjectManager()->get(RulesFactory::class)->create();
26+
$rules->load($role->getId(), 'role_id');
27+
$rules->delete();
28+
$role->delete();

0 commit comments

Comments
 (0)