You are here

function CourseObjectBookTest::testBookCourseObject in Course 3.x

File

modules/course_book/tests/src/Functional/CourseObjectBookTest.php, line 19

Class

CourseObjectBookTest
Tests books in courses.

Namespace

Drupal\Tests\course_book\Functional

Code

function testBookCourseObject() {

  // Create a course with 1 book.
  $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.');

  // Add some more book pages.
  $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,
    ),
  ));

  // Enroll the user in the course
  $course
    ->enroll($this->student_user);

  // Test fulfillment tracking, set to view all pages before complete.
  $this
    ->assertFalse($co1
    ->getFulfillment($this->student_user)
    ->isComplete(), 'Check that book object is not complete.');
  $this
    ->drupalLogin($this->student_user);

  // Visit the book parent
  $this
    ->drupalGet("node/" . $co1
    ->getInstanceId());

  // Visit the first book page
  $this
    ->drupalGet("node/{$bp1->id()}");

  // Test that course object is not yet complete.
  \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.');
}