You are here

public function ImageFieldTokensTestCase::testUploadImage in ImageField Tokens 7

Test image uploading.

Throws

\FieldException

\Exception

File

tests/imagefield_tokens.test, line 39
This file implements tests for ImageFiled Tokens.

Class

ImageFieldTokensTestCase
Class ImageFieldTokensTestCase.

Code

public function testUploadImage() {

  // Create content type.
  $type = $this
    ->drupalCreateContentType();

  // Create ImageField.
  $field_name = 'field_' . drupal_strtolower(self::randomName());
  module_load_include('test', 'image', 'image');
  module_load_include('test', 'simpletest', 'image');
  $this
    ->createImageField($field_name, $type->name, array(), array(
    'alt' => '[node:title]',
    'title' => '[node:title]',
  ));

  // Prepare a test file.
  $test_file = $this
    ->getTestFile('image');
  file_save($test_file);

  // Create and load new node.
  $node = (object) [
    'title' => self::randomName(),
    'type' => $type->name,
    'vid' => (string) (int) TRUE,
  ];
  node_save($node);
  $nid = $node->nid;

  // Load node and upload file.
  $node = node_load($nid, NULL, TRUE);
  $node->{$field_name}[LANGUAGE_NONE][0] = (array) $test_file;
  $node->{$field_name}[LANGUAGE_NONE][0]['alt'] = '[node:title]';
  $node->{$field_name}[LANGUAGE_NONE][0]['title'] = '[node:title]';
  node_save($node);

  // Get processed file from node.
  $node_file = $node->{$field_name}[LANGUAGE_NONE][0];

  // Check ALT and Title values.
  $this
    ->assertEqual($node_file['alt'], $node->title, t('Make sure ALT field has been processed.'));
  $this
    ->assertEqual($node_file['title'], $node->title, t('Make sure Title field has been processed.'));
}