<?php
// $Id: image_attach.test,v 1.1 2010/01/10 17:33:50 joachim Exp $

/**
 * Test image functionality.
 */
class ImageAttachTestCase extends DrupalWebTestCase {

  protected $user_admin;
  protected $user_no_attach;
  protected $user_no_create;

  function getInfo() {
    return array(
      'name' => t('Image attach functionality'),
      'description' => t('Test Image attach module functionality.'),
      'group' => t('Image'),
    );
  }

  function setUp() {
    parent::setUp('image', 'node', 'image_attach');
    
    // Enable attaching on story nodes
    variable_set('image_attach_story', TRUE);  

    // User to create nodes etc.
    $this->user_admin = $this->drupalCreateUser(array(
      'create images', 'view original images', 'edit own images',
      'create story content', 'edit any story content',
      'administer content types',
      'attach images',
    ));
    $this->drupalLogin($this->user_admin);
    
    // Uploadable image.
    $this->image = 'misc/druplicon.png';
  }

  /**
   * Verify that the module's settings appear in the correct places.
   */
  function testSettings() {
    $this->drupalGet('admin/content/node-type/story');
    $this->assertText(t('Image Attach settings'), t('Image attach settings displayed on the story type settings page.'));
    
    $this->drupalGet('admin/content/node-type/image');
    $this->assertNoText(t('Image Attach settings'), t('Image attach settings are not available on the image type settings page.'));
  }
  
  /**
   * Verify that on the form for new nodes the image attach form component is 
   * presented in the correct way (or indeed not at all) for the various 
   * combinations of permissions.
   */
  function testAddNodeAttaching() {
    // Create an image.
    $edit = array(
      'title' => $this->randomName(),
      'body' => $this->randomName(),
      'files[image]' => realpath($this->image),
    );
    $this->drupalPost('node/add/image', $edit, t('Save'));

    // Check existing image nodes either show or don't show as potential attachments,
    // depending on the setting.
    variable_set('image_attach_existing', FALSE);
    $this->drupalGet('node/add/story');
    $this->assertNoFieldById('edit-iids', '', t("With 'Attached existing images' DISABLED, existing images selection box is NOT shown on a new node form."));
    
    variable_set('image_attach_existing', TRUE);
    $this->drupalGet('node/add/story');
    $this->assertFieldById('edit-iids', '', t("With 'Attached existing images' enabled, existing images selection box is shown on a new node form."));
    
    // Check that a user who may attach images but not create them cannot see
    // the upload part of the form
    $this->user_no_create = $this->drupalCreateUser(array(
      'attach images',
      'create story content', 'edit any story content',
    ));
    $this->drupalLogin($this->user_no_create);
    // ... with existing images disabled, no part of the form should be visible.
    variable_set('image_attach_existing', FALSE);
    $this->drupalGet('node/add/story');
    $this->assertNoText(t('Attached images'), t("With existing images DISABLED, a user without the 'create images' permission can NOT attach images at all."));
    
    // ... with exiting images enabled, they still can't upload.
    variable_set('image_attach_existing', TRUE);
    $this->drupalGet('node/add/story');
    $this->assertNoFieldById('edit-image', '', t("A user without the 'create images' permission can NOT upload a new image to attach."));
   
    // Check that a user who may not attach images cannot see
    // any part of the form.
    $this->user_no_attach = $this->drupalCreateUser(array(
      'create story content', 'edit any story content',
    ));
    $this->drupalLogin($this->user_no_attach);
    $this->drupalGet('node/add/story');
    $this->assertNoText(t('Attached images'), t("A user without the 'attach images' permission can NOT attach images at all."));
  }
  
  /**
   * Verify that on the form for existing nodes the image attach form component is 
   * presented in the correct way (or indeed not at all) for the various 
   * combinations of permissions.
   */
  function testEditNodeAttaching() {
    variable_set('image_attach_existing', TRUE);
    
    // Create an image.
    $edit = array(
      'title' => $this->randomName(),
      'body' => $this->randomName(),
      'files[image]' => realpath($this->image),
    );    
    $this->drupalPost('node/add/image', $edit, t('Save'));
    $attached_image_node = node_load(array('title' => $edit['title']));
    
    // Create a story, attaching the existing image node.
    $edit = array(
      'title' => $this->randomName(),
      'body' => $this->randomName(),
      'iids[]' => array($attached_image_node->nid),
    );
    $this->drupalPost('node/add/story', $edit, t('Save'));
    $story_node = node_load(array('title' => $edit['title']));

    //  file_put_contents('DEBUGstory.html', $this->drupalGetContent());
    /*
    // TODO: test images are attached properly whether existing or new files.
    $this->drupalGet('node/' . $story_node->nid);
    $this->assertPattern('@<img[^>]+?' . $image_node->images['thumbnail'] . '[^>]+?>@', t('Image thumbnail displayed on the story node page.'));
    */
    
    // Check that with existing images disabled 
    variable_set('image_attach_existing', FALSE);
    // Create an UNATTACHED image.
    $edit = array(
      'title' => $this->randomName(),
      'body' => $this->randomName(),
      'files[image]' => realpath($this->image),
    );    
    $this->drupalPost('node/add/image', $edit, t('Save'));
    $free_image_node = node_load(array('title' => $edit['title']));
    
    // TODO: test that only attached nodes are presented in the field -- ie that
    // $free_image_node is not available as an option.
    // NO IDEA how to do this in simpletest.
    /*
    $this->drupalGet('node/' . $story_node->nid);
    $this->assertFalse($this->setField('iids[]', $free_image_node->title));
    */
  }
}
