-
Notifications
You must be signed in to change notification settings - Fork 7
/
coder_upgrade.test
311 lines (268 loc) · 11.9 KB
/
coder_upgrade.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
/**
* @file
* Provides tests.
*/
module_load_include('inc', 'coder_upgrade', 'coder_upgrade');
/**
* Unit tests for the upgrade routines.
*/
class CoderUpgradeUnitTestCase extends BackdropUnitTestCase {
protected $test_directory, $site_directory;
public static function getInfo1($subdir) {
// Always display these warnings since this function is only called when cache is cleared.
$settings = l('here', 'admin/config/development/coder-upgrade/settings');
// $msg = "WARNING: Do not run this test if the \"Replace files\" option is checked for this module."; // Change this setting $settings.";
$msg1 = '';
// if (variable_get('coder_upgrade_enable_debug_output', FALSE)) {
// $msg1 = " WARNING: Do not run this test if the \"Enable debug output from coder upgrade\" option is checked for this module."; // Change this setting $settings.";
// }
$msg2 = '';
// if (variable_get('coder_upgrade_enable_parser_debug_output', FALSE)) {
// $msg2 = " WARNING: Do not run this test if the \"Enable debug output from grammar parser\" option is checked for this module."; // Change this setting $settings.";
// }
// $msg3 = '';
// if (!variable_get('coder_upgrade_preserve_array_format', FALSE)) {
$msg3 = " NOTICE: During this test the \"Preserve array formatting\" option will enabled for this module. Afterward, this setting may be disabled $settings.";
// }
$msg = "WARNING: Do not run this test if any of these options is checked for this module: \"Replace files,\" \"Enable debug output from coder upgrade,\" or \"Enable debug output from grammar parser.\" Change these setting $settings.";
$dir = variable_get('coder_upgrade_dir_new', DEADWOOD_OLD);
return array(
'name' => "Run interface ($subdir)",
'description' => "Test the output from the upgrade routines on the files in the files/$dir/$subdir directory.$msg$msg1$msg2$msg3",
'group' => 'Coder Upgrade',
);
}
protected function setUp() {
/*
* When running a unit test case, we do not have access to a database once
* the parent::setUp() method is called. If there are any pre-processing
* tasks that require database access, we need to do them first.
*/
if (!variable_get('coder_upgrade_preserve_array_format', FALSE)) {
// Expected files were created with this setting enabled.
variable_set('coder_upgrade_preserve_array_format', TRUE);
}
file_put_contents('output.html', '');
file_put_contents('output.html', __METHOD__ . "\n", FILE_APPEND);
// Build theme registry cache.
$this->captureThemeInfo();
file_put_contents('output.html', "after captureThemeInfo\n", FILE_APPEND);
// Save the live site files directory path.
$this->site_directory = BACKDROP_ROOT . '/' . coder_upgrade_directory_path('new');
file_put_contents('output.html', $this->site_directory . "\n", FILE_APPEND);
parent::setUp('grammar_parser', 'coder_upgrade');
// Create output file directories.
module_load_include('install', 'coder_upgrade');
coder_upgrade_install();
file_put_contents('output.html', "after install\n", FILE_APPEND);
// Enable debug printing.
global $_coder_upgrade_debug;
$_coder_upgrade_debug = TRUE;
}
/**
* Stores the theme registry for core modules and the modules being upgraded.
*/
protected function captureThemeInfo() {
if (variable_get('coder_upgrade_replace_files', FALSE)) {
return;
}
// Include necessary files.
$module_dirname = BACKDROP_ROOT . '/' . backdrop_get_path('module', 'grammar_parser');
require_once $module_dirname . '/engine/grammar_parser.parser.inc';
require_once $module_dirname . '/engine/grammar_parser.reader.inc';
// TODO Move the debug_print method to .inc file??? Then avoid this load at this time.
$module_dirname = BACKDROP_ROOT . '/' . backdrop_get_path('module', 'coder_upgrade');
require_once $module_dirname . '/includes/main.inc';
require_once $module_dirname . '/conversions/begin.inc';
require_once $module_dirname . '/conversions/function.inc';
coder_upgrade_debug_print("module = $module_dirname");
coder_upgrade_path_clear('memory');
coder_upgrade_memory_print('load code');
$in_dirname = $module_dirname . '/tests/old/';
$out_dirname = BACKDROP_ROOT . '/' . coder_upgrade_directory_path('new');
$directories = array($this->test_directory => 1);
foreach ($directories as $key => $directory) {
$items[] = array(
'name' => $key,
'old_dir' => $in_dirname . $key,
'new_dir' => $out_dirname . $key,
);
}
// Build theme registry cache.
coder_upgrade_upgrade_begin_alter($items[0]);
}
/**
* Tests the upgrade routines (outside of the user interface).
*/
protected function testRunInterface() {
file_put_contents('output.html', __METHOD__ . "\n", FILE_APPEND);
$_coder_upgrade_replace_files = variable_get('coder_upgrade_replace_files', FALSE);
if ($_coder_upgrade_replace_files) {
$settings = l('here', 'admin/config/development/coder-upgrade/settings');
$msg = "WARNING: Do not run this test if the \"Replace files\" option is checked for this module. Change this setting $settings.";
$this->assertFalse($_coder_upgrade_replace_files, $msg, 'Settings');
return;
}
file_put_contents('output.html', __METHOD__ . "\n", FILE_APPEND);
$module_dirname = BACKDROP_ROOT . '/' . backdrop_get_path('module', 'coder_upgrade');
$in_dirname = $module_dirname . '/tests/old/';
$expected_dirname = $module_dirname . '/tests/new/';
$out_dirname = BACKDROP_ROOT . '/' . coder_upgrade_directory_path('new');
file_put_contents('output.html', "$out_dirname\n", FILE_APPEND);
$upgrades = coder_upgrade_upgrade_info();
$extensions = array(
'inc' => TRUE,
'info' => TRUE,
'install' => TRUE,
'module' => TRUE,
'php' => FALSE,
'profile' => FALSE,
'test' => FALSE,
'theme' => FALSE,
);
$directories = array($this->test_directory => 1);
foreach ($directories as $key => $directory) {
$items[] = array(
'name' => $key,
'old_dir' => $in_dirname . $key,
'new_dir' => $out_dirname . $key,
);
}
// If Backdrop won't commit patch to BackdropUnitTestCase.php, then use a global.
global $_coder_upgrade_is_test;
$_coder_upgrade_is_test = TRUE;
// Apply upgrade routines.
module_load_include('inc', 'coder_upgrade', 'includes/main');
if (coder_upgrade_start($upgrades, $extensions, $items)) {
}
$ignore = array('.', '..', 'CVS', '.svn');
// Loop on files.
// TODO This needs to recurse if there are subdirectories.
$filenames = scandir($in_dirname . $key . '/');
cdp($filenames);
foreach ($filenames as $filename) {
if (in_array($filename, $ignore)) {
continue;
}
// Set file names to be compared.
$expected_filename = $expected_dirname . $key . '/' . $filename;
$out_filename = $out_dirname . $key . '/' . $filename;
coder_upgrade_debug_print("exp = $expected_filename");
coder_upgrade_debug_print("out = $out_filename");
// Compare upgraded file to expected file.
$this->compareFiles($expected_filename, $out_filename);
}
}
function compareFiles($expected_filename, $out_filename) {
$name = pathinfo($out_filename, PATHINFO_BASENAME);
$b1 = file_exists($expected_filename);
$this->assertTrue($b1, 'Expected file exists', $name);
$b2 = file_exists($out_filename);
$this->assertTrue($b2, 'Output file exists', $name);
// Save a copy outside of the simpletest directory that will be deleted.
// TODO This copy may have name clashes.
copy($out_filename, $this->site_directory . $name);
if ($b1 && $b2) {
$expected = file_get_contents($expected_filename);
$actual = file_get_contents($out_filename);
$this->assertEqual($expected, $actual, 'Contents of output file match that of expected file', $name);
}
else {
$this->assertEqual('expected', 'actual', 'Contents of output file match that of expected file', $name);
}
}
}
/**
* Unit tests on a single file in files/coder_upgrade/old/test.
*
* Use this while developing on an upgrade routine.
*/
class CoderUpgradeUnitTestCase1 extends CoderUpgradeUnitTestCase {
public static function getInfo() {
return CoderUpgradeUnitTestCase::getInfo1('test');
}
protected function setUp() {
parent::setUp();
$this->test_directory = 'test';
}
}
/**
* Unit tests on a sample module in files/coder_upgrade/old/samples.
*/
class CoderUpgradeUnitTestCase2 extends CoderUpgradeUnitTestCase {
public static function getInfo() {
return CoderUpgradeUnitTestCase::getInfo1('samples');
}
protected function setUp() {
parent::setUp();
$this->test_directory = 'samples';
}
}
/**
* Functional tests for the admin interface (settings and execution).
*/
class CoderUpgradeWebTestCase extends BackdropWebTestCase {
public static function getInfo() {
return array(
'name' => 'Settings interface',
'description' => 'Test the administration interface for Coder Upgrade.',
'group' => 'Coder Upgrade',
);
}
function setUp() {
parent::setUp('coder_upgrade');
$this->admin_user = $this->backdropCreateUser(array('access administration pages', 'administer site configuration', 'administer code conversions'));
$this->backdropLogin($this->admin_user);
}
/**
* Call test methods (prefixed with 'check' to avoid setUp overhead).
*/
function testAdminInterface() {
// Include the admin file so we can call its functions.
module_load_include('inc', 'coder_upgrade', 'coder_upgrade');
$this->checkSettingsInterface();
$this->checkRunInterface();
}
/**
* Test the settings via the user interface.
*/
function checkSettingsInterface() {
// Visit the advanced menu settings page.
$this->backdropGet('admin/config/development/coder_upgrade/settings');
// Update the settings.
$edit = array();
$edit['coder_upgrade_dir_old'] = $this->randomName();
$edit['coder_upgrade_dir_new'] = $this->randomName();
$edit['coder_upgrade_dir_patch'] = $this->randomName();
$this->backdropPost(NULL, $edit, t('Save configuration'));
$this->assertRaw(t('The configuration options have been saved.'), t('Settings updated successfully'));
// Verify the settings.
$settings = variable_get('coder_upgrade_dir_old', '');
$this->assertEqual($settings, $edit['coder_upgrade_dir_old'], t('Old directory name found in variable.'));
$settings = variable_get('coder_upgrade_dir_new', '');
$this->assertEqual($settings, $edit['coder_upgrade_dir_new'], t('New directory name found in variable.'));
$settings = variable_get('coder_upgrade_dir_patch', '');
$this->assertEqual($settings, $edit['coder_upgrade_dir_patch'], t('Patch directory name found in variable.'));
// There is no reset button visible?
// Reset the settings.
$edit = array();
$edit['coder_upgrade_dir_old'] = DEADWOOD_OLD;
$edit['coder_upgrade_dir_new'] = DEADWOOD_NEW;
$edit['coder_upgrade_dir_patch'] = DEADWOOD_PATCH;
$this->backdropPost(NULL, $edit, t('Save configuration')); // $this->backdropPost(NULL, $edit, t('Reset to defaults'));
$this->assertRaw(t('The configuration options have been saved.'), t('Settings updated successfully'));
// Verify the settings.
$settings = variable_get('coder_upgrade_dir_old', '');
$this->assertEqual($settings, $edit['coder_upgrade_dir_old'], t('Old directory name was cleared.'));
$settings = variable_get('coder_upgrade_dir_new', '');
$this->assertEqual($settings, $edit['coder_upgrade_dir_new'], t('New directory name was cleared.'));
$settings = variable_get('coder_upgrade_dir_patch', '');
$this->assertEqual($settings, $edit['coder_upgrade_dir_patch'], t('Patch directory name was cleared.'));
}
/**
* Test the running via the user interface.
*/
function checkRunInterface() {
}
}