You are here

public function MatchingTestCase::testMatchingShuffle in Quiz 6.x

Test matching shuffle.

File

question_types/quiz_matching/tests/src/Functional/MatchingTestCase.php, line 183

Class

MatchingTestCase
Test class for matching questions.

Namespace

Drupal\Tests\quiz_matching\Functional

Code

public function testMatchingShuffle() {
  $config = \Drupal::configFactory()
    ->getEditable('quiz_matching.settings');
  $config
    ->set('shuffle', 1)
    ->save();
  $quiz = $this
    ->createQuiz();
  $question = $this
    ->testCreateQuizQuestion();
  $quiz
    ->addQuestion($question);

  // Login as non-admin.
  $this
    ->drupalLogin($this->user);

  // We have to do this a few times to get a random result.
  for ($i = 1; $i <= 5; $i++) {

    // Take the quiz.
    $this
      ->drupalGet("quiz/{$quiz->id()}/take");
    $content = $this
      ->getSession()
      ->getPage()
      ->getContent();
    $one = strpos($content, 'MAQ 1');
    $two = strpos($content, 'MAQ 2');
    $three = strpos($content, 'MAQ 3');
    $result[] = $one < $two && $two < $three;
  }

  // Assert that one of the results is different.
  $this
    ->assertNotEquals(count($result), count(array_filter($result)), 'Matching questions were shuffled.');
}