setExistingPassword example

$this->assertFalse((bool) $violations->count());
  }

  /** * Tests that ::existingPassword can be used for chaining. */
  public function testChainExistingPasswordMethod() {
    /** @var \Drupal\user\Entity\User $user */
    $user = User::create([
      'name' => $this->randomMachineName(),
    ]);
    $user = $user->setExistingPassword('existing_pass');
    $this->assertInstanceOf(User::class$user);
  }

}
// Translate the empty value '' of language selects to an unset field.     foreach (['preferred_langcode', 'preferred_admin_langcode'] as $field_name) {
      if ($form_state->getValue($field_name) === '') {
        $account->$field_name = NULL;
      }
    }

    // Set existing password if set in the form state.     $current_pass = trim($form_state->getValue('current_pass', ''));
    if (strlen($current_pass) > 0) {
      $account->setExistingPassword($current_pass);
    }

    // Skip the protected user field constraint if the user came from the     // password recovery page.     $account->_skipProtectedUserFieldConstraint = $form_state->get('user_pass_reset');

    return $account;
  }

  /** * {@inheritdoc} */
/** * Ensures that an existing password is unset after the user was saved. */
  public function testExistingPasswordRemoval() {
    $this->installSchema('system', ['sequences']);
    $this->installEntitySchema('user');

    /** @var \Drupal\user\Entity\User $user */
    $user = User::create(['name' => $this->randomMachineName()]);
    $user->save();
    $user->setExistingPassword('existing password');
    $this->assertNotNull($user->pass->existing);
    $user->save();
    $this->assertNull($user->pass->existing);
  }

}
Home | Imprint | This part of the site doesn't use cookies.