quizfileupload.test in Quiz File Upload 7.5
Unit tests for the quizfileupload Module.
File
tests/quizfileupload.testView source
<?php
/**
* @file
* Unit tests for the quizfileupload Module.
*/
/**
* Test class for the file upload question type.
*/
class QuizFileUploadTestCase extends QuizQuestionTestCase {
var $question_node_type = 'quizfileupload';
public static function getInfo() {
return array(
'name' => t('File Upload'),
'description' => t('Unit test for file upload question type.'),
'group' => t('Quiz'),
);
}
public function setUp($modules = array(), $admin_permissions = array(), $user_permissions = array()) {
parent::setUp(array(
'quizfileupload',
), array(
'score any quiz',
));
}
/**
* Retrieves a sample file of the specified type.
*/
public function getTestFile($type_name, $size = NULL) {
// Get a file to upload.
$file = current($this
->drupalGetTestFiles($type_name, $size));
// Add a filesize property to files as would be read by file_load().
$file->filesize = filesize($file->uri);
return $file;
}
/**
* Test the creation of a file upload question.
*/
public function testCreateQuizQuestion($settings = array()) {
if (!$settings) {
$settings = array(
'filetypes' => QUIZFILEUPLOAD_DEFAULT_EXTENSIONS,
'correct_answer_evaluation' => QuizfileuploadQuestion::ANSWER_MATCH,
);
}
// Login as our privileged user.
$this
->drupalLogin($this->admin);
$question_node = $this
->drupalCreateNode(array(
'type' => $this->question_node_type,
'title' => 'SA 1 title',
'body' => array(
LANGUAGE_NONE => array(
array(
'value' => 'SA 1 body text.',
),
),
),
) + $settings);
return $question_node;
}
/**
* Test automatically graded questions.
*/
public function testAutomaticallyGradeAnswer() {
// 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 a fileupload question.
$quiz_node = $this
->drupalCreateQuiz();
$question_node = $this
->testCreateQuizQuestion(array(
'filetypes' => 'txt',
'correct_answer_evaluation' => QuizfileuploadQuestion::ANSWER_MATCH,
));
$this
->linkQuestionToQuiz($question_node, $quiz_node);
// Update the default max score for each question.
$this
->drupalPost('node/' . $quiz_node->nid . '/quiz/questions', array(
'max_scores[' . $question_node->nid . '-' . $question_node->vid . ']' => 5,
), t('Submit'));
// Test with a correct file extension.
$this
->drupalPost('node/' . $quiz_node->nid . '/take', array(
'files[question_' . $question_node->nid . '_answer]' => drupal_realpath($text_file->uri),
), t('Finish'));
$this
->assertText('Your score: 100%');
// Test with a wrong file extension.
$this
->drupalGet("node/{$quiz_node->nid}/take");
$this
->drupalPost('node/' . $quiz_node->nid . '/take', array(
'files[question_' . $question_node->nid . '_answer]' => drupal_realpath($image_file->uri),
), t('Finish'));
$this
->assertText('The specified file image-test.png could not be uploaded. Only files with the following extensions are allowed: txt.');
// Test without a file.
$this
->drupalGet("node/{$quiz_node->nid}/take");
$this
->drupalPost('node/' . $quiz_node->nid . '/take', array(), t('Finish'));
$this
->assertText('You must upload a file.');
// Test skipping the question.
$this
->drupalGet("node/{$quiz_node->nid}/take");
$this
->drupalPost('node/' . $quiz_node->nid . '/take', array(), t('Leave blank and finish'));
$this
->assertText('Your score: 0%');
}
/**
* Test manually graded questions.
*
* Note: we use two questions here to make sure the grading form is handled
* correctly.
*/
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.');
}
/**
* Test filesize validation.
*/
public function testFilesizeValidation() {
// Login as our privileged user.
$this
->drupalLogin($this->admin);
// Create file objects.
$small_file = $this
->getTestFile('text', 131072);
$large_file = $this
->getTestFile('text', 1310720);
// Create the quiz with a fileupload question.
$quiz_node = $this
->drupalCreateQuiz();
$question_node = $this
->testCreateQuizQuestion(array(
'filetypes' => 'txt',
'filesize' => '1MB',
'correct_answer_evaluation' => QuizfileuploadQuestion::ANSWER_MATCH,
));
$this
->linkQuestionToQuiz($question_node, $quiz_node);
// Update the default max score for each question.
$this
->drupalPost('node/' . $quiz_node->nid . '/quiz/questions', array(
'max_scores[' . $question_node->nid . '-' . $question_node->vid . ']' => 5,
), t('Submit'));
// Test uploading a small file.
$this
->drupalPost('node/' . $quiz_node->nid . '/take', array(
'files[question_' . $question_node->nid . '_answer]' => drupal_realpath($small_file->uri),
), t('Finish'));
$this
->assertText('Your score: 100%');
// Test uploading a file which is to large.
$this
->drupalGet("node/{$quiz_node->nid}/take");
$this
->drupalPost('node/' . $quiz_node->nid . '/take', array(
'files[question_' . $question_node->nid . '_answer]' => drupal_realpath($large_file->uri),
), t('Finish'));
$this
->assertText('You must upload a file.');
}
}
Classes
Name![]() |
Description |
---|---|
QuizFileUploadTestCase | Test class for the file upload question type. |