View source
<?php
require_once drupal_get_path('module', 'course') . '/tests/CourseTestCase.test';
class CourseObjectAccessTestCase extends CourseTestCase {
public static function getInfo() {
return array(
'name' => 'Course object access',
'description' => 'Ensure that Course object access functions properly.',
'group' => 'Course',
);
}
function testCourseObjectLinearWorkflow() {
$courseNode = $this
->createCourseNode();
for ($i = 1; $i <= 4; $i++) {
$temp = $this
->createCourseObject($courseNode);
$temp
->setOption('weight', $i)
->save();
}
$user = $this->student_user;
$course = course_get_course($courseNode, $user, TRUE);
$co = $course
->getObjects();
$this
->assertFalse($co[0]
->access('take', $user), 'Check that object 1 is not accessible.');
course_enrol($courseNode, $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()
->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()
->setComplete(1)
->save();
$co[2]
->getFulfillment()
->setComplete(1)
->save();
$co[3]
->getFulfillment()
->setComplete(1)
->save();
$this
->assertTrue($course
->getTracker()
->getOption('complete'), 'Check that course is complete.');
}
function testCourseObjectOptionalWorkflow() {
$courseNode = $this
->createCourseNode();
for ($i = 1; $i <= 4; $i++) {
$temp = $this
->createCourseObject($courseNode);
$temp
->setOption('weight', $i)
->save();
}
$user = $this->student_user;
$course = course_get_course($courseNode, $user, TRUE);
$co = $course
->getObjects();
course_enrol($courseNode, $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()
->setComplete(1);
$this
->assertTrue($co[2]
->access('take', $user) && $co[3]
->access('take', $user), 'User granted access to remaining optional objects.');
}
function testHiddenCourseObjects() {
$courseNode = $this
->createCourseNode();
$user = $this->student_user;
$o1 = $this
->createCourseObject($courseNode);
$this
->assertTrue($o1
->access('see', $user));
$o1
->setOption('hidden', 1)
->save();
$this
->assertFalse($o1
->access('see', $user));
$o1
->setOption('hidden', 0)
->save();
$this
->assertTrue($o1
->access('see', $user));
}
function testDisabledCourseObjects() {
$courseNode = $this
->createCourseNode();
$user = $this->student_user;
$o1 = $this
->createCourseObject($courseNode);
$this
->assertTrue($o1
->access('see', $user));
$o1
->setOption('enabled', 1)
->save();
$this
->assertTrue($o1
->access('see', $user));
$o1
->setOption('enabled', 0)
->save();
$this
->assertFalse($o1
->access('see', $user));
}
}