View source
<?php
namespace Drupal\Tests\quiz\Functional;
use Drupal\quiz\Util\QuizUtil;
class QuizBuildOnLastTest extends QuizTestBase {
protected static $modules = [
'quiz_truefalse',
'quiz_multichoice',
];
public function testBuildOnLastAttempt() {
$this
->drupalLogin($this->admin);
$quiz = $this
->createQuiz([
'build_on_last' => 'correct',
]);
$question1 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
]);
$this
->linkQuestionToQuiz($question1, $quiz);
$question2 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
]);
$this
->linkQuestionToQuiz($question2, $quiz);
$question3 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
]);
$this
->linkQuestionToQuiz($question3, $quiz);
$question4 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
]);
$this
->linkQuestionToQuiz($question4, $quiz);
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz->id()}/take");
$this
->drupalPostForm(NULL, [
"question[{$question1->id()}][answer]" => 1,
], t('Next'));
$this
->drupalPostForm(NULL, [
"question[{$question2->id()}][answer]" => 0,
], t('Next'));
$this
->drupalPostForm(NULL, [
"question[{$question3->id()}][answer]" => 1,
], t('Next'));
$this
->drupalPostForm(NULL, [
"question[{$question4->id()}][answer]" => 0,
], t('Finish'));
$this
->drupalGet("quiz/{$quiz->id()}/take");
$this
->drupalPostForm(NULL, [
'build_on_last' => 'correct',
], t('Start @quiz', [
'@quiz' => QuizUtil::getQuizName(),
]));
$this
->assertFieldChecked("edit-question-{$question1->id()}-answer-1");
$this
->drupalPostForm(NULL, [
"question[{$question1->id()}][answer]" => 1,
], t('Next'));
$this
->assertNoFieldChecked("edit-question-{$question2->id()}-answer-1");
$this
->drupalPostForm(NULL, [
"question[{$question2->id()}][answer]" => 0,
], t('Next'));
$this
->assertFieldChecked("edit-question-{$question3->id()}-answer-1");
$this
->drupalPostForm(NULL, [
"question[{$question3->id()}][answer]" => 1,
], t('Next'));
$this
->assertNoFieldChecked("edit-question-{$question4->id()}-answer-1");
$this
->drupalPostForm(NULL, [
"question[{$question4->id()}][answer]" => 0,
], t('Finish'));
$quiz
->set('build_on_last', 'all');
$quiz
->setNewRevision();
$quiz
->save();
$this
->drupalGet("quiz/{$quiz->id()}/take");
$this
->drupalPostForm(NULL, [
"question[{$question1->id()}][answer]" => 1,
], t('Leave blank'));
$this
->drupalPostForm(NULL, [
"question[{$question2->id()}][answer]" => 0,
], t('Next'));
$this
->drupalPostForm(NULL, [
"question[{$question3->id()}][answer]" => 1,
], t('Next'));
$this
->drupalPostForm(NULL, [
"question[{$question4->id()}][answer]" => 0,
], t('Finish'));
$this
->drupalGet("quiz/{$quiz->id()}/take");
$this
->drupalPostForm(NULL, [
'build_on_last' => 'all',
], t('Start @quiz', [
'@quiz' => QuizUtil::getQuizName(),
]));
$this
->assertNoFieldChecked("edit-question-{$question1->id()}-answer-1");
$this
->assertNoFieldChecked("edit-question-{$question1->id()}-answer-0");
$this
->drupalPostForm(NULL, [
"question[{$question1->id()}][answer]" => 1,
], t('Next'));
$this
->assertFieldChecked("edit-question-{$question2->id()}-answer-0");
$this
->drupalPostForm(NULL, [
"question[{$question2->id()}][answer]" => 0,
], t('Next'));
$this
->assertFieldChecked("edit-question-{$question3->id()}-answer-1");
$this
->drupalPostForm(NULL, [
"question[{$question3->id()}][answer]" => 1,
], t('Next'));
$this
->assertFieldChecked("edit-question-{$question4->id()}-answer-0");
$this
->drupalPostForm(NULL, [
"question[{$question4->id()}][answer]" => 0,
], t('Finish'));
}
}