<?php

/**
 * @file
 * Tests for Feedback module.
 */

/**
 * Test basic Feedback functionality.
 */
class FeedbackTestCase extends DrupalWebTestCase {
  protected $profile = 'testing';

  public static function getInfo() {
    return array(
     'name' => 'Feedback functionality',
     'description' => 'Tests basic Feedback module functionality.',
     'group' => 'Feedback',
    );
  }

  function setUp() {
    // @todo Remove soft-dependency on Block.
    parent::setUp(array('block', 'feedback'));

    $this->admin_user = $this->drupalCreateUser(array('access feedback form', 'view feedback messages', 'administer feedback'));
    $this->web_user = $this->drupalCreateUser(array('access feedback form'));
    $this->drupalLogin($this->web_user);
  }

  /**
   * Test a basic feedback message.
   */
  function testFeedbackMessage() {
    $message = $this->randomString();
    $edit = array(
      'message' => $message,
    );
    $this->drupalPost('node', $edit, t('Send feedback'));

    // Verify the message was recorded.
    $this->drupalLogin($this->admin_user);
    $this->drupalGet('admin/reports/feedback');
    $this->assertRaw(check_plain($message), t('Message found.'));
    $this->assertText('node', t('Originating system path found.'));
    $this->assertLinkByHref(url('node', array('absolute' => TRUE)), 0, t('Originating absolute URL found.'));

    // Verify that we can process the message.
    $edit = array(
      'feedback-messages[0][1]' => TRUE,
    );
    $this->drupalPost(NULL, $edit, t('Submit'));
    $this->assertFieldByName('feedback-messages[1][1]', 1, t('Processed message found.'));
  }

  /**
   * Test visibility settings.
   */
   function testFeedbackVisibility() {
     $this->drupalLogin($this->admin_user);
     $this->drupalGet('user');
     $this->assertRaw('<span class="feedback-link">' . t('Feedback') . '</span>', t('Feedback form shown.'));
     $edit = array(
       'feedback_excluded_paths' => 'user*',
     );
     $this->drupalPost('admin/config/user-interface/feedback', $edit, t('Save configuration'));
     $this->drupalGet('user');
     $this->assertNoRaw('<span class="feedback-link">' . t('Feedback') . '</span>', t('Feedback form not shown.'));
   }

 /**
  * Test feedback deletion.
  */
  function testFeedbackDelete() {
    $this->drupalLogin($this->admin_user);
    $message = $this->randomString();
    $edit = array(
      'message' => $message,
    );
    $this->drupalPost('node', $edit, t('Send feedback'));

    // Verify a delete link is shown.
    $this->drupalGet('admin/reports/feedback');
    $this->assertLinkByHref('admin/reports/feedback/1/delete');

    // Verify deletion.
    $this->drupalPost('admin/reports/feedback/1/delete', array(), t('Delete'));
    $this->assertRaw(t('The feedback entry was deleted'), t('Feedback deletion message shown.'));
    $this->assertNoLinkByHref('admin/reports/feedback/1/delete');
    $this->assertNoRaw(check_plain($message), t('Message not found.'));
  }
}