View source
<?php
namespace Drupal\Tests\course_book\Functional;
use Drupal;
use Drupal\course\Entity\CourseObject;
use Drupal\Tests\course\Functional\CourseTestBase;
use function node_access_rebuild;
class CourseObjectBookTest extends CourseTestBase {
protected static $modules = array(
'course',
'course_book',
);
function testBookCourseObject() {
$course = $this
->createCourse();
$co1 = CourseObject::create([
'object_type' => 'book',
]);
$co1
->setOption('book_tracking', 'all');
$co1
->setCourse($course);
$co1
->save();
$this
->assertTrue($co1
->getInstanceId() > 0, 'book node created on course object save.');
$bp1 = $this
->drupalCreateNode(array(
'type' => 'book',
'book' => array(
'bid' => $co1
->getInstanceId(),
'pid' => -1,
),
));
$bp2 = $this
->drupalCreateNode(array(
'type' => 'book',
'book' => array(
'bid' => $co1
->getInstanceId(),
'pid' => -1,
),
));
$course
->enroll($this->student_user);
$this
->assertFalse($co1
->getFulfillment($this->student_user)
->isComplete(), 'Check that book object is not complete.');
$this
->drupalLogin($this->student_user);
$this
->drupalGet("node/" . $co1
->getInstanceId());
$this
->drupalGet("node/{$bp1->id()}");
\Drupal::entityTypeManager()
->getStorage('course_object_fulfillment')
->resetCache();
$this
->assertFalse($co1
->getFulfillment($this->student_user)
->isComplete(), 'Check that book object is not complete after visiting 2/3 pages.');
$this
->drupalGet("node/{$bp2->id()}");
\Drupal::entityTypeManager()
->getStorage('course_object_fulfillment')
->resetCache();
$this
->assertTrue($co1
->getFulfillment($this->student_user)
->isComplete(), 'Check that book object is now complete after visiting 3/3 pages.');
}
function testBookCourseObjectContentAccess() {
$this
->pass('Please fix me.');
return;
$this
->drupalLogin($this->admin_user);
node_access_rebuild();
$courseNode = $this
->createCourse();
$co1 = CourseObject::create([
'object_type' => 'book',
]);
$co1
->setCourse($courseNode->nid);
$co1
->setOption('private', 1);
$co1
->save();
$bp1 = $this
->drupalCreateNode(array(
'type' => 'book',
'book' => array(
'bid' => $co1
->getInstanceId(),
),
));
$bp2 = $this
->drupalCreateNode(array(
'type' => 'book',
'book' => array(
'bid' => $co1
->getInstanceId(),
),
));
$co1
->save();
Drupal::database()
->query("delete from {node_access} where realm = 'all'");
$this
->drupalLogin($this->student_user);
$this
->drupalGet("node/" . $co1
->getInstanceId());
$this
->assertResponse(403);
$this
->drupalGet("node/{$bp1->nid}");
$this
->assertResponse(403);
$this
->drupalGet("node/{$bp2->nid}");
$this
->assertResponse(403);
$this
->drupalLogin($this->admin_user);
$bp3 = $this
->drupalCreateNode(array(
'type' => 'book',
'book' => array(
'bid' => $co1
->getInstanceId(),
),
));
$co1
->save();
Drupal::database()
->query("delete from {node_access} where realm = 'all'");
$this
->drupalLogin($this->student_user);
$this
->drupalGet("node/{$bp3->nid}");
$this
->assertResponse(403);
course_enroll($courseNode, $this->student_user);
$this
->drupalGet("node/{$courseNode->nid}/object/" . $co1
->id());
$this
->drupalGet("node/{$bp1->nid}");
$this
->assertResponse(200);
$this
->drupalGet("node/{$bp2->nid}");
$this
->assertResponse(200);
$this
->drupalGet("node/{$bp3->nid}");
$this
->assertResponse(200);
}
}