| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
define('SIMPLETEST_AUTOMATOR_ON', 0);
|
| 5 |
define('SIMPLETEST_AUTOMATOR_FIRST_PAGE', 1);
|
| 6 |
define('SIMPLETEST_AUTOMATOR_RUNNING', 2);
|
| 7 |
|
| 8 |
/**
|
| 9 |
* API function: load a simpletest automator instance.
|
| 10 |
*
|
| 11 |
* @param $id
|
| 12 |
* The ID of the simpletest automator instance to load.
|
| 13 |
* @param $load_actions
|
| 14 |
* If TRUE, loads the simpletest automator actions as well. Defaults to FALSE.
|
| 15 |
* @param $refresh
|
| 16 |
* If TRUE, refreshes the internal static cache. Defaults to FALSE.
|
| 17 |
* @return
|
| 18 |
* A fully loaded simpletest automator object.
|
| 19 |
*/
|
| 20 |
function simpletest_automator_load($id, $load_actions = FALSE, $refresh = FALSE) {
|
| 21 |
static $cache;
|
| 22 |
$id = (int)$id;
|
| 23 |
if ($refresh || !isset($cache)) {
|
| 24 |
$cache = array();
|
| 25 |
}
|
| 26 |
if (!isset($cache[$id])) {
|
| 27 |
$result = db_query('SELECT * FROM {simpletest_automator} WHERE said = %d', $id);
|
| 28 |
if ($simpletest_automator = db_fetch_object($result)) {
|
| 29 |
$simpletest_automator->modules = unserialize($simpletest_automator->modules);
|
| 30 |
$simpletest_automator->permissions = unserialize($simpletest_automator->permissions);
|
| 31 |
$cache[$id] = $simpletest_automator;
|
| 32 |
}
|
| 33 |
else {
|
| 34 |
$cache[$id] = FALSE;
|
| 35 |
}
|
| 36 |
}
|
| 37 |
$simpletest_automator = $cache[$id];
|
| 38 |
if ($load_actions) {
|
| 39 |
$simpletest_automator->actions = simpletest_automator_action_load(NULL, $id);
|
| 40 |
}
|
| 41 |
return $simpletest_automator;
|
| 42 |
}
|
| 43 |
|
| 44 |
/**
|
| 45 |
* API function: save a simpletest automator instance.
|
| 46 |
*
|
| 47 |
* @param $data
|
| 48 |
* The data to save.
|
| 49 |
* @return
|
| 50 |
* The new id.
|
| 51 |
*/
|
| 52 |
function simpletest_automator_save($data) {
|
| 53 |
unset($data->actions);
|
| 54 |
$data->modules = serialize($data->modules);
|
| 55 |
$data->permissions = serialize($data->permissions);
|
| 56 |
if (isset($data->said)) {
|
| 57 |
db_update('simpletest_automator')->fields((array)$data)->condition('said', $data->said)->execute();
|
| 58 |
}
|
| 59 |
else {
|
| 60 |
$data->said = db_insert('simpletest_automator')->fields((array)$data)->execute();
|
| 61 |
}
|
| 62 |
return $data->said;
|
| 63 |
}
|
| 64 |
|
| 65 |
/**
|
| 66 |
* API function: delete a simpletest automator instance.
|
| 67 |
*
|
| 68 |
* @param $said
|
| 69 |
* The simpletest automator id of the simpletest automator instance to clear.
|
| 70 |
* @return
|
| 71 |
* TRUE.
|
| 72 |
*/
|
| 73 |
function simpletest_automator_delete($said) {
|
| 74 |
db_query('DELETE FROM {simpletest_automator} WHERE said = %d', $said);
|
| 75 |
db_query('DELETE FROM {simpletest_automator_actions} WHERE said = %d', $said);
|
| 76 |
return TRUE;
|
| 77 |
}
|
| 78 |
|
| 79 |
/**
|
| 80 |
* API function: load a simpletest automator action.
|
| 81 |
*
|
| 82 |
* @param $aid
|
| 83 |
* The action id of the action to load, or NULL to load all actions.
|
| 84 |
* @param $said
|
| 85 |
* The id of the simpletest automator instance to which the actions belong.
|
| 86 |
* @param $refresh
|
| 87 |
* If TRUE, refreshes the internal static cache. Defaults to FALSE.
|
| 88 |
* @return
|
| 89 |
* The fully loaded simpletest automator action, or array of actions if $aid
|
| 90 |
* was set to NULL.
|
| 91 |
*/
|
| 92 |
function simpletest_automator_action_load($aid = NULL, $said = NULL, $refresh = FALSE) {
|
| 93 |
static $aid_cache = array();
|
| 94 |
static $said_cache = array();
|
| 95 |
if ((!is_null($aid) && !isset($aid_cache[$aid])) || (!is_null($said) && !isset($said_cache[$said]))) {
|
| 96 |
$where = array();
|
| 97 |
$arguments = array();
|
| 98 |
if (!is_null($aid)) {
|
| 99 |
$aid_cache[$aid] = array();
|
| 100 |
$where[] = 'aid = %d';
|
| 101 |
$arguments[] = $aid;
|
| 102 |
}
|
| 103 |
if (!is_null($said)) {
|
| 104 |
$said_cache[$said] = array();
|
| 105 |
$where[] = 'said = %d';
|
| 106 |
$arguments[] = $said;
|
| 107 |
}
|
| 108 |
if (!empty($where)) {
|
| 109 |
$result = db_query('SELECT * FROM {simpletest_automator_actions} WHERE '. implode($where, ' AND ') .' ORDER BY weight ASC, aid ASC', $arguments);
|
| 110 |
}
|
| 111 |
else {
|
| 112 |
$said_cache[0] = array();
|
| 113 |
$result = db_query('SELECT * FROM {simpletest_automator_actions}');
|
| 114 |
}
|
| 115 |
while ($action = db_fetch_object($result)) {
|
| 116 |
$action->parameters = unserialize($action->parameters);
|
| 117 |
$aid_cache[$action->aid] = $action;
|
| 118 |
if (!is_null($said)) {
|
| 119 |
$said_cache[$said][$action->aid] = $action;
|
| 120 |
}
|
| 121 |
if (empty($where)) {
|
| 122 |
$said_cache[0][$action->aid] = $action;
|
| 123 |
}
|
| 124 |
}
|
| 125 |
}
|
| 126 |
if (!is_null($aid)) {
|
| 127 |
return $aid_cache[$aid];
|
| 128 |
}
|
| 129 |
if (!is_null($said)) {
|
| 130 |
return $said_cache[$said];
|
| 131 |
}
|
| 132 |
return $said_cache[0];
|
| 133 |
}
|
| 134 |
|
| 135 |
/**
|
| 136 |
* API function: save a simpletest automator action.
|
| 137 |
*
|
| 138 |
* @param $action
|
| 139 |
* The simpletest automator action to save.
|
| 140 |
* @return
|
| 141 |
* The action id of the saved action.
|
| 142 |
*/
|
| 143 |
function simpletest_automator_action_save($action) {
|
| 144 |
$action->parameters = serialize($action->parameters);
|
| 145 |
if (isset($action->aid)) {
|
| 146 |
drupal_write_record('simpletest_automator_actions', $action, 'aid');
|
| 147 |
}
|
| 148 |
else {
|
| 149 |
drupal_write_record('simpletest_automator_actions', $action);
|
| 150 |
}
|
| 151 |
return $action->aid;
|
| 152 |
}
|
| 153 |
|
| 154 |
/**
|
| 155 |
* API function: delete a simpletest automator action.
|
| 156 |
*
|
| 157 |
* @param $aid
|
| 158 |
* The action id of the action to delete.
|
| 159 |
* @return
|
| 160 |
* TRUE.
|
| 161 |
*/
|
| 162 |
function simpletest_automator_action_delete($aid) {
|
| 163 |
db_query('DELETE FROM {simpletest_automator_actions} WHERE aid = %d', $aid);
|
| 164 |
return TRUE;
|
| 165 |
}
|
| 166 |
|
| 167 |
/**
|
| 168 |
* Implementation of hook_menu().
|
| 169 |
*/
|
| 170 |
function simpletest_automator_menu() {
|
| 171 |
$items['admin/build/simpletest_automator'] = array(
|
| 172 |
'title' => 'SimpleTest Automator',
|
| 173 |
'description' => 'Automate your testing process using the SimpleTest automator, to record your manual testing and create automated tests based off it.',
|
| 174 |
'page callback' => 'simpletest_automator_admin_overview',
|
| 175 |
'type' => MENU_NORMAL_ITEM,
|
| 176 |
);
|
| 177 |
|
| 178 |
$items['admin/build/simpletest_automator/list'] = array(
|
| 179 |
'title' => 'List',
|
| 180 |
'page callback' => 'simpletest_automator_admin_overview',
|
| 181 |
'weight' => 1,
|
| 182 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 183 |
);
|
| 184 |
$items['admin/build/simpletest_automator/add'] = array(
|
| 185 |
'title' => 'Add',
|
| 186 |
'page arguments' => array('simpletest_automator_admin_add'),
|
| 187 |
'weight' => 2,
|
| 188 |
'type' => MENU_LOCAL_TASK,
|
| 189 |
);
|
| 190 |
$items['admin/build/simpletest_automator/import'] = array(
|
| 191 |
'title' => 'Import',
|
| 192 |
'page arguments' => array('simpletest_automator_admin_import'),
|
| 193 |
'weight' => 2,
|
| 194 |
'type' => MENU_LOCAL_TASK,
|
| 195 |
);
|
| 196 |
|
| 197 |
$items['admin/build/simpletest_automator/%simpletest_automator'] = array(
|
| 198 |
'title callback' => 'simpletest_automator_page_title',
|
| 199 |
'title arguments' => array(3),
|
| 200 |
'page arguments' => array('simpletest_automator_admin_edit', 3),
|
| 201 |
'type' => MENU_CALLBACK,
|
| 202 |
);
|
| 203 |
|
| 204 |
$items['admin/build/simpletest_automator/%simpletest_automator/edit'] = array(
|
| 205 |
'title' => 'Edit',
|
| 206 |
'weight' => 1,
|
| 207 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 208 |
);
|
| 209 |
$items['admin/build/simpletest_automator/%simpletest_automator/modules'] = array(
|
| 210 |
'title' => 'Modules',
|
| 211 |
'page arguments' => array('simpletest_automator_admin_modules_form', 3),
|
| 212 |
'weight' => 2,
|
| 213 |
'type' => MENU_LOCAL_TASK,
|
| 214 |
);
|
| 215 |
$items['admin/build/simpletest_automator/%simpletest_automator/permissions'] = array(
|
| 216 |
'title' => 'Permissions',
|
| 217 |
'page arguments' => array('simpletest_automator_admin_permissions', 3),
|
| 218 |
'weight' => 3,
|
| 219 |
'type' => MENU_LOCAL_TASK,
|
| 220 |
);
|
| 221 |
$items['admin/build/simpletest_automator/%simpletest_automator/actions'] = array(
|
| 222 |
'title' => 'Actions',
|
| 223 |
'page arguments' => array('simpletest_automator_admin_action_list', 3),
|
| 224 |
'weight' => 4,
|
| 225 |
'type' => MENU_LOCAL_TASK,
|
| 226 |
'load arguments' => array(TRUE),
|
| 227 |
);
|
| 228 |
$items['admin/build/simpletest_automator/%simpletest_automator/actions/list'] = array(
|
| 229 |
'title' => 'List',
|
| 230 |
'weight' => 1,
|
| 231 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 232 |
);
|
| 233 |
$items['admin/build/simpletest_automator/%simpletest_automator/actions/%'] = array(
|
| 234 |
'title' => 'Action',
|
| 235 |
'page arguments' => array('simpletest_automator_admin_action_edit', 3, 5),
|
| 236 |
'weight' => 4,
|
| 237 |
'type' => MENU_CALLBACK,
|
| 238 |
'load arguments' => array(TRUE),
|
| 239 |
);
|
| 240 |
$items['admin/build/simpletest_automator/%simpletest_automator/actions/%/edit'] = array(
|
| 241 |
'title' => 'Edit',
|
| 242 |
'page arguments' => array('simpletest_automator_admin_action_edit', 3, 5),
|
| 243 |
'load arguments' => array(TRUE),
|
| 244 |
'weight' => 1,
|
| 245 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 246 |
);
|
| 247 |
$items['admin/build/simpletest_automator/%simpletest_automator/actions/%/delete'] = array(
|
| 248 |
'title' => 'Delete',
|
| 249 |
'page arguments' => array('simpletest_automator_admin_action_delete', 3, 5),
|
| 250 |
'weight' => 2,
|
| 251 |
'type' => MENU_LOCAL_TASK,
|
| 252 |
'load arguments' => array(TRUE),
|
| 253 |
);
|
| 254 |
$items['admin/build/simpletest_automator/%simpletest_automator/actions/add'] = array(
|
| 255 |
'title' => 'Add',
|
| 256 |
'page arguments' => array('simpletest_automator_admin_action_edit', 3, NULL),
|
| 257 |
'weight' => 2,
|
| 258 |
'type' => MENU_LOCAL_TASK,
|
| 259 |
'load arguments' => array(TRUE),
|
| 260 |
);
|
| 261 |
$items['admin/build/simpletest_automator/%simpletest_automator/export'] = array(
|
| 262 |
'title' => 'Export',
|
| 263 |
'page callback' => 'simpletest_automator_admin_export',
|
| 264 |
'page arguments' => array(3),
|
| 265 |
'weight' => 4,
|
| 266 |
'load arguments' => array(TRUE),
|
| 267 |
'type' => MENU_LOCAL_TASK,
|
| 268 |
);
|
| 269 |
$items['admin/build/simpletest_automator/%simpletest_automator/record'] = array(
|
| 270 |
'title' => 'Record',
|
| 271 |
'page arguments' => array('simpletest_automator_admin_record', 3),
|
| 272 |
'weight' => 5,
|
| 273 |
'load arguments' => array(TRUE),
|
| 274 |
'type' => MENU_LOCAL_TASK,
|
| 275 |
);
|
| 276 |
$items['admin/build/simpletest_automator/%simpletest_automator/delete'] = array(
|
| 277 |
'title' => 'Delete',
|
| 278 |
'page arguments' => array('simpletest_automator_admin_delete', 3),
|
| 279 |
'weight' => 6,
|
| 280 |
'type' => MENU_LOCAL_TASK,
|
| 281 |
'load arguments' => array(TRUE),
|
| 282 |
);
|
| 283 |
|
| 284 |
// Javascript callbacks.
|
| 285 |
$items['simpletest_automator/admin/js'] = array(
|
| 286 |
'page callback' => 'simpletest_automator_admin_actions_js',
|
| 287 |
'type' => MENU_CALLBACK,
|
| 288 |
);
|
| 289 |
$items['simpletest_automator/export/%simpletest_automator'] = array(
|
| 290 |
'page callback' => 'simpletest_automator_export_test',
|
| 291 |
'page arguments' => array(2),
|
| 292 |
'weight' => 2,
|
| 293 |
'type' => MENU_LOCAL_TASK,
|
| 294 |
'load arguments' => array(TRUE),
|
| 295 |
);
|
| 296 |
|
| 297 |
foreach ($items as $path => $item) {
|
| 298 |
$item['access arguments'] = array('administer unit tests');
|
| 299 |
if (!isset($item['page callback'])) {
|
| 300 |
$item['page callback'] = 'drupal_get_form';
|
| 301 |
}
|
| 302 |
$items[$path] = $item;
|
| 303 |
}
|
| 304 |
|
| 305 |
return $items;
|
| 306 |
}
|
| 307 |
|
| 308 |
function simpletest_automator_page_title($simpletest_automator) {
|
| 309 |
return $simpletest_automator->name;
|
| 310 |
}
|
| 311 |
|
| 312 |
/**
|
| 313 |
* Implementation of hook_theme().
|
| 314 |
*/
|
| 315 |
function simpletest_automator_theme() {
|
| 316 |
return array(
|
| 317 |
'simpletest_automator_admin_modules' => array('arguments' => array('form' => NULL)),
|
| 318 |
'simpletest_automator_admin_modules_incompatible' => array('arguments' => array()),
|
| 319 |
'simpletest_automator_admin_permissions' => array('arguments' => array('form' => NULL)),
|
| 320 |
'simpletest_automator_admin_action_list' => array('arguments' => array('form' => NULL)),
|
| 321 |
);
|
| 322 |
}
|
| 323 |
|
| 324 |
/**
|
| 325 |
* Get some information about an action.
|
| 326 |
*/
|
| 327 |
function _simpletest_automator_action_info($action = NULL) {
|
| 328 |
if (!isset($action) || is_null($action)) {
|
| 329 |
return array();
|
| 330 |
}
|
| 331 |
static $map;
|
| 332 |
if (!isset($map)) {
|
| 333 |
$map = module_invoke_all('simpletest_automator_actions', NULL, 'list');
|
| 334 |
}
|
| 335 |
$info = array();
|
| 336 |
$info['label'] = $map[$action->type];
|
| 337 |
$info['description'] = implode(' ', module_invoke_all('simpletest_automator_actions', $action, 'description'));
|
| 338 |
return $info;
|
| 339 |
}
|
| 340 |
|
| 341 |
/**
|
| 342 |
* Implementation of hook_simpletest_automator_actions().
|
| 343 |
*/
|
| 344 |
function simpletest_automator_simpletest_automator_actions($action, $op) {
|
| 345 |
switch ($op) {
|
| 346 |
case 'list':
|
| 347 |
// We need to return an array of the action types that can be added.
|
| 348 |
// The keys should be the internal action names, while the values will be
|
| 349 |
// presented to the user.
|
| 350 |
return array(
|
| 351 |
'assert_text' => t('Make sure text appears'),
|
| 352 |
'click_link' => t('Click on a link'),
|
| 353 |
'drupal_get' => t('Make an HTTP GET request'),
|
| 354 |
'drupal_post' => t('Make an HTTP POST request'),
|
| 355 |
);
|
| 356 |
|
| 357 |
case 'description':
|
| 358 |
// Delegate the description to a helper function.
|
| 359 |
// @see _simpletest_automator_describe_action().
|
| 360 |
return _simpletest_automator_describe_action($action);
|
| 361 |
|
| 362 |
case 'export':
|
| 363 |
// Delegate the exporting to a helper function.
|
| 364 |
// @see _simpletest_automator_export_action().
|
| 365 |
return _simpletest_automator_export_action($action);
|
| 366 |
|
| 367 |
case 'form':
|
| 368 |
// Delegate the exporting to a helper function.
|
| 369 |
// @see _simpletest_automator_action_form().
|
| 370 |
return _simpletest_automator_action_form($action);
|
| 371 |
}
|
| 372 |
}
|
| 373 |
|
| 374 |
/**
|
| 375 |
* Helper function for simpletest_automator_simpletest_automator_actions().
|
| 376 |
* Returns the description for any of the core simpletest automator actions.
|
| 377 |
*
|
| 378 |
* @param $action
|
| 379 |
* The action to return the description of.
|
| 380 |
* @return
|
| 381 |
* An intelligent, 1-2 line description of the action.
|
| 382 |
*/
|
| 383 |
function _simpletest_automator_describe_action($action) {
|
| 384 |
switch ($action->type) {
|
| 385 |
case 'click_link':
|
| 386 |
return t('This will scan the current page for links with the label %label, and simulate a "click" on the #@index link.', array('%label' => $action->parameters['label'], '@index' => $action->parameters['index'] + 1));
|
| 387 |
|
| 388 |
case 'assert_text':
|
| 389 |
if ($action->parameters['should_appear']) {
|
| 390 |
return t('This will look at all the text on the current page, and make sure that the text %text appears.', array('%text' => $action->parameters['text']));
|
| 391 |
}
|
| 392 |
else {
|
| 393 |
return t('This will look at all the text on the current page, and make sure that the text %text does <strong>not</strong> appear.', array('%text' => $action->parameters['text']));
|
| 394 |
}
|
| 395 |
|
| 396 |
case 'drupal_get':
|
| 397 |
return t('This will make a GET request to the %page page.', array('%page' => $action->parameters['url']));
|
| 398 |
|
| 399 |
case 'drupal_post':
|
| 400 |
return t('This will submit the form at the @page page.', array('@page' => $action->parameters['url']));
|
| 401 |
}
|
| 402 |
}
|
| 403 |
|
| 404 |
/**
|
| 405 |
* Helper function for simpletest_automator_simpletest_automator_actions().
|
| 406 |
* Return an array of lines of code for the specified action.
|
| 407 |
*
|
| 408 |
* @param $action
|
| 409 |
* The action to return the export of.
|
| 410 |
* @return
|
| 411 |
* An array of lines of code to be added to the export.
|
| 412 |
*/
|
| 413 |
function _simpletest_automator_export_action($action) {
|
| 414 |
$lines = array();
|
| 415 |
switch ($action->type) {
|
| 416 |
case 'click_link':
|
| 417 |
// All code comments should be in English, so don't use t().
|
| 418 |
$lines[] = '// Click the link on the page in order to simulate browsing the site.';
|
| 419 |
if ($action->parameters['index']) {
|
| 420 |
$lines[] = array(
|
| 421 |
'value' => '$this->clickLink(t(%s), %d)',
|
| 422 |
'arguments' => array($action->parameters['label'], $action->parameters['index']),
|
| 423 |
);
|
| 424 |
}
|
| 425 |
else {
|
| 426 |
$lines[] = array(
|
| 427 |
'value' => '$this->clickLink(t(%s));',
|
| 428 |
'arguments' => array($action->parameters['label']),
|
| 429 |
);
|
| 430 |
}
|
| 431 |
break;
|
| 432 |
|
| 433 |
case 'assert_text':
|
| 434 |
$assert = 'assert';
|
| 435 |
if (!$action->parameters['should_appear']) {
|
| 436 |
$assert .= 'No';
|
| 437 |
}
|
| 438 |
$assert .= $action->parameters['html'] ? 'Raw' : 'Text';
|
| 439 |
$lines[] = array(
|
| 440 |
'value' => '$this->' . $assert . '(%s);',
|
| 441 |
'arguments' => array($action->parameters['text']),
|
| 442 |
);
|
| 443 |
break;
|
| 444 |
|
| 445 |
case 'drupal_get':
|
| 446 |
$lines[] = array(
|
| 447 |
'value' => '$this->drupalGet(%s);',
|
| 448 |
'arguments' => array($action->parameters['url']),
|
| 449 |
);
|
| 450 |
break;
|
| 451 |
|
| 452 |
case 'drupal_post':
|
| 453 |
$lines[] = '$edit = array();';
|
| 454 |
foreach ($action->parameters['post'] as $name => $value) {
|
| 455 |
$lines[] = array(
|
| 456 |
'value' => '$edit[%s] = %s;',
|
| 457 |
'arguments' => array($name, $value),
|
| 458 |
);
|
| 459 |
}
|
| 460 |
$lines[] = array(
|
| 461 |
'value' => '$this->drupalPost(%s, $edit, t(%s));',
|
| 462 |
'arguments' => array($action->parameters['url'], $action->parameters['operation']),
|
| 463 |
);
|
| 464 |
break;
|
| 465 |
|
| 466 |
}
|
| 467 |
return $lines;
|
| 468 |
}
|
| 469 |
|
| 470 |
/**
|
| 471 |
* Helper function for simpletest_automator_simpletest_automator_actions().
|
| 472 |
* Returns the settings form for any of the core simpletest automator actions.
|
| 473 |
*
|
| 474 |
* @param $action
|
| 475 |
* The action to return the settings form of.
|
| 476 |
* @return
|
| 477 |
* A forms API array of form items to add to the form.
|
| 478 |
*/
|
| 479 |
function _simpletest_automator_action_form($action) {
|
| 480 |
$form = array();
|
| 481 |
switch ($action->type) {
|
| 482 |
case 'click_link':
|
| 483 |
$form['label'] = array(
|
| 484 |
'#type' => 'textfield',
|
| 485 |
'#title' => t('Label'),
|
| 486 |
'#description' => t('The label of the link to click'),
|
| 487 |
'#required' => TRUE,
|
| 488 |
'#default_value' => isset($action->parameters['label']) ? $action->parameters['label'] : '',
|
| 489 |
);
|
| 490 |
$form['index'] = array(
|
| 491 |
'#type' => 'select',
|
| 492 |
'#title' => t('Index'),
|
| 493 |
'#description' => t('Select the Nth link to click - for example, if "Edit" appears as a link on the page twice, choose "0" to click the first one, or "1" to click the second one.'),
|
| 494 |
'#default_value' => isset($action->parameters['index']) ? $action->parameters['index'] : 0,
|
| 495 |
'#options' => drupal_map_assoc(range(0, 10)),
|
| 496 |
);
|
| 497 |
break;
|
| 498 |
|
| 499 |
case 'assert_text':
|
| 500 |
$form['text'] = array(
|
| 501 |
'#type' => 'textarea',
|
| 502 |
'#title' => t('Text'),
|
| 503 |
'#description' => t('The text to check for. This should not contain any HTML unless the "Text is HTML" option is selected below.'),
|
| 504 |
'#required' => TRUE,
|
| 505 |
'#default_value' => isset($action->parameters['text']) ? $action->parameters['text'] : '',
|
| 506 |
);
|
| 507 |
|
| 508 |
$form['should_appear'] = array(
|
| 509 |
'#type' => 'checkbox',
|
| 510 |
'#title' => t('Text should appear'),
|
| 511 |
'#description' => t('If unchecked, it will assert that the text does <strong>not</strong> appear, rather than asserting that the text <strong>should</strong> appear.'),
|
| 512 |
'#default_value' => isset($action->parameters['should_appear']) ? $action->parameters['should_appear'] : TRUE,
|
| 513 |
);
|
| 514 |
|
| 515 |
$form['html'] = array(
|
| 516 |
'#type' => 'checkbox',
|
| 517 |
'#title' => t('Text is HTML'),
|
| 518 |
'#description' => t('If checked, it will cause the code to check whether the raw HTML matches, rather than simply the visible text.'),
|
| 519 |
'#default_value' => isset($action->parameters['should_appear']) ? $action->parameters['should_appear'] : FALSE,
|
| 520 |
);
|
| 521 |
break;
|
| 522 |
|
| 523 |
case 'drupal_get':
|
| 524 |
$form['url'] = array(
|
| 525 |
'#type' => 'textfield',
|
| 526 |
'#title' => t('URL'),
|
| 527 |
'#required' => TRUE,
|
| 528 |
'#description' => t('Enter the URL to make a GET request to. Should be relative to the drupal install, without the ?q=.'),
|
| 529 |
'#default_value' => isset($action->parameters['url']) ? $action->parameters['url'] : '',
|
| 530 |
);
|
| 531 |
break;
|
| 532 |
|
| 533 |
case 'drupal_post':
|
| 534 |
$form['url'] = array(
|
| 535 |
'#type' => 'textfield',
|
| 536 |
'#title' => t('URL'),
|
| 537 |
'#required' => TRUE,
|
| 538 |
'#description' => t('Enter the URL to make a POST request to. Should be relative to the drupal install, without the ?q=.'),
|
| 539 |
'#default_value' => isset($action->parameters['url']) ? $action->parameters['url'] : '',
|
| 540 |
);
|
| 541 |
|
| 542 |
// @TODO
|
| 543 |
$form['post'] = array(
|
| 544 |
'#type' => 'value',
|
| 545 |
'#value' => isset($action->parameters['post']) ? $action->parameters['post'] : array(),
|
| 546 |
);
|
| 547 |
|
| 548 |
$form['operation'] = array(
|
| 549 |
'#type' => 'textfield',
|
| 550 |
'#title' => t('Operation'),
|
| 551 |
'#required' => TRUE,
|
| 552 |
'#description' => t('Enter the name of the submit button to simulate being pressed.'),
|
| 553 |
'#default_value' => isset($action->parameters['operation']) ? $action->parameters['operation'] : '',
|
| 554 |
);
|
| 555 |
break;
|
| 556 |
}
|
| 557 |
return $form;
|
| 558 |
}
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
/*
|
| 564 |
* Determines whether or not the simpletest automator is on.
|
| 565 |
*
|
| 566 |
* @return
|
| 567 |
* TRUE if simpletest automator is running, FALSE otherwise.
|
| 568 |
*
|
| 569 |
function _simpletest_automator_on() {
|
| 570 |
return isset($_SESSION['simpletest_automator']['state']) && $_SESSION['simpletest_automator']['state'] == SIMPLETEST_AUTOMATOR_RUNNING;
|
| 571 |
}
|
| 572 |
|
| 573 |
/**
|
| 574 |
* Add our javascript.
|
| 575 |
* /
|
| 576 |
function _simpletest_add_js() {
|
| 577 |
drupal_add_js(drupal_get_path('module', 'simpletest_automator') . '/simpletest_automator.js');
|
| 578 |
}
|
| 579 |
|
| 580 |
/**
|
| 581 |
* Implementation of hook_init().
|
| 582 |
* /
|
| 583 |
function simpletest_automator_init() {
|
| 584 |
global $conf;
|
| 585 |
if (_simpletest_automator_on()) {
|
| 586 |
_simpletest_add_js();
|
| 587 |
if (variable_get('simpletest_automator_record_next_get', TRUE) && empty($_POST) && (!isset($_SESSION['simpletest_automator']['url']) || $_SESSION['simpletest_automator']['url'] != $_GET['q']) && $_GET['q'] != 'simpletest_automator/stop') {
|
| 588 |
simpletest_automator_file('// Make a GET request to the %s page.', $_GET['q']);
|
| 589 |
simpletest_automator_file('$this->drupalGet(%s)', $_GET['q']);
|
| 590 |
}
|
| 591 |
elseif (!variable_get('simpletest_automator_record_next_get', TRUE)) {
|
| 592 |
variable_del('simpletest_automator_record_next_get');
|
| 593 |
}
|
| 594 |
$conf['date_format_short'] = 'custom';
|
| 595 |
$conf['date_format_short_custom'] = '\-\-\s\i\m\p\l\e\t\e\s\t\ \d\e\l\i\m\i\t\e\r\-\-';
|
| 596 |
$conf['date_format_medium'] = 'custom';
|
| 597 |
$conf['date_format_medium_custom'] = '\-\-\s\i\m\p\l\e\t\e\s\t\ \d\e\l\i\m\i\t\e\r\-\-';
|
| 598 |
$conf['date_format_long'] = 'custom';
|
| 599 |
$conf['date_format_long_custom'] = '\-\-\s\i\m\p\l\e\t\e\s\t\ \d\e\l\i\m\i\t\e\r\-\-';
|
| 600 |
}
|
| 601 |
elseif (isset($_SESSION['simpletest_automator']['state'])) {
|
| 602 |
if ($_SESSION['simpletest_automator']['state'] == SIMPLETEST_AUTOMATOR_ON) {
|
| 603 |
// This is the admin/build/simpletest/automator where we start. We move
|
| 604 |
// to the next state and we add the JS because we want to record the
|
| 605 |
// link that navigates us away.
|
| 606 |
$_SESSION['simpletest_automator']['state'] = SIMPLETEST_AUTOMATOR_FIRST_PAGE;
|
| 607 |
_simpletest_add_js();
|
| 608 |
}
|
| 609 |
elseif ($_SESSION['simpletest_automator']['state'] == SIMPLETEST_AUTOMATOR_FIRST_PAGE) {
|
| 610 |
$_SESSION['simpletest_automator']['state'] = SIMPLETEST_AUTOMATOR_RUNNING;
|
| 611 |
_simpletest_add_js();
|
| 612 |
}
|
| 613 |
}
|
| 614 |
}
|
| 615 |
|
| 616 |
/**
|
| 617 |
* Implementation of hook_exit().
|
| 618 |
* /
|
| 619 |
function simpletest_automator_exit($url = NULL) {
|
| 620 |
if (_simpletest_automator_on()) {
|
| 621 |
if (isset($url)) {
|
| 622 |
$parsed_url = parse_url($url);
|
| 623 |
$_SESSION['simpletest_automator']['url'] = substr($parsed_url['path'], strlen(base_path()));
|
| 624 |
}
|
| 625 |
// Close the file.
|
| 626 |
simpletest_automator_file();
|
| 627 |
}
|
| 628 |
}
|
| 629 |
|
| 630 |
/**
|
| 631 |
* Implementation of hook_form_alter().
|
| 632 |
* /
|
| 633 |
function simpletest_automator_form_alter(&$form, $form_state, $form_id) {
|
| 634 |
if (_simpletest_automator_on() && $form_id != 'simpletest_automator_form') {
|
| 635 |
$form['#after_build'][] = 'simpletest_automator_after_build';
|
| 636 |
}
|
| 637 |
}
|
| 638 |
|
| 639 |
/**
|
| 640 |
* Our after_build handler records the posted data and the clicked button.
|
| 641 |
* /
|
| 642 |
function simpletest_automator_after_build($form, $form_state) {
|
| 643 |
if ($post = $form['#post']) {
|
| 644 |
unset($post['form_id'], $post['form_build_id'], $post['form_token']);
|
| 645 |
$post = _simpletest_automator_flatten_post($post);
|
| 646 |
simpletest_automator_file('$edit = %s', $post);
|
| 647 |
simpletest_automator_file('$this->drupalPost(%s, $edit, %s)', $_GET['q'], $form_state['clicked_button']['#value']);
|
| 648 |
}
|
| 649 |
return $form;
|
| 650 |
}
|
| 651 |
|
| 652 |
/**
|
| 653 |
* Flatten the POST request.
|
| 654 |
* /
|
| 655 |
function _simpletest_automator_flatten_post($post, $prefix = '', $postfix = '') {
|
| 656 |
$return = array();
|
| 657 |
foreach ($post as $k => $v) {
|
| 658 |
$new_index = $prefix . $k . $postfix;
|
| 659 |
if (is_array($v)) {
|
| 660 |
$return += _simpletest_automator_flatten_post($v, $new_index .'[', ']');
|
| 661 |
}
|
| 662 |
else {
|
| 663 |
$return[$new_index] = $v;
|
| 664 |
}
|
| 665 |
}
|
| 666 |
return $return;
|
| 667 |
}
|
| 668 |
|
| 669 |
/**
|
| 670 |
* Menu JS callback.
|
| 671 |
* /
|
| 672 |
function simpletest_automator_js() {
|
| 673 |
$text = $_POST['text'];
|
| 674 |
unset($_POST['text']);
|
| 675 |
if ($text == '$this->clickLink(t(%s), %d)' || $text == '$this->clickLink(t(%s))') {
|
| 676 |
// We don't want to record the next GET request, then.
|
| 677 |
variable_set('simpletest_automator_record_next_get', FALSE);
|
| 678 |
}
|
| 679 |
$args = array_merge(array($text), $_POST);
|
| 680 |
call_user_func_array('simpletest_automator_file', $args);
|
| 681 |
}
|
| 682 |
|
| 683 |
/**
|
| 684 |
* Implementation of hook_preprocess_page().
|
| 685 |
* /
|
| 686 |
function simpletest_automator_preprocess_page($vars) {
|
| 687 |
if (_simpletest_automator_on()) {
|
| 688 |
$title = drupal_get_title() ? drupal_get_title() : variable_get('site_name', 'Drupal');
|
| 689 |
simpletest_automator_file('$this->assertTitle(t(%s), t(\'Make sure we have the correct page title.\'))', $title);
|
| 690 |
if ($title == t('Access denied')) {
|
| 691 |
simpletest_automator_file('$this->assertResponse(403)');
|
| 692 |
}
|
| 693 |
elseif ($title == t('Page not found')) {
|
| 694 |
simpletest_automator_file('$this->assertResponse(404)');
|
| 695 |
}
|
| 696 |
else {
|
| 697 |
simpletest_automator_file('$this->assertResponse(200)');
|
| 698 |
}
|
| 699 |
}
|
| 700 |
}
|
| 701 |
|
| 702 |
/**
|
| 703 |
* Implementation of hook_preprocess_block().
|
| 704 |
* /
|
| 705 |
function simpletest_automator_preprocess_block() {
|
| 706 |
static $done = FALSE;
|
| 707 |
if (!$done && _simpletest_automator_on()) {
|
| 708 |
$done = TRUE;
|
| 709 |
foreach (drupal_get_messages(NULL, FALSE) as $type => $messages) {
|
| 710 |
foreach ($messages as $message) {
|
| 711 |
simpletest_automator_file('$this->assertRaw(%s, t(%s, array(\'%message\' => %s)', $message, 'Make sure the %message message appears.', $message);
|
| 712 |
}
|
| 713 |
}
|
| 714 |
}
|
| 715 |
}
|
| 716 |
|
| 717 |
/**
|
| 718 |
* Save some code to the test file.
|
| 719 |
*
|
| 720 |
* @param $line
|
| 721 |
* The line to save, with %* delimiters.
|
| 722 |
* @param ...
|
| 723 |
* Additional arguments to be fed to sprintf().
|
| 724 |
* @return
|
| 725 |
* None.
|
| 726 |
* /
|
| 727 |
function simpletest_automator_file($line = NULL) {
|
| 728 |
static $file;
|
| 729 |
if (isset($line)) {
|
| 730 |
if (!isset($file)) {
|
| 731 |
$file = fopen($_SESSION['simpletest_automator']['filename'], 'a');
|
| 732 |
}
|
| 733 |
$args = func_get_args();
|
| 734 |
foreach ($args as $key => $value) {
|
| 735 |
// Arg 0 is the string, we do not escape that.
|
| 736 |
if ($key) {
|
| 737 |
$args[$key] = var_export($value, TRUE);
|
| 738 |
}
|
| 739 |
}
|
| 740 |
$line = call_user_func_array('sprintf', $args);
|
| 741 |
$comment = $line[0] == '/';
|
| 742 |
if ($line[0] == '$' || $comment) {
|
| 743 |
$line = ' '. $line;
|
| 744 |
}
|
| 745 |
if ($line[strlen($line) - 1] != '{' && !$comment) {
|
| 746 |
$line .= ';';
|
| 747 |
}
|
| 748 |
if ($comment) {
|
| 749 |
$line = "\n" . $line;
|
| 750 |
}
|
| 751 |
fputs($file, "$line\n");
|
| 752 |
}
|
| 753 |
elseif (isset($file)) {
|
| 754 |
fclose($file);
|
| 755 |
}
|
| 756 |
}
|
| 757 |
|
| 758 |
/**
|
| 759 |
* FAPI callback.
|
| 760 |
* /
|
| 761 |
function simpletest_automator_form($form_state) {
|
| 762 |
include_once drupal_get_path('module', 'user') . '/user.admin.inc';
|
| 763 |
$rid = DRUPAL_AUTHENTICATED_RID;
|
| 764 |
$form['permissions'] = drupal_retrieve_form('user_admin_perm', $form_state, $rid);
|
| 765 |
$form['permissions']['checkboxes'][$rid]['#default_value'] = array();
|
| 766 |
$form['permissions']['role_names'][$rid]['#value'] = 'Simpletest Automator';
|
| 767 |
$form['permissions']['#theme'] = 'user_admin_perm';
|
| 768 |
$form['permissions']['submit']['#value'] = t('Start testing with this user');
|
| 769 |
$form['simpletest'] = array(
|
| 770 |
'#weight' => -1,
|
| 771 |
);
|
| 772 |
$form['simpletest']['description'] = array(
|
| 773 |
'#type' => 'textarea',
|
| 774 |
'#title' => t('Description'),
|
| 775 |
'#default_value' => '',
|
| 776 |
'#required' => TRUE,
|
| 777 |
'#description' => t('A short overview (1-3 lines) of what this test case will be testing.'),
|
| 778 |
);
|
| 779 |
$form['simpletest']['file'] = array(
|
| 780 |
'#type' => 'textfield',
|
| 781 |
'#title' => t('Test case filename'),
|
| 782 |
'#default_value' => '',
|
| 783 |
'#required' => TRUE,
|
| 784 |
'#description' => t('Usually the lowercased version of the filename we\'re testing without the extension, and any dots replaced with underscores, e.g "comment" for comment.module or "system_admin" for system.admin.inc. The .test extension will be added automatically.'),
|
| 785 |
);
|
| 786 |
$form['simpletest']['class'] = array(
|
| 787 |
'#type' => 'textfield',
|
| 788 |
'#title' => t('PHP class name'),
|
| 789 |
'#default_value' => '',
|
| 790 |
'#required' => TRUE,
|
| 791 |
'#description' => t('Should be CamelCased, e.g. "CommentAdministrationTestCase".'),
|
| 792 |
);
|
| 793 |
$form['simpletest']['name'] = array(
|
| 794 |
'#type' => 'textfield',
|
| 795 |
'#title' => t('Human readable name'),
|
| 796 |
'#default_value' => '',
|
| 797 |
'#required' => TRUE,
|
| 798 |
'#description' => t('This will be the name as it appears on the administration interface.'),
|
| 799 |
);
|
| 800 |
$form['simpletest']['group'] = array(
|
| 801 |
'#type' => 'textfield',
|
| 802 |
'#title' => t('Group'),
|
| 803 |
'#default_value' => '',
|
| 804 |
'#required' => TRUE,
|
| 805 |
'#description' => t('Typically, the human readable name of the module in question.'),
|
| 806 |
);
|
| 807 |
return $form;
|
| 808 |
}
|
| 809 |
|
| 810 |
function simpletest_automator_form_validate($form, $form_state) {
|
| 811 |
if (!preg_match('/^[a-z][a-z0-9_]* /i', $form_state['values']['file'])) {
|
| 812 |
form_set_error('file', t('The filename needs to be valid: it needs to begin with a letter, and contain only letters, numbers and underscores.'));
|
| 813 |
}
|
| 814 |
if (!preg_match('/^[a-z][a-z0-9_]* /i', $form_state['values']['class'])) {
|
| 815 |
form_set_error('class', t('The class name should be a valid PHP identifier as well: begins with a letter, contains only letters, numbers and underscores.'));
|
| 816 |
}
|
| 817 |
}
|
| 818 |
|
| 819 |
function simpletest_automator_form_submit($form, $form_state) {
|
| 820 |
global $user;
|
| 821 |
$original_uid = $user->uid;
|
| 822 |
$form_values = $form_state['values'];
|
| 823 |
$file = $form_values['file'];
|
| 824 |
$class = $form_values['class'];
|
| 825 |
$permissions= array_keys(array_filter($form_values[DRUPAL_AUTHENTICATED_RID]));
|
| 826 |
$permstring = implode(', ', $permissions);
|
| 827 |
$role_name = 'sa_'. user_password();
|
| 828 |
db_query("INSERT INTO {role} (name) VALUES ('%s')", $role_name);
|
| 829 |
$role = db_fetch_object(db_query("SELECT * FROM {role} WHERE name = '%s'", $role_name));
|
| 830 |
$rid = $role->rid;
|
| 831 |
db_query("INSERT INTO {permission} (rid, perm) VALUES (%d, '%s')", $rid, $permstring);
|
| 832 |
$name = 'sa_'. user_password();
|
| 833 |
$ua = array(
|
| 834 |
'name' => $name,
|
| 835 |
'mail' => $name . '@example.com',
|
| 836 |
'roles' => array($rid => $rid),
|
| 837 |
'pass' => user_password(),
|
| 838 |
'status' => 1,
|
| 839 |
);
|
| 840 |
$user = user_save('', $ua);
|
| 841 |
$edit = array();
|
| 842 |
user_authenticate_finalize($edit);
|
| 843 |
$sessio = $_SESSION;
|
| 844 |
$_SESSION = array(
|
| 845 |
'simpletest_automator' => array(
|
| 846 |
'session' => $_SESSION,
|
| 847 |
'cleanup' => array(
|
| 848 |
'uid' => $user->uid,
|
| 849 |
'rid' => $rid,
|
| 850 |
),
|
| 851 |
'username' => $user->name,
|
| 852 |
'state' => SIMPLETEST_AUTOMATOR_ON,
|
| 853 |
'original_uid' => $original_uid,
|
| 854 |
'filename' => file_directory_path() ."/$file.test",
|
| 855 |
),
|
| 856 |
);
|
| 857 |
$current_modules = module_list();
|
| 858 |
$default_modules = array_merge(drupal_required_modules(), array('color', 'comment', 'help', 'menu', 'taxonomy', 'dblog'));
|
| 859 |
$new_modules = array_diff($current_modules, $default_modules);
|
| 860 |
$disabled_modules = array_diff($default_modules, $current_modules);
|
| 861 |
$module_list = '';
|
| 862 |
if (count($new_modules)) {
|
| 863 |
$module_list = '\''. implode('\', \'', $new_modules) .'\'';
|
| 864 |
}
|
| 865 |
$disable_list = '';
|
| 866 |
if (count($disabled_modules)) {
|
| 867 |
$disable_list = "\n ". implode(");\n \$this->drupalModuleDisable('", $disabled_modules) ."');";
|
| 868 |
}
|
| 869 |
$test = ucfirst($file);
|
| 870 |
simpletest_automator_file("<?php
|
| 871 |
|
| 872 |
class $class extends DrupalWebTestCase {
|
| 873 |
/**
|
| 874 |
* Implementation of getInfo().
|
| 875 |
* /
|
| 876 |
function getInfo() {
|
| 877 |
return array(
|
| 878 |
'name' => t(%s),
|
| 879 |
'description' => t(%s),
|
| 880 |
'group' => t(%s),
|
| 881 |
);
|
| 882 |
}
|
| 883 |
|
| 884 |
/**
|
| 885 |
* Implementation of setUp().
|
| 886 |
* /
|
| 887 |
function setUp() {
|
| 888 |
parent::setUp($module_list);$disable_list
|
| 889 |
}
|
| 890 |
|
| 891 |
/**
|
| 892 |
* Implementation of tearDown().
|
| 893 |
* /
|
| 894 |
function tearDown() {
|
| 895 |
parent::tearDown();
|
| 896 |
}
|
| 897 |
|
| 898 |
function test$test() {", $form_values['name'], $form_values['description'], $form_values['group']);
|
| 899 |
simpletest_automator_file('$user = $this->drupalCreateUser(%s)', $permissions);
|
| 900 |
simpletest_automator_file('$this->drupalLogin($user)');
|
| 901 |
}
|
| 902 |
|
| 903 |
function simpletest_automator_stop() {
|
| 904 |
global $user;
|
| 905 |
simpletest_automator_file(' }
|
| 906 |
}');
|
| 907 |
simpletest_automator_file();
|
| 908 |
$uid = $_SESSION['simpletest_automator']['cleanup']['uid'];
|
| 909 |
$rid = $_SESSION['simpletest_automator']['cleanup']['rid'];
|
| 910 |
db_query('DELETE FROM {users} WHERE uid = %d', $uid);
|
| 911 |
db_query('DELETE FROM {users_roles} WHERE uid = %d', $uid);
|
| 912 |
db_query('DELETE FROM {authmap} WHERE uid = %d', $uid);
|
| 913 |
module_invoke_all('user', 'delete', array(), $user);
|
| 914 |
db_query('DELETE FROM {permission} WHERE rid = %d', $rid);
|
| 915 |
db_query('DELETE FROM {role} WHERE rid = %d', $rid);
|
| 916 |
$user = user_load($_SESSION['simpletest_automator']['original_uid']);
|
| 917 |
$_SESSION = $_SESSION['simpletest_automator']['session'];
|
| 918 |
unset($_SESSION['simpletest_automator']);
|
| 919 |
$edit = array();
|
| 920 |
user_authenticate_finalize($edit);
|
| 921 |
drupal_goto();
|
| 922 |
}
|
| 923 |
*/
|