danielebarchiesi@0: 0, danielebarchiesi@0: '#fail' => 0, danielebarchiesi@0: '#exception' => 0, danielebarchiesi@0: '#debug' => 0, danielebarchiesi@0: ); danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Assertions thrown in that test case. danielebarchiesi@0: * danielebarchiesi@0: * @var Array danielebarchiesi@0: */ danielebarchiesi@0: protected $assertions = array(); danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * This class is skipped when looking for the source of an assertion. danielebarchiesi@0: * danielebarchiesi@0: * When displaying which function an assert comes from, it's not too useful danielebarchiesi@0: * to see "drupalWebTestCase->drupalLogin()', we would like to see the test danielebarchiesi@0: * that called it. So we need to skip the classes defining these helper danielebarchiesi@0: * methods. danielebarchiesi@0: */ danielebarchiesi@0: protected $skipClasses = array(__CLASS__ => TRUE); danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Flag to indicate whether the test has been set up. danielebarchiesi@0: * danielebarchiesi@0: * The setUp() method isolates the test from the parent Drupal site by danielebarchiesi@0: * creating a random prefix for the database and setting up a clean file danielebarchiesi@0: * storage directory. The tearDown() method then cleans up this test danielebarchiesi@0: * environment. We must ensure that setUp() has been run. Otherwise, danielebarchiesi@0: * tearDown() will act on the parent Drupal site rather than the test danielebarchiesi@0: * environment, destroying live data. danielebarchiesi@0: */ danielebarchiesi@0: protected $setup = FALSE; danielebarchiesi@0: danielebarchiesi@0: protected $setupDatabasePrefix = FALSE; danielebarchiesi@0: danielebarchiesi@0: protected $setupEnvironment = FALSE; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Constructor for DrupalTestCase. danielebarchiesi@0: * danielebarchiesi@0: * @param $test_id danielebarchiesi@0: * Tests with the same id are reported together. danielebarchiesi@0: */ danielebarchiesi@0: public function __construct($test_id = NULL) { danielebarchiesi@0: $this->testId = $test_id; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Internal helper: stores the assert. danielebarchiesi@0: * danielebarchiesi@0: * @param $status danielebarchiesi@0: * Can be 'pass', 'fail', 'exception'. danielebarchiesi@0: * TRUE is a synonym for 'pass', FALSE for 'fail'. danielebarchiesi@0: * @param $message danielebarchiesi@0: * The message string. danielebarchiesi@0: * @param $group danielebarchiesi@0: * Which group this assert belongs to. danielebarchiesi@0: * @param $caller danielebarchiesi@0: * By default, the assert comes from a function whose name starts with danielebarchiesi@0: * 'test'. Instead, you can specify where this assert originates from danielebarchiesi@0: * by passing in an associative array as $caller. Key 'file' is danielebarchiesi@0: * the name of the source file, 'line' is the line number and 'function' danielebarchiesi@0: * is the caller function itself. danielebarchiesi@0: */ danielebarchiesi@0: protected function assert($status, $message = '', $group = 'Other', array $caller = NULL) { danielebarchiesi@0: // Convert boolean status to string status. danielebarchiesi@0: if (is_bool($status)) { danielebarchiesi@0: $status = $status ? 'pass' : 'fail'; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Increment summary result counter. danielebarchiesi@0: $this->results['#' . $status]++; danielebarchiesi@0: danielebarchiesi@0: // Get the function information about the call to the assertion method. danielebarchiesi@0: if (!$caller) { danielebarchiesi@0: $caller = $this->getAssertionCall(); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Creation assertion array that can be displayed while tests are running. danielebarchiesi@0: $this->assertions[] = $assertion = array( danielebarchiesi@0: 'test_id' => $this->testId, danielebarchiesi@0: 'test_class' => get_class($this), danielebarchiesi@0: 'status' => $status, danielebarchiesi@0: 'message' => $message, danielebarchiesi@0: 'message_group' => $group, danielebarchiesi@0: 'function' => $caller['function'], danielebarchiesi@0: 'line' => $caller['line'], danielebarchiesi@0: 'file' => $caller['file'], danielebarchiesi@0: ); danielebarchiesi@0: danielebarchiesi@0: // Store assertion for display after the test has completed. danielebarchiesi@0: try { danielebarchiesi@0: $connection = Database::getConnection('default', 'simpletest_original_default'); danielebarchiesi@0: } danielebarchiesi@0: catch (DatabaseConnectionNotDefinedException $e) { danielebarchiesi@0: // If the test was not set up, the simpletest_original_default danielebarchiesi@0: // connection does not exist. danielebarchiesi@0: $connection = Database::getConnection('default', 'default'); danielebarchiesi@0: } danielebarchiesi@0: $connection danielebarchiesi@0: ->insert('simpletest') danielebarchiesi@0: ->fields($assertion) danielebarchiesi@0: ->execute(); danielebarchiesi@0: danielebarchiesi@0: // We do not use a ternary operator here to allow a breakpoint on danielebarchiesi@0: // test failure. danielebarchiesi@0: if ($status == 'pass') { danielebarchiesi@0: return TRUE; danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: return FALSE; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Store an assertion from outside the testing context. danielebarchiesi@0: * danielebarchiesi@0: * This is useful for inserting assertions that can only be recorded after danielebarchiesi@0: * the test case has been destroyed, such as PHP fatal errors. The caller danielebarchiesi@0: * information is not automatically gathered since the caller is most likely danielebarchiesi@0: * inserting the assertion on behalf of other code. In all other respects danielebarchiesi@0: * the method behaves just like DrupalTestCase::assert() in terms of storing danielebarchiesi@0: * the assertion. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * Message ID of the stored assertion. danielebarchiesi@0: * danielebarchiesi@0: * @see DrupalTestCase::assert() danielebarchiesi@0: * @see DrupalTestCase::deleteAssert() danielebarchiesi@0: */ danielebarchiesi@0: public static function insertAssert($test_id, $test_class, $status, $message = '', $group = 'Other', array $caller = array()) { danielebarchiesi@0: // Convert boolean status to string status. danielebarchiesi@0: if (is_bool($status)) { danielebarchiesi@0: $status = $status ? 'pass' : 'fail'; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: $caller += array( danielebarchiesi@0: 'function' => t('Unknown'), danielebarchiesi@0: 'line' => 0, danielebarchiesi@0: 'file' => t('Unknown'), danielebarchiesi@0: ); danielebarchiesi@0: danielebarchiesi@0: $assertion = array( danielebarchiesi@0: 'test_id' => $test_id, danielebarchiesi@0: 'test_class' => $test_class, danielebarchiesi@0: 'status' => $status, danielebarchiesi@0: 'message' => $message, danielebarchiesi@0: 'message_group' => $group, danielebarchiesi@0: 'function' => $caller['function'], danielebarchiesi@0: 'line' => $caller['line'], danielebarchiesi@0: 'file' => $caller['file'], danielebarchiesi@0: ); danielebarchiesi@0: danielebarchiesi@0: return db_insert('simpletest') danielebarchiesi@0: ->fields($assertion) danielebarchiesi@0: ->execute(); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Delete an assertion record by message ID. danielebarchiesi@0: * danielebarchiesi@0: * @param $message_id danielebarchiesi@0: * Message ID of the assertion to delete. danielebarchiesi@0: * @return danielebarchiesi@0: * TRUE if the assertion was deleted, FALSE otherwise. danielebarchiesi@0: * danielebarchiesi@0: * @see DrupalTestCase::insertAssert() danielebarchiesi@0: */ danielebarchiesi@0: public static function deleteAssert($message_id) { danielebarchiesi@0: return (bool) db_delete('simpletest') danielebarchiesi@0: ->condition('message_id', $message_id) danielebarchiesi@0: ->execute(); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Cycles through backtrace until the first non-assertion method is found. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * Array representing the true caller. danielebarchiesi@0: */ danielebarchiesi@0: protected function getAssertionCall() { danielebarchiesi@0: $backtrace = debug_backtrace(); danielebarchiesi@0: danielebarchiesi@0: // The first element is the call. The second element is the caller. danielebarchiesi@0: // We skip calls that occurred in one of the methods of our base classes danielebarchiesi@0: // or in an assertion function. danielebarchiesi@0: while (($caller = $backtrace[1]) && danielebarchiesi@0: ((isset($caller['class']) && isset($this->skipClasses[$caller['class']])) || danielebarchiesi@0: substr($caller['function'], 0, 6) == 'assert')) { danielebarchiesi@0: // We remove that call. danielebarchiesi@0: array_shift($backtrace); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: return _drupal_get_last_caller($backtrace); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Check to see if a value is not false (not an empty string, 0, NULL, or FALSE). danielebarchiesi@0: * danielebarchiesi@0: * @param $value danielebarchiesi@0: * The value on which the assertion is to be done. danielebarchiesi@0: * @param $message danielebarchiesi@0: * The message to display along with the assertion. danielebarchiesi@0: * @param $group danielebarchiesi@0: * The type of assertion - examples are "Browser", "PHP". danielebarchiesi@0: * @return danielebarchiesi@0: * TRUE if the assertion succeeded, FALSE otherwise. danielebarchiesi@0: */ danielebarchiesi@0: protected function assertTrue($value, $message = '', $group = 'Other') { danielebarchiesi@0: return $this->assert((bool) $value, $message ? $message : t('Value @value is TRUE.', array('@value' => var_export($value, TRUE))), $group); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Check to see if a value is false (an empty string, 0, NULL, or FALSE). danielebarchiesi@0: * danielebarchiesi@0: * @param $value danielebarchiesi@0: * The value on which the assertion is to be done. danielebarchiesi@0: * @param $message danielebarchiesi@0: * The message to display along with the assertion. danielebarchiesi@0: * @param $group danielebarchiesi@0: * The type of assertion - examples are "Browser", "PHP". danielebarchiesi@0: * @return danielebarchiesi@0: * TRUE if the assertion succeeded, FALSE otherwise. danielebarchiesi@0: */ danielebarchiesi@0: protected function assertFalse($value, $message = '', $group = 'Other') { danielebarchiesi@0: return $this->assert(!$value, $message ? $message : t('Value @value is FALSE.', array('@value' => var_export($value, TRUE))), $group); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Check to see if a value is NULL. danielebarchiesi@0: * danielebarchiesi@0: * @param $value danielebarchiesi@0: * The value on which the assertion is to be done. danielebarchiesi@0: * @param $message danielebarchiesi@0: * The message to display along with the assertion. danielebarchiesi@0: * @param $group danielebarchiesi@0: * The type of assertion - examples are "Browser", "PHP". danielebarchiesi@0: * @return danielebarchiesi@0: * TRUE if the assertion succeeded, FALSE otherwise. danielebarchiesi@0: */ danielebarchiesi@0: protected function assertNull($value, $message = '', $group = 'Other') { danielebarchiesi@0: return $this->assert(!isset($value), $message ? $message : t('Value @value is NULL.', array('@value' => var_export($value, TRUE))), $group); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Check to see if a value is not NULL. danielebarchiesi@0: * danielebarchiesi@0: * @param $value danielebarchiesi@0: * The value on which the assertion is to be done. danielebarchiesi@0: * @param $message danielebarchiesi@0: * The message to display along with the assertion. danielebarchiesi@0: * @param $group danielebarchiesi@0: * The type of assertion - examples are "Browser", "PHP". danielebarchiesi@0: * @return danielebarchiesi@0: * TRUE if the assertion succeeded, FALSE otherwise. danielebarchiesi@0: */ danielebarchiesi@0: protected function assertNotNull($value, $message = '', $group = 'Other') { danielebarchiesi@0: return $this->assert(isset($value), $message ? $message : t('Value @value is not NULL.', array('@value' => var_export($value, TRUE))), $group); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Check to see if two values are equal. danielebarchiesi@0: * danielebarchiesi@0: * @param $first danielebarchiesi@0: * The first value to check. danielebarchiesi@0: * @param $second danielebarchiesi@0: * The second value to check. danielebarchiesi@0: * @param $message danielebarchiesi@0: * The message to display along with the assertion. danielebarchiesi@0: * @param $group danielebarchiesi@0: * The type of assertion - examples are "Browser", "PHP". danielebarchiesi@0: * @return danielebarchiesi@0: * TRUE if the assertion succeeded, FALSE otherwise. danielebarchiesi@0: */ danielebarchiesi@0: protected function assertEqual($first, $second, $message = '', $group = 'Other') { danielebarchiesi@0: 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: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Check to see if two values are not equal. danielebarchiesi@0: * danielebarchiesi@0: * @param $first danielebarchiesi@0: * The first value to check. danielebarchiesi@0: * @param $second danielebarchiesi@0: * The second value to check. danielebarchiesi@0: * @param $message danielebarchiesi@0: * The message to display along with the assertion. danielebarchiesi@0: * @param $group danielebarchiesi@0: * The type of assertion - examples are "Browser", "PHP". danielebarchiesi@0: * @return danielebarchiesi@0: * TRUE if the assertion succeeded, FALSE otherwise. danielebarchiesi@0: */ danielebarchiesi@0: protected function assertNotEqual($first, $second, $message = '', $group = 'Other') { danielebarchiesi@0: 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: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Check to see if two values are identical. danielebarchiesi@0: * danielebarchiesi@0: * @param $first danielebarchiesi@0: * The first value to check. danielebarchiesi@0: * @param $second danielebarchiesi@0: * The second value to check. danielebarchiesi@0: * @param $message danielebarchiesi@0: * The message to display along with the assertion. danielebarchiesi@0: * @param $group danielebarchiesi@0: * The type of assertion - examples are "Browser", "PHP". danielebarchiesi@0: * @return danielebarchiesi@0: * TRUE if the assertion succeeded, FALSE otherwise. danielebarchiesi@0: */ danielebarchiesi@0: protected function assertIdentical($first, $second, $message = '', $group = 'Other') { danielebarchiesi@0: 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: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Check to see if two values are not identical. danielebarchiesi@0: * danielebarchiesi@0: * @param $first danielebarchiesi@0: * The first value to check. danielebarchiesi@0: * @param $second danielebarchiesi@0: * The second value to check. danielebarchiesi@0: * @param $message danielebarchiesi@0: * The message to display along with the assertion. danielebarchiesi@0: * @param $group danielebarchiesi@0: * The type of assertion - examples are "Browser", "PHP". danielebarchiesi@0: * @return danielebarchiesi@0: * TRUE if the assertion succeeded, FALSE otherwise. danielebarchiesi@0: */ danielebarchiesi@0: protected function assertNotIdentical($first, $second, $message = '', $group = 'Other') { danielebarchiesi@0: 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: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Fire an assertion that is always positive. danielebarchiesi@0: * danielebarchiesi@0: * @param $message danielebarchiesi@0: * The message to display along with the assertion. danielebarchiesi@0: * @param $group danielebarchiesi@0: * The type of assertion - examples are "Browser", "PHP". danielebarchiesi@0: * @return danielebarchiesi@0: * TRUE. danielebarchiesi@0: */ danielebarchiesi@0: protected function pass($message = NULL, $group = 'Other') { danielebarchiesi@0: return $this->assert(TRUE, $message, $group); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Fire an assertion that is always negative. danielebarchiesi@0: * danielebarchiesi@0: * @param $message danielebarchiesi@0: * The message to display along with the assertion. danielebarchiesi@0: * @param $group danielebarchiesi@0: * The type of assertion - examples are "Browser", "PHP". danielebarchiesi@0: * @return danielebarchiesi@0: * FALSE. danielebarchiesi@0: */ danielebarchiesi@0: protected function fail($message = NULL, $group = 'Other') { danielebarchiesi@0: return $this->assert(FALSE, $message, $group); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Fire an error assertion. danielebarchiesi@0: * danielebarchiesi@0: * @param $message danielebarchiesi@0: * The message to display along with the assertion. danielebarchiesi@0: * @param $group danielebarchiesi@0: * The type of assertion - examples are "Browser", "PHP". danielebarchiesi@0: * @param $caller danielebarchiesi@0: * The caller of the error. danielebarchiesi@0: * @return danielebarchiesi@0: * FALSE. danielebarchiesi@0: */ danielebarchiesi@0: protected function error($message = '', $group = 'Other', array $caller = NULL) { danielebarchiesi@0: if ($group == 'User notice') { danielebarchiesi@0: // Since 'User notice' is set by trigger_error() which is used for debug danielebarchiesi@0: // set the message to a status of 'debug'. danielebarchiesi@0: return $this->assert('debug', $message, 'Debug', $caller); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: return $this->assert('exception', $message, $group, $caller); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Logs verbose message in a text file. danielebarchiesi@0: * danielebarchiesi@0: * The a link to the vebose message will be placed in the test results via danielebarchiesi@0: * as a passing assertion with the text '[verbose message]'. danielebarchiesi@0: * danielebarchiesi@0: * @param $message danielebarchiesi@0: * The verbose message to be stored. danielebarchiesi@0: * danielebarchiesi@0: * @see simpletest_verbose() danielebarchiesi@0: */ danielebarchiesi@0: protected function verbose($message) { danielebarchiesi@0: if ($id = simpletest_verbose($message)) { danielebarchiesi@0: $class_safe = str_replace('\\', '_', get_class($this)); danielebarchiesi@0: $url = file_create_url($this->originalFileDirectory . '/simpletest/verbose/' . $class_safe . '-' . $id . '.html'); danielebarchiesi@0: $this->error(l(t('Verbose message'), $url, array('attributes' => array('target' => '_blank'))), 'User notice'); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Run all tests in this class. danielebarchiesi@0: * danielebarchiesi@0: * Regardless of whether $methods are passed or not, only method names danielebarchiesi@0: * starting with "test" are executed. danielebarchiesi@0: * danielebarchiesi@0: * @param $methods danielebarchiesi@0: * (optional) A list of method names in the test case class to run; e.g., danielebarchiesi@0: * array('testFoo', 'testBar'). By default, all methods of the class are danielebarchiesi@0: * taken into account, but it can be useful to only run a few selected test danielebarchiesi@0: * methods during debugging. danielebarchiesi@0: */ danielebarchiesi@0: public function run(array $methods = array()) { danielebarchiesi@0: // Initialize verbose debugging. danielebarchiesi@0: $class = get_class($this); danielebarchiesi@0: simpletest_verbose(NULL, variable_get('file_public_path', conf_path() . '/files'), str_replace('\\', '_', $class)); danielebarchiesi@0: danielebarchiesi@0: // HTTP auth settings (:) for the simpletest browser danielebarchiesi@0: // when sending requests to the test site. danielebarchiesi@0: $this->httpauth_method = variable_get('simpletest_httpauth_method', CURLAUTH_BASIC); danielebarchiesi@0: $username = variable_get('simpletest_httpauth_username', NULL); danielebarchiesi@0: $password = variable_get('simpletest_httpauth_password', NULL); danielebarchiesi@0: if ($username && $password) { danielebarchiesi@0: $this->httpauth_credentials = $username . ':' . $password; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: set_error_handler(array($this, 'errorHandler')); danielebarchiesi@0: // Iterate through all the methods in this class, unless a specific list of danielebarchiesi@0: // methods to run was passed. danielebarchiesi@0: $class_methods = get_class_methods($class); danielebarchiesi@0: if ($methods) { danielebarchiesi@0: $class_methods = array_intersect($class_methods, $methods); danielebarchiesi@0: } danielebarchiesi@0: foreach ($class_methods as $method) { danielebarchiesi@0: // If the current method starts with "test", run it - it's a test. danielebarchiesi@0: if (strtolower(substr($method, 0, 4)) == 'test') { danielebarchiesi@0: // Insert a fail record. This will be deleted on completion to ensure danielebarchiesi@0: // that testing completed. danielebarchiesi@0: $method_info = new ReflectionMethod($class, $method); danielebarchiesi@0: $caller = array( danielebarchiesi@0: 'file' => $method_info->getFileName(), danielebarchiesi@0: 'line' => $method_info->getStartLine(), danielebarchiesi@0: 'function' => $class . '->' . $method . '()', danielebarchiesi@0: ); danielebarchiesi@0: $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: $this->setUp(); danielebarchiesi@0: if ($this->setup) { danielebarchiesi@0: try { danielebarchiesi@0: $this->$method(); danielebarchiesi@0: // Finish up. danielebarchiesi@0: } danielebarchiesi@0: catch (Exception $e) { danielebarchiesi@0: $this->exceptionHandler($e); danielebarchiesi@0: } danielebarchiesi@0: $this->tearDown(); danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: $this->fail(t("The test cannot be executed because it has not been set up properly.")); danielebarchiesi@0: } danielebarchiesi@0: // Remove the completion check record. danielebarchiesi@0: DrupalTestCase::deleteAssert($completion_check_id); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: // Clear out the error messages and restore error handler. danielebarchiesi@0: drupal_get_messages(); danielebarchiesi@0: restore_error_handler(); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Handle errors during test runs. danielebarchiesi@0: * danielebarchiesi@0: * Because this is registered in set_error_handler(), it has to be public. danielebarchiesi@0: * @see set_error_handler danielebarchiesi@0: */ danielebarchiesi@0: public function errorHandler($severity, $message, $file = NULL, $line = NULL) { danielebarchiesi@0: if ($severity & error_reporting()) { danielebarchiesi@0: $error_map = array( danielebarchiesi@0: E_STRICT => 'Run-time notice', danielebarchiesi@0: E_WARNING => 'Warning', danielebarchiesi@0: E_NOTICE => 'Notice', danielebarchiesi@0: E_CORE_ERROR => 'Core error', danielebarchiesi@0: E_CORE_WARNING => 'Core warning', danielebarchiesi@0: E_USER_ERROR => 'User error', danielebarchiesi@0: E_USER_WARNING => 'User warning', danielebarchiesi@0: E_USER_NOTICE => 'User notice', danielebarchiesi@0: E_RECOVERABLE_ERROR => 'Recoverable error', danielebarchiesi@0: ); danielebarchiesi@0: danielebarchiesi@0: $backtrace = debug_backtrace(); danielebarchiesi@0: $this->error($message, $error_map[$severity], _drupal_get_last_caller($backtrace)); danielebarchiesi@0: } danielebarchiesi@0: return TRUE; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Handle exceptions. danielebarchiesi@0: * danielebarchiesi@0: * @see set_exception_handler danielebarchiesi@0: */ danielebarchiesi@0: protected function exceptionHandler($exception) { danielebarchiesi@0: $backtrace = $exception->getTrace(); danielebarchiesi@0: // Push on top of the backtrace the call that generated the exception. danielebarchiesi@0: array_unshift($backtrace, array( danielebarchiesi@0: 'line' => $exception->getLine(), danielebarchiesi@0: 'file' => $exception->getFile(), danielebarchiesi@0: )); danielebarchiesi@0: require_once DRUPAL_ROOT . '/includes/errors.inc'; danielebarchiesi@0: // The exception message is run through check_plain() by _drupal_decode_exception(). danielebarchiesi@0: $this->error(t('%type: !message in %function (line %line of %file).', _drupal_decode_exception($exception)), 'Uncaught exception', _drupal_get_last_caller($backtrace)); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Generates a random string of ASCII characters of codes 32 to 126. danielebarchiesi@0: * danielebarchiesi@0: * The generated string includes alpha-numeric characters and common danielebarchiesi@0: * miscellaneous characters. Use this method when testing general input danielebarchiesi@0: * where the content is not restricted. danielebarchiesi@0: * danielebarchiesi@0: * Do not use this method when special characters are not possible (e.g., in danielebarchiesi@0: * machine or file names that have already been validated); instead, danielebarchiesi@0: * use DrupalWebTestCase::randomName(). danielebarchiesi@0: * danielebarchiesi@0: * @param $length danielebarchiesi@0: * Length of random string to generate. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * Randomly generated string. danielebarchiesi@0: * danielebarchiesi@0: * @see DrupalWebTestCase::randomName() danielebarchiesi@0: */ danielebarchiesi@0: public static function randomString($length = 8) { danielebarchiesi@0: $str = ''; danielebarchiesi@0: for ($i = 0; $i < $length; $i++) { danielebarchiesi@0: $str .= chr(mt_rand(32, 126)); danielebarchiesi@0: } danielebarchiesi@0: return $str; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Generates a random string containing letters and numbers. danielebarchiesi@0: * danielebarchiesi@0: * The string will always start with a letter. The letters may be upper or danielebarchiesi@0: * lower case. This method is better for restricted inputs that do not danielebarchiesi@0: * accept certain characters. For example, when testing input fields that danielebarchiesi@0: * require machine readable values (i.e. without spaces and non-standard danielebarchiesi@0: * characters) this method is best. danielebarchiesi@0: * danielebarchiesi@0: * Do not use this method when testing unvalidated user input. Instead, use danielebarchiesi@0: * DrupalWebTestCase::randomString(). danielebarchiesi@0: * danielebarchiesi@0: * @param $length danielebarchiesi@0: * Length of random string to generate. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * Randomly generated string. danielebarchiesi@0: * danielebarchiesi@0: * @see DrupalWebTestCase::randomString() danielebarchiesi@0: */ danielebarchiesi@0: public static function randomName($length = 8) { danielebarchiesi@0: $values = array_merge(range(65, 90), range(97, 122), range(48, 57)); danielebarchiesi@0: $max = count($values) - 1; danielebarchiesi@0: $str = chr(mt_rand(97, 122)); danielebarchiesi@0: for ($i = 1; $i < $length; $i++) { danielebarchiesi@0: $str .= chr($values[mt_rand(0, $max)]); danielebarchiesi@0: } danielebarchiesi@0: return $str; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Converts a list of possible parameters into a stack of permutations. danielebarchiesi@0: * danielebarchiesi@0: * Takes a list of parameters containing possible values, and converts all of danielebarchiesi@0: * them into a list of items containing every possible permutation. danielebarchiesi@0: * danielebarchiesi@0: * Example: danielebarchiesi@0: * @code danielebarchiesi@0: * $parameters = array( danielebarchiesi@0: * 'one' => array(0, 1), danielebarchiesi@0: * 'two' => array(2, 3), danielebarchiesi@0: * ); danielebarchiesi@0: * $permutations = DrupalTestCase::generatePermutations($parameters) danielebarchiesi@0: * // Result: danielebarchiesi@0: * $permutations == array( danielebarchiesi@0: * array('one' => 0, 'two' => 2), danielebarchiesi@0: * array('one' => 1, 'two' => 2), danielebarchiesi@0: * array('one' => 0, 'two' => 3), danielebarchiesi@0: * array('one' => 1, 'two' => 3), danielebarchiesi@0: * ) danielebarchiesi@0: * @endcode danielebarchiesi@0: * danielebarchiesi@0: * @param $parameters danielebarchiesi@0: * An associative array of parameters, keyed by parameter name, and whose danielebarchiesi@0: * values are arrays of parameter values. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * A list of permutations, which is an array of arrays. Each inner array danielebarchiesi@0: * contains the full list of parameters that have been passed, but with a danielebarchiesi@0: * single value only. danielebarchiesi@0: */ danielebarchiesi@0: public static function generatePermutations($parameters) { danielebarchiesi@0: $all_permutations = array(array()); danielebarchiesi@0: foreach ($parameters as $parameter => $values) { danielebarchiesi@0: $new_permutations = array(); danielebarchiesi@0: // Iterate over all values of the parameter. danielebarchiesi@0: foreach ($values as $value) { danielebarchiesi@0: // Iterate over all existing permutations. danielebarchiesi@0: foreach ($all_permutations as $permutation) { danielebarchiesi@0: // Add the new parameter value to existing permutations. danielebarchiesi@0: $new_permutations[] = $permutation + array($parameter => $value); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: // Replace the old permutations with the new permutations. danielebarchiesi@0: $all_permutations = $new_permutations; danielebarchiesi@0: } danielebarchiesi@0: return $all_permutations; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Test case for Drupal unit tests. danielebarchiesi@0: * danielebarchiesi@0: * These tests can not access the database nor files. Calling any Drupal danielebarchiesi@0: * function that needs the database will throw exceptions. These include danielebarchiesi@0: * watchdog(), module_implements(), module_invoke_all() etc. danielebarchiesi@0: */ danielebarchiesi@0: class DrupalUnitTestCase extends DrupalTestCase { danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Constructor for DrupalUnitTestCase. danielebarchiesi@0: */ danielebarchiesi@0: function __construct($test_id = NULL) { danielebarchiesi@0: parent::__construct($test_id); danielebarchiesi@0: $this->skipClasses[__CLASS__] = TRUE; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Sets up unit test environment. danielebarchiesi@0: * danielebarchiesi@0: * Unlike DrupalWebTestCase::setUp(), DrupalUnitTestCase::setUp() does not danielebarchiesi@0: * install modules because tests are performed without accessing the database. danielebarchiesi@0: * Any required files must be explicitly included by the child class setUp() danielebarchiesi@0: * method. danielebarchiesi@0: */ danielebarchiesi@0: protected function setUp() { danielebarchiesi@0: global $conf; danielebarchiesi@0: danielebarchiesi@0: // Store necessary current values before switching to the test environment. danielebarchiesi@0: $this->originalFileDirectory = variable_get('file_public_path', conf_path() . '/files'); danielebarchiesi@0: danielebarchiesi@0: // Reset all statics so that test is performed with a clean environment. danielebarchiesi@0: drupal_static_reset(); danielebarchiesi@0: danielebarchiesi@0: // Generate temporary prefixed database to ensure that tests have a clean starting point. danielebarchiesi@0: $this->databasePrefix = Database::getConnection()->prefixTables('{simpletest' . mt_rand(1000, 1000000) . '}'); danielebarchiesi@0: danielebarchiesi@0: // Create test directory. danielebarchiesi@0: $public_files_directory = $this->originalFileDirectory . '/simpletest/' . substr($this->databasePrefix, 10); danielebarchiesi@0: file_prepare_directory($public_files_directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS); danielebarchiesi@0: $conf['file_public_path'] = $public_files_directory; danielebarchiesi@0: danielebarchiesi@0: // Clone the current connection and replace the current prefix. danielebarchiesi@0: $connection_info = Database::getConnectionInfo('default'); danielebarchiesi@0: Database::renameConnection('default', 'simpletest_original_default'); danielebarchiesi@0: foreach ($connection_info as $target => $value) { danielebarchiesi@0: $connection_info[$target]['prefix'] = array( danielebarchiesi@0: 'default' => $value['prefix']['default'] . $this->databasePrefix, danielebarchiesi@0: ); danielebarchiesi@0: } danielebarchiesi@0: Database::addConnectionInfo('default', 'default', $connection_info['default']); danielebarchiesi@0: danielebarchiesi@0: // Set user agent to be consistent with web test case. danielebarchiesi@0: $_SERVER['HTTP_USER_AGENT'] = $this->databasePrefix; danielebarchiesi@0: danielebarchiesi@0: // If locale is enabled then t() will try to access the database and danielebarchiesi@0: // subsequently will fail as the database is not accessible. danielebarchiesi@0: $module_list = module_list(); danielebarchiesi@0: if (isset($module_list['locale'])) { danielebarchiesi@0: $this->originalModuleList = $module_list; danielebarchiesi@0: unset($module_list['locale']); danielebarchiesi@0: module_list(TRUE, FALSE, FALSE, $module_list); danielebarchiesi@0: } danielebarchiesi@0: $this->setup = TRUE; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: protected function tearDown() { danielebarchiesi@0: global $conf; danielebarchiesi@0: danielebarchiesi@0: // Get back to the original connection. danielebarchiesi@0: Database::removeConnection('default'); danielebarchiesi@0: Database::renameConnection('simpletest_original_default', 'default'); danielebarchiesi@0: danielebarchiesi@0: $conf['file_public_path'] = $this->originalFileDirectory; danielebarchiesi@0: // Restore modules if necessary. danielebarchiesi@0: if (isset($this->originalModuleList)) { danielebarchiesi@0: module_list(TRUE, FALSE, FALSE, $this->originalModuleList); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Test case for typical Drupal tests. danielebarchiesi@0: */ danielebarchiesi@0: class DrupalWebTestCase extends DrupalTestCase { danielebarchiesi@0: /** danielebarchiesi@0: * The profile to install as a basis for testing. danielebarchiesi@0: * danielebarchiesi@0: * @var string danielebarchiesi@0: */ danielebarchiesi@0: protected $profile = 'standard'; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * The URL currently loaded in the internal browser. danielebarchiesi@0: * danielebarchiesi@0: * @var string danielebarchiesi@0: */ danielebarchiesi@0: protected $url; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * The handle of the current cURL connection. danielebarchiesi@0: * danielebarchiesi@0: * @var resource danielebarchiesi@0: */ danielebarchiesi@0: protected $curlHandle; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * The headers of the page currently loaded in the internal browser. danielebarchiesi@0: * danielebarchiesi@0: * @var Array danielebarchiesi@0: */ danielebarchiesi@0: protected $headers; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * The content of the page currently loaded in the internal browser. danielebarchiesi@0: * danielebarchiesi@0: * @var string danielebarchiesi@0: */ danielebarchiesi@0: protected $content; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * The content of the page currently loaded in the internal browser (plain text version). danielebarchiesi@0: * danielebarchiesi@0: * @var string danielebarchiesi@0: */ danielebarchiesi@0: protected $plainTextContent; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * The value of the Drupal.settings JavaScript variable for the page currently loaded in the internal browser. danielebarchiesi@0: * danielebarchiesi@0: * @var Array danielebarchiesi@0: */ danielebarchiesi@0: protected $drupalSettings; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * The parsed version of the page. danielebarchiesi@0: * danielebarchiesi@0: * @var SimpleXMLElement danielebarchiesi@0: */ danielebarchiesi@0: protected $elements = NULL; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * The current user logged in using the internal browser. danielebarchiesi@0: * danielebarchiesi@0: * @var bool danielebarchiesi@0: */ danielebarchiesi@0: protected $loggedInUser = FALSE; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * The current cookie file used by cURL. danielebarchiesi@0: * danielebarchiesi@0: * We do not reuse the cookies in further runs, so we do not need a file danielebarchiesi@0: * but we still need cookie handling, so we set the jar to NULL. danielebarchiesi@0: */ danielebarchiesi@0: protected $cookieFile = NULL; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Additional cURL options. danielebarchiesi@0: * danielebarchiesi@0: * DrupalWebTestCase itself never sets this but always obeys what is set. danielebarchiesi@0: */ danielebarchiesi@0: protected $additionalCurlOptions = array(); danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * The original user, before it was changed to a clean uid = 1 for testing purposes. danielebarchiesi@0: * danielebarchiesi@0: * @var object danielebarchiesi@0: */ danielebarchiesi@0: protected $originalUser = NULL; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * The original shutdown handlers array, before it was cleaned for testing purposes. danielebarchiesi@0: * danielebarchiesi@0: * @var array danielebarchiesi@0: */ danielebarchiesi@0: protected $originalShutdownCallbacks = array(); danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * HTTP authentication method danielebarchiesi@0: */ danielebarchiesi@0: protected $httpauth_method = CURLAUTH_BASIC; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * HTTP authentication credentials (:). danielebarchiesi@0: */ danielebarchiesi@0: protected $httpauth_credentials = NULL; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * The current session name, if available. danielebarchiesi@0: */ danielebarchiesi@0: protected $session_name = NULL; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * The current session ID, if available. danielebarchiesi@0: */ danielebarchiesi@0: protected $session_id = NULL; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Whether the files were copied to the test files directory. danielebarchiesi@0: */ danielebarchiesi@0: protected $generatedTestFiles = FALSE; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * The number of redirects followed during the handling of a request. danielebarchiesi@0: */ danielebarchiesi@0: protected $redirect_count; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Constructor for DrupalWebTestCase. danielebarchiesi@0: */ danielebarchiesi@0: function __construct($test_id = NULL) { danielebarchiesi@0: parent::__construct($test_id); danielebarchiesi@0: $this->skipClasses[__CLASS__] = TRUE; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Get a node from the database based on its title. danielebarchiesi@0: * danielebarchiesi@0: * @param $title danielebarchiesi@0: * A node title, usually generated by $this->randomName(). danielebarchiesi@0: * @param $reset danielebarchiesi@0: * (optional) Whether to reset the internal node_load() cache. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * A node object matching $title. danielebarchiesi@0: */ danielebarchiesi@0: function drupalGetNodeByTitle($title, $reset = FALSE) { danielebarchiesi@0: $nodes = node_load_multiple(array(), array('title' => $title), $reset); danielebarchiesi@0: // Load the first node returned from the database. danielebarchiesi@0: $returned_node = reset($nodes); danielebarchiesi@0: return $returned_node; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Creates a node based on default settings. danielebarchiesi@0: * danielebarchiesi@0: * @param $settings danielebarchiesi@0: * An associative array of settings to change from the defaults, keys are danielebarchiesi@0: * node properties, for example 'title' => 'Hello, world!'. danielebarchiesi@0: * @return danielebarchiesi@0: * Created node object. danielebarchiesi@0: */ danielebarchiesi@0: protected function drupalCreateNode($settings = array()) { danielebarchiesi@0: // Populate defaults array. danielebarchiesi@0: $settings += array( danielebarchiesi@0: 'body' => array(LANGUAGE_NONE => array(array())), danielebarchiesi@0: 'title' => $this->randomName(8), danielebarchiesi@0: 'comment' => 2, danielebarchiesi@0: 'changed' => REQUEST_TIME, danielebarchiesi@0: 'moderate' => 0, danielebarchiesi@0: 'promote' => 0, danielebarchiesi@0: 'revision' => 1, danielebarchiesi@0: 'log' => '', danielebarchiesi@0: 'status' => 1, danielebarchiesi@0: 'sticky' => 0, danielebarchiesi@0: 'type' => 'page', danielebarchiesi@0: 'revisions' => NULL, danielebarchiesi@0: 'language' => LANGUAGE_NONE, danielebarchiesi@0: ); danielebarchiesi@0: danielebarchiesi@0: // Use the original node's created time for existing nodes. danielebarchiesi@0: if (isset($settings['created']) && !isset($settings['date'])) { danielebarchiesi@0: $settings['date'] = format_date($settings['created'], 'custom', 'Y-m-d H:i:s O'); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // If the node's user uid is not specified manually, use the currently danielebarchiesi@0: // logged in user if available, or else the user running the test. danielebarchiesi@0: if (!isset($settings['uid'])) { danielebarchiesi@0: if ($this->loggedInUser) { danielebarchiesi@0: $settings['uid'] = $this->loggedInUser->uid; danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: global $user; danielebarchiesi@0: $settings['uid'] = $user->uid; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Merge body field value and format separately. danielebarchiesi@0: $body = array( danielebarchiesi@0: 'value' => $this->randomName(32), danielebarchiesi@0: 'format' => filter_default_format(), danielebarchiesi@0: ); danielebarchiesi@0: $settings['body'][$settings['language']][0] += $body; danielebarchiesi@0: danielebarchiesi@0: $node = (object) $settings; danielebarchiesi@0: node_save($node); danielebarchiesi@0: danielebarchiesi@0: // Small hack to link revisions to our test user. danielebarchiesi@0: db_update('node_revision') danielebarchiesi@0: ->fields(array('uid' => $node->uid)) danielebarchiesi@0: ->condition('vid', $node->vid) danielebarchiesi@0: ->execute(); danielebarchiesi@0: return $node; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Creates a custom content type based on default settings. danielebarchiesi@0: * danielebarchiesi@0: * @param $settings danielebarchiesi@0: * An array of settings to change from the defaults. danielebarchiesi@0: * Example: 'type' => 'foo'. danielebarchiesi@0: * @return danielebarchiesi@0: * Created content type. danielebarchiesi@0: */ danielebarchiesi@0: protected function drupalCreateContentType($settings = array()) { danielebarchiesi@0: // Find a non-existent random type name. danielebarchiesi@0: do { danielebarchiesi@0: $name = strtolower($this->randomName(8)); danielebarchiesi@0: } while (node_type_get_type($name)); danielebarchiesi@0: danielebarchiesi@0: // Populate defaults array. danielebarchiesi@0: $defaults = array( danielebarchiesi@0: 'type' => $name, danielebarchiesi@0: 'name' => $name, danielebarchiesi@0: 'base' => 'node_content', danielebarchiesi@0: 'description' => '', danielebarchiesi@0: 'help' => '', danielebarchiesi@0: 'title_label' => 'Title', danielebarchiesi@0: 'body_label' => 'Body', danielebarchiesi@0: 'has_title' => 1, danielebarchiesi@0: 'has_body' => 1, danielebarchiesi@0: ); danielebarchiesi@0: // Imposed values for a custom type. danielebarchiesi@0: $forced = array( danielebarchiesi@0: 'orig_type' => '', danielebarchiesi@0: 'old_type' => '', danielebarchiesi@0: 'module' => 'node', danielebarchiesi@0: 'custom' => 1, danielebarchiesi@0: 'modified' => 1, danielebarchiesi@0: 'locked' => 0, danielebarchiesi@0: ); danielebarchiesi@0: $type = $forced + $settings + $defaults; danielebarchiesi@0: $type = (object) $type; danielebarchiesi@0: danielebarchiesi@0: $saved_type = node_type_save($type); danielebarchiesi@0: node_types_rebuild(); danielebarchiesi@0: menu_rebuild(); danielebarchiesi@0: node_add_body_field($type); danielebarchiesi@0: danielebarchiesi@0: $this->assertEqual($saved_type, SAVED_NEW, t('Created content type %type.', array('%type' => $type->type))); danielebarchiesi@0: danielebarchiesi@0: // Reset permissions so that permissions for this content type are available. danielebarchiesi@0: $this->checkPermissions(array(), TRUE); danielebarchiesi@0: danielebarchiesi@0: return $type; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Get a list files that can be used in tests. danielebarchiesi@0: * danielebarchiesi@0: * @param $type danielebarchiesi@0: * File type, possible values: 'binary', 'html', 'image', 'javascript', 'php', 'sql', 'text'. danielebarchiesi@0: * @param $size danielebarchiesi@0: * File size in bytes to match. Please check the tests/files folder. danielebarchiesi@0: * @return danielebarchiesi@0: * List of files that match filter. danielebarchiesi@0: */ danielebarchiesi@0: protected function drupalGetTestFiles($type, $size = NULL) { danielebarchiesi@0: if (empty($this->generatedTestFiles)) { danielebarchiesi@0: // Generate binary test files. danielebarchiesi@0: $lines = array(64, 1024); danielebarchiesi@0: $count = 0; danielebarchiesi@0: foreach ($lines as $line) { danielebarchiesi@0: simpletest_generate_file('binary-' . $count++, 64, $line, 'binary'); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Generate text test files. danielebarchiesi@0: $lines = array(16, 256, 1024, 2048, 20480); danielebarchiesi@0: $count = 0; danielebarchiesi@0: foreach ($lines as $line) { danielebarchiesi@0: simpletest_generate_file('text-' . $count++, 64, $line); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Copy other test files from simpletest. danielebarchiesi@0: $original = drupal_get_path('module', 'simpletest') . '/files'; danielebarchiesi@0: $files = file_scan_directory($original, '/(html|image|javascript|php|sql)-.*/'); danielebarchiesi@0: foreach ($files as $file) { danielebarchiesi@0: file_unmanaged_copy($file->uri, variable_get('file_public_path', conf_path() . '/files')); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: $this->generatedTestFiles = TRUE; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: $files = array(); danielebarchiesi@0: // Make sure type is valid. danielebarchiesi@0: if (in_array($type, array('binary', 'html', 'image', 'javascript', 'php', 'sql', 'text'))) { danielebarchiesi@0: $files = file_scan_directory('public://', '/' . $type . '\-.*/'); danielebarchiesi@0: danielebarchiesi@0: // If size is set then remove any files that are not of that size. danielebarchiesi@0: if ($size !== NULL) { danielebarchiesi@0: foreach ($files as $file) { danielebarchiesi@0: $stats = stat($file->uri); danielebarchiesi@0: if ($stats['size'] != $size) { danielebarchiesi@0: unset($files[$file->uri]); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: usort($files, array($this, 'drupalCompareFiles')); danielebarchiesi@0: return $files; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Compare two files based on size and file name. danielebarchiesi@0: */ danielebarchiesi@0: protected function drupalCompareFiles($file1, $file2) { danielebarchiesi@0: $compare_size = filesize($file1->uri) - filesize($file2->uri); danielebarchiesi@0: if ($compare_size) { danielebarchiesi@0: // Sort by file size. danielebarchiesi@0: return $compare_size; danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: // The files were the same size, so sort alphabetically. danielebarchiesi@0: return strnatcmp($file1->name, $file2->name); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Create a user with a given set of permissions. danielebarchiesi@0: * danielebarchiesi@0: * @param array $permissions danielebarchiesi@0: * Array of permission names to assign to user. Note that the user always danielebarchiesi@0: * has the default permissions derived from the "authenticated users" role. danielebarchiesi@0: * danielebarchiesi@0: * @return object|false danielebarchiesi@0: * A fully loaded user object with pass_raw property, or FALSE if account danielebarchiesi@0: * creation fails. danielebarchiesi@0: */ danielebarchiesi@0: protected function drupalCreateUser(array $permissions = array()) { danielebarchiesi@0: // Create a role with the given permission set, if any. danielebarchiesi@0: $rid = FALSE; danielebarchiesi@0: if ($permissions) { danielebarchiesi@0: $rid = $this->drupalCreateRole($permissions); danielebarchiesi@0: if (!$rid) { danielebarchiesi@0: return FALSE; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Create a user assigned to that role. danielebarchiesi@0: $edit = array(); danielebarchiesi@0: $edit['name'] = $this->randomName(); danielebarchiesi@0: $edit['mail'] = $edit['name'] . '@example.com'; danielebarchiesi@0: $edit['pass'] = user_password(); danielebarchiesi@0: $edit['status'] = 1; danielebarchiesi@0: if ($rid) { danielebarchiesi@0: $edit['roles'] = array($rid => $rid); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: $account = user_save(drupal_anonymous_user(), $edit); danielebarchiesi@0: danielebarchiesi@0: $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: if (empty($account->uid)) { danielebarchiesi@0: return FALSE; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Add the raw password so that we can log in as this user. danielebarchiesi@0: $account->pass_raw = $edit['pass']; danielebarchiesi@0: return $account; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Internal helper function; Create a role with specified permissions. danielebarchiesi@0: * danielebarchiesi@0: * @param $permissions danielebarchiesi@0: * Array of permission names to assign to role. danielebarchiesi@0: * @param $name danielebarchiesi@0: * (optional) String for the name of the role. Defaults to a random string. danielebarchiesi@0: * @return danielebarchiesi@0: * Role ID of newly created role, or FALSE if role creation failed. danielebarchiesi@0: */ danielebarchiesi@0: protected function drupalCreateRole(array $permissions, $name = NULL) { danielebarchiesi@0: // Generate random name if it was not passed. danielebarchiesi@0: if (!$name) { danielebarchiesi@0: $name = $this->randomName(); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Check the all the permissions strings are valid. danielebarchiesi@0: if (!$this->checkPermissions($permissions)) { danielebarchiesi@0: return FALSE; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Create new role. danielebarchiesi@0: $role = new stdClass(); danielebarchiesi@0: $role->name = $name; danielebarchiesi@0: user_role_save($role); danielebarchiesi@0: user_role_grant_permissions($role->rid, $permissions); danielebarchiesi@0: danielebarchiesi@0: $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: if ($role && !empty($role->rid)) { danielebarchiesi@0: $count = db_query('SELECT COUNT(*) FROM {role_permission} WHERE rid = :rid', array(':rid' => $role->rid))->fetchField(); danielebarchiesi@0: $this->assertTrue($count == count($permissions), t('Created permissions: @perms', array('@perms' => implode(', ', $permissions))), t('Role')); danielebarchiesi@0: return $role->rid; danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: return FALSE; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Check to make sure that the array of permissions are valid. danielebarchiesi@0: * danielebarchiesi@0: * @param $permissions danielebarchiesi@0: * Permissions to check. danielebarchiesi@0: * @param $reset danielebarchiesi@0: * Reset cached available permissions. danielebarchiesi@0: * @return danielebarchiesi@0: * TRUE or FALSE depending on whether the permissions are valid. danielebarchiesi@0: */ danielebarchiesi@0: protected function checkPermissions(array $permissions, $reset = FALSE) { danielebarchiesi@0: $available = &drupal_static(__FUNCTION__); danielebarchiesi@0: danielebarchiesi@0: if (!isset($available) || $reset) { danielebarchiesi@0: $available = array_keys(module_invoke_all('permission')); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: $valid = TRUE; danielebarchiesi@0: foreach ($permissions as $permission) { danielebarchiesi@0: if (!in_array($permission, $available)) { danielebarchiesi@0: $this->fail(t('Invalid permission %permission.', array('%permission' => $permission)), t('Role')); danielebarchiesi@0: $valid = FALSE; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: return $valid; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Log in a user with the internal browser. danielebarchiesi@0: * danielebarchiesi@0: * If a user is already logged in, then the current user is logged out before danielebarchiesi@0: * logging in the specified user. danielebarchiesi@0: * danielebarchiesi@0: * Please note that neither the global $user nor the passed-in user object is danielebarchiesi@0: * populated with data of the logged in user. If you need full access to the danielebarchiesi@0: * user object after logging in, it must be updated manually. If you also need danielebarchiesi@0: * access to the plain-text password of the user (set by drupalCreateUser()), danielebarchiesi@0: * e.g. to log in the same user again, then it must be re-assigned manually. danielebarchiesi@0: * For example: danielebarchiesi@0: * @code danielebarchiesi@0: * // Create a user. danielebarchiesi@0: * $account = $this->drupalCreateUser(array()); danielebarchiesi@0: * $this->drupalLogin($account); danielebarchiesi@0: * // Load real user object. danielebarchiesi@0: * $pass_raw = $account->pass_raw; danielebarchiesi@0: * $account = user_load($account->uid); danielebarchiesi@0: * $account->pass_raw = $pass_raw; danielebarchiesi@0: * @endcode danielebarchiesi@0: * danielebarchiesi@0: * @param $account danielebarchiesi@0: * User object representing the user to log in. danielebarchiesi@0: * danielebarchiesi@0: * @see drupalCreateUser() danielebarchiesi@0: */ danielebarchiesi@0: protected function drupalLogin(stdClass $account) { danielebarchiesi@0: if ($this->loggedInUser) { danielebarchiesi@0: $this->drupalLogout(); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: $edit = array( danielebarchiesi@0: 'name' => $account->name, danielebarchiesi@0: 'pass' => $account->pass_raw danielebarchiesi@0: ); danielebarchiesi@0: $this->drupalPost('user', $edit, t('Log in')); danielebarchiesi@0: danielebarchiesi@0: // If a "log out" link appears on the page, it is almost certainly because danielebarchiesi@0: // the login was successful. danielebarchiesi@0: $pass = $this->assertLink(t('Log out'), 0, t('User %name successfully logged in.', array('%name' => $account->name)), t('User login')); danielebarchiesi@0: danielebarchiesi@0: if ($pass) { danielebarchiesi@0: $this->loggedInUser = $account; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Generate a token for the currently logged in user. danielebarchiesi@0: */ danielebarchiesi@0: protected function drupalGetToken($value = '') { danielebarchiesi@0: $private_key = drupal_get_private_key(); danielebarchiesi@0: return drupal_hmac_base64($value, $this->session_id . $private_key); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /* danielebarchiesi@0: * Logs a user out of the internal browser, then check the login page to confirm logout. danielebarchiesi@0: */ danielebarchiesi@0: protected function drupalLogout() { danielebarchiesi@0: // Make a request to the logout page, and redirect to the user page, the danielebarchiesi@0: // idea being if you were properly logged out you should be seeing a login danielebarchiesi@0: // screen. danielebarchiesi@0: $this->drupalGet('user/logout'); danielebarchiesi@0: $this->drupalGet('user'); danielebarchiesi@0: $pass = $this->assertField('name', t('Username field found.'), t('Logout')); danielebarchiesi@0: $pass = $pass && $this->assertField('pass', t('Password field found.'), t('Logout')); danielebarchiesi@0: danielebarchiesi@0: if ($pass) { danielebarchiesi@0: $this->loggedInUser = FALSE; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Generates a database prefix for running tests. danielebarchiesi@0: * danielebarchiesi@0: * The generated database table prefix is used for the Drupal installation danielebarchiesi@0: * being performed for the test. It is also used as user agent HTTP header danielebarchiesi@0: * value by the cURL-based browser of DrupalWebTestCase, which is sent danielebarchiesi@0: * to the Drupal installation of the test. During early Drupal bootstrap, the danielebarchiesi@0: * user agent HTTP header is parsed, and if it matches, all database queries danielebarchiesi@0: * use the database table prefix that has been generated here. danielebarchiesi@0: * danielebarchiesi@0: * @see DrupalWebTestCase::curlInitialize() danielebarchiesi@0: * @see drupal_valid_test_ua() danielebarchiesi@0: * @see DrupalWebTestCase::setUp() danielebarchiesi@0: */ danielebarchiesi@0: protected function prepareDatabasePrefix() { danielebarchiesi@0: $this->databasePrefix = 'simpletest' . mt_rand(1000, 1000000); danielebarchiesi@0: danielebarchiesi@0: // As soon as the database prefix is set, the test might start to execute. danielebarchiesi@0: // All assertions as well as the SimpleTest batch operations are associated danielebarchiesi@0: // with the testId, so the database prefix has to be associated with it. danielebarchiesi@0: db_update('simpletest_test_id') danielebarchiesi@0: ->fields(array('last_prefix' => $this->databasePrefix)) danielebarchiesi@0: ->condition('test_id', $this->testId) danielebarchiesi@0: ->execute(); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Changes the database connection to the prefixed one. danielebarchiesi@0: * danielebarchiesi@0: * @see DrupalWebTestCase::setUp() danielebarchiesi@0: */ danielebarchiesi@0: protected function changeDatabasePrefix() { danielebarchiesi@0: if (empty($this->databasePrefix)) { danielebarchiesi@0: $this->prepareDatabasePrefix(); danielebarchiesi@0: // If $this->prepareDatabasePrefix() failed to work, return without danielebarchiesi@0: // setting $this->setupDatabasePrefix to TRUE, so setUp() methods will danielebarchiesi@0: // know to bail out. danielebarchiesi@0: if (empty($this->databasePrefix)) { danielebarchiesi@0: return; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Clone the current connection and replace the current prefix. danielebarchiesi@0: $connection_info = Database::getConnectionInfo('default'); danielebarchiesi@0: Database::renameConnection('default', 'simpletest_original_default'); danielebarchiesi@0: foreach ($connection_info as $target => $value) { danielebarchiesi@0: $connection_info[$target]['prefix'] = array( danielebarchiesi@0: 'default' => $value['prefix']['default'] . $this->databasePrefix, danielebarchiesi@0: ); danielebarchiesi@0: } danielebarchiesi@0: Database::addConnectionInfo('default', 'default', $connection_info['default']); danielebarchiesi@0: danielebarchiesi@0: // Indicate the database prefix was set up correctly. danielebarchiesi@0: $this->setupDatabasePrefix = TRUE; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Prepares the current environment for running the test. danielebarchiesi@0: * danielebarchiesi@0: * Backups various current environment variables and resets them, so they do danielebarchiesi@0: * not interfere with the Drupal site installation in which tests are executed danielebarchiesi@0: * and can be restored in tearDown(). danielebarchiesi@0: * danielebarchiesi@0: * Also sets up new resources for the testing environment, such as the public danielebarchiesi@0: * filesystem and configuration directories. danielebarchiesi@0: * danielebarchiesi@0: * @see DrupalWebTestCase::setUp() danielebarchiesi@0: * @see DrupalWebTestCase::tearDown() danielebarchiesi@0: */ danielebarchiesi@0: protected function prepareEnvironment() { danielebarchiesi@0: global $user, $language, $conf; danielebarchiesi@0: danielebarchiesi@0: // Store necessary current values before switching to prefixed database. danielebarchiesi@0: $this->originalLanguage = $language; danielebarchiesi@0: $this->originalLanguageDefault = variable_get('language_default'); danielebarchiesi@0: $this->originalFileDirectory = variable_get('file_public_path', conf_path() . '/files'); danielebarchiesi@0: $this->originalProfile = drupal_get_profile(); danielebarchiesi@0: $this->originalCleanUrl = variable_get('clean_url', 0); danielebarchiesi@0: $this->originalUser = $user; danielebarchiesi@0: danielebarchiesi@0: // Set to English to prevent exceptions from utf8_truncate() from t() danielebarchiesi@0: // during install if the current language is not 'en'. danielebarchiesi@0: // The following array/object conversion is copied from language_default(). danielebarchiesi@0: $language = (object) array('language' => 'en', 'name' => 'English', 'native' => 'English', 'direction' => 0, 'enabled' => 1, 'plurals' => 0, 'formula' => '', 'domain' => '', 'prefix' => '', 'weight' => 0, 'javascript' => ''); danielebarchiesi@0: danielebarchiesi@0: // Save and clean the shutdown callbacks array because it is static cached danielebarchiesi@0: // and will be changed by the test run. Otherwise it will contain callbacks danielebarchiesi@0: // from both environments and the testing environment will try to call the danielebarchiesi@0: // handlers defined by the original one. danielebarchiesi@0: $callbacks = &drupal_register_shutdown_function(); danielebarchiesi@0: $this->originalShutdownCallbacks = $callbacks; danielebarchiesi@0: $callbacks = array(); danielebarchiesi@0: danielebarchiesi@0: // Create test directory ahead of installation so fatal errors and debug danielebarchiesi@0: // information can be logged during installation process. danielebarchiesi@0: // Use temporary files directory with the same prefix as the database. danielebarchiesi@0: $this->public_files_directory = $this->originalFileDirectory . '/simpletest/' . substr($this->databasePrefix, 10); danielebarchiesi@0: $this->private_files_directory = $this->public_files_directory . '/private'; danielebarchiesi@0: $this->temp_files_directory = $this->private_files_directory . '/temp'; danielebarchiesi@0: danielebarchiesi@0: // Create the directories danielebarchiesi@0: file_prepare_directory($this->public_files_directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS); danielebarchiesi@0: file_prepare_directory($this->private_files_directory, FILE_CREATE_DIRECTORY); danielebarchiesi@0: file_prepare_directory($this->temp_files_directory, FILE_CREATE_DIRECTORY); danielebarchiesi@0: $this->generatedTestFiles = FALSE; danielebarchiesi@0: danielebarchiesi@0: // Log fatal errors. danielebarchiesi@0: ini_set('log_errors', 1); danielebarchiesi@0: ini_set('error_log', $this->public_files_directory . '/error.log'); danielebarchiesi@0: danielebarchiesi@0: // Set the test information for use in other parts of Drupal. danielebarchiesi@0: $test_info = &$GLOBALS['drupal_test_info']; danielebarchiesi@0: $test_info['test_run_id'] = $this->databasePrefix; danielebarchiesi@0: $test_info['in_child_site'] = FALSE; danielebarchiesi@0: danielebarchiesi@0: // Indicate the environment was set up correctly. danielebarchiesi@0: $this->setupEnvironment = TRUE; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Sets up a Drupal site for running functional and integration tests. danielebarchiesi@0: * danielebarchiesi@0: * Generates a random database prefix and installs Drupal with the specified danielebarchiesi@0: * installation profile in DrupalWebTestCase::$profile into the danielebarchiesi@0: * prefixed database. Afterwards, installs any additional modules specified by danielebarchiesi@0: * the test. danielebarchiesi@0: * danielebarchiesi@0: * After installation all caches are flushed and several configuration values danielebarchiesi@0: * are reset to the values of the parent site executing the test, since the danielebarchiesi@0: * default values may be incompatible with the environment in which tests are danielebarchiesi@0: * being executed. danielebarchiesi@0: * danielebarchiesi@0: * @param ... danielebarchiesi@0: * List of modules to enable for the duration of the test. This can be danielebarchiesi@0: * either a single array or a variable number of string arguments. danielebarchiesi@0: * danielebarchiesi@0: * @see DrupalWebTestCase::prepareDatabasePrefix() danielebarchiesi@0: * @see DrupalWebTestCase::changeDatabasePrefix() danielebarchiesi@0: * @see DrupalWebTestCase::prepareEnvironment() danielebarchiesi@0: */ danielebarchiesi@0: protected function setUp() { danielebarchiesi@0: global $user, $language, $conf; danielebarchiesi@0: danielebarchiesi@0: // Create the database prefix for this test. danielebarchiesi@0: $this->prepareDatabasePrefix(); danielebarchiesi@0: danielebarchiesi@0: // Prepare the environment for running tests. danielebarchiesi@0: $this->prepareEnvironment(); danielebarchiesi@0: if (!$this->setupEnvironment) { danielebarchiesi@0: return FALSE; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Reset all statics and variables to perform tests in a clean environment. danielebarchiesi@0: $conf = array(); danielebarchiesi@0: drupal_static_reset(); danielebarchiesi@0: danielebarchiesi@0: // Change the database prefix. danielebarchiesi@0: // All static variables need to be reset before the database prefix is danielebarchiesi@0: // changed, since DrupalCacheArray implementations attempt to danielebarchiesi@0: // write back to persistent caches when they are destructed. danielebarchiesi@0: $this->changeDatabasePrefix(); danielebarchiesi@0: if (!$this->setupDatabasePrefix) { danielebarchiesi@0: return FALSE; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Preset the 'install_profile' system variable, so the first call into danielebarchiesi@0: // system_rebuild_module_data() (in drupal_install_system()) will register danielebarchiesi@0: // the test's profile as a module. Without this, the installation profile of danielebarchiesi@0: // the parent site (executing the test) is registered, and the test danielebarchiesi@0: // profile's hook_install() and other hook implementations are never invoked. danielebarchiesi@0: $conf['install_profile'] = $this->profile; danielebarchiesi@0: danielebarchiesi@0: // Perform the actual Drupal installation. danielebarchiesi@0: include_once DRUPAL_ROOT . '/includes/install.inc'; danielebarchiesi@0: drupal_install_system(); danielebarchiesi@0: danielebarchiesi@0: $this->preloadRegistry(); danielebarchiesi@0: danielebarchiesi@0: // Set path variables. danielebarchiesi@0: variable_set('file_public_path', $this->public_files_directory); danielebarchiesi@0: variable_set('file_private_path', $this->private_files_directory); danielebarchiesi@0: variable_set('file_temporary_path', $this->temp_files_directory); danielebarchiesi@0: danielebarchiesi@0: // Set the 'simpletest_parent_profile' variable to add the parent profile's danielebarchiesi@0: // search path to the child site's search paths. danielebarchiesi@0: // @see drupal_system_listing() danielebarchiesi@0: // @todo This may need to be primed like 'install_profile' above. danielebarchiesi@0: variable_set('simpletest_parent_profile', $this->originalProfile); danielebarchiesi@0: danielebarchiesi@0: // Include the testing profile. danielebarchiesi@0: variable_set('install_profile', $this->profile); danielebarchiesi@0: $profile_details = install_profile_info($this->profile, 'en'); danielebarchiesi@0: danielebarchiesi@0: // Install the modules specified by the testing profile. danielebarchiesi@0: module_enable($profile_details['dependencies'], FALSE); danielebarchiesi@0: danielebarchiesi@0: // Install modules needed for this test. This could have been passed in as danielebarchiesi@0: // either a single array argument or a variable number of string arguments. danielebarchiesi@0: // @todo Remove this compatibility layer in Drupal 8, and only accept danielebarchiesi@0: // $modules as a single array argument. danielebarchiesi@0: $modules = func_get_args(); danielebarchiesi@0: if (isset($modules[0]) && is_array($modules[0])) { danielebarchiesi@0: $modules = $modules[0]; danielebarchiesi@0: } danielebarchiesi@0: if ($modules) { danielebarchiesi@0: $success = module_enable($modules, TRUE); danielebarchiesi@0: $this->assertTrue($success, t('Enabled modules: %modules', array('%modules' => implode(', ', $modules)))); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Run the profile tasks. danielebarchiesi@0: $install_profile_module_exists = db_query("SELECT 1 FROM {system} WHERE type = 'module' AND name = :name", array( danielebarchiesi@0: ':name' => $this->profile, danielebarchiesi@0: ))->fetchField(); danielebarchiesi@0: if ($install_profile_module_exists) { danielebarchiesi@0: module_enable(array($this->profile), FALSE); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Reset/rebuild all data structures after enabling the modules. danielebarchiesi@0: $this->resetAll(); danielebarchiesi@0: danielebarchiesi@0: // Run cron once in that environment, as install.php does at the end of danielebarchiesi@0: // the installation process. danielebarchiesi@0: drupal_cron_run(); danielebarchiesi@0: danielebarchiesi@0: // Ensure that the session is not written to the new environment and replace danielebarchiesi@0: // the global $user session with uid 1 from the new test site. danielebarchiesi@0: drupal_save_session(FALSE); danielebarchiesi@0: // Login as uid 1. danielebarchiesi@0: $user = user_load(1); danielebarchiesi@0: danielebarchiesi@0: // Restore necessary variables. danielebarchiesi@0: variable_set('install_task', 'done'); danielebarchiesi@0: variable_set('clean_url', $this->originalCleanUrl); danielebarchiesi@0: variable_set('site_mail', 'simpletest@example.com'); danielebarchiesi@0: variable_set('date_default_timezone', date_default_timezone_get()); danielebarchiesi@0: danielebarchiesi@0: // Set up English language. danielebarchiesi@0: unset($conf['language_default']); danielebarchiesi@0: $language = language_default(); danielebarchiesi@0: danielebarchiesi@0: // Use the test mail class instead of the default mail handler class. danielebarchiesi@0: variable_set('mail_system', array('default-system' => 'TestingMailSystem')); danielebarchiesi@0: danielebarchiesi@0: drupal_set_time_limit($this->timeLimit); danielebarchiesi@0: $this->setup = TRUE; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Preload the registry from the testing site. danielebarchiesi@0: * danielebarchiesi@0: * This method is called by DrupalWebTestCase::setUp(), and preloads the danielebarchiesi@0: * registry from the testing site to cut down on the time it takes to danielebarchiesi@0: * set up a clean environment for the current test run. danielebarchiesi@0: */ danielebarchiesi@0: protected function preloadRegistry() { danielebarchiesi@0: // Use two separate queries, each with their own connections: copy the danielebarchiesi@0: // {registry} and {registry_file} tables over from the parent installation danielebarchiesi@0: // to the child installation. danielebarchiesi@0: $original_connection = Database::getConnection('default', 'simpletest_original_default'); danielebarchiesi@0: $test_connection = Database::getConnection(); danielebarchiesi@0: danielebarchiesi@0: foreach (array('registry', 'registry_file') as $table) { danielebarchiesi@0: // Find the records from the parent database. danielebarchiesi@0: $source_query = $original_connection danielebarchiesi@0: ->select($table, array(), array('fetch' => PDO::FETCH_ASSOC)) danielebarchiesi@0: ->fields($table); danielebarchiesi@0: danielebarchiesi@0: $dest_query = $test_connection->insert($table); danielebarchiesi@0: danielebarchiesi@0: $first = TRUE; danielebarchiesi@0: foreach ($source_query->execute() as $row) { danielebarchiesi@0: if ($first) { danielebarchiesi@0: $dest_query->fields(array_keys($row)); danielebarchiesi@0: $first = FALSE; danielebarchiesi@0: } danielebarchiesi@0: // Insert the records into the child database. danielebarchiesi@0: $dest_query->values($row); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: $dest_query->execute(); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Reset all data structures after having enabled new modules. danielebarchiesi@0: * danielebarchiesi@0: * This method is called by DrupalWebTestCase::setUp() after enabling danielebarchiesi@0: * the requested modules. It must be called again when additional modules danielebarchiesi@0: * are enabled later. danielebarchiesi@0: */ danielebarchiesi@0: protected function resetAll() { danielebarchiesi@0: // Reset all static variables. danielebarchiesi@0: drupal_static_reset(); danielebarchiesi@0: // Reset the list of enabled modules. danielebarchiesi@0: module_list(TRUE); danielebarchiesi@0: danielebarchiesi@0: // Reset cached schema for new database prefix. This must be done before danielebarchiesi@0: // drupal_flush_all_caches() so rebuilds can make use of the schema of danielebarchiesi@0: // modules enabled on the cURL side. danielebarchiesi@0: drupal_get_schema(NULL, TRUE); danielebarchiesi@0: danielebarchiesi@0: // Perform rebuilds and flush remaining caches. danielebarchiesi@0: drupal_flush_all_caches(); danielebarchiesi@0: danielebarchiesi@0: // Reload global $conf array and permissions. danielebarchiesi@0: $this->refreshVariables(); danielebarchiesi@0: $this->checkPermissions(array(), TRUE); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Refresh the in-memory set of variables. Useful after a page request is made danielebarchiesi@0: * that changes a variable in a different thread. danielebarchiesi@0: * danielebarchiesi@0: * In other words calling a settings page with $this->drupalPost() with a changed danielebarchiesi@0: * value would update a variable to reflect that change, but in the thread that danielebarchiesi@0: * made the call (thread running the test) the changed variable would not be danielebarchiesi@0: * picked up. danielebarchiesi@0: * danielebarchiesi@0: * This method clears the variables cache and loads a fresh copy from the database danielebarchiesi@0: * to ensure that the most up-to-date set of variables is loaded. danielebarchiesi@0: */ danielebarchiesi@0: protected function refreshVariables() { danielebarchiesi@0: global $conf; danielebarchiesi@0: cache_clear_all('variables', 'cache_bootstrap'); danielebarchiesi@0: $conf = variable_initialize(); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Delete created files and temporary files directory, delete the tables created by setUp(), danielebarchiesi@0: * and reset the database prefix. danielebarchiesi@0: */ danielebarchiesi@0: protected function tearDown() { danielebarchiesi@0: global $user, $language; danielebarchiesi@0: danielebarchiesi@0: // In case a fatal error occurred that was not in the test process read the danielebarchiesi@0: // log to pick up any fatal errors. danielebarchiesi@0: simpletest_log_read($this->testId, $this->databasePrefix, get_class($this), TRUE); danielebarchiesi@0: danielebarchiesi@0: $emailCount = count(variable_get('drupal_test_email_collector', array())); danielebarchiesi@0: if ($emailCount) { danielebarchiesi@0: $message = format_plural($emailCount, '1 e-mail was sent during this test.', '@count e-mails were sent during this test.'); danielebarchiesi@0: $this->pass($message, t('E-mail')); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Delete temporary files directory. danielebarchiesi@0: file_unmanaged_delete_recursive($this->originalFileDirectory . '/simpletest/' . substr($this->databasePrefix, 10)); danielebarchiesi@0: danielebarchiesi@0: // Remove all prefixed tables. danielebarchiesi@0: $tables = db_find_tables($this->databasePrefix . '%'); danielebarchiesi@0: $connection_info = Database::getConnectionInfo('default'); danielebarchiesi@0: $tables = db_find_tables($connection_info['default']['prefix']['default'] . '%'); danielebarchiesi@0: if (empty($tables)) { danielebarchiesi@0: $this->fail('Failed to find test tables to drop.'); danielebarchiesi@0: } danielebarchiesi@0: $prefix_length = strlen($connection_info['default']['prefix']['default']); danielebarchiesi@0: foreach ($tables as $table) { danielebarchiesi@0: if (db_drop_table(substr($table, $prefix_length))) { danielebarchiesi@0: unset($tables[$table]); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: if (!empty($tables)) { danielebarchiesi@0: $this->fail('Failed to drop all prefixed tables.'); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Get back to the original connection. danielebarchiesi@0: Database::removeConnection('default'); danielebarchiesi@0: Database::renameConnection('simpletest_original_default', 'default'); danielebarchiesi@0: danielebarchiesi@0: // Restore original shutdown callbacks array to prevent original danielebarchiesi@0: // environment of calling handlers from test run. danielebarchiesi@0: $callbacks = &drupal_register_shutdown_function(); danielebarchiesi@0: $callbacks = $this->originalShutdownCallbacks; danielebarchiesi@0: danielebarchiesi@0: // Return the user to the original one. danielebarchiesi@0: $user = $this->originalUser; danielebarchiesi@0: drupal_save_session(TRUE); danielebarchiesi@0: danielebarchiesi@0: // Ensure that internal logged in variable and cURL options are reset. danielebarchiesi@0: $this->loggedInUser = FALSE; danielebarchiesi@0: $this->additionalCurlOptions = array(); danielebarchiesi@0: danielebarchiesi@0: // Reload module list and implementations to ensure that test module hooks danielebarchiesi@0: // aren't called after tests. danielebarchiesi@0: module_list(TRUE); danielebarchiesi@0: module_implements('', FALSE, TRUE); danielebarchiesi@0: danielebarchiesi@0: // Reset the Field API. danielebarchiesi@0: field_cache_clear(); danielebarchiesi@0: danielebarchiesi@0: // Rebuild caches. danielebarchiesi@0: $this->refreshVariables(); danielebarchiesi@0: danielebarchiesi@0: // Reset public files directory. danielebarchiesi@0: $GLOBALS['conf']['file_public_path'] = $this->originalFileDirectory; danielebarchiesi@0: danielebarchiesi@0: // Reset language. danielebarchiesi@0: $language = $this->originalLanguage; danielebarchiesi@0: if ($this->originalLanguageDefault) { danielebarchiesi@0: $GLOBALS['conf']['language_default'] = $this->originalLanguageDefault; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Close the CURL handler. danielebarchiesi@0: $this->curlClose(); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Initializes the cURL connection. danielebarchiesi@0: * danielebarchiesi@0: * If the simpletest_httpauth_credentials variable is set, this function will danielebarchiesi@0: * add HTTP authentication headers. This is necessary for testing sites that danielebarchiesi@0: * are protected by login credentials from public access. danielebarchiesi@0: * See the description of $curl_options for other options. danielebarchiesi@0: */ danielebarchiesi@0: protected function curlInitialize() { danielebarchiesi@0: global $base_url; danielebarchiesi@0: danielebarchiesi@0: if (!isset($this->curlHandle)) { danielebarchiesi@0: $this->curlHandle = curl_init(); danielebarchiesi@0: danielebarchiesi@0: // Some versions/configurations of cURL break on a NULL cookie jar, so danielebarchiesi@0: // supply a real file. danielebarchiesi@0: if (empty($this->cookieFile)) { danielebarchiesi@0: $this->cookieFile = $this->public_files_directory . '/cookie.jar'; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: $curl_options = array( danielebarchiesi@0: CURLOPT_COOKIEJAR => $this->cookieFile, danielebarchiesi@0: CURLOPT_URL => $base_url, danielebarchiesi@0: CURLOPT_FOLLOWLOCATION => FALSE, danielebarchiesi@0: CURLOPT_RETURNTRANSFER => TRUE, danielebarchiesi@0: CURLOPT_SSL_VERIFYPEER => FALSE, // Required to make the tests run on HTTPS. danielebarchiesi@0: CURLOPT_SSL_VERIFYHOST => FALSE, // Required to make the tests run on HTTPS. danielebarchiesi@0: CURLOPT_HEADERFUNCTION => array(&$this, 'curlHeaderCallback'), danielebarchiesi@0: CURLOPT_USERAGENT => $this->databasePrefix, danielebarchiesi@0: ); danielebarchiesi@0: if (isset($this->httpauth_credentials)) { danielebarchiesi@0: $curl_options[CURLOPT_HTTPAUTH] = $this->httpauth_method; danielebarchiesi@0: $curl_options[CURLOPT_USERPWD] = $this->httpauth_credentials; danielebarchiesi@0: } danielebarchiesi@0: // curl_setopt_array() returns FALSE if any of the specified options danielebarchiesi@0: // cannot be set, and stops processing any further options. danielebarchiesi@0: $result = curl_setopt_array($this->curlHandle, $this->additionalCurlOptions + $curl_options); danielebarchiesi@0: if (!$result) { danielebarchiesi@0: throw new Exception('One or more cURL options could not be set.'); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // By default, the child session name should be the same as the parent. danielebarchiesi@0: $this->session_name = session_name(); danielebarchiesi@0: } danielebarchiesi@0: // We set the user agent header on each request so as to use the current danielebarchiesi@0: // time and a new uniqid. danielebarchiesi@0: if (preg_match('/simpletest\d+/', $this->databasePrefix, $matches)) { danielebarchiesi@0: curl_setopt($this->curlHandle, CURLOPT_USERAGENT, drupal_generate_test_ua($matches[0])); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Initializes and executes a cURL request. danielebarchiesi@0: * danielebarchiesi@0: * @param $curl_options danielebarchiesi@0: * An associative array of cURL options to set, where the keys are constants danielebarchiesi@0: * defined by the cURL library. For a list of valid options, see danielebarchiesi@0: * http://www.php.net/manual/function.curl-setopt.php danielebarchiesi@0: * @param $redirect danielebarchiesi@0: * FALSE if this is an initial request, TRUE if this request is the result danielebarchiesi@0: * of a redirect. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * The content returned from the call to curl_exec(). danielebarchiesi@0: * danielebarchiesi@0: * @see curlInitialize() danielebarchiesi@0: */ danielebarchiesi@0: protected function curlExec($curl_options, $redirect = FALSE) { danielebarchiesi@0: $this->curlInitialize(); danielebarchiesi@0: danielebarchiesi@0: // cURL incorrectly handles URLs with a fragment by including the danielebarchiesi@0: // fragment in the request to the server, causing some web servers danielebarchiesi@0: // to reject the request citing "400 - Bad Request". To prevent danielebarchiesi@0: // this, we strip the fragment from the request. danielebarchiesi@0: // TODO: Remove this for Drupal 8, since fixed in curl 7.20.0. danielebarchiesi@0: if (!empty($curl_options[CURLOPT_URL]) && strpos($curl_options[CURLOPT_URL], '#')) { danielebarchiesi@0: $original_url = $curl_options[CURLOPT_URL]; danielebarchiesi@0: $curl_options[CURLOPT_URL] = strtok($curl_options[CURLOPT_URL], '#'); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: $url = empty($curl_options[CURLOPT_URL]) ? curl_getinfo($this->curlHandle, CURLINFO_EFFECTIVE_URL) : $curl_options[CURLOPT_URL]; danielebarchiesi@0: danielebarchiesi@0: if (!empty($curl_options[CURLOPT_POST])) { danielebarchiesi@0: // This is a fix for the Curl library to prevent Expect: 100-continue danielebarchiesi@0: // headers in POST requests, that may cause unexpected HTTP response danielebarchiesi@0: // codes from some webservers (like lighttpd that returns a 417 error danielebarchiesi@0: // code). It is done by setting an empty "Expect" header field that is danielebarchiesi@0: // not overwritten by Curl. danielebarchiesi@0: $curl_options[CURLOPT_HTTPHEADER][] = 'Expect:'; danielebarchiesi@0: } danielebarchiesi@0: curl_setopt_array($this->curlHandle, $this->additionalCurlOptions + $curl_options); danielebarchiesi@0: danielebarchiesi@0: if (!$redirect) { danielebarchiesi@0: // Reset headers, the session ID and the redirect counter. danielebarchiesi@0: $this->session_id = NULL; danielebarchiesi@0: $this->headers = array(); danielebarchiesi@0: $this->redirect_count = 0; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: $content = curl_exec($this->curlHandle); danielebarchiesi@0: $status = curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE); danielebarchiesi@0: danielebarchiesi@0: // cURL incorrectly handles URLs with fragments, so instead of danielebarchiesi@0: // letting cURL handle redirects we take of them ourselves to danielebarchiesi@0: // to prevent fragments being sent to the web server as part danielebarchiesi@0: // of the request. danielebarchiesi@0: // TODO: Remove this for Drupal 8, since fixed in curl 7.20.0. danielebarchiesi@0: if (in_array($status, array(300, 301, 302, 303, 305, 307)) && $this->redirect_count < variable_get('simpletest_maximum_redirects', 5)) { danielebarchiesi@0: if ($this->drupalGetHeader('location')) { danielebarchiesi@0: $this->redirect_count++; danielebarchiesi@0: $curl_options = array(); danielebarchiesi@0: $curl_options[CURLOPT_URL] = $this->drupalGetHeader('location'); danielebarchiesi@0: $curl_options[CURLOPT_HTTPGET] = TRUE; danielebarchiesi@0: return $this->curlExec($curl_options, TRUE); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: $this->drupalSetContent($content, isset($original_url) ? $original_url : curl_getinfo($this->curlHandle, CURLINFO_EFFECTIVE_URL)); danielebarchiesi@0: $message_vars = array( danielebarchiesi@0: '!method' => !empty($curl_options[CURLOPT_NOBODY]) ? 'HEAD' : (empty($curl_options[CURLOPT_POSTFIELDS]) ? 'GET' : 'POST'), danielebarchiesi@0: '@url' => isset($original_url) ? $original_url : $url, danielebarchiesi@0: '@status' => $status, danielebarchiesi@0: '!length' => format_size(strlen($this->drupalGetContent())) danielebarchiesi@0: ); danielebarchiesi@0: $message = t('!method @url returned @status (!length).', $message_vars); danielebarchiesi@0: $this->assertTrue($this->drupalGetContent() !== FALSE, $message, t('Browser')); danielebarchiesi@0: return $this->drupalGetContent(); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Reads headers and registers errors received from the tested site. danielebarchiesi@0: * danielebarchiesi@0: * @see _drupal_log_error(). danielebarchiesi@0: * danielebarchiesi@0: * @param $curlHandler danielebarchiesi@0: * The cURL handler. danielebarchiesi@0: * @param $header danielebarchiesi@0: * An header. danielebarchiesi@0: */ danielebarchiesi@0: protected function curlHeaderCallback($curlHandler, $header) { danielebarchiesi@0: // Header fields can be extended over multiple lines by preceding each danielebarchiesi@0: // extra line with at least one SP or HT. They should be joined on receive. danielebarchiesi@0: // Details are in RFC2616 section 4. danielebarchiesi@0: if ($header[0] == ' ' || $header[0] == "\t") { danielebarchiesi@0: // Normalize whitespace between chucks. danielebarchiesi@0: $this->headers[] = array_pop($this->headers) . ' ' . trim($header); danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: $this->headers[] = $header; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Errors are being sent via X-Drupal-Assertion-* headers, danielebarchiesi@0: // generated by _drupal_log_error() in the exact form required danielebarchiesi@0: // by DrupalWebTestCase::error(). danielebarchiesi@0: if (preg_match('/^X-Drupal-Assertion-[0-9]+: (.*)$/', $header, $matches)) { danielebarchiesi@0: // Call DrupalWebTestCase::error() with the parameters from the header. danielebarchiesi@0: call_user_func_array(array(&$this, 'error'), unserialize(urldecode($matches[1]))); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Save cookies. danielebarchiesi@0: if (preg_match('/^Set-Cookie: ([^=]+)=(.+)/', $header, $matches)) { danielebarchiesi@0: $name = $matches[1]; danielebarchiesi@0: $parts = array_map('trim', explode(';', $matches[2])); danielebarchiesi@0: $value = array_shift($parts); danielebarchiesi@0: $this->cookies[$name] = array('value' => $value, 'secure' => in_array('secure', $parts)); danielebarchiesi@0: if ($name == $this->session_name) { danielebarchiesi@0: if ($value != 'deleted') { danielebarchiesi@0: $this->session_id = $value; danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: $this->session_id = NULL; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // This is required by cURL. danielebarchiesi@0: return strlen($header); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Close the cURL handler and unset the handler. danielebarchiesi@0: */ danielebarchiesi@0: protected function curlClose() { danielebarchiesi@0: if (isset($this->curlHandle)) { danielebarchiesi@0: curl_close($this->curlHandle); danielebarchiesi@0: unset($this->curlHandle); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Parse content returned from curlExec using DOM and SimpleXML. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * A SimpleXMLElement or FALSE on failure. danielebarchiesi@0: */ danielebarchiesi@0: protected function parse() { danielebarchiesi@0: if (!$this->elements) { danielebarchiesi@0: // DOM can load HTML soup. But, HTML soup can throw warnings, suppress danielebarchiesi@0: // them. danielebarchiesi@0: $htmlDom = new DOMDocument(); danielebarchiesi@0: @$htmlDom->loadHTML($this->drupalGetContent()); danielebarchiesi@0: if ($htmlDom) { danielebarchiesi@0: $this->pass(t('Valid HTML found on "@path"', array('@path' => $this->getUrl())), t('Browser')); danielebarchiesi@0: // It's much easier to work with simplexml than DOM, luckily enough danielebarchiesi@0: // we can just simply import our DOM tree. danielebarchiesi@0: $this->elements = simplexml_import_dom($htmlDom); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: if (!$this->elements) { danielebarchiesi@0: $this->fail(t('Parsed page successfully.'), t('Browser')); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: return $this->elements; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Retrieves a Drupal path or an absolute path. danielebarchiesi@0: * danielebarchiesi@0: * @param $path danielebarchiesi@0: * Drupal path or URL to load into internal browser danielebarchiesi@0: * @param $options danielebarchiesi@0: * Options to be forwarded to url(). danielebarchiesi@0: * @param $headers danielebarchiesi@0: * An array containing additional HTTP request headers, each formatted as danielebarchiesi@0: * "name: value". danielebarchiesi@0: * @return danielebarchiesi@0: * The retrieved HTML string, also available as $this->drupalGetContent() danielebarchiesi@0: */ danielebarchiesi@0: protected function drupalGet($path, array $options = array(), array $headers = array()) { danielebarchiesi@0: $options['absolute'] = TRUE; danielebarchiesi@0: danielebarchiesi@0: // We re-using a CURL connection here. If that connection still has certain danielebarchiesi@0: // options set, it might change the GET into a POST. Make sure we clear out danielebarchiesi@0: // previous options. danielebarchiesi@0: $out = $this->curlExec(array(CURLOPT_HTTPGET => TRUE, CURLOPT_URL => url($path, $options), CURLOPT_NOBODY => FALSE, CURLOPT_HTTPHEADER => $headers)); danielebarchiesi@0: $this->refreshVariables(); // Ensure that any changes to variables in the other thread are picked up. danielebarchiesi@0: danielebarchiesi@0: // Replace original page output with new output from redirected page(s). danielebarchiesi@0: if ($new = $this->checkForMetaRefresh()) { danielebarchiesi@0: $out = $new; danielebarchiesi@0: } danielebarchiesi@0: $this->verbose('GET request to: ' . $path . danielebarchiesi@0: '
Ending URL: ' . $this->getUrl() . danielebarchiesi@0: '
' . $out); danielebarchiesi@0: return $out; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Retrieve a Drupal path or an absolute path and JSON decode the result. danielebarchiesi@0: */ danielebarchiesi@0: protected function drupalGetAJAX($path, array $options = array(), array $headers = array()) { danielebarchiesi@0: return drupal_json_decode($this->drupalGet($path, $options, $headers)); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Execute a POST request on a Drupal page. danielebarchiesi@0: * It will be done as usual POST request with SimpleBrowser. danielebarchiesi@0: * danielebarchiesi@0: * @param $path danielebarchiesi@0: * Location of the post form. Either a Drupal path or an absolute path or danielebarchiesi@0: * NULL to post to the current page. For multi-stage forms you can set the danielebarchiesi@0: * path to NULL and have it post to the last received page. Example: danielebarchiesi@0: * danielebarchiesi@0: * @code danielebarchiesi@0: * // First step in form. danielebarchiesi@0: * $edit = array(...); danielebarchiesi@0: * $this->drupalPost('some_url', $edit, t('Save')); danielebarchiesi@0: * danielebarchiesi@0: * // Second step in form. danielebarchiesi@0: * $edit = array(...); danielebarchiesi@0: * $this->drupalPost(NULL, $edit, t('Save')); danielebarchiesi@0: * @endcode danielebarchiesi@0: * @param $edit danielebarchiesi@0: * Field data in an associative array. Changes the current input fields danielebarchiesi@0: * (where possible) to the values indicated. A checkbox can be set to danielebarchiesi@0: * TRUE to be checked and FALSE to be unchecked. Note that when a form danielebarchiesi@0: * contains file upload fields, other fields cannot start with the '@' danielebarchiesi@0: * character. danielebarchiesi@0: * danielebarchiesi@0: * Multiple select fields can be set using name[] and setting each of the danielebarchiesi@0: * possible values. Example: danielebarchiesi@0: * @code danielebarchiesi@0: * $edit = array(); danielebarchiesi@0: * $edit['name[]'] = array('value1', 'value2'); danielebarchiesi@0: * @endcode danielebarchiesi@0: * @param $submit danielebarchiesi@0: * Value of the submit button whose click is to be emulated. For example, danielebarchiesi@0: * t('Save'). The processing of the request depends on this value. For danielebarchiesi@0: * example, a form may have one button with the value t('Save') and another danielebarchiesi@0: * button with the value t('Delete'), and execute different code depending danielebarchiesi@0: * on which one is clicked. danielebarchiesi@0: * danielebarchiesi@0: * This function can also be called to emulate an Ajax submission. In this danielebarchiesi@0: * case, this value needs to be an array with the following keys: danielebarchiesi@0: * - path: A path to submit the form values to for Ajax-specific processing, danielebarchiesi@0: * which is likely different than the $path parameter used for retrieving danielebarchiesi@0: * the initial form. Defaults to 'system/ajax'. danielebarchiesi@0: * - triggering_element: If the value for the 'path' key is 'system/ajax' or danielebarchiesi@0: * another generic Ajax processing path, this needs to be set to the name danielebarchiesi@0: * of the element. If the name doesn't identify the element uniquely, then danielebarchiesi@0: * this should instead be an array with a single key/value pair, danielebarchiesi@0: * corresponding to the element name and value. The callback for the danielebarchiesi@0: * generic Ajax processing path uses this to find the #ajax information danielebarchiesi@0: * for the element, including which specific callback to use for danielebarchiesi@0: * processing the request. danielebarchiesi@0: * danielebarchiesi@0: * This can also be set to NULL in order to emulate an Internet Explorer danielebarchiesi@0: * submission of a form with a single text field, and pressing ENTER in that danielebarchiesi@0: * textfield: under these conditions, no button information is added to the danielebarchiesi@0: * POST data. danielebarchiesi@0: * @param $options danielebarchiesi@0: * Options to be forwarded to url(). danielebarchiesi@0: * @param $headers danielebarchiesi@0: * An array containing additional HTTP request headers, each formatted as danielebarchiesi@0: * "name: value". danielebarchiesi@0: * @param $form_html_id danielebarchiesi@0: * (optional) HTML ID of the form to be submitted. On some pages danielebarchiesi@0: * there are many identical forms, so just using the value of the submit danielebarchiesi@0: * button is not enough. For example: 'trigger-node-presave-assign-form'. danielebarchiesi@0: * Note that this is not the Drupal $form_id, but rather the HTML ID of the danielebarchiesi@0: * form, which is typically the same thing but with hyphens replacing the danielebarchiesi@0: * underscores. danielebarchiesi@0: * @param $extra_post danielebarchiesi@0: * (optional) A string of additional data to append to the POST submission. danielebarchiesi@0: * This can be used to add POST data for which there are no HTML fields, as danielebarchiesi@0: * is done by drupalPostAJAX(). This string is literally appended to the danielebarchiesi@0: * POST data, so it must already be urlencoded and contain a leading "&" danielebarchiesi@0: * (e.g., "&extra_var1=hello+world&extra_var2=you%26me"). danielebarchiesi@0: */ danielebarchiesi@0: protected function drupalPost($path, $edit, $submit, array $options = array(), array $headers = array(), $form_html_id = NULL, $extra_post = NULL) { danielebarchiesi@0: $submit_matches = FALSE; danielebarchiesi@0: $ajax = is_array($submit); danielebarchiesi@0: if (isset($path)) { danielebarchiesi@0: $this->drupalGet($path, $options); danielebarchiesi@0: } danielebarchiesi@0: if ($this->parse()) { danielebarchiesi@0: $edit_save = $edit; danielebarchiesi@0: // Let's iterate over all the forms. danielebarchiesi@0: $xpath = "//form"; danielebarchiesi@0: if (!empty($form_html_id)) { danielebarchiesi@0: $xpath .= "[@id='" . $form_html_id . "']"; danielebarchiesi@0: } danielebarchiesi@0: $forms = $this->xpath($xpath); danielebarchiesi@0: foreach ($forms as $form) { danielebarchiesi@0: // We try to set the fields of this form as specified in $edit. danielebarchiesi@0: $edit = $edit_save; danielebarchiesi@0: $post = array(); danielebarchiesi@0: $upload = array(); danielebarchiesi@0: $submit_matches = $this->handleForm($post, $edit, $upload, $ajax ? NULL : $submit, $form); danielebarchiesi@0: $action = isset($form['action']) ? $this->getAbsoluteUrl((string) $form['action']) : $this->getUrl(); danielebarchiesi@0: if ($ajax) { danielebarchiesi@0: $action = $this->getAbsoluteUrl(!empty($submit['path']) ? $submit['path'] : 'system/ajax'); danielebarchiesi@0: // Ajax callbacks verify the triggering element if necessary, so while danielebarchiesi@0: // we may eventually want extra code that verifies it in the danielebarchiesi@0: // handleForm() function, it's not currently a requirement. danielebarchiesi@0: $submit_matches = TRUE; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // We post only if we managed to handle every field in edit and the danielebarchiesi@0: // submit button matches. danielebarchiesi@0: if (!$edit && ($submit_matches || !isset($submit))) { danielebarchiesi@0: $post_array = $post; danielebarchiesi@0: if ($upload) { danielebarchiesi@0: // TODO: cURL handles file uploads for us, but the implementation danielebarchiesi@0: // is broken. This is a less than elegant workaround. Alternatives danielebarchiesi@0: // are being explored at #253506. danielebarchiesi@0: foreach ($upload as $key => $file) { danielebarchiesi@0: $file = drupal_realpath($file); danielebarchiesi@0: if ($file && is_file($file)) { danielebarchiesi@0: $post[$key] = '@' . $file; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: foreach ($post as $key => $value) { danielebarchiesi@0: // Encode according to application/x-www-form-urlencoded danielebarchiesi@0: // Both names and values needs to be urlencoded, according to danielebarchiesi@0: // http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1 danielebarchiesi@0: $post[$key] = urlencode($key) . '=' . urlencode($value); danielebarchiesi@0: } danielebarchiesi@0: $post = implode('&', $post) . $extra_post; danielebarchiesi@0: } danielebarchiesi@0: $out = $this->curlExec(array(CURLOPT_URL => $action, CURLOPT_POST => TRUE, CURLOPT_POSTFIELDS => $post, CURLOPT_HTTPHEADER => $headers)); danielebarchiesi@0: // Ensure that any changes to variables in the other thread are picked up. danielebarchiesi@0: $this->refreshVariables(); danielebarchiesi@0: danielebarchiesi@0: // Replace original page output with new output from redirected page(s). danielebarchiesi@0: if ($new = $this->checkForMetaRefresh()) { danielebarchiesi@0: $out = $new; danielebarchiesi@0: } danielebarchiesi@0: $this->verbose('POST request to: ' . $path . danielebarchiesi@0: '
Ending URL: ' . $this->getUrl() . danielebarchiesi@0: '
Fields: ' . highlight_string('' . $out); danielebarchiesi@0: return $out; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: // We have not found a form which contained all fields of $edit. danielebarchiesi@0: foreach ($edit as $name => $value) { danielebarchiesi@0: $this->fail(t('Failed to set field @name to @value', array('@name' => $name, '@value' => $value))); danielebarchiesi@0: } danielebarchiesi@0: if (!$ajax && isset($submit)) { danielebarchiesi@0: $this->assertTrue($submit_matches, t('Found the @submit button', array('@submit' => $submit))); danielebarchiesi@0: } danielebarchiesi@0: $this->fail(t('Found the requested form fields at @path', array('@path' => $path))); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Execute an Ajax submission. danielebarchiesi@0: * danielebarchiesi@0: * This executes a POST as ajax.js does. It uses the returned JSON data, an danielebarchiesi@0: * array of commands, to update $this->content using equivalent DOM danielebarchiesi@0: * manipulation as is used by ajax.js. It also returns the array of commands. danielebarchiesi@0: * danielebarchiesi@0: * @param $path danielebarchiesi@0: * Location of the form containing the Ajax enabled element to test. Can be danielebarchiesi@0: * either a Drupal path or an absolute path or NULL to use the current page. danielebarchiesi@0: * @param $edit danielebarchiesi@0: * Field data in an associative array. Changes the current input fields danielebarchiesi@0: * (where possible) to the values indicated. danielebarchiesi@0: * @param $triggering_element danielebarchiesi@0: * The name of the form element that is responsible for triggering the Ajax danielebarchiesi@0: * functionality to test. May be a string or, if the triggering element is danielebarchiesi@0: * a button, an associative array where the key is the name of the button danielebarchiesi@0: * and the value is the button label. i.e.) array('op' => t('Refresh')). danielebarchiesi@0: * @param $ajax_path danielebarchiesi@0: * (optional) Override the path set by the Ajax settings of the triggering danielebarchiesi@0: * element. In the absence of both the triggering element's Ajax path and danielebarchiesi@0: * $ajax_path 'system/ajax' will be used. danielebarchiesi@0: * @param $options danielebarchiesi@0: * (optional) Options to be forwarded to url(). danielebarchiesi@0: * @param $headers danielebarchiesi@0: * (optional) An array containing additional HTTP request headers, each danielebarchiesi@0: * formatted as "name: value". Forwarded to drupalPost(). danielebarchiesi@0: * @param $form_html_id danielebarchiesi@0: * (optional) HTML ID of the form to be submitted, use when there is more danielebarchiesi@0: * than one identical form on the same page and the value of the triggering danielebarchiesi@0: * element is not enough to identify the form. Note this is not the Drupal danielebarchiesi@0: * ID of the form but rather the HTML ID of the form. danielebarchiesi@0: * @param $ajax_settings danielebarchiesi@0: * (optional) An array of Ajax settings which if specified will be used in danielebarchiesi@0: * place of the Ajax settings of the triggering element. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * An array of Ajax commands. danielebarchiesi@0: * danielebarchiesi@0: * @see drupalPost() danielebarchiesi@0: * @see ajax.js danielebarchiesi@0: */ danielebarchiesi@0: 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: // Get the content of the initial page prior to calling drupalPost(), since danielebarchiesi@0: // drupalPost() replaces $this->content. danielebarchiesi@0: if (isset($path)) { danielebarchiesi@0: $this->drupalGet($path, $options); danielebarchiesi@0: } danielebarchiesi@0: $content = $this->content; danielebarchiesi@0: $drupal_settings = $this->drupalSettings; danielebarchiesi@0: danielebarchiesi@0: // Get the Ajax settings bound to the triggering element. danielebarchiesi@0: if (!isset($ajax_settings)) { danielebarchiesi@0: if (is_array($triggering_element)) { danielebarchiesi@0: $xpath = '//*[@name="' . key($triggering_element) . '" and @value="' . current($triggering_element) . '"]'; danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: $xpath = '//*[@name="' . $triggering_element . '"]'; danielebarchiesi@0: } danielebarchiesi@0: if (isset($form_html_id)) { danielebarchiesi@0: $xpath = '//form[@id="' . $form_html_id . '"]' . $xpath; danielebarchiesi@0: } danielebarchiesi@0: $element = $this->xpath($xpath); danielebarchiesi@0: $element_id = (string) $element[0]['id']; danielebarchiesi@0: $ajax_settings = $drupal_settings['ajax'][$element_id]; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Add extra information to the POST data as ajax.js does. danielebarchiesi@0: $extra_post = ''; danielebarchiesi@0: if (isset($ajax_settings['submit'])) { danielebarchiesi@0: foreach ($ajax_settings['submit'] as $key => $value) { danielebarchiesi@0: $extra_post .= '&' . urlencode($key) . '=' . urlencode($value); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: foreach ($this->xpath('//*[@id]') as $element) { danielebarchiesi@0: $id = (string) $element['id']; danielebarchiesi@0: $extra_post .= '&' . urlencode('ajax_html_ids[]') . '=' . urlencode($id); danielebarchiesi@0: } danielebarchiesi@0: if (isset($drupal_settings['ajaxPageState'])) { danielebarchiesi@0: $extra_post .= '&' . urlencode('ajax_page_state[theme]') . '=' . urlencode($drupal_settings['ajaxPageState']['theme']); danielebarchiesi@0: $extra_post .= '&' . urlencode('ajax_page_state[theme_token]') . '=' . urlencode($drupal_settings['ajaxPageState']['theme_token']); danielebarchiesi@0: foreach ($drupal_settings['ajaxPageState']['css'] as $key => $value) { danielebarchiesi@0: $extra_post .= '&' . urlencode("ajax_page_state[css][$key]") . '=1'; danielebarchiesi@0: } danielebarchiesi@0: foreach ($drupal_settings['ajaxPageState']['js'] as $key => $value) { danielebarchiesi@0: $extra_post .= '&' . urlencode("ajax_page_state[js][$key]") . '=1'; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Unless a particular path is specified, use the one specified by the danielebarchiesi@0: // Ajax settings, or else 'system/ajax'. danielebarchiesi@0: if (!isset($ajax_path)) { danielebarchiesi@0: $ajax_path = isset($ajax_settings['url']) ? $ajax_settings['url'] : 'system/ajax'; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Submit the POST request. danielebarchiesi@0: $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: danielebarchiesi@0: // Change the page content by applying the returned commands. danielebarchiesi@0: if (!empty($ajax_settings) && !empty($return)) { danielebarchiesi@0: // ajax.js applies some defaults to the settings object, so do the same danielebarchiesi@0: // for what's used by this function. danielebarchiesi@0: $ajax_settings += array( danielebarchiesi@0: 'method' => 'replaceWith', danielebarchiesi@0: ); danielebarchiesi@0: // DOM can load HTML soup. But, HTML soup can throw warnings, suppress danielebarchiesi@0: // them. danielebarchiesi@0: $dom = new DOMDocument(); danielebarchiesi@0: @$dom->loadHTML($content); danielebarchiesi@0: // XPath allows for finding wrapper nodes better than DOM does. danielebarchiesi@0: $xpath = new DOMXPath($dom); danielebarchiesi@0: foreach ($return as $command) { danielebarchiesi@0: switch ($command['command']) { danielebarchiesi@0: case 'settings': danielebarchiesi@0: $drupal_settings = drupal_array_merge_deep($drupal_settings, $command['settings']); danielebarchiesi@0: break; danielebarchiesi@0: danielebarchiesi@0: case 'insert': danielebarchiesi@0: $wrapperNode = NULL; danielebarchiesi@0: // When a command doesn't specify a selector, use the danielebarchiesi@0: // #ajax['wrapper'] which is always an HTML ID. danielebarchiesi@0: if (!isset($command['selector'])) { danielebarchiesi@0: $wrapperNode = $xpath->query('//*[@id="' . $ajax_settings['wrapper'] . '"]')->item(0); danielebarchiesi@0: } danielebarchiesi@0: // @todo Ajax commands can target any jQuery selector, but these are danielebarchiesi@0: // hard to fully emulate with XPath. For now, just handle 'head' danielebarchiesi@0: // and 'body', since these are used by ajax_render(). danielebarchiesi@0: elseif (in_array($command['selector'], array('head', 'body'))) { danielebarchiesi@0: $wrapperNode = $xpath->query('//' . $command['selector'])->item(0); danielebarchiesi@0: } danielebarchiesi@0: if ($wrapperNode) { danielebarchiesi@0: // ajax.js adds an enclosing DIV to work around a Safari bug. danielebarchiesi@0: $newDom = new DOMDocument(); danielebarchiesi@0: $newDom->loadHTML('
' . $command['data'] . '
'); danielebarchiesi@0: $newNode = $dom->importNode($newDom->documentElement->firstChild->firstChild, TRUE); danielebarchiesi@0: $method = isset($command['method']) ? $command['method'] : $ajax_settings['method']; danielebarchiesi@0: // The "method" is a jQuery DOM manipulation function. Emulate danielebarchiesi@0: // each one using PHP's DOMNode API. danielebarchiesi@0: switch ($method) { danielebarchiesi@0: case 'replaceWith': danielebarchiesi@0: $wrapperNode->parentNode->replaceChild($newNode, $wrapperNode); danielebarchiesi@0: break; danielebarchiesi@0: case 'append': danielebarchiesi@0: $wrapperNode->appendChild($newNode); danielebarchiesi@0: break; danielebarchiesi@0: case 'prepend': danielebarchiesi@0: // If no firstChild, insertBefore() falls back to danielebarchiesi@0: // appendChild(). danielebarchiesi@0: $wrapperNode->insertBefore($newNode, $wrapperNode->firstChild); danielebarchiesi@0: break; danielebarchiesi@0: case 'before': danielebarchiesi@0: $wrapperNode->parentNode->insertBefore($newNode, $wrapperNode); danielebarchiesi@0: break; danielebarchiesi@0: case 'after': danielebarchiesi@0: // If no nextSibling, insertBefore() falls back to danielebarchiesi@0: // appendChild(). danielebarchiesi@0: $wrapperNode->parentNode->insertBefore($newNode, $wrapperNode->nextSibling); danielebarchiesi@0: break; danielebarchiesi@0: case 'html': danielebarchiesi@0: foreach ($wrapperNode->childNodes as $childNode) { danielebarchiesi@0: $wrapperNode->removeChild($childNode); danielebarchiesi@0: } danielebarchiesi@0: $wrapperNode->appendChild($newNode); danielebarchiesi@0: break; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: break; danielebarchiesi@0: danielebarchiesi@0: // @todo Add suitable implementations for these commands in order to danielebarchiesi@0: // have full test coverage of what ajax.js can do. danielebarchiesi@0: case 'remove': danielebarchiesi@0: break; danielebarchiesi@0: case 'changed': danielebarchiesi@0: break; danielebarchiesi@0: case 'css': danielebarchiesi@0: break; danielebarchiesi@0: case 'data': danielebarchiesi@0: break; danielebarchiesi@0: case 'restripe': danielebarchiesi@0: break; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: $content = $dom->saveHTML(); danielebarchiesi@0: } danielebarchiesi@0: $this->drupalSetContent($content); danielebarchiesi@0: $this->drupalSetSettings($drupal_settings); danielebarchiesi@0: return $return; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Runs cron in the Drupal installed by Simpletest. danielebarchiesi@0: */ danielebarchiesi@0: protected function cronRun() { danielebarchiesi@0: $this->drupalGet($GLOBALS['base_url'] . '/cron.php', array('external' => TRUE, 'query' => array('cron_key' => variable_get('cron_key', 'drupal')))); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Check for meta refresh tag and if found call drupalGet() recursively. This danielebarchiesi@0: * function looks for the http-equiv attribute to be set to "Refresh" danielebarchiesi@0: * and is case-sensitive. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * Either the new page content or FALSE. danielebarchiesi@0: */ danielebarchiesi@0: protected function checkForMetaRefresh() { danielebarchiesi@0: if (strpos($this->drupalGetContent(), 'parse()) { danielebarchiesi@0: $refresh = $this->xpath('//meta[@http-equiv="Refresh"]'); danielebarchiesi@0: if (!empty($refresh)) { danielebarchiesi@0: // Parse the content attribute of the meta tag for the format: danielebarchiesi@0: // "[delay]: URL=[page_to_redirect_to]". danielebarchiesi@0: if (preg_match('/\d+;\s*URL=(?P.*)/i', $refresh[0]['content'], $match)) { danielebarchiesi@0: return $this->drupalGet($this->getAbsoluteUrl(decode_entities($match['url']))); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: return FALSE; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Retrieves only the headers for a Drupal path or an absolute path. danielebarchiesi@0: * danielebarchiesi@0: * @param $path danielebarchiesi@0: * Drupal path or URL to load into internal browser danielebarchiesi@0: * @param $options danielebarchiesi@0: * Options to be forwarded to url(). danielebarchiesi@0: * @param $headers danielebarchiesi@0: * An array containing additional HTTP request headers, each formatted as danielebarchiesi@0: * "name: value". danielebarchiesi@0: * @return danielebarchiesi@0: * The retrieved headers, also available as $this->drupalGetContent() danielebarchiesi@0: */ danielebarchiesi@0: protected function drupalHead($path, array $options = array(), array $headers = array()) { danielebarchiesi@0: $options['absolute'] = TRUE; danielebarchiesi@0: $out = $this->curlExec(array(CURLOPT_NOBODY => TRUE, CURLOPT_URL => url($path, $options), CURLOPT_HTTPHEADER => $headers)); danielebarchiesi@0: $this->refreshVariables(); // Ensure that any changes to variables in the other thread are picked up. danielebarchiesi@0: return $out; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Handle form input related to drupalPost(). Ensure that the specified fields danielebarchiesi@0: * exist and attempt to create POST data in the correct manner for the particular danielebarchiesi@0: * field type. danielebarchiesi@0: * danielebarchiesi@0: * @param $post danielebarchiesi@0: * Reference to array of post values. danielebarchiesi@0: * @param $edit danielebarchiesi@0: * Reference to array of edit values to be checked against the form. danielebarchiesi@0: * @param $submit danielebarchiesi@0: * Form submit button value. danielebarchiesi@0: * @param $form danielebarchiesi@0: * Array of form elements. danielebarchiesi@0: * @return danielebarchiesi@0: * Submit value matches a valid submit input in the form. danielebarchiesi@0: */ danielebarchiesi@0: protected function handleForm(&$post, &$edit, &$upload, $submit, $form) { danielebarchiesi@0: // Retrieve the form elements. danielebarchiesi@0: $elements = $form->xpath('.//input[not(@disabled)]|.//textarea[not(@disabled)]|.//select[not(@disabled)]'); danielebarchiesi@0: $submit_matches = FALSE; danielebarchiesi@0: foreach ($elements as $element) { danielebarchiesi@0: // SimpleXML objects need string casting all the time. danielebarchiesi@0: $name = (string) $element['name']; danielebarchiesi@0: // This can either be the type of or the name of the tag itself danielebarchiesi@0: // for