You are here

public function EntityMetadataTestCase::testListMetadataWrappers in Entity API 7

Test supporting multi-valued fields.

File

./entity.test, line 894
Entity CRUD API tests.

Class

EntityMetadataTestCase
Tests metadata wrappers.

Code

public function testListMetadataWrappers() {
  $property = $this->field_name;
  $values = array();
  $values[LANGUAGE_NONE][0] = array(
    'value' => '<b>2009-09-05</b>',
  );
  $values[LANGUAGE_NONE][1] = array(
    'value' => '2009-09-05',
  );
  $values[LANGUAGE_NONE][2] = array(
    'value' => '2009-08-05',
  );
  $node = $this
    ->drupalCreateNode(array(
    'type' => 'page',
    $property => $values,
  ));
  $wrapper = entity_metadata_wrapper('node', $node);
  $this
    ->assertEqual('<b>2009-09-05</b>', $wrapper->{$property}[0]
    ->value(), 'Getting array entry.');
  $this
    ->assertEqual('2009-09-05', $wrapper->{$property}
    ->get(1)
    ->value(), 'Getting array entry.');
  $this
    ->assertEqual(3, count($wrapper->{$property}
    ->value()), 'Getting the whole array.');

  // Test sanitizing.
  $this
    ->assertEqual(check_plain('<b>2009-09-05</b>'), $wrapper->{$property}[0]
    ->value(array(
    'sanitize' => TRUE,
  )), 'Getting array entry.');

  // Test iterator.
  $this
    ->assertEqual(array_keys(iterator_to_array($wrapper->{$property}
    ->getIterator())), array_keys($wrapper->{$property}
    ->value()), 'Iterator is working.');
  foreach ($wrapper->{$property} as $p) {
    $this
      ->assertTrue($p instanceof EntityMetadataWrapper, 'Iterate over list wrapper properties.');
  }

  // Make sure changing the array changes the actual entity property.
  $wrapper->{$property}[0] = '2009-10-05';
  unset($wrapper->{$property}[1], $wrapper->{$property}[2]);
  $this
    ->assertEqual($wrapper->{$property}
    ->value(), array(
    '2009-10-05',
  ), 'Setting multiple property values.');

  // Test setting an arbitrary list item.
  $list = array(
    0 => REQUEST_TIME,
  );
  $wrapper = entity_metadata_wrapper('list<date>', $list);
  $wrapper[1] = strtotime('2009-09-05');
  $this
    ->assertEqual($wrapper
    ->value(), array(
    REQUEST_TIME,
    strtotime('2009-09-05'),
  ), 'Setting a list item.');
  $this
    ->assertEqual($wrapper
    ->count(), 2, 'List count is correct.');

  // Test using a list wrapper without data.
  $wrapper = entity_metadata_wrapper('list<date>');
  $info = array();
  foreach ($wrapper as $item) {
    $info[] = $item
      ->info();
  }
  $this
    ->assertTrue($info[0]['type'] == 'date', 'Iterated over empty list wrapper.');

  // Test using a list of entities with a list of term objects.
  $list = array();
  $list[] = entity_property_values_create_entity('taxonomy_term', array(
    'name' => 'term 1',
    'vocabulary' => 1,
  ))
    ->save()
    ->value();
  $list[] = entity_property_values_create_entity('taxonomy_term', array(
    'name' => 'term 2',
    'vocabulary' => 1,
  ))
    ->save()
    ->value();
  $wrapper = entity_metadata_wrapper('list<taxonomy_term>', $list);
  $this
    ->assertTrue($wrapper[0]->name
    ->value() == 'term 1', 'Used a list of entities.');

  // Test getting a list of identifiers.
  $ids = $wrapper
    ->value(array(
    'identifier' => TRUE,
  ));
  $this
    ->assertTrue(!is_object($ids[0]), 'Get a list of entity ids.');
  $wrapper = entity_metadata_wrapper('list<taxonomy_term>', $ids);
  $this
    ->assertTrue($wrapper[0]->name
    ->value() == 'term 1', 'Created a list of entities with ids.');

  // Test with a list of generic entities. The list is expected to be a list
  // of entity wrappers, otherwise the entity type is unknown.
  $node = $this
    ->drupalCreateNode(array(
    'title' => 'node 1',
  ));
  $list = array();
  $list[] = entity_metadata_wrapper('node', $node);
  $wrapper = entity_metadata_wrapper('list<entity>', $list);
  $this
    ->assertEqual($wrapper[0]->title
    ->value(), 'node 1', 'Wrapped node was found in generic list of entities.');
}