You are here

QuizRevisioningTest.php in Quiz 8.6

File

tests/src/Functional/QuizRevisioningTest.php
View source
<?php

namespace Drupal\Tests\quiz\Functional;

use Drupal;
use Drupal\quiz\Entity\QuizQuestion;

/**
 * Test quiz revisioning.
 *
 * @group Quiz
 */
class QuizRevisioningTest extends QuizTestBase {
  public static $modules = [
    'quiz_truefalse',
  ];

  /**
   * Test quiz revisioning.
   */
  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");
  }

  /**
   * Test quiz with revisioning off.
   */
  public function testQuizNoRevisioning() {
    $this
      ->drupalLogin($this->admin);
    $question_node = $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_node);
    $this
      ->drupalGet("quiz/{$quiz_node->id()}/take");
    $this
      ->assertText("Revision 1");

    // Finish the attempt.
    $this
      ->drupalPostForm(NULL, array(
      "question[{$question_node->id()}][answer]" => 1,
    ), t('Finish'));

    // Check blocked access to update quiz and question.
    $this
      ->drupalGet("quiz/{$quiz_node->id()}/edit");
    $this
      ->assertText('You must delete all attempts on this quiz before editing.');
    $this
      ->assertSession()
      ->elementAttributeExists('css', '#edit-submit', 'disabled');
    $this
      ->drupalGet("quiz-question/{$question_node->id()}/edit");
    $this
      ->assertText('You must delete all attempts on this question before editing.');
    $this
      ->assertSession()
      ->elementAttributeExists('css', '#edit-submit', 'disabled');

    // 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()}/edit");
    $this
      ->assertText('You should delete all attempts on this quiz before editing.');
    $this
      ->drupalPostForm(NULL, array(), t('Save'));
    $this
      ->drupalGet("quiz-question/{$question_node->id()}/edit");
    $this
      ->assertText('You should delete all attempts on this question before editing.');
    $this
      ->drupalPostForm(NULL, array(), t('Save'));
  }

}

Classes

Namesort descending Description
QuizRevisioningTest Test quiz revisioning.