You are here

function CourseEnrollmentTest::testCourseEnrollmentTimestamps in Course 3.x

Test a manual course enrollment. Ensure that created and started timestamping works as expected.

File

tests/src/Functional/CourseEnrollmentTest.php, line 50

Class

CourseEnrollmentTest
Tests for Course enrollment

Namespace

Drupal\Tests\course\Functional

Code

function testCourseEnrollmentTimestamps() {
  $course = $this
    ->createCourse();
  $course_enrollment = CourseEnrollment::create(array(
    'cid' => $course
      ->id(),
    'uid' => $this->student_user
      ->id(),
    'type' => $course
      ->get('enrollment_type')->value,
  ));
  $course_enrollment
    ->save();
  $initial_created = $course_enrollment
    ->get('created')->value;
  $this
    ->assertTrue($initial_created > 0, t('Enrollment creation date was set.'));
  $this
    ->assertEqual($course_enrollment
    ->get('timestamp')->value, 0, t('Enrollment timestamp not set.'));

  // Wait one second so we can confirm a time difference.
  sleep(1);
  $this
    ->drupalLogin($this->student_user);
  $this
    ->drupalGet("course/{$course->id()}/take");
  $new_enrollment = $course
    ->getEnrollment($this->student_user);
  Drupal::entityTypeManager()
    ->getStorage('course_enrollment')
    ->resetCache();
  $this
    ->assertEqual($initial_created, $new_enrollment
    ->get('created')->value, t('Enrollment creation date retained.'));
  $this
    ->assertTrue($new_enrollment
    ->get('timestamp')->value > 0, t('Enrollment timestamp set.'));
}