You are here

public function QuizRevisioningTest::testQuizRevisioning in Quiz 8.6

Same name and namespace in other branches
  1. 8.5 tests/src/Functional/QuizRevisioningTest.php \Drupal\Tests\quiz\Functional\QuizRevisioningTest::testQuizRevisioning()
  2. 6.x tests/src/Functional/QuizRevisioningTest.php \Drupal\Tests\quiz\Functional\QuizRevisioningTest::testQuizRevisioning()

Test quiz revisioning.

File

tests/src/Functional/QuizRevisioningTest.php, line 20

Class

QuizRevisioningTest
Test quiz revisioning.

Namespace

Drupal\Tests\quiz\Functional

Code

public function testQuizRevisioning() {
  $config = Drupal::configFactory()
    ->getEditable('quiz.settings');
  $config
    ->set('revisioning', TRUE)
    ->save();
  $this
    ->drupalLogin($this->admin);
  $question = $this
    ->createQuestion(array(
    'title' => 'Revision 1',
    'body' => 'Revision 1',
    'type' => 'truefalse',
    'truefalse_correct' => 1,
    'feedback' => 'Question feedback for Revision 1',
  ));
  $quiz_node = $this
    ->linkQuestionToQuiz($question);
  $quiz_node
    ->set('allow_resume', 1)
    ->save();

  // Check for first revision.
  $this
    ->drupalLogin($this->user);
  $this
    ->drupalGet("quiz/{$quiz_node->id()}/take");
  $this
    ->assertText("Revision 1");

  // Attempt to update question. We have to create a new revision.
  $this
    ->drupalLogin($this->admin);
  $this
    ->drupalGet("quiz-question/{$question->id()}/edit");
  $this
    ->assertText('Warning: This question has attempts.');
  $this
    ->drupalPostForm(NULL, array(), t('Save'));
  $this
    ->assertText('Create new revision field is required.');
  $this
    ->drupalPostForm(NULL, array(
    'title[0][value]' => 'Revision 2',
    'body[0][value]' => 'Revision 2',
    'truefalse_correct' => 0,
    'feedback[0][value]' => 'Question feedback for Revision 2',
    'revision' => TRUE,
  ), t('Save'));

  // Reload the question to get current revision ID.
  Drupal::entityTypeManager()
    ->getStorage('quiz_question')
    ->resetCache();
  $question = QuizQuestion::load($question
    ->id());

  // As the quiz taker, finish out the attempt.
  $this
    ->drupalLogin($this->user);
  $this
    ->drupalGet("quiz/{$quiz_node->id()}/take");
  $this
    ->assertText("Revision 1");
  $this
    ->drupalPostForm(NULL, array(
    "question[{$question->id()}][answer]" => 1,
  ), t('Finish'));
  $this
    ->assertText('You got 1 of 1 possible points.');
  $this
    ->assertText('Question feedback for Revision 1');

  // Take quiz again. Should be on SAME revision of the question. We have not
  // yet updated the Quiz with the new revision of the Question.
  $this
    ->drupalGet("quiz/{$quiz_node->id()}/take");
  $this
    ->assertText("Revision 1");

  // We have an updated question and one Quiz revision with an attempt. We
  // need to update the quiz to use the new question. But there are attempts
  // on the quiz. Update the quiz to use the latest revision.
  $this
    ->drupalLogin($this->admin);
  $this
    ->drupalGet("quiz/{$quiz_node->id()}/questions");
  $this
    ->assertText('This quiz has been answered.');
  $this
    ->clickLink('create a new revision');
  $this
    ->assertText('Warning: This quiz has attempts.');
  $this
    ->drupalPostForm(NULL, array(
    'revision' => TRUE,
  ), t('Save'));
  $this
    ->assertNoText('This quiz has been answered.');
  $this
    ->drupalPostForm(NULL, array(
    "question_list[{$question->getRevisionId()}][question_vid]" => TRUE,
  ), t('Submit'));

  // Take quiz again. Should be on SAME revision. We have not yet finished
  // this attempt.
  $this
    ->drupalLogin($this->user);
  $this
    ->drupalGet("quiz/{$quiz_node->id()}/take");
  $this
    ->assertText("Revision 1");

  // Finish the attempt.
  $this
    ->drupalPostForm(NULL, array(
    "question[{$question->id()}][answer]" => 1,
  ), t('Finish'));
  $this
    ->assertText('You got 1 of 1 possible points.');
  $this
    ->assertText('Question feedback for Revision 1');

  // Take quiz again we should be on the new result, finally.
  $this
    ->drupalGet("quiz/{$quiz_node->id()}/take");
  $this
    ->assertText("Revision 2");

  // Finish the attempt.
  $this
    ->drupalPostForm(NULL, array(
    "question[{$question->id()}][answer]" => 1,
  ), t('Finish'));
  $this
    ->assertText('You got 0 of 1 possible points.');
  $this
    ->assertText('Question feedback for Revision 2');

  // Check admin override.
  $mega_admin = $this
    ->createUser([
    'administer quiz',
    'administer quiz_question',
    'override quiz revisioning',
  ]);
  $this
    ->drupalLogin($mega_admin);
  $this
    ->drupalGet("quiz/{$quiz_node->id()}/questions");
}