Chris@0: drupalPlaceBlock('system_breadcrumb_block'); Chris@0: $this->drupalPlaceBlock('page_title_block'); Chris@0: Chris@0: // Create users with specific permissions. Chris@0: $this->adminUser = $this->drupalCreateUser(['administer site configuration', 'access administration pages', 'access site reports', 'administer users']); Chris@0: $this->webUser = $this->drupalCreateUser([]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests Database Logging module functionality through interfaces. Chris@0: * Chris@0: * First logs in users, then creates database log events, and finally tests Chris@0: * Database Logging module functionality through both the admin and user Chris@0: * interfaces. Chris@0: */ Chris@0: public function testDbLog() { Chris@0: // Log in the admin user. Chris@0: $this->drupalLogin($this->adminUser); Chris@0: Chris@0: $row_limit = 100; Chris@0: $this->verifyRowLimit($row_limit); Chris@0: $this->verifyEvents(); Chris@0: $this->verifyReports(); Chris@0: $this->verifyBreadcrumbs(); Chris@0: $this->verifyLinkEscaping(); Chris@0: // Verify the overview table sorting. Chris@0: $orders = ['Date', 'Type', 'User']; Chris@0: $sorts = ['asc', 'desc']; Chris@0: foreach ($orders as $order) { Chris@0: foreach ($sorts as $sort) { Chris@0: $this->verifySort($sort, $order); Chris@0: } Chris@0: } Chris@0: Chris@0: // Log in the regular user. Chris@0: $this->drupalLogin($this->webUser); Chris@0: $this->verifyReports(403); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test individual log event page. Chris@0: */ Chris@0: public function testLogEventPage() { Chris@0: // Login the admin user. Chris@0: $this->drupalLogin($this->adminUser); Chris@0: Chris@0: // Since referrer and location links vary by how the tests are run, inject Chris@0: // fake log data to test these. Chris@0: $context = [ Chris@0: 'request_uri' => 'http://example.com?dblog=1', Chris@0: 'referer' => 'http://example.org?dblog=2', Chris@0: 'uid' => 0, Chris@0: 'channel' => 'testing', Chris@0: 'link' => 'foo/bar', Chris@0: 'ip' => '0.0.1.0', Chris@0: 'timestamp' => REQUEST_TIME, Chris@0: ]; Chris@0: \Drupal::service('logger.dblog')->log(RfcLogLevel::NOTICE, 'Test message', $context); Chris@0: $wid = db_query('SELECT MAX(wid) FROM {watchdog}')->fetchField(); Chris@0: Chris@0: // Verify the links appear correctly. Chris@0: $this->drupalGet('admin/reports/dblog/event/' . $wid); Chris@0: $this->assertLinkByHref($context['request_uri']); Chris@0: $this->assertLinkByHref($context['referer']); Chris@0: Chris@0: // Verify hostname. Chris@0: $this->assertRaw($context['ip'], 'Found hostname on the detail page.'); Chris@0: Chris@18: // Verify location. Chris@18: $this->assertRaw($context['request_uri'], 'Found location on the detail page.'); Chris@18: Chris@0: // Verify severity. Chris@0: $this->assertText('Notice', 'The severity was properly displayed on the detail page.'); Chris@0: } Chris@0: Chris@0: /** Chris@18: * Test individual log event page with missing log attributes. Chris@18: * Chris@18: * In some cases few log attributes are missing. For example: Chris@18: * - Missing referer: When request is made to a specific url directly and Chris@18: * error occurred. In this case there is no referer. Chris@18: * - Incorrect location: When location attribute is incorrect uri which can Chris@18: * not be used to generate a valid link. Chris@18: */ Chris@18: public function testLogEventPageWithMissingInfo() { Chris@18: $this->drupalLogin($this->adminUser); Chris@18: $connection = Database::getConnection(); Chris@18: Chris@18: // Test log event page with missing referer. Chris@18: $this->generateLogEntries(1, [ Chris@18: 'referer' => NULL, Chris@18: ]); Chris@18: $wid = $connection->query('SELECT MAX(wid) FROM {watchdog}')->fetchField(); Chris@18: $this->drupalGet('admin/reports/dblog/event/' . $wid); Chris@18: Chris@18: // Verify table headers are present, even though the referrer is missing. Chris@18: $this->assertText('Referrer', 'Referrer header is present on the detail page.'); Chris@18: Chris@18: // Verify severity. Chris@18: $this->assertText('Notice', 'The severity is properly displayed on the detail page.'); Chris@18: Chris@18: // Test log event page with incorrect location. Chris@18: $request_uri = '/some/incorrect/url'; Chris@18: $this->generateLogEntries(1, [ Chris@18: 'request_uri' => $request_uri, Chris@18: ]); Chris@18: $wid = $connection->query('SELECT MAX(wid) FROM {watchdog}')->fetchField(); Chris@18: $this->drupalGet('admin/reports/dblog/event/' . $wid); Chris@18: Chris@18: // Verify table headers are present. Chris@18: $this->assertText('Location', 'Location header is present on the detail page.'); Chris@18: Chris@18: // Verify severity. Chris@18: $this->assertText('Notice', 'The severity is properly displayed on the detail page.'); Chris@18: Chris@18: // Verify location is available as plain text. Chris@18: $this->assertEquals($request_uri, $this->cssSelect('table.dblog-event > tbody > tr:nth-child(4) > td')[0]->getHtml()); Chris@18: $this->assertNoLink($request_uri); Chris@18: } Chris@18: Chris@18: /** Chris@0: * Verifies setting of the database log row limit. Chris@0: * Chris@0: * @param int $row_limit Chris@0: * The row limit. Chris@0: */ Chris@0: private function verifyRowLimit($row_limit) { Chris@0: // Change the database log row limit. Chris@0: $edit = []; Chris@0: $edit['dblog_row_limit'] = $row_limit; Chris@0: $this->drupalPostForm('admin/config/development/logging', $edit, t('Save configuration')); Chris@0: $this->assertResponse(200); Chris@0: Chris@0: // Check row limit variable. Chris@0: $current_limit = $this->config('dblog.settings')->get('row_limit'); Chris@0: $this->assertTrue($current_limit == $row_limit, format_string('[Cache] Row limit variable of @count equals row limit of @limit', ['@count' => $current_limit, '@limit' => $row_limit])); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Clear the entry logs by clicking on 'Clear log messages' button. Chris@0: */ Chris@0: protected function clearLogsEntries() { Chris@0: $this->drupalGet(Url::fromRoute('dblog.confirm')); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Filters the logs according to the specific severity and log entry type. Chris@0: * Chris@0: * @param string $type Chris@0: * (optional) The log entry type. Chris@0: * @param string $severity Chris@0: * (optional) The log entry severity. Chris@0: */ Chris@0: protected function filterLogsEntries($type = NULL, $severity = NULL) { Chris@0: $edit = []; Chris@0: if (isset($type)) { Chris@0: $edit['type[]'] = $type; Chris@0: } Chris@0: if (isset($severity)) { Chris@0: $edit['severity[]'] = $severity; Chris@0: } Chris@0: $this->drupalPostForm(NULL, $edit, t('Filter')); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Confirms that database log reports are displayed at the correct paths. Chris@0: * Chris@0: * @param int $response Chris@0: * (optional) HTTP response code. Defaults to 200. Chris@0: */ Chris@0: private function verifyReports($response = 200) { Chris@0: // View the database log help page. Chris@0: $this->drupalGet('admin/help/dblog'); Chris@0: $this->assertResponse($response); Chris@0: if ($response == 200) { Chris@0: $this->assertText(t('Database Logging'), 'DBLog help was displayed'); Chris@0: } Chris@0: Chris@0: // View the database log report page. Chris@0: $this->drupalGet('admin/reports/dblog'); Chris@0: $this->assertResponse($response); Chris@0: if ($response == 200) { Chris@0: $this->assertText(t('Recent log messages'), 'DBLog report was displayed'); Chris@0: } Chris@0: Chris@0: $this->drupalGet('admin/reports/dblog/confirm'); Chris@0: $this->assertResponse($response); Chris@0: if ($response == 200) { Chris@0: $this->assertText(t('Are you sure you want to delete the recent logs?'), 'DBLog clear logs form was displayed'); Chris@0: } Chris@0: Chris@0: // View the database log page-not-found report page. Chris@0: $this->drupalGet('admin/reports/page-not-found'); Chris@0: $this->assertResponse($response); Chris@0: if ($response == 200) { Chris@0: $this->assertText("Top 'page not found' errors", 'DBLog page-not-found report was displayed'); Chris@0: } Chris@0: Chris@0: // View the database log access-denied report page. Chris@0: $this->drupalGet('admin/reports/access-denied'); Chris@0: $this->assertResponse($response); Chris@0: if ($response == 200) { Chris@0: $this->assertText("Top 'access denied' errors", 'DBLog access-denied report was displayed'); Chris@0: } Chris@0: Chris@0: // View the database log event page. Chris@0: $wid = db_query('SELECT MIN(wid) FROM {watchdog}')->fetchField(); Chris@0: $this->drupalGet('admin/reports/dblog/event/' . $wid); Chris@0: $this->assertResponse($response); Chris@0: if ($response == 200) { Chris@0: $this->assertText(t('Details'), 'DBLog event node was displayed'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Generates and then verifies breadcrumbs. Chris@0: */ Chris@0: private function verifyBreadcrumbs() { Chris@0: // View the database log event page. Chris@0: $wid = db_query('SELECT MIN(wid) FROM {watchdog}')->fetchField(); Chris@0: $this->drupalGet('admin/reports/dblog/event/' . $wid); Chris@0: $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a'; Chris@0: $this->assertEqual(current($this->xpath($xpath))->getText(), 'Recent log messages', 'DBLogs link displayed at breadcrumb in event page.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Generates and then verifies various types of events. Chris@0: */ Chris@0: private function verifyEvents() { Chris@0: // Invoke events. Chris@0: $this->doUser(); Chris@0: $this->drupalCreateContentType(['type' => 'article', 'name' => t('Article')]); Chris@0: $this->drupalCreateContentType(['type' => 'page', 'name' => t('Basic page')]); Chris@0: $this->doNode('article'); Chris@0: $this->doNode('page'); Chris@0: $this->doNode('forum'); Chris@0: Chris@0: // When a user account is canceled, any content they created remains but the Chris@0: // uid = 0. Records in the watchdog table related to that user have the uid Chris@0: // set to zero. Chris@0: } Chris@0: Chris@0: /** Chris@0: * Verifies the sorting functionality of the database logging reports table. Chris@0: * Chris@0: * @param string $sort Chris@0: * The sort direction. Chris@0: * @param string $order Chris@0: * The order by which the table should be sorted. Chris@0: */ Chris@0: public function verifySort($sort = 'asc', $order = 'Date') { Chris@0: $this->drupalGet('admin/reports/dblog', ['query' => ['sort' => $sort, 'order' => $order]]); Chris@0: $this->assertResponse(200); Chris@0: $this->assertText(t('Recent log messages'), 'DBLog report was displayed correctly and sorting went fine.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the escaping of links in the operation row of a database log detail Chris@0: * page. Chris@0: */ Chris@0: private function verifyLinkEscaping() { Chris@0: $link = \Drupal::l('View', Url::fromRoute('entity.node.canonical', ['node' => 1])); Chris@0: $message = 'Log entry added to do the verifyLinkEscaping test.'; Chris@0: $this->generateLogEntries(1, [ Chris@0: 'message' => $message, Chris@0: 'link' => $link, Chris@0: ]); Chris@0: Chris@18: $result = Database::getConnection()->queryRange('SELECT wid FROM {watchdog} ORDER BY wid DESC', 0, 1); Chris@0: $this->drupalGet('admin/reports/dblog/event/' . $result->fetchField()); Chris@0: Chris@0: // Check if the link exists (unescaped). Chris@0: $this->assertRaw($link); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Generates and then verifies some user events. Chris@0: */ Chris@0: private function doUser() { Chris@0: // Set user variables. Chris@0: $name = $this->randomMachineName(); Chris@0: $pass = user_password(); Chris@0: // Add a user using the form to generate an add user event (which is not Chris@0: // triggered by drupalCreateUser). Chris@0: $edit = []; Chris@0: $edit['name'] = $name; Chris@0: $edit['mail'] = $name . '@example.com'; Chris@0: $edit['pass[pass1]'] = $pass; Chris@0: $edit['pass[pass2]'] = $pass; Chris@0: $edit['status'] = 1; Chris@0: $this->drupalPostForm('admin/people/create', $edit, t('Create new account')); Chris@0: $this->assertResponse(200); Chris@0: // Retrieve the user object. Chris@0: $user = user_load_by_name($name); Chris@0: $this->assertTrue($user != NULL, format_string('User @name was loaded', ['@name' => $name])); Chris@0: // pass_raw property is needed by drupalLogin. Chris@0: $user->passRaw = $pass; Chris@0: // Log in user. Chris@0: $this->drupalLogin($user); Chris@0: // Log out user. Chris@0: $this->drupalLogout(); Chris@0: // Fetch the row IDs in watchdog that relate to the user. Chris@0: $result = db_query('SELECT wid FROM {watchdog} WHERE uid = :uid', [':uid' => $user->id()]); Chris@0: foreach ($result as $row) { Chris@0: $ids[] = $row->wid; Chris@0: } Chris@0: $count_before = (isset($ids)) ? count($ids) : 0; Chris@18: $this->assertTrue($count_before > 0, format_string('DBLog contains @count records for @name', ['@count' => $count_before, '@name' => $user->getAccountName()])); Chris@0: Chris@0: // Log in the admin user. Chris@0: $this->drupalLogin($this->adminUser); Chris@0: // Delete the user created at the start of this test. Chris@0: // We need to POST here to invoke batch_process() in the internal browser. Chris@0: $this->drupalPostForm('user/' . $user->id() . '/cancel', ['user_cancel_method' => 'user_cancel_reassign'], t('Cancel account')); Chris@0: Chris@0: // View the database log report. Chris@0: $this->drupalGet('admin/reports/dblog'); Chris@0: $this->assertResponse(200); Chris@0: Chris@0: // Verify that the expected events were recorded. Chris@0: // Add user. Chris@0: // Default display includes name and email address; if too long, the email Chris@0: // address is replaced by three periods. Chris@0: $this->assertLogMessage(t('New user: %name %email.', ['%name' => $name, '%email' => '<' . $user->getEmail() . '>']), 'DBLog event was recorded: [add user]'); Chris@0: // Log in user. Chris@0: $this->assertLogMessage(t('Session opened for %name.', ['%name' => $name]), 'DBLog event was recorded: [login user]'); Chris@0: // Log out user. Chris@0: $this->assertLogMessage(t('Session closed for %name.', ['%name' => $name]), 'DBLog event was recorded: [logout user]'); Chris@0: // Delete user. Chris@0: $message = t('Deleted user: %name %email.', ['%name' => $name, '%email' => '<' . $user->getEmail() . '>']); Chris@0: $message_text = Unicode::truncate(Html::decodeEntities(strip_tags($message)), 56, TRUE, TRUE); Chris@0: // Verify that the full message displays on the details page. Chris@0: $link = FALSE; Chris@0: if ($links = $this->xpath('//a[text()="' . $message_text . '"]')) { Chris@0: // Found link with the message text. Chris@0: $links = array_shift($links); Chris@0: $value = $links->getAttribute('href'); Chris@0: Chris@0: // Extract link to details page. Chris@17: $link = mb_substr($value, strpos($value, 'admin/reports/dblog/event/')); Chris@0: $this->drupalGet($link); Chris@0: // Check for full message text on the details page. Chris@0: $this->assertRaw($message, 'DBLog event details was found: [delete user]'); Chris@0: } Chris@0: $this->assertTrue($link, 'DBLog event was recorded: [delete user]'); Chris@0: // Visit random URL (to generate page not found event). Chris@0: $not_found_url = $this->randomMachineName(60); Chris@0: $this->drupalGet($not_found_url); Chris@0: $this->assertResponse(404); Chris@0: // View the database log page-not-found report page. Chris@0: $this->drupalGet('admin/reports/page-not-found'); Chris@0: $this->assertResponse(200); Chris@0: // Check that full-length URL displayed. Chris@0: $this->assertText($not_found_url, 'DBLog event was recorded: [page not found]'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Generates and then verifies some node events. Chris@0: * Chris@0: * @param string $type Chris@0: * A node type (e.g., 'article', 'page' or 'forum'). Chris@0: */ Chris@0: private function doNode($type) { Chris@0: // Create user. Chris@0: $perm = ['create ' . $type . ' content', 'edit own ' . $type . ' content', 'delete own ' . $type . ' content']; Chris@0: $user = $this->drupalCreateUser($perm); Chris@0: // Log in user. Chris@0: $this->drupalLogin($user); Chris@0: Chris@0: // Create a node using the form in order to generate an add content event Chris@0: // (which is not triggered by drupalCreateNode). Chris@0: $edit = $this->getContent($type); Chris@0: $title = $edit['title[0][value]']; Chris@0: $this->drupalPostForm('node/add/' . $type, $edit, t('Save')); Chris@0: $this->assertResponse(200); Chris@0: // Retrieve the node object. Chris@0: $node = $this->drupalGetNodeByTitle($title); Chris@0: $this->assertTrue($node != NULL, format_string('Node @title was loaded', ['@title' => $title])); Chris@0: // Edit the node. Chris@0: $edit = $this->getContentUpdate($type); Chris@0: $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save')); Chris@0: $this->assertResponse(200); Chris@0: // Delete the node. Chris@0: $this->drupalPostForm('node/' . $node->id() . '/delete', [], t('Delete')); Chris@0: $this->assertResponse(200); Chris@0: // View the node (to generate page not found event). Chris@0: $this->drupalGet('node/' . $node->id()); Chris@0: $this->assertResponse(404); Chris@0: // View the database log report (to generate access denied event). Chris@0: $this->drupalGet('admin/reports/dblog'); Chris@0: $this->assertResponse(403); Chris@0: Chris@0: // Log in the admin user. Chris@0: $this->drupalLogin($this->adminUser); Chris@0: // View the database log report. Chris@0: $this->drupalGet('admin/reports/dblog'); Chris@0: $this->assertResponse(200); Chris@0: Chris@0: // Verify that node events were recorded. Chris@0: // Was node content added? Chris@0: $this->assertLogMessage(t('@type: added %title.', ['@type' => $type, '%title' => $title]), 'DBLog event was recorded: [content added]'); Chris@0: // Was node content updated? Chris@0: $this->assertLogMessage(t('@type: updated %title.', ['@type' => $type, '%title' => $title]), 'DBLog event was recorded: [content updated]'); Chris@0: // Was node content deleted? Chris@0: $this->assertLogMessage(t('@type: deleted %title.', ['@type' => $type, '%title' => $title]), 'DBLog event was recorded: [content deleted]'); Chris@0: Chris@0: // View the database log access-denied report page. Chris@0: $this->drupalGet('admin/reports/access-denied'); Chris@0: $this->assertResponse(200); Chris@0: // Verify that the 'access denied' event was recorded. Chris@0: $this->assertText('admin/reports/dblog', 'DBLog event was recorded: [access denied]'); Chris@0: Chris@0: // View the database log page-not-found report page. Chris@0: $this->drupalGet('admin/reports/page-not-found'); Chris@0: $this->assertResponse(200); Chris@0: // Verify that the 'page not found' event was recorded. Chris@0: $this->assertText('node/' . $node->id(), 'DBLog event was recorded: [page not found]'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates random content based on node content type. Chris@0: * Chris@0: * @param string $type Chris@0: * Node content type (e.g., 'article'). Chris@0: * Chris@0: * @return array Chris@0: * Random content needed by various node types. Chris@0: */ Chris@0: private function getContent($type) { Chris@0: switch ($type) { Chris@0: case 'forum': Chris@0: $content = [ Chris@0: 'title[0][value]' => $this->randomMachineName(8), Chris@0: 'taxonomy_forums' => 1, Chris@0: 'body[0][value]' => $this->randomMachineName(32), Chris@0: ]; Chris@0: break; Chris@0: Chris@0: default: Chris@0: $content = [ Chris@0: 'title[0][value]' => $this->randomMachineName(8), Chris@0: 'body[0][value]' => $this->randomMachineName(32), Chris@0: ]; Chris@0: break; Chris@0: } Chris@0: return $content; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates random content as an update based on node content type. Chris@0: * Chris@0: * @param string $type Chris@0: * Node content type (e.g., 'article'). Chris@0: * Chris@0: * @return array Chris@0: * Random content needed by various node types. Chris@0: */ Chris@0: private function getContentUpdate($type) { Chris@0: $content = [ Chris@0: 'body[0][value]' => $this->randomMachineName(32), Chris@0: ]; Chris@0: return $content; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the addition and clearing of log events through the admin interface. Chris@0: * Chris@0: * Logs in the admin user, creates a database log event, and tests the Chris@0: * functionality of clearing the database log through the admin interface. Chris@0: */ Chris@0: public function testDBLogAddAndClear() { Chris@0: global $base_root; Chris@0: // Get a count of how many watchdog entries already exist. Chris@0: $count = db_query('SELECT COUNT(*) FROM {watchdog}')->fetchField(); Chris@0: $log = [ Chris@0: 'channel' => 'system', Chris@0: 'message' => 'Log entry added to test the doClearTest clear down.', Chris@0: 'variables' => [], Chris@0: 'severity' => RfcLogLevel::NOTICE, Chris@0: 'link' => NULL, Chris@0: 'uid' => $this->adminUser->id(), Chris@0: 'request_uri' => $base_root . \Drupal::request()->getRequestUri(), Chris@0: 'referer' => \Drupal::request()->server->get('HTTP_REFERER'), Chris@0: 'ip' => '127.0.0.1', Chris@0: 'timestamp' => REQUEST_TIME, Chris@0: ]; Chris@0: // Add a watchdog entry. Chris@0: $this->container->get('logger.dblog')->log($log['severity'], $log['message'], $log); Chris@0: // Make sure the table count has actually been incremented. Chris@0: $this->assertEqual($count + 1, db_query('SELECT COUNT(*) FROM {watchdog}')->fetchField(), format_string('\Drupal\dblog\Logger\DbLog->log() added an entry to the dblog :count', [':count' => $count])); Chris@0: // Log in the admin user. Chris@0: $this->drupalLogin($this->adminUser); Chris@0: // Post in order to clear the database table. Chris@0: $this->clearLogsEntries(); Chris@0: // Confirm that the logs should be cleared. Chris@0: $this->drupalPostForm(NULL, [], 'Confirm'); Chris@0: // Count the rows in watchdog that previously related to the deleted user. Chris@0: $count = db_query('SELECT COUNT(*) FROM {watchdog}')->fetchField(); Chris@0: $this->assertEqual($count, 0, format_string('DBLog contains :count records after a clear.', [':count' => $count])); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the database log filter functionality at admin/reports/dblog. Chris@0: */ Chris@0: public function testFilter() { Chris@0: $this->drupalLogin($this->adminUser); Chris@0: Chris@0: // Clear the log to ensure that only generated entries will be found. Chris@18: Database::getConnection()->delete('watchdog')->execute(); Chris@0: Chris@0: // Generate 9 random watchdog entries. Chris@0: $type_names = []; Chris@0: $types = []; Chris@0: for ($i = 0; $i < 3; $i++) { Chris@0: $type_names[] = $type_name = $this->randomMachineName(); Chris@0: $severity = RfcLogLevel::EMERGENCY; Chris@0: for ($j = 0; $j < 3; $j++) { Chris@0: $types[] = $type = [ Chris@0: 'count' => $j + 1, Chris@0: 'type' => $type_name, Chris@0: 'severity' => $severity++, Chris@0: ]; Chris@0: $this->generateLogEntries($type['count'], [ Chris@0: 'channel' => $type['type'], Chris@0: 'severity' => $type['severity'], Chris@0: ]); Chris@0: } Chris@0: } Chris@0: Chris@0: // View the database log page. Chris@0: $this->drupalGet('admin/reports/dblog'); Chris@0: Chris@0: // Confirm that all the entries are displayed. Chris@0: $count = $this->getTypeCount($types); Chris@0: foreach ($types as $key => $type) { Chris@0: $this->assertEqual($count[$key], $type['count'], 'Count matched'); Chris@0: } Chris@0: Chris@0: // Filter by each type and confirm that entries with various severities are Chris@0: // displayed. Chris@0: foreach ($type_names as $type_name) { Chris@0: $this->filterLogsEntries($type_name); Chris@0: Chris@0: // Count the number of entries of this type. Chris@0: $type_count = 0; Chris@0: foreach ($types as $type) { Chris@0: if ($type['type'] == $type_name) { Chris@0: $type_count += $type['count']; Chris@0: } Chris@0: } Chris@0: Chris@0: $count = $this->getTypeCount($types); Chris@0: $this->assertEqual(array_sum($count), $type_count, 'Count matched'); Chris@0: } Chris@0: Chris@0: // Set the filter to match each of the two filter-type attributes and Chris@0: // confirm the correct number of entries are displayed. Chris@0: foreach ($types as $type) { Chris@0: $this->filterLogsEntries($type['type'], $type['severity']); Chris@0: Chris@0: $count = $this->getTypeCount($types); Chris@0: $this->assertEqual(array_sum($count), $type['count'], 'Count matched'); Chris@0: } Chris@0: Chris@0: $this->drupalGet('admin/reports/dblog', ['query' => ['order' => 'Type']]); Chris@0: $this->assertResponse(200); Chris@0: $this->assertText(t('Operations'), 'Operations text found'); Chris@0: Chris@0: // Clear all logs and make sure the confirmation message is found. Chris@0: $this->clearLogsEntries(); Chris@0: // Confirm that the logs should be cleared. Chris@0: $this->drupalPostForm(NULL, [], 'Confirm'); Chris@0: $this->assertText(t('Database log cleared.'), 'Confirmation message found'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the database log event information from the browser page. Chris@0: * Chris@0: * @return array Chris@0: * List of log events where each event is an array with following keys: Chris@0: * - severity: (int) A database log severity constant. Chris@0: * - type: (string) The type of database log event. Chris@0: * - message: (string) The message for this database log event. Chris@0: * - user: (string) The user associated with this database log event. Chris@0: */ Chris@0: protected function getLogEntries() { Chris@0: $entries = []; Chris@0: if ($table = $this->getLogsEntriesTable()) { Chris@0: foreach ($table as $row) { Chris@0: $cells = $row->findAll('css', 'td'); Chris@0: $entries[] = [ Chris@0: 'severity' => $this->getSeverityConstant($row->getAttribute('class')), Chris@0: 'type' => $cells[1]->getText(), Chris@0: 'message' => $cells[3]->getText(), Chris@0: 'user' => $cells[4]->getText(), Chris@0: ]; Chris@0: } Chris@0: } Chris@0: return $entries; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Find the Logs table in the DOM. Chris@0: * Chris@0: * @return \SimpleXMLElement[] Chris@0: * The return value of a xpath search. Chris@0: */ Chris@0: protected function getLogsEntriesTable() { Chris@0: return $this->xpath('.//table[@id="admin-dblog"]/tbody/tr'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the count of database log entries by database log event type. Chris@0: * Chris@0: * @param array $types Chris@0: * The type information to compare against. Chris@0: * Chris@0: * @return array Chris@0: * The count of each type keyed by the key of the $types array. Chris@0: */ Chris@0: protected function getTypeCount(array $types) { Chris@0: $entries = $this->getLogEntries(); Chris@0: $count = array_fill(0, count($types), 0); Chris@0: foreach ($entries as $entry) { Chris@0: foreach ($types as $key => $type) { Chris@0: if ($entry['type'] == $type['type'] && $entry['severity'] == $type['severity']) { Chris@0: $count[$key]++; Chris@0: break; Chris@0: } Chris@0: } Chris@0: } Chris@0: return $count; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the watchdog severity constant corresponding to the CSS class. Chris@0: * Chris@0: * @param string $class Chris@0: * CSS class attribute. Chris@0: * Chris@0: * @return int|null Chris@0: * The watchdog severity constant or NULL if not found. Chris@0: */ Chris@0: protected function getSeverityConstant($class) { Chris@0: $map = array_flip(DbLogController::getLogLevelClassMap()); Chris@0: Chris@0: // Find the class that contains the severity. Chris@0: $classes = explode(' ', $class); Chris@0: foreach ($classes as $class) { Chris@0: if (isset($map[$class])) { Chris@0: return $map[$class]; Chris@0: } Chris@0: } Chris@0: return NULL; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Confirms that a log message appears on the database log overview screen. Chris@0: * Chris@0: * This function should only be used for the admin/reports/dblog page, because Chris@0: * it checks for the message link text truncated to 56 characters. Other log Chris@0: * pages have no detail links so they contain the full message text. Chris@0: * Chris@0: * @param string $log_message Chris@0: * The database log message to check. Chris@0: * @param string $message Chris@0: * The message to pass to simpletest. Chris@0: */ Chris@0: protected function assertLogMessage($log_message, $message) { Chris@0: $message_text = Unicode::truncate(Html::decodeEntities(strip_tags($log_message)), 56, TRUE, TRUE); Chris@0: $this->assertLink($message_text, 0, $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that the details page displays correctly for a temporary user. Chris@0: */ Chris@0: public function testTemporaryUser() { Chris@0: // Create a temporary user. Chris@0: $tempuser = $this->drupalCreateUser(); Chris@0: $tempuser_uid = $tempuser->id(); Chris@0: Chris@0: // Log in as the admin user. Chris@0: $this->drupalLogin($this->adminUser); Chris@0: Chris@0: // Generate a single watchdog entry. Chris@0: $this->generateLogEntries(1, ['user' => $tempuser, 'uid' => $tempuser_uid]); Chris@0: $wid = db_query('SELECT MAX(wid) FROM {watchdog}')->fetchField(); Chris@0: Chris@0: // Check if the full message displays on the details page. Chris@0: $this->drupalGet('admin/reports/dblog/event/' . $wid); Chris@0: $this->assertText('Dblog test log message'); Chris@0: Chris@0: // Delete the user. Chris@0: user_delete($tempuser->id()); Chris@0: $this->drupalGet('user/' . $tempuser_uid); Chris@0: $this->assertResponse(404); Chris@0: Chris@0: // Check if the full message displays on the details page. Chris@0: $this->drupalGet('admin/reports/dblog/event/' . $wid); Chris@0: $this->assertText('Dblog test log message'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Make sure HTML tags are filtered out in the log overview links. Chris@0: */ Chris@0: public function testOverviewLinks() { Chris@0: $this->drupalLogin($this->adminUser); Chris@0: $this->generateLogEntries(1, ['message' => "<script>alert('foo');</script>Lorem ipsum dolor sit amet, consectetur adipiscing & elit."]); Chris@0: $this->drupalGet('admin/reports/dblog'); Chris@0: $this->assertResponse(200); Chris@0: // Make sure HTML tags are filtered out. Chris@0: $this->assertRaw('title="alert('foo');Lorem'); Chris@0: $this->assertNoRaw(""); Chris@0: Chris@0: // Make sure HTML tags are filtered out in admin/reports/dblog/event/ too. Chris@0: $this->generateLogEntries(1, ['message' => " Lorem ipsum"]); Chris@0: $wid = db_query('SELECT MAX(wid) FROM {watchdog}')->fetchField(); Chris@0: $this->drupalGet('admin/reports/dblog/event/' . $wid); Chris@0: $this->assertNoRaw(""); Chris@0: $this->assertRaw("alert('foo'); Lorem ipsum"); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test sorting for entries with the same timestamp. Chris@0: */ Chris@0: public function testSameTimestampEntries() { Chris@0: $this->drupalLogin($this->adminUser); Chris@0: Chris@0: $this->generateLogEntries(1, ['timestamp' => 1498062000, 'type' => 'same_time', 'message' => 'First']); Chris@0: $this->generateLogEntries(1, ['timestamp' => 1498062000, 'type' => 'same_time', 'message' => 'Second']); Chris@0: $this->generateLogEntries(1, ['timestamp' => 1498062000, 'type' => 'same_time', 'message' => 'Third']); Chris@0: Chris@0: $this->drupalGet('admin/reports/dblog'); Chris@0: Chris@0: $entries = $this->getLogEntries(); Chris@0: $this->assertEquals($entries[0]['message'], 'Third Entry #0'); Chris@0: $this->assertEquals($entries[1]['message'], 'Second Entry #0'); Chris@0: $this->assertEquals($entries[2]['message'], 'First Entry #0'); Chris@0: } Chris@0: Chris@0: }