<?php

/*
 * @file
 * Test suite for scale questions type module.
 */

class QuizEvaluationUnitTest extends DrupalWebTestCase {

  /*
   * The getInfo() method provides information about the test.
   * In order for the test to be run, the getInfo() method needs
   * to be implemented.
   */
  public static function getInfo() {
    return array(
      'name' => t('Quiz Evaluation Test'),
      'description' => t('Unit test for evaluation question type.'),
      'group' => t('Quiz Evaluation'),
    );
  }

  /*
   * Implementing setUp() to enable short_answer module testing
   */
  function setUp() {
    parent::setUp('quiz', 'multichoice', 'quiz_directions', 'quiz_question', 'short_answer', 'truefalse', 'long_answer', 'quiz_evaluation');

    // array of drupal user permission
    $permission = array(
      'bypass node access',
      'administer site configuration',
      'administer nodes',
      'access administration pages',
      'administer quiz configuration',
      'access quiz',
      'view any quiz results',
      'view own quiz results',
      'view results for own quiz',
      'delete any quiz results',
      'delete results for own quiz',
      'score any quiz',
      'score own quiz',
      'view quiz question outside of a quiz',
      'view any quiz question correct response',
      'edit question titles',
      'assign any action to quiz events',
      'manual quiz revisioning',
      'create quiz content',
      'edit own quiz content',
      'edit any quiz content',
      'delete own quiz content',
      'delete any quiz content',
      'create quiz_evaluation content',
      'edit own quiz_evaluation content',
      'edit any quiz_evaluation content',
      'delete own quiz_evaluation content',
      'delete any quiz_evaluation content'
    );

    // Create and log in our test user
    $user = $this->drupalCreateUser($permission);
    $this->drupalLogin($user);
  }

  /**
   * Create a quiz, add an evaluation question to it, score top marks.
   */
  public function testCreateEvaluationQuestion() {
    $path = '/node/add/quiz';
    $edit = array(
      'title' => 'Quiz Test',
      'body[und][0][value]' => 'Quiz Test',
    );
    $submit = 'Save';
    $this->drupalPost($path, $edit, $submit);

    $path = 'node/add/quiz-evaluation';
    $options = array(
      'destination' => 'node/1/questions',
      'quiz_nid' => '1',
      'quiz_vid' => '1'
    );
    $edit = array(
      'body[und][0][value]' => 'Evaluation Quiz Test',
      'title' => 'Evaluation Quiz Test',
      'quiz_evaluation_range' => 1,
      'quiz_evaluation_answers[0][title]' => 'Question 1',
      'quiz_evaluation_answers[1][title]' => 'Question 2',
      'add_directly[latest][1-1]' => '1-1'
    );
    $submit = 'Save';
    $this->drupalPost($path, $edit, $submit, $options);

    $this->drupalGet('node/1/take');
    $this->assertText('Question 1', 'Question 1 available on page.');
    $this->assertText('Question 2', 'Question 2 available on page.');
    $this->assertText('Excellent', 'The option for Excellent is available on the page.');

    $answers = array(
      'tries[answer_1]' => 4,
      'tries[answer_2]' => 4
    );
    $this->drupalPost('node/1/take', $answers, 'Finish');
    $this->assertText('Question Results', 'Results page found.');
    $this->assertText('You got 8 of 8 possible points', 'Score for quiz is correct.');
    $this->assertText('Your score: 100 %', 'Percentage score is correct.');
    $this->assertRaw('<td>Question 1</td><td>Excellent</td>', 'The answer to question 1 is excellent.');
  }

}