You are here

function CourseObjectAccessTest::testHiddenCourseObjects in Course 3.x

Test hidden course objects do not show up in the course outline but block completion.

File

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

Class

CourseObjectAccessTest
Tests for course object access.

Namespace

Drupal\Tests\course\Functional

Code

function testHiddenCourseObjects() {

  // 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 in the outline.
  $this
    ->assertTrue($o1
    ->access('see', $user));

  // Make object hidden.
  $o1
    ->set('hidden', 1)
    ->save();
  $this
    ->assertFalse($o1
    ->access('see', $user));
  $this
    ->assertFalse($o1
    ->access('take', $user));
  $this
    ->assertFalse($o1
    ->access('view', $user));
  $this
    ->drupalGet("course/{$course->id()}/take");
  $this
    ->drupalGet("course/{$course->id()}/object/{$o_optional->id()}");
  $this
    ->assertNoLink(t('Next'));
  $this
    ->drupalGet("course/{$course->id()}/object/{$o1->id()}");
  $this
    ->assertResponse(403, t('Hidden object is not accessible.'));

  // Check that the course is not complete without completing the hidden object.
  $report = $course
    ->getEnrollment($user);
  $this
    ->assertFalse($report
    ->isComplete());
}