Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests for User #10

Open
wants to merge 8 commits into
base: 8.x-2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 34 additions & 16 deletions src/User/UserAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function authenticateUser($name, $email, $provider_user_id, $token, $pict
if ($user_id === FALSE) {
$this->associateNewProvider($provider_user_id, $token, $data);

return $this->response;
return $this->getResponse();
}
// User is authenticated and provider is already associated.
else {
Expand All @@ -138,14 +138,14 @@ public function authenticateUser($name, $email, $provider_user_id, $token, $pict
if ($user_id) {
$this->authenticateWithProvider($user_id);

return $this->response;
return $this->getResponse();
}

// Try to authenticate user using email address.
if ($email) {
// If authentication with email was successful.
if ($this->authenticateWithEmail($email, $provider_user_id, $token, $data)) {
return $this->response;
return $this->getResponse();
}
}

Expand All @@ -156,7 +156,7 @@ public function authenticateUser($name, $email, $provider_user_id, $token, $pict

$this->authenticateNewUser($drupal_user);

return $this->response;
return $this->getResponse();
}

/**
Expand All @@ -171,13 +171,13 @@ public function authenticateUser($name, $email, $provider_user_id, $token, $pict
*/
public function associateNewProvider($provider_user_id, $token, $data) {
if ($this->userManager->addUserRecord($this->currentUser->id(), $provider_user_id, $token, $data)) {
$this->response = $this->getPostLoginRedirection();
$this->setResponse($this->getPostLoginRedirection());

return;
}

$this->messenger->addError($this->t('New provider could not be associated.'));
$this->response = $this->getLoginFormRedirection();
$this->setResponse($this->getLoginFormRedirection());
}

/**
Expand Down Expand Up @@ -266,7 +266,7 @@ public function authenticateExistingUser(UserInterface $drupal_user) {
$this->nullifySessionKeys();
$this->messenger->addError($this->t('Authentication for Admin (user 1) is disabled.'));

$this->response = $this->getLoginFormRedirection();
$this->setResponse($this->getLoginFormRedirection());

return;
}
Expand All @@ -276,20 +276,20 @@ public function authenticateExistingUser(UserInterface $drupal_user) {
if ($disabled_role) {
$this->messenger->addError($this->t("Authentication for '@role' role is disabled.", ['@role' => $disabled_role]));

$this->response = $this->getLoginFormRedirection();
$this->setResponse($this->getLoginFormRedirection());

return;
}

// If user could be logged in.
if ($this->loginUser($drupal_user)) {
$this->response = $this->getPostLoginRedirection();
$this->setResponse($this->getPostLoginRedirection());
}
else {
$this->nullifySessionKeys();
$this->messenger->addError($this->t('Your account has not been approved yet or might have been canceled, please contact the administrator.'));

$this->response = $this->getLoginFormRedirection();
$this->setResponse($this->getLoginFormRedirection());
}
}

Expand All @@ -309,7 +309,7 @@ public function authenticateNewUser(UserInterface $drupal_user = NULL) {
$this->messenger->addWarning($this->t("Your account was created, but it needs administrator's approval."));
$this->nullifySessionKeys();

$this->response = $this->getLoginFormRedirection();
$this->setResponse($this->getLoginFormRedirection());

return;
}
Expand All @@ -320,12 +320,11 @@ public function authenticateNewUser(UserInterface $drupal_user = NULL) {
$redirect = $this->redirectToUserForm($drupal_user);

if ($redirect) {
$this->response = $redirect;
$this->setResponse($redirect);

return;
}

$this->response = $this->getPostLoginRedirection();
$this->setResponse($this->getPostLoginRedirection());

return;
}
Expand All @@ -336,8 +335,7 @@ public function authenticateNewUser(UserInterface $drupal_user = NULL) {
}

$this->nullifySessionKeys();

$this->response = $this->getLoginFormRedirection();
$this->setResponse($this->getLoginFormRedirection());
}

/**
Expand Down Expand Up @@ -440,4 +438,24 @@ public function dispatchBeforeRedirect($destination = NULL) {
$this->eventDispatcher->dispatch(SocialAuthEvents::BEFORE_REDIRECT, $event);
}

/**
* Gets the response.
*
* @return string
* Response
*/
public function getResponse() {
return $this->response;
}

/**
* Sets the response.
*
* @var string
* Response
*/
public function setResponse($new_response) {
$this->response = $new_response;
}

}
Loading