You are here

public function CustomerProfileTypeTest::testProfileTypeUi in Commerce Core 8.2

Tests the profile type UI.

File

modules/order/tests/src/Functional/CustomerProfileTypeTest.php, line 60

Class

CustomerProfileTypeTest
Tests the management of customer profile types.

Namespace

Drupal\Tests\commerce_order\Functional

Code

public function testProfileTypeUi() {
  $profile_type = ProfileType::load('customer');

  // Confirm that the "customer" profile type is not deletable.
  $this
    ->assertFalse($profile_type
    ->access('delete'));

  // Confirm that the "customer profile type" flag is set on the "customer"
  // profile type, and that it can't be disabled from the UI.
  $this
    ->assertTrue($profile_type
    ->getThirdPartySetting('commerce_order', 'customer_profile_type'));
  $this
    ->drupalGet($profile_type
    ->toUrl('edit-form'));
  $checkbox = $this
    ->getSession()
    ->getPage()
    ->findField('commerce_order[customer_profile_type]');
  $this
    ->assertNotEmpty($checkbox);
  $this
    ->assertNotEmpty($checkbox
    ->getAttribute('checked'));
  $this
    ->assertNotEmpty($checkbox
    ->getAttribute('disabled'));
  $this
    ->submitForm([], 'Save');

  // Confirm that saving the form doesn't unset the flag.

  /** @var \Drupal\profile\Entity\ProfileTypeInterface $profile_type */
  $profile_type = $this
    ->reloadEntity($profile_type);
  $this
    ->assertTrue($profile_type
    ->getThirdPartySetting('commerce_order', 'customer_profile_type'));

  // Confirm that the flag is unset by default when adding a new profile type.
  $this
    ->drupalGet('admin/config/people/profile-types/add');
  $checkbox = $this
    ->getSession()
    ->getPage()
    ->findField('commerce_order[customer_profile_type]');
  $this
    ->assertNotEmpty($checkbox);
  $this
    ->assertEmpty($checkbox
    ->getAttribute('checked'));
  $this
    ->assertEmpty($checkbox
    ->getAttribute('disabled'));
  $this
    ->submitForm([
    'label' => 'Customer (Shipping information)',
    'display_label' => 'Shipping information',
    'id' => 'customer_shipping',
  ], 'Save');
  $profile_type = ProfileType::load('customer_shipping');
  $this
    ->assertFalse($profile_type
    ->getThirdPartySetting('commerce_order', 'customer_profile_type'));

  // Confirm that an address field was not attached.
  $address_field = FieldConfig::loadByName('profile', 'customer_shipping', 'address');
  $this
    ->assertEmpty($address_field);

  // Confirm that the flag can be set.
  $this
    ->drupalGet($profile_type
    ->toUrl('edit-form'));
  $this
    ->submitForm([
    'commerce_order[customer_profile_type]' => TRUE,
  ], 'Save');
  $profile_type = $this
    ->reloadEntity($profile_type);
  $this
    ->assertTrue($profile_type
    ->getThirdPartySetting('commerce_order', 'customer_profile_type'));

  // Confirm that an address field was attached.
  $address_field = FieldConfig::loadByName('profile', 'customer_shipping', 'address');
  $this
    ->assertNotEmpty($address_field);
  $this
    ->assertEquals('Address', $address_field
    ->getLabel());
  $this
    ->assertTrue($address_field
    ->isRequired());

  // Confirm that the edit form reflects the updated flag.
  $this
    ->drupalGet($profile_type
    ->toUrl('edit-form'));
  $checkbox = $this
    ->getSession()
    ->getPage()
    ->findField('commerce_order[customer_profile_type]');
  $this
    ->assertNotEmpty($checkbox);
  $this
    ->assertNotEmpty($checkbox
    ->getAttribute('checked'));
  $this
    ->assertEmpty($checkbox
    ->getAttribute('disabled'));

  // Confirm that the flag can't be unset once there's data.
  $profile = Profile::create([
    'type' => 'customer_shipping',
    'address' => [
      'country_code' => 'US',
      'administrative_area' => 'SC',
      'locality' => 'Greenville',
      'postal_code' => '29616',
      'address_line1' => '9 Drupal Ave',
      'given_name' => 'Bryan',
      'family_name' => 'Centarro',
    ],
  ]);
  $profile
    ->save();
  $this
    ->drupalGet($profile_type
    ->toUrl('edit-form'));
  $checkbox = $this
    ->getSession()
    ->getPage()
    ->findField('commerce_order[customer_profile_type]');
  $this
    ->assertNotEmpty($checkbox);
  $this
    ->assertNotEmpty($checkbox
    ->getAttribute('checked'));
  $this
    ->assertNotEmpty($checkbox
    ->getAttribute('disabled'));

  // Confirm that unsetting the flag removes the address field.
  $profile
    ->delete();
  $this
    ->drupalGet($profile_type
    ->toUrl('edit-form'));
  $this
    ->submitForm([
    'commerce_order[customer_profile_type]' => FALSE,
  ], 'Save');
  $address_field = FieldConfig::loadByName('profile', 'customer_shipping', 'address');
  $this
    ->assertEmpty($address_field);
}