public function UCXFApiTestCase::testUCXF_Value in Extra Fields Checkout Pane 7
Same name and namespace in other branches
- 6.2 uc_extra_fields_pane.test \UCXFApiTestCase::testUCXF_Value()
Test if UCXF_Value behaves as excepted.
File
- ./
uc_extra_fields_pane.test, line 659 - Automated tests for Extra Fields Pane
Class
- UCXFApiTestCase
- API Test
Code
public function testUCXF_Value() {
// Setup default fields.
$this
->setupFields();
// Ensure the uc_extra_fields_values table is empty.
// @todo Rewrite using query builder.
db_query("TRUNCATE TABLE {uc_extra_fields_values}");
// Save a value for the select field
$field = UCXF_FieldList::getFieldByName($this->selectField);
$oValue = UCXF_Value::load(1, UCXF_Value::UCXF_VALUE_ORDER_DELIVERY, $field->id);
$oValue
->setValue('option1');
$oValue
->save();
// Check if the value is correctly saved to the database.
$result = db_select('uc_extra_fields_values', 'ucxfv')
->fields('ucxfv', array(
'element_id',
))
->condition('value', 'option1')
->countQuery()
->execute()
->fetchField();
$this
->assertEqual($result, 1, t('The value is correctly saved to the database.'));
// Check if output != value (value should be "safe" key, output be the "readable" part)
$this
->assertNotEqual($oValue
->getValue(), $oValue
->output(), t('The output value is different from the saved value.'));
// Check if the field that can be get through UCXF_Value is equal to that of the field gotten earlier.
$this
->assertTrue($field === $oValue
->getField());
// Insert a few values directly to the database
$value1 = array(
'element_id' => 1,
'element_type' => UCXF_Value::UCXF_VALUE_ORDER_DELIVERY,
'field_id' => UCXF_FieldList::getFieldByName($this->textField)->id,
'value' => $this
->randomName(),
);
drupal_write_record('uc_extra_fields_values', $value1);
$value2 = array(
'element_id' => 1,
'element_type' => UCXF_Value::UCXF_VALUE_ORDER_DELIVERY,
'field_id' => UCXF_FieldList::getFieldByName($this->constantField)->id,
'value' => $this
->randomName(),
);
drupal_write_record('uc_extra_fields_values', $value2);
// Load one value through the API
$oValue1 = UCXF_Value::load(1, UCXF_Value::UCXF_VALUE_ORDER_DELIVERY, UCXF_FieldList::getFieldByName($this->textField)->id);
// Check if this value has the expected value
$this
->assertEqual($value1['value'], $oValue1
->getValue(), t('The value is correctly loaded from the database.'));
// Set an unexpected value and check if the output is sanitized
$oValue1
->setValue('<script language="javascript">alert(\'hello\');</script>');
$this
->assertNotEqual($oValue1
->getValue(), $oValue1
->output(), t('The output value is different from the saved value.'));
// Set a simple value and check if the output value is equal
$oValue1
->setValue("value that does not need to be sanitized");
$this
->verbose($oValue1
->getValue() . '<br />' . $oValue1
->output());
$this
->assertEqual($oValue1
->getValue(), $oValue1
->output(), t('The output value is the same as the saved value.'));
// Load a list of values through the API
$values = UCXF_Value::load_list(1, UCXF_Value::UCXF_VALUE_ORDER_DELIVERY);
$this
->assertEqual(count($values), 3, t('%number values are loaded.', array(
'%number' => 3,
)));
// Check if $oValue and $oValue1 exists in the list.
$this
->assertEqual($this
->arrayMatch($values, array(
$oValue1,
)), 1, t('The first loaded value is not loaded again.'));
$this
->assertEqual($this
->arrayMatch($values, array(
$oValue,
)), 1, t('The first created value is not loaded again.'));
// Delete value
$oValue
->delete();
// Ensure that the value no longer exists in the database.
$result = db_select('uc_extra_fields_values', 'ucxfv')
->fields('ucxfv', array(
'element_id',
))
->condition('value', 'option1')
->countQuery()
->execute()
->fetchField();
$this
->assertEqual($result, 0, t('The value is correctly deleted from the database.'));
}