You are here

function CourseEnrollmentTestCase::testCourseEnrollment in Course 8.3

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/CourseEnrollmentTestCase.php \Drupal\Tests\course\Functional\CourseEnrollmentTestCase::testCourseEnrollment()

Test for enrollment access and timestamping.

File

tests/src/Functional/CourseEnrollmentTestCase.php, line 21

Class

CourseEnrollmentTestCase
Tests for Course enrollment

Namespace

Drupal\Tests\course\Functional

Code

function testCourseEnrollment() {
  $course = $this
    ->createCourse();
  $result = $course
    ->access('enroll', $this->student_user);
  $this
    ->assertTrue($result, 'Check that the user is allowed to self enroll into this course.');
  $result = $course
    ->access('take', $this->student_user);
  $this
    ->assertFalse($result, 'Check that the user cannot enroll in the course.');
  $course
    ->enroll($this->student_user);
  $result = $course
    ->access('take', $this->student_user);
  $this
    ->assertTrue($result, 'Check that the user can take the course.');
  $enroll = $course
    ->getEnrollment($this->student_user);
  $this
    ->assertTrue($enroll
    ->id() > 0, 'Enrollment was created.');
  $this
    ->assertTrue($enroll
    ->get('created')->value > 0, 'Enrollment has a creation timestamp.');

  //$this->assertFalse($enroll->timestamp > 0, 'Check that user has not started course.');

  // Take the course
  $this
    ->drupalLogin($this->student_user);
  $this
    ->drupalGet("course/{$course->id()}/take");

  // The enrollment has changed.
  Drupal::entityTypeManager()
    ->getStorage('course_enrollment')
    ->resetCache();
  $enroll = $course
    ->getEnrollment($this->student_user);
  $this
    ->assertTrue($enroll
    ->get('timestamp')->value > 0, 'Check for start of course timestamp.');
}