You are here

QuizAccessTest.php in Quiz 8.5

Unit tests for the quiz question Module.

File

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

namespace Drupal\Tests\quiz\Functional;

use Drupal\quiz_short_answer\Plugin\quiz\QuizQuestion\ShortAnswerQuestion;

/**
 * @file
 * Unit tests for the quiz question Module.
 */

/**
 * Test aspects of quiz access and permissions.
 *
 * @group Quiz
 */
class QuizAccessTest extends QuizTestBase {
  public static $modules = array(
    'quiz_short_answer',
  );

  /**
   * Test quiz authors being able to score results for own quiz.
   */
  public function testQuizOwnerResultEdit() {
    $grader = $this
      ->drupalCreateUser(array(
      'score own quiz',
      'view results for own quiz',
    ));
    $question = $this
      ->createQuestion(array(
      'type' => 'short_answer',
      'title' => 'SA 1 title',
      'correct_answer_evaluation' => ShortAnswerQuestion::ANSWER_MANUAL,
      'correct_answer' => 'blue',
      'body' => 'SA 1 body text',
    ));
    $quiz = $this
      ->createQuiz(array(
      'uid' => $grader
        ->id(),
    ));
    $this
      ->linkQuestionToQuiz($question, $quiz);
    $this
      ->drupalLogin($this->user);
    $this
      ->drupalGet("quiz/{$quiz->id()}/take");
    $this
      ->drupalPostForm(NULL, array(
      "question[{$question->id()}][answer]" => 'bluish',
    ), t('Finish'));

    // Score.
    $this
      ->drupalLogin($grader);
    $this
      ->drupalGet("quiz/{$quiz->id()}/result/1/edit");
    $this
      ->drupalPostForm(NULL, array(
      "question[{$question->id()}][score]" => 5,
    ), t('Save score'));
  }

  /**
   * Test quiz takers being able to grade their own results.
   */
  public function testQuizTakerAnswerScore() {
    $question = $this
      ->createQuestion(array(
      'type' => 'short_answer',
      'title' => 'SA 1 title',
      'correct_answer_evaluation' => ShortAnswerQuestion::ANSWER_MANUAL,
      'truefalse_correct' => 1,
      'body' => 'SA 1 body text',
    ));
    $quiz = $this
      ->linkQuestionToQuiz($question);
    $grader = $this
      ->drupalCreateUser(array(
      'update own quiz_result',
    ));
    $other = $this
      ->drupalCreateUser(array(
      'update own quiz_result',
    ));
    $this
      ->drupalLogin($grader);
    $this
      ->drupalGet("quiz/{$quiz->id()}/take");
    $this
      ->drupalPostForm(NULL, array(
      "question[{$question->id()}][answer]" => 'bluish',
    ), t('Finish'));

    // Make sure others cannot edit.
    $this
      ->drupalLogin($other);
    $this
      ->drupalGet("quiz/{$quiz->id()}/result/1/edit");
    $this
      ->assertResponse(403);

    // Score.
    $this
      ->drupalLogin($grader);
    $this
      ->drupalGet("quiz/{$quiz->id()}/result/1/edit");
    $this
      ->drupalPostForm(NULL, array(
      "question[{$question->id()}][score]" => 5,
    ), t('Save score'));
  }

}

Classes

Namesort descending Description
QuizAccessTest Test aspects of quiz access and permissions.