You are here

public function CommerceCustomerUITest::testCommerceCustomerUIProfileWithExtraFields in Commerce Core 7

Add extra fields to a profile type.

File

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

Class

CommerceCustomerUITest
Functional tests for the commerce customer UI module.

Code

public function testCommerceCustomerUIProfileWithExtraFields() {

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

  // Access to the profile billing type manage fields.
  $this
    ->drupalGet('admin/commerce/customer-profiles/types/billing/fields');
  $this
    ->assertResponse(200, t('Store admin user is able to access the customer profile type manage fields screen'));

  // Create an extra field for the profile.
  $edit = array(
    'fields[_add_new_field][label]' => $this
      ->randomName(),
    'fields[_add_new_field][field_name]' => strtolower($this
      ->randomName()),
    'fields[_add_new_field][type]' => 'text',
    'fields[_add_new_field][widget_type]' => 'text_textfield',
  );
  $this
    ->drupalPost(NULL, $edit, t('Save'));
  $this
    ->drupalPost(NULL, array(), t('Save field settings'));
  $this
    ->drupalPost(NULL, array(), t('Save settings'));

  // Add a new profile, check that the field is there.
  $this
    ->drupalGet('admin/commerce/customer-profiles/add');

  // Assert that the field exists in the profile add form.
  $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'],
  );

  // Also add the new field value.
  $field_value = $this
    ->randomName();
  $info['field_' . $edit['fields[_add_new_field][field_name]'] . '[und][0][value]'] = $field_value;
  $this
    ->drupalPost(NULL, $info, t('Save profile'));

  // Check that the profile got created and if the field is filled.
  $this
    ->assertText(t('Profile saved'), t('\'Profile saved\' message is displayed after saving a customer profile'));

  // Check also in database.
  foreach ($address_info as $id => $element) {
    $conditions[] = array(
      'field' => 'commerce_customer_address',
      'column' => $id,
      'value' => $element,
    );
  }

  // Load the profile and check if the field is filled.
  $profiles = $this
    ->loadCustomerProfile($conditions);
  $profile = commerce_customer_profile_load(reset($profiles)->profile_id);
  $this
    ->assertTrue($profile->{'field_' . $edit['fields[_add_new_field][field_name]']}[LANGUAGE_NONE][0]['value'] == $field_value, t('The extra field !field created for the customer profile exists and it has the correct value: !value', array(
    '!field' => $edit['fields[_add_new_field][field_name]'],
    '!value' => $field_value,
  )));
}