View source
<?php
namespace Drupal\Tests\email_registration\Functional\Plugin\Commerce\CheckoutPane;
use Drupal\commerce_product\Entity\ProductInterface;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Session\AccountInterface;
use Drupal\Tests\commerce\Functional\CommerceBrowserTestBase;
class EmailRegistrationLoginTest extends CommerceBrowserTestBase {
protected $product;
public static $modules = [
'commerce_product',
'commerce_order',
'commerce_cart',
'commerce_checkout',
'email_registration',
];
protected function setUp() {
parent::setUp();
$this
->placeBlock('commerce_cart');
$this
->placeBlock('commerce_checkout_progress');
$variation = $this
->createEntity('commerce_product_variation', [
'type' => 'default',
'sku' => strtolower($this
->randomMachineName()),
'price' => [
'number' => 9.99,
'currency_code' => 'USD',
],
]);
$this->product = $this
->createEntity('commerce_product', [
'type' => 'default',
'title' => 'My product',
'variations' => [
$variation,
],
'stores' => [
$this->store,
],
]);
$checkout_flow = $this->container
->get('entity_type.manager')
->getStorage('commerce_checkout_flow')
->load('default');
$checkout_flow_plugin = $checkout_flow
->getPlugin();
$er_login_pane = $checkout_flow_plugin
->getPane('email_registration_login');
$er_login_pane
->setConfiguration([]);
$er_login_pane
->setStepId('login');
$login_pane = $checkout_flow_plugin
->getPane('login');
$login_pane
->setStepId('_disabled');
$checkout_flow_plugin_configuration = $checkout_flow_plugin
->getConfiguration();
$checkout_flow_plugin_configuration['panes']['email_registration_login'] = $er_login_pane
->getConfiguration();
$checkout_flow_plugin_configuration['panes']['login'] = $login_pane
->getConfiguration();
$checkout_flow_plugin
->setConfiguration($checkout_flow_plugin_configuration);
$checkout_flow
->save();
$this
->drupalLogout();
$this
->addProductToCart($this->product);
}
public function testLoginWithMailAddress() {
$account = $this
->drupalCreateUser();
$this
->goToCheckout();
$this
->assertCheckoutProgressStep('Login');
$edit = [
'email_registration_login[returning_customer][name]' => $account
->getEmail(),
'email_registration_login[returning_customer][password]' => $account->passRaw,
];
$this
->submitForm($edit, 'Log in');
$this
->assertCheckoutProgressStep('Order information');
$this
->assertLoggedIn($account);
}
public function testLoginWithUsername() {
$this
->config('email_registration.settings')
->set('login_with_username', TRUE)
->save();
$account = $this
->drupalCreateUser();
$this
->goToCheckout();
$this
->assertCheckoutProgressStep('Login');
$this
->assertSession()
->pageTextContains('Email address or username');
$edit = [
'email_registration_login[returning_customer][name]' => $account
->getAccountName(),
'email_registration_login[returning_customer][password]' => $account->passRaw,
];
$this
->submitForm($edit, 'Log in');
$this
->assertCheckoutProgressStep('Order information');
$this
->assertLoggedIn($account);
}
public function testLoginWithoutValues() {
$this
->goToCheckout();
$this
->assertCheckoutProgressStep('Login');
$this
->submitForm([], 'Log in');
$this
->assertSession()
->pageTextContains('Unrecognized email address or password. Forgot your password?');
}
public function testFailedLogin() {
$account = $this
->drupalCreateUser();
$this
->goToCheckout();
$this
->assertCheckoutProgressStep('Login');
$edit = [
'email_registration_login[returning_customer][name]' => $account
->getEmail(),
'email_registration_login[returning_customer][password]' => $account->passRaw . 'foo',
];
$this
->submitForm($edit, 'Log in');
$this
->assertSession()
->pageTextContains('Unrecognized email address or password. Forgot your password?');
}
public function testFailedLoginWithUsername() {
$account = $this
->drupalCreateUser();
$this
->config('email_registration.settings')
->set('login_with_username', FALSE)
->save();
$this
->goToCheckout();
$this
->assertCheckoutProgressStep('Login');
$this
->assertSession()
->pageTextContains('Enter your email address.');
$edit = [
'email_registration_login[returning_customer][name]' => $account
->getAccountName(),
'email_registration_login[returning_customer][password]' => $account->passRaw,
];
$this
->submitForm($edit, 'Log in');
$this
->assertSession()
->pageTextContains('Unrecognized email address or password. Forgot your password?');
}
public function testFailedLoginWithMailAddressWhenUsernameIsAllowed() {
$account = $this
->drupalCreateUser();
$this
->config('email_registration.settings')
->set('login_with_username', TRUE)
->save();
$this
->goToCheckout();
$this
->assertCheckoutProgressStep('Login');
$edit = [
'email_registration_login[returning_customer][name]' => $account
->getEmail(),
'email_registration_login[returning_customer][password]' => $account->passRaw . 'foo',
];
$this
->submitForm($edit, 'Log in');
$this
->assertSession()
->pageTextContains('Unrecognized username, email, or password. Have you forgotten your password?');
}
public function testFailedLoginWithMailAddressWithBlockedUser() {
$account = $this
->drupalCreateUser();
$account->status = FALSE;
$account
->save();
$this
->goToCheckout();
$this
->assertCheckoutProgressStep('Login');
$edit = [
'email_registration_login[returning_customer][name]' => $account
->getEmail(),
'email_registration_login[returning_customer][password]' => $account->passRaw . 'foo',
];
$this
->submitForm($edit, 'Log in');
$this
->assertSession()
->pageTextContains('The account with email address ' . $account
->getEmail() . ' has not been activated or is blocked.');
$edit = [
'email_registration_login[returning_customer][name]' => $account
->getEmail(),
'email_registration_login[returning_customer][password]' => $account->passRaw,
];
$this
->submitForm($edit, 'Log in');
$this
->assertSession()
->pageTextContains('The account with email address ' . $account
->getEmail() . ' has not been activated or is blocked.');
}
protected function drupalLogin(AccountInterface $account) {
if ($this->loggedInUser) {
$this
->drupalLogout();
}
$this
->drupalGet('user/login');
$this
->submitForm([
'mail' => $account
->getEmail(),
'pass' => $account->passRaw,
], t('Log in'));
$this
->assertLoggedIn($account);
}
protected function addProductToCart(ProductInterface $product) {
$this
->drupalGet($product
->toUrl()
->toString());
$this
->submitForm([], 'Add to cart');
}
protected function goToCheckout() {
$cart_link = $this
->getSession()
->getPage()
->findLink('your cart');
$cart_link
->click();
$this
->submitForm([], 'Checkout');
}
protected function assertCheckoutProgressStep($expected) {
$current_step = $this
->getSession()
->getPage()
->find('css', '.checkout-progress--step__current')
->getText();
$this
->assertEquals($expected, $current_step);
}
protected function assertLoggedIn(AccountInterface $account) {
$account->sessionId = $this
->getSession()
->getCookie(\Drupal::service('session_configuration')
->getOptions(\Drupal::request())['name']);
$this
->assertTrue($this
->drupalUserIsLoggedIn($account), new FormattableMarkup('User %name successfully logged in.', [
'%name' => $account
->getAccountName(),
]));
$this->loggedInUser = $account;
$this->container
->get('current_user')
->setAccount($account);
}
}