<?php

/**
 * @file
 * Tests for the qforms extra module.
 */

class QformsExtrsTestCase extends DrupalWebTestCase {

  // Here we do not test custom format we test only ordinary date format
  // In selenium test custom format is tested
  const dateFormat = 'Y.d.m';

  private function getQformExtraDefinition() {
    return 'a:6:{s:23:"qforms_element_1_number";a:8:{s:4:"type";s:6:"number";s:2:"id";s:23:"qforms_element_1_number";s:6:"weight";s:1:"1";s:7:"visible";i:1;s:5:"title";s:20:"Enter your number id";s:11:"description";s:39:"Enter your number id for identification";s:8:"required";i:0;s:4:"size";s:0:"";}' .
    's:22:"qforms_element_2_email";a:8:{s:4:"type";s:5:"email";s:2:"id";s:22:"qforms_element_2_email";s:6:"weight";s:1:"2";s:7:"visible";i:1;s:5:"title";s:19:"Your e-mail address";s:11:"description";s:56:"Please enter your e-mail address that we can contact you";s:8:"required";i:0;s:4:"size";s:0:"";}' .
    's:20:"qforms_element_3_url";a:8:{s:4:"type";s:3:"url";s:2:"id";s:20:"qforms_element_3_url";s:6:"weight";s:1:"3";s:7:"visible";i:1;s:5:"title";s:17:"Your site address";s:11:"description";s:38:"If you have site you can enter address";s:8:"required";i:0;s:4:"size";s:0:"";}' .
    's:21:"qforms_element_4_date";a:9:{s:4:"type";s:4:"date";s:2:"id";s:21:"qforms_element_4_date";s:6:"weight";s:1:"4";s:7:"visible";i:1;s:5:"title";s:21:"Dead line for support";s:11:"description";s:29:"Date till we must support you";s:8:"required";i:0;s:4:"size";s:0:"";s:6:"format";s:5:"' . self::dateFormat . '";}' .
    's:23:"qforms_element_5_markup";a:5:{s:4:"type";s:6:"markup";s:2:"id";s:23:"qforms_element_5_markup";s:6:"weight";s:1:"5";s:7:"visible";i:1;s:6:"markup";s:49:"Our operator will contact you as soon as possible";}' .
    's:22:"qforms_last_element_id";i:5;}';
  }

  public static function getInfo() {
    return array(
      'name' => 'Qforms extra functionality',
      'description' => 'Tests functionalities of qforms extra module.',
      'group' => 'Qforms extra'
    );
  }

  protected function setUp() {
    parent::setUp('qforms', 'qforms_extra');

    $perm = array('create qform content', 'edit own qform content', 'delete own qform content', 'view and submit qform');
    $this->authUser = $this->drupalCreateUser($perm);
    $this->drupalLogin($this->authUser);
  }

  public function testQformExtraContent() {
    $this->addContent();
    $this->drupalGet('node/1');
    $passed = $this->assertText(t('Enter your number id'));
    $passed = $passed && $this->assertField('qforms_element_1_number', t('Founded input number.'));
    $passed = $passed && $this->assertText(t('Enter your number id for identification'));
    $this->assert($passed, t('First field of a type number is OK'));
    $passed = $this->assertText(t('Your e-mail address'));
    $passed = $passed && $this->assertField('qforms_element_2_email', t('Founded input email.'));
    $passed = $passed && $this->assertText(t('Please enter your e-mail address that we can contact you'));
    $this->assert($passed, t('Second field of a type email is OK'));
    $passed = $this->assertText(t('Your site address'));
    $passed = $passed && $this->assertFieldByName('qforms_element_3_url', 'http://', t('Founded input site with default value "http://".'));
    $passed = $passed && $this->assertText(t('If you have site you can enter address'));
    $this->assert($passed, t('Third field of a type url is OK'));
    $passed = $this->assertText(t('Dead line for support'));
    $passed = $passed && $this->assertField('qforms_element_4_date', t('Founded input date.'));
    $passed = $passed && $this-> assertText(t('Date till we must support you'));
    $this->assert($passed, t('First field of a type number is OK'));
    $passed = $this->assertText(t('Our operator will contact you as soon as possible'), t('Fifth field of type markup is OK'));

    $this->addData();
    // Test view of the node previously populated with data.
    $this->drupalGet('node/1/qforms-result/1');
    $passed = $this->assertFieldByName('qforms_element_1_number', 111, 'Number\'s value OK.');
    $passed = $this->assertFieldByName('qforms_element_2_email', 'someuser@somesite.domain', 'Email\'s value OK.') && $passed;
    $passed = $this->assertFieldByName('qforms_element_3_url', 'http://somesite.domain', 'Url\'s value OK.') && $passed;
    $passed = $this->assertFieldByName('qforms_element_4_date', '2008.02.11', 'Date\'s value OK.') && $passed;
    $passed = $this->assert($passed, 'View is OK.');


    // Try to submit invalid data.
    $this->addData(2, '11a', 'someuser#somesite', 'somesite.somedomain', '11-02-2011');
    $passed = $this->assertText('You need to enter a valid number.', 'Number validation OK.');
    $passed = $this->assertText('You need to enter a valid email address.', 'Email validation OK.') && $passed;
    $passed = $this->assertText('You need to enter a valid url.', 'Url validation OK.') && $passed;
    $passed = $this->assertText('You need to enter a valid date.', 'Date validation OK.') && $passed;
    $this->assert($passed, 'Validation is OK.');
  }

  public function testExport() {
    module_load_include('inc', 'qforms', 'qforms.pages_export');
    $this->addContent();
    // After data has been submited.
    $this->addData();
    $node = $this->drupalGetNodeByTitle(t('Support questionare'));
    $exported_results = qforms_results_submissions_export_base($node);
    $file_content = $exported_results['file_content'];

    $passed = (substr_count($file_content, 'Our operator will contact you as soon as possible') > 0) ? FALSE : TRUE;
    $this->assert($passed, t("Markup hasn't been exported"));
  }

  /**
   *  Add qform content/node.
   */
  public function addContent($qform_extra_def = NULL, $node_id = NULL) {
    $edit['title'] = t('Support questionare');
    if ($qform_extra_def == NULL) {
      $qform_extra_def = $this->getQformExtraDefinition();
    }
    if ($node_id == NULL) {
      $node_id = 1;
    }
    $this->drupalPost('node/add/qform', $edit, t('Save'));
    $this->assertText(t('Qform @title has been created.', array('@title' => $edit['title'])));
    $this->drupalGet('node/' . $node_id);

    $this->drupalGet('node/' . $node_id . '/edit');
    qforms_db_delete_form($node_id);
    qforms_db_save_form($node_id, $qform_extra_def, '');

    $this->drupalGet('node/' . $node_id . '/edit');
  }

  /**
   *  Popoulate form with data.
   */
  public function addData($node_id = 1, $number = 111, $email = 'someuser@somesite.domain',
    $url = 'http://somesite.domain', $date = '2008.02.11') {

    $this->drupalGet('node/' . $node_id);
    $datas['qforms_element_1_number'] = $number;
    $datas['qforms_element_2_email'] = $email;
    $datas['qforms_element_3_url'] = $url;
    $datas['qforms_element_4_date'] = $date;

    $this->drupalPost('node/1', $datas, t('Submit'));
    $this->assertText(t('Submission results'));
  }
}
