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();
    
    $this
      ->bakeCookie(new ChocolateChip($account
      ->getAccountName() . 'bad', $account
      ->getEmail(), $account
      ->getInitialEmail(), '1'));
    $this
      ->drupalGet('');
    $assert_session
      ->responseNotContains($account
      ->getAccountName());
    
    $this
      ->bakeCookie(new ChocolateChip($account
      ->getAccountName(), 'bad' . $account
      ->getEmail(), $account
      ->getInitialEmail(), '1'));
    $this
      ->drupalGet('');
    $assert_session
      ->responseNotContains($account
      ->getAccountName());
    
    $this
      ->bakeCookie(new ChocolateChip($account
      ->getAccountName(), $account
      ->getEmail(), $account
      ->getInitialEmail(), '1'));
    $this
      ->drupalGet('');
    $assert_session
      ->responseContains($account
      ->getAccountName());
    
    $this
      ->eatCookie(ChocolateChip::getName());
    $this
      ->drupalGet('');
    $assert_session
      ->responseNotContains($account
      ->getAccountName());
    
    $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();
    
    $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';
    
    $this
      ->bakeCookie(new ChocolateChip('tester', 'test@example.com', $valid_init, '1'));
    $this
      ->drupalGet('');
    $this
      ->drupalGet('');
    $assert_session
      ->responseContains('Tester');
    
    $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(),
    ]));
  }
}