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