1 dblog.test private DBLogTestCase::doUser()

Generates and then verifies some user events.

File

core/modules/dblog/tests/dblog.test, line 247
Tests for dblog.module.

Class

DBLogTestCase
Tests logging messages to the database.

Code

private function doUser() {
  // Set user variables.
  $name = $this->randomName();
  $pass = user_password();
  // Add a user using the form to generate an add user event (which is not
  // triggered by backdropCreateUser).
  $edit = array();
  $edit['name'] = $name;
  $edit['mail'] = $name . '@example.com';
  $edit['pass'] = $pass;
  $edit['status'] = 1;
  $edit['notify'] = FALSE;
  $this->backdropPost('admin/people/create', $edit, t('Create new account'));
  $new_user_time = time();
  $this->assertResponse(200);
  // Retrieve the user object.
  $user = user_load_by_name($name);
  $this->assertTrue($user != NULL, format_string('User @name was loaded', array('@name' => $name)));
  // pass_raw property is needed by backdropLogin.
  $user->pass_raw = $pass;
  // Login user.
  $this->backdropLogin($user);
  $login_time = time();
  // Logout user.
  $this->backdropLogout();
  $logout_time = time();
  // Fetch the row IDs in watchdog that relate to the user.
  $result = db_query('SELECT wid FROM {watchdog} WHERE uid = :uid', array(':uid' => $user->uid));
  foreach ($result as $row) {
    $ids[] = $row->wid;
  }
  $count_before = (isset($ids)) ? count($ids) : 0;
  $this->assertTrue($count_before > 0, format_string('DBLog contains @count records for @name', array('@count' => $count_before, '@name' => $user->name)));

  // Login the admin user.
  $this->backdropLogin($this->big_user);
  // Delete the user created at the start of this test.
  // We need to POST here to invoke batch_process() in the internal browser.
  $this->backdropPost('user/' . $user->uid . '/cancel', array('user_cancel_method' => 'user_cancel_reassign'), t('Cancel account'));

  // View the database log report.
  $this->backdropGet('admin/reports/dblog');
  $this->assertResponse(200);

  // Timezone for all dates is from the logged in user.
  $admin_timezone = $this->big_user->timezone;

  // Verify that the expected events were recorded.
  // Add user.
  // Default display includes name and email address; if too long, the email
  // address is replaced by three periods.
  $this->assertLogMessage(t('New user: %name (%email).', array('%name' => $name, '%email' => $user->mail)), 'DBLog event was recorded: [add user]');
  $this->assertText(format_date($new_user_time, 'custom', 'M d, Y - j:ia', $admin_timezone));
  // Login user.
  $this->assertLogMessage(t('Session opened for %name.', array('%name' => $name)), 'DBLog event was recorded: [login user]');
  $this->assertText(format_date($login_time, 'custom', 'M d, Y - j:ia', $admin_timezone));
  // Logout user.
  $this->assertLogMessage(t('Session closed for %name.', array('%name' => $name)), 'DBLog event was recorded: [logout user]');
  $this->assertText(format_date($logout_time, 'custom', 'M d, Y - j:ia', $admin_timezone));
  // 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 that the full message displays on the 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 = backdrop_substr($value, strpos($value, 'admin/reports/event/'));
        $this->backdropGet($link);
        // Check for full message text on the details page.
        $this->assertRaw($message, 'DBLog event details was found: [delete user]');
        break;
      }
    }
  }
  $this->assertTrue($link, 'DBLog event was recorded: [delete user]');
  // Visit random URL (to generate page not found event).
  $not_found_url = $this->randomName(60);
  $this->backdropGet($not_found_url);
  $this->assertResponse(404);
  // View the database log page-not-found report page.
  $this->backdropGet('admin/reports/page-not-found');
  $this->assertResponse(200);
  // Check that full-length URL displayed.
  $this->assertText($not_found_url, 'DBLog event was recorded: [page not found]');
}