CourseObjectAccessTimeTestCase.php in Course 8.2
File
tests/src/Functional/CourseObjectAccessTimeTestCase.php
View source
<?php
namespace Drupal\Tests\course\Functional;
class CourseObjectAccessTimeTestCase extends CourseTestCase {
public function testCourseObjectRestrictByRelease() {
$now = \Drupal::time()
->getRequestTime();
$course = $this
->createCourse();
$options = array();
$o1 = $this
->createCourseObject($course);
$options['plugins']['access']['timing']['release'] = gmdate('c', $now + 100);
$o1
->addOptions($options)
->save();
$this
->assertFalse($o1
->access('take', $this->student_user), 'Check that user cannot take not released object.');
$this
->assertTrue($o1
->access('see', $this->student_user), 'Check that user can still see not released object.');
$options['plugins']['access']['timing']['release_hidden'] = 1;
$o1
->addOptions($options)
->save();
$this
->assertFalse($o1
->access('see', $this->student_user), 'Check that object is hidden if hidden is checked and object is not released.');
}
public function testCourseObjectRestrictByExpiration() {
$now = \Drupal::time()
->getRequestTime();
$course = $this
->createCourse();
$options = array();
$o2 = $this
->createCourseObject($course);
$options['plugins']['access']['timing']['expiration'] = gmdate('c', $now - 100);
$o2
->addOptions($options)
->save();
$this
->assertFalse($o2
->access('take', $this->student_user), 'Check that user cannot take expired course object.');
$this
->assertTrue($o2
->access('see', $this->student_user), 'Check that user can see expired course object.');
$options['plugins']['access']['timing']['expiration_hidden'] = 1;
$o2
->addOptions($options)
->save();
$this
->assertFalse($o2
->access('see', $this->student_user), 'Check that object is hidden if hidden is checked and object is expired.');
}
public function testCourseObjectDuration() {
$now = \Drupal::time()
->getRequestTime();
$course = $this
->createCourse();
$options = array();
$course
->enroll($this->student_user);
$o3 = $this
->createCourseObject($course);
$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.');
$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.');
$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.');
}
}