You are here

function CourseObjectAccessTest::testCourseObjectLinearWorkflow in Course 3.x

Test sequential object access.

File

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

Class

CourseObjectAccessTest
Tests for course object access.

Namespace

Drupal\Tests\course\Functional

Code

function testCourseObjectLinearWorkflow() {

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

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

  /* @var $co \Drupal\course\Entity\CourseObject[] */
  $co = array_values($course
    ->getObjects());

  // Student should not be able to access any objects.
  $this
    ->assertFalse($co[0]
    ->access('take', $user), 'Check that object 1 is not accessible.');
  $this
    ->assertFalse($co[1]
    ->access('take', $user), 'Check that object 2 is not accessible.');
  $this
    ->assertFalse($co[2]
    ->access('take', $user), 'Check that object 3 is not accessible.');
  $this
    ->assertFalse($co[3]
    ->access('take', $user), 'Check that object 4 is not accessible.');

  // (until they are enrolled)
  $course
    ->enroll($user);

  // User should be able to access first object, but no others.
  $this
    ->assertTrue($co[0]
    ->access('take', $user), 'Check that object 1 is accessible after enrollment.');
  $this
    ->assertFalse($co[1]
    ->access('take', $user), 'Check that object 2 is not accessible.');
  $this
    ->assertFalse($co[2]
    ->access('take', $user), 'Check that object 3 is not accessible.');
  $this
    ->assertFalse($co[3]
    ->access('take', $user), 'Check that object 4 is not accessible.');

  // Set 1st object to complete.
  $co[0]
    ->getFulfillment($user)
    ->setComplete(1)
    ->save();

  // Reset entity access cache.
  $this
    ->assertTrue($co[1]
    ->access('take', $user), 'Check that object 2 is accessible after object 1 is complete.');
  $this
    ->assertFalse($co[2]
    ->access('take', $user), 'Check that object 3 is not accessible after object 1 is complete.');
  $this
    ->assertFalse($co[3]
    ->access('take', $user), 'Check that object 4 is not accessible after object 1 is complete.');

  // Complete the rest of the course objects.
  $co[1]
    ->getFulfillment($user)
    ->setComplete(1)
    ->save();
  $co[2]
    ->getFulfillment($user)
    ->setComplete(1)
    ->save();
  $co[3]
    ->getFulfillment($user)
    ->setComplete(1)
    ->save();

  // Reset entity access cache.
  $this
    ->assertTrue($course
    ->getEnrollment($user)
    ->get('complete')->value, 'Check that course is complete.');
}