7 * Tests for rsync command
11 class RsyncCase extends CommandUnishTestCase
14 public function setUp()
16 if (!$this->getSites()) {
17 $this->setUpDrupal(2, true);
22 * Test drush rsync --simulate.
24 public function testRsyncSimulated()
26 if ($this->isWindows()) {
27 $this->markTestSkipped('rsync command not currently available on Windows.');
32 'alias-path' => __DIR__ . '/resources/alias-fixtures',
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);
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);
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);
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=[^ ]*#');
60 public function testRsyncPathAliases()
62 $aliases = $this->getAliases();
63 $source_alias = array_shift($aliases);
64 $target_alias = current($aliases);
68 'alias-path' => __DIR__ . '/resources/alias-fixtures',
71 $source = $this->webroot() . '/sites/dev/files/a';
72 $target = $this->webroot() . '/sites/stage/files/b';
77 $source_file = "$source/example.txt";
78 $target_file = "$target/example.txt";
80 // Delete target file just to be sure that we are running a clean test.
81 if (file_exists($target_file)) {
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);
89 // We just deleted it -- should be missing
90 $this->assertFileNotExists($target_file);
91 $this->assertFileExists($source_file);
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());
98 // Test to see if our fixture file now exists at $target
99 $this->assertFileExists($target_file);
100 $this->assertStringEqualsFile($target_file, $test_data);
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.
108 public function testRsyncAndPercentFiles()
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);