You are here

public function ChildLoginTest::testChocolateChipLogin in Bakery Single Sign-On System 8.2

File

tests/src/Functional/ChildLoginTest.php, line 14

Class

ChildLoginTest

Namespace

Drupal\Tests\bakery\Functional

Code

public function testChocolateChipLogin() {
  $valid_init = 'drupal.org/user/123/edit';
  $account = $this
    ->createUser([
    'access user profiles',
  ]);
  $assert_session = $this
    ->assertSession();

  // Miss-matched username should not log in.
  $this
    ->bakeCookie(new ChocolateChip($account
    ->getAccountName() . 'bad', $account
    ->getEmail(), $account
    ->getInitialEmail(), '1'));
  $this
    ->drupalGet('');
  $assert_session
    ->responseNotContains($account
    ->getAccountName());

  // Miss-matched email shouldn't log in.
  $this
    ->bakeCookie(new ChocolateChip($account
    ->getAccountName(), 'bad' . $account
    ->getEmail(), $account
    ->getInitialEmail(), '1'));
  $this
    ->drupalGet('');
  $assert_session
    ->responseNotContains($account
    ->getAccountName());

  // Should child cookies log users in? I think not but it seems to.
  // $cookie = new ChocolateChip($account->getAccountName(), $account->getEmail(), $account->getInitialEmail(), '0');
  // $this->getSession()->setCookie($cookie::getName(), $kitchen->bakeData($cookie));
  // $this->drupalGet('');
  // $session->responseNotContains($account->getAccountName());
  // Set a cookie signed by the main site and matching a user. Should log in.
  $this
    ->bakeCookie(new ChocolateChip($account
    ->getAccountName(), $account
    ->getEmail(), $account
    ->getInitialEmail(), '1'));
  $this
    ->drupalGet('');
  $assert_session
    ->responseContains($account
    ->getAccountName());

  //  Removing the chocolate chip cookie should boot the user.
  $this
    ->eatCookie(ChocolateChip::getName());
  $this
    ->drupalGet('');
  $assert_session
    ->responseNotContains($account
    ->getAccountName());

  // See if a mangled init field is repaired on bakery login.
  $account
    ->set('init', 'bakery_temp/' . mt_rand())
    ->save();
  $this
    ->bakeCookie(new ChocolateChip($account
    ->getAccountName(), $account
    ->getEmail(), $valid_init, '1'));
  $this
    ->drupalGet('');
  $assert_session
    ->responseContains($account
    ->getAccountName());
  $account = User::load($account
    ->id());
  $this
    ->assertEquals($valid_init, $account
    ->getInitialEmail());
}