You are here

CourseGradeTestCase.test in Course 7.2

Same filename and directory in other branches
  1. 6 tests/CourseGradeTestCase.test
  2. 7 tests/CourseGradeTestCase.test

File

tests/CourseGradeTestCase.test
View source
<?php

/**
 * Tests for Course grade.
 */
class CourseGradeTestCase extends CourseTestCase {
  public static function getInfo() {

    // Note that getInfo() strings are not translated with t().
    return array(
      'name' => 'Course grading',
      'description' => 'Ensure that Course grading functions normally.',
      'group' => 'Course',
    );
  }

  /**
   * Test that the final grade is calculated correctly.
   */
  public function testCourseFinalGrade() {
    $courseNode = $this
      ->createCourseNode();

    // Create a graded course object.
    $co1 = $this
      ->createCourseObject($courseNode, TRUE);

    // Reload the course from DB.
    $course = course_get_course($courseNode);
    $courseObjects = $course
      ->getObjects();
    $co1 = reset($courseObjects);
    course_enroll($courseNode, $this->student_user);

    // Set grade result.
    $co1
      ->getFulfillment($this->student_user)
      ->setOption('grade_result', 80)
      ->save();

    // Track object not included in final grade.
    $co1
      ->setOption('grade_include', FALSE)
      ->save();
    $co1
      ->getCourse()
      ->getTracker($this->student_user)
      ->track();
    $this
      ->assertNotEqual($co1
      ->getCourse()
      ->getTracker($this->student_user)
      ->getOption('grade_result'), 80, 'Course grade was not given from course object');

    // Track object included in final grade.
    $co1
      ->setOption('grade_include', TRUE)
      ->save();
    $co1
      ->getCourse()
      ->getTracker($this->student_user)
      ->track();
    $this
      ->assertEqual($co1
      ->getCourse()
      ->getTracker($this->student_user)
      ->getOption('grade_result'), 80, 'Course grade was given from course object');
  }

  /**
   * Test that the course grade access plugin functions properly.
   */
  public function testCourseGradeAccess() {
    $courseNode = $this
      ->createCourseNode();

    // Create a graded course objects.
    $this
      ->createCourseObject($courseNode, TRUE);

    // Add a non-graded course object.
    $this
      ->createCourseObject($courseNode);

    // Reload the course from DB.
    $course = course_get_course($courseNode);
    $courseObjects = array_values($course
      ->getObjects());
    $co1 = $courseObjects[0];
    $co2 = $courseObjects[1];

    // Set object 1 to be included in the course grade.
    $co1
      ->setOption('grade_include', TRUE)
      ->save();

    // Set object 2 to require a course grade of 80.
    $co2
      ->addOptions(array(
      'plugins' => array(
        'access' => array(
          'grade' => array(
            'course_grade_range' => array(
              'low' => 80,
              'high' => 90,
            ),
          ),
        ),
      ),
    ))
      ->save();

    // Enroll the user.
    course_enroll($courseNode, $this->student_user);

    // Complete but don't hit the low grade requirement.
    $course
      ->getObjects()[1]
      ->getFulfillment($this->student_user)
      ->setOption('grade_result', 79)
      ->setComplete(1)
      ->save();
    $course
      ->getTracker($this->student_user)
      ->track();
    $this
      ->assertFalse($co2
      ->access('take', $this->student_user), 'User cannot take course object with lower grade.');

    // Complete but don't hit the high grade requirement.
    $course
      ->getObjects()[1]
      ->getFulfillment($this->student_user)
      ->setOption('grade_result', 91)
      ->setComplete(1)
      ->save();
    $course
      ->getTracker($this->student_user)
      ->track();
    $this
      ->assertFalse($co2
      ->access('take', $this->student_user), 'User cannot take course object with higher grade.');

    // Hit the grade requirement.
    $course
      ->getObjects()[1]
      ->getFulfillment($this->student_user)
      ->setOption('grade_result', 80)
      ->setComplete(1)
      ->save();
    $course
      ->getTracker($this->student_user)
      ->track();
    $this
      ->assertTrue($co2
      ->access('take', $this->student_user), 'User can take course object with required grade.');
  }

}

Classes

Namesort descending Description
CourseGradeTestCase Tests for Course grade.