Version 1
[yaffs-website] / vendor / drush / drush / tests / sqlDumpTest.php
1 <?php
2
3 namespace Unish;
4
5 /**
6  * Tests for sql-dump commands.
7  *
8  * @group commands
9  * @group sql
10  * @group slow
11  */
12 class SqlDumpTest extends CommandUnishTestCase {
13
14   /**
15    * Test that a dump file is created successfully.
16    */
17   function testSqlDump() {
18     if ($this->db_driver() == 'sqlite') {
19       $this->markTestSkipped('SQL Dump does not apply to SQLite.');
20       return;
21     }
22
23     $this->setUpDrupal(1, TRUE);
24     $root = $this->webroot();
25     $uri = 'dev';
26     $full_dump_file_path = UNISH_SANDBOX . DIRECTORY_SEPARATOR . 'full_db.sql';
27
28     $options = array(
29       'result-file' => $full_dump_file_path,
30       // Last 5 entries are for D8+
31       'skip-tables-list' => 'hist*,cache*,router,config*,watchdog,key_valu*',
32       'yes' => NULL,
33     );
34     $site_selection_options = array(
35       'root' => $root,
36       'uri' => $uri,
37     );
38
39     // Test --extra option
40     if ($this->db_driver() == 'mysql') {
41       $this->drush('sql-dump', array(), array_merge($options, $site_selection_options, array('extra' => '--skip-add-drop-table')));
42       $this->assertFileExists($full_dump_file_path);
43       $full_dump_file = file_get_contents($full_dump_file_path);
44       $this->assertNotContains('DROP TABLE IF EXISTS', $full_dump_file);
45     }
46
47
48     // First, do a test without any aliases, and dump the whole database
49     $this->drush('sql-dump', array(), array_merge($options, $site_selection_options));
50     $this->assertFileExists($full_dump_file_path);
51     $full_dump_file = file_get_contents($full_dump_file_path);
52     // Test that we have sane contents.
53     $this->assertContains('queue', $full_dump_file);
54     // Test skip-files-list and wildcard expansion.
55     $this->assertNotContains('history', $full_dump_file);
56     // Next, set up an alias file and run a couple of simulated
57     // tests to see if options are propagated correctly.
58     // Control: insure options are not set when not specified
59     unset($options['skip-tables-list']);
60     unlink($full_dump_file_path);
61     $this->drush('sql-dump', array(), array_merge($options, $site_selection_options));
62     $this->assertFileExists($full_dump_file_path);
63     $full_dump_file = file_get_contents($full_dump_file_path);
64     // Test that we have sane contents.
65     $this->assertContains('queue', $full_dump_file);
66     // Test skip-files-list and wildcard expansion.
67     $this->assertContains('history', $full_dump_file);
68
69     $aliasPath = UNISH_SANDBOX . '/aliases';
70     mkdir($aliasPath);
71     $aliasFile = $aliasPath . '/bar.aliases.drushrc.php';
72     $aliasContents = <<<EOD
73   <?php
74   // Written by Unish. This file is safe to delete.
75   \$aliases['test'] = array(
76     'root' => '$root',
77     'uri' => '$uri',
78     'site' => 'stage',
79     'command-specific' => array(
80       'sql-dump' => array(
81         'skip-tables-list' => 'hist*,cache*,router,config*,watchdog,key_valu*',
82       ),
83     ),
84   );
85 EOD;
86     file_put_contents($aliasFile, $aliasContents);
87     $options['alias-path'] = $aliasPath;
88     unlink($full_dump_file_path);
89     // Now run again with an alias, and test to see if the option is there
90     $this->drush('sql-dump', array(), array_merge($options), '@test');
91     $this->assertFileExists($full_dump_file_path);
92     $full_dump_file = file_get_contents($full_dump_file_path);
93     // Test that we have sane contents.
94     $this->assertContains('queue', $full_dump_file);
95     // Test skip-files-list and wildcard expansion.
96     $this->assertNotContains('history', $full_dump_file);
97     // Repeat control test:  options not recovered in absence of an alias.
98     unlink($full_dump_file_path);
99     $this->drush('sql-dump', array(), array_merge($options, $site_selection_options));
100     $this->assertFileExists($full_dump_file_path);
101     $full_dump_file = file_get_contents($full_dump_file_path);
102     // Test that we have sane contents.
103     $this->assertContains('queue', $full_dump_file);
104     // Test skip-files-list and wildcard expansion.
105     $this->assertContains('history', $full_dump_file);
106     // Now run yet with @self, and test to see that Drush can recover the option
107     // --skip-tables-list, defined in @test.
108     unlink($full_dump_file_path);
109     $this->drush('sql-dump', array(), array_merge($options, $site_selection_options), '@self');
110     $this->assertFileExists($full_dump_file_path);
111     $full_dump_file = file_get_contents($full_dump_file_path);
112     // Test that we have sane contents.
113     $this->assertContains('queue', $full_dump_file);
114     // Test skip-files-list and wildcard expansion.
115     $this->assertNotContains('history', $full_dump_file);
116   }
117 }