Skip to content

Commit 39f83fc

Browse files
committed
ACP2E-668: replaced legacy data fixtures with parametrized data fixtures
1 parent fabd681 commit 39f83fc

File tree

3 files changed

+257
-0
lines changed

3 files changed

+257
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
'gws_is_all' => 1,
32+
'gws_websites' => null,
33+
'gws_store_groups' => null
34+
];
35+
36+
/**
37+
* @var RoleFactory
38+
*/
39+
private $roleFactory;
40+
41+
/**
42+
* @var ProcessorInterface
43+
*/
44+
private $dataProcessor;
45+
46+
/**
47+
* @var RoleResource
48+
*/
49+
private $roleResourceModel;
50+
51+
/**
52+
* @param RoleFactory $roleFactory
53+
* @param RoleResource $roleResourceModel
54+
* @param ProcessorInterface $dataProcessor
55+
*/
56+
public function __construct(
57+
RoleFactory $roleFactory,
58+
RoleResource $roleResourceModel,
59+
ProcessorInterface $dataProcessor
60+
) {
61+
$this->roleFactory = $roleFactory;
62+
$this->roleResourceModel = $roleResourceModel;
63+
$this->dataProcessor = $dataProcessor;
64+
}
65+
66+
/**
67+
* @inheritdoc
68+
*/
69+
public function apply(array $data = []): ?DataObject
70+
{
71+
$role = $this->roleFactory->create();
72+
$role->setData($this->prepareData($data));
73+
$this->roleResourceModel->save($role);
74+
75+
return $role;
76+
}
77+
78+
/**
79+
* @inheritdoc
80+
*/
81+
public function revert(DataObject $data): void
82+
{
83+
84+
$role = $this->roleFactory->create();
85+
$role->load($data->getData('role_name'), 'role_name');
86+
87+
if ($role->getId() !== null) {
88+
$role->delete();
89+
}
90+
}
91+
92+
/**
93+
* Prepare admin role data
94+
*
95+
* @param array $data
96+
* @return array
97+
*/
98+
private function prepareData(array $data): array
99+
{
100+
$data = array_merge(self::DEFAULT_DATA, $data);
101+
return $this->dataProcessor->process($this, $data);
102+
}
103+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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\Framework\DataObject;
11+
use Magento\SharedCatalog\Model\SharedCatalogFactory;
12+
use Magento\Authorization\Model\RoleFactory;
13+
use Magento\Authorization\Model\RulesFactory;
14+
use Magento\TestFramework\Fixture\DataFixtureInterface;
15+
use Magento\User\Model\UserFactory;
16+
17+
/**
18+
* Creating a new admin rules for a new role
19+
*/
20+
class Rules implements DataFixtureInterface
21+
{
22+
private const DEFAULT_DATA = [
23+
'id' => null,
24+
'role_id' => null,
25+
'resources' => ['Magento_Backend::all']
26+
];
27+
28+
/**
29+
* @var RulesFactory
30+
*/
31+
private $rulesFactory;
32+
33+
/**
34+
* @param RulesFactory $rulesFactory
35+
*/
36+
public function __construct(
37+
RulesFactory $rulesFactory
38+
) {
39+
$this->rulesFactory = $rulesFactory;
40+
}
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
public function apply(array $data = []): ?DataObject
46+
{
47+
$data = array_merge(self::DEFAULT_DATA, $data);
48+
$rules = $this->rulesFactory->create();
49+
$rules->setRoleId($data['role_id'] ?? null);
50+
$rules->setResources($data['resources'] ?? []);
51+
$rules->saveRel();
52+
53+
return $rules;
54+
}
55+
}
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' => 'admin_user_%uniqid%',
24+
'firstname' => 'Firstname %uniqid%',
25+
'lastname' => 'Lastname %uniqid%',
26+
'email' => 'admin_user_%uniqid%@email.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)