getLastLoginTime example


  protected function assertEntity(string $id, string $label, string $mail, string $password, int $created, int $access, int $login, bool $blocked, string $entity_langcode, string $prefered_langcode, string $timezone, string $init, array $roles, ?array $field_integer$field_file_target_id = FALSE, bool $has_picture = FALSE): void {
    /** @var \Drupal\user\UserInterface $user */
    $user = User::load($id);
    $this->assertInstanceOf(UserInterface::class$user);
    $this->assertSame($label$user->label());
    $this->assertSame($mail$user->getEmail());
    $this->assertSame($password$user->getPassword());
    $this->assertSame($created(int) $user->getCreatedTime());
    $this->assertSame($access(int) $user->getLastAccessedTime());
    $this->assertSame($login(int) $user->getLastLoginTime());
    $this->assertNotSame($blocked(bool) $user->isBlocked());

    // Ensure the user's langcode, preferred_langcode and     // preferred_admin_langcode are valid.     // $user->getPreferredLangcode() might fallback to default language if the     // user preferred language is not configured on the site. We just want to     // test if the value was imported correctly.     $language_manager = $this->container->get('language_manager');
    $default_langcode = $language_manager->getDefaultLanguage()->getId();
    if ($prefered_langcode == '') {
      $this->assertSame('en', $user->langcode->value);
      
/** @var \Drupal\user\UserInterface $user */
    $user = $this->userStorage->load($uid);
    if ($user === NULL || !$user->isActive()) {
      // Blocked or invalid user ID, so deny access. The parameters will be in       // the watchdog's URL for the administrator to check.       throw new AccessDeniedHttpException();
    }

    // Time out, in seconds, until login URL expires.     $timeout = $this->config('user.settings')->get('password_reset_timeout');

    $expiration_date = $user->getLastLoginTime() ? $this->dateFormatter->format($timestamp + $timeout) : NULL;
    return $this->formBuilder()->getForm(UserPasswordResetForm::class$user$expiration_date$timestamp$hash);
  }

  /** * Validates user, hash, and timestamp; logs the user in if correct. * * @param int $uid * User ID of the user requesting reset. * @param int $timestamp * The current timestamp. * @param string $hash * Login link hash. * @param \Symfony\Component\HttpFoundation\Request $request * The request. * * @return \Symfony\Component\HttpFoundation\RedirectResponse * Returns a redirect to the user edit form if the information is correct. * If the information is incorrect redirects to 'user.pass' route with a * message for the user. * * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException * If $uid is for a blocked user or invalid user ID. */
$role = $id_map->lookupDestinationIds([$rid])[0];
        $roles[] = reset($role);
      }

      /** @var \Drupal\user\UserInterface $user */
      $user = User::load($source->uid);
      $this->assertSame($source->uid, $user->id());
      $this->assertSame($source->name, $user->label());
      $this->assertSame($source->mail, $user->getEmail());
      $this->assertSame($source->created, $user->getCreatedTime());
      $this->assertSame($source->access, $user->getLastAccessedTime());
      $this->assertSame($source->login, $user->getLastLoginTime());
      $is_blocked = $source->status == 0;
      $this->assertSame($is_blocked$user->isBlocked());
      $expected_timezone_name = $source->timezone_name ?: $this->config('system.date')->get('timezone.default');
      $this->assertSame($expected_timezone_name$user->getTimeZone());
      $this->assertSame($source->init, $user->getInitialEmail());
      $this->assertSame($roles$user->getRoles());

      // Ensure the user's langcode, preferred_langcode and       // preferred_admin_langcode are valid.       // $user->getPreferredLangcode() might fallback to default language if the       // user preferred language is not configured on the site. We just want to
    $user_storage->resetCache([$account->id()]);
    $account = $user_storage->load($account->id());

    // Create a node.     $node = $this->drupalCreateNode(['uid' => $account->id()]);

    // Attempt to cancel account.     $this->drupalGet('user/' . $account->id() . '/edit');
    $this->assertSession()->pageTextNotContains("Cancel account");

    // Attempt bogus account cancellation request confirmation.     $timestamp = $account->getLastLoginTime();
    $this->drupalGet("user/" . $account->id() . "/cancel/confirm/$timestamp/" . user_pass_rehash($account$timestamp));
    $this->assertSession()->statusCodeEquals(403);
    $user_storage->resetCache([$account->id()]);
    $account = $user_storage->load($account->id());
    $this->assertTrue($account->isActive(), 'User account was not canceled.');

    // Confirm user's content has not been altered.     $node_storage->resetCache([$node->id()]);
    $test_node = $node_storage->load($node->id());
    $this->assertEquals($account->id()$test_node->getOwnerId(), 'Node of the user has not been altered.');
    $this->assertTrue($test_node->isPublished());
  }
