You are here

class ImageModuleTest in SimpleTest 5

Hierarchy

Expanded class hierarchy of ImageModuleTest

File

tests/image_module.test, line 3

View source
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}");
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalTestCase::$_cleanupModules property
DrupalTestCase::$_cleanupRoles property
DrupalTestCase::$_cleanupUsers property
DrupalTestCase::$_cleanupVariables property
DrupalTestCase::$_content property
DrupalTestCase::assertCopy function Will trigger a pass if both parameters refer to different objects. Fail otherwise.
DrupalTestCase::assertEqual function Will trigger a pass if the two parameters have the same value only. Otherwise a fail.
DrupalTestCase::assertError function Confirms that an error has occurred and optionally that the error text matches exactly.
DrupalTestCase::assertErrorPattern function Confirms that an error has occurred and that the error text matches a Perl regular expression.
DrupalTestCase::assertIdentical function Will trigger a pass if the two parameters have the same value and same type. Otherwise a fail.
DrupalTestCase::assertIsA function Type and class test. Will pass if class matches the type name or is a subclass or if not an object, but the type is correct.
DrupalTestCase::assertNoErrors function Confirms that no errors have occurred so far in the test method.
DrupalTestCase::assertNotA function Type and class mismatch test. Will pass if class name or underling type does not match the one specified.
DrupalTestCase::assertNotEqual function Will trigger a pass if the two parameters have a different value. Otherwise a fail.
DrupalTestCase::assertNotIdentical function Will trigger a pass if the two parameters have the different value or different type.
DrupalTestCase::assertNotNull function Will be true if the value is set.
DrupalTestCase::assertNoUnwantedPattern function Will trigger a pass if the Perl regex pattern is not present in subject. Fail if found.
DrupalTestCase::assertNoUnwantedRaw function Will trigger a pass if the raw text is NOT found on the loaded page Fail otherwise.
DrupalTestCase::assertNull function Will be true if the value is null.
DrupalTestCase::assertReference function Will trigger a pass if both parameters refer to the same object. Fail otherwise.
DrupalTestCase::assertWantedPattern function Will trigger a pass if the Perl regex pattern is found in the subject. Fail otherwise.
DrupalTestCase::assertWantedRaw function Will trigger a pass if the raw text is found on the loaded page Fail otherwise.
DrupalTestCase::clickLink function Follows a link by name. Will click the first link found with this link text by default, or a later one if an index is given. Match is case insensitive with normalised space. Does make assertations if the click was sucessful or not and it does…
DrupalTestCase::drupalCheckAuth function @abstract Checks to see if we need to send a http-auth header to authenticate when browsing a site.
DrupalTestCase::drupalCreateRolePerm function Create a role / perm combination specified by permissions
DrupalTestCase::drupalCreateUserRolePerm function Creates a user / role / permissions combination specified by permissions
DrupalTestCase::drupalGet function @abstract Brokder for the get function adds the authentication headers if necessary @author Earnest Berry III <earnest.berry@gmail.com>
DrupalTestCase::drupalGetContent function @TODO: needs documentation
DrupalTestCase::drupalLoginUser function Logs in a user with the internal browser
DrupalTestCase::drupalModuleDisable function Disables a drupal module
DrupalTestCase::drupalModuleEnable function Enables a drupal module
DrupalTestCase::drupalPostRequest function Do a post request on a drupal page. It will be done as usual post request with SimpleBrowser
DrupalTestCase::drupalRawPost function @abstract Broker for the post function adds the authentication headers if necessary @author Earnest Berry III <earnest.berry@gmail.com>
DrupalTestCase::DrupalTestCase function
DrupalTestCase::drupalVariableSet function Set a druapl variable and keep track of the changes for tearDown()
DrupalTestCase::randomName function Generates a random string, to be used as name or whatever
DrupalTestCase::run function Just some info for the reporter
DrupalTestCase::tearDown function tearDown implementation, setting back switched modules etc 1
ImageModuleTest::get_info function
ImageModuleTest::setUp function
ImageModuleTest::testImageNode function
ImageModuleTest::_rolesApi function