You are here

public function QuizCategorizedRandomTest::testCategorizedRandomQuestions in Quiz 8.5

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

Test pulling questions from categories.

@todo add test for weighted questions

File

tests/src/Functional/QuizCategorizedRandomTest.php, line 22

Class

QuizCategorizedRandomTest
Tests for random questions.

Namespace

Drupal\Tests\quiz\Functional

Code

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');
}