- Patch #718662 by andypost, c960657: DBLog listings truncate messages in the middle of HTML tags.
parent
73deaa340a
commit
cb112e9a34
|
@ -8,6 +8,9 @@
|
|||
|
||||
/**
|
||||
* Menu callback; displays a listing of log messages.
|
||||
*
|
||||
* Messages are truncated at 56 chars. Full-length message could be viewed at
|
||||
* the message details page.
|
||||
*/
|
||||
function dblog_overview() {
|
||||
$filter = dblog_build_filter_query();
|
||||
|
@ -65,7 +68,7 @@ function dblog_overview() {
|
|||
$icons[$dblog->severity],
|
||||
t($dblog->type),
|
||||
format_date($dblog->timestamp, 'short'),
|
||||
l(truncate_utf8(_dblog_format_message($dblog), 56, TRUE, TRUE), 'admin/reports/event/' . $dblog->wid, array('html' => TRUE)),
|
||||
theme('dblog_message', array('event' => $dblog, 'link' => TRUE)),
|
||||
theme('username', array('account' => $dblog)),
|
||||
$dblog->link,
|
||||
),
|
||||
|
@ -87,8 +90,12 @@ function dblog_overview() {
|
|||
}
|
||||
|
||||
/**
|
||||
* Menu callback; generic function to display a page of the most frequent
|
||||
* dblog events of a specified type.
|
||||
* Menu callback; generic function to display a page of the most frequent events.
|
||||
*
|
||||
* Messages are not truncated because events from this page have no detail view.
|
||||
*
|
||||
* @param $type
|
||||
* type of dblog events to display.
|
||||
*/
|
||||
function dblog_top($type) {
|
||||
|
||||
|
@ -114,7 +121,7 @@ function dblog_top($type) {
|
|||
|
||||
$rows = array();
|
||||
foreach ($result as $dblog) {
|
||||
$rows[] = array($dblog->count, truncate_utf8(_dblog_format_message($dblog), 56, TRUE, TRUE));
|
||||
$rows[] = array($dblog->count, theme('dblog_message', array('event' => $dblog)));
|
||||
}
|
||||
|
||||
$build['dblog_top_table'] = array(
|
||||
|
@ -158,7 +165,7 @@ function dblog_event($id) {
|
|||
),
|
||||
array(
|
||||
array('data' => t('Message'), 'header' => TRUE),
|
||||
_dblog_format_message($dblog),
|
||||
theme('dblog_message', array('event' => $dblog)),
|
||||
),
|
||||
array(
|
||||
array('data' => t('Severity'), 'header' => TRUE),
|
||||
|
@ -246,21 +253,35 @@ function dblog_filters() {
|
|||
/**
|
||||
* Formats a log message for display.
|
||||
*
|
||||
* @param $dblog
|
||||
* An object with at least the message and variables properties
|
||||
* @param $variables
|
||||
* An associative array containing:
|
||||
* - event: An object with at least the message and variables properties.
|
||||
* - link: (optional) Format message as link, event->wid is required.
|
||||
*
|
||||
* @ingroup themeable
|
||||
*/
|
||||
function _dblog_format_message($dblog) {
|
||||
// Legacy messages and user specified text
|
||||
if ($dblog->variables === 'N;') {
|
||||
return $dblog->message;
|
||||
}
|
||||
// Message to translate with injected variables
|
||||
else {
|
||||
return t($dblog->message, unserialize($dblog->variables));
|
||||
function theme_dblog_message($variables) {
|
||||
$output = '';
|
||||
$event = $variables['event'];
|
||||
// Check for required properties.
|
||||
if (isset($event->message) && isset($event->variables)) {
|
||||
// Messages without variables or user specified text.
|
||||
if ($event->variables === 'N;') {
|
||||
$output = $event->message;
|
||||
}
|
||||
// Message to translate with injected variables.
|
||||
else {
|
||||
$output = t($event->message, unserialize($event->variables));
|
||||
}
|
||||
if ($variables['link'] && isset($event->wid)) {
|
||||
// Truncate message to 56 chars.
|
||||
$output = truncate_utf8(filter_xss($output, array()), 56, TRUE, TRUE);
|
||||
$output = l($output, 'admin/reports/event/' . $event->wid, array('html' => TRUE));
|
||||
}
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return form for dblog administration filters.
|
||||
*
|
||||
|
|
|
@ -144,3 +144,15 @@ function dblog_form_system_logging_settings_alter(&$form, $form_state) {
|
|||
);
|
||||
$form['actions']['#weight'] = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_theme().
|
||||
*/
|
||||
function dblog_theme() {
|
||||
return array(
|
||||
'dblog_message' => array(
|
||||
'variables' => array('event' => NULL, 'link' => FALSE),
|
||||
'file' => 'dblog.admin.inc',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
|
|
@ -74,9 +74,7 @@ class DBLogTestCase extends DrupalWebTestCase {
|
|||
$this->assertTrue($count > $row_limit, t('Dblog row count of @count exceeds row limit of @limit', array('@count' => $count, '@limit' => $row_limit)));
|
||||
|
||||
// Run cron job.
|
||||
$this->drupalGet('admin/reports/status/run-cron');
|
||||
$this->assertResponse(200);
|
||||
$this->assertText(t('Cron ran successfully'), t('Cron ran successfully'));
|
||||
$this->cronRun();
|
||||
// Verify dblog row count equals row limit plus one because cron adds a record after it runs.
|
||||
$count = db_query('SELECT COUNT(wid) FROM {watchdog}')->fetchField();
|
||||
$this->assertTrue($count == $row_limit + 1, t('Dblog row count of @count equals row limit of @limit plus one', array('@count' => $count, '@limit' => $row_limit)));
|
||||
|
@ -108,9 +106,9 @@ class DBLogTestCase extends DrupalWebTestCase {
|
|||
'ip' => ip_address(),
|
||||
'timestamp' => REQUEST_TIME,
|
||||
);
|
||||
$message = 'Log entry added to test the dblog row limit.';
|
||||
$message = 'Log entry added to test the dblog row limit. Entry #';
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
$log['message'] = $this->randomString();
|
||||
$log['message'] = $message . $i;
|
||||
dblog_watchdog($log);
|
||||
}
|
||||
}
|
||||
|
@ -181,7 +179,7 @@ class DBLogTestCase extends DrupalWebTestCase {
|
|||
*/
|
||||
private function doUser() {
|
||||
// Set user variables.
|
||||
$name = $this->randomName(4);
|
||||
$name = $this->randomName();
|
||||
$pass = user_password();
|
||||
// Add user using form to generate add user event (which is not triggered by drupalCreateUser).
|
||||
$edit = array();
|
||||
|
@ -223,11 +221,38 @@ class DBLogTestCase extends DrupalWebTestCase {
|
|||
// Default display includes name and email address; if too long then email is replaced by three periods.
|
||||
$this->assertLogMessage(t('New user: %name (%email).', array('%name' => $name, '%email' => $user->mail)), t('DBLog event was recorded: [add user]'));
|
||||
// Login user.
|
||||
$this->assertLogMessage(t('Session opened for %name', array('%name' => $name)), t('DBLog event was recorded: [login user]'));
|
||||
$this->assertLogMessage(t('Session opened for %name.', array('%name' => $name)), t('DBLog event was recorded: [login user]'));
|
||||
// Logout user.
|
||||
$this->assertLogMessage(t('Session closed for %name', array('%name' => $name)), t('DBLog event was recorded: [logout user]'));
|
||||
$this->assertLogMessage(t('Session closed for %name.', array('%name' => $name)), t('DBLog event was recorded: [logout user]'));
|
||||
// Delete user.
|
||||
$this->assertLogMessage(t('Deleted user: %name', array('%name' => $name)), t('DBLog event was recorded: [delete user]'));
|
||||
$message = t('Deleted user: %name %email.', array('%name' => $name, '%email' => '<' . $user->mail . '>'));
|
||||
$message_text = truncate_utf8(filter_xss($message, array()), 56, TRUE, TRUE);
|
||||
// Verify full message on details page.
|
||||
$link = FALSE;
|
||||
if ($links = $this->xpath('//a[text()="' . html_entity_decode($message_text) . '"]')) {
|
||||
// Found link with the message text.
|
||||
$links = array_shift($links);
|
||||
foreach ($links->attributes() as $attr => $value) {
|
||||
if ($attr == 'href') {
|
||||
// Extract link to details page.
|
||||
$link = drupal_substr($value, strpos($value, 'admin/reports/event/'));
|
||||
$this->drupalGet($link);
|
||||
// Check for full message text on the details page.
|
||||
$this->assertRaw($message, t('DBLog event details was found: [delete user]'));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->assertTrue($link, t('DBLog event was recorded: [delete user]'));
|
||||
// Visit random URL (to generate page not found event).
|
||||
$not_found_url = $this->randomName(60);
|
||||
$this->drupalGet($not_found_url);
|
||||
$this->assertResponse(404);
|
||||
// View dblog page-not-found report page.
|
||||
$this->drupalGet('admin/reports/page-not-found');
|
||||
$this->assertResponse(200);
|
||||
// Check that full-length url displayed.
|
||||
$this->assertText($not_found_url, t('DBLog event was recorded: [page not found]'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -537,15 +562,20 @@ class DBLogTestCase extends DrupalWebTestCase {
|
|||
/**
|
||||
* Assert messages appear on the log overview screen.
|
||||
*
|
||||
* This function should be used only for admin/reports/dblog page, because it
|
||||
* check for the message link text truncated to 56 characters. Other dblog
|
||||
* pages have no detail links so contains a full message text.
|
||||
*
|
||||
* @param $log_message
|
||||
* The message to check.
|
||||
* @param $message
|
||||
* The message to pass to simpletest.
|
||||
*/
|
||||
protected function assertLogMessage($log_message, $message) {
|
||||
// Truncate at 56 characters to compare with dblog's HTML output.
|
||||
// @todo: Check the database instead for the exact error string.
|
||||
$this->assertRaw(truncate_utf8($log_message, 56, TRUE, TRUE), $message);
|
||||
$message_text = truncate_utf8(filter_xss($log_message, array()), 56, TRUE, TRUE);
|
||||
// After filter_xss() HTML entities should be converted to their characters
|
||||
// because assertLink() uses this string in xpath() to query DOM.
|
||||
$this->assertLink(html_entity_decode($message_text), 0, $message);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue