View source
<?php
namespace Drupal\Tests\quiz\Functional;
class QuizBuildOnLastTest extends QuizTestBase {
public static $modules = array(
'quiz_truefalse',
'quiz_multichoice',
);
public function testBuildOnLastAttempt() {
$this
->drupalLogin($this->admin);
$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);
$this
->drupalGet("quiz/{$quiz->id()}/take");
$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'));
$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'));
$quiz
->set('build_on_last', 'all');
$quiz
->setNewRevision();
$quiz
->save();
$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'));
$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'));
}
}