You are here

public function CheckoutOrderTest::testRegistrationAfterGuestOrderCheckoutWithCustomUserFields in Commerce Core 8.2

Tests custom user fields are respected on registration after checkout.

File

modules/checkout/tests/src/Functional/CheckoutOrderTest.php, line 562

Class

CheckoutOrderTest
Tests the checkout of an order.

Namespace

Drupal\Tests\commerce_checkout\Functional

Code

public function testRegistrationAfterGuestOrderCheckoutWithCustomUserFields() {

  // Create a field on 'user' entity type.
  $field_storage = FieldStorageConfig::create([
    'field_name' => 'test_user_field',
    'entity_type' => 'user',
    'type' => 'string',
    'cardinality' => 1,
  ]);
  $field_storage
    ->save();
  $field = FieldConfig::create([
    'field_storage' => $field_storage,
    'label' => 'Custom user field',
    'bundle' => 'user',
    'required' => TRUE,
  ]);
  $field
    ->save();
  $form_display = commerce_get_entity_display('user', 'user', 'form');
  $form_display
    ->setComponent('test_user_field', [
    'type' => 'string_textfield',
  ]);
  $form_display
    ->save();
  $this
    ->drupalLogout();
  $this
    ->drupalGet($this->product
    ->toUrl());
  $this
    ->submitForm([], 'Add to cart');
  $cart_link = $this
    ->getSession()
    ->getPage()
    ->findLink('your cart');
  $cart_link
    ->click();
  $this
    ->submitForm([], 'Checkout');

  // Checkout as guest.
  $this
    ->assertCheckoutProgressStep('Login');
  $this
    ->submitForm([], 'Continue as Guest');
  $this
    ->assertCheckoutProgressStep('Order information');
  $this
    ->submitForm([
    'contact_information[email]' => 'guest@example.com',
    'contact_information[email_confirm]' => 'guest@example.com',
    'billing_information[profile][address][0][address][given_name]' => $this
      ->randomString(),
    'billing_information[profile][address][0][address][family_name]' => $this
      ->randomString(),
    'billing_information[profile][address][0][address][organization]' => $this
      ->randomString(),
    'billing_information[profile][address][0][address][address_line1]' => $this
      ->randomString(),
    'billing_information[profile][address][0][address][postal_code]' => '94043',
    'billing_information[profile][address][0][address][locality]' => 'Mountain View',
    'billing_information[profile][address][0][address][administrative_area]' => 'CA',
  ], 'Continue to review');
  $this
    ->assertCheckoutProgressStep('Review');
  $this
    ->assertSession()
    ->pageTextContains('Contact information');
  $this
    ->assertSession()
    ->pageTextContains('Billing information');
  $this
    ->assertSession()
    ->pageTextContains('Order Summary');
  $this
    ->submitForm([], 'Complete checkout');
  $this
    ->assertSession()
    ->pageTextContains('Your order number is 1. You can view your order on your account page when logged in.');
  $this
    ->assertSession()
    ->pageTextContains('Create your account');
  $this
    ->submitForm([
    'completion_register[name]' => 'User name',
    'completion_register[pass][pass1]' => 'pass',
    'completion_register[pass][pass2]' => 'pass',
  ], 'Create account');
  $this
    ->assertSession()
    ->pageTextNotContains('Registration successful. You are now logged in.');
  $this
    ->assertSession()
    ->pageTextContains('Custom user field field is required.');
  $this
    ->submitForm([
    'completion_register[name]' => 'User name',
    'completion_register[pass][pass1]' => 'pass',
    'completion_register[pass][pass2]' => 'pass',
    'completion_register[test_user_field][0][value]' => 'test_user_field_value',
  ], 'Create account');
  $this
    ->assertSession()
    ->pageTextContains('Registration successful. You are now logged in.');
  $accounts = $this->container
    ->get('entity_type.manager')
    ->getStorage('user')
    ->loadByProperties([
    'mail' => 'guest@example.com',
  ]);

  /** @var \Drupal\user\UserInterface $account */
  $account = reset($accounts);
  $this
    ->assertTrue($account
    ->isActive());
  $this
    ->assertEquals('test_user_field_value', $account
    ->get('test_user_field')->value);
}