diff --git a/app/Commands/Sites/GetCommand.php b/app/Commands/Sites/GetCommand.php new file mode 100644 index 0000000..f10e718 --- /dev/null +++ b/app/Commands/Sites/GetCommand.php @@ -0,0 +1,115 @@ +spinupwp->sites->get($this->argument('site_id')); + + if ($this->displayFormat() === 'json') { + return $site; + } + $additionalDomains = ''; + + if (!empty($site->additional_domains)) { + $additionalDomains = implode(PHP_EOL, array_map(fn ($domain) => $domain['domain'], $site->additional_domains)); + } + + $data = [ + 'ID' => $site->id, + 'Server ID' => $site->server_id, + 'Domain' => $site->domain, + 'Additional Domains' => $additionalDomains, + 'Site User' => $site->site_user, + 'PHP Version' => $site->php_version, + 'Public Folder' => $site->public_folder, + 'Page Cache' => $site->page_cache['enabled'] ? 'Enabled' : 'Disabled', + 'HTTPS' => $site->https['enabled'] ? 'Enabled' : 'Disabled', + ]; + + if ($site->database['table_prefix']) { + $data['Database Table Prefix'] = $site->database['table_prefix']; + } + + $data = $this->gitData($site, $data); + + $data['WP Core Update'] = $site->wp_core_update ? 'Yes' : 'No'; + $data['WP Theme Updates'] = $site->wp_theme_updates; + $data['WP Plugin Updates'] = $site->wp_plugin_updates; + + $data = $this->backupsData($site, $data); + + $data['Uploads Directory Protection'] = $site->nginx['uploads_directory_protected'] ? 'Enabled' : 'Disabled'; + $data['XML-RPC Protection'] = $site->nginx['xmlrpc_protected'] ? 'Enabled' : 'Disabled'; + $data['Multisite Rewrite Rules'] = $site->nginx['subdirectory_rewrite_in_place'] ? 'Enabled' : 'Disabled'; + + $data = $this->basicAuthData($site, $data); + + $data['Created At'] = $site->created_at; + $data['Status'] = ucfirst($site->status); + + return $data; + } + + public function backupsData(Site $site, array $data): array + { + $scheduledBackups = (bool) $site->backups['next_run_time']; + + $data['Scheduled Backups'] = $scheduledBackups ? 'Enabled' : 'Disabled'; + + $data['File Backups'] = ($site->backups['files'] ? 'Enabled' : 'Disabled'); + $data['Database Backups'] = ($site->backups['database'] ? 'Enabled' : 'Disabled'); + + if ($site->backups['files'] || $site->backups['database']) { + $data['Backup Retention Period'] = $site->backups['retention_period'] . ' days'; + } + + if ($scheduledBackups) { + $data['Next Backup Time'] = $site->backups['next_run_time']; + } + + return $data; + } + + public function gitData(Site $site, array $data): array + { + $data['Git'] = 'Disabled'; + + if ($site->git['enabled']) { + $data['Git'] = 'Enabled'; + $data['Repository'] = $site->git['repo']; + $data['Branch'] = $site->git['branch']; + $data['Deploy Script'] = $site->git['deploy_script']; + $data['Push-to-deploy'] = $site->git['push_enabled'] ? 'Enabled' : 'Disabled'; + } + + if ($site->git['enabled'] && $site->git['push_enabled']) { + $data['Deployment URL'] = $site->git['deployment_url']; + } + + return $data; + } + + public function basicAuthData(Site $site, array $data): array + { + $data['Basic Auth'] = 'Disabled'; + + if ($site->basic_auth['enabled']) { + $data['Basic Auth'] = 'Enabled'; + $data['Basic Auth Username'] = $site->basic_auth['username']; + } + + return $data; + } +} diff --git a/tests/Feature/Commands/ServersListCommandTest.php b/tests/Feature/Commands/ServersListCommandTest.php index bb38e88..9df310f 100644 --- a/tests/Feature/Commands/ServersListCommandTest.php +++ b/tests/Feature/Commands/ServersListCommandTest.php @@ -53,18 +53,14 @@ test('servers json list command', function () use ($response) { $this->clientMock->shouldReceive('request')->once()->with('GET', 'servers?page=1', [])->andReturn( - new Response(200, [], json_encode([ - 'data' => $response, - ])) + new Response(200, [], listResponseJson($response)) ); $this->artisan('servers:list')->expectsOutput(json_encode($response, JSON_PRETTY_PRINT)); }); test('servers table list command', function () use ($response) { $this->clientMock->shouldReceive('request')->once()->with('GET', 'servers?page=1', [])->andReturn( - new Response(200, [], json_encode([ - 'data' => $response, - ])) + new Response(200, [], listResponseJson($response)) ); $this->artisan('servers:list --format table')->expectsTable( ['ID', 'Name', 'IP Address', 'Ubuntu', 'Database'], @@ -89,9 +85,7 @@ test('empty servers list', function () { $this->clientMock->shouldReceive('request')->with('GET', 'servers?page=1', [])->andReturn( - new Response(200, [], json_encode([ - 'data' => [], - ])) + new Response(200, [], listResponseJson([])) ); $this->artisan('servers:list')->expectsOutput('No servers found.'); }); diff --git a/tests/Feature/Commands/SitesGetCommandTest.php b/tests/Feature/Commands/SitesGetCommandTest.php new file mode 100644 index 0000000..870311a --- /dev/null +++ b/tests/Feature/Commands/SitesGetCommandTest.php @@ -0,0 +1,123 @@ + 1, + 'server_id' => 1, + 'domain' => 'hellfish.media', + 'additional_domains' => [ + [ + 'domain' => 'www.hellfish.media', + 'redirect' => [ + 'enabled' => true, + ], + 'created_at' => '2019-08-24T14:15:22Z', + ], + ], + 'site_user' => 'hellfishmedia', + 'php_version' => '7.4', + 'public_folder' => '/', + 'is_wordpress' => true, + 'page_cache' => [ + 'enabled' => true, + ], + 'https' => [ + 'enabled' => true, + 'certificate_path' => '/etc/nginx/ssl/hellfish.media/certificate-bundle.crt', + 'private_key_path' => '/etc/nginx/ssl/hellfish.media/private-key.key', + ], + 'nginx' => [ + 'uploads_directory_protected' => true, + 'xmlrpc_protected' => true, + 'subdirectory_rewrite_in_place' => false, + ], + 'database' => [ + 'id' => 1, + 'user_id' => 1, + 'table_prefix' => 'wp_', + ], + 'backups' => [ + 'files' => true, + 'database' => true, + 'paths_to_exclude' => 'node_modules\\n/files/vendor', + 'retention_period' => 30, + 'next_run_time' => '2021-01-01T12:00:00.000000Z', + 'storage_provider' => [ + 'id' => 1, + 'region' => 'nyc3', + 'bucket' => 'hellfish-media', + ], + ], + 'wp_core_update' => true, + 'wp_theme_updates' => 0, + 'wp_plugin_updates' => 3, + 'git' => [ + 'enabled' => true, + 'repo' => 'git@github.com:deliciousbrains/spinupwp-composer-site.git', + 'branch' => 'main', + 'deploy_script' => 'composer install --optimize-autoload --no-dev', + 'push_enabled' => true, + 'deployment_url' => 'https://api.spinupwp.app/git/jeJLdKrl63/deploy', + ], + 'basic_auth' => [ + 'enabled' => true, + 'username' => 'hellfish', + ], + 'created_at' => '2021-01-01T12:00:00.000000Z', + 'status' => 'deployed', + +]; +beforeEach(function () use ($response) { + setTestConfigFile(); +}); + +afterEach(function () { + deleteTestConfigFile(); +}); + +test('sites json get command', function () use ($response) { + $this->clientMock->shouldReceive('request')->with('GET', 'sites/1', [])->andReturn( + new Response(200, [], json_encode(['data' => $response])) + ); + $this->artisan('sites:get 1')->expectsOutput(json_encode($response, JSON_PRETTY_PRINT)); +}); + +test('sites table get command', function () use ($response) { + $this->clientMock->shouldReceive('request')->with('GET', 'sites/1', [])->andReturn( + new Response(200, [], json_encode(['data' => $response])) + ); + $this->artisan('sites:get 1 --format=table')->expectsTable([], [ + ['ID', '1'], + ['Server ID', '1'], + ['Domain', 'hellfish.media'], + ['Additional Domains', 'www.hellfish.media'], + ['Site User', 'hellfishmedia'], + ['PHP Version', '7.4'], + ['Public Folder', '/'], + ['Page Cache', 'Enabled'], + ['HTTPS', 'Enabled'], + ['Database Table Prefix', 'wp_'], + ['Git', 'Enabled'], + ['Repository', 'git@github.com:deliciousbrains/spinupwp-composer-site.git'], + ['Branch', 'main'], + ['Deploy Script', 'composer install --optimize-autoload --no-dev'], + ['Push-to-deploy', 'Enabled'], + ['Deployment URL', 'https://api.spinupwp.app/git/jeJLdKrl63/deploy'], + ['WP Core Update', 'Yes'], + ['WP Theme Updates', '0'], + ['WP Plugin Updates', '3'], + ['Scheduled Backups', 'Enabled'], + ['File Backups', 'Enabled'], + ['Database Backups', 'Enabled'], + ['Backup Retention Period', '30 days'], + ['Next Backup Time', '2021-01-01T12:00:00.000000Z'], + ['Uploads Directory Protection', 'Enabled'], + ['XML-RPC Protection', 'Enabled'], + ['Multisite Rewrite Rules', 'Disabled'], + ['Basic Auth', 'Enabled'], + ['Basic Auth Username', 'hellfish'], + ['Created At', '2021-01-01T12:00:00.000000Z'], + ['Status', 'Deployed'], + ]); +}); diff --git a/tests/Feature/Commands/SitesListCommandTest.php b/tests/Feature/Commands/SitesListCommandTest.php index c5baddb..4a9bb68 100644 --- a/tests/Feature/Commands/SitesListCommandTest.php +++ b/tests/Feature/Commands/SitesListCommandTest.php @@ -46,18 +46,14 @@ test('sites json list command', function () use ($response) { $this->clientMock->shouldReceive('request')->with('GET', 'sites?page=1', [])->andReturn( - new Response(200, [], json_encode([ - 'data' => $response, - ])) + new Response(200, [], listResponseJson($response)) ); $this->artisan('sites:list')->expectsOutput(json_encode($response, JSON_PRETTY_PRINT)); }); test('sites table list command', function () use ($response) { $this->clientMock->shouldReceive('request')->with('GET', 'sites?page=1', [])->andReturn( - new Response(200, [], json_encode([ - 'data' => $response, - ])) + new Response(200, [], listResponseJson($response)) ); $this->artisan('sites:list --format table')->expectsTable( ['ID', 'Server ID', 'Domain', 'Site User', 'PHP', 'Page Cache', 'HTTPS'], @@ -86,9 +82,7 @@ test('empty sites list', function () { $this->clientMock->shouldReceive('request')->with('GET', 'sites?page=1', [])->andReturn( - new Response(200, [], json_encode([ - 'data' => [], - ])) + new Response(200, [], listResponseJson([])) ); $this->artisan('sites:list')->expectsOutput('No sites found.'); }); diff --git a/tests/Pest.php b/tests/Pest.php index 92517b1..e82e738 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -70,3 +70,15 @@ function deleteTestConfigFile($test = '') } unlink($configFile); } + +function listResponseJson(array $data): string +{ + return json_encode([ + 'data' => $data, + 'pagination' => [ + 'previous' => null, + 'next' => null, + 'count' => count($data), + ], + ]); +}