You are here

public function QuizFileUploadTestCase::testGradeAnswerManualFeedback in Quiz File Upload 7.5

Test manually graded questions.

Note: we use two questions here to make sure the grading form is handled correctly.

File

tests/quizfileupload.test, line 117
Unit tests for the quizfileupload Module.

Class

QuizFileUploadTestCase
Test class for the file upload question type.

Code

public function testGradeAnswerManualFeedback() {

  // Login as our privileged user.
  $this
    ->drupalLogin($this->admin);

  // Create file objects.
  $text_file = $this
    ->getTestFile('text');
  $image_file = $this
    ->getTestFile('image');

  // Create the quiz with 2 questions.
  $quiz_node = $this
    ->drupalCreateQuiz();
  $question_node1 = $this
    ->testCreateQuizQuestion(array(
    'filetypes' => 'txt png',
    'correct_answer_evaluation' => QuizfileuploadQuestion::ANSWER_MANUAL,
  ));
  $this
    ->linkQuestionToQuiz($question_node1, $quiz_node);
  $question_node2 = $this
    ->testCreateQuizQuestion(array(
    'filetypes' => 'txt png',
    'correct_answer_evaluation' => QuizfileuploadQuestion::ANSWER_MANUAL,
  ));
  $this
    ->linkQuestionToQuiz($question_node2, $quiz_node);

  // Update the default max score for each question.
  $this
    ->drupalPost('node/' . $quiz_node->nid . '/quiz/questions', array(
    'max_scores[' . $question_node1->nid . '-' . $question_node1->vid . ']' => 5,
    'max_scores[' . $question_node2->nid . '-' . $question_node2->vid . ']' => 10,
  ), t('Submit'));

  // Test with a correct file extension.
  $this
    ->drupalLogin($this->user);
  $this
    ->drupalPost('node/' . $quiz_node->nid . '/take', array(
    'files[question_' . $question_node1->nid . '_answer]' => drupal_realpath($text_file->uri),
  ), t('Next'));
  $this
    ->drupalPost(NULL, array(
    'files[question_' . $question_node2->nid . '_answer]' => drupal_realpath($image_file->uri),
  ), t('Finish'));
  $this
    ->assertText('Your score: 0%');
  $this
    ->assertText('This answer has not yet been scored.');
  $this
    ->assertNoFieldByName('question[0][score]');
  $this
    ->assertNoFieldByName('question[1][score]');
  $this
    ->assertNoFieldByName('question[0][answer_feedback][value]');
  $this
    ->assertNoFieldByName('question[1][answer_feedback][value]');
  $this
    ->assertNoRaw(t('Save score'));
  $url_of_result = $this
    ->getUrl();
  $admin_url_of_result = preg_replace('#quiz-results/(\\d+)#', 'quiz/results/$1', $this
    ->getUrl());

  // Test grading the question.
  $this
    ->drupalLogin($this->admin);
  $this
    ->drupalGet($admin_url_of_result);
  $this
    ->assertFieldByName('question[0][score]');
  $this
    ->assertFieldByName('question[1][score]');
  $this
    ->drupalPost(NULL, array(
    "question[0][score]" => 5,
    "question[1][score]" => 7,
    "question[0][answer_feedback][value]" => 'Feedback for answer 1.',
    "question[1][answer_feedback][value]" => 'Feedback for answer 2.',
    "question[0][answer_feedback][format]" => 'filtered_html',
    "question[1][answer_feedback][format]" => 'filtered_html',
  ), t('Save score'));
  $this
    ->assertText('The scoring data you provided has been saved.');

  // Test if the score is visible to the user.
  $this
    ->drupalLogin($this->user);
  $this
    ->drupalGet($url_of_result);
  $this
    ->assertText('You got 12 of 15 possible points.');
  $this
    ->assertText('Your score: 80%');
  $this
    ->assertText('Feedback for answer 1.');
  $this
    ->assertText('Feedback for answer 2.');
}