View source
<?php
namespace Drupal\Tests\quiz\Functional;
use Drupal;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\quiz\Entity\QuizResultType;
class QuizResultBundleTest extends QuizTestBase {
public static $modules = array(
'quiz_truefalse',
);
public function testFieldableResults() {
$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);
$this
->drupalGet("quiz/{$quizNodeA->id()}/take");
$this
->assertFieldById('edit-quiz-result-field-a-0-value');
$quiz_result = $quizNodeA
->getResumeableResult($this->user);
$this
->assertFalse($quiz_result, 'Quiz result does not yet exist.');
$this
->drupalPostForm(NULL, array(), t('Start Quiz'));
$this
->assertText('field is required');
$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.'));
$this
->assertText('Question 1');
}
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();
$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' => array(
'quiz' => [
'show_field' => TRUE,
],
),
]);
$instancea
->save();
Drupal::service('entity_display.repository')
->getFormDisplay('quiz_result', 'type_a', 'default')
->setComponent('result_field_a', array(
'type' => 'text_textfield',
))
->save();
$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' => array(
'quiz' => [
'show_field' => TRUE,
],
),
]);
$instanceb
->save();
Drupal::service('entity_display.repository')
->getFormDisplay('quiz_result', 'type_b', 'default')
->setComponent('result_field_b', array(
'type' => 'text_textfield',
))
->save();
$quizNodeA = $this
->createQuiz(array(
'result_type' => 'type_a',
));
$question1 = $this
->createQuestion(array(
'type' => 'truefalse',
'truefalse_correct' => 1,
));
$this
->linkQuestionToQuiz($question1, $quizNodeA);
$quizNodeB = $this
->createQuiz(array(
'result_type' => 'type_b',
));
$question2 = $this
->createQuestion(array(
'type' => 'truefalse',
'truefalse_correct' => 1,
));
$this
->linkQuestionToQuiz($question2, $quizNodeB);
$this
->drupalLogin($this->user);
$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, array(), t('Start Quiz'));
$this
->assertText('field is required');
$this
->drupalPostForm(NULL, array(
'result_field_a[0][value]' => 'test 123',
), t('Start Quiz'));
$this
->drupalGet("quiz/{$quizNodeB->id()}/take");
$this
->assertFieldById('edit-result-field-b-0-value');
$this
->assertNoFieldById('edit-result-field-a-0-value');
$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');
}
}