57649891b7ef3582678cb3188c259fd030081689
[yaffs-website] / vendor / drush / drush / tests / sqlSyncTest.php
1 <?php
2
3 /**
4 * @file
5 *  For now we only test sql-sync in simulated mode.
6 *
7 *  Future: Using two copies of Drupal, we could test
8 *  overwriting one site with another.
9 */
10
11 namespace Unish;
12
13 /**
14  *  @group slow
15  *  @group commands
16  *  @group sql
17  */
18 class sqlSyncTest extends CommandUnishTestCase {
19
20   /**
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.
24    *
25    * General handling of site aliases will be in sitealiasTest.php.
26    */
27   public function testLocalSqlSync() {
28     if ($this->db_driver() == 'sqlite') {
29       $this->markTestSkipped('SQL Sync does not apply to SQLite.');
30       return;
31     }
32
33     $sites = $this->setUpDrupal(2, TRUE);
34     return $this->localSqlSync();
35   }
36   /**
37    * Do the same test as above, but use Drupal 6 sites instead of Drupal 7.
38    */
39   public function testLocalSqlSyncD6() {
40     if (UNISH_DRUPAL_MAJOR_VERSION != 6) {
41       $this->markTestSkipped('This test class is designed for Drupal 6.');
42       return;
43     }
44
45     chdir(UNISH_TMP); // Avoids perm denied Windows error.
46     $this->setUpBeforeClass();
47     $sites = $this->setUpDrupal(2, TRUE, '6');
48     return $this->localSqlSync();
49   }
50
51   public function localSqlSync() {
52
53     $options = array(
54       'root' => $this->webroot(),
55       'uri' => 'stage',
56       'yes' => NULL,
57     );
58
59     // Create a user in the staging site
60     $name = 'joe.user';
61     $mail = "joe.user@myhome.com";
62
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,
68         $name,
69         $mail
70       ), $options + array(
71           'script-path' => __DIR__ . '/resources',
72           'debug' => NULL
73         ));
74     }
75     else {
76       $this->drush('user-create', array($name), $options + array('password' => 'password', 'mail' => $mail));
77     }
78
79     // Copy stage to dev with --sanitize.
80     $sync_options = array(
81       'sanitize' => NULL,
82       'yes' => NULL,
83       // Test wildcards expansion from within sql-sync. Also avoid D8 persistent entity cache.
84       'structure-tables-list' => 'cache,cache*',
85     );
86     $this->drush('sql-sync', array('@stage', '@dev'), $sync_options);
87
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);
92     $uid = $row[1];
93     $this->assertEquals($mail, $row[2], 'email address is unchanged on source site.');
94     $this->assertEquals($name, $row[0]);
95
96     $options = array(
97       'root' => $this->webroot(),
98       'uri' => 'dev',
99       'yes' => NULL,
100     );
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);
105     $uid = $row[1];
106     $this->assertEquals("user+$uid@localhost.localdomain", $row[2], 'email address was sanitized on destination site.');
107     $this->assertEquals($name, $row[0]);
108
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);
111
112     // Copy stage to dev with --sanitize and a fixed sanitized email
113     $sync_options = array(
114       'sanitize' => NULL,
115       'yes' => NULL,
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*',
119     );
120     $this->drush('sql-sync', array('@stage', '@dev'), $sync_options);
121
122     $options = array(
123       'root' => $this->webroot(),
124       'uri' => 'dev',
125       'yes' => NULL,
126     );
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);
131     $uid = $row[1];
132     $this->assertEquals("user@mysite.org", $row[2], 'email address was sanitized (fixed email) on destination site.');
133     $this->assertEquals($name, $row[0]);
134
135     if (UNISH_DRUPAL_MAJOR_VERSION >= 8) {
136       $fields = [
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',
143       ];
144       // Assert that field DO NOT contain values.
145       foreach ($fields as $field_name => $value) {
146         $this->assertUserFieldContents($field_name, $value, $options);
147       }
148
149       // Assert that field_user_telephone DOES contain "5555555555".
150       $this->assertUserFieldContents('field_user_telephone', '5555555555', $options, TRUE);
151     }
152   }
153
154   /**
155    * Assert that a field on the user entity does or does not contain a value.
156    *
157    * @param string $field_name
158    *   The machine name of the field.
159    * @param string $value
160    *   The field 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.
165    */
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);
172
173     if ($should_contain) {
174       $this->assertContains($value, $output);
175     }
176     else {
177       $this->assertNotContains($value, $output);
178     }
179   }
180 }