-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdrush_testcase.inc
212 lines (193 loc) · 6.03 KB
/
drush_testcase.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
// We read from globals here because env can be empty and ini did not work in quick test.
define('UNISH_DB_URL', $GLOBALS['UNISH_DB_URL']);
// UNISH_DRUSH value can come from phpunit.xml or `which drush`.
if (!defined('UNISH_DRUSH')) {
define('UNISH_DRUSH', empty($GLOBALS['UNISH_DRUSH']) ? trim(`which drush`) : $GLOBALS['UNISH_DRUSH']);
}
abstract class Drush_TestCase extends PHPUnit_Framework_TestCase {
// Unix exit codes.
const EXIT_SUCCESS = 0;
const EXIT_ERROR = 1;
/*
* An array of Drupal sites that are setup in the drush-sandbox.
*/
var $sites;
function __construct() {
$this->_output = false;
}
/**
* Assure that each class starts with an empty sandbox directory.
*/
public static function setUpBeforeClass() {
$sandbox = $GLOBALS['UNISH_SANDBOX'];
if (file_exists($sandbox)) {
self::file_delete_recursive($sandbox);
}
$ret = mkdir($sandbox);
// Path must exist before we call realpath().
if (!defined('UNISH_SANDBOX')) {
define('UNISH_SANDBOX', realpath($sandbox));
}
chdir(UNISH_SANDBOX);
}
/**
* Runs after each test case. Remove sandbox directory.
*/
public static function tearDownAfterClass() {
if (file_exists(UNISH_SANDBOX)) {
self::file_delete_recursive(UNISH_SANDBOX);
}
}
/**
* Actually runs the command. Does not trap the error stream output as this
* need PHP 4.3+.
*
* @param string $command
* The actual command line to run.
* @return integer
* Exit code. Usually self::EXIT_ERROR or self::EXIT_SUCCESS.
*/
function execute($command, $expected_return = self::EXIT_SUCCESS) {
$this->_output = FALSE;
// todo check verbose level from phpunit.
if (TRUE) {
print "\nExecuting: $command \n";
}
exec($command, $this->_output, $return);
$this->assertEquals($expected_return, $return, 'Unexpected exit code: ' . $command);
return $return;
}
/**
* Invoke drush in via execute().
*
* @param command
* A defined drush command such as 'cron', 'status' or any of the available ones such as 'drush pm'.
* @param args
* Command arguments.
* @param $options
* An associative array containing options.
* @param $site_specification
* A site alias or site specification. Include the '@' at start of a site alias.
* @param $cd
* A directory to change into before executing.
* @return integer
* An exit code.
*/
function drush($command, array $args = array(), array $options = array(), $site_specification = NULL, $cd = NULL) {
$cmd[] = $cd ? sprintf('cd %s;', escapeshellarg($cd)) : NULL;
$cmd[] = UNISH_DRUSH;
$cmd[] = empty($site_specification) ? NULL : escapeshellarg($site_specification);
$cmd[] = $command;
foreach ($args as $arg) {
$cmd[] = escapeshellarg($arg);
}
foreach ($options as $key => $value) {
if (is_null($value)) {
$cmd[] = "--$key";
}
else {
$cmd[] = "--$key=" . escapeshellarg($value);
}
}
$exec = array_filter($cmd, 'strlen'); // Remove NULLs
return $this->execute(implode(' ', $exec));
}
/**
* Accessor for the last output.
* @return string Output as text.
* @access public
*/
function getOutput() {
return implode("\n", $this->_output);
}
/**
* Accessor for the last output.
* @return array Output as array of lines.
* @access public
*/
function getOutputAsList() {
return $this->_output;
}
function setUpDrupal($env = 'dev', $install = FALSE, $version_string = '7.x', $profile = NULL) {
$root = UNISH_SANDBOX . '/web';
$this->sites[$env]['root'] = $root;
$site = "$root/sites/$env";
if (is_null($profile)) {
$profile = substr($version_string, 0, 1) >= 7 ? 'testing' : 'default';
}
// Download Drupal if not already present.
if (!file_exists($root)) {
$options = array(
'destination' => UNISH_SANDBOX,
'drupal-project-rename' => 'web',
'yes' => NULL,
'quiet' => NULL,
);
$this->drush('pm-download', array("drupal-$version_string"), $options);
}
// If specified, install Drupal as a multi-site.
if ($install) {
$options = array(
'root' => $root,
'db-url' => UNISH_DB_URL . '/unish_' . $env,
'sites-subdir' => $env,
'yes' => NULL,
'quiet' => NULL,
);
$this->drush('site-install', array($profile), $options);
// Give us our write perms back.
$ret = chmod($site, 0777);
// Stash the db_url for this site.
$this->sites[$env]['db_url'] = UNISH_DB_URL . '/unish_' . $env;
}
else {
mkdir($site);
touch("$site/settings.php");
}
}
// Copied from D7 - profiles/standard/standard.install
function create_node_types_php() {
$php = "
\$types = array(
array(
'type' => 'page',
'name' => 'Basic page',
'base' => 'node_content',
'description' => 'Use <em>basic pages</em> for your static content, such as an \'About us\' page.',
'custom' => 1,
'modified' => 1,
'locked' => 0,
),
array(
'type' => 'article',
'name' => 'Article',
'base' => 'node_content',
'description' => 'Use <em>articles</em> for time-sensitive content like news, press releases or blog posts.',
'custom' => 1,
'modified' => 1,
'locked' => 0,
),
);
foreach (\$types as \$type) {
\$type = node_type_set_defaults(\$type);
node_type_save(\$type);
node_add_body_field(\$type);
}
";
return $php;
}
/*
* Prepare the contents of an aliases file.
*/
function file_aliases($aliases) {
foreach ($aliases as $name => $alias) {
$records[] = sprintf('$aliases[\'%s\'] = %s;', $name, var_export($alias, TRUE));
}
$contents = "<?php\n\n" . implode("\n\n", $records);
return $contents;
}
function file_delete_recursive($path) {
return exec('rm -rf ' . escapeshellarg($path));
}
}