View source
<?php
namespace Drupal\Tests\course\Functional;
class CourseObjectAccessTestCase extends CourseTestCase {
use \Drupal\Core\Test\RefreshVariablesTrait;
function testCourseObjectLinearWorkflow() {
$course = $this
->createCourse();
for ($i = 1; $i <= 4; $i++) {
$temp = $this
->createCourseObject($course);
$temp
->setOption('title', "Object {$i}")
->setOption('weight', $i)
->save();
}
$user = $this->student_user;
$co = array_values($course
->getObjects());
$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.');
$course
->enroll($user);
$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.');
$co[0]
->getFulfillment($user)
->setComplete(1)
->save();
$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.');
$co[1]
->getFulfillment($user)
->setComplete(1)
->save();
$co[2]
->getFulfillment($user)
->setComplete(1)
->save();
$co[3]
->getFulfillment($user)
->setComplete(1)
->save();
$this
->assertTrue($course
->getTracker($user)
->get('complete')->value, 'Check that course is complete.');
}
function testCourseObjectOptionalWorkflow() {
$course = $this
->createCourse();
for ($i = 1; $i <= 4; $i++) {
$temp = $this
->createCourseObject($course);
$temp
->setOption('weight', $i)
->save();
}
$user = $this->student_user;
$co = array_values($course
->getObjects());
$course
->enroll($user);
$co[0]
->setOption('required', 0);
$co[1]
->setOption('required', 1);
$co[2]
->setOption('required', 0);
$co[3]
->setOption('required', 0);
$this
->assertFalse($co[2]
->access('take', $user) || $co[3]
->access('take', $user), 'User is blocked by second required object to get to fourth optional object.');
$this
->assertTrue($co[0]
->access('take', $user), 'User has access to first optional object.');
$this
->assertTrue($co[1]
->access('take', $user), 'User has access to second required object.');
$co[1]
->getFulfillment($user)
->setComplete(1)
->save();
$this
->assertTrue($co[2]
->access('take', $user) && $co[3]
->access('take', $user), 'User granted access to remaining optional objects.');
}
function testCourseSkippableRequiredObjectsWorkflow() {
$course = $this
->createCourse();
for ($i = 1; $i <= 4; $i++) {
$temp = $this
->createCourseObject($course);
$temp
->setOption('weight', $i)
->save();
}
$user = $this->student_user;
$co = array_values($course
->getObjects());
$course
->enroll($user);
$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);
$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.');
$co[0]
->getFulfillment($user)
->setComplete(1)
->save();
$this
->assertTrue($co[1]
->access('take', $user), 'User can access second object.');
$this
->assertTrue($co[2]
->access('take', $user), 'User can access third object.');
$this
->assertFalse($co[3]
->access('take', $user), 'User is blocked from fourth object.');
$co[2]
->getFulfillment($user)
->setComplete(1)
->save();
$co[3]
->getFulfillment($user)
->setComplete(1)
->save();
$this
->assertFalse($course
->getTracker($user)
->get('complete')->value, 'Check that course is not complete.');
$co[1]
->getFulfillment($user)
->setComplete(1)
->save();
$this
->assertTrue($course
->getTracker($user)
->get('complete')->value, 'Check that course is complete.');
}
function testHiddenCourseObjects() {
$course = $this
->createCourse();
$user = $this->student_user;
$this
->drupalLogin($user);
$o_optional = $this
->createCourseObject($course);
$o_optional
->set('required', FALSE)
->save();
$o1 = $this
->createCourseObject($course);
$o1
->set('required', TRUE)
->save();
$this
->assertTrue($o1
->access('see', $user));
$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.'));
$report = $course
->getEnrollment($user);
$this
->assertFalse($report
->get('complete')->value);
}
function testDisabledCourseObjects() {
$course = $this
->createCourse();
$user = $this->student_user;
$this
->drupalLogin($user);
$o_optional = $this
->createCourseObject($course);
$o_optional
->set('required', FALSE)
->save();
$o1 = $this
->createCourseObject($course);
$o1
->set('required', TRUE)
->save();
$this
->assertTrue($o1
->access('see', $user));
$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");
$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.'));
$report = $course
->getEnrollment($user);
$this
->assertTrue($report
->get('complete')->value);
$o1
->set('weight', -100)
->save();
$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.'));
}
}