You are here

public function QuizResultTestCase::testQuizResultBundles in Quiz 7.5

Test quiz result bundles.

File

tests/QuizResultTestCase.test, line 612

Class

QuizResultTestCase

Code

public function testQuizResultBundles() {
  $result_type = entity_create('quiz_result_type', array(
    'type' => 'type_A',
    'label' => t('Bundle type A'),
  ));
  $result_type
    ->save();
  $result_type = entity_create('quiz_result_type', array(
    'type' => 'type_B',
    'label' => t('Bundle type B'),
  ));
  $result_type
    ->save();
  cache_clear_all();

  // Add a field to type A and make it required for registration.
  // Create field A.
  $field = array(
    'field_name' => 'result_field_a',
    'type' => 'text',
  );
  $instance = array(
    'field_name' => 'result_field_a',
    'entity_type' => 'quiz_result',
    'bundle' => 'type_A',
    'label' => 'Result field A',
    'widget' => array(
      'active' => 1,
      'module' => 'text',
      'settings' => array(
        'size' => 60,
      ),
      'type' => 'text_textfield',
      'weight' => 1,
    ),
    'settings' => array(
      'quiz_result_show_field' => 1,
    ),
    'required' => 1,
  );
  field_create_field($field);
  $instance_a = field_create_instance($instance);

  // Add a field to type B and make it required for registration.
  // Create field B.
  $field = array(
    'field_name' => 'result_field_b',
    'type' => 'text',
  );
  $instance = array(
    'field_name' => 'result_field_b',
    'entity_type' => 'quiz_result',
    'bundle' => 'type_B',
    'label' => 'Result field B',
    'widget' => array(
      'active' => 1,
      'module' => 'text',
      'settings' => array(
        'size' => 60,
      ),
      'type' => 'text_textfield',
      'weight' => 1,
    ),
    'settings' => array(
      'quiz_result_show_field' => 1,
    ),
    'required' => 1,
  );
  field_create_field($field);
  $instance_b = field_create_instance($instance);
  $quizNodeA = $this
    ->drupalCreateQuiz(array(
    'result_type' => 'type_A',
  ));
  $question_node1 = $this
    ->drupalCreateNode(array(
    'type' => 'truefalse',
    'correct_answer' => 1,
  ));
  $this
    ->linkQuestionToQuiz($question_node1, $quizNodeA);
  $quizNodeB = $this
    ->drupalCreateQuiz(array(
    'result_type' => 'type_B',
  ));
  $question_node2 = $this
    ->drupalCreateNode(array(
    'type' => 'truefalse',
    'correct_answer' => 1,
  ));
  $this
    ->linkQuestionToQuiz($question_node2, $quizNodeB);
  $this
    ->drupalLogin($this->user);

  // Check if field shows up and user is not yet started.
  $this
    ->drupalGet("node/{$quizNodeA->nid}/take");
  $this
    ->assertFieldById('edit-result-field-a-und-0-value');
  $this
    ->assertNoFieldById('edit-result-field-b-und-0-value');
  $result = entity_load('quiz_result', FALSE, array(
    'nid' => $quizNodeA->nid,
    'uid' => $this->user->uid,
  ));
  $this
    ->assertFalse($result);
  $this
    ->drupalPost(NULL, array(), t('Start Quiz'));

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

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

  // Mark field B to not show on result.
  $instance_b['settings']['quiz_result_show_field'] = 0;
  field_update_instance($instance_b);
  $this
    ->drupalGet("node/{$quizNodeB->nid}/take");
  $this
    ->assertNoFieldById('edit-result-field-a-und-0-value');
  $this
    ->assertNoFieldById('edit-result-field-b-und-0-value');
}