diff --git a/app/Console/Commands/Environment/EmailSettingsCommand.php b/app/Console/Commands/Environment/EmailSettingsCommand.php new file mode 100644 index 000000000..25e043697 --- /dev/null +++ b/app/Console/Commands/Environment/EmailSettingsCommand.php @@ -0,0 +1,172 @@ +. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +namespace Pterodactyl\Console\Commands\Environment; + +use Illuminate\Console\Command; +use Pterodactyl\Traits\Commands\EnvironmentWriterTrait; +use Illuminate\Contracts\Config\Repository as ConfigRepository; + +class EmailSettingsCommand extends Command +{ + use EnvironmentWriterTrait; + + /** + * @var \Illuminate\Contracts\Config\Repository + */ + protected $config; + + /** + * @var string + */ + protected $description = 'Set or update the email sending configuration for the Panel.'; + + /** + * @var string + */ + protected $signature = 'p:environment:mail + {--driver= : The mail driver to use.} + {--email= : Email address that messages from the Panel will originate from.} + {--from= : The name emails from the Panel will appear to be from.} + {--encryption=} + {--host=} + {--port=} + {--username=} + {--password=}'; + + /** + * @var array + */ + protected $variables = []; + + /** + * EmailSettingsCommand constructor. + * + * @param \Illuminate\Contracts\Config\Repository $config + */ + public function __construct(ConfigRepository $config) + { + parent::__construct(); + + $this->config = $config; + } + + /** + * Handle command execution. + */ + public function handle() + { + $this->variables['MAIL_DRIVER'] = $this->option('driver') ?? $this->choice( + trans('command/messages.environment.mail.ask_driver'), [ + 'smtp' => 'SMTP Server', + 'mail' => 'PHP\'s Internal Mail Function', + 'mailgun' => 'Mailgun Transactional Email', + 'mandrill' => 'Mandrill Transactional Email', + 'postmark' => 'Postmarkapp Transactional Email', + ], $this->config->get('mail.driver', 'smtp') + ); + + $method = 'setup' . studly_case($this->variables['MAIL_DRIVER']) . 'DriverVariables'; + if (method_exists($this, $method)) { + $this->{$method}(); + } + + $this->variables['MAIL_FROM'] = $this->option('email') ?? $this->ask( + trans('command/messages.environment.mail.ask_mail_from'), $this->config->get('mail.from.address') + ); + + $this->variables['MAIL_FROM_NAME'] = $this->option('from') ?? $this->ask( + trans('command/messages.environment.mail.ask_mail_name'), $this->config->get('mail.from.name') + ); + + $this->variables['MAIL_ENCRYPTION'] = $this->option('encryption') ?? $this->choice( + trans('command/messages.environment.mail.ask_encryption'), ['tls' => 'TLS', 'ssl' => 'SSL', '' => 'None'], $this->config->get('mail.encryption', 'tls') + ); + + $this->writeToEnvironment($this->variables); + + $this->line('Updating stored environment configuration file.'); + $this->call('config:cache'); + $this->line(''); + } + + /** + * Handle variables for SMTP driver. + */ + private function setupSmtpDriverVariables() + { + $this->variables['MAIL_HOST'] = $this->option('host') ?? $this->ask( + trans('command/messages.environment.mail.ask_smtp_host'), $this->config->get('mail.host') + ); + + $this->variables['MAIL_PORT'] = $this->option('port') ?? $this->ask( + trans('command/messages.environment.mail.ask_smtp_port'), $this->config->get('mail.port') + ); + + $this->variables['MAIL_USERNAME'] = $this->option('username') ?? $this->ask( + trans('command/messages.environment.mail.ask_smtp_username'), $this->config->get('mail.username') + ); + + $this->variables['MAIL_PASSWORD'] = $this->option('password') ?? $this->secret( + trans('command/messages.environment.mail.ask_smtp_password') + ); + } + + /** + * Handle variables for mailgun driver. + */ + private function setupMailgunDriverVariables() + { + $this->variables['MAILGUN_DOMAIN'] = $this->option('host') ?? $this->ask( + trans('command/messages.environment.mail.ask_mailgun_domain'), $this->config->get('services.mailgun.domain') + ); + + $this->variables['MAILGUN_KEY'] = $this->option('password') ?? $this->ask( + trans('command/messages.environment.mail.ask_mailgun_secret'), $this->config->get('services.mailgun.secret') + ); + } + + /** + * Handle variables for mandrill driver. + */ + private function setupMandrillDriverVariables() + { + $this->variables['MANDRILL_SECRET'] = $this->option('password') ?? $this->ask( + trans('command/messages.environment.mail.ask_mandrill_secret'), $this->config->get('services.mandrill.secret') + ); + } + + /** + * Handle variables for postmark driver. + */ + private function setupPostmarkDriverVariables() + { + $this->variables['MAIL_DRIVER'] = 'smtp'; + $this->variables['MAIL_HOST'] = 'smtp.postmarkapp.com'; + $this->variables['MAIL_PORT'] = 587; + $this->variables['MAIL_USERNAME'] = $this->variables['MAIL_PASSWORD'] = $this->option('username') ?? $this->ask( + trans('command/messages.environment.mail.ask_postmark_username'), $this->config->get('mail.username') + ); + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index ccb6830eb..c74250e06 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -12,6 +12,7 @@ use Pterodactyl\Console\Commands\Location\MakeLocationCommand; use Pterodactyl\Console\Commands\User\DisableTwoFactorCommand; use Pterodactyl\Console\Commands\Location\DeleteLocationCommand; use Pterodactyl\Console\Commands\Schedule\ProcessRunnableCommand; +use Pterodactyl\Console\Commands\Environment\EmailSettingsCommand; use Pterodactyl\Console\Commands\Maintenance\CleanServiceBackupFilesCommand; class Kernel extends ConsoleKernel @@ -26,6 +27,7 @@ class Kernel extends ConsoleKernel DeleteLocationCommand::class, DeleteUserCommand::class, DisableTwoFactorCommand::class, + EmailSettingsCommand::class, InfoCommand::class, MakeLocationCommand::class, MakeUserCommand::class, diff --git a/app/Traits/Commands/EnvironmentWriterTrait.php b/app/Traits/Commands/EnvironmentWriterTrait.php new file mode 100644 index 000000000..80cf6e079 --- /dev/null +++ b/app/Traits/Commands/EnvironmentWriterTrait.php @@ -0,0 +1,63 @@ +. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +namespace Pterodactyl\Traits\Commands; + +use Pterodactyl\Exceptions\PterodactylException; + +trait EnvironmentWriterTrait +{ + /** + * Update the .env file for the application using the passed in values. + * + * @param array $values + * + * @throws \Pterodactyl\Exceptions\PterodactylException + */ + public function writeToEnvironment(array $values = []) + { + $path = base_path('.env'); + if (! file_exists($path)) { + throw new PterodactylException('Cannot locate .env file, was this software installed correctly?'); + } + + $saveContents = file_get_contents($path); + collect($values)->each(function ($value, $key) use (&$saveContents) { + $key = strtoupper($key); + if (str_contains($value, ' ') && ! preg_match('/\"(.*)\"/', $value)) { + $value = sprintf('"%s"', addslashes($value)); + } + + $saveValue = sprintf('%s=%s', $key, $value); + + if (preg_match_all('/^' . $key . '=(.*)$/m', $saveContents) < 1) { + $saveContents = $saveContents . PHP_EOL . $saveValue; + } else { + $saveContents = preg_replace('/^' . $key . '=(.*)$/m', $saveValue, $saveContents); + } + }); + + file_put_contents($path, $saveContents); + } +} diff --git a/resources/lang/en/command/messages.php b/resources/lang/en/command/messages.php index b4938bb98..d9e719679 100644 --- a/resources/lang/en/command/messages.php +++ b/resources/lang/en/command/messages.php @@ -60,4 +60,20 @@ return [ 'server' => [ 'rebuild_failed' => 'Rebuild request for ":name" (#:id) on node ":node" failed with error: :message', ], + 'environment' => [ + 'mail' => [ + 'ask_smtp_host' => 'SMTP Host (e.g. smtp.google.com)', + 'ask_smtp_port' => 'SMTP Port', + 'ask_smtp_username' => 'SMTP Username', + 'ask_smtp_password' => 'SMTP Password', + 'ask_mailgun_domain' => 'Mailgun Domain', + 'ask_mailgun_secret' => 'Mailgun Secret', + 'ask_mandrill_secret' => 'Mandrill Secret', + 'ask_postmark_username' => 'Postmark API Key', + 'ask_driver' => 'Which driver should be used for sending emails?', + 'ask_mail_from' => 'Email address emails should originate from', + 'ask_mail_name' => 'Name that emails should appear from', + 'ask_encryption' => 'Encryption method to use', + ], + ], ]; diff --git a/tests/Unit/Commands/CommandTestCase.php b/tests/Unit/Commands/CommandTestCase.php index 5c26a06d4..bc4ed7cc3 100644 --- a/tests/Unit/Commands/CommandTestCase.php +++ b/tests/Unit/Commands/CommandTestCase.php @@ -31,6 +31,23 @@ use Symfony\Component\Console\Tester\CommandTester; abstract class CommandTestCase extends TestCase { + /** + * @var bool + */ + protected $commandIsInteractive = true; + + /** + * Set a command to be non-interactive for testing purposes. + * + * @return $this + */ + public function withoutInteraction() + { + $this->commandIsInteractive = false; + + return $this; + } + /** * Return the display from running a command. * @@ -48,6 +65,8 @@ abstract class CommandTestCase extends TestCase $response = new CommandTester($command); $response->setInputs($inputs); + + $opts = array_merge($opts, ['interactive' => $this->commandIsInteractive]); $response->execute($args, $opts); return $response->getDisplay(); diff --git a/tests/Unit/Commands/Environment/EmailSettingsCommandTest.php b/tests/Unit/Commands/Environment/EmailSettingsCommandTest.php new file mode 100644 index 000000000..dba3e9e4f --- /dev/null +++ b/tests/Unit/Commands/Environment/EmailSettingsCommandTest.php @@ -0,0 +1,296 @@ +. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +namespace Tests\Unit\Commands\Environment; + +use Mockery as m; +use Tests\Unit\Commands\CommandTestCase; +use Illuminate\Contracts\Config\Repository; +use Pterodactyl\Console\Commands\Environment\EmailSettingsCommand; + +class EmailSettingsCommandTest extends CommandTestCase +{ + /** + * @var \Pterodactyl\Console\Commands\Environment\EmailSettingsCommand|\Mockery\Mock + */ + protected $command; + + /** + * @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock + */ + protected $config; + + /** + * Setup tests. + */ + public function setUp() + { + parent::setUp(); + + $this->config = m::mock(Repository::class); + $this->command = m::mock(EmailSettingsCommand::class . '[call, writeToEnvironment]', [$this->config]); + $this->command->setLaravel($this->app); + } + + /** + * Test selection of the SMTP driver with no options passed. + */ + public function testSmtpDriverSelection() + { + $data = [ + 'MAIL_DRIVER' => 'smtp', + 'MAIL_HOST' => 'mail.test.com', + 'MAIL_PORT' => '567', + 'MAIL_USERNAME' => 'username', + 'MAIL_PASSWORD' => 'password', + 'MAIL_FROM' => 'mail@from.com', + 'MAIL_FROM_NAME' => 'MailName', + 'MAIL_ENCRYPTION' => 'tls', + ]; + + $this->setupCoreFunctions($data); + $display = $this->runCommand($this->command, [], array_values($data)); + + $this->assertNotEmpty($display); + $this->assertContains('Updating stored environment configuration file.', $display); + } + + /** + * Test that the command can run when all variables are passed in as options. + */ + public function testSmtpDriverSelectionWithOptionsPassed() + { + $data = [ + 'MAIL_DRIVER' => 'smtp', + 'MAIL_HOST' => 'mail.test.com', + 'MAIL_PORT' => '567', + 'MAIL_USERNAME' => 'username', + 'MAIL_PASSWORD' => 'password', + 'MAIL_FROM' => 'mail@from.com', + 'MAIL_FROM_NAME' => 'MailName', + 'MAIL_ENCRYPTION' => 'tls', + ]; + + $this->setupCoreFunctions($data); + $display = $this->withoutInteraction()->runCommand($this->command, [ + '--driver' => $data['MAIL_DRIVER'], + '--email' => $data['MAIL_FROM'], + '--from' => $data['MAIL_FROM_NAME'], + '--encryption' => $data['MAIL_ENCRYPTION'], + '--host' => $data['MAIL_HOST'], + '--port' => $data['MAIL_PORT'], + '--username' => $data['MAIL_USERNAME'], + '--password' => $data['MAIL_PASSWORD'], + ]); + + $this->assertNotEmpty($display); + $this->assertContains('Updating stored environment configuration file.', $display); + } + + /** + * Test selection of PHP mail() as the driver. + */ + public function testPHPMailDriverSelection() + { + $data = [ + 'MAIL_DRIVER' => 'mail', + 'MAIL_FROM' => 'mail@from.com', + 'MAIL_FROM_NAME' => 'MailName', + 'MAIL_ENCRYPTION' => 'tls', + ]; + + $this->setupCoreFunctions($data); + + // The driver flag is passed because there seems to be some issue with the command tester + // when using a choice() method when two keys start with the same letters. + // + // In this case, mail and mailgun. + unset($data['MAIL_DRIVER']); + $display = $this->runCommand($this->command, ['--driver' => 'mail'], array_values($data)); + + $this->assertNotEmpty($display); + $this->assertContains('Updating stored environment configuration file.', $display); + } + + /** + * Test selection of the Mailgun driver with no options passed. + */ + public function testMailgunDriverSelection() + { + $data = [ + 'MAIL_DRIVER' => 'mailgun', + 'MAILGUN_DOMAIN' => 'domain.com', + 'MAILGUN_KEY' => '123456', + 'MAIL_FROM' => 'mail@from.com', + 'MAIL_FROM_NAME' => 'MailName', + 'MAIL_ENCRYPTION' => 'tls', + ]; + + $this->setupCoreFunctions($data); + $display = $this->runCommand($this->command, [], array_values($data)); + + $this->assertNotEmpty($display); + $this->assertContains('Updating stored environment configuration file.', $display); + } + + /** + * Test mailgun driver selection when variables are passed as options. + */ + public function testMailgunDriverSelectionWithOptionsPassed() + { + $data = [ + 'MAIL_DRIVER' => 'mailgun', + 'MAILGUN_DOMAIN' => 'domain.com', + 'MAILGUN_KEY' => '123456', + 'MAIL_FROM' => 'mail@from.com', + 'MAIL_FROM_NAME' => 'MailName', + 'MAIL_ENCRYPTION' => 'tls', + ]; + + $this->setupCoreFunctions($data); + $display = $this->withoutInteraction()->runCommand($this->command, [ + '--driver' => $data['MAIL_DRIVER'], + '--email' => $data['MAIL_FROM'], + '--from' => $data['MAIL_FROM_NAME'], + '--encryption' => $data['MAIL_ENCRYPTION'], + '--host' => $data['MAILGUN_DOMAIN'], + '--password' => $data['MAILGUN_KEY'], + ]); + + $this->assertNotEmpty($display); + $this->assertContains('Updating stored environment configuration file.', $display); + } + + /** + * Test selection of the Mandrill driver with no options passed. + */ + public function testMandrillDriverSelection() + { + $data = [ + 'MAIL_DRIVER' => 'mandrill', + 'MANDRILL_SECRET' => '123456', + 'MAIL_FROM' => 'mail@from.com', + 'MAIL_FROM_NAME' => 'MailName', + 'MAIL_ENCRYPTION' => 'tls', + ]; + + $this->setupCoreFunctions($data); + $display = $this->runCommand($this->command, [], array_values($data)); + + $this->assertNotEmpty($display); + $this->assertContains('Updating stored environment configuration file.', $display); + } + + /** + * Test mandrill driver selection when variables are passed as options. + */ + public function testMandrillDriverSelectionWithOptionsPassed() + { + $data = [ + 'MAIL_DRIVER' => 'mandrill', + 'MANDRILL_SECRET' => '123456', + 'MAIL_FROM' => 'mail@from.com', + 'MAIL_FROM_NAME' => 'MailName', + 'MAIL_ENCRYPTION' => 'tls', + ]; + + $this->setupCoreFunctions($data); + $display = $this->withoutInteraction()->runCommand($this->command, [ + '--driver' => $data['MAIL_DRIVER'], + '--email' => $data['MAIL_FROM'], + '--from' => $data['MAIL_FROM_NAME'], + '--encryption' => $data['MAIL_ENCRYPTION'], + '--password' => $data['MANDRILL_SECRET'], + ]); + + $this->assertNotEmpty($display); + $this->assertContains('Updating stored environment configuration file.', $display); + } + + /** + * Test selection of the Postmark driver with no options passed. + */ + public function testPostmarkDriverSelection() + { + $data = [ + 'MAIL_DRIVER' => 'smtp', + 'MAIL_HOST' => 'smtp.postmarkapp.com', + 'MAIL_PORT' => '587', + 'MAIL_USERNAME' => '123456', + 'MAIL_PASSWORD' => '123456', + 'MAIL_FROM' => 'mail@from.com', + 'MAIL_FROM_NAME' => 'MailName', + 'MAIL_ENCRYPTION' => 'tls', + ]; + + $this->setupCoreFunctions($data); + $display = $this->runCommand($this->command, [], [ + 'postmark', '123456', $data['MAIL_FROM'], $data['MAIL_FROM_NAME'], $data['MAIL_ENCRYPTION'], + ]); + + $this->assertNotEmpty($display); + $this->assertContains('Updating stored environment configuration file.', $display); + } + + /** + * Test postmark driver selection when variables are passed as options. + */ + public function testPostmarkDriverSelectionWithOptionsPassed() + { + $data = [ + 'MAIL_DRIVER' => 'smtp', + 'MAIL_HOST' => 'smtp.postmarkapp.com', + 'MAIL_PORT' => '587', + 'MAIL_USERNAME' => '123456', + 'MAIL_PASSWORD' => '123456', + 'MAIL_FROM' => 'mail@from.com', + 'MAIL_FROM_NAME' => 'MailName', + 'MAIL_ENCRYPTION' => 'tls', + ]; + + $this->setupCoreFunctions($data); + $display = $this->withoutInteraction()->runCommand($this->command, [ + '--driver' => 'postmark', + '--email' => $data['MAIL_FROM'], + '--from' => $data['MAIL_FROM_NAME'], + '--encryption' => $data['MAIL_ENCRYPTION'], + '--username' => $data['MAIL_USERNAME'], + ]); + + $this->assertNotEmpty($display); + $this->assertContains('Updating stored environment configuration file.', $display); + } + + /** + * Setup the core functions that are repeated across all of these tests. + * + * @param array $data + */ + private function setupCoreFunctions(array $data) + { + $this->config->shouldReceive('get')->withAnyArgs()->zeroOrMoreTimes()->andReturnNull(); + $this->command->shouldReceive('writeToEnvironment')->with($data)->once()->andReturnNull(); + $this->command->shouldReceive('call')->with('config:cache')->once()->andReturnNull(); + } +} diff --git a/tests/Unit/Commands/Location/DeleteLocationCommandTest.php b/tests/Unit/Commands/Location/DeleteLocationCommandTest.php index b1fa00504..a2d64a7b2 100644 --- a/tests/Unit/Commands/Location/DeleteLocationCommandTest.php +++ b/tests/Unit/Commands/Location/DeleteLocationCommandTest.php @@ -25,14 +25,13 @@ namespace Tests\Unit\Commands\Location; use Mockery as m; -use Tests\TestCase; use Pterodactyl\Models\Location; -use Symfony\Component\Console\Tester\CommandTester; +use Tests\Unit\Commands\CommandTestCase; use Pterodactyl\Services\Locations\LocationDeletionService; use Pterodactyl\Console\Commands\Location\DeleteLocationCommand; use Pterodactyl\Contracts\Repository\LocationRepositoryInterface; -class DeleteLocationCommandTest extends TestCase +class DeleteLocationCommandTest extends CommandTestCase { /** * @var \Pterodactyl\Console\Commands\Location\DeleteLocationCommand @@ -40,12 +39,12 @@ class DeleteLocationCommandTest extends TestCase protected $command; /** - * @var \Pterodactyl\Services\Locations\LocationDeletionService + * @var \Pterodactyl\Services\Locations\LocationDeletionService|\Mockery\Mock */ protected $deletionService; /** - * @var \Pterodactyl\Contracts\Repository\LocationRepositoryInterface + * @var \Pterodactyl\Contracts\Repository\LocationRepositoryInterface|\Mockery\Mock */ protected $repository; @@ -76,11 +75,8 @@ class DeleteLocationCommandTest extends TestCase $this->repository->shouldReceive('all')->withNoArgs()->once()->andReturn($locations); $this->deletionService->shouldReceive('handle')->with($location2->id)->once()->andReturnNull(); - $response = new CommandTester($this->command); - $response->setInputs([$location2->short]); - $response->execute([]); + $display = $this->runCommand($this->command, [], [$location2->short]); - $display = $response->getDisplay(); $this->assertNotEmpty($display); $this->assertContains(trans('command/messages.location.deleted'), $display); } @@ -98,12 +94,10 @@ class DeleteLocationCommandTest extends TestCase $this->repository->shouldReceive('all')->withNoArgs()->once()->andReturn($locations); $this->deletionService->shouldReceive('handle')->with($location2->id)->once()->andReturnNull(); - $response = new CommandTester($this->command); - $response->execute([ + $display = $this->withoutInteraction()->runCommand($this->command, [ '--short' => $location2->short, ]); - $display = $response->getDisplay(); $this->assertNotEmpty($display); $this->assertContains(trans('command/messages.location.deleted'), $display); } @@ -121,11 +115,8 @@ class DeleteLocationCommandTest extends TestCase $this->repository->shouldReceive('all')->withNoArgs()->once()->andReturn($locations); $this->deletionService->shouldReceive('handle')->with($location2->id)->once()->andReturnNull(); - $response = new CommandTester($this->command); - $response->setInputs(['123_not_exist', 'another_not_exist', $location2->short]); - $response->execute([]); + $display = $this->runCommand($this->command, [], ['123_not_exist', 'another_not_exist', $location2->short]); - $display = $response->getDisplay(); $this->assertNotEmpty($display); $this->assertContains(trans('command/messages.location.no_location_found'), $display); $this->assertContains(trans('command/messages.location.deleted'), $display); @@ -144,10 +135,8 @@ class DeleteLocationCommandTest extends TestCase $this->repository->shouldReceive('all')->withNoArgs()->once()->andReturn($locations); $this->deletionService->shouldNotReceive('handle'); - $response = new CommandTester($this->command); - $response->execute(['--short' => 'randomTestString'], ['interactive' => false]); + $display = $this->withoutInteraction()->runCommand($this->command, ['--short' => 'randomTestString']); - $display = $response->getDisplay(); $this->assertNotEmpty($display); $this->assertContains(trans('command/messages.location.no_location_found'), $display); } diff --git a/tests/Unit/Commands/Location/MakeLocationCommandTest.php b/tests/Unit/Commands/Location/MakeLocationCommandTest.php index 3ae3222eb..d11deefc0 100644 --- a/tests/Unit/Commands/Location/MakeLocationCommandTest.php +++ b/tests/Unit/Commands/Location/MakeLocationCommandTest.php @@ -25,13 +25,12 @@ namespace Tests\Unit\Commands\Location; use Mockery as m; -use Tests\TestCase; use Pterodactyl\Models\Location; -use Symfony\Component\Console\Tester\CommandTester; +use Tests\Unit\Commands\CommandTestCase; use Pterodactyl\Services\Locations\LocationCreationService; use Pterodactyl\Console\Commands\Location\MakeLocationCommand; -class MakeLocationCommandTest extends TestCase +class MakeLocationCommandTest extends CommandTestCase { /** * @var \Pterodactyl\Console\Commands\Location\MakeLocationCommand @@ -39,7 +38,7 @@ class MakeLocationCommandTest extends TestCase protected $command; /** - * @var \Pterodactyl\Services\Locations\LocationCreationService + * @var \Pterodactyl\Services\Locations\LocationCreationService|\Mockery\Mock */ protected $creationService; @@ -68,11 +67,8 @@ class MakeLocationCommandTest extends TestCase 'long' => $location->long, ])->once()->andReturn($location); - $response = new CommandTester($this->command); - $response->setInputs([$location->short, $location->long]); - $response->execute([]); + $display = $this->runCommand($this->command, [], [$location->short, $location->long]); - $display = $response->getDisplay(); $this->assertNotEmpty($display); $this->assertContains(trans('command/messages.location.created', [ 'name' => $location->short, @@ -92,13 +88,11 @@ class MakeLocationCommandTest extends TestCase 'long' => $location->long, ])->once()->andReturn($location); - $response = new CommandTester($this->command); - $response->execute([ + $display = $this->withoutInteraction()->runCommand($this->command, [ '--short' => $location->short, '--long' => $location->long, ]); - $display = $response->getDisplay(); $this->assertNotEmpty($display); $this->assertContains(trans('command/messages.location.created', [ 'name' => $location->short, diff --git a/tests/Unit/Commands/Maintenance/CleanServiceBackupFilesCommandTest.php b/tests/Unit/Commands/Maintenance/CleanServiceBackupFilesCommandTest.php index d6fd1802e..98fa8ea88 100644 --- a/tests/Unit/Commands/Maintenance/CleanServiceBackupFilesCommandTest.php +++ b/tests/Unit/Commands/Maintenance/CleanServiceBackupFilesCommandTest.php @@ -26,16 +26,15 @@ namespace Tests\Unit\Commands\Maintenance; use Mockery as m; use Carbon\Carbon; -use Tests\TestCase; +use Tests\Unit\Commands\CommandTestCase; use Illuminate\Contracts\Filesystem\Factory; use Illuminate\Contracts\Filesystem\Filesystem; -use Symfony\Component\Console\Tester\CommandTester; use Pterodactyl\Console\Commands\Maintenance\CleanServiceBackupFilesCommand; -class CleanServiceBackupFilesCommandTest extends TestCase +class CleanServiceBackupFilesCommandTest extends CommandTestCase { /** - * @var \Carbon\Carbon + * @var \Carbon\Carbon|\Mockery\Mock */ protected $carbon; @@ -45,12 +44,12 @@ class CleanServiceBackupFilesCommandTest extends TestCase protected $command; /** - * @var \Illuminate\Contracts\Filesystem\Filesystem + * @var \Illuminate\Contracts\Filesystem\Filesystem|\Mockery\Mock */ protected $disk; /** - * @var \Illuminate\Contracts\Filesystem\Factory + * @var \Illuminate\Contracts\Filesystem\Factory|\Mockery\Mock */ protected $filesystem; @@ -82,10 +81,8 @@ class CleanServiceBackupFilesCommandTest extends TestCase $this->carbon->shouldReceive('diffInMinutes')->with(null)->once()->andReturn(10); $this->disk->shouldReceive('delete')->with('testfile.txt')->once()->andReturnNull(); - $response = new CommandTester($this->command); - $response->execute([]); + $display = $this->runCommand($this->command); - $display = $response->getDisplay(); $this->assertNotEmpty($display); $this->assertContains(trans('command/messages.maintenance.deleting_service_backup', ['file' => 'testfile.txt']), $display); } @@ -101,10 +98,8 @@ class CleanServiceBackupFilesCommandTest extends TestCase $this->carbon->shouldReceive('now')->withNoArgs()->once()->andReturnNull(); $this->carbon->shouldReceive('diffInMinutes')->with(null)->once()->andReturn(2); - $response = new CommandTester($this->command); - $response->execute([]); + $display = $this->runCommand($this->command); - $display = $response->getDisplay(); $this->assertEmpty($display); } } diff --git a/tests/Unit/Commands/Schedule/ProcessRunnableCommandTest.php b/tests/Unit/Commands/Schedule/ProcessRunnableCommandTest.php index e474e98cb..a05a96e79 100644 --- a/tests/Unit/Commands/Schedule/ProcessRunnableCommandTest.php +++ b/tests/Unit/Commands/Schedule/ProcessRunnableCommandTest.php @@ -83,6 +83,7 @@ class ProcessRunnableCommandTest extends CommandTestCase $this->processScheduleService->shouldReceive('handle')->with($schedule)->once()->andReturnNull(); $display = $this->runCommand($this->command); + $this->assertNotEmpty($display); $this->assertContains(trans('command/messages.schedule.output_line', [ 'schedule' => $schedule->name, @@ -103,6 +104,7 @@ class ProcessRunnableCommandTest extends CommandTestCase $this->repository->shouldReceive('getSchedulesToProcess')->with('00:00:00')->once()->andReturn(collect([$schedule])); $display = $this->runCommand($this->command); + $this->assertNotEmpty($display); $this->assertNotContains(trans('command/messages.schedule.output_line', [ 'schedule' => $schedule->name, @@ -122,6 +124,7 @@ class ProcessRunnableCommandTest extends CommandTestCase $this->repository->shouldReceive('getSchedulesToProcess')->with('00:00:00')->once()->andReturn(collect([$schedule])); $display = $this->runCommand($this->command); + $this->assertNotEmpty($display); $this->assertNotContains(trans('command/messages.schedule.output_line', [ 'schedule' => $schedule->name, diff --git a/tests/Unit/Commands/User/DeleteUserCommandTest.php b/tests/Unit/Commands/User/DeleteUserCommandTest.php index b362dac60..6ea35bdc1 100644 --- a/tests/Unit/Commands/User/DeleteUserCommandTest.php +++ b/tests/Unit/Commands/User/DeleteUserCommandTest.php @@ -25,15 +25,14 @@ namespace Tests\Unit\Commands\User; use Mockery as m; -use Tests\TestCase; use Pterodactyl\Models\User; +use Tests\Unit\Commands\CommandTestCase; use Tests\Assertions\CommandAssertionsTrait; use Pterodactyl\Services\Users\UserDeletionService; -use Symfony\Component\Console\Tester\CommandTester; use Pterodactyl\Console\Commands\User\DeleteUserCommand; use Pterodactyl\Contracts\Repository\UserRepositoryInterface; -class DeleteUserCommandTest extends TestCase +class DeleteUserCommandTest extends CommandTestCase { use CommandAssertionsTrait; @@ -43,12 +42,12 @@ class DeleteUserCommandTest extends TestCase protected $command; /** - * @var \Pterodactyl\Services\Users\UserDeletionService + * @var \Pterodactyl\Services\Users\UserDeletionService|\Mockery\Mock */ protected $deletionService; /** - * @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface + * @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface|\Mockery\Mock */ protected $repository; @@ -80,11 +79,8 @@ class DeleteUserCommandTest extends TestCase ->shouldReceive('all')->withNoArgs()->once()->andReturn($users); $this->deletionService->shouldReceive('handle')->with($user1->id)->once()->andReturnNull(); - $response = new CommandTester($this->command); - $response->setInputs([$user1->username, $user1->id, 'yes']); - $response->execute([]); + $display = $this->runCommand($this->command, [], [$user1->username, $user1->id, 'yes']); - $display = $response->getDisplay(); $this->assertNotEmpty($display); $this->assertTableContains($user1->id, $display); $this->assertTableContains($user1->email, $display); @@ -107,11 +103,8 @@ class DeleteUserCommandTest extends TestCase ->shouldReceive('all')->withNoArgs()->once()->andReturn($users); $this->deletionService->shouldReceive('handle')->with($user1->id)->once()->andReturnNull(); - $response = new CommandTester($this->command); - $response->setInputs(['noResults', $user1->username, $user1->id, 'yes']); - $response->execute([]); + $display = $this->runCommand($this->command, [], ['noResults', $user1->username, $user1->id, 'yes']); - $display = $response->getDisplay(); $this->assertNotEmpty($display); $this->assertContains(trans('command/messages.user.no_users_found'), $display); $this->assertTableContains($user1->id, $display); @@ -133,11 +126,8 @@ class DeleteUserCommandTest extends TestCase ->shouldReceive('all')->withNoArgs()->twice()->andReturn($users); $this->deletionService->shouldReceive('handle')->with($user1->id)->once()->andReturnNull(); - $response = new CommandTester($this->command); - $response->setInputs([$user1->username, 0, $user1->username, $user1->id, 'yes']); - $response->execute([]); + $display = $this->runCommand($this->command, [], [$user1->username, 0, $user1->username, $user1->id, 'yes']); - $display = $response->getDisplay(); $this->assertNotEmpty($display); $this->assertContains(trans('command/messages.user.select_search_user'), $display); $this->assertTableContains($user1->id, $display); @@ -159,11 +149,8 @@ class DeleteUserCommandTest extends TestCase ->shouldReceive('all')->withNoArgs()->once()->andReturn($users); $this->deletionService->shouldNotReceive('handle'); - $response = new CommandTester($this->command); - $response->setInputs([$user1->username, $user1->id, 'no']); - $response->execute([]); + $display = $this->runCommand($this->command, [], [$user1->username, $user1->id, 'no']); - $display = $response->getDisplay(); $this->assertNotEmpty($display); $this->assertNotContains(trans('command/messages.user.deleted'), $display); } @@ -181,10 +168,8 @@ class DeleteUserCommandTest extends TestCase ->shouldReceive('all')->withNoArgs()->once()->andReturn($users); $this->deletionService->shouldReceive('handle')->with($user1)->once()->andReturnNull(); - $response = new CommandTester($this->command); - $response->execute(['--user' => $user1->username], ['interactive' => false]); + $display = $this->withoutInteraction()->runCommand($this->command, ['--user' => $user1->username]); - $display = $response->getDisplay(); $this->assertNotEmpty($display); $this->assertContains(trans('command/messages.user.deleted'), $display); } @@ -203,10 +188,8 @@ class DeleteUserCommandTest extends TestCase ->shouldReceive('all')->withNoArgs()->once()->andReturn($users); $this->deletionService->shouldNotReceive('handle'); - $response = new CommandTester($this->command); - $response->execute(['--user' => $user1->username], ['interactive' => false]); + $display = $this->withoutInteraction()->runCommand($this->command, ['--user' => $user1->username]); - $display = $response->getDisplay(); $this->assertNotEmpty($display); $this->assertContains(trans('command/messages.user.multiple_found'), $display); } @@ -219,10 +202,8 @@ class DeleteUserCommandTest extends TestCase $this->repository->shouldReceive('search')->with(123456)->once()->andReturnSelf() ->shouldReceive('all')->withNoArgs()->once()->andReturn([]); - $response = new CommandTester($this->command); - $response->execute(['--user' => 123456], ['interactive' => false]); + $display = $this->withoutInteraction()->runCommand($this->command, ['--user' => 123456]); - $display = $response->getDisplay(); $this->assertNotEmpty($display); $this->assertContains(trans('command/messages.user.no_users_found'), $display); } diff --git a/tests/Unit/Commands/User/DisableTwoFactorCommandTest.php b/tests/Unit/Commands/User/DisableTwoFactorCommandTest.php index 644155b7d..60669f41a 100644 --- a/tests/Unit/Commands/User/DisableTwoFactorCommandTest.php +++ b/tests/Unit/Commands/User/DisableTwoFactorCommandTest.php @@ -25,13 +25,12 @@ namespace Tests\Unit\Commands\User; use Mockery as m; -use Tests\TestCase; use Pterodactyl\Models\User; -use Symfony\Component\Console\Tester\CommandTester; +use Tests\Unit\Commands\CommandTestCase; use Pterodactyl\Contracts\Repository\UserRepositoryInterface; use Pterodactyl\Console\Commands\User\DisableTwoFactorCommand; -class DisableTwoFactorCommandTest extends TestCase +class DisableTwoFactorCommandTest extends CommandTestCase { /** * @var \Pterodactyl\Console\Commands\User\DisableTwoFactorCommand @@ -39,10 +38,13 @@ class DisableTwoFactorCommandTest extends TestCase protected $command; /** - * @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface + * @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface|\Mockery\Mock */ protected $repository; + /** + * Setup tests. + */ public function setUp() { parent::setUp(); @@ -68,11 +70,8 @@ class DisableTwoFactorCommandTest extends TestCase 'totp_secret' => null, ])->once()->andReturnNull(); - $response = new CommandTester($this->command); - $response->setInputs([$user->email]); - $response->execute([]); + $display = $this->runCommand($this->command, [], [$user->email]); - $display = $response->getDisplay(); $this->assertNotEmpty($display); $this->assertContains(trans('command/messages.user.2fa_disabled', ['email' => $user->email]), $display); } @@ -92,12 +91,7 @@ class DisableTwoFactorCommandTest extends TestCase 'totp_secret' => null, ])->once()->andReturnNull(); - $response = new CommandTester($this->command); - $response->execute([ - '--email' => $user->email, - ]); - - $display = $response->getDisplay(); + $display = $this->withoutInteraction()->runCommand($this->command, ['--email' => $user->email]); $this->assertNotEmpty($display); $this->assertContains(trans('command/messages.user.2fa_disabled', ['email' => $user->email]), $display); } diff --git a/tests/Unit/Commands/User/MakeUserCommandTest.php b/tests/Unit/Commands/User/MakeUserCommandTest.php index 19c08dbe8..694f54329 100644 --- a/tests/Unit/Commands/User/MakeUserCommandTest.php +++ b/tests/Unit/Commands/User/MakeUserCommandTest.php @@ -25,13 +25,12 @@ namespace Tests\Unit\Commands\User; use Mockery as m; -use Tests\TestCase; use Pterodactyl\Models\User; +use Tests\Unit\Commands\CommandTestCase; use Pterodactyl\Services\Users\UserCreationService; -use Symfony\Component\Console\Tester\CommandTester; use Pterodactyl\Console\Commands\User\MakeUserCommand; -class MakeUserCommandTest extends TestCase +class MakeUserCommandTest extends CommandTestCase { /** * @var \Pterodactyl\Console\Commands\User\MakeUserCommand @@ -72,13 +71,10 @@ class MakeUserCommandTest extends TestCase 'root_admin' => $user->root_admin, ])->once()->andReturn($user); - $response = new CommandTester($this->command); - $response->setInputs([ + $display = $this->runCommand($this->command, [], [ 'yes', $user->email, $user->username, $user->name_first, $user->name_last, 'Password123', ]); - $response->execute([]); - $display = $response->getDisplay(); $this->assertNotEmpty($display); $this->assertContains(trans('command/messages.user.ask_password_help'), $display); $this->assertContains($user->uuid, $display); @@ -104,13 +100,10 @@ class MakeUserCommandTest extends TestCase 'root_admin' => $user->root_admin, ])->once()->andReturn($user); - $response = new CommandTester($this->command); - $response->setInputs([ + $display = $this->runCommand($this->command, ['--no-password' => true], [ 'yes', $user->email, $user->username, $user->name_first, $user->name_last, ]); - $response->execute(['--no-password' => true]); - $display = $response->getDisplay(); $this->assertNotEmpty($display); $this->assertNotContains(trans('command/messages.user.ask_password_help'), $display); } @@ -131,8 +124,7 @@ class MakeUserCommandTest extends TestCase 'root_admin' => $user->root_admin, ])->once()->andReturn($user); - $response = new CommandTester($this->command); - $response->execute([ + $display = $this->withoutInteraction()->runCommand($this->command, [ '--email' => $user->email, '--username' => $user->username, '--name-first' => $user->name_first, @@ -141,7 +133,6 @@ class MakeUserCommandTest extends TestCase '--admin' => 0, ]); - $display = $response->getDisplay(); $this->assertNotEmpty($display); $this->assertNotContains(trans('command/messages.user.ask_password_help'), $display); $this->assertContains($user->uuid, $display);