Version 1
[yaffs-website] / vendor / drush / drush / tests / archiveDumpTest.php
1 <?php
2
3 namespace Unish;
4
5 require_once dirname(__FILE__) . '/../includes/filesystem.inc';
6
7 /**
8  * Tests for archive-dump and archive-restore
9  *
10  * @group commands
11  */
12 class archiveDumpCase extends CommandUnishTestCase {
13   /**
14    * archive-dump behaves slightly different when archiving a site installed
15    * at sites/default so we make the test to use sites/default as the
16    * installation directory instead of default sites/dev.
17    */
18   const uri = 'default';
19
20   /**
21    * Install a site and dump it to an archive.
22    */
23   private function archiveDump($no_core) {
24     $profile = UNISH_DRUPAL_MAJOR_VERSION >= 7 ? 'testing' : 'default';
25     $this->fetchInstallDrupal(self::uri, TRUE, UNISH_DRUPAL_MAJOR_VERSION, $profile);
26     $root = $this->webroot();
27     $dump_dest = UNISH_SANDBOX . DIRECTORY_SEPARATOR . 'dump.tar.gz';
28     $options = array(
29       'root' => $root,
30       'uri' => self::uri,
31       'yes' => NULL,
32       'destination' => $dump_dest,
33       'overwrite' => NULL,
34     );
35     if ($no_core) {
36       $options['no-core'] = NULL;
37     }
38     $this->drush('archive-dump', array(self::uri), $options);
39
40     return $dump_dest;
41   }
42
43   /**
44    * Untar an archive and return the path to the untarred folder.
45    */
46   private function unTar($dump_dest) {
47     $untar_dest = UNISH_SANDBOX . DIRECTORY_SEPARATOR . 'untar';
48     unish_file_delete_recursive($untar_dest, TRUE);
49     $tar = self::get_tar_executable();
50     $exec = sprintf("mkdir %s && cd %s && $tar -xzf %s", $untar_dest, $untar_dest, $dump_dest);
51     $this->execute($exec);
52
53     return $untar_dest;
54   }
55
56   /**
57    * Test if tarball generated by archive-dump looks right.
58    */
59   public function testArchiveDump() {
60     $dump_dest = $this->archiveDump(FALSE);
61     $docroot = basename($this->webroot());
62
63     // Check the dump file is a gzip file.
64     $exec = sprintf('file %s', $dump_dest);
65     $this->execute($exec);
66     $output = $this->getOutput();
67     $expected = '%sgzip compressed data%s';
68     $this->assertStringMatchesFormat($expected, $output);
69
70     // Untar the archive and make sure it looks right.
71     $untar_dest = $this->unTar($dump_dest);
72
73     if (strpos(UNISH_DB_URL, 'mysql') !== FALSE) {
74       $this->execute(sprintf('head %s/unish_%s.sql | grep "MySQL dump"', $untar_dest, self::uri));
75     }
76     $this->assertFileExists($untar_dest . '/MANIFEST.ini');
77     $this->assertFileExists($untar_dest . '/' . $docroot);
78
79     return $dump_dest;
80   }
81
82   /**
83    * Test archive-restore.
84    *
85    * Restore the archive generated in testArchiveDump() and verify that the
86    * directory contents are identical.
87    *
88    * @depends testArchiveDump
89    */
90    public function testArchiveRestore($dump_dest) {
91     $restore_dest = UNISH_SANDBOX . DIRECTORY_SEPARATOR . 'restore';
92     $options = array(
93       'yes' => NULL,
94       'destination' => $restore_dest,
95     );
96     $this->drush('archive-restore', array($dump_dest), $options);
97     $original_codebase = drush_dir_md5($this->webroot());
98     $restored_codebase = drush_dir_md5($restore_dest);
99     $this->assertEquals($original_codebase, $restored_codebase);
100   }
101
102   /**
103    * Test if tarball generated by archive-dump with --no-core looks right.
104    */
105   public function testArchiveDumpNoCore() {
106     $dump_dest = $this->archiveDump(TRUE);
107     $untar_dest = $this->unTar($dump_dest);
108     $docroot = basename($this->webroot());
109     $this->assertFileExists($untar_dest . '/MANIFEST.ini');
110     $this->assertFileExists($untar_dest . '/' . $docroot);
111     $modules_dir = UNISH_DRUPAL_MAJOR_VERSION >= 8 ? '/core/modules' : '/modules';
112     $this->assertFileNotExists($untar_dest . '/' . $docroot . $modules_dir, 'No modules directory should exist with --no-core');
113
114     return $dump_dest;
115   }
116
117   /**
118    * Test archive-restore for a site archive (--no-core).
119    *
120    * @depends testArchiveDumpNoCore
121    */
122   public function testArchiveRestoreNoCore($dump_dest) {
123     $root = $this->webroot();
124     $original_codebase = drush_dir_md5($root);
125     unish_file_delete_recursive($root . '/sites/' . self::uri, TRUE);
126     $options = array(
127       'yes' => NULL,
128       'destination' => $root,
129     );
130     $this->drush('archive-restore', array($dump_dest), $options);
131
132     $restored_codebase = drush_dir_md5($root);
133     $this->assertEquals($original_codebase, $restored_codebase);
134    }
135 }