function DbLogTest::doUser

Same name and namespace in other branches
  1. 9 core/modules/dblog/tests/src/Functional/DbLogTest.php \Drupal\Tests\dblog\Functional\DbLogTest::doUser()
  2. 8.9.x core/modules/dblog/tests/src/Functional/DbLogTest.php \Drupal\Tests\dblog\Functional\DbLogTest::doUser()
  3. 11.x core/modules/dblog/tests/src/Functional/DbLogTest.php \Drupal\Tests\dblog\Functional\DbLogTest::doUser()

Generates and then verifies some user events.

1 call to DbLogTest::doUser()
DbLogTest::verifyEvents in core/modules/dblog/tests/src/Functional/DbLogTest.php
Generates and then verifies various types of events.

File

core/modules/dblog/tests/src/Functional/DbLogTest.php, line 466

Class

DbLogTest
Verifies log entries and user access based on permissions.

Namespace

Drupal\Tests\dblog\Functional

Code

private function doUser() {
  // Set user variables.
  $name = $this->randomMachineName();
  $pass = \Drupal::service('password_generator')->generate();
  // Add a user using the form to generate an add user event (which is not
  // triggered by drupalCreateUser).
  $edit = [];
  $edit['name'] = $name;
  $edit['mail'] = $name . '@example.com';
  $edit['pass[pass1]'] = $pass;
  $edit['pass[pass2]'] = $pass;
  $edit['status'] = 1;
  $this->drupalGet('admin/people/create');
  $this->submitForm($edit, 'Create new account');
  $this->assertSession()
    ->statusCodeEquals(200);
  // Retrieve the user object.
  $user = user_load_by_name($name);
  $this->assertNotNull($user, "User {$name} was loaded");
  // pass_raw property is needed by drupalLogin.
  $user->passRaw = $pass;
  // Log in user.
  $this->drupalLogin($user);
  // Log out user.
  $this->drupalLogout();
  // Fetch the row IDs in watchdog that relate to the user.
  $result = Database::getConnection()->select('watchdog', 'w')
    ->fields('w', [
    'wid',
  ])
    ->condition('uid', $user->id())
    ->execute();
  foreach ($result as $row) {
    $ids[] = $row->wid;
  }
  $count_before = isset($ids) ? count($ids) : 0;
  $this->assertGreaterThan(0, $count_before, "DBLog contains {$count_before} records for {$user->getAccountName()}");
  // Log in the admin user.
  $this->drupalLogin($this->adminUser);
  // Delete the user created at the start of this test.
  // We need to POST here to invoke batch_process() in the internal browser.
  $this->drupalGet('user/' . $user->id() . '/cancel');
  $this->submitForm([
    'user_cancel_method' => 'user_cancel_reassign',
  ], 'Confirm');
  // View the database log report.
  $this->drupalGet('admin/reports/dblog');
  $this->assertSession()
    ->statusCodeEquals(200);
  // 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("New user: {$name} <{$user->getEmail()}>.", 'DBLog event was recorded: [add user]');
  // Log in user.
  $this->assertLogMessage("Session opened for {$name}.", 'DBLog event was recorded: [login user]');
  // Log out user.
  $this->assertLogMessage("Session closed for {$name}.", 'DBLog event was recorded: [logout user]');
  // Delete user.
  $message = "Deleted user: {$name} <{$user->getEmail()}>.";
  $message_text = Unicode::truncate($message, 56, TRUE, TRUE);
  // Verify that the full message displays on the details page.
  $link = FALSE;
  if ($links = $this->xpath('//a[text()="' . $message_text . '"]')) {
    // Found link with the message text.
    $links = array_shift($links);
    $value = $links->getAttribute('href');
    // Extract link to details page.
    $link = mb_substr($value, strpos($value, 'admin/reports/dblog/event/'));
    $this->drupalGet($link);
    // Check for full message text on the details page.
    $this->assertSession()
      ->pageTextContains($message);
  }
  $this->assertNotEmpty($link, 'DBLog event was recorded: [delete user]');
  // Visit random URL (to generate page not found event).
  $not_found_url = $this->randomMachineName(60);
  $this->drupalGet($not_found_url);
  $this->assertSession()
    ->statusCodeEquals(404);
  // View the database log page-not-found report page.
  $this->drupalGet('admin/reports/page-not-found');
  $this->assertSession()
    ->statusCodeEquals(200);
  // Check that full-length URL displayed.
  $this->assertSession()
    ->pageTextContains($not_found_url);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.