View source
<?php
namespace Drupal\Tests\quiz\Functional;
use Drupal\quiz\Util\QuizUtil;
class QuizTakingTest extends QuizTestBase {
protected static $modules = [
'quiz_multichoice',
'quiz_directions',
'quiz_truefalse',
];
public function testQuizAvailability() {
$this
->drupalLogin($this->user);
$future = \Drupal::time()
->getRequestTime() + 86400;
$past = \Drupal::time()
->getRequestTime() - 86400;
$quiz_node_open = $this
->createQuiz([
'quiz_date' => [
'value' => date('Y-m-d\\TH:i:s', $past),
'end_value' => date('Y-m-d\\TH:i:s', $future),
],
]);
$this
->drupalGet("quiz/{$quiz_node_open->id()}");
$this
->assertNoText(t('This @quiz is closed.', [
'@quiz' => QuizUtil::getQuizName(),
]));
$this
->assertNoText(t('You are not allowed to take this @quiz.', [
'@quiz' => QuizUtil::getQuizName(),
]));
$quiz_node_future = $this
->createQuiz([
'quiz_date' => [
'value' => date('Y-m-d\\TH:i:s', $future),
'end_value' => date('Y-m-d\\TH:i:s', $future + 1),
],
]);
$this
->drupalGet("quiz/{$quiz_node_future->id()}");
$this
->assertText(t('This @quiz is not yet open.', [
'@quiz' => QuizUtil::getQuizName(),
]));
$this
->assertNoText(t('You are not allowed to take this @quiz.', [
'@quiz' => QuizUtil::getQuizName(),
]));
$this
->drupalGet("quiz/{$quiz_node_future->id()}/take");
$this
->assertText(t('This @quiz is not yet open.', [
'@quiz' => QuizUtil::getQuizName(),
]));
$quiz_node_past = $this
->createQuiz([
'quiz_date' => [
'value' => date('Y-m-d\\TH:i:s', $past),
'end_value' => date('Y-m-d\\TH:i:s', $past + 1),
],
]);
$this
->drupalGet("quiz/{$quiz_node_past->id()}");
$this
->assertText(t('This @quiz is closed.', [
'@quiz' => QuizUtil::getQuizName(),
]));
$this
->assertNoText(t('You are not allowed to take this @quiz.', [
'@quiz' => QuizUtil::getQuizName(),
]));
$this
->drupalGet("quiz/{$quiz_node_past->id()}/take");
$this
->assertText(t('This @quiz is closed.', [
'@quiz' => QuizUtil::getQuizName(),
]));
$quiz = $this
->createQuiz([]);
$this
->drupalGet("quiz/{$quiz->id()}");
$this
->assertNoText(t('This @quiz is closed.', [
'@quiz' => QuizUtil::getQuizName(),
]));
$this
->assertNoText(t('You are not allowed to take this @quiz.', [
'@quiz' => QuizUtil::getQuizName(),
]));
}
public function testViewQuestionsOutsideQuiz() {
$this
->drupalLogin($this->admin);
$quiz_node = $this
->createQuiz();
$question1 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
]);
$this
->linkQuestionToQuiz($question1, $quiz_node);
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz-question/{$question1->id()}");
$this
->assertResponse(403);
$user_with_privs = $this
->drupalCreateUser([
'view quiz_question',
'access quiz',
]);
$this
->drupalLogin($user_with_privs);
$this
->drupalGet("quiz-question/{$question1->id()}");
$this
->assertResponse(200);
}
public function testChangeAnswer() {
$quiz_node = $this
->createQuiz([
'review_options' => [
'question' => [
'score' => 'score',
],
],
]);
$question1 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
]);
$this
->linkQuestionToQuiz($question1, $quiz_node);
$question2 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
]);
$this
->linkQuestionToQuiz($question2, $quiz_node);
$question3 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
]);
$this
->linkQuestionToQuiz($question3, $quiz_node);
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz_node->id()}/take");
$this
->drupalGet("quiz/{$quiz_node->id()}/take/1");
$this
->drupalPostForm(NULL, [
"question[{$question1->id()}][answer]" => 0,
], t('Next'));
$this
->assertText('Score: 0 of 1');
$this
->drupalGet("quiz/{$quiz_node->id()}/take/1");
$this
->drupalPostForm(NULL, [
"question[{$question1->id()}][answer]" => 1,
], t('Next'));
$this
->assertText('Score: 1 of 1');
$this
->drupalGet("quiz/{$quiz_node->id()}/take/1");
$this
->drupalPostForm(NULL, [
"question[{$question1->id()}][answer]" => 0,
], t('Next'));
$this
->assertText('Score: 0 of 1');
$quiz_node
->set('allow_change', 0);
$quiz_node
->save();
$this
->drupalGet("quiz/{$quiz_node->id()}/take/1");
$this
->assertSession()
->fieldDisabled('edit-question-1-answer-1');
$this
->drupalPostForm(NULL, [], t('Next'));
$this
->assertText('Score: 0 of 1');
$this
->drupalGet("quiz/{$quiz_node->id()}/take/2");
$this
->drupalPostForm(NULL, [], t('Leave blank'));
$this
->drupalGet("quiz/{$quiz_node->id()}/take/2");
$this
->assertSession()
->fieldDisabled('edit-question-2-answer-1');
$quiz_node
->set('allow_change_blank', 1);
$quiz_node
->save();
$this
->drupalGet("quiz/{$quiz_node->id()}/take/2");
$this
->assertSession()
->fieldEnabled('edit-question-2-answer-1');
}
public function testQuizMaxAttemptsMessage() {
$quiz_node = $this
->createQuiz([
'takes' => 2,
]);
$question1 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
]);
$this
->linkQuestionToQuiz($question1, $quiz_node);
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz_node->id()}/take");
$this
->drupalGet("quiz/{$quiz_node->id()}/take/1");
$this
->drupalPostForm(NULL, [
"question[{$question1->id()}][answer]" => 0,
], t('Finish'));
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz_node->id()}");
$this
->assertText('You can only take this Quiz 2 times. You have taken it 1 time.');
$quiz_node
->set('show_attempt_stats', 0)
->save();
$this
->drupalGet("quiz/{$quiz_node->id()}");
$this
->assertNoText('You can only take this Quiz 2 times. You have taken it 1 time.');
}
public function testQuizMaxAttempts() {
$quiz_node = $this
->createQuiz([
'takes' => 2,
]);
$question1 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
]);
$this
->linkQuestionToQuiz($question1, $quiz_node);
$question2 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
]);
$this
->linkQuestionToQuiz($question2, $quiz_node);
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz_node->id()}/take");
$this
->drupalGet("quiz/{$quiz_node->id()}/take/1");
$this
->drupalPostForm(NULL, [
"question[{$question1->id()}][answer]" => 0,
], t('Next'));
$this
->drupalPostForm(NULL, [
"question[{$question2->id()}][answer]" => 0,
], t('Finish'));
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz_node->id()}");
$this
->assertText('You can only take this Quiz 2 times. You have taken it 1 time.');
$this
->drupalGet("quiz/{$quiz_node->id()}/take");
$this
->drupalPostForm(NULL, [
"question[{$question1->id()}][answer]" => 0,
], t('Next'));
$this
->drupalGet("quiz/{$quiz_node->id()}");
$this
->assertNoText('You can only take this Quiz 2 times. You have taken it 1 time.');
$this
->drupalGet("quiz/{$quiz_node->id()}/take/2");
$this
->drupalPostForm(NULL, [
"question[{$question2->id()}][answer]" => 0,
], t('Finish'));
$this
->drupalGet("quiz/{$quiz_node->id()}");
$this
->assertText('You have already taken this Quiz 2 times. You may not take it again.');
}
public function testAnswerSkipped() {
$this
->drupalLogin($this->admin);
$quiz_node = $this
->createQuiz([
'allow_skipping' => 1,
'allow_jumping' => 1,
]);
$question1 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
]);
$this
->linkQuestionToQuiz($question1, $quiz_node);
$question2 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
]);
$this
->linkQuestionToQuiz($question2, $quiz_node);
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz_node->id()}/take");
$this
->drupalGet("quiz/{$quiz_node->id()}/take/1");
$this
->drupalPostForm(NULL, [], t('Leave blank'));
$this
->drupalGet("quiz/{$quiz_node->id()}/take/1");
$this
->drupalPostForm(NULL, [
"question[{$question1->id()}][answer]" => 1,
], t('Next'));
$this
->drupalPostForm(NULL, [
"question[{$question2->id()}][answer]" => 1,
], t('Finish'));
$this
->assertText("Your score: 100%");
}
public function testAnswerOnOldQuizRevisioning() {
$this
->drupalLogin($this->admin);
$question1 = $this
->createQuestion([
'title' => 'Q 1',
'body' => 'Q 1',
'type' => 'truefalse',
'truefalse_correct' => 1,
]);
$quiz_node = $this
->linkQuestionToQuiz($question1);
$question2 = $this
->createQuestion([
'title' => 'Q 2',
'body' => 'Q 2',
'type' => 'truefalse',
'truefalse_correct' => 1,
]);
$this
->linkQuestionToQuiz($question2, $quiz_node);
$question1->revision = TRUE;
$question1
->save();
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz_node->id()}/take");
$this
->drupalGet("quiz/{$quiz_node->id()}/take/1");
$this
->drupalPostForm(NULL, [], t('Leave blank'));
$this
->drupalGet("quiz/{$quiz_node->id()}/take/1");
$this
->drupalPostForm(NULL, [
"question[{$question1->id()}][answer]" => 1,
], t('Next'));
}
public function testQuestionCount() {
$quiz_node = $this
->createQuiz([
'review_options' => [
'question' => [
'score' => 'score',
],
],
]);
$question1 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
]);
$this
->linkQuestionToQuiz($question1, $quiz_node);
$question2 = $this
->createQuestion([
'type' => 'directions',
]);
$this
->linkQuestionToQuiz($question2, $quiz_node);
$question3 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
]);
$this
->linkQuestionToQuiz($question3, $quiz_node);
$question4 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
]);
$this
->linkQuestionToQuiz($question4, $quiz_node);
$question5 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
]);
$this
->linkQuestionToQuiz($question5, $quiz_node);
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz_node->id()}");
$this
->assertText("4");
}
public function testMarkDoubtful() {
$this
->drupalLogin($this->admin);
$quiz_node = $this
->createQuiz([
'allow_skipping' => 1,
'allow_jumping' => 1,
'mark_doubtful' => 1,
]);
$question1 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
]);
$this
->linkQuestionToQuiz($question1, $quiz_node);
$question2 = $this
->createQuestion([
'type' => 'directions',
]);
$this
->linkQuestionToQuiz($question2, $quiz_node);
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz_node->id()}/take");
$this
->drupalGet("quiz/{$quiz_node->id()}/take/1");
$this
->assertField("edit-question-{$question1->id()}-is-doubtful");
$this
->drupalPostForm(NULL, [
"question[{$question1->id()}][answer]" => 1,
"question[{$question1->id()}][is_doubtful]" => 1,
], t('Next'));
$this
->drupalGet("quiz/{$quiz_node->id()}/take/1");
$this
->assertFieldChecked("edit-question-{$question1->id()}-is-doubtful");
$this
->drupalGet("quiz/{$quiz_node->id()}/take/2");
$this
->assertNoField("edit-question-{$question2->id()}-is-doubtful");
}
}