Skip to content
question_fields.test 3.08 KiB
Newer Older
<?php

/**
 * @file
 * Simpletests for Question Fields 7.x.
 *
 * Verify Question fields functionality.
 */

/**
 * Functionality tests for question module.
 */
class QuestionFieldsTestCase extends DrupalWebTestCase {

  public static function getInfo() {
    return array(
      'name' => 'Question Fields',
      'description' => 'Verify functionality of the question fields module.',
      'group' => 'Question',
    );
  }

  function setUp() {
    // Enable the module.
    parent::setUp('question', 'question_fields');
    // Create a user that has the privilege to ask questions
    $this->question_user = $this->drupalCreateUser(array('ask questions'));
    // Create a user that has the privilege to manage questions
    $this->question_admin_user = $this->drupalCreateUser(array('view questions', 'answer questions', 'delete questions'));
    // Create a site admin user
    $this->site_admin_user = $this->drupalCreateUser(array('administer site configuration'));
  }

  /**
   * Verify the functionality of the example module.
   */
  function testQuestionCreation() {

    // Login as a site admin user
    $this->drupalLogin($this->question_user); 
  
    // Check that unauthorised users cannot ask questions
    $this->drupalGet('question');
    
    $this->assertFieldByName('questioner', '', t("Question submission form contains field for questioner name"));
    $this->assertFieldByName('age', '', t("Question submission form contains field for age"));
    $this->assertFieldByName('sex', '', t("Question submission form contains field for sex"));
 
        // Ask a question
    $this->drupalPost(
             'question', // The menu item of the form
             array(
               'question' => 'This is a test question',
               'questioner' => 'Bob',
               'age' => '10',
               'sex' => 'M',
               ),
               t('Submit Question')
               
             );
  
    // Check that there is one question in the database
    $count = db_query('SELECT COUNT(*) FROM {question_queue} q INNER JOIN {question_fields} qf ON q.qid = qf.qid')->fetchField();
    $this->assertEqual($count, 1, t("There is one question with additional fields in the question queue table."));

    // Login as question admin user
    $this->drupalLogout($this->question_user);
    $this->drupalLogin($this->question_admin_user);
             
    // Check that the questions appear on the queue page
    $this->drupalGet('admin/content/question');
    $this->assertPattern("/Bob.*10.*M/s", t("Additional field values found on question queue page"));
    
    // Go to the page to answer the first question
    $this->drupalGet('node/add/question/1');
    $this->assertFieldByName('question_fields_questioner[und][0][value]', 'Bob', t("The questioner field is correctly pre-populated on node creation form"));
    $this->assertFieldByName('question_fields_age[und][0][value]', '10', t("The age field is correctly pre-populated on node creation form"));
    $this->assertFieldByName('question_fields_sex[und][0][value]', 'M', t("The questioner field is correctly pre-populated on node creation form"));

  } 
}