/
simpletest_clone.test
196 lines (172 loc) · 6.03 KB
/
simpletest_clone.test
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
<?php
// $Id$
/**
* @file
* Helper test case for cloning an existing Drupal configuration.
*/
/**
* Clone existing Drupal instance.
*/
class SimpleTestCloneTestCase extends DrupalWebTestCase {
/**
* Tables listed in this array will not have data copied, only table
* structure. This is so that sites with large amounts of data can still be
* tested to a certain extent, without having the tests run on forever while
* the tables are copied over.
*/
protected $excludeTables = array();
/**
* Don't create test db via install, instead copy existing db.
*/
function setUp() {
// BEGIN BLOCK FROM DrupalWebTestCase::setUp().
global $db_prefix, $user;
// Store necessary current values before switching to prefixed database.
$this->originalPrefix = $db_prefix;
$clean_url_original = variable_get('clean_url', 0);
// END BLOCK FROM DrupalWebTestCase::setUp()
// Get the schema, being sure to rebuild it
$schemas = drupal_get_schema(NULL, TRUE);
// Generate temporary prefixed database to ensure that tests have a clean starting point.
$db_prefix = 'simpletest' . mt_rand(1000, 1000000);
// Copy each table into new database.
foreach ($schemas as $name => $schema) {
$this->cloneTable($name, $schema);
}
// BEGIN BLOCK FROM DrupalWebTestCase::setUp().
// Because the schema is static cached, we need to flush
// it between each run. If we don't, then it will contain
// stale data for the previous run's database prefix and all
// calls to it will fail.
drupal_get_schema(NULL, TRUE);
// Rebuild caches.
actions_synchronize();
_drupal_flush_css_js();
$this->refreshVariables();
$this->checkPermissions(array(), TRUE);
// Log in with a clean $user.
$this->originalUser = $user;
session_save_session(FALSE);
$user = user_load(array('uid' => 1));
// Restore necessary variables.
// variable_set('install_profile', 'default');
// variable_set('install_task', 'profile-finished');
// variable_set('clean_url', $clean_url_original);
// variable_set('site_mail', 'simpletest@example.com');
// Use temporary files directory with the same prefix as database.
$this->originalFileDirectory = file_directory_path();
variable_set('file_directory_path', file_directory_path() . '/' . $db_prefix);
$directory = file_directory_path();
file_check_directory($directory, FILE_CREATE_DIRECTORY); // Create the files directory.
set_time_limit($this->timeLimit);
// END BLOCK FROM DrupalWebTestCase::setUp().
}
/**
* Need to override DrupalWebTestCase's tearDown() which rebuilds
* module_list() in bootstrap mode and will cause things like
* drupal_get_schema() to only think devel modules are loaded. Put it back
* into run mode.
*/
function tearDown() {
parent::tearDown();
module_list(TRUE, FALSE);
}
/**
* Correctly prefix a table name.
*
* This code is based off of core's db_prefix_tables().
*
* @param $db_prefix
* Mixed array or string with prefixes.
* @param $name
* String with the table name.
* @return
* String with prefixed table name.
*/
protected function prefixTable($db_prefix, $name) {
if (is_array($db_prefix)) {
if (array_key_exists($name, $db_prefix)) {
return $db_prefix[$name] . $name;
}
elseif (array_key_exists('default', $db_prefix)) {
return $db_prefix['default'] . $name;
}
return $name;
}
return $db_prefix . $name;
}
/**
* Mirror over an existing tables structure, and copy the data.
*
* @param $name
* Table name.
* @param $schema
* A Schema API definition array.
* @return
* Array of table creation results.
*/
protected function cloneTable($name, $schema) {
global $db_prefix;
$return = array();
db_create_table($return, $name, $schema);
// Do our own prefixing of the table names.
$source = db_escape_table($this->prefixTable($this->originalPrefix, $name));
$target = db_escape_table($this->prefixTable($db_prefix, $name));
if (!in_array($name, $this->excludeTables)) {
if ($name == 'users') {
// UID = 0 confuses mysql. Special handling here, taken from system.install
db_query("INSERT INTO %s (uid, name, pass, mail, mode, sort, threshold, theme, signature, created, access, login, status, timezone, language, picture, init, data)
SELECT uid + 1, name, pass, mail, mode, sort, threshold, theme, signature, created, access, login, status, timezone, language, picture, init, data FROM %s ORDER BY uid", $target, $source);
// Update uid
db_query("UPDATE %s SET uid = uid - 1", $target);
}
else {
db_query("INSERT INTO %s SELECT * FROM %s", $target, $source);
}
}
}
/**
* Create user with Web Admin role.
*
* @param string $role
* An existing Drupal user role to assign to the newly created user.
* @return object
* A Drupal user object.
*/
protected function drupalCreateUserWithRole($role) {
$rid = $this->_getRoleId($role);
if ($rid) {
$user = $this->drupalCreateUser(array('access content'));
$edit['roles'] = array($rid => $rid);
$account = user_save($user, $edit);
$account->pass_raw = $user->pass_raw;
return $account;
}
}
/**
* Create normal user.
*/
protected function drupalCreateNormalUser() {
$user = $this->drupalCreateUser(array('access content'));
$edit['roles'] = array();
$account = user_save($user, $edit);
$account->pass_raw = $user->pass_raw;
return $account;
}
/**
* Find the role ID for a given role name.
*
* @param string $name
* The role name
* @return integer
* The corresponding role ID.
*/
private function _getRoleId($name) {
$rid = db_result(db_query("SELECT rid FROM {role} WHERE name = '%s'", array(':name' => $name)));
if (!$rid) {
$this->fail(t('No corresponding rid found for the %role role.', array('%role' => $name)));
return FALSE;
}
return $rid;
}
}