You are here

imagecache_token.test in Imagecache Token 7

Various tests for the Imagecache Token module.

File

tests/imagecache_token.test
View source
<?php

/**
 * @file
 * Various tests for the Imagecache Token module.
 */

/**
 * Test the Imagecache Token module.
 */
class ImagecacheTokenTests extends DrupalWebTestCase {

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'Imagecache Token tests',
      'description' => 'Test stuff.',
      'group' => 'Imagecache Token',
      'dependencies' => array(
        'token',
      ),
    );
  }

  /**
   * {@inheritdoc}
   */
  function setUp(array $modules = array()) {
    $modules[] = 'token';
    $modules[] = 'imagecache_token';
    parent::setUp($modules);

    // Create an admin user and log them in.
    $admin_user = $this
      ->drupalCreateUser(array(
      'bypass node access',
      'access content overview',
      'administer nodes',
    ));
    $this
      ->drupalLogin($admin_user);

    // Clear all of the token caches.
    token_clear_cache();
  }

  /**
   * Text that the image style tokens exist.
   */
  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.');
        }
      }
    }
  }

  /**
   * Generates a test image.
   *
   * @return stdClass
   *   A file object.
   */
  function getTestImage() {

    // Get a file to upload.
    $file = current($this
      ->drupalGetTestFiles('image'));

    // Add a filesize property to files as would be read by file_load().
    $file->filesize = filesize($file->uri);
    return $file;
  }

}

Classes

Namesort descending Description
ImagecacheTokenTests Test the Imagecache Token module.