function CourseDeleteTest::testCourseDelete in Course 3.x
Ensure that data is cleaned up after a course is deleted.
File
- tests/
src/ Functional/ CourseDeleteTest.php, line 22
Class
- CourseDeleteTest
- Tests for Course deletion.
Namespace
Drupal\Tests\course\FunctionalCode
function testCourseDelete() {
// Add two course objects to a course.
$course = $this
->createCourse();
$co1 = CourseObject::create([
'object_type' => 'course_test_object',
]);
$co1
->setCourse($course);
$co1
->setOption('title', 'Course object 1');
$co1
->setOption('weight', 1);
$co1
->save();
$co2 = CourseObject::create([
'object_type' => 'course_test_object',
]);
$co2
->setCourse($course);
$co2
->setOption('title', 'Course object 2');
$co2
->setOption('weight', 2);
$co2
->save();
// Login, enroll, and try to access the objects via links.
$this
->drupalLogin($this->student_user);
$course_enrollment = $course
->enroll($this->student_user);
$course_enrollment2 = $course
->enroll($this->admin_user);
$this
->drupalGet("course/{$course->id()}/take");
$this
->clickLink('Course object 1');
// Set the objects complete.
$cof1 = $co1
->getFulfillment($this->student_user);
$cof2 = $co2
->getFulfillment($this->student_user);
$cof1
->setComplete(1)
->save();
$cof2
->setComplete(1)
->save();
$this
->refreshVariables();
// Delete the course, objects, and fulfillments.
$course
->delete();
$this
->assertNull(Course::load($course
->id()), 'Course #1 was deleted');
$this
->assertNull(CourseEnrollment::load($course_enrollment
->id()), 'Course enrollment #1 was deleted');
$this
->assertNull(CourseEnrollment::load($course_enrollment2
->id()), 'Course enrollment #2 was deleted');
$this
->assertNull(CourseObject::load($co1
->id()), 'Course object #1 was deleted');
$this
->assertNull(CourseObject::load($co2
->id()), 'Course object #2 was deleted');
$this
->assertNull(CourseObjectFulfillment::load($cof1
->id()), 'Course object fulfillment #1 was deleted');
$this
->assertNull(CourseObjectFulfillment::load($cof2
->id()), 'Course object fulfillment #2 was deleted');
}