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 * Covers the following responsibilities.
22 * - A user created on the source site is copied to the destination site.
23 * - The email address of the copied user is sanitized on the destination site.
25 * General handling of site aliases will be in sitealiasTest.php.
27 public function testLocalSqlSync() {
28 if ($this->db_driver() == 'sqlite') {
29 $this->markTestSkipped('SQL Sync does not apply to SQLite.');
33 $sites = $this->setUpDrupal(2, TRUE);
34 return $this->localSqlSync();
37 * Do the same test as above, but use Drupal 6 sites instead of Drupal 7.
39 public function testLocalSqlSyncD6() {
40 if (UNISH_DRUPAL_MAJOR_VERSION != 6) {
41 $this->markTestSkipped('This test class is designed for Drupal 6.');
45 chdir(UNISH_TMP); // Avoids perm denied Windows error.
46 $this->setUpBeforeClass();
47 $sites = $this->setUpDrupal(2, TRUE, '6');
48 return $this->localSqlSync();
51 public function localSqlSync() {
54 'root' => $this->webroot(),
59 // Create a user in the staging site
61 $mail = "joe.user@myhome.com";
63 if (UNISH_DRUPAL_MAJOR_VERSION >= 8) {
64 // Add user fields and a test User.
65 $this->drush('pm-enable', array('field,text,telephone,comment'), $options + array('yes' => NULL));
66 $this->drush('php-script', array(
67 'user_fields-D' . UNISH_DRUPAL_MAJOR_VERSION,
71 'script-path' => __DIR__ . '/resources',
76 $this->drush('user-create', array($name), $options + array('password' => 'password', 'mail' => $mail));
79 // Copy stage to dev with --sanitize.
80 $sync_options = array(
83 // Test wildcards expansion from within sql-sync. Also avoid D8 persistent entity cache.
84 'structure-tables-list' => 'cache,cache*',
86 $this->drush('sql-sync', array('@stage', '@dev'), $sync_options);
88 // Confirm that the sample user has the correct email address on the staging site
89 $this->drush('user-information', array($name), $options + array('pipe' => NULL));
90 $output = $this->getOutput();
91 $row = str_getcsv($output);
93 $this->assertEquals($mail, $row[2], 'email address is unchanged on source site.');
94 $this->assertEquals($name, $row[0]);
97 'root' => $this->webroot(),
101 // Confirm that the sample user's email address has been sanitized on the dev site
102 $this->drush('user-information', array($name), $options + array('pipe' => NULL));
103 $output = $this->getOutput();
104 $row = str_getcsv($output);
106 $this->assertEquals("user+$uid@localhost.localdomain", $row[2], 'email address was sanitized on destination site.');
107 $this->assertEquals($name, $row[0]);
109 // @todo Confirm that the role_permissions table no longer exists in dev site (i.e. wildcard expansion works in sql-sync).
110 // $this->drush('sql-query', array('SELECT * FROM role_permission'), $options, NULL, NULL, self::EXIT_ERROR);
112 // Copy stage to dev with --sanitize and a fixed sanitized email
113 $sync_options = array(
116 'sanitize-email' => 'user@mysite.org',
117 // Test wildcards expansion from within sql-sync. Also avoid D8 persistent entity cache.
118 'structure-tables-list' => 'cache,cache*',
120 $this->drush('sql-sync', array('@stage', '@dev'), $sync_options);
123 'root' => $this->webroot(),
127 // Confirm that the sample user's email address has been sanitized on the dev site
128 $this->drush('user-information', array($name), $options + array('pipe' => NULL));
129 $output = $this->getOutput();
130 $row = str_getcsv($output);
132 $this->assertEquals("user@mysite.org", $row[2], 'email address was sanitized (fixed email) on destination site.');
133 $this->assertEquals($name, $row[0]);
135 if (UNISH_DRUPAL_MAJOR_VERSION >= 8) {
137 'field_user_email' => 'joe.user.alt@myhome.com',
138 'field_user_string' => 'Private info',
139 'field_user_string_long' => 'Really private info',
140 'field_user_text' => 'Super private info',
141 'field_user_text_long' => 'Super duper private info',
142 'field_user_text_with_summary' => 'Private',
144 // Assert that field DO NOT contain values.
145 foreach ($fields as $field_name => $value) {
146 $this->assertUserFieldContents($field_name, $value, $options);
149 // Assert that field_user_telephone DOES contain "5555555555".
150 $this->assertUserFieldContents('field_user_telephone', '5555555555', $options, TRUE);
155 * Assert that a field on the user entity does or does not contain a value.
157 * @param string $field_name
158 * The machine name of the field.
159 * @param string $value
161 * @param array $options
162 * Options to be added to the sql-query command.
163 * @param bool $should_contain
164 * Whether the field should contain the value. Defaults to false.
166 public function assertUserFieldContents($field_name, $value, $options = [], $should_contain = FALSE) {
167 $table = 'user__' . $field_name;
168 $column = $field_name . '_value';
169 $this->drush('sql-query', [ "SELECT $column FROM $table LIMIT 1" ], $options);
170 $output = $this->getOutput();
171 $this->assertNotEmpty($output);
173 if ($should_contain) {
174 $this->assertContains($value, $output);
177 $this->assertNotContains($value, $output);