<?php
/**
 * @file
 * Test definitions for the SuiteDesk dok module
 */
class StormdokTestCase extends DrupalWebTestCase {

  public static function getInfo() {
    return array(
      'name' => t('SuiteDesk Dok Functionality'),
      'description' => t('Test the functionality of the SuiteDesk Dok module'),
      'group' => 'Storm',
    );
  }

  public function setUp() {
    parent::setUp('storm', 'stormorganization', 'stormproject', 'stormtask', 'stormdok', 'stormperson');
  }

  public function testStormdokAccess() {
    $this->drupalGet('doks');
    $this->assertResponse(403, t('Make sure access is denied to SuiteDesk Doks list for anonymous user'));

    $basic_user = $this->drupalCreateUser();
    $this->drupalLogin($basic_user);
    $this->drupalGet('doks');
    $this->assertResponse(403, t('Make sure access is denied to SuiteDesk Doks list for basic user'));

    $privileged_user = $this->drupalCreateUser(array('Storm dok: access'));
    $this->drupalLogin($privileged_user);
    $this->drupalGet('doks');
    $this->assertText(t('Doks'), t('Make sure the correct page has been displayed by checking that the title is "Doks".'));
  }

  public function testStormdokCreate() {
    // Create and login user
    $user = $this->drupalCreateUser(array('Storm organization: add', 'Storm organization: view all', 'Storm project: add', 'Storm project: view all', 'Storm task: add', 'Storm task: view all', 'Storm dok: add', 'Storm dok: view all'));
    $this->drupalLogin($user);

    // Create organization and invoice
    $org = array(
      'title' => $this->randomName(32),
      'body' => $this->randomName(64),
    );
    $prj = array(
      'title' => $this->randomName(32),
      'organization_nid' => '1',
    );
    $task = array(
      'title' => $this->randomName(32),
      'body' => $this->randomName(64),
    );
    $dok = array(
      'title' => $this->randomName(32),
      'body' => $this->randomName(64),
    );
    $this->drupalPost('node/add/stormorganization', $org, t('Save'));
    $this->drupalPost('node/add/stormproject', $prj, t('Save'));
    $this->drupalPost('node/add/stormtask', $task, t('Save'));
    $this->drupalPost('node/add/stormdok', $dok, t('Save'));

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

  public function testStormdokList() {
    // Create and login user
    $userAll = $this->drupalCreateUser(array('Storm organization: add', 'Storm organization: view all', 'Storm dok: access', 'Storm dok: add', 'Storm dok: view all', 'Storm dok: edit all', 'Storm dok: delete all', 'Storm person: add'));
    $userOrg = $this->drupalCreateUser(array('Storm organization: add', 'Storm organization: view all', 'Storm dok: access', 'Storm dok: add', 'Storm dok: view of user organization', 'Storm dok: edit of user organization', 'Storm dok: delete of user organization'));
    $userOwn = $this->drupalCreateUser(array('Storm organization: add', 'Storm organization: view all', 'Storm dok: access', 'Storm dok: add', 'Storm dok: view own', 'Storm dok: edit own', 'Storm dok: delete own'));
    $userViewAllEditOwn = $this->drupalCreateUser(array('Storm organization: add', 'Storm organization: view all', 'Storm dok: access', 'Storm dok: add', 'Storm dok: view all', 'Storm dok: edit own', 'Storm dok: delete own'));

    $this->drupalLogin($userAll);

    // Create organization
    $org = array(
      'title' => $this->randomName(32),
      'body' => $this->randomName(64),
    );
    $this->drupalPost('node/add/stormorganization', $org, t('Save'));
    $org = node_load(array('title' => $org['title']));

    // Create organization
    $org2 = array(
      'title' => $this->randomName(32),
      'body' => $this->randomName(64),
    );
    $this->drupalPost('node/add/stormorganization', $org2, t('Save'));
    $org2 = node_load(array('title' => $org2['title']));

    // Create stormperson with organization to userOrg
    $personOrg = array(
      'title' => $this->randomName(32),
      'body' => $this->randomName(64),
      'organization_nid' => $org->nid,
      'user_name' => $userOrg->name,
    );
    $this->drupalPost('node/add/stormperson', $personOrg, t('Save'));

    // Create doks
    $dok1 = array(
      'organization_nid' => $org->nid,
      'title' => $this->randomName(32),
      'body' => $this->randomName(64),
    );
    $this->drupalPost('node/add/stormdok', $dok1, t('Save'));
    $dok1 = node_load(array('title' => $dok1['title']));

    $this->drupalLogin($userOwn);
    $dok2 = array(
      'title' => $this->randomName(32),
      'body' => $this->randomName(64),
      'organization_nid' => $org->nid,
    );
    $this->drupalPost('node/add/stormdok', $dok2, t('Save'));
    $dok2 = node_load(array('title' => $dok2['title']));

    $this->drupalLogin($userViewAllEditOwn);
    $dok3 = array(
      'title' => $this->randomName(32),
      'body' => $this->randomName(64),
      'organization_nid' => $org2->nid,
    );
    $this->drupalPost('node/add/stormdok', $dok3, t('Save'));
    $dok3 = node_load(array('title' => $dok3['title']));

    // Test for 'Storm dok: view all'
    $this->drupalLogin($userAll);
    $this->drupalGet('doks');

    $this->assertLink($dok1->title, 0, 'The Dok appears on the list');
    $this->assertRaw('node/'. $dok1->nid .'/edit', 'The Dok edit icon appears on the list');
    $this->assertRaw('node/'. $dok1->nid .'/delete', 'The Dok delete icon appears on the list');

    $this->assertLink($dok2->title, 0, 'The Dok appears on the list');
    $this->assertRaw('node/'. $dok2->nid .'/edit', 'The Dok edit icon appears on the list');
    $this->assertRaw('node/'. $dok2->nid .'/delete', 'The Dok delete icon appears on the list');

    $this->assertLink($dok3->title, 0, 'The Dok appears on the list');
    $this->assertRaw('node/'. $dok3->nid .'/edit', 'The Dok edit icon appears on the list');
    $this->assertRaw('node/'. $dok3->nid .'/delete', 'The Dok delete icon appears on the list');

    // Test for 'Storm dok: view of user organization'
    $this->drupalLogin($userOrg);
    $this->drupalGet('doks');

    $this->assertLink($dok1->title, 0, 'The Dok appears on the list');
    $this->assertRaw('node/'. $dok1->nid .'/edit', 'The Dok edit icon appears on the list');
    $this->assertRaw('node/'. $dok1->nid .'/delete', 'The Dok delete icon appears on the list');

    $this->assertLink($dok2->title, 0, 'The Dok appears on the list');
    $this->assertRaw('node/'. $dok2->nid .'/edit', 'The Dok edit icon appears on the list');
    $this->assertRaw('node/'. $dok2->nid .'/delete', 'The Dok delete icon appears on the list');

    $this->assertNoLink($dok3->title, 'The Dok does not appear on the list');
    $this->assertNoRaw('node/'. $dok3->nid .'/edit', 'The Dok edit icon does not appear on the list');
    $this->assertNoRaw('node/'. $dok3->nid .'/delete', 'The Dok delete icon does not appear on the list');

    // Test for 'Storm dok: view own'
    $this->drupalLogin($userOwn);
    $this->drupalGet('doks');

    $this->assertNoLink($dok1->title, 'The Dok does not appear on the list');
    $this->assertNoRaw('node/'. $dok1->nid .'/edit', 'The Dok edit icon does not appear on the list');
    $this->assertNoRaw('node/'. $dok1->nid .'/delete', 'The Dok delete icon does not appear on the list');

    $this->assertLink($dok2->title, 0, 'The Dok appears on the list');
    $this->assertRaw('node/'. $dok2->nid .'/edit', 'The Dok edit icon appears on the list');
    $this->assertRaw('node/'. $dok2->nid .'/delete', 'The Dok delete icon appears on the list');

    $this->assertNoLink($dok3->title, 'The Dok does not appear on the list');
    $this->assertNoRaw('node/'. $dok3->nid .'/edit', 'The Dok edit icon does not appear on the list');
    $this->assertNoRaw('node/'. $dok3->nid .'/delete', 'The Dok delete icon does not appear on the list');


    // Test for 'Storm dok: view all', 'Storm dok: edit own'
    $this->drupalLogin($userViewAllEditOwn);
    $this->drupalGet('doks');

    $this->assertLink($dok1->title, 0, 'The Dok appears on the list');
    $this->assertNoRaw('node/'. $dok1->nid .'/edit', 'The Dok edit icon does not appear on the list');
    $this->assertNoRaw('node/'. $dok1->nid .'/delete', 'The Dok edit icon does not appear on the list');

    $this->assertLink($dok2->title, 0, 'The Dok appears on the list');
    $this->assertNoRaw('node/'. $dok2->nid .'/edit', 'The Dok edit icon does not appear on the list');
    $this->assertNoRaw('node/'. $dok2->nid .'/delete', 'The Dok delete icon does not appear on the list');

    $this->assertLink($dok3->title, 0, 'The Dok appears on the list');
    $this->assertRaw('node/'. $dok3->nid .'/edit', 'The Dok edit icon appears on the list');
    $this->assertRaw('node/'. $dok3->nid .'/delete', 'The Dok delete icon appears on the list');

  }
}
