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