You are here

public function TextimageFieldFormatterTest::testTextimageMultiValueFieldFormatter in Textimage 7.3

Test Textimage formatter on multi-value text fields.

File

tests/textimage.test, line 398
Textimage - web test case script.

Class

TextimageFieldFormatterTest
Test class to check that Textimage formatter is working.

Code

public function testTextimageMultiValueFieldFormatter() {

  // Create a multi-value text field for Textimage test.
  $field_name = strtolower($this
    ->randomName());
  $this
    ->createTextimageField($field_name, 'article', array(
    'cardinality' => 4,
  ));

  // Create a new node, with 4 text values for the field.
  $field_value = array();
  for ($i = 0; $i < 4; $i++) {
    $field_value[] = $this
      ->randomName(20);
  }
  $nid = $this
    ->createTextimageNode($field_name, $field_value, 'article');
  $node = node_load($nid, NULL, TRUE);

  // Test the textimage formatter - one image.
  $textimage_url = TextimageImager::getImageUrl('textimage_test', NULL, $field_value, 'png', TRUE, $node);
  $instance = field_info_instance('node', $field_name, 'article');
  $instance['display']['default']['type'] = 'textimage';
  $instance['display']['default']['settings']['image_style'] = 'textimage_test';
  $instance['display']['default']['settings']['image_text_values'] = 'merge';
  $instance['display']['default']['settings']['image_alt'] = 'Alternate text: [node:title]';
  $instance['display']['default']['settings']['image_title'] = 'Title: [node:title]';
  field_update_instance($instance);
  $this
    ->drupalGet('node/' . $nid);
  $elements = $this
    ->xpath('//div[contains(@class, :class)]/div[@class = "field-items"]/div/img', array(
    ':class' => 'field-name-' . $field_name,
  ));
  $this
    ->assertEqual(1, count($elements));
  $this
    ->assertEqual($textimage_url, $elements[0]['src'], 'Textimage has expected URL.');
  $this
    ->assertEqual('Alternate text: ' . $field_value[0], $elements[0]['alt'], 'Textimage has expected alt attribute.');
  $this
    ->assertEqual('Title: ' . $field_value[0], $elements[0]['title'], 'Textimage has expected title attribute.');

  // Test the textimage formatter - multiple images.
  $instance = field_info_instance('node', $field_name, 'article');
  $instance['display']['default']['settings']['image_text_values'] = 'itemize';
  field_update_instance($instance);
  $this
    ->drupalGet('node/' . $nid);
  $elements = $this
    ->xpath('//div[contains(@class, :class)]/div[@class = "field-items"]/div/img', array(
    ':class' => 'field-name-' . $field_name,
  ));
  $this
    ->assertEqual(4, count($elements));
  for ($i = 0; $i < 4; $i++) {
    $textimage_url = TextimageImager::getImageUrl('textimage_test', NULL, array(
      $field_value[$i],
    ), 'png', TRUE, $node);
    $this
      ->assertEqual($textimage_url, $elements[$i]['src'], 'Textimage has expected URL.');
    $this
      ->assertEqual('Alternate text: ' . $field_value[0], $elements[0]['alt'], 'Textimage has expected alt attribute.');
    $this
      ->assertEqual('Title: ' . $field_value[0], $elements[0]['title'], 'Textimage has expected title attribute.');
  }
}