1 field.test FieldGetValueTestCase::testFieldGetValue()

Test field_get_value().

File

core/modules/field/tests/field.test, line 3723
Tests for field.module.

Class

FieldGetValueTestCase
Test field_get_value().

Code

function testFieldGetValue() {
  // Test single 'value' return.
  $single_value = field_get_value($this->node, $this->test_field_name);
  $this->assertEqual($this->value1, $single_value, 'The first value in test_field is returned.');

  // Test single 'value' return with $entity->getFieldValue() directly.
  $single_value = $this->node->getFieldValue($this->test_field_name);
  $this->assertEqual($this->value1, $single_value, 'The first value in test_field is returned.');

  // Test multiple 'value' return
  $multiple_value = field_get_values($this->node, $this->test_field_name, 'value', FALSE);
  $this->assertEqual(2, count($multiple_value), 'An array containing both test_field values is returned.');
  $this->assertEqual($this->value2, $multiple_value[1], 'Returned value at array position 1 is correct.');

  // Test multiple 'value' return with $entity->getFieldValues() directly.
  $multiple_value = $this->node->getFieldValues($this->test_field_name, 'value', FALSE);
  $this->assertEqual(2, count($multiple_value), 'An array containing both test_field values is returned.');
  $this->assertEqual($this->value2, $multiple_value[1], 'Returned value at array position 1 is correct.');

  // Test 'email' as key.
  $email_value = field_get_value($this->node, $this->test_email_field_name, 'email');
  $this->assertEqual($this->value_email1, $email_value, 'The correct item key is returned.');

  // Test an invalid key.
  $invalid_key = field_get_value($this->node, $this->test_email_field_name, 'wrong_key');
  $this->assertNull($invalid_key, 'Null is returned with an invalid key.');

  // Test an invalid field name.
  $invalid_field = field_get_value($this->node, 'no_field');
  $this->assertNull($invalid_field, 'Null is returned with an invalid field name.');

  // Test an invalid key.
  $invalid_key = field_get_values($this->node, $this->test_email_field_name, 'wrong_key');
  $this->assertEqual(array(), $invalid_key, 'Empty array is returned with an invalid field name.');

  // Test an invalid field name.
  $invalid_field = field_get_values($this->node, 'no_field');
  $this->assertEqual(array(), $invalid_field, 'Empty array is returned with an invalid field name.');

}