You are here

public function EntityMetadataIntegrationTestCase::testImageFields in Entity API 7

Test making use of an image field.

File

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

Class

EntityMetadataIntegrationTestCase
Tests provided entity property info of the core modules.

Code

public function testImageFields() {
  $file = $this
    ->createFile('image');

  // Just use the image field on the article node.
  $node = $this
    ->drupalCreateNode(array(
    'type' => 'article',
  ));
  $wrapper = entity_metadata_wrapper('node', $node);
  $wrapper->field_image = array(
    'fid' => $file->fid,
  );
  $this
    ->assertEqual($file->filename, $wrapper->field_image->file->name
    ->value(), 'File has been specified.');
  $wrapper->field_image->alt = 'foo';
  $this
    ->assertEqual($wrapper->field_image->alt
    ->value(), 'foo', 'Image alt attribute has been correctly set.');

  // Try saving the node and make sure the information is still there after
  // loading the node again, thus the correct data structure has been written.
  node_save($node);
  $node = node_load($node->nid, NULL, TRUE);
  $wrapper = entity_metadata_wrapper('node', $node);
  $this
    ->assertEqual($wrapper->field_image->alt
    ->value(), 'foo', 'File description has been correctly set.');

  // Test adding a new image.
  $wrapper->field_image->file = $file;
  node_save($node);
  $node = node_load($node->nid, NULL, TRUE);
  $this
    ->assertEqual($file->fid, $wrapper->field_image->file
    ->getIdentifier(), 'New file has been added.');

  // Test adding an invalid image-field item, i.e. without any file.
  try {
    $wrapper->field_image = array();
    $this
      ->fail('Exception not thrown.');
  } catch (EntityMetadataWrapperException $e) {
    $this
      ->pass('Not valid image-field item has thrown an exception.');
  }
}