You are here

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

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

Test if UCXF_FieldList behaves as excepted.

File

./uc_extra_fields_pane.test, line 562
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_select('uc_extra_fields')
    ->fields('uc_extra_fields', array(
    'field_id',
  ))
    ->condition('db_name', $delivery_address_values['db_name'])
    ->condition('label', $delivery_address_values['label'])
    ->countQuery()
    ->execute()
    ->fetchField();
  $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');
  $billing_address_field
    ->save();
  $address_field
    ->save();

  // Test if we have four fields in the database.
  $result = db_select('uc_extra_fields')
    ->fields('uc_extra_fields', array(
    'field_id',
  ))
    ->countQuery()
    ->execute()
    ->fetchField();
  $this
    ->assertEqual($result, 3, t('%number fields have been saved in the database.', array(
    '%number' => 3,
  )));

  // 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,
  )));

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

  // 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 fields and ensure it matches the rest of the fields that are loaded.
  $fields = UCXF_FieldList::getAllFields();
  $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.'));

  // 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_select('uc_extra_fields')
    ->fields('uc_extra_fields', array(
    'field_id',
  ))
    ->condition('field_id', $delivery_address_field->id)
    ->countQuery()
    ->execute()
    ->fetchField();
  $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_select('uc_extra_fields')
    ->fields('uc_extra_fields', array(
    'field_id',
  ))
    ->condition('field_id', $billing_address_field->id)
    ->countQuery()
    ->execute()
    ->fetchField();
  $this
    ->assertEqual($result, 0, t('The field has been deleted.'));
}