You are here

public function MultifieldUnitTestCase::testDefaultValue in Multifield 7

Same name and namespace in other branches
  1. 7.2 tests/MultifieldUnitTestCase.test \MultifieldUnitTestCase::testDefaultValue()

Regression test for https://drupal.org/node/2198697.

File

tests/MultifieldUnitTestCase.test, line 435

Class

MultifieldUnitTestCase

Code

public function testDefaultValue() {

  // Add the test node type.
  $node_type = $this
    ->drupalCreateContentType();

  // Create the multifield.
  $multifield_field = array(
    'field_name' => 'field_multifield',
    'type' => 'multifield',
    'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  );
  field_create_field($multifield_field);

  // Create fields on the multifield.
  $field_name1 = 'field_text1';
  $field1 = array(
    'field_name' => $field_name1,
    'type' => 'text',
    'cardinality' => 1,
  );
  field_create_field($field1);
  $instance1 = array(
    'field_name' => $field_name1,
    'entity_type' => 'multifield',
    'bundle' => 'field_multifield',
  );
  field_create_instance($instance1);
  $multifield_instance = array(
    'field_name' => 'field_multifield',
    'entity_type' => 'node',
    'bundle' => $node_type->type,
    'default_value' => array(
      array(
        'field_text1' => array(
          LANGUAGE_NONE => array(
            array(
              'value' => 'Default value',
            ),
          ),
        ),
        // Default values should not have an ID.
        'id' => NULL,
        // Sometimes forms add in additional values that should be ignored.
        'actions' => array(
          'remove_button' => 'Remove',
        ),
      ),
    ),
  );
  field_create_instance($multifield_instance);
  $node = new stdClass();
  $node->type = $node_type->type;
  $node->uid = 0;
  $node->status = 1;
  $node->title = 'Test node';
  $node->language = LANGUAGE_NONE;
  node_object_prepare($node);
  $node = node_submit($node);
  node_save($node);

  // Check that saving a node object without provide a multifield value used
  // the default value, but added a proper ID value.
  $items = field_get_items('node', $node, 'field_multifield');
  $this
    ->assertIdentical($items[0], array(
    'field_text1' => array(
      LANGUAGE_NONE => array(
        0 => array(
          'value' => 'Default value',
          'format' => NULL,
        ),
      ),
    ),
    'id' => 1,
    'actions' => array(
      'remove_button' => 'Remove',
    ),
    'field_text1_value' => 'Default value',
    'field_text1_format' => NULL,
  ));

  // Check the value after doing a reload.
  $node = node_load($node->nid, NULL, TRUE);
  $items = field_get_items('node', $node, 'field_multifield');
  $this
    ->assertIdentical($items[0], array(
    // Default value should now have a valid ID.
    'id' => '1',
    'field_text1' => array(
      LANGUAGE_NONE => array(
        0 => array(
          'value' => 'Default value',
          'format' => NULL,
          'safe_value' => 'Default value',
        ),
      ),
    ),
  ));
}