You are here

public function QuizResultBundleTest::testFieldableResults in Quiz 8.5

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

Test fieldable Quiz results.

File

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

Class

QuizResultBundleTest
Test quiz result bundle and fields behavior.

Namespace

Drupal\Tests\quiz\Functional

Code

public function testFieldableResults() {

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

  // Check if field shows up and user is not yet started.
  $this
    ->drupalGet("quiz/{$quizNodeA->id()}/take");
  $this
    ->assertFieldById('edit-quiz-result-field-a-0-value');

  // We haven't submitted the form so we should not have a Quiz result yet.
  $quiz_result = $quizNodeA
    ->getResumeableResult($this->user);
  $this
    ->assertFalse($quiz_result, 'Quiz result does not yet exist.');

  // Submit the form.
  $this
    ->drupalPostForm(NULL, array(), t('Start Quiz'));

  // Check that we hooked into Form API correctly.
  $this
    ->assertText('field is required');

  // SUbmit the form with data.
  $this
    ->drupalPostForm(NULL, array(
    'quiz_result_field_a[0][value]' => 'test 123',
  ), t('Start Quiz'));
  $this
    ->assertNotEmpty($quizNodeA
    ->getResumeableResult($this->user), t('Found quiz result.'));

  // Check the result exists now.
  $this
    ->assertText('Question 1');
}