You are here

function CourseObjectAccessTest::testDisabledCourseObjects in Course 3.x

Test disabled course objects do not show up in the course outline and do not block completion.

File

tests/src/Functional/CourseObjectAccessTest.php, line 192

Class

CourseObjectAccessTest
Tests for course object access.

Namespace

Drupal\Tests\course\Functional

Code

function testDisabledCourseObjects() {

  // Create a course.
  $course = $this
    ->createCourse();

  // Use the student user.
  $user = $this->student_user;
  $this
    ->drupalLogin($user);

  // Create an optional object so we can test the "Next" button.
  $o_optional = $this
    ->createCourseObject($course);
  $o_optional
    ->set('required', FALSE)
    ->save();
  $o1 = $this
    ->createCourseObject($course);
  $o1
    ->set('required', TRUE)
    ->save();

  // By default, should be visible.
  $this
    ->assertTrue($o1
    ->access('see', $user));

  // Make object disabled.
  $o1
    ->set('enabled', 0)
    ->save();
  $this
    ->assertFalse($o1
    ->access('see', $user));
  $this
    ->assertFalse($o1
    ->access('view', $user));
  $this
    ->assertFalse($o1
    ->access('take', $user));
  $this
    ->drupalGet("course/{$course->id()}/take");

  // Completion link.
  $this
    ->clickLink(t('Next'));
  $this
    ->assertResponse(200, t('Did not get access denied.'));
  $this
    ->drupalGet("course/{$course->id()}/object/{$o1->id()}");
  $this
    ->assertResponse(403, t('Disabled object is not accessible.'));

  // Check that the course is complete even without completing the disabled
  // object.
  $report = $course
    ->getEnrollment($user);
  $this
    ->assertTrue($report
    ->get('complete')->value);

  // Set the disabled object to be at the beginning of the course.
  $o1
    ->set('weight', -100)
    ->save();

  // Try with a new user.
  $this->student_user = $this
    ->createStudentUser();
  $this
    ->drupalLogin($this->student_user);
  $this
    ->drupalGet("course/{$course->id()}/take");
  $this
    ->drupalGet("course/{$course->id()}/object/{$o1->id()}");
  $this
    ->assertResponse(403, t('Disabled first object is not accessible.'));
  $this
    ->drupalGet("course/{$course->id()}/object/{$o_optional->id()}");
  $this
    ->assertResponse(200, t('Second object is accessible.'));
}