You are here

public function QuizResultBundleTest::testQuizResultBundles in Quiz 6.x

Same name and namespace in other branches
  1. 8.6 tests/src/Functional/QuizResultBundleTest.php \Drupal\Tests\quiz\Functional\QuizResultBundleTest::testQuizResultBundles()
  2. 8.5 tests/src/Functional/QuizResultBundleTest.php \Drupal\Tests\quiz\Functional\QuizResultBundleTest::testQuizResultBundles()

Test quiz result bundles.

File

tests/src/Functional/QuizResultBundleTest.php, line 86

Class

QuizResultBundleTest
Test quiz result bundle and fields behavior.

Namespace

Drupal\Tests\quiz\Functional

Code

public function testQuizResultBundles() {
  QuizResultType::create([
    'id' => 'type_a',
    'label' => t('Bundle type A'),
  ])
    ->save();
  QuizResultType::create([
    'id' => 'type_b',
    'label' => t('Bundle type B'),
  ])
    ->save();

  // Add a field to quiz result and make it required for starting.
  $field_storagea = FieldStorageConfig::create([
    'id' => 'quiz_result.result_field_a',
    'field_name' => 'result_field_a',
    'entity_type' => 'quiz_result',
    'type' => 'string',
    'module' => 'core',
  ]);
  $field_storagea
    ->save();
  $instancea = FieldConfig::create([
    'field_storage' => $field_storagea,
    'bundle' => 'type_a',
    'label' => 'Result field A',
    'required' => TRUE,
    'field_name' => 'result_field_a',
    'entity_type' => 'quiz_result',
    'third_party_settings' => [
      'quiz' => [
        'show_field' => TRUE,
      ],
    ],
  ]);
  $instancea
    ->save();
  Drupal::service('entity_display.repository')
    ->getFormDisplay('quiz_result', 'type_a', 'default')
    ->setComponent('result_field_a', [
    'type' => 'text_textfield',
  ])
    ->save();

  // Add a field to quiz result and make it required for starting.
  $field_storageb = FieldStorageConfig::create([
    'id' => 'quiz_result.result_field_b',
    'field_name' => 'result_field_b',
    'entity_type' => 'quiz_result',
    'type' => 'string',
    'module' => 'core',
  ]);
  $field_storageb
    ->save();
  $instanceb = FieldConfig::create([
    'field_storage' => $field_storageb,
    'bundle' => 'type_b',
    'label' => 'Result field B',
    'required' => TRUE,
    'field_name' => 'result_field_b',
    'entity_type' => 'quiz_result',
    'third_party_settings' => [
      'quiz' => [
        'show_field' => TRUE,
      ],
    ],
  ]);
  $instanceb
    ->save();
  Drupal::service('entity_display.repository')
    ->getFormDisplay('quiz_result', 'type_b', 'default')
    ->setComponent('result_field_b', [
    'type' => 'text_textfield',
  ])
    ->save();
  $quizNodeA = $this
    ->createQuiz([
    'result_type' => 'type_a',
  ]);
  $question1 = $this
    ->createQuestion([
    'type' => 'truefalse',
    'truefalse_correct' => 1,
  ]);
  $this
    ->linkQuestionToQuiz($question1, $quizNodeA);
  $quizNodeB = $this
    ->createQuiz([
    'result_type' => 'type_b',
  ]);
  $question2 = $this
    ->createQuestion([
    'type' => 'truefalse',
    'truefalse_correct' => 1,
  ]);
  $this
    ->linkQuestionToQuiz($question2, $quizNodeB);
  $this
    ->drupalLogin($this->user);

  // Check if field shows up and user is not yet started.
  $this
    ->drupalGet("quiz/{$quizNodeA->id()}/take");
  $this
    ->assertFieldById('edit-result-field-a-0-value');
  $this
    ->assertNoFieldById('edit-result-field-b-0-value');
  $results = Drupal::entityQuery('quiz_result')
    ->condition('qid', $quizNodeA
    ->id())
    ->condition('uid', $this->user
    ->id())
    ->execute();
  $this
    ->assertEmpty($results);
  $this
    ->drupalPostForm(NULL, [], t('Start Quiz'));

  // Check that form API is working.
  $this
    ->assertText('field is required');
  $this
    ->drupalPostForm(NULL, [
    'result_field_a[0][value]' => 'test 123',
  ], t('Start Quiz'));

  // Check that a different field is on quiz B.
  $this
    ->drupalGet("quiz/{$quizNodeB->id()}/take");
  $this
    ->assertFieldById('edit-result-field-b-0-value');
  $this
    ->assertNoFieldById('edit-result-field-a-0-value');

  // Mark field B to not show on result.
  $instanceb
    ->setThirdPartySetting('quiz', 'show_field', FALSE);
  $instanceb
    ->save();
  $this
    ->drupalGet("quiz/{$quizNodeB->id()}/take");
  $this
    ->assertNoFieldById('edit-result-field-a-0-value');
  $this
    ->assertNoFieldById('edit-result-field-b-0-value');
}