abstract class WebTestBase extends TestBase {
protected $profile = 'testing';
protected $url;
protected $curlHandle;
protected $headers;
protected $content;
protected $plainTextContent;
protected $drupalSettings;
protected $elements = NULL;
protected $loggedInUser = FALSE;
protected $cookieFile = NULL;
protected $additionalCurlOptions = array();
protected $originalUser = NULL;
protected $originalShutdownCallbacks = array();
protected $httpauth_method = CURLAUTH_BASIC;
protected $httpauth_credentials = NULL;
protected $session_name = NULL;
protected $session_id = NULL;
protected $generatedTestFiles = FALSE;
protected $maximumRedirects = 5;
protected $redirect_count;
protected $kernel;
function __construct($test_id = NULL) {
parent::__construct($test_id);
$this->skipClasses[__CLASS__] = TRUE;
}
function drupalGetNodeByTitle($title, $reset = FALSE) {
if ($reset) {
entity_get_controller('node')
->resetCache();
}
$nodes = entity_load_multiple_by_properties('node', array(
'title' => $title,
));
$returned_node = reset($nodes);
return $returned_node;
}
protected function drupalCreateNode(array $settings = array()) {
$settings += array(
'body' => array(
LANGUAGE_NOT_SPECIFIED => array(
array(),
),
),
'title' => $this
->randomName(8),
'changed' => REQUEST_TIME,
'promote' => NODE_NOT_PROMOTED,
'revision' => 1,
'log' => '',
'status' => NODE_PUBLISHED,
'sticky' => NODE_NOT_STICKY,
'type' => 'page',
'langcode' => LANGUAGE_NOT_SPECIFIED,
);
if (module_exists('comment')) {
$settings += array(
'comment' => COMMENT_NODE_OPEN,
);
}
if (isset($settings['created']) && !isset($settings['date'])) {
$settings['date'] = format_date($settings['created'], 'custom', 'Y-m-d H:i:s O');
}
if (!isset($settings['uid'])) {
if ($this->loggedInUser) {
$settings['uid'] = $this->loggedInUser->uid;
}
else {
global $user;
$settings['uid'] = $user->uid;
}
}
$body = array(
'value' => $this
->randomName(32),
'format' => filter_default_format(),
);
if (empty($settings['body'][$settings['langcode']])) {
$settings['body'][$settings['langcode']][0] = array();
}
$settings['body'][$settings['langcode']][0] += $body;
$node = entity_create('node', $settings);
if (!empty($settings['revision'])) {
$node
->setNewRevision();
}
$node
->save();
db_update('node_revision')
->fields(array(
'uid' => $node->uid,
))
->condition('vid', $node->vid)
->execute();
return $node;
}
protected function drupalCreateContentType($settings = array()) {
do {
$name = strtolower($this
->randomName(8));
} while (node_type_load($name));
$defaults = array(
'type' => $name,
'name' => $name,
'base' => 'node_content',
'description' => '',
'help' => '',
'title_label' => 'Title',
'body_label' => 'Body',
'has_title' => 1,
'has_body' => 1,
);
$forced = array(
'orig_type' => '',
'old_type' => '',
'module' => 'node',
'custom' => 1,
'modified' => 1,
'locked' => 0,
);
$type = $forced + $settings + $defaults;
$type = (object) $type;
$saved_type = node_type_save($type);
node_types_rebuild();
menu_router_rebuild();
node_add_body_field($type);
$this
->assertEqual($saved_type, SAVED_NEW, t('Created content type %type.', array(
'%type' => $type->type,
)));
$this
->checkPermissions(array(), TRUE);
return $type;
}
protected function drupalGetTestFiles($type, $size = NULL) {
if (empty($this->generatedTestFiles)) {
$lines = array(
64,
1024,
);
$count = 0;
foreach ($lines as $line) {
simpletest_generate_file('binary-' . $count++, 64, $line, 'binary');
}
$lines = array(
16,
256,
1024,
2048,
20480,
);
$count = 0;
foreach ($lines as $line) {
simpletest_generate_file('text-' . $count++, 64, $line);
}
$original = drupal_get_path('module', 'simpletest') . '/files';
$files = file_scan_directory($original, '/(html|image|javascript|php|sql)-.*/');
foreach ($files as $file) {
file_unmanaged_copy($file->uri, variable_get('file_public_path', conf_path() . '/files'));
}
$this->generatedTestFiles = TRUE;
}
$files = array();
if (in_array($type, array(
'binary',
'html',
'image',
'javascript',
'php',
'sql',
'text',
))) {
$files = file_scan_directory('public://', '/' . $type . '\\-.*/');
if ($size !== NULL) {
foreach ($files as $file) {
$stats = stat($file->uri);
if ($stats['size'] != $size) {
unset($files[$file->uri]);
}
}
}
}
usort($files, array(
$this,
'drupalCompareFiles',
));
return $files;
}
protected function drupalCompareFiles($file1, $file2) {
$compare_size = filesize($file1->uri) - filesize($file2->uri);
if ($compare_size) {
return $compare_size;
}
else {
return strnatcmp($file1->name, $file2->name);
}
}
protected function drupalCreateUser(array $permissions = array()) {
$rid = FALSE;
if ($permissions) {
$rid = $this
->drupalCreateRole($permissions);
if (!$rid) {
return FALSE;
}
}
$edit = array();
$edit['name'] = $this
->randomName();
$edit['mail'] = $edit['name'] . '@example.com';
$edit['pass'] = user_password();
$edit['status'] = 1;
if ($rid) {
$edit['roles'] = array(
$rid => $rid,
);
}
$account = entity_create('user', $edit);
$account
->save();
$this
->assertTrue(!empty($account->uid), t('User created with name %name and pass %pass', array(
'%name' => $edit['name'],
'%pass' => $edit['pass'],
)), t('User login'));
if (empty($account->uid)) {
return FALSE;
}
$account->pass_raw = $edit['pass'];
return $account;
}
protected function drupalCreateRole(array $permissions, $rid = NULL, $name = NULL) {
if (!isset($rid)) {
$rid = strtolower($this
->randomName(8));
}
if (!isset($name)) {
$name = $this
->randomString(8);
}
if (!$this
->checkPermissions($permissions)) {
return FALSE;
}
$role = new stdClass();
$role->rid = $rid;
$role->name = $name;
$result = user_role_save($role);
$this
->assertIdentical($result, SAVED_NEW, t('Created role ID @rid with name @name.', array(
'@name' => var_export($role->name, TRUE),
'@rid' => var_export($role->rid, TRUE),
)), t('Role'));
if ($result === SAVED_NEW) {
if (!empty($permissions)) {
user_role_grant_permissions($role->rid, $permissions);
$assigned_permissions = db_query('SELECT permission FROM {role_permission} WHERE rid = :rid', array(
':rid' => $role->rid,
))
->fetchCol();
$missing_permissions = array_diff($permissions, $assigned_permissions);
if (!$missing_permissions) {
$this
->pass(t('Created permissions: @perms', array(
'@perms' => implode(', ', $permissions),
)), t('Role'));
}
else {
$this
->fail(t('Failed to create permissions: @perms', array(
'@perms' => implode(', ', $missing_permissions),
)), t('Role'));
}
}
return $role->rid;
}
else {
return FALSE;
}
}
protected function checkPermissions(array $permissions, $reset = FALSE) {
$available =& drupal_static(__FUNCTION__);
if (!isset($available) || $reset) {
$available = array_keys(module_invoke_all('permission'));
}
$valid = TRUE;
foreach ($permissions as $permission) {
if (!in_array($permission, $available)) {
$this
->fail(t('Invalid permission %permission.', array(
'%permission' => $permission,
)), t('Role'));
$valid = FALSE;
}
}
return $valid;
}
protected function drupalLogin($user) {
if ($this->loggedInUser) {
$this
->drupalLogout();
}
$edit = array(
'name' => $user->name,
'pass' => $user->pass_raw,
);
$this
->drupalPost('user', $edit, t('Log in'));
$pass = $this
->assertLink(t('Log out'), 0, t('User %name successfully logged in.', array(
'%name' => $user->name,
)), t('User login'));
if ($pass) {
$this->loggedInUser = $user;
}
}
protected function drupalGetToken($value = '') {
$private_key = drupal_get_private_key();
return drupal_hmac_base64($value, $this->session_id . $private_key);
}
protected function drupalLogout() {
$this
->drupalGet('user/logout', array(
'query' => array(
'destination' => 'user',
),
));
$this
->assertResponse(200, t('User was logged out.'));
$pass = $this
->assertField('name', t('Username field found.'), t('Logout'));
$pass = $pass && $this
->assertField('pass', t('Password field found.'), t('Logout'));
if ($pass) {
$this->loggedInUser = FALSE;
}
}
protected function setUp() {
global $user, $conf;
$this->originalBatch = batch_get();
$this
->prepareDatabasePrefix();
$this
->prepareEnvironment();
if (!$this->setupEnvironment) {
return FALSE;
}
$conf = array();
drupal_static_reset();
$this
->changeDatabasePrefix();
if (!$this->setupDatabasePrefix) {
return FALSE;
}
$conf['simpletest_parent_profile'] = $this->originalProfile;
$connection_info = Database::getConnectionInfo();
$this->root_user = (object) array(
'name' => 'admin',
'mail' => 'admin@example.com',
'pass_raw' => $this
->randomName(),
);
$settings = array(
'interactive' => FALSE,
'parameters' => array(
'profile' => $this->profile,
'langcode' => 'en',
),
'forms' => array(
'install_settings_form' => $connection_info['default'],
'install_configure_form' => array(
'site_name' => 'Drupal',
'site_mail' => 'simpletest@example.com',
'account' => array(
'name' => $this->root_user->name,
'mail' => $this->root_user->mail,
'pass' => array(
'pass1' => $this->root_user->pass_raw,
'pass2' => $this->root_user->pass_raw,
),
),
'update_status_module' => array(
1 => NULL,
2 => NULL,
),
),
),
);
$user = drupal_anonymous_user();
$batch =& batch_get();
$batch = array();
$variables = array(
'file_public_path' => $this->public_files_directory,
'file_private_path' => $this->private_files_directory,
'file_temporary_path' => $this->temp_files_directory,
'locale_translate_file_directory' => $this->translation_files_directory,
);
foreach ($variables as $name => $value) {
$GLOBALS['conf'][$name] = $value;
}
require_once DRUPAL_ROOT . '/core/includes/install.core.inc';
install_drupal($settings);
$this
->rebuildContainer();
foreach ($variables as $name => $value) {
variable_set($name, $value);
}
$batch =& batch_get();
$batch = $this->originalBatch;
unset($conf['cache_classes']);
unset($conf['lock_backend']);
config('simpletest.settings')
->set('parent_profile', $this->originalProfile)
->save();
$class = get_class($this);
$modules = array();
while ($class) {
if (property_exists($class, 'modules')) {
$modules = array_merge($modules, $class::$modules);
}
$class = get_parent_class($class);
}
if ($modules) {
$success = module_enable($modules, TRUE);
$this
->assertTrue($success, t('Enabled modules: %modules', array(
'%modules' => implode(', ', $modules),
)));
$this
->rebuildContainer();
}
$this
->resetAll();
variable_set('mail_system', array(
'default-system' => 'Drupal\\Core\\Mail\\VariableLog',
));
drupal_set_time_limit($this->timeLimit);
$path = current_path();
if (empty($path)) {
_current_path('run-tests');
}
$this->setup = TRUE;
}
protected function resetAll() {
drupal_flush_all_caches();
$this
->refreshVariables();
$this
->checkPermissions(array(), TRUE);
}
protected function refreshVariables() {
global $conf;
cache('bootstrap')
->delete('variables');
$conf = variable_initialize();
}
protected function tearDown() {
if (isset($this->kernel)) {
$this->kernel
->shutdown();
}
parent::tearDown();
$this->loggedInUser = FALSE;
$this->additionalCurlOptions = array();
system_list_reset();
module_list_reset();
module_implements_reset();
field_cache_clear();
$this
->refreshVariables();
$this
->curlClose();
}
protected function curlInitialize() {
global $base_url;
if (!isset($this->curlHandle)) {
$this->curlHandle = curl_init();
if (empty($this->cookieFile)) {
$this->cookieFile = $this->public_files_directory . '/cookie.jar';
}
$curl_options = array(
CURLOPT_COOKIEJAR => $this->cookieFile,
CURLOPT_URL => $base_url,
CURLOPT_FOLLOWLOCATION => FALSE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_SSL_VERIFYHOST => FALSE,
CURLOPT_HEADERFUNCTION => array(
&$this,
'curlHeaderCallback',
),
CURLOPT_USERAGENT => $this->databasePrefix,
);
if (isset($this->httpauth_credentials)) {
$curl_options[CURLOPT_HTTPAUTH] = $this->httpauth_method;
$curl_options[CURLOPT_USERPWD] = $this->httpauth_credentials;
}
$result = curl_setopt_array($this->curlHandle, $this->additionalCurlOptions + $curl_options);
if (!$result) {
throw new \UnexpectedValueException('One or more cURL options could not be set.');
}
$this->session_name = session_name();
}
if (preg_match('/simpletest\\d+/', $this->databasePrefix, $matches)) {
curl_setopt($this->curlHandle, CURLOPT_USERAGENT, drupal_generate_test_ua($matches[0]));
}
}
protected function curlExec($curl_options, $redirect = FALSE) {
$this
->curlInitialize();
if (!empty($curl_options[CURLOPT_URL]) && strpos($curl_options[CURLOPT_URL], '#')) {
$original_url = $curl_options[CURLOPT_URL];
$curl_options[CURLOPT_URL] = strtok($curl_options[CURLOPT_URL], '#');
}
$url = empty($curl_options[CURLOPT_URL]) ? curl_getinfo($this->curlHandle, CURLINFO_EFFECTIVE_URL) : $curl_options[CURLOPT_URL];
if (!empty($curl_options[CURLOPT_POST])) {
$curl_options[CURLOPT_HTTPHEADER][] = 'Expect:';
}
curl_setopt_array($this->curlHandle, $this->additionalCurlOptions + $curl_options);
if (!$redirect) {
$this->session_id = NULL;
$this->headers = array();
$this->redirect_count = 0;
}
$content = curl_exec($this->curlHandle);
$status = curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE);
if (in_array($status, array(
300,
301,
302,
303,
305,
307,
)) && $this->redirect_count < $this->maximumRedirects) {
if ($this
->drupalGetHeader('location')) {
$this->redirect_count++;
$curl_options = array();
$curl_options[CURLOPT_URL] = $this
->drupalGetHeader('location');
$curl_options[CURLOPT_HTTPGET] = TRUE;
return $this
->curlExec($curl_options, TRUE);
}
}
$this
->drupalSetContent($content, isset($original_url) ? $original_url : curl_getinfo($this->curlHandle, CURLINFO_EFFECTIVE_URL));
$message_vars = array(
'!method' => !empty($curl_options[CURLOPT_NOBODY]) ? 'HEAD' : (empty($curl_options[CURLOPT_POSTFIELDS]) ? 'GET' : 'POST'),
'@url' => isset($original_url) ? $original_url : $url,
'@status' => $status,
'!length' => format_size(strlen($this
->drupalGetContent())),
);
$message = t('!method @url returned @status (!length).', $message_vars);
$this
->assertTrue($this
->drupalGetContent() !== FALSE, $message, t('Browser'));
return $this
->drupalGetContent();
}
protected function curlHeaderCallback($curlHandler, $header) {
if ($header[0] == ' ' || $header[0] == "\t") {
$this->headers[] = array_pop($this->headers) . ' ' . trim($header);
}
else {
$this->headers[] = $header;
}
if (preg_match('/^X-Drupal-Assertion-[0-9]+: (.*)$/', $header, $matches)) {
call_user_func_array(array(
&$this,
'error',
), unserialize(urldecode($matches[1])));
}
if (preg_match('/^Set-Cookie: ([^=]+)=(.+)/', $header, $matches)) {
$name = $matches[1];
$parts = array_map('trim', explode(';', $matches[2]));
$value = array_shift($parts);
$this->cookies[$name] = array(
'value' => $value,
'secure' => in_array('secure', $parts),
);
if ($name == $this->session_name) {
if ($value != 'deleted') {
$this->session_id = $value;
}
else {
$this->session_id = NULL;
}
}
}
return strlen($header);
}
protected function curlClose() {
if (isset($this->curlHandle)) {
curl_close($this->curlHandle);
unset($this->curlHandle);
}
}
protected function parse() {
if (!$this->elements) {
$htmlDom = new DOMDocument();
@$htmlDom
->loadHTML('<?xml encoding="UTF-8">' . $this
->drupalGetContent());
if ($htmlDom) {
$this
->pass(t('Valid HTML found on "@path"', array(
'@path' => $this
->getUrl(),
)), t('Browser'));
$this->elements = simplexml_import_dom($htmlDom);
}
}
if (!$this->elements) {
$this
->fail(t('Parsed page successfully.'), t('Browser'));
}
return $this->elements;
}
protected function drupalGet($path, array $options = array(), array $headers = array()) {
$options['absolute'] = TRUE;
$out = $this
->curlExec(array(
CURLOPT_HTTPGET => TRUE,
CURLOPT_URL => url($path, $options),
CURLOPT_NOBODY => FALSE,
CURLOPT_HTTPHEADER => $headers,
));
$this
->refreshVariables();
if ($new = $this
->checkForMetaRefresh()) {
$out = $new;
}
$this
->verbose('GET request to: ' . $path . '<hr />Ending URL: ' . $this
->getUrl() . '<hr />' . $out);
return $out;
}
protected function drupalGetAJAX($path, array $options = array(), array $headers = array()) {
$headers[] = 'X-Requested-With: XMLHttpRequest';
return drupal_json_decode($this
->drupalGet($path, $options, $headers));
}
protected function drupalPost($path, $edit, $submit, array $options = array(), array $headers = array(), $form_html_id = NULL, $extra_post = NULL) {
$submit_matches = FALSE;
$ajax = is_array($submit);
if (isset($path)) {
$this
->drupalGet($path, $options);
}
if ($this
->parse()) {
$edit_save = $edit;
$xpath = "//form";
if (!empty($form_html_id)) {
$xpath .= "[@id='" . $form_html_id . "']";
}
$forms = $this
->xpath($xpath);
foreach ($forms as $form) {
$edit = $edit_save;
$post = array();
$upload = array();
$submit_matches = $this
->handleForm($post, $edit, $upload, $ajax ? NULL : $submit, $form);
$action = isset($form['action']) ? $this
->getAbsoluteUrl((string) $form['action']) : $this
->getUrl();
if ($ajax) {
$action = $this
->getAbsoluteUrl(!empty($submit['path']) ? $submit['path'] : 'system/ajax');
$submit_matches = TRUE;
}
if (!$edit && ($submit_matches || !isset($submit))) {
$post_array = $post;
if ($upload) {
foreach ($upload as $key => $file) {
$file = drupal_realpath($file);
if ($file && is_file($file)) {
$post[$key] = '@' . $file;
}
}
}
else {
foreach ($post as $key => $value) {
$post[$key] = urlencode($key) . '=' . urlencode($value);
}
$post = implode('&', $post) . $extra_post;
}
$out = $this
->curlExec(array(
CURLOPT_URL => $action,
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => $post,
CURLOPT_HTTPHEADER => $headers,
));
$this
->refreshVariables();
if ($new = $this
->checkForMetaRefresh()) {
$out = $new;
}
$this
->verbose('POST request to: ' . $path . '<hr />Ending URL: ' . $this
->getUrl() . '<hr />Fields: ' . highlight_string('<?php ' . var_export($post_array, TRUE), TRUE) . '<hr />' . $out);
return $out;
}
}
foreach ($edit as $name => $value) {
$this
->fail(t('Failed to set field @name to @value', array(
'@name' => $name,
'@value' => $value,
)));
}
if (!$ajax && isset($submit)) {
$this
->assertTrue($submit_matches, t('Found the @submit button', array(
'@submit' => $submit,
)));
}
$this
->fail(t('Found the requested form fields at @path', array(
'@path' => $path,
)));
}
}
protected function drupalPostAJAX($path, $edit, $triggering_element, $ajax_path = NULL, array $options = array(), array $headers = array(), $form_html_id = NULL, $ajax_settings = NULL) {
if (isset($path)) {
$this
->drupalGet($path, $options);
}
$content = $this->content;
$drupal_settings = $this->drupalSettings;
$headers[] = 'X-Requested-With: XMLHttpRequest';
if (!isset($ajax_settings)) {
if (is_array($triggering_element)) {
$xpath = '//*[@name="' . key($triggering_element) . '" and @value="' . current($triggering_element) . '"]';
}
else {
$xpath = '//*[@name="' . $triggering_element . '"]';
}
if (isset($form_html_id)) {
$xpath = '//form[@id="' . $form_html_id . '"]' . $xpath;
}
$element = $this
->xpath($xpath);
$element_id = (string) $element[0]['id'];
$ajax_settings = $drupal_settings['ajax'][$element_id];
}
$extra_post = '';
if (isset($ajax_settings['submit'])) {
foreach ($ajax_settings['submit'] as $key => $value) {
$extra_post .= '&' . urlencode($key) . '=' . urlencode($value);
}
}
$ajax_html_ids = array();
foreach ($this
->xpath('//*[@id]') as $element) {
$ajax_html_ids[] = (string) $element['id'];
}
if (!empty($ajax_html_ids)) {
$extra_post .= '&' . urlencode('ajax_html_ids') . '=' . urlencode(implode(' ', $ajax_html_ids));
}
if (isset($drupal_settings['ajaxPageState'])) {
$extra_post .= '&' . urlencode('ajax_page_state[theme]') . '=' . urlencode($drupal_settings['ajaxPageState']['theme']);
$extra_post .= '&' . urlencode('ajax_page_state[theme_token]') . '=' . urlencode($drupal_settings['ajaxPageState']['theme_token']);
foreach ($drupal_settings['ajaxPageState']['css'] as $key => $value) {
$extra_post .= '&' . urlencode("ajax_page_state[css][{$key}]") . '=1';
}
foreach ($drupal_settings['ajaxPageState']['js'] as $key => $value) {
$extra_post .= '&' . urlencode("ajax_page_state[js][{$key}]") . '=1';
}
}
if (!isset($ajax_path)) {
$ajax_path = isset($ajax_settings['url']) ? $ajax_settings['url'] : 'system/ajax';
}
$return = drupal_json_decode($this
->drupalPost(NULL, $edit, array(
'path' => $ajax_path,
'triggering_element' => $triggering_element,
), $options, $headers, $form_html_id, $extra_post));
if (!empty($ajax_settings) && !empty($return)) {
$ajax_settings += array(
'method' => 'replaceWith',
);
$dom = new DOMDocument();
@$dom
->loadHTML($content);
$xpath = new DOMXPath($dom);
foreach ($return as $command) {
switch ($command['command']) {
case 'settings':
$drupal_settings = drupal_array_merge_deep($drupal_settings, $command['settings']);
break;
case 'insert':
$wrapperNode = NULL;
if (!isset($command['selector'])) {
$wrapperNode = $xpath
->query('//*[@id="' . $ajax_settings['wrapper'] . '"]')
->item(0);
}
elseif (in_array($command['selector'], array(
'head',
'body',
))) {
$wrapperNode = $xpath
->query('//' . $command['selector'])
->item(0);
}
if ($wrapperNode) {
$newDom = new DOMDocument();
@$newDom
->loadHTML('<div>' . $command['data'] . '</div>');
$newNode = $dom
->importNode($newDom->documentElement->firstChild->firstChild, TRUE);
$method = isset($command['method']) ? $command['method'] : $ajax_settings['method'];
switch ($method) {
case 'replaceWith':
$wrapperNode->parentNode
->replaceChild($newNode, $wrapperNode);
break;
case 'append':
$wrapperNode
->appendChild($newNode);
break;
case 'prepend':
$wrapperNode
->insertBefore($newNode, $wrapperNode->firstChild);
break;
case 'before':
$wrapperNode->parentNode
->insertBefore($newNode, $wrapperNode);
break;
case 'after':
$wrapperNode->parentNode
->insertBefore($newNode, $wrapperNode->nextSibling);
break;
case 'html':
foreach ($wrapperNode->childNodes as $childNode) {
$wrapperNode
->removeChild($childNode);
}
$wrapperNode
->appendChild($newNode);
break;
}
}
break;
case 'remove':
break;
case 'changed':
break;
case 'css':
break;
case 'data':
break;
case 'restripe':
break;
case 'add_css':
break;
}
}
$content = $dom
->saveHTML();
}
$this
->drupalSetContent($content);
$this
->drupalSetSettings($drupal_settings);
return $return;
}
protected function cronRun() {
$this
->drupalGet('cron/' . state()
->get('system.cron_key'));
}
protected function checkForMetaRefresh() {
if (strpos($this
->drupalGetContent(), '<meta ') && $this
->parse()) {
$refresh = $this
->xpath('//meta[@http-equiv="Refresh"]');
if (!empty($refresh)) {
if (preg_match('/\\d+;\\s*URL=(?P<url>.*)/i', $refresh[0]['content'], $match)) {
return $this
->drupalGet($this
->getAbsoluteUrl(decode_entities($match['url'])));
}
}
}
return FALSE;
}
protected function drupalHead($path, array $options = array(), array $headers = array()) {
$options['absolute'] = TRUE;
$out = $this
->curlExec(array(
CURLOPT_NOBODY => TRUE,
CURLOPT_URL => url($path, $options),
CURLOPT_HTTPHEADER => $headers,
));
$this
->refreshVariables();
return $out;
}
protected function handleForm(&$post, &$edit, &$upload, $submit, $form) {
$elements = $form
->xpath('.//input[not(@disabled)]|.//textarea[not(@disabled)]|.//select[not(@disabled)]');
$submit_matches = FALSE;
foreach ($elements as $element) {
$name = (string) $element['name'];
$type = isset($element['type']) ? (string) $element['type'] : $element
->getName();
$value = isset($element['value']) ? (string) $element['value'] : '';
$done = FALSE;
if (isset($edit[$name])) {
switch ($type) {
case 'text':
case 'tel':
case 'textarea':
case 'url':
case 'number':
case 'range':
case 'color':
case 'hidden':
case 'password':
case 'email':
case 'search':
$post[$name] = $edit[$name];
unset($edit[$name]);
break;
case 'radio':
if ($edit[$name] == $value) {
$post[$name] = $edit[$name];
unset($edit[$name]);
}
break;
case 'checkbox':
if ($edit[$name] === FALSE) {
unset($edit[$name]);
continue 2;
}
else {
unset($edit[$name]);
$post[$name] = $value;
}
break;
case 'select':
$new_value = $edit[$name];
$options = $this
->getAllOptions($element);
if (is_array($new_value)) {
if (!empty($new_value)) {
$index = 0;
$key = preg_replace('/\\[\\]$/', '', $name);
foreach ($options as $option) {
$option_value = (string) $option['value'];
if (in_array($option_value, $new_value)) {
$post[$key . '[' . $index++ . ']'] = $option_value;
$done = TRUE;
unset($edit[$name]);
}
}
}
else {
$done = TRUE;
unset($edit[$name]);
}
}
else {
foreach ($options as $option) {
if ($new_value == $option['value']) {
$post[$name] = $new_value;
unset($edit[$name]);
$done = TRUE;
break;
}
}
}
break;
case 'file':
$upload[$name] = $edit[$name];
unset($edit[$name]);
break;
}
}
if (!isset($post[$name]) && !$done) {
switch ($type) {
case 'textarea':
$post[$name] = (string) $element;
break;
case 'select':
$single = empty($element['multiple']);
$first = TRUE;
$index = 0;
$key = preg_replace('/\\[\\]$/', '', $name);
$options = $this
->getAllOptions($element);
foreach ($options as $option) {
if ($option['selected'] || $first && $single) {
$first = FALSE;
if ($single) {
$post[$name] = (string) $option['value'];
}
else {
$post[$key . '[' . $index++ . ']'] = (string) $option['value'];
}
}
}
break;
case 'file':
break;
case 'submit':
case 'image':
if (isset($submit) && $submit == $value) {
$post[$name] = $value;
$submit_matches = TRUE;
}
break;
case 'radio':
case 'checkbox':
if (!isset($element['checked'])) {
break;
}
default:
$post[$name] = $value;
}
}
}
return $submit_matches;
}
protected function buildXPathQuery($xpath, array $args = array()) {
foreach ($args as $placeholder => $value) {
if (is_string($value)) {
$parts = explode('"', $value);
foreach ($parts as &$part) {
$part = '"' . $part . '"';
}
$value = count($parts) > 1 ? 'concat(' . implode(', \'"\', ', $parts) . ')' : $parts[0];
}
$xpath = preg_replace('/' . preg_quote($placeholder) . '\\b/', $value, $xpath);
}
return $xpath;
}
protected function xpath($xpath, array $arguments = array()) {
if ($this
->parse()) {
$xpath = $this
->buildXPathQuery($xpath, $arguments);
$result = $this->elements
->xpath($xpath);
return $result ? $result : array();
}
else {
return FALSE;
}
}
protected function getAllOptions(SimpleXMLElement $element) {
$options = array();
foreach ($element->option as $option) {
$options[] = $option;
}
if (isset($element->optgroup)) {
foreach ($element->optgroup as $group) {
$options = array_merge($options, $this
->getAllOptions($group));
}
}
return $options;
}
protected function assertLink($label, $index = 0, $message = '', $group = 'Other') {
$links = $this
->xpath('//a[normalize-space(text())=:label]', array(
':label' => $label,
));
$message = $message ? $message : t('Link with label %label found.', array(
'%label' => $label,
));
return $this
->assert(isset($links[$index]), $message, $group);
}
protected function assertNoLink($label, $message = '', $group = 'Other') {
$links = $this
->xpath('//a[normalize-space(text())=:label]', array(
':label' => $label,
));
$message = $message ? $message : t('Link with label %label not found.', array(
'%label' => $label,
));
return $this
->assert(empty($links), $message, $group);
}
protected function assertLinkByHref($href, $index = 0, $message = '', $group = 'Other') {
$links = $this
->xpath('//a[contains(@href, :href)]', array(
':href' => $href,
));
$message = $message ? $message : t('Link containing href %href found.', array(
'%href' => $href,
));
return $this
->assert(isset($links[$index]), $message, $group);
}
protected function assertNoLinkByHref($href, $message = '', $group = 'Other') {
$links = $this
->xpath('//a[contains(@href, :href)]', array(
':href' => $href,
));
$message = $message ? $message : t('No link containing href %href found.', array(
'%href' => $href,
));
return $this
->assert(empty($links), $message, $group);
}
protected function clickLink($label, $index = 0) {
$url_before = $this
->getUrl();
$urls = $this
->xpath('//a[normalize-space(text())=:label]', array(
':label' => $label,
));
if (isset($urls[$index])) {
$url_target = $this
->getAbsoluteUrl($urls[$index]['href']);
}
$this
->assertTrue(isset($urls[$index]), t('Clicked link %label (@url_target) from @url_before', array(
'%label' => $label,
'@url_target' => $url_target,
'@url_before' => $url_before,
)), t('Browser'));
if (isset($url_target)) {
return $this
->drupalGet($url_target);
}
return FALSE;
}
protected function getAbsoluteUrl($path) {
global $base_url, $base_path;
$parts = parse_url($path);
if (empty($parts['host'])) {
$path = (string) $path;
$length = strlen($base_path);
if (substr($path, 0, $length) === $base_path) {
$path = substr($path, $length);
}
if ($path[0] !== '/') {
$path = '/' . $path;
}
$path = $base_url . $path;
}
return $path;
}
protected function getUrl() {
return $this->url;
}
protected function drupalGetHeaders($all_requests = FALSE) {
$request = 0;
$headers = array(
$request => array(),
);
foreach ($this->headers as $header) {
$header = trim($header);
if ($header === '') {
$request++;
}
else {
if (strpos($header, 'HTTP/') === 0) {
$name = ':status';
$value = $header;
}
else {
list($name, $value) = explode(':', $header, 2);
$name = strtolower($name);
}
if (isset($headers[$request][$name])) {
$headers[$request][$name] .= ',' . trim($value);
}
else {
$headers[$request][$name] = trim($value);
}
}
}
if (!$all_requests) {
$headers = array_pop($headers);
}
return $headers;
}
protected function drupalGetHeader($name, $all_requests = FALSE) {
$name = strtolower($name);
$header = FALSE;
if ($all_requests) {
foreach (array_reverse($this
->drupalGetHeaders(TRUE)) as $headers) {
if (isset($headers[$name])) {
$header = $headers[$name];
break;
}
}
}
else {
$headers = $this
->drupalGetHeaders();
if (isset($headers[$name])) {
$header = $headers[$name];
}
}
return $header;
}
protected function drupalGetContent() {
return $this->content;
}
protected function drupalGetSettings() {
return $this->drupalSettings;
}
protected function drupalGetMails($filter = array()) {
$captured_emails = state()
->get('system.test_email_collector') ?: array();
$filtered_emails = array();
foreach ($captured_emails as $message) {
foreach ($filter as $key => $value) {
if (!isset($message[$key]) || $message[$key] != $value) {
continue 2;
}
}
$filtered_emails[] = $message;
}
return $filtered_emails;
}
protected function drupalSetContent($content, $url = 'internal:') {
$this->content = $content;
$this->url = $url;
$this->plainTextContent = FALSE;
$this->elements = FALSE;
$this->drupalSettings = array();
if (preg_match('/var drupalSettings = (.*?);/', $content, $matches)) {
$this->drupalSettings = drupal_json_decode($matches[1]);
}
}
protected function drupalSetSettings($settings) {
$this->drupalSettings = $settings;
}
protected function createTypedData($definition, $value = NULL, $context = array()) {
$type = $definition['type'];
$data = typed_data()
->create($definition, $value, $context);
$this
->assertTrue($data instanceof \Drupal\Core\TypedData\TypedDataInterface, 'Typed data object is an instance of the typed data interface.');
$definition = $data
->getDefinition();
$this
->assertTrue(!empty($definition['label']), $definition['label'] . ' data definition was returned.');
$this
->assertEqual($data
->getType(), $type, $definition['label'] . ' object returned type.');
return $data;
}
protected function assertUrl($path, array $options = array(), $message = '', $group = 'Other') {
if (!$message) {
$message = t('Current URL is @url.', array(
'@url' => var_export(url($path, $options), TRUE),
));
}
$options['absolute'] = TRUE;
return $this
->assertEqual($this
->getUrl(), url($path, $options), $message, $group);
}
protected function assertRaw($raw, $message = '', $group = 'Other') {
if (!$message) {
$message = t('Raw "@raw" found', array(
'@raw' => $raw,
));
}
return $this
->assert(strpos($this
->drupalGetContent(), $raw) !== FALSE, $message, $group);
}
protected function assertNoRaw($raw, $message = '', $group = 'Other') {
if (!$message) {
$message = t('Raw "@raw" not found', array(
'@raw' => $raw,
));
}
return $this
->assert(strpos($this
->drupalGetContent(), $raw) === FALSE, $message, $group);
}
protected function assertText($text, $message = '', $group = 'Other') {
return $this
->assertTextHelper($text, $message, $group, FALSE);
}
protected function assertNoText($text, $message = '', $group = 'Other') {
return $this
->assertTextHelper($text, $message, $group, TRUE);
}
protected function assertTextHelper($text, $message = '', $group, $not_exists) {
if ($this->plainTextContent === FALSE) {
$this->plainTextContent = filter_xss($this
->drupalGetContent(), array());
}
if (!$message) {
$message = !$not_exists ? t('"@text" found', array(
'@text' => $text,
)) : t('"@text" not found', array(
'@text' => $text,
));
}
return $this
->assert($not_exists == (strpos($this->plainTextContent, $text) === FALSE), $message, $group);
}
protected function assertUniqueText($text, $message = '', $group = 'Other') {
return $this
->assertUniqueTextHelper($text, $message, $group, TRUE);
}
protected function assertNoUniqueText($text, $message = '', $group = 'Other') {
return $this
->assertUniqueTextHelper($text, $message, $group, FALSE);
}
protected function assertUniqueTextHelper($text, $message = '', $group, $be_unique) {
if ($this->plainTextContent === FALSE) {
$this->plainTextContent = filter_xss($this
->drupalGetContent(), array());
}
if (!$message) {
$message = '"' . $text . '"' . ($be_unique ? ' found only once' : ' found more than once');
}
$first_occurance = strpos($this->plainTextContent, $text);
if ($first_occurance === FALSE) {
return $this
->assert(FALSE, $message, $group);
}
$offset = $first_occurance + strlen($text);
$second_occurance = strpos($this->plainTextContent, $text, $offset);
return $this
->assert($be_unique == ($second_occurance === FALSE), $message, $group);
}
protected function assertPattern($pattern, $message = '', $group = 'Other') {
if (!$message) {
$message = t('Pattern "@pattern" found', array(
'@pattern' => $pattern,
));
}
return $this
->assert((bool) preg_match($pattern, $this
->drupalGetContent()), $message, $group);
}
protected function assertNoPattern($pattern, $message = '', $group = 'Other') {
if (!$message) {
$message = t('Pattern "@pattern" not found', array(
'@pattern' => $pattern,
));
}
return $this
->assert(!preg_match($pattern, $this
->drupalGetContent()), $message, $group);
}
protected function assertTitle($title, $message = '', $group = 'Other') {
$actual = (string) current($this
->xpath('//title'));
if (!$message) {
$message = t('Page title @actual is equal to @expected.', array(
'@actual' => var_export($actual, TRUE),
'@expected' => var_export($title, TRUE),
));
}
return $this
->assertEqual($actual, $title, $message, $group);
}
protected function assertNoTitle($title, $message = '', $group = 'Other') {
$actual = (string) current($this
->xpath('//title'));
if (!$message) {
$message = t('Page title @actual is not equal to @unexpected.', array(
'@actual' => var_export($actual, TRUE),
'@unexpected' => var_export($title, TRUE),
));
}
return $this
->assertNotEqual($actual, $title, $message, $group);
}
protected function assertThemeOutput($callback, array $variables = array(), $expected, $message = '', $group = 'Other') {
$output = theme($callback, $variables);
$this
->verbose('Variables:' . '<pre>' . check_plain(var_export($variables, TRUE)) . '</pre>' . '<hr />' . 'Result:' . '<pre>' . check_plain(var_export($output, TRUE)) . '</pre>' . '<hr />' . 'Expected:' . '<pre>' . check_plain(var_export($expected, TRUE)) . '</pre>' . '<hr />' . $output);
if (!$message) {
$message = '%callback rendered correctly.';
}
$message = format_string($message, array(
'%callback' => 'theme_' . $callback . '()',
));
return $this
->assertIdentical($output, $expected, $message, $group);
}
protected function assertFieldByXPath($xpath, $value = NULL, $message = '', $group = 'Other') {
$fields = $this
->xpath($xpath);
$found = TRUE;
if (isset($value)) {
$found = FALSE;
if ($fields) {
foreach ($fields as $field) {
if (isset($field['value']) && $field['value'] == $value) {
$found = TRUE;
}
elseif (isset($field->option)) {
if ($this
->getSelectedItem($field) == $value) {
$found = TRUE;
}
else {
$items = $this
->getAllOptions($field);
if (!empty($items) && $items[0]['value'] == $value) {
$found = TRUE;
}
}
}
elseif ((string) $field == $value) {
$found = TRUE;
}
}
}
}
return $this
->assertTrue($fields && $found, $message, $group);
}
protected function getSelectedItem(SimpleXMLElement $element) {
foreach ($element
->children() as $item) {
if (isset($item['selected'])) {
return $item['value'];
}
elseif ($item
->getName() == 'optgroup') {
if ($value = $this
->getSelectedItem($item)) {
return $value;
}
}
}
return FALSE;
}
protected function assertNoFieldByXPath($xpath, $value = NULL, $message = '', $group = 'Other') {
$fields = $this
->xpath($xpath);
$found = TRUE;
if (isset($value)) {
$found = FALSE;
if ($fields) {
foreach ($fields as $field) {
if ($field['value'] == $value) {
$found = TRUE;
}
}
}
}
return $this
->assertFalse($fields && $found, $message, $group);
}
protected function assertFieldByName($name, $value = NULL, $message = NULL, $group = 'Browser') {
if (!isset($message)) {
if (!isset($value)) {
$message = t('Found field with name @name', array(
'@name' => var_export($name, TRUE),
));
}
else {
$message = t('Found field with name @name and value @value', array(
'@name' => var_export($name, TRUE),
'@value' => var_export($value, TRUE),
));
}
}
return $this
->assertFieldByXPath($this
->constructFieldXpath('name', $name), $value, $message, $group);
}
protected function assertNoFieldByName($name, $value = '', $message = '', $group = 'Browser') {
return $this
->assertNoFieldByXPath($this
->constructFieldXpath('name', $name), $value, $message ? $message : t('Did not find field by name @name', array(
'@name' => $name,
)), $group);
}
protected function assertFieldById($id, $value = '', $message = '', $group = 'Browser') {
return $this
->assertFieldByXPath($this
->constructFieldXpath('id', $id), $value, $message ? $message : t('Found field by id @id', array(
'@id' => $id,
)), $group);
}
protected function assertNoFieldById($id, $value = '', $message = '', $group = 'Browser') {
return $this
->assertNoFieldByXPath($this
->constructFieldXpath('id', $id), $value, $message ? $message : t('Did not find field by id @id', array(
'@id' => $id,
)), $group);
}
protected function assertFieldChecked($id, $message = '', $group = 'Browser') {
$elements = $this
->xpath('//input[@id=:id]', array(
':id' => $id,
));
return $this
->assertTrue(isset($elements[0]) && !empty($elements[0]['checked']), $message ? $message : t('Checkbox field @id is checked.', array(
'@id' => $id,
)), $group);
}
protected function assertNoFieldChecked($id, $message = '', $group = 'Browser') {
$elements = $this
->xpath('//input[@id=:id]', array(
':id' => $id,
));
return $this
->assertTrue(isset($elements[0]) && empty($elements[0]['checked']), $message ? $message : t('Checkbox field @id is not checked.', array(
'@id' => $id,
)), $group);
}
protected function assertOption($id, $option, $message = '', $group = 'Browser') {
$options = $this
->xpath('//select[@id=:id]/option[@value=:option]', array(
':id' => $id,
':option' => $option,
));
return $this
->assertTrue(isset($options[0]), $message ? $message : t('Option @option for field @id exists.', array(
'@option' => $option,
'@id' => $id,
)), $group);
}
protected function assertNoOption($id, $option, $message = '', $group = 'Browser') {
$selects = $this
->xpath('//select[@id=:id]', array(
':id' => $id,
));
$options = $this
->xpath('//select[@id=:id]/option[@value=:option]', array(
':id' => $id,
':option' => $option,
));
return $this
->assertTrue(isset($selects[0]) && !isset($options[0]), $message ? $message : t('Option @option for field @id does not exist.', array(
'@option' => $option,
'@id' => $id,
)), $group);
}
protected function assertOptionSelected($id, $option, $message = '', $group = 'Browser') {
$elements = $this
->xpath('//select[@id=:id]//option[@value=:option]', array(
':id' => $id,
':option' => $option,
));
return $this
->assertTrue(isset($elements[0]) && !empty($elements[0]['selected']), $message ? $message : t('Option @option for field @id is selected.', array(
'@option' => $option,
'@id' => $id,
)), $group);
}
protected function assertNoOptionSelected($id, $option, $message = '', $group = 'Browser') {
$elements = $this
->xpath('//select[@id=:id]//option[@value=:option]', array(
':id' => $id,
':option' => $option,
));
return $this
->assertTrue(isset($elements[0]) && empty($elements[0]['selected']), $message ? $message : t('Option @option for field @id is not selected.', array(
'@option' => $option,
'@id' => $id,
)), $group);
}
protected function assertField($field, $message = '', $group = 'Other') {
return $this
->assertFieldByXPath($this
->constructFieldXpath('name', $field) . '|' . $this
->constructFieldXpath('id', $field), NULL, $message, $group);
}
protected function assertNoField($field, $message = '', $group = 'Other') {
return $this
->assertNoFieldByXPath($this
->constructFieldXpath('name', $field) . '|' . $this
->constructFieldXpath('id', $field), NULL, $message, $group);
}
protected function assertNoDuplicateIds($message = '', $group = 'Other', $ids_to_skip = array()) {
$status = TRUE;
foreach ($this
->xpath('//*[@id]') as $element) {
$id = (string) $element['id'];
if (isset($seen_ids[$id]) && !in_array($id, $ids_to_skip)) {
$this
->fail(t('The HTML ID %id is unique.', array(
'%id' => $id,
)), $group);
$status = FALSE;
}
$seen_ids[$id] = TRUE;
}
return $this
->assert($status, $message, $group);
}
protected function constructFieldXpath($attribute, $value) {
$xpath = '//textarea[@' . $attribute . '=:value]|//input[@' . $attribute . '=:value]|//select[@' . $attribute . '=:value]';
return $this
->buildXPathQuery($xpath, array(
':value' => $value,
));
}
protected function assertResponse($code, $message = '', $group = 'Browser') {
$curl_code = curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE);
$match = is_array($code) ? in_array($curl_code, $code) : $curl_code == $code;
return $this
->assertTrue($match, $message ? $message : t('HTTP response expected !code, actual !curl_code', array(
'!code' => $code,
'!curl_code' => $curl_code,
)), $group);
}
protected function assertNoResponse($code, $message = '', $group = 'Browser') {
$curl_code = curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE);
$match = is_array($code) ? in_array($curl_code, $code) : $curl_code == $code;
return $this
->assertFalse($match, $message ? $message : t('HTTP response not expected !code, actual !curl_code', array(
'!code' => $code,
'!curl_code' => $curl_code,
)), $group);
}
protected function assertMail($name, $value = '', $message = '', $group = 'E-mail') {
$captured_emails = state()
->get('system.test_email_collector') ?: array();
$email = end($captured_emails);
return $this
->assertTrue($email && isset($email[$name]) && $email[$name] == $value, $message, $group);
}
protected function assertMailString($field_name, $string, $email_depth, $message = '', $group = 'Other') {
$mails = $this
->drupalGetMails();
$string_found = FALSE;
for ($i = count($mails) - 1; $i >= count($mails) - $email_depth && $i >= 0; $i--) {
$mail = $mails[$i];
$normalized_mail = preg_replace('/\\s+/', ' ', $mail[$field_name]);
$normalized_string = preg_replace('/\\s+/', ' ', $string);
$string_found = FALSE !== strpos($normalized_mail, $normalized_string);
if ($string_found) {
break;
}
}
if (!$message) {
$message = format_string('Expected text found in @field of email message: "@expected".', array(
'@field' => $field_name,
'@expected' => $string,
));
}
return $this
->assertTrue($string_found, $message, $group);
}
protected function assertMailPattern($field_name, $regex, $message = '', $group = 'Other') {
$mails = $this
->drupalGetMails();
$mail = end($mails);
$regex_found = preg_match("/{$regex}/", $mail[$field_name]);
if (!$message) {
$message = format_string('Expected text found in @field of email message: "@expected".', array(
'@field' => $field_name,
'@expected' => $regex,
));
}
return $this
->assertTrue($regex_found, $message, $group);
}
protected function verboseEmail($count = 1) {
$mails = $this
->drupalGetMails();
for ($i = count($mails) - 1; $i >= count($mails) - $count && $i >= 0; $i--) {
$mail = $mails[$i];
$this
->verbose(t('Email:') . '<pre>' . print_r($mail, TRUE) . '</pre>');
}
}
}