You are here

function ImagecacheTokenTests::testTokens in Imagecache Token 7

Text that the image style tokens exist.

File

tests/imagecache_token.test, line 49
Various tests for the Imagecache Token module.

Class

ImagecacheTokenTests
Test the Imagecache Token module.

Code

function testTokens() {
  $bundle_label = 'Article';
  $node_title = 'Test article';

  // Load the node form.
  $this
    ->drupalGet('node/add/article');
  $this
    ->assertResponse(200, 'Loaded the article node form.');
  $this
    ->assertText(strip_tags(t('Create @name', array(
    '@name' => $bundle_label,
  ))));

  // Generate an image file.
  $image = $this
    ->getTestImage();

  // Submit the form.
  $edit = array(
    'title' => $node_title,
    'body[und][0][value]' => 'Test article',
    'body[und][0][format]' => 'filtered_html',
    'files[field_image_und_0]' => drupal_realpath($image->uri),
  );
  $this
    ->drupalPost(NULL, $edit, t('Save'));
  $this
    ->assertResponse(200);
  $t_args = array(
    '@type' => $bundle_label,
    '%title' => $node_title,
  );
  $this
    ->assertText(strip_tags(t('@type %title has been created.', $t_args)), 'The node was created.');
  $matches = array();
  if (preg_match('@node/(\\d+)$@', $this
    ->getUrl(), $matches)) {
    $nid = end($matches);
    $this
      ->assertNotEqual($nid, 0, 'The node ID was extracted from the URL.');
    $node = node_load($nid);
    $this
      ->assertNotEqual($node, NULL, 'The node was loaded successfully.');
    $this
      ->assertEqual($node->field_image[LANGUAGE_NONE][0]['filename'], $image->filename, 'The image was uploaded successfully.');

    // Load the tokens for this object, verify the correct tokens exist.
    $tokens = token_build_tree('node', array(
      'data' => $node,
    ));
    $this
      ->assertTrue(isset($tokens['[node:field_image]']), 'The image field token exists.');
    $this
      ->assertTrue(isset($tokens['[node:field_image]']['children']), 'The image field token has child tokens.');
    foreach (array(
      'large',
      'medium',
      'thumbnail',
    ) as $style) {
      $this
        ->assertTrue(isset($tokens['[node:field_image]']['children']['[node:field_image:' . $style . ']']), 'The image style token exists.');
      $this
        ->assertTrue(isset($tokens['[node:field_image]']['children']['[node:field_image:' . $style . ']']['children']), 'The image style token has child tokens.');
      $attributes = array(
        'alt',
        'extension',
        'filesize',
        'first',
        'height',
        'mimetype',
        'path',
        'render',
        'second',
        'style_name',
        'third',
        'title',
        'uri',
        'width',
      );
      foreach ($attributes as $attribute) {
        $this
          ->assertTrue(isset($tokens['[node:field_image]']['children']['[node:field_image:' . $style . ']']['children']['[node:field_image:' . $style . ':' . $attribute . ']']), 'The "' . $attribute . '" image style token exists.');
      }
    }
  }
}