public function test_course_completion()
 {
     global $DB;
     $this->resetAfterTest();
     // Enable avaibility.
     // If not enabled all conditional fields will be ignored.
     set_config('enableavailability', 1);
     // Enable course completion.
     // If not enabled all completion settings will be ignored.
     set_config('enablecompletion', COMPLETION_ENABLED);
     $generator = $this->getDataGenerator();
     // Create course with completion tracking enabled.
     $course = $generator->create_course(['enablecompletion' => 1, 'numsections' => 3], ['createsections' => true]);
     // Enrol user to completion tracking course.
     $sturole = $DB->get_record('role', array('shortname' => 'student'));
     $generator->enrol_user($this->user1->id, $course->id, $sturole->id);
     // Create page with completion marked on view.
     $page1 = $generator->create_module('page', array('course' => $course->id, 'name' => 'page1 complete on view'), array('completion' => 2, 'completionview' => 1));
     $modinfo = get_fast_modinfo($course);
     $page1cm = $modinfo->get_cm($page1->cmid);
     // Create page restricted to only show when first page is viewed.
     $moduleinfo = (object) [];
     $moduleinfo->course = $course->id;
     $moduleinfo->name = 'page2 available after page1 viewed';
     $moduleinfo->availability = json_encode(\core_availability\tree::get_root_json([\availability_completion\condition::get_json($page1->cmid, COMPLETION_COMPLETE)], '&'));
     $page2 = $generator->create_module('page', $moduleinfo);
     // Make section 2 restricted to only show when first page is viewed.
     $section = $modinfo->get_section_info(2);
     $sectionupdate = ['id' => $section->id, 'availability' => json_encode(\core_availability\tree::get_root_json([\availability_completion\condition::get_json($page1->cmid, COMPLETION_COMPLETE)], '&'))];
     $DB->update_record('course_sections', $sectionupdate);
     // Check user1 has expected unavailable section and mod.
     $this->setUser($this->user1);
     // Dump cache and reget modinfo.
     get_fast_modinfo($course, 0, true);
     $modinfo = get_fast_modinfo($course);
     $page2cm = $modinfo->get_cm($page2->cmid);
     list($previouslyunavailablesections, $previouslyunavailablemods) = local::conditionally_unavailable_elements($course);
     $this->assertContains(2, $previouslyunavailablesections);
     $this->assertContains($page2cm->id, $previouslyunavailablemods);
     // View page1 to trigger completion
     $context = context_module::instance($page1->cmid);
     page_view($page1, $course, $page1cm, $context);
     $completion = new completion_info($course);
     $completiondata = $completion->get_data($page1cm);
     $this->assertEquals(COMPLETION_COMPLETE, $completiondata->completionstate);
     get_fast_modinfo($course, 0, true);
     // Reset modinfo.
     // Make sure that unavailable sections and mods no longer contain the ones requiring availabililty criteria
     // satisfying.
     list($unavailablesections, $unavailablemods) = local::conditionally_unavailable_elements($course);
     $this->assertNotContains($page2cm->id, $unavailablemods);
     $this->assertNotContains(2, $unavailablesections);
     $result = $this->courseservice->course_completion($course->shortname, $previouslyunavailablesections, $previouslyunavailablemods);
     // Make sure that the second page module (which is now newly available) appears in the list of newly available
     // module html.
     $this->assertTrue(isset($result['newlyavailablemodhtml'][$page2->cmid]));
     // Make sure that the second section (which is now wnely available) appears in the list of newly available
     // section html.
     $this->assertTrue(isset($result['newlyavailablesectionhtml'][2]));
 }