You are here

public function FieldHelperTestCase::testValues in Helper 7

File

tests/FieldHelperTestCase.test, line 52

Class

FieldHelperTestCase

Code

public function testValues() {
  $contentType = $this
    ->drupalCreateContentType();
  field_create_instance(array(
    'field_name' => 'field_text',
    'entity_type' => 'node',
    'bundle' => $contentType->type,
    'label' => $this
      ->randomName(),
  ));
  $node = new stdClass();
  $node->type = $contentType->type;
  $node->language = LANGUAGE_NONE;
  $node->field_text = array(
    LANGUAGE_NONE => array(
      0 => array(
        'value' => 'und-value0',
      ),
    ),
    'en' => array(
      0 => array(
        'value' => 'value0',
      ),
      1 => array(
        'value' => 'value1',
        'format' => 'filter1',
      ),
      2 => array(
        'value' => 'value2',
        'format' => 'filter2',
        'nested' => array(
          'subkey' => 'sub2',
          0 => 'sub22',
        ),
      ),
    ),
    'invalid' => array(
      0 => array(
        'value' => 'invalid-value0',
      ),
    ),
  );
  $value = $node->field_text['en'];

  // Test FieldHelper::getValues().
  $this
    ->assertIdentical(FieldHelper::getValues('node', $node, 'field_text'), $value);
  $this
    ->assertIdentical(FieldHelper::getValues('node', $node, 'field_text', 'value'), array(
    0 => 'value0',
    1 => 'value1',
    2 => 'value2',
  ));
  $this
    ->assertIdentical(FieldHelper::getValues('node', $node, 'field_text', array(
    'format',
  )), array(
    1 => 'filter1',
    2 => 'filter2',
  ));
  $this
    ->assertIdentical(FieldHelper::getValues('node', $node, 'field_text', 'nested'), array(
    2 => $value[2]['nested'],
  ));
  $this
    ->assertIdentical(FieldHelper::getValues('node', $node, 'field_text', array(
    'nested',
    'subkey',
  )), array(
    2 => 'sub2',
  ));
  $this
    ->assertIdentical(FieldHelper::getValues('node', $node, 'field_text', array(
    'nested',
    0,
  )), array(
    2 => 'sub22',
  ));
  $this
    ->assertIdentical(FieldHelper::getValues('node', $node, 'field_text', array(
    'nested',
    'invalid',
  )), array());
  $this
    ->assertIdentical(FieldHelper::getValues('node', $node, 'field_text', 'invalid'), array());
  $this
    ->assertIdentical(FieldHelper::getValues('node', $node, 'invalid'), array());

  // Test FieldHelper::getValue().
  $this
    ->assertIdentical(FieldHelper::getValue('node', $node, 'field_text'), $value[0]);
  $this
    ->assertIdentical(FieldHelper::getValue('node', $node, 'field_text', NULL, 1), $value[1]);
  $this
    ->assertIdentical(FieldHelper::getValue('node', $node, 'field_text', NULL, 2), $value[2]);
  $this
    ->assertIdentical(FieldHelper::getValue('node', $node, 'field_text', NULL, 3), NULL);
  $this
    ->assertIdentical(FieldHelper::getValue('node', $node, 'field_text', 'value'), $value[0]['value']);
  $this
    ->assertIdentical(FieldHelper::getValue('node', $node, 'field_text', 'value', 1), $value[1]['value']);
  $this
    ->assertIdentical(FieldHelper::getValue('node', $node, 'field_text', 'value', 2), $value[2]['value']);
  $this
    ->assertIdentical(FieldHelper::getValue('node', $node, 'field_text', 'value', 3), NULL);
  $this
    ->assertIdentical(FieldHelper::getValue('node', $node, 'field_text', array(
    'format',
  )), NULL);
  $this
    ->assertIdentical(FieldHelper::getValue('node', $node, 'field_text', array(
    'format',
  ), 1), $value[1]['format']);
  $this
    ->assertIdentical(FieldHelper::getValue('node', $node, 'field_text', array(
    'format',
  ), 2), $value[2]['format']);
  $this
    ->assertIdentical(FieldHelper::getValue('node', $node, 'field_text', array(
    'format',
  ), 3), NULL);
  $this
    ->assertIdentical(FieldHelper::getValue('node', $node, 'field_text', 'nested'), NULL);
  $this
    ->assertIdentical(FieldHelper::getValue('node', $node, 'field_text', 'nested', 1), NULL);
  $this
    ->assertIdentical(FieldHelper::getValue('node', $node, 'field_text', 'nested', 2), $value[2]['nested']);
  $this
    ->assertIdentical(FieldHelper::getValue('node', $node, 'field_text', 'nested', 3), NULL);
  $this
    ->assertIdentical(FieldHelper::getValue('node', $node, 'field_text', array(
    'nested',
    'subkey',
  )), NULL);
  $this
    ->assertIdentical(FieldHelper::getValue('node', $node, 'field_text', array(
    'nested',
    'subkey',
  ), 1), NULL);
  $this
    ->assertIdentical(FieldHelper::getValue('node', $node, 'field_text', array(
    'nested',
    'subkey',
  ), 2), 'sub2');
  $this
    ->assertIdentical(FieldHelper::getValue('node', $node, 'field_text', array(
    'nested',
    'subkey',
  ), 3), NULL);
  $this
    ->assertIdentical(FieldHelper::getValue('node', $node, 'field_text', array(
    'nested',
    0,
  ), 2), 'sub22');
  $this
    ->assertIdentical(FieldHelper::getValue('node', $node, 'field_text', array(
    'nested',
    'invalid',
  ), 2), NULL);
  $this
    ->assertIdentical(FieldHelper::getValue('node', $node, 'invalid'), NULL);

  // Test FieldHelper::hasDelta().
  $this
    ->assertTrue(FieldHelper::hasDelta('node', $node, 'field_text', 0));
  $this
    ->assertFalse(FieldHelper::hasDelta('node', $node, 'field_text', 3));
  $this
    ->assertFalse(FieldHelper::hasDelta('node', $node, 'field_text', 'invalid_delta'));
  $this
    ->assertFalse(FieldHelper::hasDelta('node', $node, 'invalid', 0));
}