You are here

function ImageModuleTest::testImageNode in SimpleTest 5

File

tests/image_module.test, line 61

Class

ImageModuleTest

Code

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