| 1 |
<?php
|
| 2 |
|
| 3 |
/**
|
| 4 |
* Implementation of hook_help().
|
| 5 |
*/
|
| 6 |
function simpletest_automator_help($section, $arg) {
|
| 7 |
$output = '';
|
| 8 |
switch ($section) {
|
| 9 |
case 'admin/build/simpletest_automator':
|
| 10 |
$output .= '<p>'. t('The SimpleTest Automator is designed to let you perform manual testing once, and then automate the same actions you performed automatically using the SimpleTest automated testing framework.') .'</p>';
|
| 11 |
}
|
| 12 |
return $output;
|
| 13 |
}
|
| 14 |
|
| 15 |
/**
|
| 16 |
* Menu callback: list of all simpletests.
|
| 17 |
*/
|
| 18 |
function simpletest_automator_admin_overview() {
|
| 19 |
$tests = db_query('SELECT name, said, description FROM {simpletest_automator}');
|
| 20 |
$rows = array();
|
| 21 |
while ($test = db_fetch_object($tests)) {
|
| 22 |
$rows[] = array(
|
| 23 |
check_plain($test->name) .'<div class="description">' . check_plain($test->description) . '</div>',
|
| 24 |
l(t('Edit'), 'admin/build/simpletest_automator/' . $test->said),
|
| 25 |
l(t('Modules'), 'admin/build/simpletest_automator/' . $test->said . '/modules'),
|
| 26 |
l(t('Permissions'), 'admin/build/simpletest_automator/' . $test->said . '/permissions'),
|
| 27 |
l(t('Actions'), 'admin/build/simpletest_automator/' . $test->said . '/actions'),
|
| 28 |
l(t('Export'), 'admin/build/simpletest_automator/' . $test->said . '/export'),
|
| 29 |
l(t('Record'), 'admin/build/simpletest_automator/' . $test->said . '/record'),
|
| 30 |
l(t('Delete'), 'admin/build/simpletest_automator/' . $test->said . '/delete'),
|
| 31 |
);
|
| 32 |
}
|
| 33 |
if (empty($rows)) {
|
| 34 |
$rows = array(array(array('data' => t('No tests found. You can <a href="!url">add a new one</a>.', array('!url' => url('admin/build/simpletest_automator/add'))), 'colspan' => 8)));
|
| 35 |
}
|
| 36 |
return theme('table', array('Name', array('data' => t('Operations'), 'colspan' => 7)), $rows);
|
| 37 |
}
|
| 38 |
|
| 39 |
|
| 40 |
function simpletest_automator_admin_import(&$form_state) {
|
| 41 |
$form = array();
|
| 42 |
$form['import'] = array(
|
| 43 |
'#type' => 'textarea',
|
| 44 |
'#title' => t('Test'),
|
| 45 |
'#description' => t('Enter a previously exported ("For import") SimpleTest automator test, as the serialized output given.'),
|
| 46 |
'#size' => 40,
|
| 47 |
);
|
| 48 |
$form['submit'] = array(
|
| 49 |
'#type' => 'submit',
|
| 50 |
'#value' => t('Import'),
|
| 51 |
);
|
| 52 |
return $form;
|
| 53 |
}
|
| 54 |
|
| 55 |
function simpletest_automator_admin_import_submit($form, &$form_state) {
|
| 56 |
eval(trim($form_state['values']['import']));
|
| 57 |
$actions = $simpletest_automator->actions;
|
| 58 |
$said = simpletest_automator_save($simpletest_automator);
|
| 59 |
foreach ($actions as $action) {
|
| 60 |
$action->said = $said;
|
| 61 |
simpletest_automator_action_save($action);
|
| 62 |
}
|
| 63 |
drupal_set_message(t('Import successful.'));
|
| 64 |
$form_state['redirect'] = 'admin/build/simpletest_automator';
|
| 65 |
}
|
| 66 |
|
| 67 |
/**
|
| 68 |
* Menu callback: simpletest editing page.
|
| 69 |
*/
|
| 70 |
function simpletest_automator_admin_add(&$form_state) {
|
| 71 |
if (!isset($form_state['storage']['simpletest_automator'])) {
|
| 72 |
$form_state['storage']['step'] = 0;
|
| 73 |
}
|
| 74 |
|
| 75 |
if ($form_state['storage']['step'] == 0) {
|
| 76 |
$form = simpletest_automator_admin_edit(array(), NULL);
|
| 77 |
}
|
| 78 |
elseif ($form_state['storage']['step'] == 1) {
|
| 79 |
$form = simpletest_automator_admin_modules_form(array(), NULL, FALSE);
|
| 80 |
$form['overview'] = array(
|
| 81 |
'#type' => 'item',
|
| 82 |
'#title' => t('Please specify which modules will be enabled in your test'),
|
| 83 |
'#weight' => -1,
|
| 84 |
);
|
| 85 |
}
|
| 86 |
else {
|
| 87 |
$form = simpletest_automator_admin_permissions($form_state, NULL);
|
| 88 |
$form['#theme'] = 'simpletest_automator_admin_permissions';
|
| 89 |
$form['overview'] = array(
|
| 90 |
'#type' => 'item',
|
| 91 |
'#title' => t('Please specify which permissions the testing user will have'),
|
| 92 |
'#weight' => -1,
|
| 93 |
);
|
| 94 |
}
|
| 95 |
|
| 96 |
$form['submit'] = array(
|
| 97 |
'#type' => 'submit',
|
| 98 |
'#value' => ($form_state['storage']['step'] != 2 ? t('Next') : t('Finish')),
|
| 99 |
'#weight' => 100,
|
| 100 |
);
|
| 101 |
|
| 102 |
return $form;
|
| 103 |
}
|
| 104 |
|
| 105 |
function simpletest_automator_admin_modules_form($form_state = array(), $simpletest_automator = NULL, $edit = TRUE) {
|
| 106 |
if ($simpletest_automator) {
|
| 107 |
$enabled_modules = $simpletest_automator->modules;
|
| 108 |
}
|
| 109 |
if (!isset($enabled_modules)) {
|
| 110 |
$enabled_modules = array();
|
| 111 |
}
|
| 112 |
// Get current list of modules.
|
| 113 |
$files = module_rebuild_cache();
|
| 114 |
uasort($files, 'simpletest_automator_admin_modules_by_info_name');
|
| 115 |
$dependencies = array();
|
| 116 |
$modules = array();
|
| 117 |
$form['modules'] = array('#tree' => TRUE);
|
| 118 |
// The list of required modules.
|
| 119 |
$modules_required = drupal_required_modules();
|
| 120 |
// Iterate through each of the modules.
|
| 121 |
foreach ($files as $filename => $module) {
|
| 122 |
if (isset($module->info['hidden']) && $module->info['hidden'] == TRUE) {
|
| 123 |
continue;
|
| 124 |
}
|
| 125 |
$extra = array();
|
| 126 |
// If the module is requried, set it to be so.
|
| 127 |
if (in_array($filename, $modules_required)) {
|
| 128 |
$extra['required'] = TRUE;
|
| 129 |
}
|
| 130 |
$extra['enabled'] = in_array($filename, $enabled_modules);
|
| 131 |
// If this module has dependencies, add them to the array.
|
| 132 |
if (is_array($module->info['dependencies'])) {
|
| 133 |
foreach ($module->info['dependencies'] as $dependency) {
|
| 134 |
if (!isset($files[$dependency])) {
|
| 135 |
$extra['dependencies'][$dependency] = drupal_ucfirst($dependency) . t(' (<span class="admin-missing">missing</span>)');
|
| 136 |
$extra['disabled'] = TRUE;
|
| 137 |
}
|
| 138 |
elseif (!$files[$dependency]->status) {
|
| 139 |
$extra['dependencies'][$dependency] = $files[$dependency]->info['name'] . t(' (<span class="admin-disabled">disabled</span>)');
|
| 140 |
}
|
| 141 |
else {
|
| 142 |
$extra['dependencies'][$dependency] = $files[$dependency]->info['name'] . t(' (<span class="admin-enabled">enabled</span>)');
|
| 143 |
}
|
| 144 |
}
|
| 145 |
}
|
| 146 |
// Mark dependents disabled so user can not remove modules being depended on.
|
| 147 |
$dependents = array();
|
| 148 |
foreach ($module->info['dependents'] as $dependent) {
|
| 149 |
if ($files[$dependent]->status == 1) {
|
| 150 |
$extra['dependents'][] = $files[$dependent]->info['name'] . t(' (<span class="admin-enabled">enabled</span>)');
|
| 151 |
$extra['disabled'] = TRUE;
|
| 152 |
}
|
| 153 |
else {
|
| 154 |
$extra['dependents'][] = $files[$dependent]->info['name'] . t(' (<span class="admin-disabled">disabled</span>)');
|
| 155 |
}
|
| 156 |
}
|
| 157 |
$form['modules'][$filename] = _simpletest_automator_admin_modules_build_row($module->info, $extra);
|
| 158 |
}
|
| 159 |
$form['modules'] += array(
|
| 160 |
'#theme' => 'simpletest_automator_admin_modules',
|
| 161 |
'#header' => array(
|
| 162 |
array('data' => t('Enabled'), 'class' => 'checkbox'),
|
| 163 |
t('Name'),
|
| 164 |
t('Version'),
|
| 165 |
t('Description'),
|
| 166 |
),
|
| 167 |
);
|
| 168 |
if ($edit) {
|
| 169 |
$form['submit'] = array(
|
| 170 |
'#type' => 'submit',
|
| 171 |
'#value' => t('Update'),
|
| 172 |
);
|
| 173 |
}
|
| 174 |
return $form;
|
| 175 |
}
|
| 176 |
|
| 177 |
/**
|
| 178 |
* Array sorting callback; sorts modules or themes by their name.
|
| 179 |
*/
|
| 180 |
function simpletest_automator_admin_modules_by_info_name($a, $b) {
|
| 181 |
return strcasecmp($a->info['name'], $b->info['name']);
|
| 182 |
}
|
| 183 |
|
| 184 |
/**
|
| 185 |
* Admin permissions page.
|
| 186 |
*/
|
| 187 |
function simpletest_automator_admin_permissions($form_state, $simpletest_automator) {
|
| 188 |
$form = array();
|
| 189 |
if (!isset($form_state['storage']['permissions'])) {
|
| 190 |
global $db_prefix;
|
| 191 |
$db_prefix_original = $db_prefix;
|
| 192 |
$db_prefix = $simpletest_automator->db_prefix;
|
| 193 |
module_list(TRUE, FALSE);
|
| 194 |
|
| 195 |
foreach (module_implements('perm', FALSE, TRUE) as $module) {
|
| 196 |
$form_state['storage']['permissions'][$module] = module_invoke($module, 'perm');
|
| 197 |
}
|
| 198 |
|
| 199 |
$db_prefix = $db_prefix_original;
|
| 200 |
module_list(TRUE, FALSE);
|
| 201 |
module_implements(NULL, FALSE, TRUE);
|
| 202 |
node_get_types(NULL, NULL, TRUE);
|
| 203 |
}
|
| 204 |
if ($simpletest_automator) {
|
| 205 |
$form_state['storage']['simpletest_automator'] = (array)$simpletest_automator;
|
| 206 |
}
|
| 207 |
|
| 208 |
// Render role/permission overview:
|
| 209 |
$options = array();
|
| 210 |
$hide_descriptions = !system_admin_compact_mode();
|
| 211 |
foreach ($form_state['storage']['permissions'] as $module => $permissions) {
|
| 212 |
foreach ($permissions as $permission => $permission_data) {
|
| 213 |
$form['permissions'][$module][$permission]['name'] = array(
|
| 214 |
'#type' => 'item',
|
| 215 |
'#markup' => t($permission_data['title']),
|
| 216 |
'#description' => $hide_descriptions ? $permission_data['description'] : NULL,
|
| 217 |
);
|
| 218 |
$form['permissions'][$module][$permission]['enable'] = array(
|
| 219 |
'#type' => 'checkbox',
|
| 220 |
'#parents' => array('permissions', $permission),
|
| 221 |
'#default_value' => (isset($simpletest_automator->permissions) && in_array($permission, $simpletest_automator->permissions)),
|
| 222 |
);
|
| 223 |
}
|
| 224 |
}
|
| 225 |
$form['submit'] = array(
|
| 226 |
'#type' => 'submit',
|
| 227 |
'#value' => t('Save'),
|
| 228 |
);
|
| 229 |
|
| 230 |
return $form;
|
| 231 |
}
|
| 232 |
|
| 233 |
function simpletest_automator_admin_permissions_submit($form, $form_state) {
|
| 234 |
$form_state['storage']['simpletest_automator']['permissions'] = array_keys(array_filter($form_state['values']['permissions']));
|
| 235 |
simpletest_automator_save((object) $form_state['storage']['simpletest_automator']);
|
| 236 |
$form_state['redirect'] = 'admin/build/simpletest_automator' . $form_state['storage']['simpletest_automator']['said'] . '/permissions';
|
| 237 |
}
|
| 238 |
|
| 239 |
function _simpletest_automator_admin_modules_build_row($info, $extra) {
|
| 240 |
// Add in the defaults.
|
| 241 |
$extra += array(
|
| 242 |
'required' => FALSE,
|
| 243 |
'dependencies' => array(),
|
| 244 |
'dependents' => array(),
|
| 245 |
'disabled' => FALSE,
|
| 246 |
'enabled' => FALSE,
|
| 247 |
'help' => '',
|
| 248 |
);
|
| 249 |
$form = array(
|
| 250 |
'#tree' => TRUE,
|
| 251 |
);
|
| 252 |
// Set the basic properties.
|
| 253 |
$form['name'] = array(
|
| 254 |
'#markup' => t($info['name']),
|
| 255 |
);
|
| 256 |
$form['description'] = array(
|
| 257 |
'#markup' => t($info['description']),
|
| 258 |
);
|
| 259 |
$form['version'] = array(
|
| 260 |
'#markup' => $info['version'],
|
| 261 |
);
|
| 262 |
$form['#dependencies'] = $extra['dependencies'];
|
| 263 |
$form['#dependents'] = $extra['dependents'];
|
| 264 |
|
| 265 |
// Check the compatibilities.
|
| 266 |
$compatible = TRUE;
|
| 267 |
$status_short = '';
|
| 268 |
$status_long = '';
|
| 269 |
|
| 270 |
// Check the core compatibility.
|
| 271 |
if (!isset($info['core']) || $info['core'] != DRUPAL_CORE_COMPATIBILITY) {
|
| 272 |
$compatible = FALSE;
|
| 273 |
$status_short .= t('Incompatible with this version of Drupal core. ');
|
| 274 |
$status_long .= t('This version is incompatible with the !core_version version of Drupal core. ', array('!core_version' => VERSION));
|
| 275 |
}
|
| 276 |
|
| 277 |
// Ensure this module is compatible with the currently installed version of PHP.
|
| 278 |
if (version_compare(phpversion(), $info['php']) < 0) {
|
| 279 |
$compatible = FALSE;
|
| 280 |
$status_short .= t('Incompatible with this version of PHP');
|
| 281 |
if (substr_count($info['php'], '.') < 2) {
|
| 282 |
$php_required .= '.*';
|
| 283 |
}
|
| 284 |
$status_long .= t('This module requires PHP version @php_required and is incompatible with PHP version !php_version.', array('@php_required' => $php_required, '!php_version' => phpversion()));
|
| 285 |
}
|
| 286 |
|
| 287 |
// If this module is compatible, present a checkbox indicating
|
| 288 |
// this module may be installed. Otherwise, show a big red X.
|
| 289 |
if ($compatible) {
|
| 290 |
$form['enable'] = array(
|
| 291 |
'#type' => 'checkbox',
|
| 292 |
'#title' => t('Enable'),
|
| 293 |
'#required' => $extra['required'],
|
| 294 |
'#default_value' => $extra['required'] || $extra['enabled'],
|
| 295 |
'#disabled' => $extra['required'],
|
| 296 |
);
|
| 297 |
}
|
| 298 |
else {
|
| 299 |
$form['enable'] = array(
|
| 300 |
'#markup' => theme('image', 'misc/watchdog-error.png', t('incompatible'), $status_short),
|
| 301 |
);
|
| 302 |
$form['description']['#value'] .= theme('simpletest_automator_admin_modules_incompatible', $status_long);
|
| 303 |
}
|
| 304 |
|
| 305 |
// Show a "more help" link for modules that have them.
|
| 306 |
if ($extra['help']) {
|
| 307 |
$form['help'] = array(
|
| 308 |
'#markup' => $extra['help'],
|
| 309 |
);
|
| 310 |
}
|
| 311 |
return $form;
|
| 312 |
}
|
| 313 |
|
| 314 |
function simpletest_automator_admin_add_submit($form, &$form_state) {
|
| 315 |
if ($form_state['storage']['step'] == 0) {
|
| 316 |
$form_state['storage']['step'] = 1;
|
| 317 |
foreach (array('name', 'description', 'test_group', 'file', 'class', 'method') as $field) {
|
| 318 |
$form_state['storage']['simpletest_automator'][$field] = $form_state['values'][$field];
|
| 319 |
}
|
| 320 |
}
|
| 321 |
elseif ($form_state['storage']['step'] == 1) {
|
| 322 |
$required_modules = drupal_required_modules();
|
| 323 |
foreach ($form_state['values']['modules'] as $module => $enabled) {
|
| 324 |
if (!in_array($module, $required_modules) && $enabled['enable']) {
|
| 325 |
$form_state['storage']['simpletest_automator']['modules'][] = $module;
|
| 326 |
}
|
| 327 |
}
|
| 328 |
$form_state['storage']['simpletest_automator']['modules'][] = 'simpletest_automator_proctor';
|
| 329 |
$form_state['storage']['step'] = 2;
|
| 330 |
|
| 331 |
global $db_prefix, $conf;
|
| 332 |
|
| 333 |
// Store necessary current values before switching to prefixed database.
|
| 334 |
$db_prefix_original = $db_prefix;
|
| 335 |
$file_directory_path_original = file_directory_path();
|
| 336 |
$clean_url_original = variable_get('clean_url', 0);
|
| 337 |
|
| 338 |
// Generate temporary prefixed database to ensure that tests have a clean starting point.
|
| 339 |
$db_prefix = Database::getConnection()->prefixTables('{st_automator' . mt_rand(1000, 1000000) . '}');
|
| 340 |
|
| 341 |
include_once DRUPAL_ROOT . '/includes/install.inc';
|
| 342 |
drupal_install_system();
|
| 343 |
|
| 344 |
db_query('INSERT INTO {registry} SELECT * FROM ' . $db_prefix_original . 'registry');
|
| 345 |
db_query('INSERT INTO {registry_file} SELECT * FROM ' . $db_prefix_original . 'registry_file');
|
| 346 |
|
| 347 |
// Add the specified modules to the list of modules in the default profile.
|
| 348 |
$args = func_get_args();
|
| 349 |
$modules = array_unique(array_merge(drupal_get_profile_modules('default', 'en'), $form_state['storage']['simpletest_automator']['modules']));;
|
| 350 |
drupal_install_modules($modules, TRUE);
|
| 351 |
|
| 352 |
// Because the schema is static cached, we need to flush it between each
|
| 353 |
// run. If we don't, then it will contain stale data for the previous run's
|
| 354 |
// database prefix and all calls to it will fail.
|
| 355 |
drupal_get_schema(NULL, TRUE);
|
| 356 |
|
| 357 |
// Run default profile tasks.
|
| 358 |
$task = 'profile';
|
| 359 |
default_profile_tasks($task, '');
|
| 360 |
|
| 361 |
// Rebuild caches.
|
| 362 |
actions_synchronize();
|
| 363 |
_drupal_flush_css_js();
|
| 364 |
cache_clear_all('variables', 'cache');
|
| 365 |
$conf = variable_init();
|
| 366 |
|
| 367 |
// Restore necessary variables.
|
| 368 |
variable_set('install_profile', 'default');
|
| 369 |
variable_set('install_task', 'profile-finished');
|
| 370 |
variable_set('clean_url', $clean_url_original);
|
| 371 |
variable_set('site_mail', 'simpletest@example.com');
|
| 372 |
|
| 373 |
// Use temporary files directory with the same prefix as database.
|
| 374 |
variable_set('file_directory_path', $file_directory_path_original . '/' . $db_prefix);
|
| 375 |
$directory = file_directory_path();
|
| 376 |
// Create the files directory.
|
| 377 |
file_check_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
|
| 378 |
|
| 379 |
foreach (module_implements('perm', FALSE, TRUE) as $module) {
|
| 380 |
$form_state['storage']['permissions'][$module] = module_invoke($module, 'perm');
|
| 381 |
}
|
| 382 |
|
| 383 |
$db_prefix = $db_prefix_original;
|
| 384 |
module_list(TRUE, FALSE);
|
| 385 |
module_implements(NULL, FALSE, TRUE);
|
| 386 |
}
|
| 387 |
else {
|
| 388 |
$form_state['storage']['simpletest_automator']['permissions'] = array_keys(array_filter($form_state['values']['permissions']));
|
| 389 |
simpletest_automator_save((object)$form_state['storage']['simpletest_automator']);
|
| 390 |
drupal_set_message(t('Your test has been saved.'));
|
| 391 |
$form_state['redirect'] = 'admin/build/simpletest_automator';
|
| 392 |
unset($form_state['storage']);
|
| 393 |
}
|
| 394 |
}
|
| 395 |
|
| 396 |
/**
|
| 397 |
* Menu callback: display a confirm form for deleting a SimpleTest.
|
| 398 |
*/
|
| 399 |
function simpletest_automator_admin_delete(&$form_state, $simpletest_automator) {
|
| 400 |
return confirm_form(array('said' => array('#type' => 'value', '#value' => $simpletest_automator->said)), t('Are you sure you want to delete %test?', array('%test' => $simpletest_automator->name)), 'admin/build/simpletest_automator/' . $simpletest_automator->said, t('Are you sure you want to delete %test? Deleting it will permanently delete all data associated with the test, in a non-recoverable manner.', array('%test' => $simpletest_automator->name)), t('Delete'));
|
| 401 |
}
|
| 402 |
|
| 403 |
function simpletest_automator_admin_delete_submit($form, &$form_state) {
|
| 404 |
simpletest_automator_delete($form_state['values']['said']);
|
| 405 |
$form_state['redirect'] = 'admin/build/simpletest_automator';
|
| 406 |
drupal_set_message(t('Test deleted.'));
|
| 407 |
}
|
| 408 |
|
| 409 |
function simpletest_automator_admin_edit($form_state, $simpletest_automator) {
|
| 410 |
$form_state['storage']['simpletest_automator'] = $simpletest_automator;
|
| 411 |
$form = array();
|
| 412 |
$form['name'] = array(
|
| 413 |
'#title' => t('Name'),
|
| 414 |
'#description' => t('The human-readable name of the test.'),
|
| 415 |
'#attributes' => array('class' => 'simpletest-automator-name'),
|
| 416 |
);
|
| 417 |
$form['description'] = array(
|
| 418 |
'#title' => t('Description'),
|
| 419 |
'#description' => t('The human-readable description of the test.'),
|
| 420 |
);
|
| 421 |
$form['test_group'] = array(
|
| 422 |
'#title' => t('Group'),
|
| 423 |
'#description' => t('The human-readable group of the test.'),
|
| 424 |
// TODO: Add auto-complete and make it hook into simpletest module if enabled.
|
| 425 |
);
|
| 426 |
$form['file'] = array(
|
| 427 |
'#title' => t('File'),
|
| 428 |
'#description' => t('The filename of the test.'),
|
| 429 |
'#attributes' => array('class' => 'simpletest-automator-file simpletest-automator-php'),
|
| 430 |
);
|
| 431 |
$form['class'] = array(
|
| 432 |
'#title' => t('Class'),
|
| 433 |
'#description' => t('The class to use in the test case.'),
|
| 434 |
'#attributes' => array('class' => 'simpletest-automator-class simpletest-automator-php'),
|
| 435 |
);
|
| 436 |
$form['method'] = array(
|
| 437 |
'#title' => t('Method'),
|
| 438 |
'#description' => t('The method to use in the test case.'),
|
| 439 |
'#attributes' => array('class' => 'simpletest-automator-method simpletest-automator-php'),
|
| 440 |
);
|
| 441 |
foreach ($form as $key => $element) {
|
| 442 |
if (isset($simpletest_automator->$key)) {
|
| 443 |
$form[$key]['#default_value'] = $simpletest_automator->$key;
|
| 444 |
}
|
| 445 |
$form[$key]['#required'] = TRUE;
|
| 446 |
$form[$key]['#type'] = 'textfield';
|
| 447 |
}
|
| 448 |
$form['submit'] = array(
|
| 449 |
'#type' => 'submit',
|
| 450 |
'#value' => t('Save'),
|
| 451 |
);
|
| 452 |
drupal_add_js(drupal_get_path('module', 'simpletest_automator') . '/simpletest_automator.admin.js');
|
| 453 |
return $form;
|
| 454 |
}
|
| 455 |
|
| 456 |
function simpletest_automator_admin_edit_submit($form, &$form_state) {
|
| 457 |
simpletest_automator_save((object)($form_state['values'] + (array) $form_state['storage']['simpletest_automator']));
|
| 458 |
drupal_set_message(t('SimpleTest saved.'));
|
| 459 |
unset($form_state['storage']);
|
| 460 |
}
|
| 461 |
|
| 462 |
/**
|
| 463 |
* Menu callback for the actions list page.
|
| 464 |
*/
|
| 465 |
function simpletest_automator_admin_action_list(&$form_state, $simpletest_automator) {
|
| 466 |
$form['#said'] = $simpletest_automator->said;
|
| 467 |
$form['actions'] = array('#tree' => TRUE);
|
| 468 |
foreach ($simpletest_automator->actions as $action) {
|
| 469 |
$form['actions'][$action->aid] = array();
|
| 470 |
$info = _simpletest_automator_action_info($action);
|
| 471 |
$form['actions'][$action->aid]['label']['#markup'] = $info['label'];
|
| 472 |
$form['actions'][$action->aid]['description']['#markup'] = $info['description'];
|
| 473 |
$form['actions'][$action->aid]['edit']['#markup'] = l(t('Edit'), "admin/build/simpletest_automator/$simpletest_automator->said/actions/$action->aid/edit");
|
| 474 |
$form['actions'][$action->aid]['delete']['#markup'] = l(t('Delete'), "admin/build/simpletest_automator/$simpletest_automator->said/actions/$action->aid/delete");
|
| 475 |
if (count($simpletest_automator->actions) > 1) {
|
| 476 |
$form['actions'][$action->aid]['weight'] = array(
|
| 477 |
'#type' => 'weight',
|
| 478 |
'#delta' => 20,
|
| 479 |
'#default_value' => $action->weight,
|
| 480 |
'#attributes' => array('class' => 'simpletest-automator-action-weight')
|
| 481 |
);
|
| 482 |
}
|
| 483 |
}
|
| 484 |
if (count($simpletest_automator->actions) > 1) {
|
| 485 |
$form['submit'] = array(
|
| 486 |
'#type' => 'submit',
|
| 487 |
'#value' => t('Save'),
|
| 488 |
);
|
| 489 |
}
|
| 490 |
return $form;
|
| 491 |
}
|
| 492 |
|
| 493 |
/**
|
| 494 |
* Submit handler for simpletest_automator_admin_action_list().
|
| 495 |
*/
|
| 496 |
function simpletest_automator_admin_action_list_submit($form, &$form_state) {
|
| 497 |
$simpletest_automator = simpletest_automator_load($form['#said'], TRUE);
|
| 498 |
foreach ($form_state['values']['actions'] as $aid => $data) {
|
| 499 |
$simpletest_automator->actions[$aid]->weight = $data['weight'];
|
| 500 |
}
|
| 501 |
simpletest_automator_save($simpletest_automator);
|
| 502 |
}
|
| 503 |
|
| 504 |
function theme_simpletest_automator_admin_action_list($form) {
|
| 505 |
$headers = array(t('Name'), array('data' => t('Operations'), 'colspan' => 2));
|
| 506 |
if (count(element_children($form['actions'])) > 1) {
|
| 507 |
$headers[] = t('Weight');
|
| 508 |
drupal_add_tabledrag('simpletest-automator-actions', 'order', 'sibling', 'simpletest-automator-action-weight');
|
| 509 |
}
|
| 510 |
$rows = array();
|
| 511 |
foreach (element_children($form['actions']) as $key) {
|
| 512 |
$row = array();
|
| 513 |
$row[] = drupal_render($form['actions'][$key]['label']) .'<br /><span class="description">'. drupal_render($form['actions'][$key]['description']) .'</span>';
|
| 514 |
$row[] = drupal_render($form['actions'][$key]['edit']);
|
| 515 |
$row[] = drupal_render($form['actions'][$key]['delete']);
|
| 516 |
if (count(element_children($form['actions'])) > 1) {
|
| 517 |
$row[] = drupal_render($form['actions'][$key]['weight']);
|
| 518 |
$rows[] = array('class' => 'draggable', 'data' => $row);
|
| 519 |
}
|
| 520 |
else {
|
| 521 |
$rows[] = $row;
|
| 522 |
}
|
| 523 |
}
|
| 524 |
if (empty($rows)) {
|
| 525 |
$rows[] = array(
|
| 526 |
array(
|
| 527 |
'data' => t('There are no actions yet associated with this SimpleTest automator instance. You can <a href="@url">add a new one</a>.', array('@url' => url('admin/build/simpletest_automator/' . $form['#said'] . '/actions/add'))),
|
| 528 |
'colspan' => 4,
|
| 529 |
),
|
| 530 |
);
|
| 531 |
}
|
| 532 |
unset($form['#theme']);
|
| 533 |
return theme('table', $headers, $rows, array('id' => 'simpletest-automator-actions')) . drupal_render($form);
|
| 534 |
}
|
| 535 |
|
| 536 |
/**
|
| 537 |
* FAPI calblack for the action add/edit page.
|
| 538 |
*/
|
| 539 |
function simpletest_automator_admin_action_edit($form_state, $simpletest_automator, $aid) {
|
| 540 |
if ($aid) {
|
| 541 |
if (!isset($simpletest_automator->actions[$aid])) {
|
| 542 |
return drupal_not_found();
|
| 543 |
}
|
| 544 |
$action = $simpletest_automator->actions[$aid];
|
| 545 |
}
|
| 546 |
else {
|
| 547 |
$action = (object) array('said' => $simpletest_automator->said, 'type' => '', 'parameters' => array(), 'weight' => 0);
|
| 548 |
}
|
| 549 |
$form = array(
|
| 550 |
'#said' => $simpletest_automator->said,
|
| 551 |
'#aid' => $aid,
|
| 552 |
'#action_weight' => $action->weight,
|
| 553 |
);
|
| 554 |
// We might need textarea javascript later on. @todo - find better way.
|
| 555 |
drupal_add_js('misc/textarea.js');
|
| 556 |
$form['type'] = array(
|
| 557 |
'#type' => 'select',
|
| 558 |
'#title' => t('Type'),
|
| 559 |
'#description' => $action->type ? t('This setting cannot be changed after the action has already been added. If you need to add an action of a different type, try adding a new one.') : t('Please choose what type of action you wish to add. Once you choose, more options may become available.'),
|
| 560 |
'#options' => array_merge(array('' => t('<none>')), module_invoke_all('simpletest_automator_actions', $action, 'list')),
|
| 561 |
'#default_value' => $action->type,
|
| 562 |
'#disabled' => (bool)$action->type,
|
| 563 |
'#ahah' => array(
|
| 564 |
'path' => 'simpletest_automator/admin/js',
|
| 565 |
'wrapper' => 'action-parameters',
|
| 566 |
),
|
| 567 |
'#suffix' => ($action->type) ? NULL : '<div id="action-parameters"> </div>',
|
| 568 |
'#required' => TRUE,
|
| 569 |
);
|
| 570 |
$form['parameters'] = array('#tree' => TRUE);
|
| 571 |
if ($action->type) {
|
| 572 |
$form['parameters'] += array('#prefix' => '<div id="action-parameters">', '#suffix' => '</div>');
|
| 573 |
}
|
| 574 |
$form['parameters'] += module_invoke_all('simpletest_automator_actions', $action, 'form');
|
| 575 |
$form['save'] = array(
|
| 576 |
'#type' => 'submit',
|
| 577 |
'#value' => t('Save'),
|
| 578 |
);
|
| 579 |
return $form;
|
| 580 |
}
|
| 581 |
|
| 582 |
/**
|
| 583 |
* Submit handler for simpletest_automator_admin_action_edit().
|
| 584 |
*/
|
| 585 |
function simpletest_automator_admin_action_edit_submit($form, &$form_state) {
|
| 586 |
$action = (object)array(
|
| 587 |
'aid' => $form['#aid'],
|
| 588 |
'said' => $form['#said'],
|
| 589 |
'type' => $form_state['values']['type'],
|
| 590 |
'weight' => $form['#action_weight'],
|
| 591 |
'parameters' => $form_state['values']['parameters'],
|
| 592 |
);
|
| 593 |
simpletest_automator_action_save($action);
|
| 594 |
$form_state['redirect'] = 'admin/build/simpletest_automator/' . $form['#said'] . '/actions';
|
| 595 |
}
|
| 596 |
|
| 597 |
function simpletest_automator_admin_action_delete($form_state, $simpletest_automator, $aid) {
|
| 598 |
if (!isset($simpletest_automator->actions[$aid])) {
|
| 599 |
return drupal_not_found();
|
| 600 |
}
|
| 601 |
$info = _simpletest_automator_action_info($simpletest_automator->actions[$aid]);
|
| 602 |
$form = array();
|
| 603 |
$form['aid'] = array(
|
| 604 |
'#type' => 'value',
|
| 605 |
'#value' => $aid,
|
| 606 |
);
|
| 607 |
$form['said'] = array(
|
| 608 |
'#type' => 'value',
|
| 609 |
'#value' => $simpletest_automator->said,
|
| 610 |
);
|
| 611 |
return confirm_form($form, t('Are you sure you want to delete %action', array('%action' => $info['label'])), 'admin/build/simpletest_automator/'. $simpletest_automator->said .'/actions');
|
| 612 |
}
|
| 613 |
|
| 614 |
function simpletest_automator_admin_record($form_state, $simpletest_automator) {
|
| 615 |
$form = array();
|
| 616 |
|
| 617 |
$form['said'] = array(
|
| 618 |
'#type' => 'value',
|
| 619 |
'#value' => $simpletest_automator->said,
|
| 620 |
);
|
| 621 |
|
| 622 |
return confirm_form($form, t('Are you sure you want to enter record mode?'), 'admin/build/simpletest_automator', t('Once in record mode, you can exit by clicking the "Stop recording" button at the top of the page.'), t('Record'), t('Cancel'));
|
| 623 |
}
|
| 624 |
|
| 625 |
function simpletest_automator_admin_record_submit($form, &$form_state) {
|
| 626 |
global $db_prefix;
|
| 627 |
$simpletest_automator = simpletest_automator_load($form_state['values']['said'], TRUE);
|
| 628 |
$db_prefix = $simpletest_automator->db_prefix;
|
| 629 |
setcookie('simpletest_automator', $simpletest_automator->db_prefix, 0, base_path());
|
| 630 |
// Create new role.
|
| 631 |
db_query("INSERT INTO {role} (name) VALUES ('%s')", _simpletest_automator_random_name());
|
| 632 |
$rid = db_last_insert_id('role', 'rid');
|
| 633 |
foreach ($simpletest_automator->permissions as $permission) {
|
| 634 |
db_query("INSERT INTO {role_permission} (rid, permission) VALUES (%d, '%s')", $rid, $permission);
|
| 635 |
}
|
| 636 |
$edit = array();
|
| 637 |
$edit['name'] = _simpletest_automator_random_name();
|
| 638 |
$edit['mail'] = $edit['name'] . '@example.com';
|
| 639 |
$edit['roles'] = array($rid => $rid);
|
| 640 |
$edit['pass'] = user_password();
|
| 641 |
$edit['status'] = 1;
|
| 642 |
$account = user_save('', $edit);
|
| 643 |
drupal_function_exists('user_logout');
|
| 644 |
$GLOBALS['user'] = $account;
|
| 645 |
|
| 646 |
drupal_goto('<front>');
|
| 647 |
}
|
| 648 |
|
| 649 |
/**
|
| 650 |
* Helper function for simpletest_automator_admin_record_submit().
|
| 651 |
*/
|
| 652 |
function _simpletest_automator_random_name($number = 4, $prefix = 'simpletest_') {
|
| 653 |
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_';
|
| 654 |
for ($x = 0; $x < $number; $x++) {
|
| 655 |
$prefix .= $chars{mt_rand(0, strlen($chars) - 1)};
|
| 656 |
if ($x == 0) {
|
| 657 |
$chars .= '0123456789';
|
| 658 |
}
|
| 659 |
}
|
| 660 |
return $prefix;
|
| 661 |
}
|
| 662 |
|
| 663 |
function simpletest_automator_admin_export($simpletest_automator, $type = NULL) {
|
| 664 |
if (empty($type)) {
|
| 665 |
$items = array(
|
| 666 |
l(t('File'), 'admin/build/simpletest_automator/' . $simpletest_automator->said . '/export/file') . '<div class="description">' . t('Select this option to export your test to a .test file.') . '</div>',
|
| 667 |
l(t('For import'), 'admin/build/simpletest_automator/' . $simpletest_automator->said . '/export/import') . '<div class="description">' . t('Select this option to export your test in a format that can be re-imported in the "import" tab.') . '</div>',
|
| 668 |
);
|
| 669 |
return theme('item_list', $items);
|
| 670 |
}
|
| 671 |
else {
|
| 672 |
switch ($type) {
|
| 673 |
case 'file':
|
| 674 |
return '<iframe src="'. url('simpletest_automator/export/' . $simpletest_automator->said) . '" width="1" height="1" style="border:0"></iframe>'. t('Your download should begin immediately. If it doesn\'t, click <a href="!url">here</a>.', array('!url' => url('simpletest_automator/export/' . $simpletest_automator->said)));
|
| 675 |
|
| 676 |
case 'import':
|
| 677 |
return drupal_get_form('simpletest_automator_export_php', $simpletest_automator);
|
| 678 |
|
| 679 |
default:
|
| 680 |
return drupal_not_found();
|
| 681 |
|
| 682 |
}
|
| 683 |
}
|
| 684 |
return $form;
|
| 685 |
}
|
| 686 |
|
| 687 |
/**
|
| 688 |
* Submission callback for simpletest_automator_admin_action_delete().
|
| 689 |
*/
|
| 690 |
function simpletest_automator_admin_action_delete_submit($form, &$form_state) {
|
| 691 |
simpletest_automator_action_delete($form_state['values']['aid']);
|
| 692 |
drupal_set_message(t('The action has been deleted.'));
|
| 693 |
$form_state['redirect'] = 'admin/build/simpletest_automator/' . $form_state['values']['said'] . '/actions';
|
| 694 |
}
|
| 695 |
|
| 696 |
/**
|
| 697 |
* AHAH callback upon the change of form type.
|
| 698 |
*/
|
| 699 |
function simpletest_automator_admin_actions_js() {
|
| 700 |
// Add the new element to the stored form state. Without adding the element
|
| 701 |
// to the form, Drupal is not aware of this new elements existence and will
|
| 702 |
// not process it. We retreive the cached form, add the element, and resave.
|
| 703 |
$form_build_id = $_POST['form_build_id'];
|
| 704 |
$type = $_POST['type'];
|
| 705 |
$form_state = array('submitted' => FALSE);
|
| 706 |
$form = form_get_cache($form_build_id, $form_state);
|
| 707 |
|
| 708 |
if ($form) {
|
| 709 |
// Validate the type.
|
| 710 |
if (isset($form['type']['#options'][$type])) {
|
| 711 |
$action = (object) array('said' => $form['#said'], 'type' => $type, 'parameters' => array());
|
| 712 |
$form['parameters'] = array('#tree' => TRUE);
|
| 713 |
$form['parameters'] += module_invoke_all('simpletest_automator_actions', $action, 'form');
|
| 714 |
form_set_cache($form_build_id, $form, $form_state);
|
| 715 |
// Build and render the new elements, then return them in JSON format.
|
| 716 |
$form_state = array();
|
| 717 |
$form['#post'] = array();
|
| 718 |
$form = form_builder($form['form_id']['#value'] , $form, $form_state);
|
| 719 |
$output = drupal_render($form['parameters']);
|
| 720 |
drupal_json(array('status' => TRUE, 'data' => $output));
|
| 721 |
}
|
| 722 |
else {
|
| 723 |
drupal_json(array('status' => FALSE, 'data' => ''));
|
| 724 |
}
|
| 725 |
}
|
| 726 |
else {
|
| 727 |
drupal_json(array('status' => FALSE, 'data' => ''));
|
| 728 |
}
|
| 729 |
}
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
function theme_simpletest_automator_admin_modules($form) {
|
| 734 |
// Individual table rows.
|
| 735 |
$rows = array();
|
| 736 |
// Iterate through all the modules, which are
|
| 737 |
// children of this fieldset.
|
| 738 |
foreach (element_children($form) as $key) {
|
| 739 |
// Stick it into $module for easier accessing.
|
| 740 |
$module = $form[$key];
|
| 741 |
$row = array();
|
| 742 |
unset($module['enable']['#title']);
|
| 743 |
$row[] = array('class' => 'checkbox', 'data' => drupal_render($module['enable']));
|
| 744 |
$row[] = '<strong>'. drupal_render($module['name']) .'</strong>';
|
| 745 |
$row[] = drupal_render($module['version']);
|
| 746 |
$description = '';
|
| 747 |
// Add the description, along with any dependencies.
|
| 748 |
$description .= drupal_render($module['description']);
|
| 749 |
if ($module['#dependencies']) {
|
| 750 |
$description .= '<div class="admin-dependencies">' . t('Depends on: ') . implode(', ', $module['#dependencies']) . '</div>';
|
| 751 |
}
|
| 752 |
if ($module['#dependents']) {
|
| 753 |
$description .= '<div class="admin-dependencies">' . t('Required by: ') . implode(', ', $module['#dependents']) . '</div>';
|
| 754 |
}
|
| 755 |
$row[] = array('data' => $description, 'class' => 'description');
|
| 756 |
$rows[] = $row;
|
| 757 |
}
|
| 758 |
return theme('table', $form['#header'], $rows);
|
| 759 |
}
|
| 760 |
|
| 761 |
/**
|
| 762 |
* Themes an incompatible message.
|
| 763 |
*
|
| 764 |
* @ingroup themeable
|
| 765 |
* @param $message
|
| 766 |
* The form array representing the currently disabled modules.
|
| 767 |
* @return
|
| 768 |
* An HTML string for the message.
|
| 769 |
*/
|
| 770 |
function theme_simpletest_automator_admin_modules_incompatible($message) {
|
| 771 |
return '<div class="incompatible">'. $message .'</div>';
|
| 772 |
}
|
| 773 |
|
| 774 |
/**
|
| 775 |
* Theme function.
|
| 776 |
*/
|
| 777 |
function theme_simpletest_automator_admin_permissions($form) {
|
| 778 |
drupal_add_css(drupal_get_path('module', 'user') . '/user.css');
|
| 779 |
$submit = $form['submit'];
|
| 780 |
$permissions = $form['permissions'];
|
| 781 |
unset($form['submit'], $form['permissions'], $form['#theme']);
|
| 782 |
$output = drupal_render($form);
|
| 783 |
$roles = user_roles();
|
| 784 |
foreach (element_children($permissions) as $module) {
|
| 785 |
$rows[] = array(array('data' => t('@module module', array('@module' => $module)), 'class' => 'module', 'id' => 'module-'. $module, 'colspan' => 2));
|
| 786 |
foreach (element_children($permissions[$module]) as $permission) {
|
| 787 |
$rows[] = array(
|
| 788 |
array('data' => drupal_render($permissions[$module][$permission]['name']), 'class' => 'permission'),
|
| 789 |
array('data' => drupal_render($permissions[$module][$permission]['enable']), 'class' => 'checkbox'),
|
| 790 |
);
|
| 791 |
}
|
| 792 |
}
|
| 793 |
$header[] = t('Permission');
|
| 794 |
$header[] = array('data' => t('Test user'), 'class' => 'checkbox');
|
| 795 |
$output .= theme('table', $header, $rows, array('id' => 'permissions'));
|
| 796 |
$output .= drupal_render($submit);
|
| 797 |
return $output;
|
| 798 |
}
|