You are here

QuizCategorizedRandomTest.php in Quiz 8.6

File

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

namespace Drupal\Tests\quiz\Functional;


/**
 * Tests for random questions.
 *
 * Since this is random by nature, there is a chance that these will fail. We
 * use 5 layout builds to try and mitigate that chance.
 *
 * @group Quiz
 */
class QuizCategorizedRandomTest extends QuizTestBase {
  public static $modules = array(
    'quiz_truefalse',
    'taxonomy',
  );

  /**
   * Test pulling questions from categories.
   *
   * @todo add test for weighted questions
   */
  public function testCategorizedRandomQuestions() {

    // Vocabs.
    $v1 = \Drupal\taxonomy\Entity\Vocabulary::create([
      'name' => 'Vocab 1',
      'vid' => 'vocab1',
    ]);
    $v1
      ->save();
    $v1t1 = \Drupal\taxonomy\Entity\Term::create([
      'name' => 'Vocab 1 Term 1',
      'vid' => 'vocab1',
    ]);
    $v1t1
      ->save();
    $v1t2 = \Drupal\taxonomy\Entity\Term::create([
      'name' => 'Vocab 1 Term 2',
      'vid' => 'vocab1',
    ]);
    $v1t2
      ->save();
    $v1t3 = \Drupal\taxonomy\Entity\Term::create([
      'name' => 'Vocab 1 Term 3',
      'vid' => 'vocab1',
    ]);
    $v1t3
      ->save();
    $pg1 = \Drupal\paragraphs\Entity\Paragraph::create([
      'type' => 'quiz_question_term_pool',
      'quiz_question_tid' => [
        'target_id' => $v1t1
          ->id(),
      ],
      'quiz_question_number' => 2,
    ]);
    $pg1
      ->save();
    $pg2 = \Drupal\paragraphs\Entity\Paragraph::create([
      'type' => 'quiz_question_term_pool',
      'quiz_question_tid' => [
        'target_id' => $v1t2
          ->id(),
      ],
      'quiz_question_number' => 2,
    ]);
    $pg2
      ->save();
    $quiz = $this
      ->createQuiz(array(
      'randomization' => 3,
    ));
    $quiz
      ->get('quiz_terms')
      ->appendItem($pg1);
    $quiz
      ->get('quiz_terms')
      ->appendItem($pg2);
    $quiz
      ->save();
    $field_storage = \Drupal\field\Entity\FieldStorageConfig::create([
      'id' => 'quiz_question.question_category',
      'field_name' => 'question_category',
      'entity_type' => 'quiz_question',
      'type' => 'entity_reference',
      'settings' => array(
        'target_type' => 'taxonomy_term',
      ),
      'module' => 'core',
    ]);
    $field_storage
      ->save();
    $instance = \Drupal\field\Entity\FieldConfig::create([
      'field_storage' => $field_storage,
      'bundle' => 'truefalse',
      'label' => 'Question category',
      'field_name' => 'question_category',
      'entity_type' => 'quiz_question',
    ]);
    $instance
      ->save();
    \Drupal::service('entity_display.repository')
      ->getFormDisplay('quiz_question', 'truefalse', 'default')
      ->setComponent('question_category', array(
      'type' => 'options_select',
    ))
      ->save();
    $questions[] = $this
      ->createQuestion(array(
      'title' => 'tf 1 v1t1',
      'type' => 'truefalse',
      'truefalse_correct' => 1,
      'question_category' => [
        'target_id' => $v1t1
          ->id(),
      ],
      'body' => 'TF 1 body text',
    ))
      ->id();
    $questions[] = $this
      ->createQuestion(array(
      'title' => 'tf 2 v1t1',
      'type' => 'truefalse',
      'truefalse_correct' => 1,
      'question_category' => [
        'target_id' => $v1t1
          ->id(),
      ],
      'body' => 'TF 1 body text',
    ))
      ->id();
    $questions[] = $this
      ->createQuestion(array(
      'title' => 'tf 3 v1t1',
      'type' => 'truefalse',
      'truefalse_correct' => 1,
      'question_category' => [
        'target_id' => $v1t1
          ->id(),
      ],
      'body' => 'TF 1 body text',
    ))
      ->id();
    $questions[] = $this
      ->createQuestion(array(
      'title' => 'tf 4 v1t2',
      'type' => 'truefalse',
      'truefalse_correct' => 1,
      'question_category' => [
        'target_id' => $v1t2
          ->id(),
      ],
      'body' => 'TF 1 body text',
    ))
      ->id();
    $questions[] = $this
      ->createQuestion(array(
      'title' => 'tf 5 v1t2',
      'type' => 'truefalse',
      'truefalse_correct' => 1,
      'question_category' => [
        'target_id' => $v1t2
          ->id(),
      ],
      'body' => 'TF 1 body text',
    ))
      ->id();
    $questions[] = $this
      ->createQuestion(array(
      'title' => 'tf 6 v1t2',
      'type' => 'truefalse',
      'truefalse_correct' => 1,
      'question_category' => [
        'target_id' => $v1t2
          ->id(),
      ],
      'body' => 'TF 1 body text',
    ))
      ->id();
    $list = $quiz
      ->buildLayout();
    $this
      ->assertEqual(count($list), 4, 'Quiz had 4 questions.');
    foreach ($list as $qinfo) {
      $qq_ids[] = $qinfo['qqid'];
    }
    $this
      ->assertEqual(count(array_intersect($qq_ids, $questions)), 4, 'Questions were from the terms excluding 2.');

    // Test number of questions.
    $num_questions = $quiz
      ->getNumberOfQuestions();
    $this
      ->assertEqual($num_questions, 4);

    // Start the quiz.
    $this
      ->drupalLogin($this->user);
    $this
      ->drupalGet("quiz/{$quiz->id()}/take");
    $this
      ->assertText('Page 1 of 4');
  }

}

Classes

Namesort descending Description
QuizCategorizedRandomTest Tests for random questions.