<?php
/**
 * @file
 * Test definitions for the PM Expense module
 */

/**
 * Defines tests for the PM Expense module.
 */
class PMExpenseTestCase extends DrupalWebTestCase {

  /**
   * Defines metadata for test case.
   */
  public static function getInfo() {
    return array(
      'name' => t('PM Expense Functionality'),
      'description' => t('Test the functionality of the PM Expense module'),
      'group' => 'Project Management',
    );
  }

  /**
   * Defines modules to be enabled for each test run.
   */
  public function setUp() {
    parent::setUp('pm', 'pmorganization', 'pmproject', 'pmtask', 'pmticket', 'pmexpense');
  }

  /**
   * Tests access to various Expense pages.
   */
  public function testpmexpenseAccess() {
    $this->drupalGet('pm/expenses');
    $this->assertResponse(403, t('Make sure access is denied to Project Management Expense list for anonymous user'));

    $basic_user = $this->drupalCreateUser();
    $this->drupalLogin($basic_user);
    $this->drupalGet('pm/expenses');
    $this->assertResponse(403, t('Make sure access is denied to Project Management Expense list for basic user'));

    $privileged_user = $this->drupalCreateUser(array('Project Management expense: access'));
    $this->drupalLogin($privileged_user);
    $this->drupalGet('pm/expenses');
    $this->assertText(t('Expenses'), t('Make sure the correct page has been displayed by checking that the title is "Expenses".'));
  }

  /**
   * Tests creation of an Expense.
   */
  public function testpmexpenseCreate() {
    // Create and login user
    $user = $this->drupalCreateUser(array('Project Management Organization: add', 'Project Management Organization: view all', 'Project Management expense: add', 'Project Management expense: view all', 'Project Management Project: view all', 'Project Management Task: view all'));
    $this->drupalLogin($user);

    // Create an organization
    $org = array(
      'title' => $this->randomName(32),
    );

    $expense = array(
      'organization_nid' => '1',
      'title' => $this->randomName(32),
    );

    $this->drupalPost('node/add/pmorganization', $org, t('Save'));

    $this->drupalPost('node/add/pmexpense', $expense, t('Save'));

    $this->assertText(t('Expense @title has been created.', array('@title' => $expense['title'])));
  }

}

