You are here

private function MongoDBLogTestCase::doUser in MongoDB 8

Same name and namespace in other branches
  1. 6 mongodb_watchdog/mongodb_watchdog.test \MongoDBLogTestCase::doUser()
  2. 7 mongodb_watchdog/mongodb_watchdog.test \MongoDBLogTestCase::doUser()

Generate and verify user events.

1 call to MongoDBLogTestCase::doUser()
MongoDBLogTestCase::verifyEvents in mongodb_watchdog/mongodb_watchdog.test
Verify events.

File

mongodb_watchdog/mongodb_watchdog.test, line 329
Test class for MongoDB_watchdog.

Class

MongoDBLogTestCase
Test the behaviour of watchdog() mongodb_watchdog, not dblog

Code

private function doUser() {

  // Set user variables.
  $name = $this
    ->randomName();
  $pass = user_password();

  // Add user using form to generate add user event (which is not triggered by drupalCreateUser).
  $edit = array();
  $edit['name'] = $name;
  $edit['mail'] = $name . '@example.com';
  $edit['pass[pass1]'] = $pass;
  $edit['pass[pass2]'] = $pass;
  $edit['status'] = 1;
  $this
    ->drupalPost('admin/people/create', $edit, t('Create new account'));
  $this
    ->assertResponse(200);

  // Retrieve user object.
  $user = user_load_by_name($name);
  $this
    ->assertTrue($user != NULL, t('User @name was loaded', array(
    '@name' => $name,
  )));
  $user->pass_raw = $pass;

  // Needed by drupalLogin.
  // Login user.
  $this
    ->drupalLogin($user);

  // Logout user.
  $this
    ->drupalLogout();

  // Fetch row ids in watchdog that relate to the user.
  $collection = mongodb_collection(variable_get('mongodb_watchdog', 'watchdog'));
  $query = array(
    'uid' => $user->uid,
  );
  $result = $collection
    ->find($query, array(
    '_id' => 1,
  ));
  foreach ($result as $row) {
    $ids[] = $row['_id'];
  }
  $count_before = isset($ids) ? count($ids) : 0;
  $this
    ->assertTrue($count_before > 0, t('DBLog contains @count records for @name', array(
    '@count' => $count_before,
    '@name' => $user->name,
  )));

  // Login the admin user.
  $this
    ->drupalLogin($this->big_user);

  // On D6, delete user. On D7, cancel it.
  // We need to POST here to invoke batch_process() in the internal browser.
  $this
    ->drupalPost('user/' . $user->uid . '/cancel', array(
    'user_cancel_method' => 'user_cancel_reassign',
  ), t('Cancel account'));

  // Count rows that have uids for the user.
  $count = $collection
    ->find($query)
    ->count();
  $this
    ->assertTrue($count == 0, t('DBLog contains @count records for @name', array(
    '@count' => $count,
    '@name' => $user->name,
  )));

  // Count rows in watchdog that previously related to the deleted user.
  if (!empty($ids)) {
    $query['_id']['$in'] = $ids;
  }
  $count_after = $collection
    ->find($query)
    ->count();
  $this
    ->assertTrue($count_after == $count_before, t('DBLog contains @count records for @name that now have uid = 0', array(
    '@count' => $count_before,
    '@name' => $user->name,
  )));
  unset($ids);

  // Fetch row ids in watchdog that relate to the user.
  $result = $collection
    ->find($query, array(
    '_id' => 1,
  ));
  foreach ($result as $row) {
    $ids[] = $row->wid;
  }
  $this
    ->assertTrue(!isset($ids), t('DBLog contains no records for @name', array(
    '@name' => $user->name,
  )));

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

  // Verify events were recorded.
  // Add user.
  // Default display includes name and email address; if too long then email is replaced by three periods.
  // $this->assertRaw(t('New user: %name (%mail)', array('%name' => $edit['name'], '%mail' => $edit['mail'])), t('DBLog event was recorded: [add user]'));
  $this
    ->assertRaw(t('New user: %name', array(
    '%name' => $name,
  )), t('DBLog event was recorded: [add user]'));

  // Login user.
  $this
    ->assertRaw(t('Session opened for %name', array(
    '%name' => $name,
  )), t('DBLog event was recorded: [login user]'));

  // Logout user.
  $this
    ->assertRaw(t('Session closed for %name', array(
    '%name' => $name,
  )), t('DBLog event was recorded: [logout user]'));

  // Delete user.
  $this
    ->assertRaw(t('Deleted user: %name', array(
    '%name' => $name,
  )), t('DBLog event was recorded: [delete user]'));
}