You are here

public function CommerceCustomerUITest::testCommerceCustomerUIAddCustomerProfile in Commerce Core 7

Add a customer profile.

File

modules/customer/tests/commerce_customer_ui.test, line 104
Commerce customer profile tests.

Class

CommerceCustomerUITest
Functional tests for the commerce customer UI module.

Code

public function testCommerceCustomerUIAddCustomerProfile() {

  // Login with customer.
  $this
    ->drupalLogin($this->store_customer);

  // Check the access to the profile add page.
  $this
    ->drupalGet('admin/commerce/customer-profiles/add');

  // Login with store admin.
  $this
    ->drupalLogin($this->store_admin);

  // Check the access to the profile add page.
  $this
    ->drupalGet('admin/commerce/customer-profiles/add');

  // As The billing information is the only profile shipped by default at
  // the moment, the destination url is the billing information creation
  // form.
  $this
    ->assertTrue($this->url = url('admin/commerce/customer-profiles/add/billing', array(
    'absolute => TRUE',
  )));

  // Get the default values for the address.
  $field = field_info_field('commerce_customer_address');
  $instance = field_info_instance('commerce_customer_profile', 'commerce_customer_address', 'billing');
  $address = addressfield_default_values($field, $instance);

  // Check the integrity of the add form.
  $this
    ->pass(t('Test the integrity of the add customer profile form:'));
  $billing_country = $this
    ->xpath("//select[starts-with(@name, 'commerce_customer_address')]");
  $this
    ->drupalPostAJAX(NULL, array(
    (string) $billing_country[0]['name'] => $address['country'],
  ), (string) $billing_country[0]['name']);
  $this
    ->assertFieldByXPath("//select[starts-with(@id, 'edit-commerce-customer-address-und-0-country')]", $address['country'], t('Country field exists and it has the default country selected'));
  $this
    ->assertFieldByXPath("//input[starts-with(@id, 'edit-commerce-customer-address-und-0-name-line')]", NULL, t('Field !field exists in the customer profile form', array(
    '!field' => 'Name line',
  )));

  // Also check for the buttons and cancel link.
  $this
    ->assertFieldById('edit-submit', t('Save profile'), t('\'Save profile\' button is present'));
  $this
    ->assertFieldById('edit-save-continue', t('Save and add another'), t('\'Save an add another\' button is present'));
  $this
    ->assertRaw(l(t('Cancel'), 'admin/commerce/customer-profiles'), t('Cancel link is present'));

  // Generate random information, as city, postal code, etc.
  $address_info = $this
    ->generateAddressInformation();

  // Fill the profile information and Save.
  $info = array(
    'commerce_customer_address[und][0][name_line]' => $address_info['name_line'],
    'commerce_customer_address[und][0][thoroughfare]' => $address_info['thoroughfare'],
    'commerce_customer_address[und][0][locality]' => $address_info['locality'],
    'commerce_customer_address[und][0][administrative_area]' => $address_info['administrative_area'],
    'commerce_customer_address[und][0][postal_code]' => $address_info['postal_code'],
  );
  $this
    ->drupalPost(NULL, $info, t('Save profile'));

  // Check in database if the profile got created.
  $conditions = array();
  foreach ($address_info as $id => $element) {
    $conditions[] = array(
      'field' => 'commerce_customer_address',
      'column' => $id,
      'value' => $element,
    );
  }
  $profile = $this
    ->loadCustomerProfile($conditions);
  $this
    ->assertFalse(empty($profile), t('Profile has been created in database'));

  // Check the landing url and if the profile is in the listing.
  $this
    ->assertTrue($this->url == url('admin/commerce/customer-profiles', array(
    'absolute' => TRUE,
  )), t('Landing page after save the profile is the profile listing page'));
  $this
    ->assertText(t('Profile saved'), t('\'Profile saved\' message is displayed after saving a customer profile'));
  $this
    ->assertText($address_info['name_line'], t('Profile name line value: !value is present in the customer profile listing', array(
    '!value' => $address_info['name_line'],
  )));
}