You are here

public function UCXFApiTestCase::testUCXF_FieldList in Extra Fields Checkout Pane 6.2

Same name and namespace in other branches
  1. 7 uc_extra_fields_pane.test \UCXFApiTestCase::testUCXF_FieldList()

Test if UCXF_FieldList behaves as excepted.

File

./uc_extra_fields_pane.test, line 578
Automated tests for Extra Fields Pane

Class

UCXFApiTestCase
API Test

Code

public function testUCXF_FieldList() {

  // Create an address field for delivery pane
  $delivery_db_name_without_prefix = drupal_strtolower($this
    ->randomName(12));
  $delivery_address_values = array(
    'db_name' => 'ucxf_' . $delivery_db_name_without_prefix,
    'label' => $this
      ->randomName(12),
  );
  $delivery_address_field = $this
    ->createFieldThroughAPI('extra_delivery', $delivery_address_values);
  $delivery_address_field
    ->save();

  // Test if the field is correctly saved to the database.
  $result = db_result(db_query("SELECT COUNT(field_id) FROM {uc_extra_fields} WHERE db_name = '%s' AND label = '%s'", $delivery_address_values['db_name'], $delivery_address_values['label']));
  $this
    ->assertEqual($result, 1, t('The field %field is correctly saved to the database.', array(
    '%field' => $delivery_address_field->db_name,
  )));

  // Create a few other fields.
  $billing_address_field = $this
    ->createFieldThroughAPI('extra_billing');
  $address_field = $this
    ->createFieldThroughAPI('extra_delivery|extra_billing');
  $custom_field = $this
    ->createFieldThroughAPI('extra_information');
  $billing_address_field
    ->save();
  $address_field
    ->save();
  $custom_field
    ->save();

  // Test if we have four fields in the database.
  $result = db_result(db_query("SELECT COUNT(field_id) FROM {uc_extra_fields}"));
  $this
    ->assertEqual($result, 4, t('%number fields have been saved in the database.', array(
    '%number' => 4,
  )));

  // Reset the field list so we are sure no fields are loaded.
  UCXF_FieldList::reset();

  // Try to load the delivery field through the API.
  $field1 = UCXF_FieldList::getFieldByID($delivery_address_field->id);

  // Ensure both fields have the same db_name
  $this
    ->assertEqual($field1->db_name, $delivery_address_field->db_name, t('The field %field is correctly loaded.', array(
    '%field' => $delivery_address_field->db_name,
  )));

  // Try to load the same field, but now by name
  $field2 = UCXF_FieldList::getFieldByName($delivery_address_field->db_name);

  // Ensure $field1 and $field2 are 100% equal
  $this
    ->assertTrue($field1 === $field2, t('The field %field has been found by name and by ID.', array(
    '%field' => $delivery_address_field->db_name,
  )));

  // Try to load the custom field through the API by it's name and by it's ID.
  $field1 = UCXF_FieldList::getFieldByName($custom_field->db_name);

  // Ensure both fields have the same ID
  $this
    ->assertEqual($field1->id, $custom_field->id, t('The field %field is correctly loaded.', array(
    '%field' => $custom_field->id,
  )));
  $field2 = UCXF_FieldList::getFieldByID($custom_field->id);

  // Ensure $field1 and $field2 are 100% equal
  $this
    ->assertTrue($field1 === $field2, t('The field %field has been found by name and by ID.', array(
    '%field' => $custom_field->db_name,
  )));

  // Reset the field list again.
  UCXF_FieldList::reset();

  // Get all custom fields (should be one field)
  $custom_fields = UCXF_FieldList::getAllCustomFields();
  $this
    ->assertEqual(count($custom_fields), 1, t('There is one custom field loaded.'));

  // Get all address fields (should be three)
  $address_fields = UCXF_FieldList::getAllAddressFields();
  $this
    ->assertEqual(count($address_fields), 3, t('There are %number address fields loaded.', array(
    '%number' => 3,
  )));

  // Get all delivery fields (should be two)
  $delivery_fields = UCXF_FieldList::getFieldsFromPane('extra_delivery');
  $this
    ->assertEqual(count($delivery_fields), 2, t('There are %number delivery fields loaded.', array(
    '%number' => 2,
  )));

  // Get all billing fields (should be two)
  $billing_fields = UCXF_FieldList::getFieldsFromPane('extra_billing');
  $this
    ->assertEqual(count($billing_fields), 2, t('There are %number billing fields loaded.', array(
    '%number' => 2,
  )));

  // Get all extra information fields (should be one)
  $info_fields = UCXF_FieldList::getFieldsFromPane('extra_information');
  $this
    ->assertEqual(count($info_fields), 1, t('There are %number information fields loaded.', array(
    '%number' => 1,
  )));

  // Get all fields and ensure it matches the rest of the fields that are loaded.
  $fields = UCXF_FieldList::getAllFields();
  $this
    ->assertEqual($this
    ->arrayMatch($fields, $custom_fields), 1, t('The custom field is not loaded again.'));
  $this
    ->assertEqual($this
    ->arrayMatch($fields, $address_fields), 3, t('The address fields are not loaded again.'));
  $this
    ->assertEqual($this
    ->arrayMatch($fields, $delivery_fields), 2, t('The delivery fields are not loaded again.'));
  $this
    ->assertEqual($this
    ->arrayMatch($fields, $billing_fields), 2, t('The billing fields are not loaded again.'));
  $this
    ->assertEqual($this
    ->arrayMatch($fields, $info_fields), 1, t('The info fields are not loaded again.'));

  // Reset the field list again.
  UCXF_FieldList::reset();

  // First, try to delete a field that does not exists.
  $this
    ->assertFalse(UCXF_FieldList::deleteFieldById(5), t("Field %id does not exists and thus can't be deleted.", array(
    '%id' => 5,
  )));

  // Try to delete an other field that does not exists.
  $this
    ->assertFalse(UCXF_FieldList::deleteFieldByName($delivery_db_name_without_prefix), t("Field %name does not exists and thus can't be deleted.", array(
    '%name' => $delivery_db_name_without_prefix,
  )));

  // Try to delete the delivery field through the API.
  UCXF_FieldList::deleteFieldById($delivery_address_field->id);

  // Ensure the field no longer exists in the database.
  $result = db_result(db_query("SELECT COUNT(field_id) FROM {uc_extra_fields} WHERE field_id = %d", $delivery_address_field->id));
  $this
    ->assertEqual($result, 0, t('The field has been deleted.'));

  // Try to delete the billing field by name.
  UCXF_FieldList::deleteFieldByName($billing_address_field->db_name);

  // Ensure the field no longer exists in the database.
  $result = db_result(db_query("SELECT COUNT(field_id) FROM {uc_extra_fields} WHERE field_id = %d", $billing_address_field->id));
  $this
    ->assertEqual($result, 0, t('The field has been deleted.'));
}