You are here

image_module.test in SimpleTest 5

File

tests/image_module.test
View source
<?php

class ImageModuleTest extends DrupalTestCase {
  function get_info() {
    return array(
      'name' => t('Image Module'),
      'desc' => t('Testing add/edit/delete image node. If GD tooklit is found, perform image crop test. Requires image.module'),
      'group' => 'Image',
    );
  }
  function setUp() {
    parent::setUp();
    $dir = file_directory_path() . '/' . variable_get('image_default_path', 'images');
    $temp_dir = $dir . '/temp';
    $dir_created = file_check_directory($dir, TRUE);
    $temp_dir_created = file_check_directory($temp_dir, TRUE);
    if (!$dir_created || !$temp_dir_created) {
      print "Images dir could not be create";
      exit;
    }
  }
  function _rolesApi($op, $edit) {
    if ($op == 'delete') {
      $id = $edit['rid'];
      db_query('DELETE FROM {role} WHERE rid = %d', $id);
      db_query('DELETE FROM {permission} WHERE rid = %d', $id);

      // Update the users who have this role set:
      $result = db_query('SELECT DISTINCT(ur1.uid) FROM {users_roles} ur1 LEFT JOIN {users_roles} ur2 ON ur2.uid = ur1.uid WHERE ur1.rid = %d AND ur2.rid != ur1.rid', $id);
      $uid = array();
      while ($u = db_fetch_object($result)) {
        $uid[] = $u->uid;
      }
      if ($uid) {
        db_query('DELETE FROM {users_roles} WHERE rid = %d AND uid IN (%s)', $id, implode(', ', $uid));
      }

      // Users with only the deleted role are put back in the authenticated users pool.
      db_query('UPDATE {users_roles} SET rid = %d WHERE rid = %d', _user_authenticated_id(), $id);
    }
    else {
      if ($op == 'add') {
        if (isset($edit['name'])) {
          db_query("INSERT INTO {role} (name) VALUES ('%s')", $edit['name']);
          $result = db_query("SELECT rid FROM {role} WHERE name = '%s'", $edit['name']);
          $rid = db_result($result);
          db_query("INSERT INTO {permission} (rid, perm) VALUES (%d, '%s')", $rid, $edit['perm']);
          return $rid;
        }
        else {
          return 0;
        }
      }
    }
  }
  function testImageNode() {
    global $base_url;
    $this
      ->drupalModuleEnable('image');

    //TODO: make sure image.module (and file api) is configured properly

    // create and login a new user
    $perm = array(
      'access content',
      'create images',
      'administer nodes',
    );
    $account = $this
      ->drupalCreateUserRolePerm($perm);
    $this
      ->drupalLoginUser($account);
    $title = $this
      ->randomName(10);
    $description = $this
      ->randomName(100);
    $img_path = dirname(__FILE__) . '/ImageModuleTest.png';
    $my_size = getimagesize($img_path);
    if (image_get_toolkit()) {

      // if we got gd we perform more precise test
      $temppath = variable_get('image_default_path', 'images') . '/temp';
      $path = realpath(file_create_path($temppath));
      $new_image = $path . '/ImageTestGd.png';
      $width = rand(20, $my_size[0]);
      $height = rand(20, $my_size[1]);
      image_crop($img_path, $new_image, 0, 0, $width, $height);
      $my_size = getimagesize($new_image);
      $img_path = $new_image;
    }

    // Add image
    unset($edit);
    $edit = array(
      'title' => $title,
      'body' => $description,
      'files[image]' => $img_path,
    );
    $this
      ->drupalPostRequest('node/add/image', $edit, 'Submit');
    $content = $this->_browser
      ->getContent();

    // test message
    $this
      ->assertWantedRaw(t('Your %post has been created.', array(
      '%post' => 'Image',
    )), "Checking message");

    // get node nid
    $node = node_load(array(
      'title' => $title,
    ));
    $this
      ->drupalGet(url("node/{$node->nid}", NULL, NULL, TRUE));
    $content = $this->_browser
      ->getContent();
    $this
      ->assertWantedText($title, "Checking title : {$title}");
    $this
      ->assertWantedText($description, "Checking body: {$description}");

    // sprawdz czy obrazek jest dobry
    preg_match_all('~<img.*?src="?([^"]*png?)"?[^>]*>~i', $content, $matches);
    $found = false;

    // TODO Make is to that getimagesize goes green on HTTP-AUTH
    // right now it will get denied.
    foreach ($matches[1] as $file) {
      if (strripos($file, "TestGD")) {
        $size = getimagesize($file);
        if ($size == $my_size) {
          $found = true;
          break;
        }
      }
    }
    $this
      ->assertTrue($found, 'Checking for presence and size of uploaded image');

    // update image node
    // TODO: upload a different file so we know that updating image files works
    $title = $this
      ->randomName(10);
    $description = $this
      ->randomName(100);
    $img_path = dirname(__FILE__) . '/ImageModuleTest.png';

    // powininem byc inny obrazek
    unset($edit);
    $edit = array(
      'title' => $title,
      'body' => $description,
      'files[image]' => $img_path,
    );
    $this
      ->drupalPostRequest('node/' . $node->nid . '/edit', $edit, 'Submit');
    $content = $this->_browser
      ->getContent();
    $this
      ->drupalGet(url("node/{$node->nid}"));
    $this
      ->assertWantedText($title, "Checking title : {$title}");
    $this
      ->assertWantedText($description, "Checking body: {$description}");
  }

}

Classes

Namesort descending Description
ImageModuleTest