public function QuizBuildOnLastTest::testBuildOnLastAttempt in Quiz 8.5
Same name and namespace in other branches
- 8.6 tests/src/Functional/QuizBuildOnLastTest.php \Drupal\Tests\quiz\Functional\QuizBuildOnLastTest::testBuildOnLastAttempt()
- 6.x tests/src/Functional/QuizBuildOnLastTest.php \Drupal\Tests\quiz\Functional\QuizBuildOnLastTest::testBuildOnLastAttempt()
Test the build on last attempt functionality.
File
- tests/
src/ Functional/ QuizBuildOnLastTest.php, line 17
Class
- QuizBuildOnLastTest
- Test quiz results behavior.
Namespace
Drupal\Tests\quiz\FunctionalCode
public function testBuildOnLastAttempt() {
$this
->drupalLogin($this->admin);
// Prepopulate correct answers.
$quiz = $this
->createQuiz(array(
'build_on_last' => 'correct',
));
$question1 = $this
->createQuestion(array(
'type' => 'truefalse',
'truefalse_correct' => 1,
));
$this
->linkQuestionToQuiz($question1, $quiz);
$question2 = $this
->createQuestion(array(
'type' => 'truefalse',
'truefalse_correct' => 1,
));
$this
->linkQuestionToQuiz($question2, $quiz);
$question3 = $this
->createQuestion(array(
'type' => 'truefalse',
'truefalse_correct' => 1,
));
$this
->linkQuestionToQuiz($question3, $quiz);
$question4 = $this
->createQuestion(array(
'type' => 'truefalse',
'truefalse_correct' => 1,
));
$this
->linkQuestionToQuiz($question4, $quiz);
$this
->drupalLogin($this->user);
// Take the quiz.
$this
->drupalGet("quiz/{$quiz->id()}/take");
// No build on last form here.
$this
->drupalPostForm(NULL, array(
"question[{$question1->id()}][answer]" => 1,
), t('Next'));
$this
->drupalPostForm(NULL, array(
"question[{$question2->id()}][answer]" => 0,
), t('Next'));
$this
->drupalPostForm(NULL, array(
"question[{$question3->id()}][answer]" => 1,
), t('Next'));
$this
->drupalPostForm(NULL, array(
"question[{$question4->id()}][answer]" => 0,
), t('Finish'));
// Take it again, ensure the correct answers are prefilled.
$this
->drupalGet("quiz/{$quiz->id()}/take");
$this
->drupalPostForm(NULL, array(
'build_on_last' => 'correct',
), t('Start @quiz', array(
'@quiz' => _quiz_get_quiz_name(),
)));
$this
->assertFieldChecked("edit-question-{$question1->id()}-answer-1");
$this
->drupalPostForm(NULL, array(
"question[{$question1->id()}][answer]" => 1,
), t('Next'));
$this
->assertNoFieldChecked("edit-question-{$question2->id()}-answer-1");
$this
->drupalPostForm(NULL, array(
"question[{$question2->id()}][answer]" => 0,
), t('Next'));
$this
->assertFieldChecked("edit-question-{$question3->id()}-answer-1");
$this
->drupalPostForm(NULL, array(
"question[{$question3->id()}][answer]" => 1,
), t('Next'));
$this
->assertNoFieldChecked("edit-question-{$question4->id()}-answer-1");
$this
->drupalPostForm(NULL, array(
"question[{$question4->id()}][answer]" => 0,
), t('Finish'));
// Switch the build option.
$quiz
->set('build_on_last', 'all');
$quiz
->setNewRevision();
$quiz
->save();
// @todo the change above would have forced a new revision, need to account
// for this in D8. User is getting a screen, they should not because there
// shouldn't be an existing attempt for this version.
// Take the quiz with this revision.
$this
->drupalGet("quiz/{$quiz->id()}/take");
$this
->drupalPostForm(NULL, array(
"question[{$question1->id()}][answer]" => 1,
), t('Leave blank'));
$this
->drupalPostForm(NULL, array(
"question[{$question2->id()}][answer]" => 0,
), t('Next'));
$this
->drupalPostForm(NULL, array(
"question[{$question3->id()}][answer]" => 1,
), t('Next'));
$this
->drupalPostForm(NULL, array(
"question[{$question4->id()}][answer]" => 0,
), t('Finish'));
// Take it again, ensure all the answers are prefilled.
$this
->drupalGet("quiz/{$quiz->id()}/take");
$this
->drupalPostForm(NULL, array(
'build_on_last' => 'all',
), t('Start @quiz', array(
'@quiz' => _quiz_get_quiz_name(),
)));
$this
->assertNoFieldChecked("edit-question-{$question1->id()}-answer-1");
$this
->assertNoFieldChecked("edit-question-{$question1->id()}-answer-0");
$this
->drupalPostForm(NULL, array(
"question[{$question1->id()}][answer]" => 1,
), t('Next'));
$this
->assertFieldChecked("edit-question-{$question2->id()}-answer-0");
$this
->drupalPostForm(NULL, array(
"question[{$question2->id()}][answer]" => 0,
), t('Next'));
$this
->assertFieldChecked("edit-question-{$question3->id()}-answer-1");
$this
->drupalPostForm(NULL, array(
"question[{$question3->id()}][answer]" => 1,
), t('Next'));
$this
->assertFieldChecked("edit-question-{$question4->id()}-answer-0");
$this
->drupalPostForm(NULL, array(
"question[{$question4->id()}][answer]" => 0,
), t('Finish'));
}