You are here

public function MultifieldCommerceIntegrationTest::testCommerceFields in Multifield 7.2

Same name and namespace in other branches
  1. 7 tests/MultifieldCommerceIntegrationTest.test \MultifieldCommerceIntegrationTest::testCommerceFields()

File

tests/MultifieldCommerceIntegrationTest.test, line 29

Class

MultifieldCommerceIntegrationTest

Code

public function testCommerceFields() {
  $price_field = array(
    'field_name' => 'field_price',
    'type' => 'commerce_price',
  );
  field_create_field($price_field);
  $price_instance = array(
    'entity_type' => 'multifield',
    'bundle' => 'multifield_commerce',
    'field_name' => 'field_price',
  );
  field_create_instance($price_instance);
  $multifield_instance = array(
    'entity_type' => 'test_entity',
    'bundle' => 'test_bundle',
    'field_name' => 'multifield_commerce',
  );
  field_create_instance($multifield_instance);
  $entity = field_test_create_stub_entity(NULL, NULL, 'test_bundle', 'Commerce field test');

  // First value should evaluate to empty.
  // @see commerce_price_field_is_empty()
  $entity->multifield_commerce[LANGUAGE_NONE][0]['field_price'][LANGUAGE_NONE][0] = array(
    'amount' => '',
    'currency_code' => 'USD',
    'data' => array(
      'components' => array(),
    ),
  );

  // Add a second value with the minimal amount of data.
  $entity->multifield_commerce[LANGUAGE_NONE][1]['field_price'][LANGUAGE_NONE][0] = array(
    'amount' => '5.00',
  );

  // Add a third value with the data array value.
  $entity->multifield_commerce[LANGUAGE_NONE][2]['field_price'][LANGUAGE_NONE][0] = array(
    'amount' => '10.25',
    'currency_code' => 'USD',
    'data' => array(
      'components' => array(
        'multifield' => TRUE,
      ),
    ),
  );

  // Save the entity.
  $entity = $this
    ->saveTestEntity($entity);

  // The first value should be filtered out, leaving two values.
  $items = field_get_items('test_entity', $entity, 'multifield_commerce');
  $this
    ->assertEqual(count($items), 2, 'Only two field values saved.');

  // Ensure the field values were serialized and saved properly.
  $this
    ->assertIdentical($items[0]['field_price'][LANGUAGE_NONE][0], array(
    'amount' => '5',
    'currency_code' => NULL,
    'data' => array(
      'components' => array(),
    ),
  ));
  $this
    ->assertIdentical($items[1]['field_price'][LANGUAGE_NONE][0], array(
    'amount' => '10',
    'currency_code' => 'USD',
    'data' => array(
      'components' => array(
        'multifield' => TRUE,
      ),
    ),
  ));
}