You are here

function CourseObjectAccessTest::testCourseSkippableRequiredObjectsWorkflow in Course 3.x

Test an outline with skippable required objects.

File

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

Class

CourseObjectAccessTest
Tests for course object access.

Namespace

Drupal\Tests\course\Functional

Code

function testCourseSkippableRequiredObjectsWorkflow() {

  // Create a course with 4 objects.
  $course = $this
    ->createCourse();
  for ($i = 1; $i <= 4; $i++) {
    $temp = $this
      ->createCourseObject($course);
    $temp
      ->setOption('weight', $i)
      ->save();
  }

  // Use the student user.
  $user = $this->student_user;
  $co = array_values($course
    ->getObjects());
  $course
    ->enroll($user);

  // Set all required, but make the second one skippable.
  $co[0]
    ->setOption('required', 1);
  $co[1]
    ->setOption('required', 1);
  $co[1]
    ->setOption('skippable', 1)
    ->save();
  $co[2]
    ->setOption('required', 1);
  $co[3]
    ->setOption('required', 1);

  // We should not be able to bypass the first object.
  $this
    ->assertTrue($co[0]
    ->access('take', $user), 'User can access first object.');
  $this
    ->assertFalse($co[1]
    ->access('take', $user), 'User is blocked from second object.');

  // Complete the first object.
  $co[0]
    ->getFulfillment($user)
    ->setComplete(1)
    ->save();
  $this
    ->assertTrue($co[1]
    ->access('take', $user), 'User can access second object.');

  // Now that the user can access the second object, because it is required,
  // and skippable, they should be able to access the third object.
  $this
    ->assertTrue($co[2]
    ->access('take', $user), 'User can access third object.');

  // However because the third object is required, they cannot acess the
  // fourth.
  $this
    ->assertFalse($co[3]
    ->access('take', $user), 'User is blocked from fourth object.');

  // Complete all the required, but not skippable objects.
  $co[2]
    ->getFulfillment($user)
    ->setComplete(1)
    ->save();
  $co[3]
    ->getFulfillment($user)
    ->setComplete(1)
    ->save();

  // Even though all required, non-skippable objects were completed, the
  // course is not complete. The skippable object must still be completed.
  $this
    ->assertFalse($course
    ->getEnrollment($user)
    ->isComplete(), 'Check that course is not complete.');
  $co[1]
    ->getFulfillment($user)
    ->setComplete(1)
    ->save();
  $this
    ->assertTrue($course
    ->getEnrollment($user)
    ->isComplete(), 'Check that course is complete.');
}