5 * For now we only test sql-sync in simulated mode.
7 * Future: Using two copies of Drupal, we could test
8 * overwriting one site with another.
18 class SqlSyncTest extends CommandUnishTestCase
21 public function testSimulatedSqlSync()
25 'host' => 'server.isp.simulated',
26 'user' => 'www-admin',
28 'options' => '-o PasswordAuthentication=whatever'
31 'drush-script' => '/path/to/drush',
37 $this->setUpSettings($fixtureSites, 'synctest');
40 'alias-path' => __DIR__ . '/resources/alias-fixtures',
43 // Test simulated simple rsync remote-to-local
44 $this->drush('sql:sync', ['@synctest.remote', '@synctest.local'], $options, '@synctest.local', null, self::EXIT_SUCCESS, '2>&1');
45 $output = $this->getSimplifiedOutput();
46 $this->assertContains("Simulating backend invoke: ssh -o PasswordAuthentication=whatever www-admin@server.isp.simulated '/path/to/drush --backend=2 --strict=0 --alias-path=__DIR__/resources/alias-fixtures:__SANDBOX__/etc/drush/sites --root=__SUT__/web --uri=remote sql-dump --no-interaction --gzip --result-file", $output);
47 $this->assertContains("Simulating backend invoke: __SUT__/vendor/drush/drush/drush --backend=2 --alias-path=__DIR__/resources/alias-fixtures:__SANDBOX__/etc/drush/sites --root=__SUT__/web --uri=local core-rsync '@synctest.remote:/simulated/path/to/dump.tgz' '@synctest.local:__SANDBOX__/tmp/dump.tgz' -- --remove-source-files", $output);
48 $this->assertContains("Simulating backend invoke: __SUT__/vendor/drush/drush/drush --backend=2 --strict=0 --alias-path=__DIR__/resources/alias-fixtures:__SANDBOX__/etc/drush/sites --root=__SUT__/web --uri=local sql-query --no-interaction --file=__SANDBOX__/tmp/dump.tgz --file-delete", $output);
50 // Test simulated simple rsync local-to-remote
51 $this->drush('sql:sync', ['@synctest.local', '@synctest.remote'], $options, '@synctest.local', null, self::EXIT_SUCCESS, '2>&1');
52 $output = $this->getSimplifiedOutput();
53 $this->assertContains("Simulating backend invoke: __SUT__/vendor/drush/drush/drush --backend=2 --strict=0 --alias-path=__DIR__/resources/alias-fixtures:__SANDBOX__/etc/drush/sites --root=__SUT__/web --uri=local sql-dump --no-interaction --gzip --result-file", $output);
54 $this->assertContains("Simulating backend invoke: __SUT__/vendor/drush/drush/drush --backend=2 --alias-path=__DIR__/resources/alias-fixtures:__SANDBOX__/etc/drush/sites --root=__SUT__/web --uri=local core-rsync '@synctest.local:/simulated/path/to/dump.tgz' '@synctest.remote:/tmp/dump.tgz' -- --remove-source-files", $output);
55 $this->assertContains("Simulating backend invoke: ssh -o PasswordAuthentication=whatever www-admin@server.isp.simulated '/path/to/drush --backend=2 --strict=0 --alias-path=__DIR__/resources/alias-fixtures:__SANDBOX__/etc/drush/sites --root=__SUT__/web --uri=remote sql-query --no-interaction --file=/tmp/dump.tgz --file-delete", $output);
58 // Test simulated backend invoke with a remote runner.
59 // Note that the target aliases are not interpreted at all until they recach
60 // the remote side. Injection for alias parameters is currently only done when
61 // they are used with drush_invoke_process.
62 $this->drush('sql:sync', ['@synctest.remote', '@synctest.local'], $options, 'user@server/path/to/drupal#sitename', null, self::EXIT_SUCCESS, '2>&1');
63 $output = $this->getSimplifiedOutput();
64 $this->assertContains("Simulating backend invoke: ssh -o PasswordAuthentication=no user@server 'drush --alias-path=__DIR__/resources/alias-fixtures:__SANDBOX__/etc/drush/sites --root=/path/to/drupal --uri=sitename --no-interaction sql:sync '\''@synctest.remote'\'' '\''@synctest.local'\''", $output);
68 * Covers the following responsibilities.
69 * - A user created on the source site is copied to the destination site.
70 * - The email address of the copied user is sanitized on the destination site.
72 * General handling of site aliases will be in sitealiasTest.php.
74 public function testLocalSqlSync()
76 if ($this->dbDriver() == 'sqlite') {
77 $this->markTestSkipped('SQL Sync does not apply to SQLite.');
81 $this->setUpDrupal(2, true);
82 return $this->localSqlSync();
85 public function localSqlSync()
93 // Create a user in the staging site
95 $mail = "joe.user@myhome.com";
97 // Add user fields and a test User.
98 $this->drush('pm-enable', ['field,text,telephone,comment'], $options + ['yes' => null]);
99 $this->drush('php-script', ['user_fields-D' . UNISH_DRUPAL_MAJOR_VERSION, $name, $mail], $options + ['script-path' => __DIR__ . '/resources',]);
101 // Copy stage to dev, and then sql:sanitize.
104 // Test wildcards expansion from within sql-sync. Also avoid D8 persistent entity cache.
105 'structure-tables-list' => 'cache,cache*',
107 $this->drush('sql-sync', ['@unish.stage', '@unish.dev'], $sync_options);
108 $this->drush('sql-sanitize', [], ['yes' => null], '@unish.dev');
110 // Confirm that the sample user is unchanged on the staging site
111 $this->drush('user-information', [$name], $options + ['format' => 'json'], '@unish.stage');
112 $info = $this->getOutputFromJSON(2);
113 $this->assertEquals($mail, $info->mail, 'Email address is unchanged on source site.');
114 $this->assertEquals($name, $info->name);
115 // Get the unchanged pass.
116 $this->drush('user-information', [$name], $options + ['field' => 'pass']);
117 $original_hashed_pass = $this->getOutput();
119 // Confirm that the sample user's email and password have been sanitized on the dev site
120 $this->drush('user-information', [$name], $options + ['fields' => 'uid,name,mail,pass', 'format' => 'json', 'yes' => null], '@unish.dev');
121 $info = $this->getOutputFromJSON(2);
122 $this->assertEquals("user+2@localhost.localdomain", $info->mail, 'Email address was sanitized on destination site.');
123 $this->assertEquals($name, $info->name);
124 $this->assertNotEquals($info->pass, $original_hashed_pass);
126 // Copy stage to dev with --sanitize and a fixed sanitized email
129 // Test wildcards expansion from within sql-sync. Also avoid D8 persistent entity cache.
130 'structure-tables-list' => 'cache,cache*',
132 $this->drush('sql-sync', ['@unish.stage', '@unish.dev'], $sync_options);
133 $this->drush('sql-sanitize', [], ['yes' => null, 'sanitize-email' => 'user@mysite.org'], '@unish.dev');
135 // Confirm that the sample user's email address has been sanitized on the dev site
136 $this->drush('user-information', [$name], $options + ['yes' => null, 'format' => 'json'], '@unish.dev');
137 $info = $this->getOutputFromJSON(2);
138 $this->assertEquals('user@mysite.org', $info->mail, 'Email address was sanitized (fixed email) on destination site.');
139 $this->assertEquals($name, $info->name);
143 'field_user_email' => 'joe.user.alt@myhome.com',
144 'field_user_string' => 'Private info',
145 'field_user_string_long' => 'Really private info',
146 'field_user_text' => 'Super private info',
147 'field_user_text_long' => 'Super duper private info',
148 'field_user_text_with_summary' => 'Private',
150 // Assert that field DO NOT contain values.
151 foreach ($fields as $field_name => $value) {
152 $this->assertUserFieldContents($field_name, $value);
155 // Assert that field_user_telephone DOES contain "5555555555".
156 $this->assertUserFieldContents('field_user_telephone', '5555555555', true);
160 * Assert that a field on the user entity does or does not contain a value.
162 * @param string $field_name
163 * The machine name of the field.
164 * @param string $value
166 * @param bool $should_contain
167 * Whether the field should contain the value. Defaults to false.
169 public function assertUserFieldContents($field_name, $value, $should_contain = false)
171 $table = 'user__' . $field_name;
172 $column = $field_name . '_value';
173 $this->drush('sql-query', ["SELECT $column FROM $table LIMIT 1"], [], '@unish.dev');
174 $output = $this->getOutput();
175 $this->assertNotEmpty($output);
177 if ($should_contain) {
178 $this->assertContains($value, $output);
180 $this->assertNotContains($value, $output);