<?php
// $Id: image_import.test,v 1.3 2009/08/08 13:32:54 joachim Exp $

class ImageImportTest extends DrupalTestCase {


  function get_info() {
    return array('name' => t('Image Import tests'), 'desc' => t('Test Image Import module functionality.'), 'group' => 'Image');
  }

  function setUp() {
    parent::setUp();
    
    // Set an image import patch.
    $image_import_path = file_directory_path() . '/' . $this->randomName();
    file_check_directory($image_import_path, FILE_CREATE_DIRECTORY);
    $this->drupalVariableSet('image_import_path', $image_import_path);
    
    // User to create image galleries.
    $this->web_user = $this->drupalCreateUserRolePerm(array('create images', 'view original images', 'edit own images', 'edit any images', 'administer image galleries', 'import images'));
    $this->drupalGet('logout');
    $this->drupalLoginUser($this->web_user);
  }
  

  function testImport() {
    // Get 10 random images from 'misc' directory.
    $images_count = 10;
    $images_misc = file_scan_directory('misc', '.png');
    shuffle($images_misc);
    $images = array_slice($images_misc, 0, $images_count);
    
    $image_prefix = $this->randomName();
    $edit = array();
    $i = 1;
    foreach ($images as $image) {
      // Copy each image to import directory with random names.
      file_copy($image->filename, variable_get('image_import_path', '') . '/' . $image_prefix . $image->basename);
      $edit['import_file['. $i .']'] = $i;
      $edit['title['. $i .']'] = $image_prefix . $image->basename;
      $edit['body['. $i .']'] = $image_prefix . $image->basename;
      $i++;
    }
    $this->drupalPost('admin/content/image_import', $edit, 'Import');
    $this->assertWantedRaw('Successfully imported', 'Successfully imported. %s');

    // Check each image is a node now.
    foreach ($images as $image) {
      $node = node_load(array('title' => $image_prefix . $image->basename));
      $this->assertTrue($node, 'Image '. $image_prefix . $image->basename .' successfully imported. %s');
    }
    // @todo import into a gallery.
  }
  
  function tearDown() {
    @rmdir(variable_get('image_import_path', ''));

    parent::tearDown();
  }
}
