annotate modules/simpletest/drupal_web_test_case.php @ 0:ff03f76ab3fe

initial version
author danieleb <danielebarchiesi@me.com>
date Wed, 21 Aug 2013 18:51:11 +0100
parents
children
rev   line source
danielebarchiesi@0 1 <?php
danielebarchiesi@0 2
danielebarchiesi@0 3 /**
danielebarchiesi@0 4 * Global variable that holds information about the tests being run.
danielebarchiesi@0 5 *
danielebarchiesi@0 6 * An array, with the following keys:
danielebarchiesi@0 7 * - 'test_run_id': the ID of the test being run, in the form 'simpletest_%"
danielebarchiesi@0 8 * - 'in_child_site': TRUE if the current request is a cURL request from
danielebarchiesi@0 9 * the parent site.
danielebarchiesi@0 10 *
danielebarchiesi@0 11 * @var array
danielebarchiesi@0 12 */
danielebarchiesi@0 13 global $drupal_test_info;
danielebarchiesi@0 14
danielebarchiesi@0 15 /**
danielebarchiesi@0 16 * Base class for Drupal tests.
danielebarchiesi@0 17 *
danielebarchiesi@0 18 * Do not extend this class, use one of the subclasses in this file.
danielebarchiesi@0 19 */
danielebarchiesi@0 20 abstract class DrupalTestCase {
danielebarchiesi@0 21 /**
danielebarchiesi@0 22 * The test run ID.
danielebarchiesi@0 23 *
danielebarchiesi@0 24 * @var string
danielebarchiesi@0 25 */
danielebarchiesi@0 26 protected $testId;
danielebarchiesi@0 27
danielebarchiesi@0 28 /**
danielebarchiesi@0 29 * The database prefix of this test run.
danielebarchiesi@0 30 *
danielebarchiesi@0 31 * @var string
danielebarchiesi@0 32 */
danielebarchiesi@0 33 protected $databasePrefix = NULL;
danielebarchiesi@0 34
danielebarchiesi@0 35 /**
danielebarchiesi@0 36 * The original file directory, before it was changed for testing purposes.
danielebarchiesi@0 37 *
danielebarchiesi@0 38 * @var string
danielebarchiesi@0 39 */
danielebarchiesi@0 40 protected $originalFileDirectory = NULL;
danielebarchiesi@0 41
danielebarchiesi@0 42 /**
danielebarchiesi@0 43 * Time limit for the test.
danielebarchiesi@0 44 */
danielebarchiesi@0 45 protected $timeLimit = 500;
danielebarchiesi@0 46
danielebarchiesi@0 47 /**
danielebarchiesi@0 48 * Current results of this test case.
danielebarchiesi@0 49 *
danielebarchiesi@0 50 * @var Array
danielebarchiesi@0 51 */
danielebarchiesi@0 52 public $results = array(
danielebarchiesi@0 53 '#pass' => 0,
danielebarchiesi@0 54 '#fail' => 0,
danielebarchiesi@0 55 '#exception' => 0,
danielebarchiesi@0 56 '#debug' => 0,
danielebarchiesi@0 57 );
danielebarchiesi@0 58
danielebarchiesi@0 59 /**
danielebarchiesi@0 60 * Assertions thrown in that test case.
danielebarchiesi@0 61 *
danielebarchiesi@0 62 * @var Array
danielebarchiesi@0 63 */
danielebarchiesi@0 64 protected $assertions = array();
danielebarchiesi@0 65
danielebarchiesi@0 66 /**
danielebarchiesi@0 67 * This class is skipped when looking for the source of an assertion.
danielebarchiesi@0 68 *
danielebarchiesi@0 69 * When displaying which function an assert comes from, it's not too useful
danielebarchiesi@0 70 * to see "drupalWebTestCase->drupalLogin()', we would like to see the test
danielebarchiesi@0 71 * that called it. So we need to skip the classes defining these helper
danielebarchiesi@0 72 * methods.
danielebarchiesi@0 73 */
danielebarchiesi@0 74 protected $skipClasses = array(__CLASS__ => TRUE);
danielebarchiesi@0 75
danielebarchiesi@0 76 /**
danielebarchiesi@0 77 * Flag to indicate whether the test has been set up.
danielebarchiesi@0 78 *
danielebarchiesi@0 79 * The setUp() method isolates the test from the parent Drupal site by
danielebarchiesi@0 80 * creating a random prefix for the database and setting up a clean file
danielebarchiesi@0 81 * storage directory. The tearDown() method then cleans up this test
danielebarchiesi@0 82 * environment. We must ensure that setUp() has been run. Otherwise,
danielebarchiesi@0 83 * tearDown() will act on the parent Drupal site rather than the test
danielebarchiesi@0 84 * environment, destroying live data.
danielebarchiesi@0 85 */
danielebarchiesi@0 86 protected $setup = FALSE;
danielebarchiesi@0 87
danielebarchiesi@0 88 protected $setupDatabasePrefix = FALSE;
danielebarchiesi@0 89
danielebarchiesi@0 90 protected $setupEnvironment = FALSE;
danielebarchiesi@0 91
danielebarchiesi@0 92 /**
danielebarchiesi@0 93 * Constructor for DrupalTestCase.
danielebarchiesi@0 94 *
danielebarchiesi@0 95 * @param $test_id
danielebarchiesi@0 96 * Tests with the same id are reported together.
danielebarchiesi@0 97 */
danielebarchiesi@0 98 public function __construct($test_id = NULL) {
danielebarchiesi@0 99 $this->testId = $test_id;
danielebarchiesi@0 100 }
danielebarchiesi@0 101
danielebarchiesi@0 102 /**
danielebarchiesi@0 103 * Internal helper: stores the assert.
danielebarchiesi@0 104 *
danielebarchiesi@0 105 * @param $status
danielebarchiesi@0 106 * Can be 'pass', 'fail', 'exception'.
danielebarchiesi@0 107 * TRUE is a synonym for 'pass', FALSE for 'fail'.
danielebarchiesi@0 108 * @param $message
danielebarchiesi@0 109 * The message string.
danielebarchiesi@0 110 * @param $group
danielebarchiesi@0 111 * Which group this assert belongs to.
danielebarchiesi@0 112 * @param $caller
danielebarchiesi@0 113 * By default, the assert comes from a function whose name starts with
danielebarchiesi@0 114 * 'test'. Instead, you can specify where this assert originates from
danielebarchiesi@0 115 * by passing in an associative array as $caller. Key 'file' is
danielebarchiesi@0 116 * the name of the source file, 'line' is the line number and 'function'
danielebarchiesi@0 117 * is the caller function itself.
danielebarchiesi@0 118 */
danielebarchiesi@0 119 protected function assert($status, $message = '', $group = 'Other', array $caller = NULL) {
danielebarchiesi@0 120 // Convert boolean status to string status.
danielebarchiesi@0 121 if (is_bool($status)) {
danielebarchiesi@0 122 $status = $status ? 'pass' : 'fail';
danielebarchiesi@0 123 }
danielebarchiesi@0 124
danielebarchiesi@0 125 // Increment summary result counter.
danielebarchiesi@0 126 $this->results['#' . $status]++;
danielebarchiesi@0 127
danielebarchiesi@0 128 // Get the function information about the call to the assertion method.
danielebarchiesi@0 129 if (!$caller) {
danielebarchiesi@0 130 $caller = $this->getAssertionCall();
danielebarchiesi@0 131 }
danielebarchiesi@0 132
danielebarchiesi@0 133 // Creation assertion array that can be displayed while tests are running.
danielebarchiesi@0 134 $this->assertions[] = $assertion = array(
danielebarchiesi@0 135 'test_id' => $this->testId,
danielebarchiesi@0 136 'test_class' => get_class($this),
danielebarchiesi@0 137 'status' => $status,
danielebarchiesi@0 138 'message' => $message,
danielebarchiesi@0 139 'message_group' => $group,
danielebarchiesi@0 140 'function' => $caller['function'],
danielebarchiesi@0 141 'line' => $caller['line'],
danielebarchiesi@0 142 'file' => $caller['file'],
danielebarchiesi@0 143 );
danielebarchiesi@0 144
danielebarchiesi@0 145 // Store assertion for display after the test has completed.
danielebarchiesi@0 146 try {
danielebarchiesi@0 147 $connection = Database::getConnection('default', 'simpletest_original_default');
danielebarchiesi@0 148 }
danielebarchiesi@0 149 catch (DatabaseConnectionNotDefinedException $e) {
danielebarchiesi@0 150 // If the test was not set up, the simpletest_original_default
danielebarchiesi@0 151 // connection does not exist.
danielebarchiesi@0 152 $connection = Database::getConnection('default', 'default');
danielebarchiesi@0 153 }
danielebarchiesi@0 154 $connection
danielebarchiesi@0 155 ->insert('simpletest')
danielebarchiesi@0 156 ->fields($assertion)
danielebarchiesi@0 157 ->execute();
danielebarchiesi@0 158
danielebarchiesi@0 159 // We do not use a ternary operator here to allow a breakpoint on
danielebarchiesi@0 160 // test failure.
danielebarchiesi@0 161 if ($status == 'pass') {
danielebarchiesi@0 162 return TRUE;
danielebarchiesi@0 163 }
danielebarchiesi@0 164 else {
danielebarchiesi@0 165 return FALSE;
danielebarchiesi@0 166 }
danielebarchiesi@0 167 }
danielebarchiesi@0 168
danielebarchiesi@0 169 /**
danielebarchiesi@0 170 * Store an assertion from outside the testing context.
danielebarchiesi@0 171 *
danielebarchiesi@0 172 * This is useful for inserting assertions that can only be recorded after
danielebarchiesi@0 173 * the test case has been destroyed, such as PHP fatal errors. The caller
danielebarchiesi@0 174 * information is not automatically gathered since the caller is most likely
danielebarchiesi@0 175 * inserting the assertion on behalf of other code. In all other respects
danielebarchiesi@0 176 * the method behaves just like DrupalTestCase::assert() in terms of storing
danielebarchiesi@0 177 * the assertion.
danielebarchiesi@0 178 *
danielebarchiesi@0 179 * @return
danielebarchiesi@0 180 * Message ID of the stored assertion.
danielebarchiesi@0 181 *
danielebarchiesi@0 182 * @see DrupalTestCase::assert()
danielebarchiesi@0 183 * @see DrupalTestCase::deleteAssert()
danielebarchiesi@0 184 */
danielebarchiesi@0 185 public static function insertAssert($test_id, $test_class, $status, $message = '', $group = 'Other', array $caller = array()) {
danielebarchiesi@0 186 // Convert boolean status to string status.
danielebarchiesi@0 187 if (is_bool($status)) {
danielebarchiesi@0 188 $status = $status ? 'pass' : 'fail';
danielebarchiesi@0 189 }
danielebarchiesi@0 190
danielebarchiesi@0 191 $caller += array(
danielebarchiesi@0 192 'function' => t('Unknown'),
danielebarchiesi@0 193 'line' => 0,
danielebarchiesi@0 194 'file' => t('Unknown'),
danielebarchiesi@0 195 );
danielebarchiesi@0 196
danielebarchiesi@0 197 $assertion = array(
danielebarchiesi@0 198 'test_id' => $test_id,
danielebarchiesi@0 199 'test_class' => $test_class,
danielebarchiesi@0 200 'status' => $status,
danielebarchiesi@0 201 'message' => $message,
danielebarchiesi@0 202 'message_group' => $group,
danielebarchiesi@0 203 'function' => $caller['function'],
danielebarchiesi@0 204 'line' => $caller['line'],
danielebarchiesi@0 205 'file' => $caller['file'],
danielebarchiesi@0 206 );
danielebarchiesi@0 207
danielebarchiesi@0 208 return db_insert('simpletest')
danielebarchiesi@0 209 ->fields($assertion)
danielebarchiesi@0 210 ->execute();
danielebarchiesi@0 211 }
danielebarchiesi@0 212
danielebarchiesi@0 213 /**
danielebarchiesi@0 214 * Delete an assertion record by message ID.
danielebarchiesi@0 215 *
danielebarchiesi@0 216 * @param $message_id
danielebarchiesi@0 217 * Message ID of the assertion to delete.
danielebarchiesi@0 218 * @return
danielebarchiesi@0 219 * TRUE if the assertion was deleted, FALSE otherwise.
danielebarchiesi@0 220 *
danielebarchiesi@0 221 * @see DrupalTestCase::insertAssert()
danielebarchiesi@0 222 */
danielebarchiesi@0 223 public static function deleteAssert($message_id) {
danielebarchiesi@0 224 return (bool) db_delete('simpletest')
danielebarchiesi@0 225 ->condition('message_id', $message_id)
danielebarchiesi@0 226 ->execute();
danielebarchiesi@0 227 }
danielebarchiesi@0 228
danielebarchiesi@0 229 /**
danielebarchiesi@0 230 * Cycles through backtrace until the first non-assertion method is found.
danielebarchiesi@0 231 *
danielebarchiesi@0 232 * @return
danielebarchiesi@0 233 * Array representing the true caller.
danielebarchiesi@0 234 */
danielebarchiesi@0 235 protected function getAssertionCall() {
danielebarchiesi@0 236 $backtrace = debug_backtrace();
danielebarchiesi@0 237
danielebarchiesi@0 238 // The first element is the call. The second element is the caller.
danielebarchiesi@0 239 // We skip calls that occurred in one of the methods of our base classes
danielebarchiesi@0 240 // or in an assertion function.
danielebarchiesi@0 241 while (($caller = $backtrace[1]) &&
danielebarchiesi@0 242 ((isset($caller['class']) && isset($this->skipClasses[$caller['class']])) ||
danielebarchiesi@0 243 substr($caller['function'], 0, 6) == 'assert')) {
danielebarchiesi@0 244 // We remove that call.
danielebarchiesi@0 245 array_shift($backtrace);
danielebarchiesi@0 246 }
danielebarchiesi@0 247
danielebarchiesi@0 248 return _drupal_get_last_caller($backtrace);
danielebarchiesi@0 249 }
danielebarchiesi@0 250
danielebarchiesi@0 251 /**
danielebarchiesi@0 252 * Check to see if a value is not false (not an empty string, 0, NULL, or FALSE).
danielebarchiesi@0 253 *
danielebarchiesi@0 254 * @param $value
danielebarchiesi@0 255 * The value on which the assertion is to be done.
danielebarchiesi@0 256 * @param $message
danielebarchiesi@0 257 * The message to display along with the assertion.
danielebarchiesi@0 258 * @param $group
danielebarchiesi@0 259 * The type of assertion - examples are "Browser", "PHP".
danielebarchiesi@0 260 * @return
danielebarchiesi@0 261 * TRUE if the assertion succeeded, FALSE otherwise.
danielebarchiesi@0 262 */
danielebarchiesi@0 263 protected function assertTrue($value, $message = '', $group = 'Other') {
danielebarchiesi@0 264 return $this->assert((bool) $value, $message ? $message : t('Value @value is TRUE.', array('@value' => var_export($value, TRUE))), $group);
danielebarchiesi@0 265 }
danielebarchiesi@0 266
danielebarchiesi@0 267 /**
danielebarchiesi@0 268 * Check to see if a value is false (an empty string, 0, NULL, or FALSE).
danielebarchiesi@0 269 *
danielebarchiesi@0 270 * @param $value
danielebarchiesi@0 271 * The value on which the assertion is to be done.
danielebarchiesi@0 272 * @param $message
danielebarchiesi@0 273 * The message to display along with the assertion.
danielebarchiesi@0 274 * @param $group
danielebarchiesi@0 275 * The type of assertion - examples are "Browser", "PHP".
danielebarchiesi@0 276 * @return
danielebarchiesi@0 277 * TRUE if the assertion succeeded, FALSE otherwise.
danielebarchiesi@0 278 */
danielebarchiesi@0 279 protected function assertFalse($value, $message = '', $group = 'Other') {
danielebarchiesi@0 280 return $this->assert(!$value, $message ? $message : t('Value @value is FALSE.', array('@value' => var_export($value, TRUE))), $group);
danielebarchiesi@0 281 }
danielebarchiesi@0 282
danielebarchiesi@0 283 /**
danielebarchiesi@0 284 * Check to see if a value is NULL.
danielebarchiesi@0 285 *
danielebarchiesi@0 286 * @param $value
danielebarchiesi@0 287 * The value on which the assertion is to be done.
danielebarchiesi@0 288 * @param $message
danielebarchiesi@0 289 * The message to display along with the assertion.
danielebarchiesi@0 290 * @param $group
danielebarchiesi@0 291 * The type of assertion - examples are "Browser", "PHP".
danielebarchiesi@0 292 * @return
danielebarchiesi@0 293 * TRUE if the assertion succeeded, FALSE otherwise.
danielebarchiesi@0 294 */
danielebarchiesi@0 295 protected function assertNull($value, $message = '', $group = 'Other') {
danielebarchiesi@0 296 return $this->assert(!isset($value), $message ? $message : t('Value @value is NULL.', array('@value' => var_export($value, TRUE))), $group);
danielebarchiesi@0 297 }
danielebarchiesi@0 298
danielebarchiesi@0 299 /**
danielebarchiesi@0 300 * Check to see if a value is not NULL.
danielebarchiesi@0 301 *
danielebarchiesi@0 302 * @param $value
danielebarchiesi@0 303 * The value on which the assertion is to be done.
danielebarchiesi@0 304 * @param $message
danielebarchiesi@0 305 * The message to display along with the assertion.
danielebarchiesi@0 306 * @param $group
danielebarchiesi@0 307 * The type of assertion - examples are "Browser", "PHP".
danielebarchiesi@0 308 * @return
danielebarchiesi@0 309 * TRUE if the assertion succeeded, FALSE otherwise.
danielebarchiesi@0 310 */
danielebarchiesi@0 311 protected function assertNotNull($value, $message = '', $group = 'Other') {
danielebarchiesi@0 312 return $this->assert(isset($value), $message ? $message : t('Value @value is not NULL.', array('@value' => var_export($value, TRUE))), $group);
danielebarchiesi@0 313 }
danielebarchiesi@0 314
danielebarchiesi@0 315 /**
danielebarchiesi@0 316 * Check to see if two values are equal.
danielebarchiesi@0 317 *
danielebarchiesi@0 318 * @param $first
danielebarchiesi@0 319 * The first value to check.
danielebarchiesi@0 320 * @param $second
danielebarchiesi@0 321 * The second value to check.
danielebarchiesi@0 322 * @param $message
danielebarchiesi@0 323 * The message to display along with the assertion.
danielebarchiesi@0 324 * @param $group
danielebarchiesi@0 325 * The type of assertion - examples are "Browser", "PHP".
danielebarchiesi@0 326 * @return
danielebarchiesi@0 327 * TRUE if the assertion succeeded, FALSE otherwise.
danielebarchiesi@0 328 */
danielebarchiesi@0 329 protected function assertEqual($first, $second, $message = '', $group = 'Other') {
danielebarchiesi@0 330 return $this->assert($first == $second, $message ? $message : t('Value @first is equal to value @second.', array('@first' => var_export($first, TRUE), '@second' => var_export($second, TRUE))), $group);
danielebarchiesi@0 331 }
danielebarchiesi@0 332
danielebarchiesi@0 333 /**
danielebarchiesi@0 334 * Check to see if two values are not equal.
danielebarchiesi@0 335 *
danielebarchiesi@0 336 * @param $first
danielebarchiesi@0 337 * The first value to check.
danielebarchiesi@0 338 * @param $second
danielebarchiesi@0 339 * The second value to check.
danielebarchiesi@0 340 * @param $message
danielebarchiesi@0 341 * The message to display along with the assertion.
danielebarchiesi@0 342 * @param $group
danielebarchiesi@0 343 * The type of assertion - examples are "Browser", "PHP".
danielebarchiesi@0 344 * @return
danielebarchiesi@0 345 * TRUE if the assertion succeeded, FALSE otherwise.
danielebarchiesi@0 346 */
danielebarchiesi@0 347 protected function assertNotEqual($first, $second, $message = '', $group = 'Other') {
danielebarchiesi@0 348 return $this->assert($first != $second, $message ? $message : t('Value @first is not equal to value @second.', array('@first' => var_export($first, TRUE), '@second' => var_export($second, TRUE))), $group);
danielebarchiesi@0 349 }
danielebarchiesi@0 350
danielebarchiesi@0 351 /**
danielebarchiesi@0 352 * Check to see if two values are identical.
danielebarchiesi@0 353 *
danielebarchiesi@0 354 * @param $first
danielebarchiesi@0 355 * The first value to check.
danielebarchiesi@0 356 * @param $second
danielebarchiesi@0 357 * The second value to check.
danielebarchiesi@0 358 * @param $message
danielebarchiesi@0 359 * The message to display along with the assertion.
danielebarchiesi@0 360 * @param $group
danielebarchiesi@0 361 * The type of assertion - examples are "Browser", "PHP".
danielebarchiesi@0 362 * @return
danielebarchiesi@0 363 * TRUE if the assertion succeeded, FALSE otherwise.
danielebarchiesi@0 364 */
danielebarchiesi@0 365 protected function assertIdentical($first, $second, $message = '', $group = 'Other') {
danielebarchiesi@0 366 return $this->assert($first === $second, $message ? $message : t('Value @first is identical to value @second.', array('@first' => var_export($first, TRUE), '@second' => var_export($second, TRUE))), $group);
danielebarchiesi@0 367 }
danielebarchiesi@0 368
danielebarchiesi@0 369 /**
danielebarchiesi@0 370 * Check to see if two values are not identical.
danielebarchiesi@0 371 *
danielebarchiesi@0 372 * @param $first
danielebarchiesi@0 373 * The first value to check.
danielebarchiesi@0 374 * @param $second
danielebarchiesi@0 375 * The second value to check.
danielebarchiesi@0 376 * @param $message
danielebarchiesi@0 377 * The message to display along with the assertion.
danielebarchiesi@0 378 * @param $group
danielebarchiesi@0 379 * The type of assertion - examples are "Browser", "PHP".
danielebarchiesi@0 380 * @return
danielebarchiesi@0 381 * TRUE if the assertion succeeded, FALSE otherwise.
danielebarchiesi@0 382 */
danielebarchiesi@0 383 protected function assertNotIdentical($first, $second, $message = '', $group = 'Other') {
danielebarchiesi@0 384 return $this->assert($first !== $second, $message ? $message : t('Value @first is not identical to value @second.', array('@first' => var_export($first, TRUE), '@second' => var_export($second, TRUE))), $group);
danielebarchiesi@0 385 }
danielebarchiesi@0 386
danielebarchiesi@0 387 /**
danielebarchiesi@0 388 * Fire an assertion that is always positive.
danielebarchiesi@0 389 *
danielebarchiesi@0 390 * @param $message
danielebarchiesi@0 391 * The message to display along with the assertion.
danielebarchiesi@0 392 * @param $group
danielebarchiesi@0 393 * The type of assertion - examples are "Browser", "PHP".
danielebarchiesi@0 394 * @return
danielebarchiesi@0 395 * TRUE.
danielebarchiesi@0 396 */
danielebarchiesi@0 397 protected function pass($message = NULL, $group = 'Other') {
danielebarchiesi@0 398 return $this->assert(TRUE, $message, $group);
danielebarchiesi@0 399 }
danielebarchiesi@0 400
danielebarchiesi@0 401 /**
danielebarchiesi@0 402 * Fire an assertion that is always negative.
danielebarchiesi@0 403 *
danielebarchiesi@0 404 * @param $message
danielebarchiesi@0 405 * The message to display along with the assertion.
danielebarchiesi@0 406 * @param $group
danielebarchiesi@0 407 * The type of assertion - examples are "Browser", "PHP".
danielebarchiesi@0 408 * @return
danielebarchiesi@0 409 * FALSE.
danielebarchiesi@0 410 */
danielebarchiesi@0 411 protected function fail($message = NULL, $group = 'Other') {
danielebarchiesi@0 412 return $this->assert(FALSE, $message, $group);
danielebarchiesi@0 413 }
danielebarchiesi@0 414
danielebarchiesi@0 415 /**
danielebarchiesi@0 416 * Fire an error assertion.
danielebarchiesi@0 417 *
danielebarchiesi@0 418 * @param $message
danielebarchiesi@0 419 * The message to display along with the assertion.
danielebarchiesi@0 420 * @param $group
danielebarchiesi@0 421 * The type of assertion - examples are "Browser", "PHP".
danielebarchiesi@0 422 * @param $caller
danielebarchiesi@0 423 * The caller of the error.
danielebarchiesi@0 424 * @return
danielebarchiesi@0 425 * FALSE.
danielebarchiesi@0 426 */
danielebarchiesi@0 427 protected function error($message = '', $group = 'Other', array $caller = NULL) {
danielebarchiesi@0 428 if ($group == 'User notice') {
danielebarchiesi@0 429 // Since 'User notice' is set by trigger_error() which is used for debug
danielebarchiesi@0 430 // set the message to a status of 'debug'.
danielebarchiesi@0 431 return $this->assert('debug', $message, 'Debug', $caller);
danielebarchiesi@0 432 }
danielebarchiesi@0 433
danielebarchiesi@0 434 return $this->assert('exception', $message, $group, $caller);
danielebarchiesi@0 435 }
danielebarchiesi@0 436
danielebarchiesi@0 437 /**
danielebarchiesi@0 438 * Logs verbose message in a text file.
danielebarchiesi@0 439 *
danielebarchiesi@0 440 * The a link to the vebose message will be placed in the test results via
danielebarchiesi@0 441 * as a passing assertion with the text '[verbose message]'.
danielebarchiesi@0 442 *
danielebarchiesi@0 443 * @param $message
danielebarchiesi@0 444 * The verbose message to be stored.
danielebarchiesi@0 445 *
danielebarchiesi@0 446 * @see simpletest_verbose()
danielebarchiesi@0 447 */
danielebarchiesi@0 448 protected function verbose($message) {
danielebarchiesi@0 449 if ($id = simpletest_verbose($message)) {
danielebarchiesi@0 450 $class_safe = str_replace('\\', '_', get_class($this));
danielebarchiesi@0 451 $url = file_create_url($this->originalFileDirectory . '/simpletest/verbose/' . $class_safe . '-' . $id . '.html');
danielebarchiesi@0 452 $this->error(l(t('Verbose message'), $url, array('attributes' => array('target' => '_blank'))), 'User notice');
danielebarchiesi@0 453 }
danielebarchiesi@0 454 }
danielebarchiesi@0 455
danielebarchiesi@0 456 /**
danielebarchiesi@0 457 * Run all tests in this class.
danielebarchiesi@0 458 *
danielebarchiesi@0 459 * Regardless of whether $methods are passed or not, only method names
danielebarchiesi@0 460 * starting with "test" are executed.
danielebarchiesi@0 461 *
danielebarchiesi@0 462 * @param $methods
danielebarchiesi@0 463 * (optional) A list of method names in the test case class to run; e.g.,
danielebarchiesi@0 464 * array('testFoo', 'testBar'). By default, all methods of the class are
danielebarchiesi@0 465 * taken into account, but it can be useful to only run a few selected test
danielebarchiesi@0 466 * methods during debugging.
danielebarchiesi@0 467 */
danielebarchiesi@0 468 public function run(array $methods = array()) {
danielebarchiesi@0 469 // Initialize verbose debugging.
danielebarchiesi@0 470 $class = get_class($this);
danielebarchiesi@0 471 simpletest_verbose(NULL, variable_get('file_public_path', conf_path() . '/files'), str_replace('\\', '_', $class));
danielebarchiesi@0 472
danielebarchiesi@0 473 // HTTP auth settings (<username>:<password>) for the simpletest browser
danielebarchiesi@0 474 // when sending requests to the test site.
danielebarchiesi@0 475 $this->httpauth_method = variable_get('simpletest_httpauth_method', CURLAUTH_BASIC);
danielebarchiesi@0 476 $username = variable_get('simpletest_httpauth_username', NULL);
danielebarchiesi@0 477 $password = variable_get('simpletest_httpauth_password', NULL);
danielebarchiesi@0 478 if ($username && $password) {
danielebarchiesi@0 479 $this->httpauth_credentials = $username . ':' . $password;
danielebarchiesi@0 480 }
danielebarchiesi@0 481
danielebarchiesi@0 482 set_error_handler(array($this, 'errorHandler'));
danielebarchiesi@0 483 // Iterate through all the methods in this class, unless a specific list of
danielebarchiesi@0 484 // methods to run was passed.
danielebarchiesi@0 485 $class_methods = get_class_methods($class);
danielebarchiesi@0 486 if ($methods) {
danielebarchiesi@0 487 $class_methods = array_intersect($class_methods, $methods);
danielebarchiesi@0 488 }
danielebarchiesi@0 489 foreach ($class_methods as $method) {
danielebarchiesi@0 490 // If the current method starts with "test", run it - it's a test.
danielebarchiesi@0 491 if (strtolower(substr($method, 0, 4)) == 'test') {
danielebarchiesi@0 492 // Insert a fail record. This will be deleted on completion to ensure
danielebarchiesi@0 493 // that testing completed.
danielebarchiesi@0 494 $method_info = new ReflectionMethod($class, $method);
danielebarchiesi@0 495 $caller = array(
danielebarchiesi@0 496 'file' => $method_info->getFileName(),
danielebarchiesi@0 497 'line' => $method_info->getStartLine(),
danielebarchiesi@0 498 'function' => $class . '->' . $method . '()',
danielebarchiesi@0 499 );
danielebarchiesi@0 500 $completion_check_id = DrupalTestCase::insertAssert($this->testId, $class, FALSE, t('The test did not complete due to a fatal error.'), 'Completion check', $caller);
danielebarchiesi@0 501 $this->setUp();
danielebarchiesi@0 502 if ($this->setup) {
danielebarchiesi@0 503 try {
danielebarchiesi@0 504 $this->$method();
danielebarchiesi@0 505 // Finish up.
danielebarchiesi@0 506 }
danielebarchiesi@0 507 catch (Exception $e) {
danielebarchiesi@0 508 $this->exceptionHandler($e);
danielebarchiesi@0 509 }
danielebarchiesi@0 510 $this->tearDown();
danielebarchiesi@0 511 }
danielebarchiesi@0 512 else {
danielebarchiesi@0 513 $this->fail(t("The test cannot be executed because it has not been set up properly."));
danielebarchiesi@0 514 }
danielebarchiesi@0 515 // Remove the completion check record.
danielebarchiesi@0 516 DrupalTestCase::deleteAssert($completion_check_id);
danielebarchiesi@0 517 }
danielebarchiesi@0 518 }
danielebarchiesi@0 519 // Clear out the error messages and restore error handler.
danielebarchiesi@0 520 drupal_get_messages();
danielebarchiesi@0 521 restore_error_handler();
danielebarchiesi@0 522 }
danielebarchiesi@0 523
danielebarchiesi@0 524 /**
danielebarchiesi@0 525 * Handle errors during test runs.
danielebarchiesi@0 526 *
danielebarchiesi@0 527 * Because this is registered in set_error_handler(), it has to be public.
danielebarchiesi@0 528 * @see set_error_handler
danielebarchiesi@0 529 */
danielebarchiesi@0 530 public function errorHandler($severity, $message, $file = NULL, $line = NULL) {
danielebarchiesi@0 531 if ($severity & error_reporting()) {
danielebarchiesi@0 532 $error_map = array(
danielebarchiesi@0 533 E_STRICT => 'Run-time notice',
danielebarchiesi@0 534 E_WARNING => 'Warning',
danielebarchiesi@0 535 E_NOTICE => 'Notice',
danielebarchiesi@0 536 E_CORE_ERROR => 'Core error',
danielebarchiesi@0 537 E_CORE_WARNING => 'Core warning',
danielebarchiesi@0 538 E_USER_ERROR => 'User error',
danielebarchiesi@0 539 E_USER_WARNING => 'User warning',
danielebarchiesi@0 540 E_USER_NOTICE => 'User notice',
danielebarchiesi@0 541 E_RECOVERABLE_ERROR => 'Recoverable error',
danielebarchiesi@0 542 );
danielebarchiesi@0 543
danielebarchiesi@0 544 $backtrace = debug_backtrace();
danielebarchiesi@0 545 $this->error($message, $error_map[$severity], _drupal_get_last_caller($backtrace));
danielebarchiesi@0 546 }
danielebarchiesi@0 547 return TRUE;
danielebarchiesi@0 548 }
danielebarchiesi@0 549
danielebarchiesi@0 550 /**
danielebarchiesi@0 551 * Handle exceptions.
danielebarchiesi@0 552 *
danielebarchiesi@0 553 * @see set_exception_handler
danielebarchiesi@0 554 */
danielebarchiesi@0 555 protected function exceptionHandler($exception) {
danielebarchiesi@0 556 $backtrace = $exception->getTrace();
danielebarchiesi@0 557 // Push on top of the backtrace the call that generated the exception.
danielebarchiesi@0 558 array_unshift($backtrace, array(
danielebarchiesi@0 559 'line' => $exception->getLine(),
danielebarchiesi@0 560 'file' => $exception->getFile(),
danielebarchiesi@0 561 ));
danielebarchiesi@0 562 require_once DRUPAL_ROOT . '/includes/errors.inc';
danielebarchiesi@0 563 // The exception message is run through check_plain() by _drupal_decode_exception().
danielebarchiesi@0 564 $this->error(t('%type: !message in %function (line %line of %file).', _drupal_decode_exception($exception)), 'Uncaught exception', _drupal_get_last_caller($backtrace));
danielebarchiesi@0 565 }
danielebarchiesi@0 566
danielebarchiesi@0 567 /**
danielebarchiesi@0 568 * Generates a random string of ASCII characters of codes 32 to 126.
danielebarchiesi@0 569 *
danielebarchiesi@0 570 * The generated string includes alpha-numeric characters and common
danielebarchiesi@0 571 * miscellaneous characters. Use this method when testing general input
danielebarchiesi@0 572 * where the content is not restricted.
danielebarchiesi@0 573 *
danielebarchiesi@0 574 * Do not use this method when special characters are not possible (e.g., in
danielebarchiesi@0 575 * machine or file names that have already been validated); instead,
danielebarchiesi@0 576 * use DrupalWebTestCase::randomName().
danielebarchiesi@0 577 *
danielebarchiesi@0 578 * @param $length
danielebarchiesi@0 579 * Length of random string to generate.
danielebarchiesi@0 580 *
danielebarchiesi@0 581 * @return
danielebarchiesi@0 582 * Randomly generated string.
danielebarchiesi@0 583 *
danielebarchiesi@0 584 * @see DrupalWebTestCase::randomName()
danielebarchiesi@0 585 */
danielebarchiesi@0 586 public static function randomString($length = 8) {
danielebarchiesi@0 587 $str = '';
danielebarchiesi@0 588 for ($i = 0; $i < $length; $i++) {
danielebarchiesi@0 589 $str .= chr(mt_rand(32, 126));
danielebarchiesi@0 590 }
danielebarchiesi@0 591 return $str;
danielebarchiesi@0 592 }
danielebarchiesi@0 593
danielebarchiesi@0 594 /**
danielebarchiesi@0 595 * Generates a random string containing letters and numbers.
danielebarchiesi@0 596 *
danielebarchiesi@0 597 * The string will always start with a letter. The letters may be upper or
danielebarchiesi@0 598 * lower case. This method is better for restricted inputs that do not
danielebarchiesi@0 599 * accept certain characters. For example, when testing input fields that
danielebarchiesi@0 600 * require machine readable values (i.e. without spaces and non-standard
danielebarchiesi@0 601 * characters) this method is best.
danielebarchiesi@0 602 *
danielebarchiesi@0 603 * Do not use this method when testing unvalidated user input. Instead, use
danielebarchiesi@0 604 * DrupalWebTestCase::randomString().
danielebarchiesi@0 605 *
danielebarchiesi@0 606 * @param $length
danielebarchiesi@0 607 * Length of random string to generate.
danielebarchiesi@0 608 *
danielebarchiesi@0 609 * @return
danielebarchiesi@0 610 * Randomly generated string.
danielebarchiesi@0 611 *
danielebarchiesi@0 612 * @see DrupalWebTestCase::randomString()
danielebarchiesi@0 613 */
danielebarchiesi@0 614 public static function randomName($length = 8) {
danielebarchiesi@0 615 $values = array_merge(range(65, 90), range(97, 122), range(48, 57));
danielebarchiesi@0 616 $max = count($values) - 1;
danielebarchiesi@0 617 $str = chr(mt_rand(97, 122));
danielebarchiesi@0 618 for ($i = 1; $i < $length; $i++) {
danielebarchiesi@0 619 $str .= chr($values[mt_rand(0, $max)]);
danielebarchiesi@0 620 }
danielebarchiesi@0 621 return $str;
danielebarchiesi@0 622 }
danielebarchiesi@0 623
danielebarchiesi@0 624 /**
danielebarchiesi@0 625 * Converts a list of possible parameters into a stack of permutations.
danielebarchiesi@0 626 *
danielebarchiesi@0 627 * Takes a list of parameters containing possible values, and converts all of
danielebarchiesi@0 628 * them into a list of items containing every possible permutation.
danielebarchiesi@0 629 *
danielebarchiesi@0 630 * Example:
danielebarchiesi@0 631 * @code
danielebarchiesi@0 632 * $parameters = array(
danielebarchiesi@0 633 * 'one' => array(0, 1),
danielebarchiesi@0 634 * 'two' => array(2, 3),
danielebarchiesi@0 635 * );
danielebarchiesi@0 636 * $permutations = DrupalTestCase::generatePermutations($parameters)
danielebarchiesi@0 637 * // Result:
danielebarchiesi@0 638 * $permutations == array(
danielebarchiesi@0 639 * array('one' => 0, 'two' => 2),
danielebarchiesi@0 640 * array('one' => 1, 'two' => 2),
danielebarchiesi@0 641 * array('one' => 0, 'two' => 3),
danielebarchiesi@0 642 * array('one' => 1, 'two' => 3),
danielebarchiesi@0 643 * )
danielebarchiesi@0 644 * @endcode
danielebarchiesi@0 645 *
danielebarchiesi@0 646 * @param $parameters
danielebarchiesi@0 647 * An associative array of parameters, keyed by parameter name, and whose
danielebarchiesi@0 648 * values are arrays of parameter values.
danielebarchiesi@0 649 *
danielebarchiesi@0 650 * @return
danielebarchiesi@0 651 * A list of permutations, which is an array of arrays. Each inner array
danielebarchiesi@0 652 * contains the full list of parameters that have been passed, but with a
danielebarchiesi@0 653 * single value only.
danielebarchiesi@0 654 */
danielebarchiesi@0 655 public static function generatePermutations($parameters) {
danielebarchiesi@0 656 $all_permutations = array(array());
danielebarchiesi@0 657 foreach ($parameters as $parameter => $values) {
danielebarchiesi@0 658 $new_permutations = array();
danielebarchiesi@0 659 // Iterate over all values of the parameter.
danielebarchiesi@0 660 foreach ($values as $value) {
danielebarchiesi@0 661 // Iterate over all existing permutations.
danielebarchiesi@0 662 foreach ($all_permutations as $permutation) {
danielebarchiesi@0 663 // Add the new parameter value to existing permutations.
danielebarchiesi@0 664 $new_permutations[] = $permutation + array($parameter => $value);
danielebarchiesi@0 665 }
danielebarchiesi@0 666 }
danielebarchiesi@0 667 // Replace the old permutations with the new permutations.
danielebarchiesi@0 668 $all_permutations = $new_permutations;
danielebarchiesi@0 669 }
danielebarchiesi@0 670 return $all_permutations;
danielebarchiesi@0 671 }
danielebarchiesi@0 672 }
danielebarchiesi@0 673
danielebarchiesi@0 674 /**
danielebarchiesi@0 675 * Test case for Drupal unit tests.
danielebarchiesi@0 676 *
danielebarchiesi@0 677 * These tests can not access the database nor files. Calling any Drupal
danielebarchiesi@0 678 * function that needs the database will throw exceptions. These include
danielebarchiesi@0 679 * watchdog(), module_implements(), module_invoke_all() etc.
danielebarchiesi@0 680 */
danielebarchiesi@0 681 class DrupalUnitTestCase extends DrupalTestCase {
danielebarchiesi@0 682
danielebarchiesi@0 683 /**
danielebarchiesi@0 684 * Constructor for DrupalUnitTestCase.
danielebarchiesi@0 685 */
danielebarchiesi@0 686 function __construct($test_id = NULL) {
danielebarchiesi@0 687 parent::__construct($test_id);
danielebarchiesi@0 688 $this->skipClasses[__CLASS__] = TRUE;
danielebarchiesi@0 689 }
danielebarchiesi@0 690
danielebarchiesi@0 691 /**
danielebarchiesi@0 692 * Sets up unit test environment.
danielebarchiesi@0 693 *
danielebarchiesi@0 694 * Unlike DrupalWebTestCase::setUp(), DrupalUnitTestCase::setUp() does not
danielebarchiesi@0 695 * install modules because tests are performed without accessing the database.
danielebarchiesi@0 696 * Any required files must be explicitly included by the child class setUp()
danielebarchiesi@0 697 * method.
danielebarchiesi@0 698 */
danielebarchiesi@0 699 protected function setUp() {
danielebarchiesi@0 700 global $conf;
danielebarchiesi@0 701
danielebarchiesi@0 702 // Store necessary current values before switching to the test environment.
danielebarchiesi@0 703 $this->originalFileDirectory = variable_get('file_public_path', conf_path() . '/files');
danielebarchiesi@0 704
danielebarchiesi@0 705 // Reset all statics so that test is performed with a clean environment.
danielebarchiesi@0 706 drupal_static_reset();
danielebarchiesi@0 707
danielebarchiesi@0 708 // Generate temporary prefixed database to ensure that tests have a clean starting point.
danielebarchiesi@0 709 $this->databasePrefix = Database::getConnection()->prefixTables('{simpletest' . mt_rand(1000, 1000000) . '}');
danielebarchiesi@0 710
danielebarchiesi@0 711 // Create test directory.
danielebarchiesi@0 712 $public_files_directory = $this->originalFileDirectory . '/simpletest/' . substr($this->databasePrefix, 10);
danielebarchiesi@0 713 file_prepare_directory($public_files_directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
danielebarchiesi@0 714 $conf['file_public_path'] = $public_files_directory;
danielebarchiesi@0 715
danielebarchiesi@0 716 // Clone the current connection and replace the current prefix.
danielebarchiesi@0 717 $connection_info = Database::getConnectionInfo('default');
danielebarchiesi@0 718 Database::renameConnection('default', 'simpletest_original_default');
danielebarchiesi@0 719 foreach ($connection_info as $target => $value) {
danielebarchiesi@0 720 $connection_info[$target]['prefix'] = array(
danielebarchiesi@0 721 'default' => $value['prefix']['default'] . $this->databasePrefix,
danielebarchiesi@0 722 );
danielebarchiesi@0 723 }
danielebarchiesi@0 724 Database::addConnectionInfo('default', 'default', $connection_info['default']);
danielebarchiesi@0 725
danielebarchiesi@0 726 // Set user agent to be consistent with web test case.
danielebarchiesi@0 727 $_SERVER['HTTP_USER_AGENT'] = $this->databasePrefix;
danielebarchiesi@0 728
danielebarchiesi@0 729 // If locale is enabled then t() will try to access the database and
danielebarchiesi@0 730 // subsequently will fail as the database is not accessible.
danielebarchiesi@0 731 $module_list = module_list();
danielebarchiesi@0 732 if (isset($module_list['locale'])) {
danielebarchiesi@0 733 $this->originalModuleList = $module_list;
danielebarchiesi@0 734 unset($module_list['locale']);
danielebarchiesi@0 735 module_list(TRUE, FALSE, FALSE, $module_list);
danielebarchiesi@0 736 }
danielebarchiesi@0 737 $this->setup = TRUE;
danielebarchiesi@0 738 }
danielebarchiesi@0 739
danielebarchiesi@0 740 protected function tearDown() {
danielebarchiesi@0 741 global $conf;
danielebarchiesi@0 742
danielebarchiesi@0 743 // Get back to the original connection.
danielebarchiesi@0 744 Database::removeConnection('default');
danielebarchiesi@0 745 Database::renameConnection('simpletest_original_default', 'default');
danielebarchiesi@0 746
danielebarchiesi@0 747 $conf['file_public_path'] = $this->originalFileDirectory;
danielebarchiesi@0 748 // Restore modules if necessary.
danielebarchiesi@0 749 if (isset($this->originalModuleList)) {
danielebarchiesi@0 750 module_list(TRUE, FALSE, FALSE, $this->originalModuleList);
danielebarchiesi@0 751 }
danielebarchiesi@0 752 }
danielebarchiesi@0 753 }
danielebarchiesi@0 754
danielebarchiesi@0 755 /**
danielebarchiesi@0 756 * Test case for typical Drupal tests.
danielebarchiesi@0 757 */
danielebarchiesi@0 758 class DrupalWebTestCase extends DrupalTestCase {
danielebarchiesi@0 759 /**
danielebarchiesi@0 760 * The profile to install as a basis for testing.
danielebarchiesi@0 761 *
danielebarchiesi@0 762 * @var string
danielebarchiesi@0 763 */
danielebarchiesi@0 764 protected $profile = 'standard';
danielebarchiesi@0 765
danielebarchiesi@0 766 /**
danielebarchiesi@0 767 * The URL currently loaded in the internal browser.
danielebarchiesi@0 768 *
danielebarchiesi@0 769 * @var string
danielebarchiesi@0 770 */
danielebarchiesi@0 771 protected $url;
danielebarchiesi@0 772
danielebarchiesi@0 773 /**
danielebarchiesi@0 774 * The handle of the current cURL connection.
danielebarchiesi@0 775 *
danielebarchiesi@0 776 * @var resource
danielebarchiesi@0 777 */
danielebarchiesi@0 778 protected $curlHandle;
danielebarchiesi@0 779
danielebarchiesi@0 780 /**
danielebarchiesi@0 781 * The headers of the page currently loaded in the internal browser.
danielebarchiesi@0 782 *
danielebarchiesi@0 783 * @var Array
danielebarchiesi@0 784 */
danielebarchiesi@0 785 protected $headers;
danielebarchiesi@0 786
danielebarchiesi@0 787 /**
danielebarchiesi@0 788 * The content of the page currently loaded in the internal browser.
danielebarchiesi@0 789 *
danielebarchiesi@0 790 * @var string
danielebarchiesi@0 791 */
danielebarchiesi@0 792 protected $content;
danielebarchiesi@0 793
danielebarchiesi@0 794 /**
danielebarchiesi@0 795 * The content of the page currently loaded in the internal browser (plain text version).
danielebarchiesi@0 796 *
danielebarchiesi@0 797 * @var string
danielebarchiesi@0 798 */
danielebarchiesi@0 799 protected $plainTextContent;
danielebarchiesi@0 800
danielebarchiesi@0 801 /**
danielebarchiesi@0 802 * The value of the Drupal.settings JavaScript variable for the page currently loaded in the internal browser.
danielebarchiesi@0 803 *
danielebarchiesi@0 804 * @var Array
danielebarchiesi@0 805 */
danielebarchiesi@0 806 protected $drupalSettings;
danielebarchiesi@0 807
danielebarchiesi@0 808 /**
danielebarchiesi@0 809 * The parsed version of the page.
danielebarchiesi@0 810 *
danielebarchiesi@0 811 * @var SimpleXMLElement
danielebarchiesi@0 812 */
danielebarchiesi@0 813 protected $elements = NULL;
danielebarchiesi@0 814
danielebarchiesi@0 815 /**
danielebarchiesi@0 816 * The current user logged in using the internal browser.
danielebarchiesi@0 817 *
danielebarchiesi@0 818 * @var bool
danielebarchiesi@0 819 */
danielebarchiesi@0 820 protected $loggedInUser = FALSE;
danielebarchiesi@0 821
danielebarchiesi@0 822 /**
danielebarchiesi@0 823 * The current cookie file used by cURL.
danielebarchiesi@0 824 *
danielebarchiesi@0 825 * We do not reuse the cookies in further runs, so we do not need a file
danielebarchiesi@0 826 * but we still need cookie handling, so we set the jar to NULL.
danielebarchiesi@0 827 */
danielebarchiesi@0 828 protected $cookieFile = NULL;
danielebarchiesi@0 829
danielebarchiesi@0 830 /**
danielebarchiesi@0 831 * Additional cURL options.
danielebarchiesi@0 832 *
danielebarchiesi@0 833 * DrupalWebTestCase itself never sets this but always obeys what is set.
danielebarchiesi@0 834 */
danielebarchiesi@0 835 protected $additionalCurlOptions = array();
danielebarchiesi@0 836
danielebarchiesi@0 837 /**
danielebarchiesi@0 838 * The original user, before it was changed to a clean uid = 1 for testing purposes.
danielebarchiesi@0 839 *
danielebarchiesi@0 840 * @var object
danielebarchiesi@0 841 */
danielebarchiesi@0 842 protected $originalUser = NULL;
danielebarchiesi@0 843
danielebarchiesi@0 844 /**
danielebarchiesi@0 845 * The original shutdown handlers array, before it was cleaned for testing purposes.
danielebarchiesi@0 846 *
danielebarchiesi@0 847 * @var array
danielebarchiesi@0 848 */
danielebarchiesi@0 849 protected $originalShutdownCallbacks = array();
danielebarchiesi@0 850
danielebarchiesi@0 851 /**
danielebarchiesi@0 852 * HTTP authentication method
danielebarchiesi@0 853 */
danielebarchiesi@0 854 protected $httpauth_method = CURLAUTH_BASIC;
danielebarchiesi@0 855
danielebarchiesi@0 856 /**
danielebarchiesi@0 857 * HTTP authentication credentials (<username>:<password>).
danielebarchiesi@0 858 */
danielebarchiesi@0 859 protected $httpauth_credentials = NULL;
danielebarchiesi@0 860
danielebarchiesi@0 861 /**
danielebarchiesi@0 862 * The current session name, if available.
danielebarchiesi@0 863 */
danielebarchiesi@0 864 protected $session_name = NULL;
danielebarchiesi@0 865
danielebarchiesi@0 866 /**
danielebarchiesi@0 867 * The current session ID, if available.
danielebarchiesi@0 868 */
danielebarchiesi@0 869 protected $session_id = NULL;
danielebarchiesi@0 870
danielebarchiesi@0 871 /**
danielebarchiesi@0 872 * Whether the files were copied to the test files directory.
danielebarchiesi@0 873 */
danielebarchiesi@0 874 protected $generatedTestFiles = FALSE;
danielebarchiesi@0 875
danielebarchiesi@0 876 /**
danielebarchiesi@0 877 * The number of redirects followed during the handling of a request.
danielebarchiesi@0 878 */
danielebarchiesi@0 879 protected $redirect_count;
danielebarchiesi@0 880
danielebarchiesi@0 881 /**
danielebarchiesi@0 882 * Constructor for DrupalWebTestCase.
danielebarchiesi@0 883 */
danielebarchiesi@0 884 function __construct($test_id = NULL) {
danielebarchiesi@0 885 parent::__construct($test_id);
danielebarchiesi@0 886 $this->skipClasses[__CLASS__] = TRUE;
danielebarchiesi@0 887 }
danielebarchiesi@0 888
danielebarchiesi@0 889 /**
danielebarchiesi@0 890 * Get a node from the database based on its title.
danielebarchiesi@0 891 *
danielebarchiesi@0 892 * @param $title
danielebarchiesi@0 893 * A node title, usually generated by $this->randomName().
danielebarchiesi@0 894 * @param $reset
danielebarchiesi@0 895 * (optional) Whether to reset the internal node_load() cache.
danielebarchiesi@0 896 *
danielebarchiesi@0 897 * @return
danielebarchiesi@0 898 * A node object matching $title.
danielebarchiesi@0 899 */
danielebarchiesi@0 900 function drupalGetNodeByTitle($title, $reset = FALSE) {
danielebarchiesi@0 901 $nodes = node_load_multiple(array(), array('title' => $title), $reset);
danielebarchiesi@0 902 // Load the first node returned from the database.
danielebarchiesi@0 903 $returned_node = reset($nodes);
danielebarchiesi@0 904 return $returned_node;
danielebarchiesi@0 905 }
danielebarchiesi@0 906
danielebarchiesi@0 907 /**
danielebarchiesi@0 908 * Creates a node based on default settings.
danielebarchiesi@0 909 *
danielebarchiesi@0 910 * @param $settings
danielebarchiesi@0 911 * An associative array of settings to change from the defaults, keys are
danielebarchiesi@0 912 * node properties, for example 'title' => 'Hello, world!'.
danielebarchiesi@0 913 * @return
danielebarchiesi@0 914 * Created node object.
danielebarchiesi@0 915 */
danielebarchiesi@0 916 protected function drupalCreateNode($settings = array()) {
danielebarchiesi@0 917 // Populate defaults array.
danielebarchiesi@0 918 $settings += array(
danielebarchiesi@0 919 'body' => array(LANGUAGE_NONE => array(array())),
danielebarchiesi@0 920 'title' => $this->randomName(8),
danielebarchiesi@0 921 'comment' => 2,
danielebarchiesi@0 922 'changed' => REQUEST_TIME,
danielebarchiesi@0 923 'moderate' => 0,
danielebarchiesi@0 924 'promote' => 0,
danielebarchiesi@0 925 'revision' => 1,
danielebarchiesi@0 926 'log' => '',
danielebarchiesi@0 927 'status' => 1,
danielebarchiesi@0 928 'sticky' => 0,
danielebarchiesi@0 929 'type' => 'page',
danielebarchiesi@0 930 'revisions' => NULL,
danielebarchiesi@0 931 'language' => LANGUAGE_NONE,
danielebarchiesi@0 932 );
danielebarchiesi@0 933
danielebarchiesi@0 934 // Use the original node's created time for existing nodes.
danielebarchiesi@0 935 if (isset($settings['created']) && !isset($settings['date'])) {
danielebarchiesi@0 936 $settings['date'] = format_date($settings['created'], 'custom', 'Y-m-d H:i:s O');
danielebarchiesi@0 937 }
danielebarchiesi@0 938
danielebarchiesi@0 939 // If the node's user uid is not specified manually, use the currently
danielebarchiesi@0 940 // logged in user if available, or else the user running the test.
danielebarchiesi@0 941 if (!isset($settings['uid'])) {
danielebarchiesi@0 942 if ($this->loggedInUser) {
danielebarchiesi@0 943 $settings['uid'] = $this->loggedInUser->uid;
danielebarchiesi@0 944 }
danielebarchiesi@0 945 else {
danielebarchiesi@0 946 global $user;
danielebarchiesi@0 947 $settings['uid'] = $user->uid;
danielebarchiesi@0 948 }
danielebarchiesi@0 949 }
danielebarchiesi@0 950
danielebarchiesi@0 951 // Merge body field value and format separately.
danielebarchiesi@0 952 $body = array(
danielebarchiesi@0 953 'value' => $this->randomName(32),
danielebarchiesi@0 954 'format' => filter_default_format(),
danielebarchiesi@0 955 );
danielebarchiesi@0 956 $settings['body'][$settings['language']][0] += $body;
danielebarchiesi@0 957
danielebarchiesi@0 958 $node = (object) $settings;
danielebarchiesi@0 959 node_save($node);
danielebarchiesi@0 960
danielebarchiesi@0 961 // Small hack to link revisions to our test user.
danielebarchiesi@0 962 db_update('node_revision')
danielebarchiesi@0 963 ->fields(array('uid' => $node->uid))
danielebarchiesi@0 964 ->condition('vid', $node->vid)
danielebarchiesi@0 965 ->execute();
danielebarchiesi@0 966 return $node;
danielebarchiesi@0 967 }
danielebarchiesi@0 968
danielebarchiesi@0 969 /**
danielebarchiesi@0 970 * Creates a custom content type based on default settings.
danielebarchiesi@0 971 *
danielebarchiesi@0 972 * @param $settings
danielebarchiesi@0 973 * An array of settings to change from the defaults.
danielebarchiesi@0 974 * Example: 'type' => 'foo'.
danielebarchiesi@0 975 * @return
danielebarchiesi@0 976 * Created content type.
danielebarchiesi@0 977 */
danielebarchiesi@0 978 protected function drupalCreateContentType($settings = array()) {
danielebarchiesi@0 979 // Find a non-existent random type name.
danielebarchiesi@0 980 do {
danielebarchiesi@0 981 $name = strtolower($this->randomName(8));
danielebarchiesi@0 982 } while (node_type_get_type($name));
danielebarchiesi@0 983
danielebarchiesi@0 984 // Populate defaults array.
danielebarchiesi@0 985 $defaults = array(
danielebarchiesi@0 986 'type' => $name,
danielebarchiesi@0 987 'name' => $name,
danielebarchiesi@0 988 'base' => 'node_content',
danielebarchiesi@0 989 'description' => '',
danielebarchiesi@0 990 'help' => '',
danielebarchiesi@0 991 'title_label' => 'Title',
danielebarchiesi@0 992 'body_label' => 'Body',
danielebarchiesi@0 993 'has_title' => 1,
danielebarchiesi@0 994 'has_body' => 1,
danielebarchiesi@0 995 );
danielebarchiesi@0 996 // Imposed values for a custom type.
danielebarchiesi@0 997 $forced = array(
danielebarchiesi@0 998 'orig_type' => '',
danielebarchiesi@0 999 'old_type' => '',
danielebarchiesi@0 1000 'module' => 'node',
danielebarchiesi@0 1001 'custom' => 1,
danielebarchiesi@0 1002 'modified' => 1,
danielebarchiesi@0 1003 'locked' => 0,
danielebarchiesi@0 1004 );
danielebarchiesi@0 1005 $type = $forced + $settings + $defaults;
danielebarchiesi@0 1006 $type = (object) $type;
danielebarchiesi@0 1007
danielebarchiesi@0 1008 $saved_type = node_type_save($type);
danielebarchiesi@0 1009 node_types_rebuild();
danielebarchiesi@0 1010 menu_rebuild();
danielebarchiesi@0 1011 node_add_body_field($type);
danielebarchiesi@0 1012
danielebarchiesi@0 1013 $this->assertEqual($saved_type, SAVED_NEW, t('Created content type %type.', array('%type' => $type->type)));
danielebarchiesi@0 1014
danielebarchiesi@0 1015 // Reset permissions so that permissions for this content type are available.
danielebarchiesi@0 1016 $this->checkPermissions(array(), TRUE);
danielebarchiesi@0 1017
danielebarchiesi@0 1018 return $type;
danielebarchiesi@0 1019 }
danielebarchiesi@0 1020
danielebarchiesi@0 1021 /**
danielebarchiesi@0 1022 * Get a list files that can be used in tests.
danielebarchiesi@0 1023 *
danielebarchiesi@0 1024 * @param $type
danielebarchiesi@0 1025 * File type, possible values: 'binary', 'html', 'image', 'javascript', 'php', 'sql', 'text'.
danielebarchiesi@0 1026 * @param $size
danielebarchiesi@0 1027 * File size in bytes to match. Please check the tests/files folder.
danielebarchiesi@0 1028 * @return
danielebarchiesi@0 1029 * List of files that match filter.
danielebarchiesi@0 1030 */
danielebarchiesi@0 1031 protected function drupalGetTestFiles($type, $size = NULL) {
danielebarchiesi@0 1032 if (empty($this->generatedTestFiles)) {
danielebarchiesi@0 1033 // Generate binary test files.
danielebarchiesi@0 1034 $lines = array(64, 1024);
danielebarchiesi@0 1035 $count = 0;
danielebarchiesi@0 1036 foreach ($lines as $line) {
danielebarchiesi@0 1037 simpletest_generate_file('binary-' . $count++, 64, $line, 'binary');
danielebarchiesi@0 1038 }
danielebarchiesi@0 1039
danielebarchiesi@0 1040 // Generate text test files.
danielebarchiesi@0 1041 $lines = array(16, 256, 1024, 2048, 20480);
danielebarchiesi@0 1042 $count = 0;
danielebarchiesi@0 1043 foreach ($lines as $line) {
danielebarchiesi@0 1044 simpletest_generate_file('text-' . $count++, 64, $line);
danielebarchiesi@0 1045 }
danielebarchiesi@0 1046
danielebarchiesi@0 1047 // Copy other test files from simpletest.
danielebarchiesi@0 1048 $original = drupal_get_path('module', 'simpletest') . '/files';
danielebarchiesi@0 1049 $files = file_scan_directory($original, '/(html|image|javascript|php|sql)-.*/');
danielebarchiesi@0 1050 foreach ($files as $file) {
danielebarchiesi@0 1051 file_unmanaged_copy($file->uri, variable_get('file_public_path', conf_path() . '/files'));
danielebarchiesi@0 1052 }
danielebarchiesi@0 1053
danielebarchiesi@0 1054 $this->generatedTestFiles = TRUE;
danielebarchiesi@0 1055 }
danielebarchiesi@0 1056
danielebarchiesi@0 1057 $files = array();
danielebarchiesi@0 1058 // Make sure type is valid.
danielebarchiesi@0 1059 if (in_array($type, array('binary', 'html', 'image', 'javascript', 'php', 'sql', 'text'))) {
danielebarchiesi@0 1060 $files = file_scan_directory('public://', '/' . $type . '\-.*/');
danielebarchiesi@0 1061
danielebarchiesi@0 1062 // If size is set then remove any files that are not of that size.
danielebarchiesi@0 1063 if ($size !== NULL) {
danielebarchiesi@0 1064 foreach ($files as $file) {
danielebarchiesi@0 1065 $stats = stat($file->uri);
danielebarchiesi@0 1066 if ($stats['size'] != $size) {
danielebarchiesi@0 1067 unset($files[$file->uri]);
danielebarchiesi@0 1068 }
danielebarchiesi@0 1069 }
danielebarchiesi@0 1070 }
danielebarchiesi@0 1071 }
danielebarchiesi@0 1072 usort($files, array($this, 'drupalCompareFiles'));
danielebarchiesi@0 1073 return $files;
danielebarchiesi@0 1074 }
danielebarchiesi@0 1075
danielebarchiesi@0 1076 /**
danielebarchiesi@0 1077 * Compare two files based on size and file name.
danielebarchiesi@0 1078 */
danielebarchiesi@0 1079 protected function drupalCompareFiles($file1, $file2) {
danielebarchiesi@0 1080 $compare_size = filesize($file1->uri) - filesize($file2->uri);
danielebarchiesi@0 1081 if ($compare_size) {
danielebarchiesi@0 1082 // Sort by file size.
danielebarchiesi@0 1083 return $compare_size;
danielebarchiesi@0 1084 }
danielebarchiesi@0 1085 else {
danielebarchiesi@0 1086 // The files were the same size, so sort alphabetically.
danielebarchiesi@0 1087 return strnatcmp($file1->name, $file2->name);
danielebarchiesi@0 1088 }
danielebarchiesi@0 1089 }
danielebarchiesi@0 1090
danielebarchiesi@0 1091 /**
danielebarchiesi@0 1092 * Create a user with a given set of permissions.
danielebarchiesi@0 1093 *
danielebarchiesi@0 1094 * @param array $permissions
danielebarchiesi@0 1095 * Array of permission names to assign to user. Note that the user always
danielebarchiesi@0 1096 * has the default permissions derived from the "authenticated users" role.
danielebarchiesi@0 1097 *
danielebarchiesi@0 1098 * @return object|false
danielebarchiesi@0 1099 * A fully loaded user object with pass_raw property, or FALSE if account
danielebarchiesi@0 1100 * creation fails.
danielebarchiesi@0 1101 */
danielebarchiesi@0 1102 protected function drupalCreateUser(array $permissions = array()) {
danielebarchiesi@0 1103 // Create a role with the given permission set, if any.
danielebarchiesi@0 1104 $rid = FALSE;
danielebarchiesi@0 1105 if ($permissions) {
danielebarchiesi@0 1106 $rid = $this->drupalCreateRole($permissions);
danielebarchiesi@0 1107 if (!$rid) {
danielebarchiesi@0 1108 return FALSE;
danielebarchiesi@0 1109 }
danielebarchiesi@0 1110 }
danielebarchiesi@0 1111
danielebarchiesi@0 1112 // Create a user assigned to that role.
danielebarchiesi@0 1113 $edit = array();
danielebarchiesi@0 1114 $edit['name'] = $this->randomName();
danielebarchiesi@0 1115 $edit['mail'] = $edit['name'] . '@example.com';
danielebarchiesi@0 1116 $edit['pass'] = user_password();
danielebarchiesi@0 1117 $edit['status'] = 1;
danielebarchiesi@0 1118 if ($rid) {
danielebarchiesi@0 1119 $edit['roles'] = array($rid => $rid);
danielebarchiesi@0 1120 }
danielebarchiesi@0 1121
danielebarchiesi@0 1122 $account = user_save(drupal_anonymous_user(), $edit);
danielebarchiesi@0 1123
danielebarchiesi@0 1124 $this->assertTrue(!empty($account->uid), t('User created with name %name and pass %pass', array('%name' => $edit['name'], '%pass' => $edit['pass'])), t('User login'));
danielebarchiesi@0 1125 if (empty($account->uid)) {
danielebarchiesi@0 1126 return FALSE;
danielebarchiesi@0 1127 }
danielebarchiesi@0 1128
danielebarchiesi@0 1129 // Add the raw password so that we can log in as this user.
danielebarchiesi@0 1130 $account->pass_raw = $edit['pass'];
danielebarchiesi@0 1131 return $account;
danielebarchiesi@0 1132 }
danielebarchiesi@0 1133
danielebarchiesi@0 1134 /**
danielebarchiesi@0 1135 * Internal helper function; Create a role with specified permissions.
danielebarchiesi@0 1136 *
danielebarchiesi@0 1137 * @param $permissions
danielebarchiesi@0 1138 * Array of permission names to assign to role.
danielebarchiesi@0 1139 * @param $name
danielebarchiesi@0 1140 * (optional) String for the name of the role. Defaults to a random string.
danielebarchiesi@0 1141 * @return
danielebarchiesi@0 1142 * Role ID of newly created role, or FALSE if role creation failed.
danielebarchiesi@0 1143 */
danielebarchiesi@0 1144 protected function drupalCreateRole(array $permissions, $name = NULL) {
danielebarchiesi@0 1145 // Generate random name if it was not passed.
danielebarchiesi@0 1146 if (!$name) {
danielebarchiesi@0 1147 $name = $this->randomName();
danielebarchiesi@0 1148 }
danielebarchiesi@0 1149
danielebarchiesi@0 1150 // Check the all the permissions strings are valid.
danielebarchiesi@0 1151 if (!$this->checkPermissions($permissions)) {
danielebarchiesi@0 1152 return FALSE;
danielebarchiesi@0 1153 }
danielebarchiesi@0 1154
danielebarchiesi@0 1155 // Create new role.
danielebarchiesi@0 1156 $role = new stdClass();
danielebarchiesi@0 1157 $role->name = $name;
danielebarchiesi@0 1158 user_role_save($role);
danielebarchiesi@0 1159 user_role_grant_permissions($role->rid, $permissions);
danielebarchiesi@0 1160
danielebarchiesi@0 1161 $this->assertTrue(isset($role->rid), t('Created role of name: @name, id: @rid', array('@name' => $name, '@rid' => (isset($role->rid) ? $role->rid : t('-n/a-')))), t('Role'));
danielebarchiesi@0 1162 if ($role && !empty($role->rid)) {
danielebarchiesi@0 1163 $count = db_query('SELECT COUNT(*) FROM {role_permission} WHERE rid = :rid', array(':rid' => $role->rid))->fetchField();
danielebarchiesi@0 1164 $this->assertTrue($count == count($permissions), t('Created permissions: @perms', array('@perms' => implode(', ', $permissions))), t('Role'));
danielebarchiesi@0 1165 return $role->rid;
danielebarchiesi@0 1166 }
danielebarchiesi@0 1167 else {
danielebarchiesi@0 1168 return FALSE;
danielebarchiesi@0 1169 }
danielebarchiesi@0 1170 }
danielebarchiesi@0 1171
danielebarchiesi@0 1172 /**
danielebarchiesi@0 1173 * Check to make sure that the array of permissions are valid.
danielebarchiesi@0 1174 *
danielebarchiesi@0 1175 * @param $permissions
danielebarchiesi@0 1176 * Permissions to check.
danielebarchiesi@0 1177 * @param $reset
danielebarchiesi@0 1178 * Reset cached available permissions.
danielebarchiesi@0 1179 * @return
danielebarchiesi@0 1180 * TRUE or FALSE depending on whether the permissions are valid.
danielebarchiesi@0 1181 */
danielebarchiesi@0 1182 protected function checkPermissions(array $permissions, $reset = FALSE) {
danielebarchiesi@0 1183 $available = &drupal_static(__FUNCTION__);
danielebarchiesi@0 1184
danielebarchiesi@0 1185 if (!isset($available) || $reset) {
danielebarchiesi@0 1186 $available = array_keys(module_invoke_all('permission'));
danielebarchiesi@0 1187 }
danielebarchiesi@0 1188
danielebarchiesi@0 1189 $valid = TRUE;
danielebarchiesi@0 1190 foreach ($permissions as $permission) {
danielebarchiesi@0 1191 if (!in_array($permission, $available)) {
danielebarchiesi@0 1192 $this->fail(t('Invalid permission %permission.', array('%permission' => $permission)), t('Role'));
danielebarchiesi@0 1193 $valid = FALSE;
danielebarchiesi@0 1194 }
danielebarchiesi@0 1195 }
danielebarchiesi@0 1196 return $valid;
danielebarchiesi@0 1197 }
danielebarchiesi@0 1198
danielebarchiesi@0 1199 /**
danielebarchiesi@0 1200 * Log in a user with the internal browser.
danielebarchiesi@0 1201 *
danielebarchiesi@0 1202 * If a user is already logged in, then the current user is logged out before
danielebarchiesi@0 1203 * logging in the specified user.
danielebarchiesi@0 1204 *
danielebarchiesi@0 1205 * Please note that neither the global $user nor the passed-in user object is
danielebarchiesi@0 1206 * populated with data of the logged in user. If you need full access to the
danielebarchiesi@0 1207 * user object after logging in, it must be updated manually. If you also need
danielebarchiesi@0 1208 * access to the plain-text password of the user (set by drupalCreateUser()),
danielebarchiesi@0 1209 * e.g. to log in the same user again, then it must be re-assigned manually.
danielebarchiesi@0 1210 * For example:
danielebarchiesi@0 1211 * @code
danielebarchiesi@0 1212 * // Create a user.
danielebarchiesi@0 1213 * $account = $this->drupalCreateUser(array());
danielebarchiesi@0 1214 * $this->drupalLogin($account);
danielebarchiesi@0 1215 * // Load real user object.
danielebarchiesi@0 1216 * $pass_raw = $account->pass_raw;
danielebarchiesi@0 1217 * $account = user_load($account->uid);
danielebarchiesi@0 1218 * $account->pass_raw = $pass_raw;
danielebarchiesi@0 1219 * @endcode
danielebarchiesi@0 1220 *
danielebarchiesi@0 1221 * @param $account
danielebarchiesi@0 1222 * User object representing the user to log in.
danielebarchiesi@0 1223 *
danielebarchiesi@0 1224 * @see drupalCreateUser()
danielebarchiesi@0 1225 */
danielebarchiesi@0 1226 protected function drupalLogin(stdClass $account) {
danielebarchiesi@0 1227 if ($this->loggedInUser) {
danielebarchiesi@0 1228 $this->drupalLogout();
danielebarchiesi@0 1229 }
danielebarchiesi@0 1230
danielebarchiesi@0 1231 $edit = array(
danielebarchiesi@0 1232 'name' => $account->name,
danielebarchiesi@0 1233 'pass' => $account->pass_raw
danielebarchiesi@0 1234 );
danielebarchiesi@0 1235 $this->drupalPost('user', $edit, t('Log in'));
danielebarchiesi@0 1236
danielebarchiesi@0 1237 // If a "log out" link appears on the page, it is almost certainly because
danielebarchiesi@0 1238 // the login was successful.
danielebarchiesi@0 1239 $pass = $this->assertLink(t('Log out'), 0, t('User %name successfully logged in.', array('%name' => $account->name)), t('User login'));
danielebarchiesi@0 1240
danielebarchiesi@0 1241 if ($pass) {
danielebarchiesi@0 1242 $this->loggedInUser = $account;
danielebarchiesi@0 1243 }
danielebarchiesi@0 1244 }
danielebarchiesi@0 1245
danielebarchiesi@0 1246 /**
danielebarchiesi@0 1247 * Generate a token for the currently logged in user.
danielebarchiesi@0 1248 */
danielebarchiesi@0 1249 protected function drupalGetToken($value = '') {
danielebarchiesi@0 1250 $private_key = drupal_get_private_key();
danielebarchiesi@0 1251 return drupal_hmac_base64($value, $this->session_id . $private_key);
danielebarchiesi@0 1252 }
danielebarchiesi@0 1253
danielebarchiesi@0 1254 /*
danielebarchiesi@0 1255 * Logs a user out of the internal browser, then check the login page to confirm logout.
danielebarchiesi@0 1256 */
danielebarchiesi@0 1257 protected function drupalLogout() {
danielebarchiesi@0 1258 // Make a request to the logout page, and redirect to the user page, the
danielebarchiesi@0 1259 // idea being if you were properly logged out you should be seeing a login
danielebarchiesi@0 1260 // screen.
danielebarchiesi@0 1261 $this->drupalGet('user/logout');
danielebarchiesi@0 1262 $this->drupalGet('user');
danielebarchiesi@0 1263 $pass = $this->assertField('name', t('Username field found.'), t('Logout'));
danielebarchiesi@0 1264 $pass = $pass && $this->assertField('pass', t('Password field found.'), t('Logout'));
danielebarchiesi@0 1265
danielebarchiesi@0 1266 if ($pass) {
danielebarchiesi@0 1267 $this->loggedInUser = FALSE;
danielebarchiesi@0 1268 }
danielebarchiesi@0 1269 }
danielebarchiesi@0 1270
danielebarchiesi@0 1271 /**
danielebarchiesi@0 1272 * Generates a database prefix for running tests.
danielebarchiesi@0 1273 *
danielebarchiesi@0 1274 * The generated database table prefix is used for the Drupal installation
danielebarchiesi@0 1275 * being performed for the test. It is also used as user agent HTTP header
danielebarchiesi@0 1276 * value by the cURL-based browser of DrupalWebTestCase, which is sent
danielebarchiesi@0 1277 * to the Drupal installation of the test. During early Drupal bootstrap, the
danielebarchiesi@0 1278 * user agent HTTP header is parsed, and if it matches, all database queries
danielebarchiesi@0 1279 * use the database table prefix that has been generated here.
danielebarchiesi@0 1280 *
danielebarchiesi@0 1281 * @see DrupalWebTestCase::curlInitialize()
danielebarchiesi@0 1282 * @see drupal_valid_test_ua()
danielebarchiesi@0 1283 * @see DrupalWebTestCase::setUp()
danielebarchiesi@0 1284 */
danielebarchiesi@0 1285 protected function prepareDatabasePrefix() {
danielebarchiesi@0 1286 $this->databasePrefix = 'simpletest' . mt_rand(1000, 1000000);
danielebarchiesi@0 1287
danielebarchiesi@0 1288 // As soon as the database prefix is set, the test might start to execute.
danielebarchiesi@0 1289 // All assertions as well as the SimpleTest batch operations are associated
danielebarchiesi@0 1290 // with the testId, so the database prefix has to be associated with it.
danielebarchiesi@0 1291 db_update('simpletest_test_id')
danielebarchiesi@0 1292 ->fields(array('last_prefix' => $this->databasePrefix))
danielebarchiesi@0 1293 ->condition('test_id', $this->testId)
danielebarchiesi@0 1294 ->execute();
danielebarchiesi@0 1295 }
danielebarchiesi@0 1296
danielebarchiesi@0 1297 /**
danielebarchiesi@0 1298 * Changes the database connection to the prefixed one.
danielebarchiesi@0 1299 *
danielebarchiesi@0 1300 * @see DrupalWebTestCase::setUp()
danielebarchiesi@0 1301 */
danielebarchiesi@0 1302 protected function changeDatabasePrefix() {
danielebarchiesi@0 1303 if (empty($this->databasePrefix)) {
danielebarchiesi@0 1304 $this->prepareDatabasePrefix();
danielebarchiesi@0 1305 // If $this->prepareDatabasePrefix() failed to work, return without
danielebarchiesi@0 1306 // setting $this->setupDatabasePrefix to TRUE, so setUp() methods will
danielebarchiesi@0 1307 // know to bail out.
danielebarchiesi@0 1308 if (empty($this->databasePrefix)) {
danielebarchiesi@0 1309 return;
danielebarchiesi@0 1310 }
danielebarchiesi@0 1311 }
danielebarchiesi@0 1312
danielebarchiesi@0 1313 // Clone the current connection and replace the current prefix.
danielebarchiesi@0 1314 $connection_info = Database::getConnectionInfo('default');
danielebarchiesi@0 1315 Database::renameConnection('default', 'simpletest_original_default');
danielebarchiesi@0 1316 foreach ($connection_info as $target => $value) {
danielebarchiesi@0 1317 $connection_info[$target]['prefix'] = array(
danielebarchiesi@0 1318 'default' => $value['prefix']['default'] . $this->databasePrefix,
danielebarchiesi@0 1319 );
danielebarchiesi@0 1320 }
danielebarchiesi@0 1321 Database::addConnectionInfo('default', 'default', $connection_info['default']);
danielebarchiesi@0 1322
danielebarchiesi@0 1323 // Indicate the database prefix was set up correctly.
danielebarchiesi@0 1324 $this->setupDatabasePrefix = TRUE;
danielebarchiesi@0 1325 }
danielebarchiesi@0 1326
danielebarchiesi@0 1327 /**
danielebarchiesi@0 1328 * Prepares the current environment for running the test.
danielebarchiesi@0 1329 *
danielebarchiesi@0 1330 * Backups various current environment variables and resets them, so they do
danielebarchiesi@0 1331 * not interfere with the Drupal site installation in which tests are executed
danielebarchiesi@0 1332 * and can be restored in tearDown().
danielebarchiesi@0 1333 *
danielebarchiesi@0 1334 * Also sets up new resources for the testing environment, such as the public
danielebarchiesi@0 1335 * filesystem and configuration directories.
danielebarchiesi@0 1336 *
danielebarchiesi@0 1337 * @see DrupalWebTestCase::setUp()
danielebarchiesi@0 1338 * @see DrupalWebTestCase::tearDown()
danielebarchiesi@0 1339 */
danielebarchiesi@0 1340 protected function prepareEnvironment() {
danielebarchiesi@0 1341 global $user, $language, $conf;
danielebarchiesi@0 1342
danielebarchiesi@0 1343 // Store necessary current values before switching to prefixed database.
danielebarchiesi@0 1344 $this->originalLanguage = $language;
danielebarchiesi@0 1345 $this->originalLanguageDefault = variable_get('language_default');
danielebarchiesi@0 1346 $this->originalFileDirectory = variable_get('file_public_path', conf_path() . '/files');
danielebarchiesi@0 1347 $this->originalProfile = drupal_get_profile();
danielebarchiesi@0 1348 $this->originalCleanUrl = variable_get('clean_url', 0);
danielebarchiesi@0 1349 $this->originalUser = $user;
danielebarchiesi@0 1350
danielebarchiesi@0 1351 // Set to English to prevent exceptions from utf8_truncate() from t()
danielebarchiesi@0 1352 // during install if the current language is not 'en'.
danielebarchiesi@0 1353 // The following array/object conversion is copied from language_default().
danielebarchiesi@0 1354 $language = (object) array('language' => 'en', 'name' => 'English', 'native' => 'English', 'direction' => 0, 'enabled' => 1, 'plurals' => 0, 'formula' => '', 'domain' => '', 'prefix' => '', 'weight' => 0, 'javascript' => '');
danielebarchiesi@0 1355
danielebarchiesi@0 1356 // Save and clean the shutdown callbacks array because it is static cached
danielebarchiesi@0 1357 // and will be changed by the test run. Otherwise it will contain callbacks
danielebarchiesi@0 1358 // from both environments and the testing environment will try to call the
danielebarchiesi@0 1359 // handlers defined by the original one.
danielebarchiesi@0 1360 $callbacks = &drupal_register_shutdown_function();
danielebarchiesi@0 1361 $this->originalShutdownCallbacks = $callbacks;
danielebarchiesi@0 1362 $callbacks = array();
danielebarchiesi@0 1363
danielebarchiesi@0 1364 // Create test directory ahead of installation so fatal errors and debug
danielebarchiesi@0 1365 // information can be logged during installation process.
danielebarchiesi@0 1366 // Use temporary files directory with the same prefix as the database.
danielebarchiesi@0 1367 $this->public_files_directory = $this->originalFileDirectory . '/simpletest/' . substr($this->databasePrefix, 10);
danielebarchiesi@0 1368 $this->private_files_directory = $this->public_files_directory . '/private';
danielebarchiesi@0 1369 $this->temp_files_directory = $this->private_files_directory . '/temp';
danielebarchiesi@0 1370
danielebarchiesi@0 1371 // Create the directories
danielebarchiesi@0 1372 file_prepare_directory($this->public_files_directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
danielebarchiesi@0 1373 file_prepare_directory($this->private_files_directory, FILE_CREATE_DIRECTORY);
danielebarchiesi@0 1374 file_prepare_directory($this->temp_files_directory, FILE_CREATE_DIRECTORY);
danielebarchiesi@0 1375 $this->generatedTestFiles = FALSE;
danielebarchiesi@0 1376
danielebarchiesi@0 1377 // Log fatal errors.
danielebarchiesi@0 1378 ini_set('log_errors', 1);
danielebarchiesi@0 1379 ini_set('error_log', $this->public_files_directory . '/error.log');
danielebarchiesi@0 1380
danielebarchiesi@0 1381 // Set the test information for use in other parts of Drupal.
danielebarchiesi@0 1382 $test_info = &$GLOBALS['drupal_test_info'];
danielebarchiesi@0 1383 $test_info['test_run_id'] = $this->databasePrefix;
danielebarchiesi@0 1384 $test_info['in_child_site'] = FALSE;
danielebarchiesi@0 1385
danielebarchiesi@0 1386 // Indicate the environment was set up correctly.
danielebarchiesi@0 1387 $this->setupEnvironment = TRUE;
danielebarchiesi@0 1388 }
danielebarchiesi@0 1389
danielebarchiesi@0 1390 /**
danielebarchiesi@0 1391 * Sets up a Drupal site for running functional and integration tests.
danielebarchiesi@0 1392 *
danielebarchiesi@0 1393 * Generates a random database prefix and installs Drupal with the specified
danielebarchiesi@0 1394 * installation profile in DrupalWebTestCase::$profile into the
danielebarchiesi@0 1395 * prefixed database. Afterwards, installs any additional modules specified by
danielebarchiesi@0 1396 * the test.
danielebarchiesi@0 1397 *
danielebarchiesi@0 1398 * After installation all caches are flushed and several configuration values
danielebarchiesi@0 1399 * are reset to the values of the parent site executing the test, since the
danielebarchiesi@0 1400 * default values may be incompatible with the environment in which tests are
danielebarchiesi@0 1401 * being executed.
danielebarchiesi@0 1402 *
danielebarchiesi@0 1403 * @param ...
danielebarchiesi@0 1404 * List of modules to enable for the duration of the test. This can be
danielebarchiesi@0 1405 * either a single array or a variable number of string arguments.
danielebarchiesi@0 1406 *
danielebarchiesi@0 1407 * @see DrupalWebTestCase::prepareDatabasePrefix()
danielebarchiesi@0 1408 * @see DrupalWebTestCase::changeDatabasePrefix()
danielebarchiesi@0 1409 * @see DrupalWebTestCase::prepareEnvironment()
danielebarchiesi@0 1410 */
danielebarchiesi@0 1411 protected function setUp() {
danielebarchiesi@0 1412 global $user, $language, $conf;
danielebarchiesi@0 1413
danielebarchiesi@0 1414 // Create the database prefix for this test.
danielebarchiesi@0 1415 $this->prepareDatabasePrefix();
danielebarchiesi@0 1416
danielebarchiesi@0 1417 // Prepare the environment for running tests.
danielebarchiesi@0 1418 $this->prepareEnvironment();
danielebarchiesi@0 1419 if (!$this->setupEnvironment) {
danielebarchiesi@0 1420 return FALSE;
danielebarchiesi@0 1421 }
danielebarchiesi@0 1422
danielebarchiesi@0 1423 // Reset all statics and variables to perform tests in a clean environment.
danielebarchiesi@0 1424 $conf = array();
danielebarchiesi@0 1425 drupal_static_reset();
danielebarchiesi@0 1426
danielebarchiesi@0 1427 // Change the database prefix.
danielebarchiesi@0 1428 // All static variables need to be reset before the database prefix is
danielebarchiesi@0 1429 // changed, since DrupalCacheArray implementations attempt to
danielebarchiesi@0 1430 // write back to persistent caches when they are destructed.
danielebarchiesi@0 1431 $this->changeDatabasePrefix();
danielebarchiesi@0 1432 if (!$this->setupDatabasePrefix) {
danielebarchiesi@0 1433 return FALSE;
danielebarchiesi@0 1434 }
danielebarchiesi@0 1435
danielebarchiesi@0 1436 // Preset the 'install_profile' system variable, so the first call into
danielebarchiesi@0 1437 // system_rebuild_module_data() (in drupal_install_system()) will register
danielebarchiesi@0 1438 // the test's profile as a module. Without this, the installation profile of
danielebarchiesi@0 1439 // the parent site (executing the test) is registered, and the test
danielebarchiesi@0 1440 // profile's hook_install() and other hook implementations are never invoked.
danielebarchiesi@0 1441 $conf['install_profile'] = $this->profile;
danielebarchiesi@0 1442
danielebarchiesi@0 1443 // Perform the actual Drupal installation.
danielebarchiesi@0 1444 include_once DRUPAL_ROOT . '/includes/install.inc';
danielebarchiesi@0 1445 drupal_install_system();
danielebarchiesi@0 1446
danielebarchiesi@0 1447 $this->preloadRegistry();
danielebarchiesi@0 1448
danielebarchiesi@0 1449 // Set path variables.
danielebarchiesi@0 1450 variable_set('file_public_path', $this->public_files_directory);
danielebarchiesi@0 1451 variable_set('file_private_path', $this->private_files_directory);
danielebarchiesi@0 1452 variable_set('file_temporary_path', $this->temp_files_directory);
danielebarchiesi@0 1453
danielebarchiesi@0 1454 // Set the 'simpletest_parent_profile' variable to add the parent profile's
danielebarchiesi@0 1455 // search path to the child site's search paths.
danielebarchiesi@0 1456 // @see drupal_system_listing()
danielebarchiesi@0 1457 // @todo This may need to be primed like 'install_profile' above.
danielebarchiesi@0 1458 variable_set('simpletest_parent_profile', $this->originalProfile);
danielebarchiesi@0 1459
danielebarchiesi@0 1460 // Include the testing profile.
danielebarchiesi@0 1461 variable_set('install_profile', $this->profile);
danielebarchiesi@0 1462 $profile_details = install_profile_info($this->profile, 'en');
danielebarchiesi@0 1463
danielebarchiesi@0 1464 // Install the modules specified by the testing profile.
danielebarchiesi@0 1465 module_enable($profile_details['dependencies'], FALSE);
danielebarchiesi@0 1466
danielebarchiesi@0 1467 // Install modules needed for this test. This could have been passed in as
danielebarchiesi@0 1468 // either a single array argument or a variable number of string arguments.
danielebarchiesi@0 1469 // @todo Remove this compatibility layer in Drupal 8, and only accept
danielebarchiesi@0 1470 // $modules as a single array argument.
danielebarchiesi@0 1471 $modules = func_get_args();
danielebarchiesi@0 1472 if (isset($modules[0]) && is_array($modules[0])) {
danielebarchiesi@0 1473 $modules = $modules[0];
danielebarchiesi@0 1474 }
danielebarchiesi@0 1475 if ($modules) {
danielebarchiesi@0 1476 $success = module_enable($modules, TRUE);
danielebarchiesi@0 1477 $this->assertTrue($success, t('Enabled modules: %modules', array('%modules' => implode(', ', $modules))));
danielebarchiesi@0 1478 }
danielebarchiesi@0 1479
danielebarchiesi@0 1480 // Run the profile tasks.
danielebarchiesi@0 1481 $install_profile_module_exists = db_query("SELECT 1 FROM {system} WHERE type = 'module' AND name = :name", array(
danielebarchiesi@0 1482 ':name' => $this->profile,
danielebarchiesi@0 1483 ))->fetchField();
danielebarchiesi@0 1484 if ($install_profile_module_exists) {
danielebarchiesi@0 1485 module_enable(array($this->profile), FALSE);
danielebarchiesi@0 1486 }
danielebarchiesi@0 1487
danielebarchiesi@0 1488 // Reset/rebuild all data structures after enabling the modules.
danielebarchiesi@0 1489 $this->resetAll();
danielebarchiesi@0 1490
danielebarchiesi@0 1491 // Run cron once in that environment, as install.php does at the end of
danielebarchiesi@0 1492 // the installation process.
danielebarchiesi@0 1493 drupal_cron_run();
danielebarchiesi@0 1494
danielebarchiesi@0 1495 // Ensure that the session is not written to the new environment and replace
danielebarchiesi@0 1496 // the global $user session with uid 1 from the new test site.
danielebarchiesi@0 1497 drupal_save_session(FALSE);
danielebarchiesi@0 1498 // Login as uid 1.
danielebarchiesi@0 1499 $user = user_load(1);
danielebarchiesi@0 1500
danielebarchiesi@0 1501 // Restore necessary variables.
danielebarchiesi@0 1502 variable_set('install_task', 'done');
danielebarchiesi@0 1503 variable_set('clean_url', $this->originalCleanUrl);
danielebarchiesi@0 1504 variable_set('site_mail', 'simpletest@example.com');
danielebarchiesi@0 1505 variable_set('date_default_timezone', date_default_timezone_get());
danielebarchiesi@0 1506
danielebarchiesi@0 1507 // Set up English language.
danielebarchiesi@0 1508 unset($conf['language_default']);
danielebarchiesi@0 1509 $language = language_default();
danielebarchiesi@0 1510
danielebarchiesi@0 1511 // Use the test mail class instead of the default mail handler class.
danielebarchiesi@0 1512 variable_set('mail_system', array('default-system' => 'TestingMailSystem'));
danielebarchiesi@0 1513
danielebarchiesi@0 1514 drupal_set_time_limit($this->timeLimit);
danielebarchiesi@0 1515 $this->setup = TRUE;
danielebarchiesi@0 1516 }
danielebarchiesi@0 1517
danielebarchiesi@0 1518 /**
danielebarchiesi@0 1519 * Preload the registry from the testing site.
danielebarchiesi@0 1520 *
danielebarchiesi@0 1521 * This method is called by DrupalWebTestCase::setUp(), and preloads the
danielebarchiesi@0 1522 * registry from the testing site to cut down on the time it takes to
danielebarchiesi@0 1523 * set up a clean environment for the current test run.
danielebarchiesi@0 1524 */
danielebarchiesi@0 1525 protected function preloadRegistry() {
danielebarchiesi@0 1526 // Use two separate queries, each with their own connections: copy the
danielebarchiesi@0 1527 // {registry} and {registry_file} tables over from the parent installation
danielebarchiesi@0 1528 // to the child installation.
danielebarchiesi@0 1529 $original_connection = Database::getConnection('default', 'simpletest_original_default');
danielebarchiesi@0 1530 $test_connection = Database::getConnection();
danielebarchiesi@0 1531
danielebarchiesi@0 1532 foreach (array('registry', 'registry_file') as $table) {
danielebarchiesi@0 1533 // Find the records from the parent database.
danielebarchiesi@0 1534 $source_query = $original_connection
danielebarchiesi@0 1535 ->select($table, array(), array('fetch' => PDO::FETCH_ASSOC))
danielebarchiesi@0 1536 ->fields($table);
danielebarchiesi@0 1537
danielebarchiesi@0 1538 $dest_query = $test_connection->insert($table);
danielebarchiesi@0 1539
danielebarchiesi@0 1540 $first = TRUE;
danielebarchiesi@0 1541 foreach ($source_query->execute() as $row) {
danielebarchiesi@0 1542 if ($first) {
danielebarchiesi@0 1543 $dest_query->fields(array_keys($row));
danielebarchiesi@0 1544 $first = FALSE;
danielebarchiesi@0 1545 }
danielebarchiesi@0 1546 // Insert the records into the child database.
danielebarchiesi@0 1547 $dest_query->values($row);
danielebarchiesi@0 1548 }
danielebarchiesi@0 1549
danielebarchiesi@0 1550 $dest_query->execute();
danielebarchiesi@0 1551 }
danielebarchiesi@0 1552 }
danielebarchiesi@0 1553
danielebarchiesi@0 1554 /**
danielebarchiesi@0 1555 * Reset all data structures after having enabled new modules.
danielebarchiesi@0 1556 *
danielebarchiesi@0 1557 * This method is called by DrupalWebTestCase::setUp() after enabling
danielebarchiesi@0 1558 * the requested modules. It must be called again when additional modules
danielebarchiesi@0 1559 * are enabled later.
danielebarchiesi@0 1560 */
danielebarchiesi@0 1561 protected function resetAll() {
danielebarchiesi@0 1562 // Reset all static variables.
danielebarchiesi@0 1563 drupal_static_reset();
danielebarchiesi@0 1564 // Reset the list of enabled modules.
danielebarchiesi@0 1565 module_list(TRUE);
danielebarchiesi@0 1566
danielebarchiesi@0 1567 // Reset cached schema for new database prefix. This must be done before
danielebarchiesi@0 1568 // drupal_flush_all_caches() so rebuilds can make use of the schema of
danielebarchiesi@0 1569 // modules enabled on the cURL side.
danielebarchiesi@0 1570 drupal_get_schema(NULL, TRUE);
danielebarchiesi@0 1571
danielebarchiesi@0 1572 // Perform rebuilds and flush remaining caches.
danielebarchiesi@0 1573 drupal_flush_all_caches();
danielebarchiesi@0 1574
danielebarchiesi@0 1575 // Reload global $conf array and permissions.
danielebarchiesi@0 1576 $this->refreshVariables();
danielebarchiesi@0 1577 $this->checkPermissions(array(), TRUE);
danielebarchiesi@0 1578 }
danielebarchiesi@0 1579
danielebarchiesi@0 1580 /**
danielebarchiesi@0 1581 * Refresh the in-memory set of variables. Useful after a page request is made
danielebarchiesi@0 1582 * that changes a variable in a different thread.
danielebarchiesi@0 1583 *
danielebarchiesi@0 1584 * In other words calling a settings page with $this->drupalPost() with a changed
danielebarchiesi@0 1585 * value would update a variable to reflect that change, but in the thread that
danielebarchiesi@0 1586 * made the call (thread running the test) the changed variable would not be
danielebarchiesi@0 1587 * picked up.
danielebarchiesi@0 1588 *
danielebarchiesi@0 1589 * This method clears the variables cache and loads a fresh copy from the database
danielebarchiesi@0 1590 * to ensure that the most up-to-date set of variables is loaded.
danielebarchiesi@0 1591 */
danielebarchiesi@0 1592 protected function refreshVariables() {
danielebarchiesi@0 1593 global $conf;
danielebarchiesi@0 1594 cache_clear_all('variables', 'cache_bootstrap');
danielebarchiesi@0 1595 $conf = variable_initialize();
danielebarchiesi@0 1596 }
danielebarchiesi@0 1597
danielebarchiesi@0 1598 /**
danielebarchiesi@0 1599 * Delete created files and temporary files directory, delete the tables created by setUp(),
danielebarchiesi@0 1600 * and reset the database prefix.
danielebarchiesi@0 1601 */
danielebarchiesi@0 1602 protected function tearDown() {
danielebarchiesi@0 1603 global $user, $language;
danielebarchiesi@0 1604
danielebarchiesi@0 1605 // In case a fatal error occurred that was not in the test process read the
danielebarchiesi@0 1606 // log to pick up any fatal errors.
danielebarchiesi@0 1607 simpletest_log_read($this->testId, $this->databasePrefix, get_class($this), TRUE);
danielebarchiesi@0 1608
danielebarchiesi@0 1609 $emailCount = count(variable_get('drupal_test_email_collector', array()));
danielebarchiesi@0 1610 if ($emailCount) {
danielebarchiesi@0 1611 $message = format_plural($emailCount, '1 e-mail was sent during this test.', '@count e-mails were sent during this test.');
danielebarchiesi@0 1612 $this->pass($message, t('E-mail'));
danielebarchiesi@0 1613 }
danielebarchiesi@0 1614
danielebarchiesi@0 1615 // Delete temporary files directory.
danielebarchiesi@0 1616 file_unmanaged_delete_recursive($this->originalFileDirectory . '/simpletest/' . substr($this->databasePrefix, 10));
danielebarchiesi@0 1617
danielebarchiesi@0 1618 // Remove all prefixed tables.
danielebarchiesi@0 1619 $tables = db_find_tables($this->databasePrefix . '%');
danielebarchiesi@0 1620 $connection_info = Database::getConnectionInfo('default');
danielebarchiesi@0 1621 $tables = db_find_tables($connection_info['default']['prefix']['default'] . '%');
danielebarchiesi@0 1622 if (empty($tables)) {
danielebarchiesi@0 1623 $this->fail('Failed to find test tables to drop.');
danielebarchiesi@0 1624 }
danielebarchiesi@0 1625 $prefix_length = strlen($connection_info['default']['prefix']['default']);
danielebarchiesi@0 1626 foreach ($tables as $table) {
danielebarchiesi@0 1627 if (db_drop_table(substr($table, $prefix_length))) {
danielebarchiesi@0 1628 unset($tables[$table]);
danielebarchiesi@0 1629 }
danielebarchiesi@0 1630 }
danielebarchiesi@0 1631 if (!empty($tables)) {
danielebarchiesi@0 1632 $this->fail('Failed to drop all prefixed tables.');
danielebarchiesi@0 1633 }
danielebarchiesi@0 1634
danielebarchiesi@0 1635 // Get back to the original connection.
danielebarchiesi@0 1636 Database::removeConnection('default');
danielebarchiesi@0 1637 Database::renameConnection('simpletest_original_default', 'default');
danielebarchiesi@0 1638
danielebarchiesi@0 1639 // Restore original shutdown callbacks array to prevent original
danielebarchiesi@0 1640 // environment of calling handlers from test run.
danielebarchiesi@0 1641 $callbacks = &drupal_register_shutdown_function();
danielebarchiesi@0 1642 $callbacks = $this->originalShutdownCallbacks;
danielebarchiesi@0 1643
danielebarchiesi@0 1644 // Return the user to the original one.
danielebarchiesi@0 1645 $user = $this->originalUser;
danielebarchiesi@0 1646 drupal_save_session(TRUE);
danielebarchiesi@0 1647
danielebarchiesi@0 1648 // Ensure that internal logged in variable and cURL options are reset.
danielebarchiesi@0 1649 $this->loggedInUser = FALSE;
danielebarchiesi@0 1650 $this->additionalCurlOptions = array();
danielebarchiesi@0 1651
danielebarchiesi@0 1652 // Reload module list and implementations to ensure that test module hooks
danielebarchiesi@0 1653 // aren't called after tests.
danielebarchiesi@0 1654 module_list(TRUE);
danielebarchiesi@0 1655 module_implements('', FALSE, TRUE);
danielebarchiesi@0 1656
danielebarchiesi@0 1657 // Reset the Field API.
danielebarchiesi@0 1658 field_cache_clear();
danielebarchiesi@0 1659
danielebarchiesi@0 1660 // Rebuild caches.
danielebarchiesi@0 1661 $this->refreshVariables();
danielebarchiesi@0 1662
danielebarchiesi@0 1663 // Reset public files directory.
danielebarchiesi@0 1664 $GLOBALS['conf']['file_public_path'] = $this->originalFileDirectory;
danielebarchiesi@0 1665
danielebarchiesi@0 1666 // Reset language.
danielebarchiesi@0 1667 $language = $this->originalLanguage;
danielebarchiesi@0 1668 if ($this->originalLanguageDefault) {
danielebarchiesi@0 1669 $GLOBALS['conf']['language_default'] = $this->originalLanguageDefault;
danielebarchiesi@0 1670 }
danielebarchiesi@0 1671
danielebarchiesi@0 1672 // Close the CURL handler.
danielebarchiesi@0 1673 $this->curlClose();
danielebarchiesi@0 1674 }
danielebarchiesi@0 1675
danielebarchiesi@0 1676 /**
danielebarchiesi@0 1677 * Initializes the cURL connection.
danielebarchiesi@0 1678 *
danielebarchiesi@0 1679 * If the simpletest_httpauth_credentials variable is set, this function will
danielebarchiesi@0 1680 * add HTTP authentication headers. This is necessary for testing sites that
danielebarchiesi@0 1681 * are protected by login credentials from public access.
danielebarchiesi@0 1682 * See the description of $curl_options for other options.
danielebarchiesi@0 1683 */
danielebarchiesi@0 1684 protected function curlInitialize() {
danielebarchiesi@0 1685 global $base_url;
danielebarchiesi@0 1686
danielebarchiesi@0 1687 if (!isset($this->curlHandle)) {
danielebarchiesi@0 1688 $this->curlHandle = curl_init();
danielebarchiesi@0 1689
danielebarchiesi@0 1690 // Some versions/configurations of cURL break on a NULL cookie jar, so
danielebarchiesi@0 1691 // supply a real file.
danielebarchiesi@0 1692 if (empty($this->cookieFile)) {
danielebarchiesi@0 1693 $this->cookieFile = $this->public_files_directory . '/cookie.jar';
danielebarchiesi@0 1694 }
danielebarchiesi@0 1695
danielebarchiesi@0 1696 $curl_options = array(
danielebarchiesi@0 1697 CURLOPT_COOKIEJAR => $this->cookieFile,
danielebarchiesi@0 1698 CURLOPT_URL => $base_url,
danielebarchiesi@0 1699 CURLOPT_FOLLOWLOCATION => FALSE,
danielebarchiesi@0 1700 CURLOPT_RETURNTRANSFER => TRUE,
danielebarchiesi@0 1701 CURLOPT_SSL_VERIFYPEER => FALSE, // Required to make the tests run on HTTPS.
danielebarchiesi@0 1702 CURLOPT_SSL_VERIFYHOST => FALSE, // Required to make the tests run on HTTPS.
danielebarchiesi@0 1703 CURLOPT_HEADERFUNCTION => array(&$this, 'curlHeaderCallback'),
danielebarchiesi@0 1704 CURLOPT_USERAGENT => $this->databasePrefix,
danielebarchiesi@0 1705 );
danielebarchiesi@0 1706 if (isset($this->httpauth_credentials)) {
danielebarchiesi@0 1707 $curl_options[CURLOPT_HTTPAUTH] = $this->httpauth_method;
danielebarchiesi@0 1708 $curl_options[CURLOPT_USERPWD] = $this->httpauth_credentials;
danielebarchiesi@0 1709 }
danielebarchiesi@0 1710 // curl_setopt_array() returns FALSE if any of the specified options
danielebarchiesi@0 1711 // cannot be set, and stops processing any further options.
danielebarchiesi@0 1712 $result = curl_setopt_array($this->curlHandle, $this->additionalCurlOptions + $curl_options);
danielebarchiesi@0 1713 if (!$result) {
danielebarchiesi@0 1714 throw new Exception('One or more cURL options could not be set.');
danielebarchiesi@0 1715 }
danielebarchiesi@0 1716
danielebarchiesi@0 1717 // By default, the child session name should be the same as the parent.
danielebarchiesi@0 1718 $this->session_name = session_name();
danielebarchiesi@0 1719 }
danielebarchiesi@0 1720 // We set the user agent header on each request so as to use the current
danielebarchiesi@0 1721 // time and a new uniqid.
danielebarchiesi@0 1722 if (preg_match('/simpletest\d+/', $this->databasePrefix, $matches)) {
danielebarchiesi@0 1723 curl_setopt($this->curlHandle, CURLOPT_USERAGENT, drupal_generate_test_ua($matches[0]));
danielebarchiesi@0 1724 }
danielebarchiesi@0 1725 }
danielebarchiesi@0 1726
danielebarchiesi@0 1727 /**
danielebarchiesi@0 1728 * Initializes and executes a cURL request.
danielebarchiesi@0 1729 *
danielebarchiesi@0 1730 * @param $curl_options
danielebarchiesi@0 1731 * An associative array of cURL options to set, where the keys are constants
danielebarchiesi@0 1732 * defined by the cURL library. For a list of valid options, see
danielebarchiesi@0 1733 * http://www.php.net/manual/function.curl-setopt.php
danielebarchiesi@0 1734 * @param $redirect
danielebarchiesi@0 1735 * FALSE if this is an initial request, TRUE if this request is the result
danielebarchiesi@0 1736 * of a redirect.
danielebarchiesi@0 1737 *
danielebarchiesi@0 1738 * @return
danielebarchiesi@0 1739 * The content returned from the call to curl_exec().
danielebarchiesi@0 1740 *
danielebarchiesi@0 1741 * @see curlInitialize()
danielebarchiesi@0 1742 */
danielebarchiesi@0 1743 protected function curlExec($curl_options, $redirect = FALSE) {
danielebarchiesi@0 1744 $this->curlInitialize();
danielebarchiesi@0 1745
danielebarchiesi@0 1746 // cURL incorrectly handles URLs with a fragment by including the
danielebarchiesi@0 1747 // fragment in the request to the server, causing some web servers
danielebarchiesi@0 1748 // to reject the request citing "400 - Bad Request". To prevent
danielebarchiesi@0 1749 // this, we strip the fragment from the request.
danielebarchiesi@0 1750 // TODO: Remove this for Drupal 8, since fixed in curl 7.20.0.
danielebarchiesi@0 1751 if (!empty($curl_options[CURLOPT_URL]) && strpos($curl_options[CURLOPT_URL], '#')) {
danielebarchiesi@0 1752 $original_url = $curl_options[CURLOPT_URL];
danielebarchiesi@0 1753 $curl_options[CURLOPT_URL] = strtok($curl_options[CURLOPT_URL], '#');
danielebarchiesi@0 1754 }
danielebarchiesi@0 1755
danielebarchiesi@0 1756 $url = empty($curl_options[CURLOPT_URL]) ? curl_getinfo($this->curlHandle, CURLINFO_EFFECTIVE_URL) : $curl_options[CURLOPT_URL];
danielebarchiesi@0 1757
danielebarchiesi@0 1758 if (!empty($curl_options[CURLOPT_POST])) {
danielebarchiesi@0 1759 // This is a fix for the Curl library to prevent Expect: 100-continue
danielebarchiesi@0 1760 // headers in POST requests, that may cause unexpected HTTP response
danielebarchiesi@0 1761 // codes from some webservers (like lighttpd that returns a 417 error
danielebarchiesi@0 1762 // code). It is done by setting an empty "Expect" header field that is
danielebarchiesi@0 1763 // not overwritten by Curl.
danielebarchiesi@0 1764 $curl_options[CURLOPT_HTTPHEADER][] = 'Expect:';
danielebarchiesi@0 1765 }
danielebarchiesi@0 1766 curl_setopt_array($this->curlHandle, $this->additionalCurlOptions + $curl_options);
danielebarchiesi@0 1767
danielebarchiesi@0 1768 if (!$redirect) {
danielebarchiesi@0 1769 // Reset headers, the session ID and the redirect counter.
danielebarchiesi@0 1770 $this->session_id = NULL;
danielebarchiesi@0 1771 $this->headers = array();
danielebarchiesi@0 1772 $this->redirect_count = 0;
danielebarchiesi@0 1773 }
danielebarchiesi@0 1774
danielebarchiesi@0 1775 $content = curl_exec($this->curlHandle);
danielebarchiesi@0 1776 $status = curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE);
danielebarchiesi@0 1777
danielebarchiesi@0 1778 // cURL incorrectly handles URLs with fragments, so instead of
danielebarchiesi@0 1779 // letting cURL handle redirects we take of them ourselves to
danielebarchiesi@0 1780 // to prevent fragments being sent to the web server as part
danielebarchiesi@0 1781 // of the request.
danielebarchiesi@0 1782 // TODO: Remove this for Drupal 8, since fixed in curl 7.20.0.
danielebarchiesi@0 1783 if (in_array($status, array(300, 301, 302, 303, 305, 307)) && $this->redirect_count < variable_get('simpletest_maximum_redirects', 5)) {
danielebarchiesi@0 1784 if ($this->drupalGetHeader('location')) {
danielebarchiesi@0 1785 $this->redirect_count++;
danielebarchiesi@0 1786 $curl_options = array();
danielebarchiesi@0 1787 $curl_options[CURLOPT_URL] = $this->drupalGetHeader('location');
danielebarchiesi@0 1788 $curl_options[CURLOPT_HTTPGET] = TRUE;
danielebarchiesi@0 1789 return $this->curlExec($curl_options, TRUE);
danielebarchiesi@0 1790 }
danielebarchiesi@0 1791 }
danielebarchiesi@0 1792
danielebarchiesi@0 1793 $this->drupalSetContent($content, isset($original_url) ? $original_url : curl_getinfo($this->curlHandle, CURLINFO_EFFECTIVE_URL));
danielebarchiesi@0 1794 $message_vars = array(
danielebarchiesi@0 1795 '!method' => !empty($curl_options[CURLOPT_NOBODY]) ? 'HEAD' : (empty($curl_options[CURLOPT_POSTFIELDS]) ? 'GET' : 'POST'),
danielebarchiesi@0 1796 '@url' => isset($original_url) ? $original_url : $url,
danielebarchiesi@0 1797 '@status' => $status,
danielebarchiesi@0 1798 '!length' => format_size(strlen($this->drupalGetContent()))
danielebarchiesi@0 1799 );
danielebarchiesi@0 1800 $message = t('!method @url returned @status (!length).', $message_vars);
danielebarchiesi@0 1801 $this->assertTrue($this->drupalGetContent() !== FALSE, $message, t('Browser'));
danielebarchiesi@0 1802 return $this->drupalGetContent();
danielebarchiesi@0 1803 }
danielebarchiesi@0 1804
danielebarchiesi@0 1805 /**
danielebarchiesi@0 1806 * Reads headers and registers errors received from the tested site.
danielebarchiesi@0 1807 *
danielebarchiesi@0 1808 * @see _drupal_log_error().
danielebarchiesi@0 1809 *
danielebarchiesi@0 1810 * @param $curlHandler
danielebarchiesi@0 1811 * The cURL handler.
danielebarchiesi@0 1812 * @param $header
danielebarchiesi@0 1813 * An header.
danielebarchiesi@0 1814 */
danielebarchiesi@0 1815 protected function curlHeaderCallback($curlHandler, $header) {
danielebarchiesi@0 1816 // Header fields can be extended over multiple lines by preceding each
danielebarchiesi@0 1817 // extra line with at least one SP or HT. They should be joined on receive.
danielebarchiesi@0 1818 // Details are in RFC2616 section 4.
danielebarchiesi@0 1819 if ($header[0] == ' ' || $header[0] == "\t") {
danielebarchiesi@0 1820 // Normalize whitespace between chucks.
danielebarchiesi@0 1821 $this->headers[] = array_pop($this->headers) . ' ' . trim($header);
danielebarchiesi@0 1822 }
danielebarchiesi@0 1823 else {
danielebarchiesi@0 1824 $this->headers[] = $header;
danielebarchiesi@0 1825 }
danielebarchiesi@0 1826
danielebarchiesi@0 1827 // Errors are being sent via X-Drupal-Assertion-* headers,
danielebarchiesi@0 1828 // generated by _drupal_log_error() in the exact form required
danielebarchiesi@0 1829 // by DrupalWebTestCase::error().
danielebarchiesi@0 1830 if (preg_match('/^X-Drupal-Assertion-[0-9]+: (.*)$/', $header, $matches)) {
danielebarchiesi@0 1831 // Call DrupalWebTestCase::error() with the parameters from the header.
danielebarchiesi@0 1832 call_user_func_array(array(&$this, 'error'), unserialize(urldecode($matches[1])));
danielebarchiesi@0 1833 }
danielebarchiesi@0 1834
danielebarchiesi@0 1835 // Save cookies.
danielebarchiesi@0 1836 if (preg_match('/^Set-Cookie: ([^=]+)=(.+)/', $header, $matches)) {
danielebarchiesi@0 1837 $name = $matches[1];
danielebarchiesi@0 1838 $parts = array_map('trim', explode(';', $matches[2]));
danielebarchiesi@0 1839 $value = array_shift($parts);
danielebarchiesi@0 1840 $this->cookies[$name] = array('value' => $value, 'secure' => in_array('secure', $parts));
danielebarchiesi@0 1841 if ($name == $this->session_name) {
danielebarchiesi@0 1842 if ($value != 'deleted') {
danielebarchiesi@0 1843 $this->session_id = $value;
danielebarchiesi@0 1844 }
danielebarchiesi@0 1845 else {
danielebarchiesi@0 1846 $this->session_id = NULL;
danielebarchiesi@0 1847 }
danielebarchiesi@0 1848 }
danielebarchiesi@0 1849 }
danielebarchiesi@0 1850
danielebarchiesi@0 1851 // This is required by cURL.
danielebarchiesi@0 1852 return strlen($header);
danielebarchiesi@0 1853 }
danielebarchiesi@0 1854
danielebarchiesi@0 1855 /**
danielebarchiesi@0 1856 * Close the cURL handler and unset the handler.
danielebarchiesi@0 1857 */
danielebarchiesi@0 1858 protected function curlClose() {
danielebarchiesi@0 1859 if (isset($this->curlHandle)) {
danielebarchiesi@0 1860 curl_close($this->curlHandle);
danielebarchiesi@0 1861 unset($this->curlHandle);
danielebarchiesi@0 1862 }
danielebarchiesi@0 1863 }
danielebarchiesi@0 1864
danielebarchiesi@0 1865 /**
danielebarchiesi@0 1866 * Parse content returned from curlExec using DOM and SimpleXML.
danielebarchiesi@0 1867 *
danielebarchiesi@0 1868 * @return
danielebarchiesi@0 1869 * A SimpleXMLElement or FALSE on failure.
danielebarchiesi@0 1870 */
danielebarchiesi@0 1871 protected function parse() {
danielebarchiesi@0 1872 if (!$this->elements) {
danielebarchiesi@0 1873 // DOM can load HTML soup. But, HTML soup can throw warnings, suppress
danielebarchiesi@0 1874 // them.
danielebarchiesi@0 1875 $htmlDom = new DOMDocument();
danielebarchiesi@0 1876 @$htmlDom->loadHTML($this->drupalGetContent());
danielebarchiesi@0 1877 if ($htmlDom) {
danielebarchiesi@0 1878 $this->pass(t('Valid HTML found on "@path"', array('@path' => $this->getUrl())), t('Browser'));
danielebarchiesi@0 1879 // It's much easier to work with simplexml than DOM, luckily enough
danielebarchiesi@0 1880 // we can just simply import our DOM tree.
danielebarchiesi@0 1881 $this->elements = simplexml_import_dom($htmlDom);
danielebarchiesi@0 1882 }
danielebarchiesi@0 1883 }
danielebarchiesi@0 1884 if (!$this->elements) {
danielebarchiesi@0 1885 $this->fail(t('Parsed page successfully.'), t('Browser'));
danielebarchiesi@0 1886 }
danielebarchiesi@0 1887
danielebarchiesi@0 1888 return $this->elements;
danielebarchiesi@0 1889 }
danielebarchiesi@0 1890
danielebarchiesi@0 1891 /**
danielebarchiesi@0 1892 * Retrieves a Drupal path or an absolute path.
danielebarchiesi@0 1893 *
danielebarchiesi@0 1894 * @param $path
danielebarchiesi@0 1895 * Drupal path or URL to load into internal browser
danielebarchiesi@0 1896 * @param $options
danielebarchiesi@0 1897 * Options to be forwarded to url().
danielebarchiesi@0 1898 * @param $headers
danielebarchiesi@0 1899 * An array containing additional HTTP request headers, each formatted as
danielebarchiesi@0 1900 * "name: value".
danielebarchiesi@0 1901 * @return
danielebarchiesi@0 1902 * The retrieved HTML string, also available as $this->drupalGetContent()
danielebarchiesi@0 1903 */
danielebarchiesi@0 1904 protected function drupalGet($path, array $options = array(), array $headers = array()) {
danielebarchiesi@0 1905 $options['absolute'] = TRUE;
danielebarchiesi@0 1906
danielebarchiesi@0 1907 // We re-using a CURL connection here. If that connection still has certain
danielebarchiesi@0 1908 // options set, it might change the GET into a POST. Make sure we clear out
danielebarchiesi@0 1909 // previous options.
danielebarchiesi@0 1910 $out = $this->curlExec(array(CURLOPT_HTTPGET => TRUE, CURLOPT_URL => url($path, $options), CURLOPT_NOBODY => FALSE, CURLOPT_HTTPHEADER => $headers));
danielebarchiesi@0 1911 $this->refreshVariables(); // Ensure that any changes to variables in the other thread are picked up.
danielebarchiesi@0 1912
danielebarchiesi@0 1913 // Replace original page output with new output from redirected page(s).
danielebarchiesi@0 1914 if ($new = $this->checkForMetaRefresh()) {
danielebarchiesi@0 1915 $out = $new;
danielebarchiesi@0 1916 }
danielebarchiesi@0 1917 $this->verbose('GET request to: ' . $path .
danielebarchiesi@0 1918 '<hr />Ending URL: ' . $this->getUrl() .
danielebarchiesi@0 1919 '<hr />' . $out);
danielebarchiesi@0 1920 return $out;
danielebarchiesi@0 1921 }
danielebarchiesi@0 1922
danielebarchiesi@0 1923 /**
danielebarchiesi@0 1924 * Retrieve a Drupal path or an absolute path and JSON decode the result.
danielebarchiesi@0 1925 */
danielebarchiesi@0 1926 protected function drupalGetAJAX($path, array $options = array(), array $headers = array()) {
danielebarchiesi@0 1927 return drupal_json_decode($this->drupalGet($path, $options, $headers));
danielebarchiesi@0 1928 }
danielebarchiesi@0 1929
danielebarchiesi@0 1930 /**
danielebarchiesi@0 1931 * Execute a POST request on a Drupal page.
danielebarchiesi@0 1932 * It will be done as usual POST request with SimpleBrowser.
danielebarchiesi@0 1933 *
danielebarchiesi@0 1934 * @param $path
danielebarchiesi@0 1935 * Location of the post form. Either a Drupal path or an absolute path or
danielebarchiesi@0 1936 * NULL to post to the current page. For multi-stage forms you can set the
danielebarchiesi@0 1937 * path to NULL and have it post to the last received page. Example:
danielebarchiesi@0 1938 *
danielebarchiesi@0 1939 * @code
danielebarchiesi@0 1940 * // First step in form.
danielebarchiesi@0 1941 * $edit = array(...);
danielebarchiesi@0 1942 * $this->drupalPost('some_url', $edit, t('Save'));
danielebarchiesi@0 1943 *
danielebarchiesi@0 1944 * // Second step in form.
danielebarchiesi@0 1945 * $edit = array(...);
danielebarchiesi@0 1946 * $this->drupalPost(NULL, $edit, t('Save'));
danielebarchiesi@0 1947 * @endcode
danielebarchiesi@0 1948 * @param $edit
danielebarchiesi@0 1949 * Field data in an associative array. Changes the current input fields
danielebarchiesi@0 1950 * (where possible) to the values indicated. A checkbox can be set to
danielebarchiesi@0 1951 * TRUE to be checked and FALSE to be unchecked. Note that when a form
danielebarchiesi@0 1952 * contains file upload fields, other fields cannot start with the '@'
danielebarchiesi@0 1953 * character.
danielebarchiesi@0 1954 *
danielebarchiesi@0 1955 * Multiple select fields can be set using name[] and setting each of the
danielebarchiesi@0 1956 * possible values. Example:
danielebarchiesi@0 1957 * @code
danielebarchiesi@0 1958 * $edit = array();
danielebarchiesi@0 1959 * $edit['name[]'] = array('value1', 'value2');
danielebarchiesi@0 1960 * @endcode
danielebarchiesi@0 1961 * @param $submit
danielebarchiesi@0 1962 * Value of the submit button whose click is to be emulated. For example,
danielebarchiesi@0 1963 * t('Save'). The processing of the request depends on this value. For
danielebarchiesi@0 1964 * example, a form may have one button with the value t('Save') and another
danielebarchiesi@0 1965 * button with the value t('Delete'), and execute different code depending
danielebarchiesi@0 1966 * on which one is clicked.
danielebarchiesi@0 1967 *
danielebarchiesi@0 1968 * This function can also be called to emulate an Ajax submission. In this
danielebarchiesi@0 1969 * case, this value needs to be an array with the following keys:
danielebarchiesi@0 1970 * - path: A path to submit the form values to for Ajax-specific processing,
danielebarchiesi@0 1971 * which is likely different than the $path parameter used for retrieving
danielebarchiesi@0 1972 * the initial form. Defaults to 'system/ajax'.
danielebarchiesi@0 1973 * - triggering_element: If the value for the 'path' key is 'system/ajax' or
danielebarchiesi@0 1974 * another generic Ajax processing path, this needs to be set to the name
danielebarchiesi@0 1975 * of the element. If the name doesn't identify the element uniquely, then
danielebarchiesi@0 1976 * this should instead be an array with a single key/value pair,
danielebarchiesi@0 1977 * corresponding to the element name and value. The callback for the
danielebarchiesi@0 1978 * generic Ajax processing path uses this to find the #ajax information
danielebarchiesi@0 1979 * for the element, including which specific callback to use for
danielebarchiesi@0 1980 * processing the request.
danielebarchiesi@0 1981 *
danielebarchiesi@0 1982 * This can also be set to NULL in order to emulate an Internet Explorer
danielebarchiesi@0 1983 * submission of a form with a single text field, and pressing ENTER in that
danielebarchiesi@0 1984 * textfield: under these conditions, no button information is added to the
danielebarchiesi@0 1985 * POST data.
danielebarchiesi@0 1986 * @param $options
danielebarchiesi@0 1987 * Options to be forwarded to url().
danielebarchiesi@0 1988 * @param $headers
danielebarchiesi@0 1989 * An array containing additional HTTP request headers, each formatted as
danielebarchiesi@0 1990 * "name: value".
danielebarchiesi@0 1991 * @param $form_html_id
danielebarchiesi@0 1992 * (optional) HTML ID of the form to be submitted. On some pages
danielebarchiesi@0 1993 * there are many identical forms, so just using the value of the submit
danielebarchiesi@0 1994 * button is not enough. For example: 'trigger-node-presave-assign-form'.
danielebarchiesi@0 1995 * Note that this is not the Drupal $form_id, but rather the HTML ID of the
danielebarchiesi@0 1996 * form, which is typically the same thing but with hyphens replacing the
danielebarchiesi@0 1997 * underscores.
danielebarchiesi@0 1998 * @param $extra_post
danielebarchiesi@0 1999 * (optional) A string of additional data to append to the POST submission.
danielebarchiesi@0 2000 * This can be used to add POST data for which there are no HTML fields, as
danielebarchiesi@0 2001 * is done by drupalPostAJAX(). This string is literally appended to the
danielebarchiesi@0 2002 * POST data, so it must already be urlencoded and contain a leading "&"
danielebarchiesi@0 2003 * (e.g., "&extra_var1=hello+world&extra_var2=you%26me").
danielebarchiesi@0 2004 */
danielebarchiesi@0 2005 protected function drupalPost($path, $edit, $submit, array $options = array(), array $headers = array(), $form_html_id = NULL, $extra_post = NULL) {
danielebarchiesi@0 2006 $submit_matches = FALSE;
danielebarchiesi@0 2007 $ajax = is_array($submit);
danielebarchiesi@0 2008 if (isset($path)) {
danielebarchiesi@0 2009 $this->drupalGet($path, $options);
danielebarchiesi@0 2010 }
danielebarchiesi@0 2011 if ($this->parse()) {
danielebarchiesi@0 2012 $edit_save = $edit;
danielebarchiesi@0 2013 // Let's iterate over all the forms.
danielebarchiesi@0 2014 $xpath = "//form";
danielebarchiesi@0 2015 if (!empty($form_html_id)) {
danielebarchiesi@0 2016 $xpath .= "[@id='" . $form_html_id . "']";
danielebarchiesi@0 2017 }
danielebarchiesi@0 2018 $forms = $this->xpath($xpath);
danielebarchiesi@0 2019 foreach ($forms as $form) {
danielebarchiesi@0 2020 // We try to set the fields of this form as specified in $edit.
danielebarchiesi@0 2021 $edit = $edit_save;
danielebarchiesi@0 2022 $post = array();
danielebarchiesi@0 2023 $upload = array();
danielebarchiesi@0 2024 $submit_matches = $this->handleForm($post, $edit, $upload, $ajax ? NULL : $submit, $form);
danielebarchiesi@0 2025 $action = isset($form['action']) ? $this->getAbsoluteUrl((string) $form['action']) : $this->getUrl();
danielebarchiesi@0 2026 if ($ajax) {
danielebarchiesi@0 2027 $action = $this->getAbsoluteUrl(!empty($submit['path']) ? $submit['path'] : 'system/ajax');
danielebarchiesi@0 2028 // Ajax callbacks verify the triggering element if necessary, so while
danielebarchiesi@0 2029 // we may eventually want extra code that verifies it in the
danielebarchiesi@0 2030 // handleForm() function, it's not currently a requirement.
danielebarchiesi@0 2031 $submit_matches = TRUE;
danielebarchiesi@0 2032 }
danielebarchiesi@0 2033
danielebarchiesi@0 2034 // We post only if we managed to handle every field in edit and the
danielebarchiesi@0 2035 // submit button matches.
danielebarchiesi@0 2036 if (!$edit && ($submit_matches || !isset($submit))) {
danielebarchiesi@0 2037 $post_array = $post;
danielebarchiesi@0 2038 if ($upload) {
danielebarchiesi@0 2039 // TODO: cURL handles file uploads for us, but the implementation
danielebarchiesi@0 2040 // is broken. This is a less than elegant workaround. Alternatives
danielebarchiesi@0 2041 // are being explored at #253506.
danielebarchiesi@0 2042 foreach ($upload as $key => $file) {
danielebarchiesi@0 2043 $file = drupal_realpath($file);
danielebarchiesi@0 2044 if ($file && is_file($file)) {
danielebarchiesi@0 2045 $post[$key] = '@' . $file;
danielebarchiesi@0 2046 }
danielebarchiesi@0 2047 }
danielebarchiesi@0 2048 }
danielebarchiesi@0 2049 else {
danielebarchiesi@0 2050 foreach ($post as $key => $value) {
danielebarchiesi@0 2051 // Encode according to application/x-www-form-urlencoded
danielebarchiesi@0 2052 // Both names and values needs to be urlencoded, according to
danielebarchiesi@0 2053 // http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1
danielebarchiesi@0 2054 $post[$key] = urlencode($key) . '=' . urlencode($value);
danielebarchiesi@0 2055 }
danielebarchiesi@0 2056 $post = implode('&', $post) . $extra_post;
danielebarchiesi@0 2057 }
danielebarchiesi@0 2058 $out = $this->curlExec(array(CURLOPT_URL => $action, CURLOPT_POST => TRUE, CURLOPT_POSTFIELDS => $post, CURLOPT_HTTPHEADER => $headers));
danielebarchiesi@0 2059 // Ensure that any changes to variables in the other thread are picked up.
danielebarchiesi@0 2060 $this->refreshVariables();
danielebarchiesi@0 2061
danielebarchiesi@0 2062 // Replace original page output with new output from redirected page(s).
danielebarchiesi@0 2063 if ($new = $this->checkForMetaRefresh()) {
danielebarchiesi@0 2064 $out = $new;
danielebarchiesi@0 2065 }
danielebarchiesi@0 2066 $this->verbose('POST request to: ' . $path .
danielebarchiesi@0 2067 '<hr />Ending URL: ' . $this->getUrl() .
danielebarchiesi@0 2068 '<hr />Fields: ' . highlight_string('<?php ' . var_export($post_array, TRUE), TRUE) .
danielebarchiesi@0 2069 '<hr />' . $out);
danielebarchiesi@0 2070 return $out;
danielebarchiesi@0 2071 }
danielebarchiesi@0 2072 }
danielebarchiesi@0 2073 // We have not found a form which contained all fields of $edit.
danielebarchiesi@0 2074 foreach ($edit as $name => $value) {
danielebarchiesi@0 2075 $this->fail(t('Failed to set field @name to @value', array('@name' => $name, '@value' => $value)));
danielebarchiesi@0 2076 }
danielebarchiesi@0 2077 if (!$ajax && isset($submit)) {
danielebarchiesi@0 2078 $this->assertTrue($submit_matches, t('Found the @submit button', array('@submit' => $submit)));
danielebarchiesi@0 2079 }
danielebarchiesi@0 2080 $this->fail(t('Found the requested form fields at @path', array('@path' => $path)));
danielebarchiesi@0 2081 }
danielebarchiesi@0 2082 }
danielebarchiesi@0 2083
danielebarchiesi@0 2084 /**
danielebarchiesi@0 2085 * Execute an Ajax submission.
danielebarchiesi@0 2086 *
danielebarchiesi@0 2087 * This executes a POST as ajax.js does. It uses the returned JSON data, an
danielebarchiesi@0 2088 * array of commands, to update $this->content using equivalent DOM
danielebarchiesi@0 2089 * manipulation as is used by ajax.js. It also returns the array of commands.
danielebarchiesi@0 2090 *
danielebarchiesi@0 2091 * @param $path
danielebarchiesi@0 2092 * Location of the form containing the Ajax enabled element to test. Can be
danielebarchiesi@0 2093 * either a Drupal path or an absolute path or NULL to use the current page.
danielebarchiesi@0 2094 * @param $edit
danielebarchiesi@0 2095 * Field data in an associative array. Changes the current input fields
danielebarchiesi@0 2096 * (where possible) to the values indicated.
danielebarchiesi@0 2097 * @param $triggering_element
danielebarchiesi@0 2098 * The name of the form element that is responsible for triggering the Ajax
danielebarchiesi@0 2099 * functionality to test. May be a string or, if the triggering element is
danielebarchiesi@0 2100 * a button, an associative array where the key is the name of the button
danielebarchiesi@0 2101 * and the value is the button label. i.e.) array('op' => t('Refresh')).
danielebarchiesi@0 2102 * @param $ajax_path
danielebarchiesi@0 2103 * (optional) Override the path set by the Ajax settings of the triggering
danielebarchiesi@0 2104 * element. In the absence of both the triggering element's Ajax path and
danielebarchiesi@0 2105 * $ajax_path 'system/ajax' will be used.
danielebarchiesi@0 2106 * @param $options
danielebarchiesi@0 2107 * (optional) Options to be forwarded to url().
danielebarchiesi@0 2108 * @param $headers
danielebarchiesi@0 2109 * (optional) An array containing additional HTTP request headers, each
danielebarchiesi@0 2110 * formatted as "name: value". Forwarded to drupalPost().
danielebarchiesi@0 2111 * @param $form_html_id
danielebarchiesi@0 2112 * (optional) HTML ID of the form to be submitted, use when there is more
danielebarchiesi@0 2113 * than one identical form on the same page and the value of the triggering
danielebarchiesi@0 2114 * element is not enough to identify the form. Note this is not the Drupal
danielebarchiesi@0 2115 * ID of the form but rather the HTML ID of the form.
danielebarchiesi@0 2116 * @param $ajax_settings
danielebarchiesi@0 2117 * (optional) An array of Ajax settings which if specified will be used in
danielebarchiesi@0 2118 * place of the Ajax settings of the triggering element.
danielebarchiesi@0 2119 *
danielebarchiesi@0 2120 * @return
danielebarchiesi@0 2121 * An array of Ajax commands.
danielebarchiesi@0 2122 *
danielebarchiesi@0 2123 * @see drupalPost()
danielebarchiesi@0 2124 * @see ajax.js
danielebarchiesi@0 2125 */
danielebarchiesi@0 2126 protected function drupalPostAJAX($path, $edit, $triggering_element, $ajax_path = NULL, array $options = array(), array $headers = array(), $form_html_id = NULL, $ajax_settings = NULL) {
danielebarchiesi@0 2127 // Get the content of the initial page prior to calling drupalPost(), since
danielebarchiesi@0 2128 // drupalPost() replaces $this->content.
danielebarchiesi@0 2129 if (isset($path)) {
danielebarchiesi@0 2130 $this->drupalGet($path, $options);
danielebarchiesi@0 2131 }
danielebarchiesi@0 2132 $content = $this->content;
danielebarchiesi@0 2133 $drupal_settings = $this->drupalSettings;
danielebarchiesi@0 2134
danielebarchiesi@0 2135 // Get the Ajax settings bound to the triggering element.
danielebarchiesi@0 2136 if (!isset($ajax_settings)) {
danielebarchiesi@0 2137 if (is_array($triggering_element)) {
danielebarchiesi@0 2138 $xpath = '//*[@name="' . key($triggering_element) . '" and @value="' . current($triggering_element) . '"]';
danielebarchiesi@0 2139 }
danielebarchiesi@0 2140 else {
danielebarchiesi@0 2141 $xpath = '//*[@name="' . $triggering_element . '"]';
danielebarchiesi@0 2142 }
danielebarchiesi@0 2143 if (isset($form_html_id)) {
danielebarchiesi@0 2144 $xpath = '//form[@id="' . $form_html_id . '"]' . $xpath;
danielebarchiesi@0 2145 }
danielebarchiesi@0 2146 $element = $this->xpath($xpath);
danielebarchiesi@0 2147 $element_id = (string) $element[0]['id'];
danielebarchiesi@0 2148 $ajax_settings = $drupal_settings['ajax'][$element_id];
danielebarchiesi@0 2149 }
danielebarchiesi@0 2150
danielebarchiesi@0 2151 // Add extra information to the POST data as ajax.js does.
danielebarchiesi@0 2152 $extra_post = '';
danielebarchiesi@0 2153 if (isset($ajax_settings['submit'])) {
danielebarchiesi@0 2154 foreach ($ajax_settings['submit'] as $key => $value) {
danielebarchiesi@0 2155 $extra_post .= '&' . urlencode($key) . '=' . urlencode($value);
danielebarchiesi@0 2156 }
danielebarchiesi@0 2157 }
danielebarchiesi@0 2158 foreach ($this->xpath('//*[@id]') as $element) {
danielebarchiesi@0 2159 $id = (string) $element['id'];
danielebarchiesi@0 2160 $extra_post .= '&' . urlencode('ajax_html_ids[]') . '=' . urlencode($id);
danielebarchiesi@0 2161 }
danielebarchiesi@0 2162 if (isset($drupal_settings['ajaxPageState'])) {
danielebarchiesi@0 2163 $extra_post .= '&' . urlencode('ajax_page_state[theme]') . '=' . urlencode($drupal_settings['ajaxPageState']['theme']);
danielebarchiesi@0 2164 $extra_post .= '&' . urlencode('ajax_page_state[theme_token]') . '=' . urlencode($drupal_settings['ajaxPageState']['theme_token']);
danielebarchiesi@0 2165 foreach ($drupal_settings['ajaxPageState']['css'] as $key => $value) {
danielebarchiesi@0 2166 $extra_post .= '&' . urlencode("ajax_page_state[css][$key]") . '=1';
danielebarchiesi@0 2167 }
danielebarchiesi@0 2168 foreach ($drupal_settings['ajaxPageState']['js'] as $key => $value) {
danielebarchiesi@0 2169 $extra_post .= '&' . urlencode("ajax_page_state[js][$key]") . '=1';
danielebarchiesi@0 2170 }
danielebarchiesi@0 2171 }
danielebarchiesi@0 2172
danielebarchiesi@0 2173 // Unless a particular path is specified, use the one specified by the
danielebarchiesi@0 2174 // Ajax settings, or else 'system/ajax'.
danielebarchiesi@0 2175 if (!isset($ajax_path)) {
danielebarchiesi@0 2176 $ajax_path = isset($ajax_settings['url']) ? $ajax_settings['url'] : 'system/ajax';
danielebarchiesi@0 2177 }
danielebarchiesi@0 2178
danielebarchiesi@0 2179 // Submit the POST request.
danielebarchiesi@0 2180 $return = drupal_json_decode($this->drupalPost(NULL, $edit, array('path' => $ajax_path, 'triggering_element' => $triggering_element), $options, $headers, $form_html_id, $extra_post));
danielebarchiesi@0 2181
danielebarchiesi@0 2182 // Change the page content by applying the returned commands.
danielebarchiesi@0 2183 if (!empty($ajax_settings) && !empty($return)) {
danielebarchiesi@0 2184 // ajax.js applies some defaults to the settings object, so do the same
danielebarchiesi@0 2185 // for what's used by this function.
danielebarchiesi@0 2186 $ajax_settings += array(
danielebarchiesi@0 2187 'method' => 'replaceWith',
danielebarchiesi@0 2188 );
danielebarchiesi@0 2189 // DOM can load HTML soup. But, HTML soup can throw warnings, suppress
danielebarchiesi@0 2190 // them.
danielebarchiesi@0 2191 $dom = new DOMDocument();
danielebarchiesi@0 2192 @$dom->loadHTML($content);
danielebarchiesi@0 2193 // XPath allows for finding wrapper nodes better than DOM does.
danielebarchiesi@0 2194 $xpath = new DOMXPath($dom);
danielebarchiesi@0 2195 foreach ($return as $command) {
danielebarchiesi@0 2196 switch ($command['command']) {
danielebarchiesi@0 2197 case 'settings':
danielebarchiesi@0 2198 $drupal_settings = drupal_array_merge_deep($drupal_settings, $command['settings']);
danielebarchiesi@0 2199 break;
danielebarchiesi@0 2200
danielebarchiesi@0 2201 case 'insert':
danielebarchiesi@0 2202 $wrapperNode = NULL;
danielebarchiesi@0 2203 // When a command doesn't specify a selector, use the
danielebarchiesi@0 2204 // #ajax['wrapper'] which is always an HTML ID.
danielebarchiesi@0 2205 if (!isset($command['selector'])) {
danielebarchiesi@0 2206 $wrapperNode = $xpath->query('//*[@id="' . $ajax_settings['wrapper'] . '"]')->item(0);
danielebarchiesi@0 2207 }
danielebarchiesi@0 2208 // @todo Ajax commands can target any jQuery selector, but these are
danielebarchiesi@0 2209 // hard to fully emulate with XPath. For now, just handle 'head'
danielebarchiesi@0 2210 // and 'body', since these are used by ajax_render().
danielebarchiesi@0 2211 elseif (in_array($command['selector'], array('head', 'body'))) {
danielebarchiesi@0 2212 $wrapperNode = $xpath->query('//' . $command['selector'])->item(0);
danielebarchiesi@0 2213 }
danielebarchiesi@0 2214 if ($wrapperNode) {
danielebarchiesi@0 2215 // ajax.js adds an enclosing DIV to work around a Safari bug.
danielebarchiesi@0 2216 $newDom = new DOMDocument();
danielebarchiesi@0 2217 $newDom->loadHTML('<div>' . $command['data'] . '</div>');
danielebarchiesi@0 2218 $newNode = $dom->importNode($newDom->documentElement->firstChild->firstChild, TRUE);
danielebarchiesi@0 2219 $method = isset($command['method']) ? $command['method'] : $ajax_settings['method'];
danielebarchiesi@0 2220 // The "method" is a jQuery DOM manipulation function. Emulate
danielebarchiesi@0 2221 // each one using PHP's DOMNode API.
danielebarchiesi@0 2222 switch ($method) {
danielebarchiesi@0 2223 case 'replaceWith':
danielebarchiesi@0 2224 $wrapperNode->parentNode->replaceChild($newNode, $wrapperNode);
danielebarchiesi@0 2225 break;
danielebarchiesi@0 2226 case 'append':
danielebarchiesi@0 2227 $wrapperNode->appendChild($newNode);
danielebarchiesi@0 2228 break;
danielebarchiesi@0 2229 case 'prepend':
danielebarchiesi@0 2230 // If no firstChild, insertBefore() falls back to
danielebarchiesi@0 2231 // appendChild().
danielebarchiesi@0 2232 $wrapperNode->insertBefore($newNode, $wrapperNode->firstChild);
danielebarchiesi@0 2233 break;
danielebarchiesi@0 2234 case 'before':
danielebarchiesi@0 2235 $wrapperNode->parentNode->insertBefore($newNode, $wrapperNode);
danielebarchiesi@0 2236 break;
danielebarchiesi@0 2237 case 'after':
danielebarchiesi@0 2238 // If no nextSibling, insertBefore() falls back to
danielebarchiesi@0 2239 // appendChild().
danielebarchiesi@0 2240 $wrapperNode->parentNode->insertBefore($newNode, $wrapperNode->nextSibling);
danielebarchiesi@0 2241 break;
danielebarchiesi@0 2242 case 'html':
danielebarchiesi@0 2243 foreach ($wrapperNode->childNodes as $childNode) {
danielebarchiesi@0 2244 $wrapperNode->removeChild($childNode);
danielebarchiesi@0 2245 }
danielebarchiesi@0 2246 $wrapperNode->appendChild($newNode);
danielebarchiesi@0 2247 break;
danielebarchiesi@0 2248 }
danielebarchiesi@0 2249 }
danielebarchiesi@0 2250 break;
danielebarchiesi@0 2251
danielebarchiesi@0 2252 // @todo Add suitable implementations for these commands in order to
danielebarchiesi@0 2253 // have full test coverage of what ajax.js can do.
danielebarchiesi@0 2254 case 'remove':
danielebarchiesi@0 2255 break;
danielebarchiesi@0 2256 case 'changed':
danielebarchiesi@0 2257 break;
danielebarchiesi@0 2258 case 'css':
danielebarchiesi@0 2259 break;
danielebarchiesi@0 2260 case 'data':
danielebarchiesi@0 2261 break;
danielebarchiesi@0 2262 case 'restripe':
danielebarchiesi@0 2263 break;
danielebarchiesi@0 2264 }
danielebarchiesi@0 2265 }
danielebarchiesi@0 2266 $content = $dom->saveHTML();
danielebarchiesi@0 2267 }
danielebarchiesi@0 2268 $this->drupalSetContent($content);
danielebarchiesi@0 2269 $this->drupalSetSettings($drupal_settings);
danielebarchiesi@0 2270 return $return;
danielebarchiesi@0 2271 }
danielebarchiesi@0 2272
danielebarchiesi@0 2273 /**
danielebarchiesi@0 2274 * Runs cron in the Drupal installed by Simpletest.
danielebarchiesi@0 2275 */
danielebarchiesi@0 2276 protected function cronRun() {
danielebarchiesi@0 2277 $this->drupalGet($GLOBALS['base_url'] . '/cron.php', array('external' => TRUE, 'query' => array('cron_key' => variable_get('cron_key', 'drupal'))));
danielebarchiesi@0 2278 }
danielebarchiesi@0 2279
danielebarchiesi@0 2280 /**
danielebarchiesi@0 2281 * Check for meta refresh tag and if found call drupalGet() recursively. This
danielebarchiesi@0 2282 * function looks for the http-equiv attribute to be set to "Refresh"
danielebarchiesi@0 2283 * and is case-sensitive.
danielebarchiesi@0 2284 *
danielebarchiesi@0 2285 * @return
danielebarchiesi@0 2286 * Either the new page content or FALSE.
danielebarchiesi@0 2287 */
danielebarchiesi@0 2288 protected function checkForMetaRefresh() {
danielebarchiesi@0 2289 if (strpos($this->drupalGetContent(), '<meta ') && $this->parse()) {
danielebarchiesi@0 2290 $refresh = $this->xpath('//meta[@http-equiv="Refresh"]');
danielebarchiesi@0 2291 if (!empty($refresh)) {
danielebarchiesi@0 2292 // Parse the content attribute of the meta tag for the format:
danielebarchiesi@0 2293 // "[delay]: URL=[page_to_redirect_to]".
danielebarchiesi@0 2294 if (preg_match('/\d+;\s*URL=(?P<url>.*)/i', $refresh[0]['content'], $match)) {
danielebarchiesi@0 2295 return $this->drupalGet($this->getAbsoluteUrl(decode_entities($match['url'])));
danielebarchiesi@0 2296 }
danielebarchiesi@0 2297 }
danielebarchiesi@0 2298 }
danielebarchiesi@0 2299 return FALSE;
danielebarchiesi@0 2300 }
danielebarchiesi@0 2301
danielebarchiesi@0 2302 /**
danielebarchiesi@0 2303 * Retrieves only the headers for a Drupal path or an absolute path.
danielebarchiesi@0 2304 *
danielebarchiesi@0 2305 * @param $path
danielebarchiesi@0 2306 * Drupal path or URL to load into internal browser
danielebarchiesi@0 2307 * @param $options
danielebarchiesi@0 2308 * Options to be forwarded to url().
danielebarchiesi@0 2309 * @param $headers
danielebarchiesi@0 2310 * An array containing additional HTTP request headers, each formatted as
danielebarchiesi@0 2311 * "name: value".
danielebarchiesi@0 2312 * @return
danielebarchiesi@0 2313 * The retrieved headers, also available as $this->drupalGetContent()
danielebarchiesi@0 2314 */
danielebarchiesi@0 2315 protected function drupalHead($path, array $options = array(), array $headers = array()) {
danielebarchiesi@0 2316 $options['absolute'] = TRUE;
danielebarchiesi@0 2317 $out = $this->curlExec(array(CURLOPT_NOBODY => TRUE, CURLOPT_URL => url($path, $options), CURLOPT_HTTPHEADER => $headers));
danielebarchiesi@0 2318 $this->refreshVariables(); // Ensure that any changes to variables in the other thread are picked up.
danielebarchiesi@0 2319 return $out;
danielebarchiesi@0 2320 }
danielebarchiesi@0 2321
danielebarchiesi@0 2322 /**
danielebarchiesi@0 2323 * Handle form input related to drupalPost(). Ensure that the specified fields
danielebarchiesi@0 2324 * exist and attempt to create POST data in the correct manner for the particular
danielebarchiesi@0 2325 * field type.
danielebarchiesi@0 2326 *
danielebarchiesi@0 2327 * @param $post
danielebarchiesi@0 2328 * Reference to array of post values.
danielebarchiesi@0 2329 * @param $edit
danielebarchiesi@0 2330 * Reference to array of edit values to be checked against the form.
danielebarchiesi@0 2331 * @param $submit
danielebarchiesi@0 2332 * Form submit button value.
danielebarchiesi@0 2333 * @param $form
danielebarchiesi@0 2334 * Array of form elements.
danielebarchiesi@0 2335 * @return
danielebarchiesi@0 2336 * Submit value matches a valid submit input in the form.
danielebarchiesi@0 2337 */
danielebarchiesi@0 2338 protected function handleForm(&$post, &$edit, &$upload, $submit, $form) {
danielebarchiesi@0 2339 // Retrieve the form elements.
danielebarchiesi@0 2340 $elements = $form->xpath('.//input[not(@disabled)]|.//textarea[not(@disabled)]|.//select[not(@disabled)]');
danielebarchiesi@0 2341 $submit_matches = FALSE;
danielebarchiesi@0 2342 foreach ($elements as $element) {
danielebarchiesi@0 2343 // SimpleXML objects need string casting all the time.
danielebarchiesi@0 2344 $name = (string) $element['name'];
danielebarchiesi@0 2345 // This can either be the type of <input> or the name of the tag itself
danielebarchiesi@0 2346 // for <select> or <textarea>.
danielebarchiesi@0 2347 $type = isset($element['type']) ? (string) $element['type'] : $element->getName();
danielebarchiesi@0 2348 $value = isset($element['value']) ? (string) $element['value'] : '';
danielebarchiesi@0 2349 $done = FALSE;
danielebarchiesi@0 2350 if (isset($edit[$name])) {
danielebarchiesi@0 2351 switch ($type) {
danielebarchiesi@0 2352 case 'text':
danielebarchiesi@0 2353 case 'tel':
danielebarchiesi@0 2354 case 'textarea':
danielebarchiesi@0 2355 case 'url':
danielebarchiesi@0 2356 case 'number':
danielebarchiesi@0 2357 case 'range':
danielebarchiesi@0 2358 case 'color':
danielebarchiesi@0 2359 case 'hidden':
danielebarchiesi@0 2360 case 'password':
danielebarchiesi@0 2361 case 'email':
danielebarchiesi@0 2362 case 'search':
danielebarchiesi@0 2363 $post[$name] = $edit[$name];
danielebarchiesi@0 2364 unset($edit[$name]);
danielebarchiesi@0 2365 break;
danielebarchiesi@0 2366 case 'radio':
danielebarchiesi@0 2367 if ($edit[$name] == $value) {
danielebarchiesi@0 2368 $post[$name] = $edit[$name];
danielebarchiesi@0 2369 unset($edit[$name]);
danielebarchiesi@0 2370 }
danielebarchiesi@0 2371 break;
danielebarchiesi@0 2372 case 'checkbox':
danielebarchiesi@0 2373 // To prevent checkbox from being checked.pass in a FALSE,
danielebarchiesi@0 2374 // otherwise the checkbox will be set to its value regardless
danielebarchiesi@0 2375 // of $edit.
danielebarchiesi@0 2376 if ($edit[$name] === FALSE) {
danielebarchiesi@0 2377 unset($edit[$name]);
danielebarchiesi@0 2378 continue 2;
danielebarchiesi@0 2379 }
danielebarchiesi@0 2380 else {
danielebarchiesi@0 2381 unset($edit[$name]);
danielebarchiesi@0 2382 $post[$name] = $value;
danielebarchiesi@0 2383 }
danielebarchiesi@0 2384 break;
danielebarchiesi@0 2385 case 'select':
danielebarchiesi@0 2386 $new_value = $edit[$name];
danielebarchiesi@0 2387 $options = $this->getAllOptions($element);
danielebarchiesi@0 2388 if (is_array($new_value)) {
danielebarchiesi@0 2389 // Multiple select box.
danielebarchiesi@0 2390 if (!empty($new_value)) {
danielebarchiesi@0 2391 $index = 0;
danielebarchiesi@0 2392 $key = preg_replace('/\[\]$/', '', $name);
danielebarchiesi@0 2393 foreach ($options as $option) {
danielebarchiesi@0 2394 $option_value = (string) $option['value'];
danielebarchiesi@0 2395 if (in_array($option_value, $new_value)) {
danielebarchiesi@0 2396 $post[$key . '[' . $index++ . ']'] = $option_value;
danielebarchiesi@0 2397 $done = TRUE;
danielebarchiesi@0 2398 unset($edit[$name]);
danielebarchiesi@0 2399 }
danielebarchiesi@0 2400 }
danielebarchiesi@0 2401 }
danielebarchiesi@0 2402 else {
danielebarchiesi@0 2403 // No options selected: do not include any POST data for the
danielebarchiesi@0 2404 // element.
danielebarchiesi@0 2405 $done = TRUE;
danielebarchiesi@0 2406 unset($edit[$name]);
danielebarchiesi@0 2407 }
danielebarchiesi@0 2408 }
danielebarchiesi@0 2409 else {
danielebarchiesi@0 2410 // Single select box.
danielebarchiesi@0 2411 foreach ($options as $option) {
danielebarchiesi@0 2412 if ($new_value == $option['value']) {
danielebarchiesi@0 2413 $post[$name] = $new_value;
danielebarchiesi@0 2414 unset($edit[$name]);
danielebarchiesi@0 2415 $done = TRUE;
danielebarchiesi@0 2416 break;
danielebarchiesi@0 2417 }
danielebarchiesi@0 2418 }
danielebarchiesi@0 2419 }
danielebarchiesi@0 2420 break;
danielebarchiesi@0 2421 case 'file':
danielebarchiesi@0 2422 $upload[$name] = $edit[$name];
danielebarchiesi@0 2423 unset($edit[$name]);
danielebarchiesi@0 2424 break;
danielebarchiesi@0 2425 }
danielebarchiesi@0 2426 }
danielebarchiesi@0 2427 if (!isset($post[$name]) && !$done) {
danielebarchiesi@0 2428 switch ($type) {
danielebarchiesi@0 2429 case 'textarea':
danielebarchiesi@0 2430 $post[$name] = (string) $element;
danielebarchiesi@0 2431 break;
danielebarchiesi@0 2432 case 'select':
danielebarchiesi@0 2433 $single = empty($element['multiple']);
danielebarchiesi@0 2434 $first = TRUE;
danielebarchiesi@0 2435 $index = 0;
danielebarchiesi@0 2436 $key = preg_replace('/\[\]$/', '', $name);
danielebarchiesi@0 2437 $options = $this->getAllOptions($element);
danielebarchiesi@0 2438 foreach ($options as $option) {
danielebarchiesi@0 2439 // For single select, we load the first option, if there is a
danielebarchiesi@0 2440 // selected option that will overwrite it later.
danielebarchiesi@0 2441 if ($option['selected'] || ($first && $single)) {
danielebarchiesi@0 2442 $first = FALSE;
danielebarchiesi@0 2443 if ($single) {
danielebarchiesi@0 2444 $post[$name] = (string) $option['value'];
danielebarchiesi@0 2445 }
danielebarchiesi@0 2446 else {
danielebarchiesi@0 2447 $post[$key . '[' . $index++ . ']'] = (string) $option['value'];
danielebarchiesi@0 2448 }
danielebarchiesi@0 2449 }
danielebarchiesi@0 2450 }
danielebarchiesi@0 2451 break;
danielebarchiesi@0 2452 case 'file':
danielebarchiesi@0 2453 break;
danielebarchiesi@0 2454 case 'submit':
danielebarchiesi@0 2455 case 'image':
danielebarchiesi@0 2456 if (isset($submit) && $submit == $value) {
danielebarchiesi@0 2457 $post[$name] = $value;
danielebarchiesi@0 2458 $submit_matches = TRUE;
danielebarchiesi@0 2459 }
danielebarchiesi@0 2460 break;
danielebarchiesi@0 2461 case 'radio':
danielebarchiesi@0 2462 case 'checkbox':
danielebarchiesi@0 2463 if (!isset($element['checked'])) {
danielebarchiesi@0 2464 break;
danielebarchiesi@0 2465 }
danielebarchiesi@0 2466 // Deliberate no break.
danielebarchiesi@0 2467 default:
danielebarchiesi@0 2468 $post[$name] = $value;
danielebarchiesi@0 2469 }
danielebarchiesi@0 2470 }
danielebarchiesi@0 2471 }
danielebarchiesi@0 2472 return $submit_matches;
danielebarchiesi@0 2473 }
danielebarchiesi@0 2474
danielebarchiesi@0 2475 /**
danielebarchiesi@0 2476 * Builds an XPath query.
danielebarchiesi@0 2477 *
danielebarchiesi@0 2478 * Builds an XPath query by replacing placeholders in the query by the value
danielebarchiesi@0 2479 * of the arguments.
danielebarchiesi@0 2480 *
danielebarchiesi@0 2481 * XPath 1.0 (the version supported by libxml2, the underlying XML library
danielebarchiesi@0 2482 * used by PHP) doesn't support any form of quotation. This function
danielebarchiesi@0 2483 * simplifies the building of XPath expression.
danielebarchiesi@0 2484 *
danielebarchiesi@0 2485 * @param $xpath
danielebarchiesi@0 2486 * An XPath query, possibly with placeholders in the form ':name'.
danielebarchiesi@0 2487 * @param $args
danielebarchiesi@0 2488 * An array of arguments with keys in the form ':name' matching the
danielebarchiesi@0 2489 * placeholders in the query. The values may be either strings or numeric
danielebarchiesi@0 2490 * values.
danielebarchiesi@0 2491 * @return
danielebarchiesi@0 2492 * An XPath query with arguments replaced.
danielebarchiesi@0 2493 */
danielebarchiesi@0 2494 protected function buildXPathQuery($xpath, array $args = array()) {
danielebarchiesi@0 2495 // Replace placeholders.
danielebarchiesi@0 2496 foreach ($args as $placeholder => $value) {
danielebarchiesi@0 2497 // XPath 1.0 doesn't support a way to escape single or double quotes in a
danielebarchiesi@0 2498 // string literal. We split double quotes out of the string, and encode
danielebarchiesi@0 2499 // them separately.
danielebarchiesi@0 2500 if (is_string($value)) {
danielebarchiesi@0 2501 // Explode the text at the quote characters.
danielebarchiesi@0 2502 $parts = explode('"', $value);
danielebarchiesi@0 2503
danielebarchiesi@0 2504 // Quote the parts.
danielebarchiesi@0 2505 foreach ($parts as &$part) {
danielebarchiesi@0 2506 $part = '"' . $part . '"';
danielebarchiesi@0 2507 }
danielebarchiesi@0 2508
danielebarchiesi@0 2509 // Return the string.
danielebarchiesi@0 2510 $value = count($parts) > 1 ? 'concat(' . implode(', \'"\', ', $parts) . ')' : $parts[0];
danielebarchiesi@0 2511 }
danielebarchiesi@0 2512 $xpath = preg_replace('/' . preg_quote($placeholder) . '\b/', $value, $xpath);
danielebarchiesi@0 2513 }
danielebarchiesi@0 2514 return $xpath;
danielebarchiesi@0 2515 }
danielebarchiesi@0 2516
danielebarchiesi@0 2517 /**
danielebarchiesi@0 2518 * Perform an xpath search on the contents of the internal browser. The search
danielebarchiesi@0 2519 * is relative to the root element (HTML tag normally) of the page.
danielebarchiesi@0 2520 *
danielebarchiesi@0 2521 * @param $xpath
danielebarchiesi@0 2522 * The xpath string to use in the search.
danielebarchiesi@0 2523 * @return
danielebarchiesi@0 2524 * The return value of the xpath search. For details on the xpath string
danielebarchiesi@0 2525 * format and return values see the SimpleXML documentation,
danielebarchiesi@0 2526 * http://us.php.net/manual/function.simplexml-element-xpath.php.
danielebarchiesi@0 2527 */
danielebarchiesi@0 2528 protected function xpath($xpath, array $arguments = array()) {
danielebarchiesi@0 2529 if ($this->parse()) {
danielebarchiesi@0 2530 $xpath = $this->buildXPathQuery($xpath, $arguments);
danielebarchiesi@0 2531 $result = $this->elements->xpath($xpath);
danielebarchiesi@0 2532 // Some combinations of PHP / libxml versions return an empty array
danielebarchiesi@0 2533 // instead of the documented FALSE. Forcefully convert any falsish values
danielebarchiesi@0 2534 // to an empty array to allow foreach(...) constructions.
danielebarchiesi@0 2535 return $result ? $result : array();
danielebarchiesi@0 2536 }
danielebarchiesi@0 2537 else {
danielebarchiesi@0 2538 return FALSE;
danielebarchiesi@0 2539 }
danielebarchiesi@0 2540 }
danielebarchiesi@0 2541
danielebarchiesi@0 2542 /**
danielebarchiesi@0 2543 * Get all option elements, including nested options, in a select.
danielebarchiesi@0 2544 *
danielebarchiesi@0 2545 * @param $element
danielebarchiesi@0 2546 * The element for which to get the options.
danielebarchiesi@0 2547 * @return
danielebarchiesi@0 2548 * Option elements in select.
danielebarchiesi@0 2549 */
danielebarchiesi@0 2550 protected function getAllOptions(SimpleXMLElement $element) {
danielebarchiesi@0 2551 $options = array();
danielebarchiesi@0 2552 // Add all options items.
danielebarchiesi@0 2553 foreach ($element->option as $option) {
danielebarchiesi@0 2554 $options[] = $option;
danielebarchiesi@0 2555 }
danielebarchiesi@0 2556
danielebarchiesi@0 2557 // Search option group children.
danielebarchiesi@0 2558 if (isset($element->optgroup)) {
danielebarchiesi@0 2559 foreach ($element->optgroup as $group) {
danielebarchiesi@0 2560 $options = array_merge($options, $this->getAllOptions($group));
danielebarchiesi@0 2561 }
danielebarchiesi@0 2562 }
danielebarchiesi@0 2563 return $options;
danielebarchiesi@0 2564 }
danielebarchiesi@0 2565
danielebarchiesi@0 2566 /**
danielebarchiesi@0 2567 * Pass if a link with the specified label is found, and optional with the
danielebarchiesi@0 2568 * specified index.
danielebarchiesi@0 2569 *
danielebarchiesi@0 2570 * @param $label
danielebarchiesi@0 2571 * Text between the anchor tags.
danielebarchiesi@0 2572 * @param $index
danielebarchiesi@0 2573 * Link position counting from zero.
danielebarchiesi@0 2574 * @param $message
danielebarchiesi@0 2575 * Message to display.
danielebarchiesi@0 2576 * @param $group
danielebarchiesi@0 2577 * The group this message belongs to, defaults to 'Other'.
danielebarchiesi@0 2578 * @return
danielebarchiesi@0 2579 * TRUE if the assertion succeeded, FALSE otherwise.
danielebarchiesi@0 2580 */
danielebarchiesi@0 2581 protected function assertLink($label, $index = 0, $message = '', $group = 'Other') {
danielebarchiesi@0 2582 $links = $this->xpath('//a[normalize-space(text())=:label]', array(':label' => $label));
danielebarchiesi@0 2583 $message = ($message ? $message : t('Link with label %label found.', array('%label' => $label)));
danielebarchiesi@0 2584 return $this->assert(isset($links[$index]), $message, $group);
danielebarchiesi@0 2585 }
danielebarchiesi@0 2586
danielebarchiesi@0 2587 /**
danielebarchiesi@0 2588 * Pass if a link with the specified label is not found.
danielebarchiesi@0 2589 *
danielebarchiesi@0 2590 * @param $label
danielebarchiesi@0 2591 * Text between the anchor tags.
danielebarchiesi@0 2592 * @param $index
danielebarchiesi@0 2593 * Link position counting from zero.
danielebarchiesi@0 2594 * @param $message
danielebarchiesi@0 2595 * Message to display.
danielebarchiesi@0 2596 * @param $group
danielebarchiesi@0 2597 * The group this message belongs to, defaults to 'Other'.
danielebarchiesi@0 2598 * @return
danielebarchiesi@0 2599 * TRUE if the assertion succeeded, FALSE otherwise.
danielebarchiesi@0 2600 */
danielebarchiesi@0 2601 protected function assertNoLink($label, $message = '', $group = 'Other') {
danielebarchiesi@0 2602 $links = $this->xpath('//a[normalize-space(text())=:label]', array(':label' => $label));
danielebarchiesi@0 2603 $message = ($message ? $message : t('Link with label %label not found.', array('%label' => $label)));
danielebarchiesi@0 2604 return $this->assert(empty($links), $message, $group);
danielebarchiesi@0 2605 }
danielebarchiesi@0 2606
danielebarchiesi@0 2607 /**
danielebarchiesi@0 2608 * Pass if a link containing a given href (part) is found.
danielebarchiesi@0 2609 *
danielebarchiesi@0 2610 * @param $href
danielebarchiesi@0 2611 * The full or partial value of the 'href' attribute of the anchor tag.
danielebarchiesi@0 2612 * @param $index
danielebarchiesi@0 2613 * Link position counting from zero.
danielebarchiesi@0 2614 * @param $message
danielebarchiesi@0 2615 * Message to display.
danielebarchiesi@0 2616 * @param $group
danielebarchiesi@0 2617 * The group this message belongs to, defaults to 'Other'.
danielebarchiesi@0 2618 *
danielebarchiesi@0 2619 * @return
danielebarchiesi@0 2620 * TRUE if the assertion succeeded, FALSE otherwise.
danielebarchiesi@0 2621 */
danielebarchiesi@0 2622 protected function assertLinkByHref($href, $index = 0, $message = '', $group = 'Other') {
danielebarchiesi@0 2623 $links = $this->xpath('//a[contains(@href, :href)]', array(':href' => $href));
danielebarchiesi@0 2624 $message = ($message ? $message : t('Link containing href %href found.', array('%href' => $href)));
danielebarchiesi@0 2625 return $this->assert(isset($links[$index]), $message, $group);
danielebarchiesi@0 2626 }
danielebarchiesi@0 2627
danielebarchiesi@0 2628 /**
danielebarchiesi@0 2629 * Pass if a link containing a given href (part) is not found.
danielebarchiesi@0 2630 *
danielebarchiesi@0 2631 * @param $href
danielebarchiesi@0 2632 * The full or partial value of the 'href' attribute of the anchor tag.
danielebarchiesi@0 2633 * @param $message
danielebarchiesi@0 2634 * Message to display.
danielebarchiesi@0 2635 * @param $group
danielebarchiesi@0 2636 * The group this message belongs to, defaults to 'Other'.
danielebarchiesi@0 2637 *
danielebarchiesi@0 2638 * @return
danielebarchiesi@0 2639 * TRUE if the assertion succeeded, FALSE otherwise.
danielebarchiesi@0 2640 */
danielebarchiesi@0 2641 protected function assertNoLinkByHref($href, $message = '', $group = 'Other') {
danielebarchiesi@0 2642 $links = $this->xpath('//a[contains(@href, :href)]', array(':href' => $href));
danielebarchiesi@0 2643 $message = ($message ? $message : t('No link containing href %href found.', array('%href' => $href)));
danielebarchiesi@0 2644 return $this->assert(empty($links), $message, $group);
danielebarchiesi@0 2645 }
danielebarchiesi@0 2646
danielebarchiesi@0 2647 /**
danielebarchiesi@0 2648 * Follows a link by name.
danielebarchiesi@0 2649 *
danielebarchiesi@0 2650 * Will click the first link found with this link text by default, or a later
danielebarchiesi@0 2651 * one if an index is given. Match is case sensitive with normalized space.
danielebarchiesi@0 2652 * The label is translated label. There is an assert for successful click.
danielebarchiesi@0 2653 *
danielebarchiesi@0 2654 * @param $label
danielebarchiesi@0 2655 * Text between the anchor tags.
danielebarchiesi@0 2656 * @param $index
danielebarchiesi@0 2657 * Link position counting from zero.
danielebarchiesi@0 2658 * @return
danielebarchiesi@0 2659 * Page on success, or FALSE on failure.
danielebarchiesi@0 2660 */
danielebarchiesi@0 2661 protected function clickLink($label, $index = 0) {
danielebarchiesi@0 2662 $url_before = $this->getUrl();
danielebarchiesi@0 2663 $urls = $this->xpath('//a[normalize-space(text())=:label]', array(':label' => $label));
danielebarchiesi@0 2664
danielebarchiesi@0 2665 if (isset($urls[$index])) {
danielebarchiesi@0 2666 $url_target = $this->getAbsoluteUrl($urls[$index]['href']);
danielebarchiesi@0 2667 }
danielebarchiesi@0 2668
danielebarchiesi@0 2669 $this->assertTrue(isset($urls[$index]), t('Clicked link %label (@url_target) from @url_before', array('%label' => $label, '@url_target' => $url_target, '@url_before' => $url_before)), t('Browser'));
danielebarchiesi@0 2670
danielebarchiesi@0 2671 if (isset($url_target)) {
danielebarchiesi@0 2672 return $this->drupalGet($url_target);
danielebarchiesi@0 2673 }
danielebarchiesi@0 2674 return FALSE;
danielebarchiesi@0 2675 }
danielebarchiesi@0 2676
danielebarchiesi@0 2677 /**
danielebarchiesi@0 2678 * Takes a path and returns an absolute path.
danielebarchiesi@0 2679 *
danielebarchiesi@0 2680 * @param $path
danielebarchiesi@0 2681 * A path from the internal browser content.
danielebarchiesi@0 2682 * @return
danielebarchiesi@0 2683 * The $path with $base_url prepended, if necessary.
danielebarchiesi@0 2684 */
danielebarchiesi@0 2685 protected function getAbsoluteUrl($path) {
danielebarchiesi@0 2686 global $base_url, $base_path;
danielebarchiesi@0 2687
danielebarchiesi@0 2688 $parts = parse_url($path);
danielebarchiesi@0 2689 if (empty($parts['host'])) {
danielebarchiesi@0 2690 // Ensure that we have a string (and no xpath object).
danielebarchiesi@0 2691 $path = (string) $path;
danielebarchiesi@0 2692 // Strip $base_path, if existent.
danielebarchiesi@0 2693 $length = strlen($base_path);
danielebarchiesi@0 2694 if (substr($path, 0, $length) === $base_path) {
danielebarchiesi@0 2695 $path = substr($path, $length);
danielebarchiesi@0 2696 }
danielebarchiesi@0 2697 // Ensure that we have an absolute path.
danielebarchiesi@0 2698 if ($path[0] !== '/') {
danielebarchiesi@0 2699 $path = '/' . $path;
danielebarchiesi@0 2700 }
danielebarchiesi@0 2701 // Finally, prepend the $base_url.
danielebarchiesi@0 2702 $path = $base_url . $path;
danielebarchiesi@0 2703 }
danielebarchiesi@0 2704 return $path;
danielebarchiesi@0 2705 }
danielebarchiesi@0 2706
danielebarchiesi@0 2707 /**
danielebarchiesi@0 2708 * Get the current URL from the cURL handler.
danielebarchiesi@0 2709 *
danielebarchiesi@0 2710 * @return
danielebarchiesi@0 2711 * The current URL.
danielebarchiesi@0 2712 */
danielebarchiesi@0 2713 protected function getUrl() {
danielebarchiesi@0 2714 return $this->url;
danielebarchiesi@0 2715 }
danielebarchiesi@0 2716
danielebarchiesi@0 2717 /**
danielebarchiesi@0 2718 * Gets the HTTP response headers of the requested page. Normally we are only
danielebarchiesi@0 2719 * interested in the headers returned by the last request. However, if a page
danielebarchiesi@0 2720 * is redirected or HTTP authentication is in use, multiple requests will be
danielebarchiesi@0 2721 * required to retrieve the page. Headers from all requests may be requested
danielebarchiesi@0 2722 * by passing TRUE to this function.
danielebarchiesi@0 2723 *
danielebarchiesi@0 2724 * @param $all_requests
danielebarchiesi@0 2725 * Boolean value specifying whether to return headers from all requests
danielebarchiesi@0 2726 * instead of just the last request. Defaults to FALSE.
danielebarchiesi@0 2727 * @return
danielebarchiesi@0 2728 * A name/value array if headers from only the last request are requested.
danielebarchiesi@0 2729 * If headers from all requests are requested, an array of name/value
danielebarchiesi@0 2730 * arrays, one for each request.
danielebarchiesi@0 2731 *
danielebarchiesi@0 2732 * The pseudonym ":status" is used for the HTTP status line.
danielebarchiesi@0 2733 *
danielebarchiesi@0 2734 * Values for duplicate headers are stored as a single comma-separated list.
danielebarchiesi@0 2735 */
danielebarchiesi@0 2736 protected function drupalGetHeaders($all_requests = FALSE) {
danielebarchiesi@0 2737 $request = 0;
danielebarchiesi@0 2738 $headers = array($request => array());
danielebarchiesi@0 2739 foreach ($this->headers as $header) {
danielebarchiesi@0 2740 $header = trim($header);
danielebarchiesi@0 2741 if ($header === '') {
danielebarchiesi@0 2742 $request++;
danielebarchiesi@0 2743 }
danielebarchiesi@0 2744 else {
danielebarchiesi@0 2745 if (strpos($header, 'HTTP/') === 0) {
danielebarchiesi@0 2746 $name = ':status';
danielebarchiesi@0 2747 $value = $header;
danielebarchiesi@0 2748 }
danielebarchiesi@0 2749 else {
danielebarchiesi@0 2750 list($name, $value) = explode(':', $header, 2);
danielebarchiesi@0 2751 $name = strtolower($name);
danielebarchiesi@0 2752 }
danielebarchiesi@0 2753 if (isset($headers[$request][$name])) {
danielebarchiesi@0 2754 $headers[$request][$name] .= ',' . trim($value);
danielebarchiesi@0 2755 }
danielebarchiesi@0 2756 else {
danielebarchiesi@0 2757 $headers[$request][$name] = trim($value);
danielebarchiesi@0 2758 }
danielebarchiesi@0 2759 }
danielebarchiesi@0 2760 }
danielebarchiesi@0 2761 if (!$all_requests) {
danielebarchiesi@0 2762 $headers = array_pop($headers);
danielebarchiesi@0 2763 }
danielebarchiesi@0 2764 return $headers;
danielebarchiesi@0 2765 }
danielebarchiesi@0 2766
danielebarchiesi@0 2767 /**
danielebarchiesi@0 2768 * Gets the value of an HTTP response header. If multiple requests were
danielebarchiesi@0 2769 * required to retrieve the page, only the headers from the last request will
danielebarchiesi@0 2770 * be checked by default. However, if TRUE is passed as the second argument,
danielebarchiesi@0 2771 * all requests will be processed from last to first until the header is
danielebarchiesi@0 2772 * found.
danielebarchiesi@0 2773 *
danielebarchiesi@0 2774 * @param $name
danielebarchiesi@0 2775 * The name of the header to retrieve. Names are case-insensitive (see RFC
danielebarchiesi@0 2776 * 2616 section 4.2).
danielebarchiesi@0 2777 * @param $all_requests
danielebarchiesi@0 2778 * Boolean value specifying whether to check all requests if the header is
danielebarchiesi@0 2779 * not found in the last request. Defaults to FALSE.
danielebarchiesi@0 2780 * @return
danielebarchiesi@0 2781 * The HTTP header value or FALSE if not found.
danielebarchiesi@0 2782 */
danielebarchiesi@0 2783 protected function drupalGetHeader($name, $all_requests = FALSE) {
danielebarchiesi@0 2784 $name = strtolower($name);
danielebarchiesi@0 2785 $header = FALSE;
danielebarchiesi@0 2786 if ($all_requests) {
danielebarchiesi@0 2787 foreach (array_reverse($this->drupalGetHeaders(TRUE)) as $headers) {
danielebarchiesi@0 2788 if (isset($headers[$name])) {
danielebarchiesi@0 2789 $header = $headers[$name];
danielebarchiesi@0 2790 break;
danielebarchiesi@0 2791 }
danielebarchiesi@0 2792 }
danielebarchiesi@0 2793 }
danielebarchiesi@0 2794 else {
danielebarchiesi@0 2795 $headers = $this->drupalGetHeaders();
danielebarchiesi@0 2796 if (isset($headers[$name])) {
danielebarchiesi@0 2797 $header = $headers[$name];
danielebarchiesi@0 2798 }
danielebarchiesi@0 2799 }
danielebarchiesi@0 2800 return $header;
danielebarchiesi@0 2801 }
danielebarchiesi@0 2802
danielebarchiesi@0 2803 /**
danielebarchiesi@0 2804 * Gets the current raw HTML of requested page.
danielebarchiesi@0 2805 */
danielebarchiesi@0 2806 protected function drupalGetContent() {
danielebarchiesi@0 2807 return $this->content;
danielebarchiesi@0 2808 }
danielebarchiesi@0 2809
danielebarchiesi@0 2810 /**
danielebarchiesi@0 2811 * Gets the value of the Drupal.settings JavaScript variable for the currently loaded page.
danielebarchiesi@0 2812 */
danielebarchiesi@0 2813 protected function drupalGetSettings() {
danielebarchiesi@0 2814 return $this->drupalSettings;
danielebarchiesi@0 2815 }
danielebarchiesi@0 2816
danielebarchiesi@0 2817 /**
danielebarchiesi@0 2818 * Gets an array containing all e-mails sent during this test case.
danielebarchiesi@0 2819 *
danielebarchiesi@0 2820 * @param $filter
danielebarchiesi@0 2821 * An array containing key/value pairs used to filter the e-mails that are returned.
danielebarchiesi@0 2822 * @return
danielebarchiesi@0 2823 * An array containing e-mail messages captured during the current test.
danielebarchiesi@0 2824 */
danielebarchiesi@0 2825 protected function drupalGetMails($filter = array()) {
danielebarchiesi@0 2826 $captured_emails = variable_get('drupal_test_email_collector', array());
danielebarchiesi@0 2827 $filtered_emails = array();
danielebarchiesi@0 2828
danielebarchiesi@0 2829 foreach ($captured_emails as $message) {
danielebarchiesi@0 2830 foreach ($filter as $key => $value) {
danielebarchiesi@0 2831 if (!isset($message[$key]) || $message[$key] != $value) {
danielebarchiesi@0 2832 continue 2;
danielebarchiesi@0 2833 }
danielebarchiesi@0 2834 }
danielebarchiesi@0 2835 $filtered_emails[] = $message;
danielebarchiesi@0 2836 }
danielebarchiesi@0 2837
danielebarchiesi@0 2838 return $filtered_emails;
danielebarchiesi@0 2839 }
danielebarchiesi@0 2840
danielebarchiesi@0 2841 /**
danielebarchiesi@0 2842 * Sets the raw HTML content. This can be useful when a page has been fetched
danielebarchiesi@0 2843 * outside of the internal browser and assertions need to be made on the
danielebarchiesi@0 2844 * returned page.
danielebarchiesi@0 2845 *
danielebarchiesi@0 2846 * A good example would be when testing drupal_http_request(). After fetching
danielebarchiesi@0 2847 * the page the content can be set and page elements can be checked to ensure
danielebarchiesi@0 2848 * that the function worked properly.
danielebarchiesi@0 2849 */
danielebarchiesi@0 2850 protected function drupalSetContent($content, $url = 'internal:') {
danielebarchiesi@0 2851 $this->content = $content;
danielebarchiesi@0 2852 $this->url = $url;
danielebarchiesi@0 2853 $this->plainTextContent = FALSE;
danielebarchiesi@0 2854 $this->elements = FALSE;
danielebarchiesi@0 2855 $this->drupalSettings = array();
danielebarchiesi@0 2856 if (preg_match('/jQuery\.extend\(Drupal\.settings, (.*?)\);/', $content, $matches)) {
danielebarchiesi@0 2857 $this->drupalSettings = drupal_json_decode($matches[1]);
danielebarchiesi@0 2858 }
danielebarchiesi@0 2859 }
danielebarchiesi@0 2860
danielebarchiesi@0 2861 /**
danielebarchiesi@0 2862 * Sets the value of the Drupal.settings JavaScript variable for the currently loaded page.
danielebarchiesi@0 2863 */
danielebarchiesi@0 2864 protected function drupalSetSettings($settings) {
danielebarchiesi@0 2865 $this->drupalSettings = $settings;
danielebarchiesi@0 2866 }
danielebarchiesi@0 2867
danielebarchiesi@0 2868 /**
danielebarchiesi@0 2869 * Pass if the internal browser's URL matches the given path.
danielebarchiesi@0 2870 *
danielebarchiesi@0 2871 * @param $path
danielebarchiesi@0 2872 * The expected system path.
danielebarchiesi@0 2873 * @param $options
danielebarchiesi@0 2874 * (optional) Any additional options to pass for $path to url().
danielebarchiesi@0 2875 * @param $message
danielebarchiesi@0 2876 * Message to display.
danielebarchiesi@0 2877 * @param $group
danielebarchiesi@0 2878 * The group this message belongs to, defaults to 'Other'.
danielebarchiesi@0 2879 *
danielebarchiesi@0 2880 * @return
danielebarchiesi@0 2881 * TRUE on pass, FALSE on fail.
danielebarchiesi@0 2882 */
danielebarchiesi@0 2883 protected function assertUrl($path, array $options = array(), $message = '', $group = 'Other') {
danielebarchiesi@0 2884 if (!$message) {
danielebarchiesi@0 2885 $message = t('Current URL is @url.', array(
danielebarchiesi@0 2886 '@url' => var_export(url($path, $options), TRUE),
danielebarchiesi@0 2887 ));
danielebarchiesi@0 2888 }
danielebarchiesi@0 2889 $options['absolute'] = TRUE;
danielebarchiesi@0 2890 return $this->assertEqual($this->getUrl(), url($path, $options), $message, $group);
danielebarchiesi@0 2891 }
danielebarchiesi@0 2892
danielebarchiesi@0 2893 /**
danielebarchiesi@0 2894 * Pass if the raw text IS found on the loaded page, fail otherwise. Raw text
danielebarchiesi@0 2895 * refers to the raw HTML that the page generated.
danielebarchiesi@0 2896 *
danielebarchiesi@0 2897 * @param $raw
danielebarchiesi@0 2898 * Raw (HTML) string to look for.
danielebarchiesi@0 2899 * @param $message
danielebarchiesi@0 2900 * Message to display.
danielebarchiesi@0 2901 * @param $group
danielebarchiesi@0 2902 * The group this message belongs to, defaults to 'Other'.
danielebarchiesi@0 2903 * @return
danielebarchiesi@0 2904 * TRUE on pass, FALSE on fail.
danielebarchiesi@0 2905 */
danielebarchiesi@0 2906 protected function assertRaw($raw, $message = '', $group = 'Other') {
danielebarchiesi@0 2907 if (!$message) {
danielebarchiesi@0 2908 $message = t('Raw "@raw" found', array('@raw' => $raw));
danielebarchiesi@0 2909 }
danielebarchiesi@0 2910 return $this->assert(strpos($this->drupalGetContent(), $raw) !== FALSE, $message, $group);
danielebarchiesi@0 2911 }
danielebarchiesi@0 2912
danielebarchiesi@0 2913 /**
danielebarchiesi@0 2914 * Pass if the raw text is NOT found on the loaded page, fail otherwise. Raw text
danielebarchiesi@0 2915 * refers to the raw HTML that the page generated.
danielebarchiesi@0 2916 *
danielebarchiesi@0 2917 * @param $raw
danielebarchiesi@0 2918 * Raw (HTML) string to look for.
danielebarchiesi@0 2919 * @param $message
danielebarchiesi@0 2920 * Message to display.
danielebarchiesi@0 2921 * @param $group
danielebarchiesi@0 2922 * The group this message belongs to, defaults to 'Other'.
danielebarchiesi@0 2923 * @return
danielebarchiesi@0 2924 * TRUE on pass, FALSE on fail.
danielebarchiesi@0 2925 */
danielebarchiesi@0 2926 protected function assertNoRaw($raw, $message = '', $group = 'Other') {
danielebarchiesi@0 2927 if (!$message) {
danielebarchiesi@0 2928 $message = t('Raw "@raw" not found', array('@raw' => $raw));
danielebarchiesi@0 2929 }
danielebarchiesi@0 2930 return $this->assert(strpos($this->drupalGetContent(), $raw) === FALSE, $message, $group);
danielebarchiesi@0 2931 }
danielebarchiesi@0 2932
danielebarchiesi@0 2933 /**
danielebarchiesi@0 2934 * Pass if the text IS found on the text version of the page. The text version
danielebarchiesi@0 2935 * is the equivalent of what a user would see when viewing through a web browser.
danielebarchiesi@0 2936 * In other words the HTML has been filtered out of the contents.
danielebarchiesi@0 2937 *
danielebarchiesi@0 2938 * @param $text
danielebarchiesi@0 2939 * Plain text to look for.
danielebarchiesi@0 2940 * @param $message
danielebarchiesi@0 2941 * Message to display.
danielebarchiesi@0 2942 * @param $group
danielebarchiesi@0 2943 * The group this message belongs to, defaults to 'Other'.
danielebarchiesi@0 2944 * @return
danielebarchiesi@0 2945 * TRUE on pass, FALSE on fail.
danielebarchiesi@0 2946 */
danielebarchiesi@0 2947 protected function assertText($text, $message = '', $group = 'Other') {
danielebarchiesi@0 2948 return $this->assertTextHelper($text, $message, $group, FALSE);
danielebarchiesi@0 2949 }
danielebarchiesi@0 2950
danielebarchiesi@0 2951 /**
danielebarchiesi@0 2952 * Pass if the text is NOT found on the text version of the page. The text version
danielebarchiesi@0 2953 * is the equivalent of what a user would see when viewing through a web browser.
danielebarchiesi@0 2954 * In other words the HTML has been filtered out of the contents.
danielebarchiesi@0 2955 *
danielebarchiesi@0 2956 * @param $text
danielebarchiesi@0 2957 * Plain text to look for.
danielebarchiesi@0 2958 * @param $message
danielebarchiesi@0 2959 * Message to display.
danielebarchiesi@0 2960 * @param $group
danielebarchiesi@0 2961 * The group this message belongs to, defaults to 'Other'.
danielebarchiesi@0 2962 * @return
danielebarchiesi@0 2963 * TRUE on pass, FALSE on fail.
danielebarchiesi@0 2964 */
danielebarchiesi@0 2965 protected function assertNoText($text, $message = '', $group = 'Other') {
danielebarchiesi@0 2966 return $this->assertTextHelper($text, $message, $group, TRUE);
danielebarchiesi@0 2967 }
danielebarchiesi@0 2968
danielebarchiesi@0 2969 /**
danielebarchiesi@0 2970 * Helper for assertText and assertNoText.
danielebarchiesi@0 2971 *
danielebarchiesi@0 2972 * It is not recommended to call this function directly.
danielebarchiesi@0 2973 *
danielebarchiesi@0 2974 * @param $text
danielebarchiesi@0 2975 * Plain text to look for.
danielebarchiesi@0 2976 * @param $message
danielebarchiesi@0 2977 * Message to display.
danielebarchiesi@0 2978 * @param $group
danielebarchiesi@0 2979 * The group this message belongs to.
danielebarchiesi@0 2980 * @param $not_exists
danielebarchiesi@0 2981 * TRUE if this text should not exist, FALSE if it should.
danielebarchiesi@0 2982 * @return
danielebarchiesi@0 2983 * TRUE on pass, FALSE on fail.
danielebarchiesi@0 2984 */
danielebarchiesi@0 2985 protected function assertTextHelper($text, $message = '', $group, $not_exists) {
danielebarchiesi@0 2986 if ($this->plainTextContent === FALSE) {
danielebarchiesi@0 2987 $this->plainTextContent = filter_xss($this->drupalGetContent(), array());
danielebarchiesi@0 2988 }
danielebarchiesi@0 2989 if (!$message) {
danielebarchiesi@0 2990 $message = !$not_exists ? t('"@text" found', array('@text' => $text)) : t('"@text" not found', array('@text' => $text));
danielebarchiesi@0 2991 }
danielebarchiesi@0 2992 return $this->assert($not_exists == (strpos($this->plainTextContent, $text) === FALSE), $message, $group);
danielebarchiesi@0 2993 }
danielebarchiesi@0 2994
danielebarchiesi@0 2995 /**
danielebarchiesi@0 2996 * Pass if the text is found ONLY ONCE on the text version of the page.
danielebarchiesi@0 2997 *
danielebarchiesi@0 2998 * The text version is the equivalent of what a user would see when viewing
danielebarchiesi@0 2999 * through a web browser. In other words the HTML has been filtered out of
danielebarchiesi@0 3000 * the contents.
danielebarchiesi@0 3001 *
danielebarchiesi@0 3002 * @param $text
danielebarchiesi@0 3003 * Plain text to look for.
danielebarchiesi@0 3004 * @param $message
danielebarchiesi@0 3005 * Message to display.
danielebarchiesi@0 3006 * @param $group
danielebarchiesi@0 3007 * The group this message belongs to, defaults to 'Other'.
danielebarchiesi@0 3008 * @return
danielebarchiesi@0 3009 * TRUE on pass, FALSE on fail.
danielebarchiesi@0 3010 */
danielebarchiesi@0 3011 protected function assertUniqueText($text, $message = '', $group = 'Other') {
danielebarchiesi@0 3012 return $this->assertUniqueTextHelper($text, $message, $group, TRUE);
danielebarchiesi@0 3013 }
danielebarchiesi@0 3014
danielebarchiesi@0 3015 /**
danielebarchiesi@0 3016 * Pass if the text is found MORE THAN ONCE on the text version of the page.
danielebarchiesi@0 3017 *
danielebarchiesi@0 3018 * The text version is the equivalent of what a user would see when viewing
danielebarchiesi@0 3019 * through a web browser. In other words the HTML has been filtered out of
danielebarchiesi@0 3020 * the contents.
danielebarchiesi@0 3021 *
danielebarchiesi@0 3022 * @param $text
danielebarchiesi@0 3023 * Plain text to look for.
danielebarchiesi@0 3024 * @param $message
danielebarchiesi@0 3025 * Message to display.
danielebarchiesi@0 3026 * @param $group
danielebarchiesi@0 3027 * The group this message belongs to, defaults to 'Other'.
danielebarchiesi@0 3028 * @return
danielebarchiesi@0 3029 * TRUE on pass, FALSE on fail.
danielebarchiesi@0 3030 */
danielebarchiesi@0 3031 protected function assertNoUniqueText($text, $message = '', $group = 'Other') {
danielebarchiesi@0 3032 return $this->assertUniqueTextHelper($text, $message, $group, FALSE);
danielebarchiesi@0 3033 }
danielebarchiesi@0 3034
danielebarchiesi@0 3035 /**
danielebarchiesi@0 3036 * Helper for assertUniqueText and assertNoUniqueText.
danielebarchiesi@0 3037 *
danielebarchiesi@0 3038 * It is not recommended to call this function directly.
danielebarchiesi@0 3039 *
danielebarchiesi@0 3040 * @param $text
danielebarchiesi@0 3041 * Plain text to look for.
danielebarchiesi@0 3042 * @param $message
danielebarchiesi@0 3043 * Message to display.
danielebarchiesi@0 3044 * @param $group
danielebarchiesi@0 3045 * The group this message belongs to.
danielebarchiesi@0 3046 * @param $be_unique
danielebarchiesi@0 3047 * TRUE if this text should be found only once, FALSE if it should be found more than once.
danielebarchiesi@0 3048 * @return
danielebarchiesi@0 3049 * TRUE on pass, FALSE on fail.
danielebarchiesi@0 3050 */
danielebarchiesi@0 3051 protected function assertUniqueTextHelper($text, $message = '', $group, $be_unique) {
danielebarchiesi@0 3052 if ($this->plainTextContent === FALSE) {
danielebarchiesi@0 3053 $this->plainTextContent = filter_xss($this->drupalGetContent(), array());
danielebarchiesi@0 3054 }
danielebarchiesi@0 3055 if (!$message) {
danielebarchiesi@0 3056 $message = '"' . $text . '"' . ($be_unique ? ' found only once' : ' found more than once');
danielebarchiesi@0 3057 }
danielebarchiesi@0 3058 $first_occurance = strpos($this->plainTextContent, $text);
danielebarchiesi@0 3059 if ($first_occurance === FALSE) {
danielebarchiesi@0 3060 return $this->assert(FALSE, $message, $group);
danielebarchiesi@0 3061 }
danielebarchiesi@0 3062 $offset = $first_occurance + strlen($text);
danielebarchiesi@0 3063 $second_occurance = strpos($this->plainTextContent, $text, $offset);
danielebarchiesi@0 3064 return $this->assert($be_unique == ($second_occurance === FALSE), $message, $group);
danielebarchiesi@0 3065 }
danielebarchiesi@0 3066
danielebarchiesi@0 3067 /**
danielebarchiesi@0 3068 * Will trigger a pass if the Perl regex pattern is found in the raw content.
danielebarchiesi@0 3069 *
danielebarchiesi@0 3070 * @param $pattern
danielebarchiesi@0 3071 * Perl regex to look for including the regex delimiters.
danielebarchiesi@0 3072 * @param $message
danielebarchiesi@0 3073 * Message to display.
danielebarchiesi@0 3074 * @param $group
danielebarchiesi@0 3075 * The group this message belongs to.
danielebarchiesi@0 3076 * @return
danielebarchiesi@0 3077 * TRUE on pass, FALSE on fail.
danielebarchiesi@0 3078 */
danielebarchiesi@0 3079 protected function assertPattern($pattern, $message = '', $group = 'Other') {
danielebarchiesi@0 3080 if (!$message) {
danielebarchiesi@0 3081 $message = t('Pattern "@pattern" found', array('@pattern' => $pattern));
danielebarchiesi@0 3082 }
danielebarchiesi@0 3083 return $this->assert((bool) preg_match($pattern, $this->drupalGetContent()), $message, $group);
danielebarchiesi@0 3084 }
danielebarchiesi@0 3085
danielebarchiesi@0 3086 /**
danielebarchiesi@0 3087 * Will trigger a pass if the perl regex pattern is not present in raw content.
danielebarchiesi@0 3088 *
danielebarchiesi@0 3089 * @param $pattern
danielebarchiesi@0 3090 * Perl regex to look for including the regex delimiters.
danielebarchiesi@0 3091 * @param $message
danielebarchiesi@0 3092 * Message to display.
danielebarchiesi@0 3093 * @param $group
danielebarchiesi@0 3094 * The group this message belongs to.
danielebarchiesi@0 3095 * @return
danielebarchiesi@0 3096 * TRUE on pass, FALSE on fail.
danielebarchiesi@0 3097 */
danielebarchiesi@0 3098 protected function assertNoPattern($pattern, $message = '', $group = 'Other') {
danielebarchiesi@0 3099 if (!$message) {
danielebarchiesi@0 3100 $message = t('Pattern "@pattern" not found', array('@pattern' => $pattern));
danielebarchiesi@0 3101 }
danielebarchiesi@0 3102 return $this->assert(!preg_match($pattern, $this->drupalGetContent()), $message, $group);
danielebarchiesi@0 3103 }
danielebarchiesi@0 3104
danielebarchiesi@0 3105 /**
danielebarchiesi@0 3106 * Pass if the page title is the given string.
danielebarchiesi@0 3107 *
danielebarchiesi@0 3108 * @param $title
danielebarchiesi@0 3109 * The string the title should be.
danielebarchiesi@0 3110 * @param $message
danielebarchiesi@0 3111 * Message to display.
danielebarchiesi@0 3112 * @param $group
danielebarchiesi@0 3113 * The group this message belongs to.
danielebarchiesi@0 3114 * @return
danielebarchiesi@0 3115 * TRUE on pass, FALSE on fail.
danielebarchiesi@0 3116 */
danielebarchiesi@0 3117 protected function assertTitle($title, $message = '', $group = 'Other') {
danielebarchiesi@0 3118 $actual = (string) current($this->xpath('//title'));
danielebarchiesi@0 3119 if (!$message) {
danielebarchiesi@0 3120 $message = t('Page title @actual is equal to @expected.', array(
danielebarchiesi@0 3121 '@actual' => var_export($actual, TRUE),
danielebarchiesi@0 3122 '@expected' => var_export($title, TRUE),
danielebarchiesi@0 3123 ));
danielebarchiesi@0 3124 }
danielebarchiesi@0 3125 return $this->assertEqual($actual, $title, $message, $group);
danielebarchiesi@0 3126 }
danielebarchiesi@0 3127
danielebarchiesi@0 3128 /**
danielebarchiesi@0 3129 * Pass if the page title is not the given string.
danielebarchiesi@0 3130 *
danielebarchiesi@0 3131 * @param $title
danielebarchiesi@0 3132 * The string the title should not be.
danielebarchiesi@0 3133 * @param $message
danielebarchiesi@0 3134 * Message to display.
danielebarchiesi@0 3135 * @param $group
danielebarchiesi@0 3136 * The group this message belongs to.
danielebarchiesi@0 3137 * @return
danielebarchiesi@0 3138 * TRUE on pass, FALSE on fail.
danielebarchiesi@0 3139 */
danielebarchiesi@0 3140 protected function assertNoTitle($title, $message = '', $group = 'Other') {
danielebarchiesi@0 3141 $actual = (string) current($this->xpath('//title'));
danielebarchiesi@0 3142 if (!$message) {
danielebarchiesi@0 3143 $message = t('Page title @actual is not equal to @unexpected.', array(
danielebarchiesi@0 3144 '@actual' => var_export($actual, TRUE),
danielebarchiesi@0 3145 '@unexpected' => var_export($title, TRUE),
danielebarchiesi@0 3146 ));
danielebarchiesi@0 3147 }
danielebarchiesi@0 3148 return $this->assertNotEqual($actual, $title, $message, $group);
danielebarchiesi@0 3149 }
danielebarchiesi@0 3150
danielebarchiesi@0 3151 /**
danielebarchiesi@0 3152 * Asserts themed output.
danielebarchiesi@0 3153 *
danielebarchiesi@0 3154 * @param $callback
danielebarchiesi@0 3155 * The name of the theme function to invoke; e.g. 'links' for theme_links().
danielebarchiesi@0 3156 * @param $variables
danielebarchiesi@0 3157 * An array of variables to pass to the theme function.
danielebarchiesi@0 3158 * @param $expected
danielebarchiesi@0 3159 * The expected themed output string.
danielebarchiesi@0 3160 * @param $message
danielebarchiesi@0 3161 * (optional) A message to display with the assertion. Do not translate
danielebarchiesi@0 3162 * messages: use format_string() to embed variables in the message text, not
danielebarchiesi@0 3163 * t(). If left blank, a default message will be displayed.
danielebarchiesi@0 3164 * @param $group
danielebarchiesi@0 3165 * (optional) The group this message is in, which is displayed in a column
danielebarchiesi@0 3166 * in test output. Use 'Debug' to indicate this is debugging output. Do not
danielebarchiesi@0 3167 * translate this string. Defaults to 'Other'; most tests do not override
danielebarchiesi@0 3168 * this default.
danielebarchiesi@0 3169 *
danielebarchiesi@0 3170 * @return
danielebarchiesi@0 3171 * TRUE on pass, FALSE on fail.
danielebarchiesi@0 3172 */
danielebarchiesi@0 3173 protected function assertThemeOutput($callback, array $variables = array(), $expected, $message = '', $group = 'Other') {
danielebarchiesi@0 3174 $output = theme($callback, $variables);
danielebarchiesi@0 3175 $this->verbose('Variables:' . '<pre>' . check_plain(var_export($variables, TRUE)) . '</pre>'
danielebarchiesi@0 3176 . '<hr />' . 'Result:' . '<pre>' . check_plain(var_export($output, TRUE)) . '</pre>'
danielebarchiesi@0 3177 . '<hr />' . 'Expected:' . '<pre>' . check_plain(var_export($expected, TRUE)) . '</pre>'
danielebarchiesi@0 3178 . '<hr />' . $output
danielebarchiesi@0 3179 );
danielebarchiesi@0 3180 if (!$message) {
danielebarchiesi@0 3181 $message = '%callback rendered correctly.';
danielebarchiesi@0 3182 }
danielebarchiesi@0 3183 $message = format_string($message, array('%callback' => 'theme_' . $callback . '()'));
danielebarchiesi@0 3184 return $this->assertIdentical($output, $expected, $message, $group);
danielebarchiesi@0 3185 }
danielebarchiesi@0 3186
danielebarchiesi@0 3187 /**
danielebarchiesi@0 3188 * Asserts that a field exists in the current page by the given XPath.
danielebarchiesi@0 3189 *
danielebarchiesi@0 3190 * @param $xpath
danielebarchiesi@0 3191 * XPath used to find the field.
danielebarchiesi@0 3192 * @param $value
danielebarchiesi@0 3193 * (optional) Value of the field to assert.
danielebarchiesi@0 3194 * @param $message
danielebarchiesi@0 3195 * (optional) Message to display.
danielebarchiesi@0 3196 * @param $group
danielebarchiesi@0 3197 * (optional) The group this message belongs to.
danielebarchiesi@0 3198 *
danielebarchiesi@0 3199 * @return
danielebarchiesi@0 3200 * TRUE on pass, FALSE on fail.
danielebarchiesi@0 3201 */
danielebarchiesi@0 3202 protected function assertFieldByXPath($xpath, $value = NULL, $message = '', $group = 'Other') {
danielebarchiesi@0 3203 $fields = $this->xpath($xpath);
danielebarchiesi@0 3204
danielebarchiesi@0 3205 // If value specified then check array for match.
danielebarchiesi@0 3206 $found = TRUE;
danielebarchiesi@0 3207 if (isset($value)) {
danielebarchiesi@0 3208 $found = FALSE;
danielebarchiesi@0 3209 if ($fields) {
danielebarchiesi@0 3210 foreach ($fields as $field) {
danielebarchiesi@0 3211 if (isset($field['value']) && $field['value'] == $value) {
danielebarchiesi@0 3212 // Input element with correct value.
danielebarchiesi@0 3213 $found = TRUE;
danielebarchiesi@0 3214 }
danielebarchiesi@0 3215 elseif (isset($field->option)) {
danielebarchiesi@0 3216 // Select element found.
danielebarchiesi@0 3217 if ($this->getSelectedItem($field) == $value) {
danielebarchiesi@0 3218 $found = TRUE;
danielebarchiesi@0 3219 }
danielebarchiesi@0 3220 else {
danielebarchiesi@0 3221 // No item selected so use first item.
danielebarchiesi@0 3222 $items = $this->getAllOptions($field);
danielebarchiesi@0 3223 if (!empty($items) && $items[0]['value'] == $value) {
danielebarchiesi@0 3224 $found = TRUE;
danielebarchiesi@0 3225 }
danielebarchiesi@0 3226 }
danielebarchiesi@0 3227 }
danielebarchiesi@0 3228 elseif ((string) $field == $value) {
danielebarchiesi@0 3229 // Text area with correct text.
danielebarchiesi@0 3230 $found = TRUE;
danielebarchiesi@0 3231 }
danielebarchiesi@0 3232 }
danielebarchiesi@0 3233 }
danielebarchiesi@0 3234 }
danielebarchiesi@0 3235 return $this->assertTrue($fields && $found, $message, $group);
danielebarchiesi@0 3236 }
danielebarchiesi@0 3237
danielebarchiesi@0 3238 /**
danielebarchiesi@0 3239 * Get the selected value from a select field.
danielebarchiesi@0 3240 *
danielebarchiesi@0 3241 * @param $element
danielebarchiesi@0 3242 * SimpleXMLElement select element.
danielebarchiesi@0 3243 * @return
danielebarchiesi@0 3244 * The selected value or FALSE.
danielebarchiesi@0 3245 */
danielebarchiesi@0 3246 protected function getSelectedItem(SimpleXMLElement $element) {
danielebarchiesi@0 3247 foreach ($element->children() as $item) {
danielebarchiesi@0 3248 if (isset($item['selected'])) {
danielebarchiesi@0 3249 return $item['value'];
danielebarchiesi@0 3250 }
danielebarchiesi@0 3251 elseif ($item->getName() == 'optgroup') {
danielebarchiesi@0 3252 if ($value = $this->getSelectedItem($item)) {
danielebarchiesi@0 3253 return $value;
danielebarchiesi@0 3254 }
danielebarchiesi@0 3255 }
danielebarchiesi@0 3256 }
danielebarchiesi@0 3257 return FALSE;
danielebarchiesi@0 3258 }
danielebarchiesi@0 3259
danielebarchiesi@0 3260 /**
danielebarchiesi@0 3261 * Asserts that a field does not exist in the current page by the given XPath.
danielebarchiesi@0 3262 *
danielebarchiesi@0 3263 * @param $xpath
danielebarchiesi@0 3264 * XPath used to find the field.
danielebarchiesi@0 3265 * @param $value
danielebarchiesi@0 3266 * (optional) Value of the field to assert.
danielebarchiesi@0 3267 * @param $message
danielebarchiesi@0 3268 * (optional) Message to display.
danielebarchiesi@0 3269 * @param $group
danielebarchiesi@0 3270 * (optional) The group this message belongs to.
danielebarchiesi@0 3271 *
danielebarchiesi@0 3272 * @return
danielebarchiesi@0 3273 * TRUE on pass, FALSE on fail.
danielebarchiesi@0 3274 */
danielebarchiesi@0 3275 protected function assertNoFieldByXPath($xpath, $value = NULL, $message = '', $group = 'Other') {
danielebarchiesi@0 3276 $fields = $this->xpath($xpath);
danielebarchiesi@0 3277
danielebarchiesi@0 3278 // If value specified then check array for match.
danielebarchiesi@0 3279 $found = TRUE;
danielebarchiesi@0 3280 if (isset($value)) {
danielebarchiesi@0 3281 $found = FALSE;
danielebarchiesi@0 3282 if ($fields) {
danielebarchiesi@0 3283 foreach ($fields as $field) {
danielebarchiesi@0 3284 if ($field['value'] == $value) {
danielebarchiesi@0 3285 $found = TRUE;
danielebarchiesi@0 3286 }
danielebarchiesi@0 3287 }
danielebarchiesi@0 3288 }
danielebarchiesi@0 3289 }
danielebarchiesi@0 3290 return $this->assertFalse($fields && $found, $message, $group);
danielebarchiesi@0 3291 }
danielebarchiesi@0 3292
danielebarchiesi@0 3293 /**
danielebarchiesi@0 3294 * Asserts that a field exists in the current page with the given name and value.
danielebarchiesi@0 3295 *
danielebarchiesi@0 3296 * @param $name
danielebarchiesi@0 3297 * Name of field to assert.
danielebarchiesi@0 3298 * @param $value
danielebarchiesi@0 3299 * Value of the field to assert.
danielebarchiesi@0 3300 * @param $message
danielebarchiesi@0 3301 * Message to display.
danielebarchiesi@0 3302 * @param $group
danielebarchiesi@0 3303 * The group this message belongs to.
danielebarchiesi@0 3304 * @return
danielebarchiesi@0 3305 * TRUE on pass, FALSE on fail.
danielebarchiesi@0 3306 */
danielebarchiesi@0 3307 protected function assertFieldByName($name, $value = NULL, $message = NULL) {
danielebarchiesi@0 3308 if (!isset($message)) {
danielebarchiesi@0 3309 if (!isset($value)) {
danielebarchiesi@0 3310 $message = t('Found field with name @name', array(
danielebarchiesi@0 3311 '@name' => var_export($name, TRUE),
danielebarchiesi@0 3312 ));
danielebarchiesi@0 3313 }
danielebarchiesi@0 3314 else {
danielebarchiesi@0 3315 $message = t('Found field with name @name and value @value', array(
danielebarchiesi@0 3316 '@name' => var_export($name, TRUE),
danielebarchiesi@0 3317 '@value' => var_export($value, TRUE),
danielebarchiesi@0 3318 ));
danielebarchiesi@0 3319 }
danielebarchiesi@0 3320 }
danielebarchiesi@0 3321 return $this->assertFieldByXPath($this->constructFieldXpath('name', $name), $value, $message, t('Browser'));
danielebarchiesi@0 3322 }
danielebarchiesi@0 3323
danielebarchiesi@0 3324 /**
danielebarchiesi@0 3325 * Asserts that a field does not exist with the given name and value.
danielebarchiesi@0 3326 *
danielebarchiesi@0 3327 * @param $name
danielebarchiesi@0 3328 * Name of field to assert.
danielebarchiesi@0 3329 * @param $value
danielebarchiesi@0 3330 * Value of the field to assert.
danielebarchiesi@0 3331 * @param $message
danielebarchiesi@0 3332 * Message to display.
danielebarchiesi@0 3333 * @param $group
danielebarchiesi@0 3334 * The group this message belongs to.
danielebarchiesi@0 3335 * @return
danielebarchiesi@0 3336 * TRUE on pass, FALSE on fail.
danielebarchiesi@0 3337 */
danielebarchiesi@0 3338 protected function assertNoFieldByName($name, $value = '', $message = '') {
danielebarchiesi@0 3339 return $this->assertNoFieldByXPath($this->constructFieldXpath('name', $name), $value, $message ? $message : t('Did not find field by name @name', array('@name' => $name)), t('Browser'));
danielebarchiesi@0 3340 }
danielebarchiesi@0 3341
danielebarchiesi@0 3342 /**
danielebarchiesi@0 3343 * Asserts that a field exists in the current page with the given id and value.
danielebarchiesi@0 3344 *
danielebarchiesi@0 3345 * @param $id
danielebarchiesi@0 3346 * Id of field to assert.
danielebarchiesi@0 3347 * @param $value
danielebarchiesi@0 3348 * Value of the field to assert.
danielebarchiesi@0 3349 * @param $message
danielebarchiesi@0 3350 * Message to display.
danielebarchiesi@0 3351 * @param $group
danielebarchiesi@0 3352 * The group this message belongs to.
danielebarchiesi@0 3353 * @return
danielebarchiesi@0 3354 * TRUE on pass, FALSE on fail.
danielebarchiesi@0 3355 */
danielebarchiesi@0 3356 protected function assertFieldById($id, $value = '', $message = '') {
danielebarchiesi@0 3357 return $this->assertFieldByXPath($this->constructFieldXpath('id', $id), $value, $message ? $message : t('Found field by id @id', array('@id' => $id)), t('Browser'));
danielebarchiesi@0 3358 }
danielebarchiesi@0 3359
danielebarchiesi@0 3360 /**
danielebarchiesi@0 3361 * Asserts that a field does not exist with the given id and value.
danielebarchiesi@0 3362 *
danielebarchiesi@0 3363 * @param $id
danielebarchiesi@0 3364 * Id of field to assert.
danielebarchiesi@0 3365 * @param $value
danielebarchiesi@0 3366 * Value of the field to assert.
danielebarchiesi@0 3367 * @param $message
danielebarchiesi@0 3368 * Message to display.
danielebarchiesi@0 3369 * @param $group
danielebarchiesi@0 3370 * The group this message belongs to.
danielebarchiesi@0 3371 * @return
danielebarchiesi@0 3372 * TRUE on pass, FALSE on fail.
danielebarchiesi@0 3373 */
danielebarchiesi@0 3374 protected function assertNoFieldById($id, $value = '', $message = '') {
danielebarchiesi@0 3375 return $this->assertNoFieldByXPath($this->constructFieldXpath('id', $id), $value, $message ? $message : t('Did not find field by id @id', array('@id' => $id)), t('Browser'));
danielebarchiesi@0 3376 }
danielebarchiesi@0 3377
danielebarchiesi@0 3378 /**
danielebarchiesi@0 3379 * Asserts that a checkbox field in the current page is checked.
danielebarchiesi@0 3380 *
danielebarchiesi@0 3381 * @param $id
danielebarchiesi@0 3382 * Id of field to assert.
danielebarchiesi@0 3383 * @param $message
danielebarchiesi@0 3384 * Message to display.
danielebarchiesi@0 3385 * @return
danielebarchiesi@0 3386 * TRUE on pass, FALSE on fail.
danielebarchiesi@0 3387 */
danielebarchiesi@0 3388 protected function assertFieldChecked($id, $message = '') {
danielebarchiesi@0 3389 $elements = $this->xpath('//input[@id=:id]', array(':id' => $id));
danielebarchiesi@0 3390 return $this->assertTrue(isset($elements[0]) && !empty($elements[0]['checked']), $message ? $message : t('Checkbox field @id is checked.', array('@id' => $id)), t('Browser'));
danielebarchiesi@0 3391 }
danielebarchiesi@0 3392
danielebarchiesi@0 3393 /**
danielebarchiesi@0 3394 * Asserts that a checkbox field in the current page is not checked.
danielebarchiesi@0 3395 *
danielebarchiesi@0 3396 * @param $id
danielebarchiesi@0 3397 * Id of field to assert.
danielebarchiesi@0 3398 * @param $message
danielebarchiesi@0 3399 * Message to display.
danielebarchiesi@0 3400 * @return
danielebarchiesi@0 3401 * TRUE on pass, FALSE on fail.
danielebarchiesi@0 3402 */
danielebarchiesi@0 3403 protected function assertNoFieldChecked($id, $message = '') {
danielebarchiesi@0 3404 $elements = $this->xpath('//input[@id=:id]', array(':id' => $id));
danielebarchiesi@0 3405 return $this->assertTrue(isset($elements[0]) && empty($elements[0]['checked']), $message ? $message : t('Checkbox field @id is not checked.', array('@id' => $id)), t('Browser'));
danielebarchiesi@0 3406 }
danielebarchiesi@0 3407
danielebarchiesi@0 3408 /**
danielebarchiesi@0 3409 * Asserts that a select option in the current page is checked.
danielebarchiesi@0 3410 *
danielebarchiesi@0 3411 * @param $id
danielebarchiesi@0 3412 * Id of select field to assert.
danielebarchiesi@0 3413 * @param $option
danielebarchiesi@0 3414 * Option to assert.
danielebarchiesi@0 3415 * @param $message
danielebarchiesi@0 3416 * Message to display.
danielebarchiesi@0 3417 * @return
danielebarchiesi@0 3418 * TRUE on pass, FALSE on fail.
danielebarchiesi@0 3419 *
danielebarchiesi@0 3420 * @todo $id is unusable. Replace with $name.
danielebarchiesi@0 3421 */
danielebarchiesi@0 3422 protected function assertOptionSelected($id, $option, $message = '') {
danielebarchiesi@0 3423 $elements = $this->xpath('//select[@id=:id]//option[@value=:option]', array(':id' => $id, ':option' => $option));
danielebarchiesi@0 3424 return $this->assertTrue(isset($elements[0]) && !empty($elements[0]['selected']), $message ? $message : t('Option @option for field @id is selected.', array('@option' => $option, '@id' => $id)), t('Browser'));
danielebarchiesi@0 3425 }
danielebarchiesi@0 3426
danielebarchiesi@0 3427 /**
danielebarchiesi@0 3428 * Asserts that a select option in the current page is not checked.
danielebarchiesi@0 3429 *
danielebarchiesi@0 3430 * @param $id
danielebarchiesi@0 3431 * Id of select field to assert.
danielebarchiesi@0 3432 * @param $option
danielebarchiesi@0 3433 * Option to assert.
danielebarchiesi@0 3434 * @param $message
danielebarchiesi@0 3435 * Message to display.
danielebarchiesi@0 3436 * @return
danielebarchiesi@0 3437 * TRUE on pass, FALSE on fail.
danielebarchiesi@0 3438 */
danielebarchiesi@0 3439 protected function assertNoOptionSelected($id, $option, $message = '') {
danielebarchiesi@0 3440 $elements = $this->xpath('//select[@id=:id]//option[@value=:option]', array(':id' => $id, ':option' => $option));
danielebarchiesi@0 3441 return $this->assertTrue(isset($elements[0]) && empty($elements[0]['selected']), $message ? $message : t('Option @option for field @id is not selected.', array('@option' => $option, '@id' => $id)), t('Browser'));
danielebarchiesi@0 3442 }
danielebarchiesi@0 3443
danielebarchiesi@0 3444 /**
danielebarchiesi@0 3445 * Asserts that a field exists with the given name or id.
danielebarchiesi@0 3446 *
danielebarchiesi@0 3447 * @param $field
danielebarchiesi@0 3448 * Name or id of field to assert.
danielebarchiesi@0 3449 * @param $message
danielebarchiesi@0 3450 * Message to display.
danielebarchiesi@0 3451 * @param $group
danielebarchiesi@0 3452 * The group this message belongs to.
danielebarchiesi@0 3453 * @return
danielebarchiesi@0 3454 * TRUE on pass, FALSE on fail.
danielebarchiesi@0 3455 */
danielebarchiesi@0 3456 protected function assertField($field, $message = '', $group = 'Other') {
danielebarchiesi@0 3457 return $this->assertFieldByXPath($this->constructFieldXpath('name', $field) . '|' . $this->constructFieldXpath('id', $field), NULL, $message, $group);
danielebarchiesi@0 3458 }
danielebarchiesi@0 3459
danielebarchiesi@0 3460 /**
danielebarchiesi@0 3461 * Asserts that a field does not exist with the given name or id.
danielebarchiesi@0 3462 *
danielebarchiesi@0 3463 * @param $field
danielebarchiesi@0 3464 * Name or id of field to assert.
danielebarchiesi@0 3465 * @param $message
danielebarchiesi@0 3466 * Message to display.
danielebarchiesi@0 3467 * @param $group
danielebarchiesi@0 3468 * The group this message belongs to.
danielebarchiesi@0 3469 * @return
danielebarchiesi@0 3470 * TRUE on pass, FALSE on fail.
danielebarchiesi@0 3471 */
danielebarchiesi@0 3472 protected function assertNoField($field, $message = '', $group = 'Other') {
danielebarchiesi@0 3473 return $this->assertNoFieldByXPath($this->constructFieldXpath('name', $field) . '|' . $this->constructFieldXpath('id', $field), NULL, $message, $group);
danielebarchiesi@0 3474 }
danielebarchiesi@0 3475
danielebarchiesi@0 3476 /**
danielebarchiesi@0 3477 * Asserts that each HTML ID is used for just a single element.
danielebarchiesi@0 3478 *
danielebarchiesi@0 3479 * @param $message
danielebarchiesi@0 3480 * Message to display.
danielebarchiesi@0 3481 * @param $group
danielebarchiesi@0 3482 * The group this message belongs to.
danielebarchiesi@0 3483 * @param $ids_to_skip
danielebarchiesi@0 3484 * An optional array of ids to skip when checking for duplicates. It is
danielebarchiesi@0 3485 * always a bug to have duplicate HTML IDs, so this parameter is to enable
danielebarchiesi@0 3486 * incremental fixing of core code. Whenever a test passes this parameter,
danielebarchiesi@0 3487 * it should add a "todo" comment above the call to this function explaining
danielebarchiesi@0 3488 * the legacy bug that the test wishes to ignore and including a link to an
danielebarchiesi@0 3489 * issue that is working to fix that legacy bug.
danielebarchiesi@0 3490 * @return
danielebarchiesi@0 3491 * TRUE on pass, FALSE on fail.
danielebarchiesi@0 3492 */
danielebarchiesi@0 3493 protected function assertNoDuplicateIds($message = '', $group = 'Other', $ids_to_skip = array()) {
danielebarchiesi@0 3494 $status = TRUE;
danielebarchiesi@0 3495 foreach ($this->xpath('//*[@id]') as $element) {
danielebarchiesi@0 3496 $id = (string) $element['id'];
danielebarchiesi@0 3497 if (isset($seen_ids[$id]) && !in_array($id, $ids_to_skip)) {
danielebarchiesi@0 3498 $this->fail(t('The HTML ID %id is unique.', array('%id' => $id)), $group);
danielebarchiesi@0 3499 $status = FALSE;
danielebarchiesi@0 3500 }
danielebarchiesi@0 3501 $seen_ids[$id] = TRUE;
danielebarchiesi@0 3502 }
danielebarchiesi@0 3503 return $this->assert($status, $message, $group);
danielebarchiesi@0 3504 }
danielebarchiesi@0 3505
danielebarchiesi@0 3506 /**
danielebarchiesi@0 3507 * Helper function: construct an XPath for the given set of attributes and value.
danielebarchiesi@0 3508 *
danielebarchiesi@0 3509 * @param $attribute
danielebarchiesi@0 3510 * Field attributes.
danielebarchiesi@0 3511 * @param $value
danielebarchiesi@0 3512 * Value of field.
danielebarchiesi@0 3513 * @return
danielebarchiesi@0 3514 * XPath for specified values.
danielebarchiesi@0 3515 */
danielebarchiesi@0 3516 protected function constructFieldXpath($attribute, $value) {
danielebarchiesi@0 3517 $xpath = '//textarea[@' . $attribute . '=:value]|//input[@' . $attribute . '=:value]|//select[@' . $attribute . '=:value]';
danielebarchiesi@0 3518 return $this->buildXPathQuery($xpath, array(':value' => $value));
danielebarchiesi@0 3519 }
danielebarchiesi@0 3520
danielebarchiesi@0 3521 /**
danielebarchiesi@0 3522 * Asserts the page responds with the specified response code.
danielebarchiesi@0 3523 *
danielebarchiesi@0 3524 * @param $code
danielebarchiesi@0 3525 * Response code. For example 200 is a successful page request. For a list
danielebarchiesi@0 3526 * of all codes see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html.
danielebarchiesi@0 3527 * @param $message
danielebarchiesi@0 3528 * Message to display.
danielebarchiesi@0 3529 * @return
danielebarchiesi@0 3530 * Assertion result.
danielebarchiesi@0 3531 */
danielebarchiesi@0 3532 protected function assertResponse($code, $message = '') {
danielebarchiesi@0 3533 $curl_code = curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE);
danielebarchiesi@0 3534 $match = is_array($code) ? in_array($curl_code, $code) : $curl_code == $code;
danielebarchiesi@0 3535 return $this->assertTrue($match, $message ? $message : t('HTTP response expected !code, actual !curl_code', array('!code' => $code, '!curl_code' => $curl_code)), t('Browser'));
danielebarchiesi@0 3536 }
danielebarchiesi@0 3537
danielebarchiesi@0 3538 /**
danielebarchiesi@0 3539 * Asserts the page did not return the specified response code.
danielebarchiesi@0 3540 *
danielebarchiesi@0 3541 * @param $code
danielebarchiesi@0 3542 * Response code. For example 200 is a successful page request. For a list
danielebarchiesi@0 3543 * of all codes see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html.
danielebarchiesi@0 3544 * @param $message
danielebarchiesi@0 3545 * Message to display.
danielebarchiesi@0 3546 *
danielebarchiesi@0 3547 * @return
danielebarchiesi@0 3548 * Assertion result.
danielebarchiesi@0 3549 */
danielebarchiesi@0 3550 protected function assertNoResponse($code, $message = '') {
danielebarchiesi@0 3551 $curl_code = curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE);
danielebarchiesi@0 3552 $match = is_array($code) ? in_array($curl_code, $code) : $curl_code == $code;
danielebarchiesi@0 3553 return $this->assertFalse($match, $message ? $message : t('HTTP response not expected !code, actual !curl_code', array('!code' => $code, '!curl_code' => $curl_code)), t('Browser'));
danielebarchiesi@0 3554 }
danielebarchiesi@0 3555
danielebarchiesi@0 3556 /**
danielebarchiesi@0 3557 * Asserts that the most recently sent e-mail message has the given value.
danielebarchiesi@0 3558 *
danielebarchiesi@0 3559 * The field in $name must have the content described in $value.
danielebarchiesi@0 3560 *
danielebarchiesi@0 3561 * @param $name
danielebarchiesi@0 3562 * Name of field or message property to assert. Examples: subject, body, id, ...
danielebarchiesi@0 3563 * @param $value
danielebarchiesi@0 3564 * Value of the field to assert.
danielebarchiesi@0 3565 * @param $message
danielebarchiesi@0 3566 * Message to display.
danielebarchiesi@0 3567 *
danielebarchiesi@0 3568 * @return
danielebarchiesi@0 3569 * TRUE on pass, FALSE on fail.
danielebarchiesi@0 3570 */
danielebarchiesi@0 3571 protected function assertMail($name, $value = '', $message = '') {
danielebarchiesi@0 3572 $captured_emails = variable_get('drupal_test_email_collector', array());
danielebarchiesi@0 3573 $email = end($captured_emails);
danielebarchiesi@0 3574 return $this->assertTrue($email && isset($email[$name]) && $email[$name] == $value, $message, t('E-mail'));
danielebarchiesi@0 3575 }
danielebarchiesi@0 3576
danielebarchiesi@0 3577 /**
danielebarchiesi@0 3578 * Asserts that the most recently sent e-mail message has the string in it.
danielebarchiesi@0 3579 *
danielebarchiesi@0 3580 * @param $field_name
danielebarchiesi@0 3581 * Name of field or message property to assert: subject, body, id, ...
danielebarchiesi@0 3582 * @param $string
danielebarchiesi@0 3583 * String to search for.
danielebarchiesi@0 3584 * @param $email_depth
danielebarchiesi@0 3585 * Number of emails to search for string, starting with most recent.
danielebarchiesi@0 3586 *
danielebarchiesi@0 3587 * @return
danielebarchiesi@0 3588 * TRUE on pass, FALSE on fail.
danielebarchiesi@0 3589 */
danielebarchiesi@0 3590 protected function assertMailString($field_name, $string, $email_depth) {
danielebarchiesi@0 3591 $mails = $this->drupalGetMails();
danielebarchiesi@0 3592 $string_found = FALSE;
danielebarchiesi@0 3593 for ($i = sizeof($mails) -1; $i >= sizeof($mails) - $email_depth && $i >= 0; $i--) {
danielebarchiesi@0 3594 $mail = $mails[$i];
danielebarchiesi@0 3595 // Normalize whitespace, as we don't know what the mail system might have
danielebarchiesi@0 3596 // done. Any run of whitespace becomes a single space.
danielebarchiesi@0 3597 $normalized_mail = preg_replace('/\s+/', ' ', $mail[$field_name]);
danielebarchiesi@0 3598 $normalized_string = preg_replace('/\s+/', ' ', $string);
danielebarchiesi@0 3599 $string_found = (FALSE !== strpos($normalized_mail, $normalized_string));
danielebarchiesi@0 3600 if ($string_found) {
danielebarchiesi@0 3601 break;
danielebarchiesi@0 3602 }
danielebarchiesi@0 3603 }
danielebarchiesi@0 3604 return $this->assertTrue($string_found, t('Expected text found in @field of email message: "@expected".', array('@field' => $field_name, '@expected' => $string)));
danielebarchiesi@0 3605 }
danielebarchiesi@0 3606
danielebarchiesi@0 3607 /**
danielebarchiesi@0 3608 * Asserts that the most recently sent e-mail message has the pattern in it.
danielebarchiesi@0 3609 *
danielebarchiesi@0 3610 * @param $field_name
danielebarchiesi@0 3611 * Name of field or message property to assert: subject, body, id, ...
danielebarchiesi@0 3612 * @param $regex
danielebarchiesi@0 3613 * Pattern to search for.
danielebarchiesi@0 3614 *
danielebarchiesi@0 3615 * @return
danielebarchiesi@0 3616 * TRUE on pass, FALSE on fail.
danielebarchiesi@0 3617 */
danielebarchiesi@0 3618 protected function assertMailPattern($field_name, $regex, $message) {
danielebarchiesi@0 3619 $mails = $this->drupalGetMails();
danielebarchiesi@0 3620 $mail = end($mails);
danielebarchiesi@0 3621 $regex_found = preg_match("/$regex/", $mail[$field_name]);
danielebarchiesi@0 3622 return $this->assertTrue($regex_found, t('Expected text found in @field of email message: "@expected".', array('@field' => $field_name, '@expected' => $regex)));
danielebarchiesi@0 3623 }
danielebarchiesi@0 3624
danielebarchiesi@0 3625 /**
danielebarchiesi@0 3626 * Outputs to verbose the most recent $count emails sent.
danielebarchiesi@0 3627 *
danielebarchiesi@0 3628 * @param $count
danielebarchiesi@0 3629 * Optional number of emails to output.
danielebarchiesi@0 3630 */
danielebarchiesi@0 3631 protected function verboseEmail($count = 1) {
danielebarchiesi@0 3632 $mails = $this->drupalGetMails();
danielebarchiesi@0 3633 for ($i = sizeof($mails) -1; $i >= sizeof($mails) - $count && $i >= 0; $i--) {
danielebarchiesi@0 3634 $mail = $mails[$i];
danielebarchiesi@0 3635 $this->verbose(t('Email:') . '<pre>' . print_r($mail, TRUE) . '</pre>');
danielebarchiesi@0 3636 }
danielebarchiesi@0 3637 }
danielebarchiesi@0 3638 }
danielebarchiesi@0 3639
danielebarchiesi@0 3640 /**
danielebarchiesi@0 3641 * Logs verbose message in a text file.
danielebarchiesi@0 3642 *
danielebarchiesi@0 3643 * If verbose mode is enabled then page requests will be dumped to a file and
danielebarchiesi@0 3644 * presented on the test result screen. The messages will be placed in a file
danielebarchiesi@0 3645 * located in the simpletest directory in the original file system.
danielebarchiesi@0 3646 *
danielebarchiesi@0 3647 * @param $message
danielebarchiesi@0 3648 * The verbose message to be stored.
danielebarchiesi@0 3649 * @param $original_file_directory
danielebarchiesi@0 3650 * The original file directory, before it was changed for testing purposes.
danielebarchiesi@0 3651 * @param $test_class
danielebarchiesi@0 3652 * The active test case class.
danielebarchiesi@0 3653 *
danielebarchiesi@0 3654 * @return
danielebarchiesi@0 3655 * The ID of the message to be placed in related assertion messages.
danielebarchiesi@0 3656 *
danielebarchiesi@0 3657 * @see DrupalTestCase->originalFileDirectory
danielebarchiesi@0 3658 * @see DrupalWebTestCase->verbose()
danielebarchiesi@0 3659 */
danielebarchiesi@0 3660 function simpletest_verbose($message, $original_file_directory = NULL, $test_class = NULL) {
danielebarchiesi@0 3661 static $file_directory = NULL, $class = NULL, $id = 1, $verbose = NULL;
danielebarchiesi@0 3662
danielebarchiesi@0 3663 // Will pass first time during setup phase, and when verbose is TRUE.
danielebarchiesi@0 3664 if (!isset($original_file_directory) && !$verbose) {
danielebarchiesi@0 3665 return FALSE;
danielebarchiesi@0 3666 }
danielebarchiesi@0 3667
danielebarchiesi@0 3668 if ($message && $file_directory) {
danielebarchiesi@0 3669 $message = '<hr />ID #' . $id . ' (<a href="' . $class . '-' . ($id - 1) . '.html">Previous</a> | <a href="' . $class . '-' . ($id + 1) . '.html">Next</a>)<hr />' . $message;
danielebarchiesi@0 3670 file_put_contents($file_directory . "/simpletest/verbose/$class-$id.html", $message, FILE_APPEND);
danielebarchiesi@0 3671 return $id++;
danielebarchiesi@0 3672 }
danielebarchiesi@0 3673
danielebarchiesi@0 3674 if ($original_file_directory) {
danielebarchiesi@0 3675 $file_directory = $original_file_directory;
danielebarchiesi@0 3676 $class = $test_class;
danielebarchiesi@0 3677 $verbose = variable_get('simpletest_verbose', TRUE);
danielebarchiesi@0 3678 $directory = $file_directory . '/simpletest/verbose';
danielebarchiesi@0 3679 $writable = file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
danielebarchiesi@0 3680 if ($writable && !file_exists($directory . '/.htaccess')) {
danielebarchiesi@0 3681 file_put_contents($directory . '/.htaccess', "<IfModule mod_expires.c>\nExpiresActive Off\n</IfModule>\n");
danielebarchiesi@0 3682 }
danielebarchiesi@0 3683 return $writable;
danielebarchiesi@0 3684 }
danielebarchiesi@0 3685 return FALSE;
danielebarchiesi@0 3686 }