More updates to stop using dev or alpha or beta versions.
[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     public function testSimulatedSqlSync()
22     {
23         $fixtureSites = [
24             'remote' => [
25                 'host' => 'server.isp.simulated',
26                 'user' => 'www-admin',
27                 'ssh' => [
28                     'options' => '-o PasswordAuthentication=whatever'
29                 ],
30                 'paths' => [
31                     'drush-script' => '/path/to/drush',
32                 ],
33             ],
34             'local' => [
35             ],
36         ];
37         $this->setUpSettings($fixtureSites, 'synctest');
38         $options = [
39             'simulate' => null,
40             'alias-path' => __DIR__ . '/resources/alias-fixtures',
41         ];
42
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);
49
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);
56
57
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);
65     }
66
67     /**
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.
71      *
72      * General handling of site aliases will be in sitealiasTest.php.
73      */
74     public function testLocalSqlSync()
75     {
76         if ($this->dbDriver() == 'sqlite') {
77             $this->markTestSkipped('SQL Sync does not apply to SQLite.');
78             return;
79         }
80
81         $this->setUpDrupal(2, true);
82         return $this->localSqlSync();
83     }
84
85     public function localSqlSync()
86     {
87
88         $options = [
89             'uri' => 'stage',
90             'yes' => null,
91         ];
92
93         // Create a user in the staging site
94         $name = 'joe.user';
95         $mail = "joe.user@myhome.com";
96
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',]);
100
101         // Copy stage to dev, and then sql:sanitize.
102         $sync_options = [
103             'yes' => null,
104             // Test wildcards expansion from within sql-sync. Also avoid D8 persistent entity cache.
105             'structure-tables-list' => 'cache,cache*',
106         ];
107         $this->drush('sql-sync', ['@unish.stage', '@unish.dev'], $sync_options);
108         $this->drush('sql-sanitize', [], ['yes' => null], '@unish.dev');
109
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();
118
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);
125
126         // Copy stage to dev with --sanitize and a fixed sanitized email
127         $sync_options = [
128             'yes' => null,
129             // Test wildcards expansion from within sql-sync. Also avoid D8 persistent entity cache.
130             'structure-tables-list' => 'cache,cache*',
131         ];
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');
134
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);
140
141
142         $fields = [
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',
149         ];
150         // Assert that field DO NOT contain values.
151         foreach ($fields as $field_name => $value) {
152             $this->assertUserFieldContents($field_name, $value);
153         }
154
155         // Assert that field_user_telephone DOES contain "5555555555".
156         $this->assertUserFieldContents('field_user_telephone', '5555555555', true);
157     }
158
159     /**
160      * Assert that a field on the user entity does or does not contain a value.
161      *
162      * @param string $field_name
163      *   The machine name of the field.
164      * @param string $value
165      *   The field value.
166      * @param bool $should_contain
167      *   Whether the field should contain the value. Defaults to false.
168      */
169     public function assertUserFieldContents($field_name, $value, $should_contain = false)
170     {
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);
176
177         if ($should_contain) {
178             $this->assertContains($value, $output);
179         } else {
180             $this->assertNotContains($value, $output);
181         }
182     }
183 }