diff --git a/.env.dist b/.env.dist index d53dbdcae..bbf91e14d 100644 --- a/.env.dist +++ b/.env.dist @@ -1,9 +1,9 @@ export PROJECT_PATH=$(realpath .) export TERMINUS_SITE="terminus-test-site" -export TERMINUS_SITE_WP="terminus-test-site-wordpress" +export TERMINUS_SITE_WP="terminus-test-wp-site" export TERMINUS_SITE_WP_NETWORK="terminus-test-site-wp-network" export TERMINUS_ENV="dev" -export TERMINUS_ORG="Agency" +export TERMINUS_ORG="agency-org" export TERMINUS_USER="devuser@pantheon.io" ## To Autoload your token from your local machine, change the TERMINUS_TOKEN to the following command: ## export TERMINUS_TOKEN=$(cat $HOME/.terminus/cache/tokens/your.email@getpantheon.com | jq -r .token) diff --git a/scripts/make-wordpress-multisite.sh b/scripts/make-wordpress-multisite.sh new file mode 100755 index 000000000..8605cde5c --- /dev/null +++ b/scripts/make-wordpress-multisite.sh @@ -0,0 +1,61 @@ +#!/usr/bin/env bash + +set -e + +TERMINUS_SITE_WP_NETWORK_UPSTREAM="word_press_multisite" +TERMINUS_SITE_WP_NETWORK_TITLE="TERMINUS-TEST-SITE" +TERMINUS_SITE_WP_NETWORK_ADMIN_USER="admin" +TERMINUS_SITE_WP_NETWORK_ADMIN_EMAIL="admin@pantheon.io" +LOCAL_CLONED_DIRECTORY="${HOME}/pantheon-local-copies/${TERMINUS_SITE_WP_NETWORK}" + +if test -z "${TERMINUS_SITE_WP_NETWORK}" +then + echo "Please set the following environment variables:" + echo "TERMINUS_SITE_WP_NETWORK" + echo "TERMINUS_SITE_WP_NETWORK_LABEL" + echo "TERMINUS_ORG" + exit 1 +fi + +if terminus site:info ${TERMINUS_SITE_WP_NETWORK} --format=json | jq -r .id +then + echo "${TERMINUS_SITE_WP_NETWORK} exists... skipping creation..." +else + echo "${TERMINUS_SITE_WP_NETWORK} doesn't exist... creating..." + terminus site:create \ + "${TERMINUS_SITE_WP_NETWORK}" \ + "${TERMINUS_SITE_WP_NETWORK}" \ + "${TERMINUS_SITE_WP_NETWORK_UPSTREAM}" \ + --org="${TERMINUS_ORG}" +fi + +if [ ! -d "${LOCAL_CLONED_DIRECTORY}" ] +then + echo "Cloning ${TERMINUS_SITE_WP_NETWORK}" + terminus local:clone \ + "${TERMINUS_SITE_WP_NETWORK}" +fi + +terminus wp ${TERMINUS_SITE_WP_NETWORK}.dev -- core install \ + --title="${TERMINUS_SITE_WP_NETWORK_TITLE}" \ + --admin_user="${TERMINUS_SITE_WP_NETWORK_ADMIN_USER}" \ + --admin_email="${TERMINUS_SITE_WP_NETWORK_ADMIN_EMAIL}" + +terminus wp ${TERMINUS_SITE_WP_NETWORK}.dev -- core multisite-install \ + --title="${TERMINUS_SITE_WP_NETWORK_TITLE}" \ + --admin_user="${TERMINUS_SITE_WP_NETWORK_ADMIN_USER}" \ + --admin_email="${TERMINUS_SITE_WP_NETWORK_ADMIN_EMAIL}" + +terminus wp ${TERMINUS_SITE_WP_NETWORK}.dev -- plugin install \ + pantheon-advanced-page-cache \ + --activate + +terminus env:commit \ + "${TERMINUS_SITE_WP_NETWORK}.dev" \ + --message="Initial commit" + +terminus connection:set \ + "${TERMINUS_SITE_WP_NETWORK}.dev" \ + git + +cd "${LOCAL_CLONED_DIRECTORY}" && git pull diff --git a/src/Commands/Env/CloneContentCommand.php b/src/Commands/Env/CloneContentCommand.php index 018b8a39b..53c6d2559 100644 --- a/src/Commands/Env/CloneContentCommand.php +++ b/src/Commands/Env/CloneContentCommand.php @@ -12,6 +12,7 @@ /** * Class CloneContentCommand + * * @package Pantheon\Terminus\Commands\Env */ class CloneContentCommand extends TerminusCommand implements SiteAwareInterface @@ -23,6 +24,7 @@ class CloneContentCommand extends TerminusCommand implements SiteAwareInterface * @var Environment */ private $source_env; + /** * @var Environment */ @@ -43,9 +45,11 @@ class CloneContentCommand extends TerminusCommand implements SiteAwareInterface * * @command env:clone-content * - * @param string $site_env Origin site & environment in the format `site-name.env` + * @param string $site_env Origin site & environment in the format + * `site-name.env` * @param string $target_env Target environment * @param array $options + * * @option bool $cc Whether or not to clear caches * @option bool $db-only Only clone database * @option bool $files-only Only clone files @@ -55,12 +59,21 @@ class CloneContentCommand extends TerminusCommand implements SiteAwareInterface * * @throws \Pantheon\Terminus\Exceptions\TerminusException * - * @usage . Clones database and files from 's environment to environment. - * @usage . --cc Clones from 's environment to environment and clears the cache. - * @usage . --db-only Clones only the database from 's environment to environment. - * @usage . --files-only Clones only files from 's environment to environment. - * @usage . --updatedb Clones from 's environment to environment and updates the Drupal database (if applicable). - * @usage . --from-url=www.example.com --to-url=mulitidevenv.example.com (WordPress only) Clones from 's environment to environment and replaces www.example.com with mulitidevenv.example.com in the database. + * @usage . Clones database and files from 's + * environment to environment. + * @usage . --cc Clones from 's + * environment to environment and clears the cache. + * @usage . --db-only Clones only the database from + * 's environment to environment. + * @usage . --files-only Clones only files from + * 's environment to environment. + * @usage . --updatedb Clones from 's + * environment to environment and updates the Drupal + * database (if applicable). + * @usage . --from-url=www.example.com + * --to-url=mulitidevenv.example.com (WordPress only) Clones from + * 's environment to environment and replaces + * www.example.com with mulitidevenv.example.com in the database. */ public function cloneContent( $site_env, @@ -75,7 +88,9 @@ public function cloneContent( ] ) { if (!empty($options['db-only']) && !empty($options['files-only'])) { - throw new TerminusException('You cannot specify both --db-only and --files-only'); + throw new TerminusException( + 'You cannot specify both --db-only and --files-only' + ); } $this->requireSiteIsNotFrozen($site_env); @@ -84,7 +99,9 @@ public function cloneContent( $this->target_env = $site->getEnvironments()->get($target_env); if ($this->source_env->id === $target_env) { - $this->log()->notice('The clone has been skipped because the source and target environments are the same.'); + $this->log()->notice( + 'The clone has been skipped because the source and target environments are the same.' + ); return; } @@ -113,8 +130,10 @@ public function cloneContent( // environment. if ($site->getFramework()->isWordpressFramework()) { $options['search-replace'] = [ - 'from_url' => $options['from-url'] ?? $this->source_env->domain(), - 'to_url' => $options['to-url'] ?? $this->target_env->domain(), + 'from_url' => $options['from-url'] ?? $this->source_env->domain( + ), + 'to_url' => $options['to-url'] ?? $this->target_env->domain( + ), ]; } $this->cloneDatabase($options); @@ -122,11 +141,14 @@ public function cloneContent( } /** - * Checks to see whether the indicated environment is initialized and stops the process if it isn't + * Checks to see whether the indicated environment is initialized and stops + * the process if it isn't * * @param Environment $env * @param string $direction "into" or "from" are recommended. - * @throws TerminusException Thrown if the passed-in environment is not initialized + * + * @throws TerminusException Thrown if the passed-in environment is not + * initialized */ private function checkForInitialization(Environment $env, $direction = '') { @@ -175,7 +197,10 @@ private function emitNotice($element) { $this->log()->notice( "Cloning {$element} from {source} environment to {target} environment", - ['source' => $this->source_env->getName(), 'target' => $this->target_env->getName(),] + [ + 'source' => $this->source_env->getName(), + 'target' => $this->target_env->getName(), + ] ); } diff --git a/src/Models/Site.php b/src/Models/Site.php index 9b32c6d7f..208845373 100755 --- a/src/Models/Site.php +++ b/src/Models/Site.php @@ -118,6 +118,11 @@ class Site extends TerminusModel implements */ public $tags; + /** + * @var string + */ + public $framework; + /** * Add a payment method to the given site * diff --git a/tests/Functional/EnvCommandsTest.php b/tests/Functional/EnvCommandsTest.php index 5414b9a59..7fde6fd9b 100644 --- a/tests/Functional/EnvCommandsTest.php +++ b/tests/Functional/EnvCommandsTest.php @@ -15,9 +15,13 @@ class EnvCommandsTest extends TerminusTestBase * * @return string|null */ - protected function ensureSiteEnvironment(string $siteName, string $envName): ?string - { - return $this->terminus(sprintf('multidev:create %s.dev %s', $siteName, $envName)); + protected function ensureSiteEnvironment( + string $siteName, + string $envName + ): ?string { + return $this->terminus( + sprintf('multidev:create %s.dev %s', $siteName, $envName) + ); } /** @@ -26,9 +30,13 @@ protected function ensureSiteEnvironment(string $siteName, string $envName): ?st * * @return string|null */ - protected function deleteSiteEnvironment(string $siteName, string $envName): ?string - { - return $this->terminus(sprintf('multidev:delete %s.%s', $siteName, $envName)); + protected function deleteSiteEnvironment( + string $siteName, + string $envName + ): ?string { + return $this->terminus( + sprintf('multidev:delete %s.%s', $siteName, $envName) + ); } @@ -53,7 +61,9 @@ public function testClearCacheCommand() */ public function testDeployCommand() { - $this->terminus(sprintf('env:deploy %s.%s', $this->getSiteName(), 'live')); + $this->terminus( + sprintf('env:deploy %s.%s', $this->getSiteName(), 'live') + ); } /** @@ -65,7 +75,14 @@ public function testDeployCommand() */ public function testCloneContentCommand() { - $this->terminus(sprintf('env:clone-content %s.%s %s', $this->getSiteName(), 'dev', $this->getMdEnv())); + $this->terminus( + sprintf( + 'env:clone-content %s.%s %s', + $this->getSiteName(), + 'dev', + $this->getMdEnv() + ) + ); } /** @@ -74,13 +91,56 @@ public function testCloneContentCommand() * * @group env * @group long + * @group wordpress */ public function testCloneContentForWordpressCommand() { // 1. Ensure that the multidev environment exists - $this->ensureSiteEnvironment($this->getSiteName("wordpress"), $this->getMdEnv()); - $this->terminus(sprintf('env:clone-content %s.%s %s', $this->getSiteName(), 'dev', $this->getMdEnv())); - $this->deleteSiteEnvironment($this->getSiteName("wordpress"), $this->getMdEnv()); + $this->ensureSiteEnvironment( + $this->getSiteName("wordpress"), + $this->getMdEnv() + ); + $this->terminus( + sprintf( + 'env:clone-content %1$s.%2$s %3$s', + $this->getSiteName("wordpress"), + 'dev', + $this->getMdEnv() + ) + ); + $this->deleteSiteEnvironment( + $this->getSiteName("wordpress"), + $this->getMdEnv() + ); + } + + /** + * @test + * @covers \Pantheon\Terminus\Commands\Env\CloneContentCommand + * + * @group env + * @group long + * @group wordpress + */ + public function testCloneContentForWordpressNetworkCommand() + { + // 1. Ensure that the multidev environment exists + $this->ensureSiteEnvironment( + $this->getSiteName("wordpress_network"), + $this->getMdEnv() + ); + $this->terminus( + sprintf( + 'env:clone-content %s.%s %s', + $this->getSiteName("wordpress_network"), + 'dev', + $this->getMdEnv() + ) + ); + $this->deleteSiteEnvironment( + $this->getSiteName("wordpress_network"), + $this->getMdEnv() + ); } /** @@ -92,7 +152,9 @@ public function testCloneContentForWordpressCommand() */ public function testCodelogCommand() { - $codeLogs = $this->terminusJsonResponse(sprintf('env:code-log %s', $this->getSiteEnv())); + $codeLogs = $this->terminusJsonResponse( + sprintf('env:code-log %s', $this->getSiteEnv()) + ); $this->assertIsArray($codeLogs); $this->assertNotEmpty($codeLogs); $codeLog = array_shift($codeLogs); @@ -143,7 +205,9 @@ public function testCommitAndDiffStatCommands() sleep(60); // Check the diff - no diff is expected. - $diff = $this->terminusJsonResponse(sprintf('env:diffstat %s', $siteEnv)); + $diff = $this->terminusJsonResponse( + sprintf('env:diffstat %s', $siteEnv) + ); $this->assertEquals([], $diff); // Upload a test file to the site. @@ -158,9 +222,14 @@ public function testCommitAndDiffStatCommands() 'additions' => '1', ], ]; - $this->assertTerminusCommandResultEqualsInAttempts(function () use ($siteEnv) { - return $this->terminusJsonResponse(sprintf('env:diffstat %s', $siteEnv)); - }, $expectedDiff); + $this->assertTerminusCommandResultEqualsInAttempts( + function () use ($siteEnv) { + return $this->terminusJsonResponse( + sprintf('env:diffstat %s', $siteEnv) + ); + }, + $expectedDiff + ); // Commit the changes. $this->terminus( @@ -173,9 +242,14 @@ public function testCommitAndDiffStatCommands() sleep(60); // Check the diff - no diff is expected. - $this->assertTerminusCommandResultEqualsInAttempts(function () use ($siteEnv) { - return $this->terminusJsonResponse(sprintf('env:diffstat %s', $siteEnv)); - }, []); + $this->assertTerminusCommandResultEqualsInAttempts( + function () use ($siteEnv) { + return $this->terminusJsonResponse( + sprintf('env:diffstat %s', $siteEnv) + ); + }, + [] + ); } /** @@ -187,7 +261,9 @@ public function testCommitAndDiffStatCommands() */ public function testInfoCommand() { - $envInfo = $this->terminusJsonResponse(sprintf('env:info %s', $this->getSiteEnv())); + $envInfo = $this->terminusJsonResponse( + sprintf('env:info %s', $this->getSiteEnv()) + ); $this->assertIsArray($envInfo); $this->assertArrayHasKey( 'id', @@ -220,10 +296,16 @@ public function testInfoCommand() */ public function testMetricsCommand() { - $metrics = $this->terminusJsonResponse(sprintf('env:metrics %s', $this->getSiteEnv())); + $metrics = $this->terminusJsonResponse( + sprintf('env:metrics %s', $this->getSiteEnv()) + ); $this->assertIsArray($metrics); $this->assertNotEmpty($metrics); - $this->assertArrayHasKey('timeseries', $metrics, 'Metrics should have "timeseries" field.'); + $this->assertArrayHasKey( + 'timeseries', + $metrics, + 'Metrics should have "timeseries" field.' + ); $metric = array_shift($metrics['timeseries']); $this->assertIsArray($metric); $this->assertNotEmpty($metric); @@ -268,7 +350,9 @@ public function testMetricsCommand() */ public function testListCommand() { - $envs = $this->terminusJsonResponse(sprintf('env:list %s', $this->getSiteName())); + $envs = $this->terminusJsonResponse( + sprintf('env:list %s', $this->getSiteName()) + ); $this->assertIsArray($envs); $env = array_shift($envs); @@ -298,8 +382,14 @@ public function testListCommand() */ public function testViewCommand() { - $url = $this->terminus(sprintf('env:view %s --print', $this->getSiteEnv())); - $expectedUrl = sprintf('https://%s-%s.pantheonsite.io/', $this->getMdEnv(), $this->getSiteName()); + $url = $this->terminus( + sprintf('env:view %s --print', $this->getSiteEnv()) + ); + $expectedUrl = sprintf( + 'https://%s-%s.pantheonsite.io/', + $this->getMdEnv(), + $this->getSiteName() + ); $this->assertEquals($expectedUrl, $url); } }