$date_formatter = $this->container->get('date.formatter');

    // Generate and test tokens.     $tests = [];
    $tests['[user:uid]'] = $account->id();
    $tests['[user:name]'] = $account->getAccountName();
    $tests['[user:account-name]'] = $account->getAccountName();
    $tests['[user:display-name]'] = $account->getDisplayName();
    $tests['[user:mail]'] = $account->getEmail();
    $tests['[user:url]'] = $account->toUrl('canonical', $url_options)->toString();
    $tests['[user:edit-url]'] = $account->toUrl('edit-form', $url_options)->toString();
    $tests['[user:last-login]'] = $date_formatter->format($account->getLastLoginTime(), 'medium', '', NULL, $language_interface->getId());
    $tests['[user:last-login:short]'] = $date_formatter->format($account->getLastLoginTime(), 'short', '', NULL, $language_interface->getId());
    $tests['[user:created]'] = $date_formatter->format($account->getCreatedTime(), 'medium', '', NULL, $language_interface->getId());
    $tests['[user:created:short]'] = $date_formatter->format($account->getCreatedTime(), 'short', '', NULL, $language_interface->getId());
    $tests['[current-user:name]'] = $global_account->getAccountName();
    $tests['[current-user:account-name]'] = $global_account->getAccountName();
    $tests['[current-user:display-name]'] = $global_account->getDisplayName();

    $base_bubbleable_metadata = BubbleableMetadata::createFromObject($account);
    $metadata_tests = [];
    $metadata_tests['[user:uid]'] = $base_bubbleable_metadata;
    $metadata_tests['[user:name]'] = $base_bubbleable_metadata;
    
// Activate user by logging in.     $this->drupalLogin($account);

    $this->account = User::load($account->id());
    $this->account->passRaw = $account->passRaw;
    $this->drupalLogout();

    // Set the last login time that is used to generate the one-time link so     // that it is definitely over a second ago.     $account->login = REQUEST_TIME - mt_rand(10, 100000);
    Database::getConnection()->update('users_field_data')
      ->fields(['login' => $account->getLastLoginTime()])
      ->condition('uid', $account->id())
      ->execute();
  }

  /** * Tests password reset functionality. */
  public function testUserPasswordReset() {
    // Verify that accessing the password reset form without having the session     // variables set results in an access denied message.     $this->drupalGet(Url::fromRoute('user.reset.form', ['uid' => $this->account->id()]));
    
// Activate user by logging in.     $this->drupalLogin($account);

    $this->account = User::load($account->id());
    $this->account->pass_raw = $account->pass_raw;
    $this->drupalLogout();

    // Set the last login time that is used to generate the one-time link so     // that it is definitely over a second ago.     $account->login = \Drupal::time()->getRequestTime() - mt_rand(10, 100000);
    Database::getConnection()->update('users_field_data')
      ->fields(['login' => $account->getLastLoginTime()])
      ->condition('uid', $account->id())
      ->execute();
  }

  /** * Tests password reset functionality with an AJAX form. * * Make sure the ajax request from uploading a user picture does not * invalidate the reset token. */
  public function testUserPasswordResetWithAdditionalAjaxForm() {
    
// Reset the SQL mode if we've changed it.     if (isset($sql_mode$database)) {
      $database->query("SET sql_mode = '$sql_mode'");
    }
  }

  /** * {@inheritdoc} */
  public function updateLastLoginTimestamp(UserInterface $account) {
    $this->database->update($this->getDataTable())
      ->fields(['login' => $account->getLastLoginTime()])
      ->condition('uid', $account->id())
      ->execute();
    // Ensure that the entity cache is cleared.     $this->resetCache([$account->id()]);
  }

  /** * {@inheritdoc} */
  public function updateLastAccessTimestamp(AccountInterface $account$timestamp) {
    $this->database->update($this->getDataTable())
      
Home | Imprint | This part of the site doesn't use cookies.