Skip to content

Commit 873f46c

Browse files
committed
Merge remote-tracking branch 'l3/ACP2E-668' into PR_L3_06_04_2022
2 parents 5f057f4 + 2a17587 commit 873f46c

File tree

2 files changed

+217
-0
lines changed

2 files changed

+217
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Authorization\Test\Fixture;
9+
10+
use Magento\Authorization\Model\Acl\Role\Group;
11+
use Magento\Authorization\Model\ResourceModel\Role as RoleResource;
12+
use Magento\Authorization\Model\UserContextInterface;
13+
use Magento\Framework\DataObject;
14+
use Magento\SharedCatalog\Model\SharedCatalogFactory;
15+
use Magento\TestFramework\Fixture\Data\ProcessorInterface;
16+
use Magento\TestFramework\Fixture\RevertibleDataFixtureInterface;
17+
use Magento\Authorization\Model\RoleFactory;
18+
use Magento\Authorization\Model\RulesFactory;
19+
use Magento\User\Model\UserFactory;
20+
21+
/**
22+
* Creating a new admin role
23+
*/
24+
class Role implements RevertibleDataFixtureInterface
25+
{
26+
private const DEFAULT_DATA = [
27+
'role_name' => 'Role Name %uniqid%',
28+
'role_type' => Group::ROLE_TYPE,
29+
'user_id' => 0,
30+
'user_type' => UserContextInterface::USER_TYPE_ADMIN
31+
];
32+
33+
private const DEFAULT_DATA_RULES = [
34+
'id' => null,
35+
'role_id' => null,
36+
'resources' => ['Magento_Backend::all']
37+
];
38+
39+
/**
40+
* @var RoleFactory
41+
*/
42+
private $roleFactory;
43+
44+
/**
45+
* @var ProcessorInterface
46+
*/
47+
private $dataProcessor;
48+
49+
/**
50+
* @var RoleResource
51+
*/
52+
private $roleResourceModel;
53+
54+
/**
55+
* @var RulesFactory
56+
*/
57+
private $rulesFactory;
58+
59+
/**
60+
* @param RoleFactory $roleFactory
61+
* @param RoleResource $roleResourceModel
62+
* @param RulesFactory $rulesFactory
63+
* @param ProcessorInterface $dataProcessor
64+
*/
65+
public function __construct(
66+
RoleFactory $roleFactory,
67+
RoleResource $roleResourceModel,
68+
RulesFactory $rulesFactory,
69+
ProcessorInterface $dataProcessor
70+
) {
71+
$this->roleFactory = $roleFactory;
72+
$this->roleResourceModel = $roleResourceModel;
73+
$this->rulesFactory = $rulesFactory;
74+
$this->dataProcessor = $dataProcessor;
75+
}
76+
77+
/**
78+
* @inheritdoc
79+
*/
80+
public function apply(array $data = []): ?DataObject
81+
{
82+
$role = $this->roleFactory->create();
83+
$role->setData($this->prepareData(array_diff_key($data, self::DEFAULT_DATA_RULES)));
84+
$this->roleResourceModel->save($role);
85+
86+
$rules = $this->rulesFactory->create();
87+
$rules->setRoleId($role->getId() ?? null);
88+
$rules->setResources($data['resources'] ?? self::DEFAULT_DATA_RULES['resources']);
89+
$rules->saveRel();
90+
91+
return $role;
92+
}
93+
94+
/**
95+
* @inheritdoc
96+
*/
97+
public function revert(DataObject $data): void
98+
{
99+
$role = $this->roleFactory->create();
100+
$role->load($data->getId());
101+
102+
if ($role->getId() !== null) {
103+
$role->delete();
104+
}
105+
}
106+
107+
/**
108+
* Prepare admin role data
109+
*
110+
* @param array $data
111+
* @return array
112+
*/
113+
private function prepareData(array $data): array
114+
{
115+
$data = array_merge(self::DEFAULT_DATA, $data);
116+
return $this->dataProcessor->process($this, $data);
117+
}
118+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\User\Test\Fixture;
9+
10+
use Magento\Framework\DataObject;
11+
use Magento\TestFramework\Bootstrap;
12+
use Magento\TestFramework\Fixture\Data\ProcessorInterface;
13+
use Magento\TestFramework\Fixture\RevertibleDataFixtureInterface;
14+
use Magento\User\Model\UserFactory;
15+
use Magento\User\Model\ResourceModel\User as UserResource;
16+
17+
/**
18+
* Creating a new admin user with variable role
19+
*/
20+
class User implements RevertibleDataFixtureInterface
21+
{
22+
private const DEFAULT_DATA = [
23+
'username' => 'adminuser%uniqid%',
24+
'firstname' => 'AdminFirstname%uniqid%',
25+
'lastname' => 'AdminLastname%uniqid%',
26+
'email' => 'adminuser%uniqid%@example.com',
27+
'password' => Bootstrap::ADMIN_PASSWORD,
28+
'interface_locale' => 'en_US',
29+
'is_active' => 1
30+
];
31+
32+
/**
33+
* @var UserFactory
34+
*/
35+
private $userFactory;
36+
37+
/**
38+
* @var ProcessorInterface
39+
*/
40+
private $dataProcessor;
41+
42+
/**
43+
* @var UserResource
44+
*/
45+
private $userResource;
46+
47+
/**
48+
* @param UserFactory $userFactory
49+
* @param UserResource $userResource
50+
* @param ProcessorInterface $dataProcessor
51+
*/
52+
public function __construct(
53+
UserFactory $userFactory,
54+
UserResource $userResource,
55+
ProcessorInterface $dataProcessor
56+
) {
57+
$this->userFactory = $userFactory;
58+
$this->userResource = $userResource;
59+
$this->dataProcessor = $dataProcessor;
60+
}
61+
62+
/**
63+
* @inheritdoc
64+
*/
65+
public function apply(array $data = []): ?DataObject
66+
{
67+
$user = $this->userFactory->create();
68+
$user->setData($this->prepareData($data));
69+
$user->setRoleId($data['role_id'] ?? 0);
70+
$this->userResource->save($user);
71+
72+
return $user;
73+
}
74+
75+
/**
76+
* @inheritdoc
77+
*/
78+
public function revert(DataObject $data): void
79+
{
80+
$user = $this->userFactory->create();
81+
$user->load($data->getData('username'), 'username');
82+
83+
if ($user->getId() !== null) {
84+
$this->userResource->delete($user);
85+
}
86+
}
87+
88+
/**
89+
* Prepare admin user data
90+
*
91+
* @param array $data
92+
* @return array
93+
*/
94+
private function prepareData(array $data): array
95+
{
96+
$data = array_merge(self::DEFAULT_DATA, $data);
97+
return $this->dataProcessor->process($this, $data);
98+
}
99+
}

0 commit comments

Comments
 (0)