forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreatesTestModels.php
More file actions
137 lines (112 loc) · 4.39 KB
/
CreatesTestModels.php
File metadata and controls
137 lines (112 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace Pterodactyl\Tests\Traits\Integration;
use Ramsey\Uuid\Uuid;
use Pterodactyl\Models\Egg;
use Pterodactyl\Models\Node;
use Pterodactyl\Models\User;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Subuser;
use Pterodactyl\Models\Location;
use Pterodactyl\Models\Allocation;
trait CreatesTestModels
{
/**
* Creates a server model in the databases for the purpose of testing. If an attribute
* is passed in that normally requires this function to create a model no model will be
* created and that attribute's value will be used.
*
* The returned server model will have all the relationships loaded onto it.
*/
public function createServerModel(array $attributes = []): Server
{
if (isset($attributes['user_id'])) {
$attributes['owner_id'] = $attributes['user_id'];
}
if (!isset($attributes['owner_id'])) {
/** @var User $user */
$user = User::factory()->create();
$attributes['owner_id'] = $user->id;
}
if (!isset($attributes['node_id'])) {
if (!isset($attributes['location_id'])) {
/** @var Location $location */
$location = Location::factory()->create();
$attributes['location_id'] = $location->id;
}
/** @var Node $node */
$node = Node::factory()->create(['location_id' => $attributes['location_id']]);
$attributes['node_id'] = $node->id;
}
if (!isset($attributes['allocation_id'])) {
/** @var Allocation $allocation */
$allocation = Allocation::factory()->create(['node_id' => $attributes['node_id']]);
$attributes['allocation_id'] = $allocation->id;
}
if (empty($attributes['egg_id'])) {
$egg = !empty($attributes['nest_id'])
? Egg::query()->where('nest_id', $attributes['nest_id'])->firstOrFail()
: $this->getBungeecordEgg();
$attributes['egg_id'] = $egg->id;
$attributes['nest_id'] = $egg->nest_id;
}
if (empty($attributes['nest_id'])) {
$attributes['nest_id'] = Egg::query()->findOrFail($attributes['egg_id'])->nest_id;
}
unset($attributes['user_id'], $attributes['location_id']);
/** @var Server $server */
$server = Server::factory()->create($attributes);
Allocation::query()->where('id', $server->allocation_id)->update(['server_id' => $server->id]);
return $server->fresh([
'location', 'user', 'node', 'allocation', 'nest', 'egg',
]);
}
/**
* Generates a user and a server for that user. If an array of permissions is passed it
* is assumed that the user is actually a subuser of the server.
*
* @param string[] $permissions
*
* @return array{\Pterodactyl\Models\User, \Pterodactyl\Models\Server}
*/
public function generateTestAccount(array $permissions = []): array
{
/** @var User $user */
$user = User::factory()->create();
if (empty($permissions)) {
return [$user, $this->createServerModel(['user_id' => $user->id])];
}
$server = $this->createServerModel();
Subuser::query()->create([
'user_id' => $user->id,
'server_id' => $server->id,
'permissions' => $permissions,
]);
return [$user, $server];
}
/**
* Clones a given egg allowing us to make modifications that don't affect other
* tests that rely on the egg existing in the correct state.
*/
protected function cloneEggAndVariables(Egg $egg): Egg
{
$model = $egg->replicate(['id', 'uuid']);
$model->uuid = Uuid::uuid4()->toString();
$model->push();
/** @var Egg $model */
$model = $model->fresh();
foreach ($egg->variables as $variable) {
$variable->replicate(['id', 'egg_id'])->forceFill(['egg_id' => $model->id])->push();
}
return $model->fresh();
}
/**
* Almost every test just assumes it is using BungeeCord — this is the critical
* egg model for all tests unless specified otherwise.
*/
private function getBungeecordEgg(): Egg
{
/** @var Egg $egg */
$egg = Egg::query()->where('author', 'support@pterodactyl.io')->where('name', 'Bungeecord')->firstOrFail();
return $egg;
}
}