Path: blob/1.0-develop/tests/Traits/Integration/CreatesTestModels.php
7461 views
<?php12namespace Pterodactyl\Tests\Traits\Integration;34use Ramsey\Uuid\Uuid;5use Pterodactyl\Models\Egg;6use Pterodactyl\Models\Node;7use Pterodactyl\Models\User;8use Pterodactyl\Models\Server;9use Pterodactyl\Models\Subuser;10use Pterodactyl\Models\Location;11use Pterodactyl\Models\Allocation;1213trait CreatesTestModels14{15/**16* Creates a server model in the databases for the purpose of testing. If an attribute17* is passed in that normally requires this function to create a model no model will be18* created and that attribute's value will be used.19*20* The returned server model will have all the relationships loaded onto it.21*/22public function createServerModel(array $attributes = []): Server23{24if (isset($attributes['user_id'])) {25$attributes['owner_id'] = $attributes['user_id'];26}2728if (!isset($attributes['owner_id'])) {29/** @var User $user */30$user = User::factory()->create();31$attributes['owner_id'] = $user->id;32}3334if (!isset($attributes['node_id'])) {35if (!isset($attributes['location_id'])) {36/** @var Location $location */37$location = Location::factory()->create();38$attributes['location_id'] = $location->id;39}4041/** @var Node $node */42$node = Node::factory()->create(['location_id' => $attributes['location_id']]);43$attributes['node_id'] = $node->id;44}4546if (!isset($attributes['allocation_id'])) {47/** @var Allocation $allocation */48$allocation = Allocation::factory()->create(['node_id' => $attributes['node_id']]);49$attributes['allocation_id'] = $allocation->id;50}5152if (empty($attributes['egg_id'])) {53$egg = !empty($attributes['nest_id'])54? Egg::query()->where('nest_id', $attributes['nest_id'])->firstOrFail()55: $this->getBungeecordEgg();5657$attributes['egg_id'] = $egg->id;58$attributes['nest_id'] = $egg->nest_id;59}6061if (empty($attributes['nest_id'])) {62$attributes['nest_id'] = Egg::query()->findOrFail($attributes['egg_id'])->nest_id;63}6465unset($attributes['user_id'], $attributes['location_id']);6667/** @var Server $server */68$server = Server::factory()->create($attributes);6970Allocation::query()->where('id', $server->allocation_id)->update(['server_id' => $server->id]);7172return $server->fresh([73'location', 'user', 'node', 'allocation', 'nest', 'egg',74]);75}7677/**78* Generates a user and a server for that user. If an array of permissions is passed it79* is assumed that the user is actually a subuser of the server.80*81* @param string[] $permissions82*83* @return array{\Pterodactyl\Models\User, \Pterodactyl\Models\Server}84*/85public function generateTestAccount(array $permissions = []): array86{87/** @var User $user */88$user = User::factory()->create();8990if (empty($permissions)) {91return [$user, $this->createServerModel(['user_id' => $user->id])];92}9394$server = $this->createServerModel();9596Subuser::query()->create([97'user_id' => $user->id,98'server_id' => $server->id,99'permissions' => $permissions,100]);101102return [$user, $server];103}104105/**106* Clones a given egg allowing us to make modifications that don't affect other107* tests that rely on the egg existing in the correct state.108*/109protected function cloneEggAndVariables(Egg $egg): Egg110{111$model = $egg->replicate(['id', 'uuid']);112$model->uuid = Uuid::uuid4()->toString();113$model->push();114115/** @var Egg $model */116$model = $model->fresh();117118foreach ($egg->variables as $variable) {119$variable->replicate(['id', 'egg_id'])->forceFill(['egg_id' => $model->id])->push();120}121122return $model->fresh();123}124125/**126* Almost every test just assumes it is using BungeeCord — this is the critical127* egg model for all tests unless specified otherwise.128*/129private function getBungeecordEgg(): Egg130{131/** @var Egg $egg */132$egg = Egg::query()->where('author', '[email protected]')->where('name', 'Bungeecord')->firstOrFail();133134return $egg;135}136}137138139