1b8b0f523f5d75ebb8914bbc7a053e624737faf1
[yaffs-website] / vendor / drush / drush / tests / RsyncTest.php
1 <?php
2
3 namespace Unish;
4
5 /**
6  * @file
7  *   Tests for rsync command
8  *
9  * @group commands
10  */
11 class RsyncCase extends CommandUnishTestCase
12 {
13
14     public function setUp()
15     {
16         if (!$this->getSites()) {
17             $this->setUpDrupal(2, true);
18         }
19     }
20
21   /**
22    * Test drush rsync --simulate.
23    */
24     public function testRsyncSimulated()
25     {
26         if ($this->isWindows()) {
27             $this->markTestSkipped('rsync command not currently available on Windows.');
28         }
29
30         $options = [
31             'simulate' => null,
32             'alias-path' => __DIR__ . '/resources/alias-fixtures',
33         ];
34
35         // Test simulated simple rsync with two local sites
36         $this->drush('rsync', ['@example.stage', '@example.dev'], $options, null, null, self::EXIT_SUCCESS, '2>&1');
37         $expected = "Calling system(rsync -e 'ssh ' -akz /path/to/stage /path/to/dev);";
38         $this->assertOutputEquals($expected);
39
40         // Test simulated rsync with relative paths
41         $this->drush('rsync', ['@example.dev:files', '@example.stage:files'], $options, null, null, self::EXIT_SUCCESS, '2>&1');
42         $expected = "Calling system(rsync -e 'ssh ' -akz /path/to/dev/files /path/to/stage/files);";
43         $this->assertOutputEquals($expected);
44
45         // Test simulated rsync on local machine with a remote target
46         $this->drush('rsync', ['@example.dev:files', '@example.live:files'], $options, null, null, self::EXIT_SUCCESS, '2>&1');
47         $expected = "Calling system(rsync -e 'ssh -o PasswordAuthentication=example' -akz /path/to/dev/files www-admin@service-provider.com:/path/on/service-provider/files);";
48         $this->assertOutputEquals($expected);
49
50         // Test simulated backend invoke.
51         // Note that command-specific options are not processed for remote
52         // targets. The aliases are not interpreted at all until they recache
53         // the remote side, at which point they will be evaluated & any needed
54         // injection will be done.
55         $this->drush('rsync', ['@example.dev', '@example.stage'], $options, 'user@server/path/to/drupal#sitename', null, self::EXIT_SUCCESS, '2>&1');
56         $expected = "Simulating backend invoke: ssh -o PasswordAuthentication=no user@server 'drush --root=/path/to/drupal --uri=sitename --no-interaction rsync '\''@example.dev'\'' '\''@example.stage'\'' 2>&1' 2>&1";
57         $this->assertOutputEquals($expected, '# --alias-path=[^ ]*#');
58     }
59
60     public function testRsyncPathAliases()
61     {
62         $aliases = $this->getAliases();
63         $source_alias = array_shift($aliases);
64         $target_alias = current($aliases);
65
66         $options = [
67             'yes' => null,
68             'alias-path' => __DIR__ . '/resources/alias-fixtures',
69         ];
70
71         $source = $this->webroot() . '/sites/dev/files/a';
72         $target = $this->webroot() . '/sites/stage/files/b';
73
74         @mkdir($source);
75         @mkdir($target);
76
77         $source_file = "$source/example.txt";
78         $target_file = "$target/example.txt";
79
80         // Delete target file just to be sure that we are running a clean test.
81         if (file_exists($target_file)) {
82             unlink($target_file);
83         }
84
85         // Create something on the dev site at $source for us to copy
86         $test_data = "This is my test data";
87         file_put_contents($source_file, $test_data);
88
89         // We just deleted it -- should be missing
90         $this->assertFileNotExists($target_file);
91         $this->assertFileExists($source_file);
92
93         // Test an actual rsync between our two fixture sites. Note that
94         // these sites share the same web root.
95         $this->drush('rsync', ["$source_alias:%files/a/", "$target_alias:%files/b"], $options, null, null, self::EXIT_SUCCESS, '2>&1');
96         $this->assertContains('You will delete files in', $this->getOutput());
97
98         // Test to see if our fixture file now exists at $target
99         $this->assertFileExists($target_file);
100         $this->assertStringEqualsFile($target_file, $test_data);
101     }
102
103   /**
104    * Test to see if rsync @site:%files calculates the %files path correctly.
105    * This tests the non-optimized code path. The optimized code path (direct
106    * call to Drush API functions rather than an `exec`) has not been implemented.
107    */
108     public function testRsyncAndPercentFiles()
109     {
110         $site = current($this->getAliases());
111         $uri = $this->getUri();
112         $options['simulate'] = null;
113         $this->drush('core-rsync', ["$site:%files", "/tmp"], $options, null, null, self::EXIT_SUCCESS, '2>&1;');
114         $output = $this->getOutput();
115         $level = $this->logLevel();
116         $pattern = in_array($level, ['verbose', 'debug']) ? "Calling system(rsync -e 'ssh ' -akzv --stats --progress %s /tmp);" : "Calling system(rsync -e 'ssh ' -akz %s /tmp);";
117         $expected = sprintf($pattern, $this->webroot(). "/sites/$uri/files/");
118         $this->assertEquals($expected, $output);
119     }
120 }