You are here

ChildLoginTest.php in Bakery Single Sign-On System 8.2

File

tests/src/Functional/ChildLoginTest.php
View source
<?php

namespace Drupal\Tests\bakery\Functional;

use Drupal\bakery\Cookies\ChocolateChip;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Url;
use Drupal\user\Entity\User;
class ChildLoginTest extends ChildSiteTestBase {
  protected $defaultTheme = 'stark';
  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());
  }
  public function testLogout() {
    $account = $this
      ->createUser([
      'access user profiles',
    ]);
    $assert_session = $this
      ->assertSession();

    // 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());
    $this
      ->drupalLogout();
    $assert_session
      ->responseNotContains($account
      ->getAccountName());
  }
  public function ttestAccountCreation() {
    $this
      ->markTestIncomplete('This can not currently work during a request because it can not talk to a main site.');
    $assert_session = $this
      ->assertSession();
    $valid_init = 'drupal.org/user/123/edit';

    // Miss-matched username should not log in.
    $this
      ->bakeCookie(new ChocolateChip('tester', 'test@example.com', $valid_init, '1'));
    $this
      ->drupalGet('');
    $this
      ->drupalGet('');
    $assert_session
      ->responseContains('Tester');

    /** @var \Drupal\user\UserStorageInterface $user_storage */
    $user_storage = $this->container
      ->get('entity_type.manager')
      ->getStorage('user');
    $user = $user_storage
      ->loadByProperties([
      'name' => 'tester',
    ]);
    $this
      ->assertEquals('test@example.com', $user[0]
      ->getEamil());
    $this
      ->assertEquals($valid_init, $user[0]
      ->getInitialEmail());
  }
  public function testLoginPage() {
    $this
      ->drupalGet('user');
    $assert_session = $this
      ->assertSession();
    $assert_session
      ->fieldNotExists('name');
    $assert_session
      ->fieldNotExists('pass');
    $assert_session
      ->buttonNotExists('edit-submit');
    $assert_session
      ->linkByHrefExists('https://example.com/user?' . UrlHelper::buildQuery([
      'bd' => Url::fromRoute('user.login')
        ->setAbsolute(TRUE)
        ->toString(),
    ]));
  }

}

Classes

Namesort descending Description
ChildLoginTest