You are here

public function CourseObjectAccessTimeTest::testCourseObjectDuration in Course 3.x

Check that user cannot access course object outside of duration period.

File

tests/src/Functional/CourseObjectAccessTimeTest.php, line 61

Class

CourseObjectAccessTimeTest
Tests for course object access based on time.

Namespace

Drupal\Tests\course\Functional

Code

public function testCourseObjectDuration() {
  $now = \Drupal::time()
    ->getRequestTime();
  $course = $this
    ->createCourse();
  $options = array();
  $course
    ->enroll($this->student_user);
  $o3 = $this
    ->createCourseObject($course);

  // Start the course object 5 minutes ago.
  $o3
    ->getFulfillment($this->student_user)
    ->setOption('date_started', $now - 300)
    ->save();
  \Drupal::entityTypeManager()
    ->getAccessControlHandler('course_object')
    ->resetCache();
  $this
    ->assertTrue($o3
    ->access('take', $this->student_user), 'Check that user can access course object without duration.');

  // Set the duration to 1 minute.
  $options['plugins']['access']['timing']['duration'] = 60;
  $o3
    ->addOptions($options)
    ->save();
  $this
    ->assertFalse($o3
    ->access('take', $this->student_user), 'Check that user cannot access course object when duration has passed.');

  // Extend the duration to 10 minutes.
  $options['plugins']['access']['timing']['duration'] = 600;
  $o3
    ->addOptions($options)
    ->save();
  $this
    ->assertTrue($o3
    ->access('take', $this->student_user), 'Check that user can access course object when duration has been extended.');
}