misc_pterodactyl-panel/tests/Traits/Integration/CreatesTestModels.php

137 lines
4.5 KiB
PHP
Raw Normal View History

2018-03-04 22:30:16 +00:00
<?php
namespace Pterodactyl\Tests\Traits\Integration;
2018-03-04 22:30:16 +00:00
use Ramsey\Uuid\Uuid;
2018-03-04 22:30:16 +00:00
use Pterodactyl\Models\Egg;
use Pterodactyl\Models\Node;
use Pterodactyl\Models\User;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Subuser;
2018-03-04 22:30:16 +00:00
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 of the relationships loaded onto it.
*
* @return \Pterodactyl\Models\Server
*/
public function createServerModel(array $attributes = [])
2018-03-04 22:30:16 +00:00
{
if (isset($attributes['user_id'])) {
$attributes['owner_id'] = $attributes['user_id'];
}
2021-01-23 20:33:34 +00:00
if (!isset($attributes['owner_id'])) {
/** @var \Pterodactyl\Models\User $user */
$user = User::factory()->create();
2018-03-04 22:30:16 +00:00
$attributes['owner_id'] = $user->id;
}
2021-01-23 20:33:34 +00:00
if (!isset($attributes['node_id'])) {
if (!isset($attributes['location_id'])) {
/** @var \Pterodactyl\Models\Location $location */
$location = Location::factory()->create();
2018-03-04 22:30:16 +00:00
$attributes['location_id'] = $location->id;
}
/** @var \Pterodactyl\Models\Node $node */
$node = Node::factory()->create(['location_id' => $attributes['location_id']]);
2018-03-04 22:30:16 +00:00
$attributes['node_id'] = $node->id;
}
2021-01-23 20:33:34 +00:00
if (!isset($attributes['allocation_id'])) {
/** @var \Pterodactyl\Models\Allocation $allocation */
$allocation = Allocation::factory()->create(['node_id' => $attributes['node_id']]);
2018-03-04 22:30:16 +00:00
$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();
2018-03-04 22:30:16 +00:00
$attributes['egg_id'] = $egg->id;
$attributes['nest_id'] = $egg->nest_id;
2018-03-04 22:30:16 +00:00
}
if (empty($attributes['nest_id'])) {
$attributes['nest_id'] = Egg::query()->findOrFail($attributes['egg_id'])->nest_id;
2018-03-04 22:30:16 +00:00
}
unset($attributes['user_id'], $attributes['location_id']);
/** @var \Pterodactyl\Models\Server $server */
$server = Server::factory()->create($attributes);
2018-03-04 22:30:16 +00:00
Allocation::query()->where('id', $server->allocation_id)->update(['server_id' => $server->id]);
return $server->fresh([
2018-03-04 22:30:16 +00:00
'location', 'user', 'node', 'allocation', 'nest', 'egg',
]);
2018-03-04 22:30:16 +00:00
}
/**
* 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 \Pterodactyl\Models\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 \Pterodactyl\Models\Egg $model */
$model = $model->fresh();
foreach ($egg->variables as $variable) {
$variable->replicate(['id', 'egg_id'])->forceFill(['egg_id' => $model->id])->push();
}
return $model->fresh();
}
/**
* Most every test just assumes it is using Bungeecord this is the critical
* egg model for all tests unless specified otherwise.
*/
private function getBungeecordEgg()
{
return Egg::query()->where('author', 'support@pterodactyl.io')->where('name', 'Bungeecord')->firstOrFail();
}
2018-03-04 22:30:16 +00:00
}