You are here

function CourseObjectQuizTestCase::testQuizCourseObject in Course 7.2

Same name and namespace in other branches
  1. 6 modules/course_quiz/course_quiz.test \CourseObjectQuizTestCase::testQuizCourseObject()
  2. 7 modules/course_quiz/course_quiz.test \CourseObjectQuizTestCase::testQuizCourseObject()

File

modules/course_quiz/course_quiz.test, line 32

Class

CourseObjectQuizTestCase
Tests for quiz Course objects.

Code

function testQuizCourseObject() {
  $this
    ->drupalLogin($this->quiz_admin);

  // Create a course with 1 quiz.
  $courseNode = $this
    ->createCourseNode();
  $co1 = course_get_course_object('course_quiz', 'quiz');
  $co1
    ->setCourse($courseNode->nid);
  $co1
    ->setOption('passing_grade', 100);
  $co1
    ->save();
  $this
    ->assertTrue($co1
    ->getInstanceId() > 0, 'Quiz node created on course object save.');
  $quizNode = node_load($co1
    ->getInstanceId());
  $this
    ->assertTrue($quizNode->type == 'quiz', 'Quiz node is a quiz.');

  // Add a question.
  $this
    ->drupalGet('node/add/truefalse', array(
    'query' => array(
      'quiz_nid' => $quizNode->nid,
      'quiz_vid' => $quizNode->vid,
    ),
  ));
  $this
    ->drupalPost(NULL, array(
    'body[und][0][value]' => 'Test question',
    'correct_answer' => 1,
  ), t('Save'));

  // Makes tests work against 7.x-4.x and 7.x-5.x. Node ID of the question is 3.
  $answer_element = course_quiz_quiz_version() >= 5 ? "question[3][answer]" : "tries";

  // Enroll the user in the course
  course_enroll($courseNode, $this->quiz_admin);

  // Fail the quiz.
  $this
    ->drupalGet("node/{$quizNode->nid}/take");
  $this
    ->assertFalse($co1
    ->getFulfillment($this->quiz_admin)
    ->isComplete(), 'Check that quiz fulfillment is not complete after fail.');
  $this
    ->drupalPost(NULL, array(
    $answer_element => 0,
  ), t('Finish'));
  $this
    ->assertFalse($co1
    ->getFulfillment($this->quiz_admin)
    ->isComplete(), 'Check that quiz fulfillment is not complete.');

  // Pass the quiz.
  $this
    ->drupalGet("node/{$quizNode->nid}/take");
  $this
    ->drupalPost(NULL, array(
    $answer_element => 1,
  ), t('Finish'));
  entity_get_controller('course_object_fulfillment')
    ->resetCache();
  $this
    ->assertTrue($co1
    ->getFulfillment($this->quiz_admin)
    ->isComplete(), 'Check that quiz fulfillment is complete.');
}