You are here

QuizCreationTest.php in Quiz 8.6

File

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

namespace Drupal\Tests\quiz\Functional;


/**
 * Test aspects of quiz creation including global and user defaults.
 *
 * @group Quiz
 */
class QuizCreationTest extends QuizTestBase {
  public static $modules = array(
    'quiz_truefalse',
  );

  /**
   * Test basic quiz creation.
   */
  public function testQuizCreation() {
    $this
      ->drupalLogin($this->admin);
    $this
      ->drupalGet("quiz/add/quiz");
    $this
      ->drupalPostForm(NULL, array(
      'title[0][value]' => 'Test quiz creation',
      'body[0][value]' => 'Test quiz description',
    ), t('Save'));
    $this
      ->assertText('Manage questions');
  }

  /**
   * Test system, global, and user defaults.
   */
  public function testQuizDefaults() {

    // We need another admin user here.
    $admin_permissions = array();
    $admin_permissions[] = 'administer quiz configuration';
    $admin_permissions[] = 'create quiz quiz';
    $admin_permissions[] = 'update any quiz quiz';
    $admin_permissions[] = 'edit question titles';
    $admin_permissions[] = 'access quiz';
    $this->admin2 = $this
      ->drupalCreateUser(array_unique($admin_permissions));
    $this
      ->drupalLogin($this->admin);
    $this
      ->drupalGet("quiz/add/quiz");

    // These are the basic system defaults.
    $this
      ->assertFieldChecked('edit-allow-resume');
    $this
      ->assertFieldChecked('edit-allow-skipping');
    $this
      ->assertNoFieldChecked('edit-allow-jumping');
    $this
      ->assertFieldChecked('edit-allow-change');
    $this
      ->assertFieldChecked('edit-backwards-navigation');
    $this
      ->assertNoFieldChecked('edit-repeat-until-correct');
    $this
      ->assertNoFieldChecked('edit-mark-doubtful');
    $this
      ->assertFieldChecked('edit-show-passed');
    $this
      ->drupalGet('admin/quiz/settings/quiz-form');

    // Verify the same ones show up.
    $this
      ->assertFieldChecked('edit-allow-resume');
    $this
      ->assertFieldChecked('edit-allow-skipping');
    $this
      ->assertNoFieldChecked('edit-allow-jumping');
    $this
      ->assertFieldChecked('edit-allow-change');
    $this
      ->assertFieldChecked('edit-backwards-navigation');
    $this
      ->assertNoFieldChecked('edit-repeat-until-correct');
    $this
      ->assertNoFieldChecked('edit-mark-doubtful');
    $this
      ->assertFieldChecked('edit-show-passed');

    // Change a default.
    $this
      ->drupalPostForm(NULL, array(
      'allow_resume' => FALSE,
    ), t('Save'));
    $this
      ->drupalGet('admin/quiz/settings/quiz-form');
    $this
      ->assertNoFieldChecked('edit-allow-resume');

    // Check that the default carried over. Check that other options remained
    // set.
    $this
      ->drupalGet("quiz/add/quiz");
    $this
      ->assertNoFieldChecked('edit-allow-resume');
    $this
      ->assertFieldChecked('edit-allow-skipping');

    // Check defaults for another user.
    $this
      ->drupalLogin($this->admin2);
    $this
      ->drupalGet("quiz/add/quiz");
    $this
      ->assertNoFieldChecked('edit-allow-resume');
    $this
      ->assertFieldChecked('edit-allow-skipping');

    // Set allow skipping to off as a user default.
    $this
      ->drupalGet("quiz/add/quiz");
    $this
      ->drupalPostForm(NULL, array(
      'title' => 'Test quiz',
      'allow_skipping' => FALSE,
      'remember_settings' => TRUE,
    ), t('Save'));
    $this
      ->drupalGet("quiz/add/quiz");
    $this
      ->assertNoFieldChecked('edit-allow-resume');
    $this
      ->assertNoFieldChecked('edit-allow-skipping');

    // Ensure it didn't affect another user.
    $this
      ->drupalLogin($this->admin);
    $this
      ->drupalGet("quiz/add/quiz");
    $this
      ->assertNoFieldChecked('edit-allow-resume');
    $this
      ->assertFieldChecked('edit-allow-skipping');
  }

