CourseGradeTestCase.php in Course 8.3        
                          
                  
                        
  
  
  
  
File
  tests/src/Functional/CourseGradeTestCase.php
  
    View source  
  <?php
namespace Drupal\Tests\course\Functional;
class CourseGradeTestCase extends CourseTestCase {
  
  public function testCourseFinalGrade() {
    $course = $this
      ->createCourse();
    
    $co1 = $this
      ->createCourseObject($course, TRUE);
    $course
      ->enroll($this->student_user);
    
    $co1
      ->getFulfillment($this->student_user)
      ->set('grade_result', 80)
      ->save();
    
    $co1
      ->set('grade_include', 0)
      ->save();
    $co1
      ->getCourse()
      ->resetCache();
    $co1
      ->getCourse()
      ->getTracker($this->student_user)
      ->track();
    $this
      ->assertNotEqual($co1
      ->getCourse()
      ->getTracker($this->student_user)
      ->get('grade_result')
      ->getString(), 80, 'Course grade was not given from course object');
    
    $co1
      ->set('grade_include', 1)
      ->save();
    $co1
      ->getCourse()
      ->resetCache();
    $co1
      ->getCourse()
      ->getTracker($this->student_user)
      ->track();
    $this
      ->assertEqual($co1
      ->getCourse()
      ->getTracker($this->student_user)
      ->get('grade_result')
      ->getString(), 80, 'Course grade was given from course object');
  }
  
  public function testCourseGradeAccess() {
    $course = $this
      ->createCourse();
    
    $this
      ->createCourseObject($course, TRUE);
    
    $this
      ->createCourseObject($course);
    
    $courseObjects = array_values($course
      ->getObjects());
    $co1 = $courseObjects[0];
    $co2 = $courseObjects[1];
    
    $co1
      ->set('grade_include', TRUE)
      ->save();
    $co1
      ->getCourse()
      ->resetCache();
    
    $co2
      ->addOptions(array(
      'plugins' => array(
        'access' => array(
          'grade' => array(
            'course_grade_range' => array(
              'low' => 80,
              'high' => 90,
            ),
          ),
        ),
      ),
    ))
      ->save();
    
    $course
      ->enroll($this->student_user);
    
    $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.');
    
    $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.');
    
    $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.');
  }
}