diff --git a/app/Services/Nests/NestCreationService.php b/app/Services/Nests/NestCreationService.php index cad638844..439164485 100644 --- a/app/Services/Nests/NestCreationService.php +++ b/app/Services/Nests/NestCreationService.php @@ -1,11 +1,4 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Services\Nests; @@ -19,12 +12,12 @@ class NestCreationService /** * @var \Illuminate\Contracts\Config\Repository */ - protected $config; + private $config; /** * @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface */ - protected $repository; + private $repository; /** * NestCreationService constructor. @@ -41,16 +34,16 @@ class NestCreationService /** * Create a new nest on the system. * - * @param array $data + * @param array $data + * @param string|null $author * @return \Pterodactyl\Models\Nest - * * @throws \Pterodactyl\Exceptions\Model\DataValidationException */ - public function handle(array $data): Nest + public function handle(array $data, string $author = null): Nest { return $this->repository->create([ 'uuid' => Uuid::uuid4()->toString(), - 'author' => $this->config->get('pterodactyl.service.author'), + 'author' => $author ?? $this->config->get('pterodactyl.service.author'), 'name' => array_get($data, 'name'), 'description' => array_get($data, 'description'), ], true, true); diff --git a/config/pterodactyl.php b/config/pterodactyl.php index f232d8a1e..6df7056e5 100644 --- a/config/pterodactyl.php +++ b/config/pterodactyl.php @@ -11,8 +11,7 @@ return [ | standard Pterodactyl shipped services. */ 'service' => [ - 'core' => 'ptrdctyl-v040-11e6-8b77-86f30ca893d3', - 'author' => env('SERVICE_AUTHOR'), + 'author' => env('SERVICE_AUTHOR', 'unknown@unknown.com'), ], /* diff --git a/database/seeds/NestSeeder.php b/database/seeds/NestSeeder.php index ae2b56953..99239daf1 100644 --- a/database/seeds/NestSeeder.php +++ b/database/seeds/NestSeeder.php @@ -59,7 +59,7 @@ class NestSeeder extends Seeder $this->creationService->handle([ 'name' => 'Minecraft', 'description' => 'Minecraft - the classic game from Mojang. With support for Vanilla MC, Spigot, and many others!', - ]); + ], 'support@pterodactyl.io'); } } @@ -76,7 +76,7 @@ class NestSeeder extends Seeder $this->creationService->handle([ 'name' => 'Source Engine', 'description' => 'Includes support for most Source Dedicated Server games.', - ]); + ], 'support@pterodactyl.io'); } } @@ -93,7 +93,7 @@ class NestSeeder extends Seeder $this->creationService->handle([ 'name' => 'Voice Servers', 'description' => 'Voice servers such as Mumble and Teamspeak 3.', - ]); + ], 'support@pterodactyl.io'); } } } diff --git a/tests/Unit/Services/Nests/NestCreationServiceTest.php b/tests/Unit/Services/Nests/NestCreationServiceTest.php index b5d38e071..e5d3d7c16 100644 --- a/tests/Unit/Services/Nests/NestCreationServiceTest.php +++ b/tests/Unit/Services/Nests/NestCreationServiceTest.php @@ -11,7 +11,6 @@ namespace Tests\Unit\Services\Services; use Mockery as m; use Tests\TestCase; -use Ramsey\Uuid\Uuid; use Pterodactyl\Models\Nest; use Tests\Traits\MocksUuids; use Illuminate\Contracts\Config\Repository; @@ -25,17 +24,12 @@ class NestCreationServiceTest extends TestCase /** * @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock */ - protected $config; + private $config; /** * @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface|\Mockery\Mock */ - protected $repository; - - /** - * @var \Pterodactyl\Services\Nests\NestCreationService - */ - protected $service; + private $repository; /** * Setup tests. @@ -46,8 +40,6 @@ class NestCreationServiceTest extends TestCase $this->config = m::mock(Repository::class); $this->repository = m::mock(NestRepositoryInterface::class); - - $this->service = new NestCreationService($this->config, $this->repository); } /** @@ -56,21 +48,48 @@ class NestCreationServiceTest extends TestCase public function testCreateNewService() { $model = factory(Nest::class)->make(); - $data = [ - 'name' => $model->name, - 'description' => $model->description, - ]; $this->config->shouldReceive('get')->with('pterodactyl.service.author')->once()->andReturn('testauthor@example.com'); $this->repository->shouldReceive('create')->with([ 'uuid' => $this->getKnownUuid(), 'author' => 'testauthor@example.com', - 'name' => $data['name'], - 'description' => $data['description'], + 'name' => $model->name, + 'description' => $model->description, ], true, true)->once()->andReturn($model); - $response = $this->service->handle($data); + $response = $this->getService()->handle(['name' => $model->name, 'description' => $model->description]); $this->assertInstanceOf(Nest::class, $response); $this->assertEquals($model, $response); } + + /** + * Test creation of a new nest with a defined author. This is used by seeder + * scripts which need to set a specific author for nests in order for other + * functionality to work correctly. + */ + public function testCreateServiceWithDefinedAuthor() + { + $model = factory(Nest::class)->make(); + + $this->repository->shouldReceive('create')->with([ + 'uuid' => $this->getKnownUuid(), + 'author' => 'support@pterodactyl.io', + 'name' => $model->name, + 'description' => $model->description, + ], true, true)->once()->andReturn($model); + + $response = $this->getService()->handle(['name' => $model->name, 'description' => $model->description], 'support@pterodactyl.io'); + $this->assertInstanceOf(Nest::class, $response); + $this->assertEquals($model, $response); + } + + /** + * Return an instance of the service with mocked dependencies. + * + * @return \Pterodactyl\Services\Nests\NestCreationService + */ + private function getService(): NestCreationService + { + return new NestCreationService($this->config, $this->repository); + } }