View source
<?php
namespace Drupal\Tests\quiz\Functional;
class QuizNavigationTest extends QuizTestBase {
protected static $modules = [
'quiz_truefalse',
];
public function testQuestionNavigationBasic() {
$this
->drupalLogin($this->admin);
$quiz_node = $this
->createQuiz();
$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
->assertText("Page 1 of 3");
$this
->assertResponse(200);
$this
->drupalGet("quiz/{$quiz_node->id()}/take/2");
$this
->assertResponse(403);
$this
->drupalGet("quiz/{$quiz_node->id()}/take/3");
$this
->assertResponse(403);
$this
->drupalGet("quiz/{$quiz_node->id()}/take/1");
$this
->drupalPostForm(NULL, [
"question[{$question1->id()}][answer]" => 1,
], t('Next'));
$this
->drupalGet("quiz/{$quiz_node->id()}/take/2");
$this
->assertText("Page 2 of 3");
$this
->assertResponse(200);
$this
->drupalGet("quiz/{$quiz_node->id()}/take/3");
$this
->assertResponse(403);
}
public function testQuestionNavigationJumping() {
$this
->drupalLogin($this->admin);
$quiz_node = $this
->createQuiz([
'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);
$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()}/take");
$this
->drupalGet("quiz/{$quiz_node->id()}/take/1");
$this
->assertResponse(200);
$this
->drupalGet("quiz/{$quiz_node->id()}/take/2");
$this
->assertResponse(200);
$this
->drupalGet("quiz/{$quiz_node->id()}/take/3");
$this
->assertResponse(200);
$this
->drupalGet("quiz/{$quiz_node->id()}/take");
$this
->assertFieldById('edit-question-number', NULL);
$this
->assertNoLinkByHref("quiz/{$quiz_node->id()}/take/1");
$config = \Drupal::configFactory()
->getEditable('quiz.settings');
$config
->set('pager_start', 5);
$config
->set('pager_siblings', 2);
$config
->save();
$this
->drupalGet("quiz/{$quiz_node->id()}/take/3");
$this
->assertNoFieldById('edit-question-number');
$this
->assertNoLink('1');
$this
->assertLinkByHref("quiz/{$quiz_node->id()}/take/2");
$this
->assertNoLinkByHref("quiz/{$quiz_node->id()}/take/3");
$this
->assertLinkByHref("quiz/{$quiz_node->id()}/take/4");
$this
->assertNoLink('5');
}
public function testQuestionNavigationSkipping() {
$this
->drupalLogin($this->admin);
$quiz_node = $this
->createQuiz([
'allow_skipping' => 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);
$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
->assertResponse(200);
$this
->drupalGet("quiz/{$quiz_node->id()}/take/2");
$this
->assertResponse(403);
$this
->drupalGet("quiz/{$quiz_node->id()}/take/3");
$this
->assertResponse(403);
$this
->drupalGet("quiz/{$quiz_node->id()}/take/1");
$this
->drupalPostForm(NULL, [], t('Leave blank'));
$this
->drupalGet("quiz/{$quiz_node->id()}/take/2");
$this
->assertResponse(200);
$this
->drupalGet("quiz/{$quiz_node->id()}/take/3");
$this
->assertResponse(403);
}
public function testQuestionNavigationBackwards() {
$this
->drupalLogin($this->admin);
$quiz_node = $this
->createQuiz([
'backwards_navigation' => 0,
'allow_skipping' => 0,
'allow_jumping' => 0,
]);
$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
->assertResponse(200);
$this
->drupalGet("quiz/{$quiz_node->id()}/take/2");
$this
->assertResponse(403);
$this
->drupalGet("quiz/{$quiz_node->id()}/take/3");
$this
->assertResponse(403);
$this
->drupalGet("quiz/{$quiz_node->id()}/take/1");
$this
->drupalPostForm(NULL, [
"question[{$question1->id()}][answer]" => 1,
], t('Next'));
$this
->drupalGet("quiz/{$quiz_node->id()}/take/2");
$this
->assertResponse(200);
$this
->drupalGet("quiz/{$quiz_node->id()}/take/1");
$this
->assertResponse(403);
}
}