You are here

public function CourseObjectAccessTimeTestCase::testCourseObjectDuration in Course 8.2

Same name and namespace in other branches
  1. 8.3 tests/src/Functional/CourseObjectAccessTimeTestCase.php \Drupal\Tests\course\Functional\CourseObjectAccessTimeTestCase::testCourseObjectDuration()

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

File

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

Class

CourseObjectAccessTimeTestCase
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.');
}