CourseAccessTestCase.php in Course 8.3
File
tests/src/Functional/CourseAccessTestCase.php
View source
<?php
namespace Drupal\Tests\course\Functional;
class CourseAccessTestCase extends CourseTestCase {
function testDurationExpiration() {
$course = $this
->createCourse();
$course
->set('duration', 86400 * 30);
$course
->save();
$course
->enroll($this->student_user);
$this
->assertTrue($course
->access('take', $this->student_user));
$enroll = $course
->getEnrollment($this->student_user);
$enroll
->set('enroll_end', 1);
$enroll
->save();
\Drupal::entityTypeManager()
->getAccessControlHandler('course')
->resetCache();
$this
->assertFalse($course
->access('take', $this->student_user), 'User cannot access course with expired enrollment.');
}
function testReleaseExpiration() {
$course = $this
->createCourse();
$now = \Drupal::time()
->getRequestTime();
$formatter = \Drupal::service('date.formatter');
$this
->assertTrue($course
->access('enroll', $this->student_user), 'User can enroll in course past start date.');
$course
->set('course_date', [
'value' => $formatter
->format($now + 30, 'custom', DATETIME_DATETIME_STORAGE_FORMAT),
]);
$course
->save();
\Drupal::entityTypeManager()
->getAccessControlHandler('course')
->resetCache();
$this
->assertFalse($course
->access('enroll', $this->student_user), 'User cannot enroll in not yet open course.');
$course
->set('course_date', [
'value' => $formatter
->format($now - 60, 'custom', DATETIME_DATETIME_STORAGE_FORMAT),
'end_value' => $formatter
->format($now - 30, 'custom', DATETIME_DATETIME_STORAGE_FORMAT),
]);
$course
->save();
\Drupal::entityTypeManager()
->getAccessControlHandler('course')
->resetCache();
$this
->assertFalse($course
->access('enroll', $this->student_user), 'User cannot enroll in expired course.');
$course
->enroll($this->student_user);
\Drupal::entityTypeManager()
->getAccessControlHandler('course')
->resetCache();
$this
->assertFalse($course
->access('enroll', $this->student_user), 'User cannot take expired course.');
}
}