  /**
   * Test cloning quizzes with questions.
   */
  public function testCloneQuiz() {
    $this
      ->drupalLogin($this->admin);
    $question = $this
      ->createQuestion(array(
      'title' => 'TF 1',
      'body' => 'TF 1',
      'type' => 'truefalse',
      'truefalse_correct' => 1,
    ));
    $quiz = $this
      ->linkQuestionToQuiz($question);
    $quiz
      ->save();
    $new_quiz = $quiz
      ->createDuplicate();
    $new_quiz
      ->save();
    $this
      ->assertNotEquals($new_quiz
      ->id(), $quiz
      ->id());
    $this
      ->drupalLogin($this->user);
    $this
      ->drupalGet("quiz/{$quiz->id()}/take");
    $this
      ->assertText('TF 1');
    $this
      ->drupalGet("quiz/{$new_quiz->id()}/take");
    $this
      ->assertText('TF 1');
  }

  /**
   * Test quiz revisioning.
   */
  public function testQuestionRevisionActions() {
    $this
      ->drupalLogin($this->admin);
    $question_node = $this
      ->createQuestion(array(
      'title' => 'Revision 1',
      'body' => 'Revision 1',
      'type' => 'truefalse',
      'truefalse_correct' => 1,
    ));
    $quiz_node = $this
      ->linkQuestionToQuiz($question_node);

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

    // Save the question.
    $this
      ->drupalGet("quiz/{$question_node->nid}/edit");
    $this
      ->drupalPostForm(NULL, array(), t('Save'));
    $this
      ->assertText(t('Revision actions'));

    // Basic quiz admins won't be able to select this one.
    $this
      ->assertNoText(t('Update (no revision)'));
    $this
      ->assertText(t('Create new revision'));
    $this
      ->assertText(t('Do nothing'));
    $this
      ->assertText(t('Leave published'));

    // Check that we have the opportunity to revise the quiz.
    $this
      ->drupalGet("quiz/{$quiz_node->id()}/questions");
    $this
      ->assertField("edit-revision-{$question_node->nid}-{$question_node->vid}");
    $this
      ->drupalGet("quiz/{$question_node->nid}/question-revision-actions");
    $this
      ->drupalPostForm(NULL, array(
      "quizzes[{$quiz_node->nid}][revise]" => 'revise',
    ), t('Submit'));
    $this
      ->assertText('succesfully revised');
    $this
      ->drupalGet("quiz/{$question_node->nid}/question-revision-actions");
    $this
      ->drupalPostForm(NULL, array(
      "quizzes[{$quiz_node->nid}][revise]" => 'nothing',
    ), t('Submit'));
    $this
      ->assertText('Quiz not updated, please use the Quiz question listing to if you decide to update the quiz manually.');

    // Check that we no longer have the opportunity to revise the quiz.
    $this
      ->drupalGet("quiz/{$quiz_node->id()}/questions");
    $this
      ->assertNoField("edit-revision-{$question_node->nid}-{$question_node->vid}");

    // Check that a better admin can manually update the quiz (no revisioning).
    $better_admin = $this
      ->drupalCreateUser(array(
      'edit any truefalse content',
      'update any quiz quiz',
      'manual quiz revisioning',
    ));
    $this
      ->drupalLogin($better_admin);
    $this
      ->drupalGet("quiz/{$question_node->nid}/question-revision-actions");
    $this
      ->drupalPostForm(NULL, array(
      "quizzes[{$quiz_node->nid}][revise]" => 'update',
    ), t('Submit'));
    $this
      ->assertText('succesfully updated');
  }

}

Classes

Namesort descending Description
QuizCreationTest Test aspects of quiz creation including global and user defaults.