You are here

CourseDeleteTest.php in Course 3.x

File

tests/src/Functional/CourseDeleteTest.php
View source
<?php

namespace Drupal\Tests\course\Functional;

use Drupal\course\Entity\Course;
use Drupal\course\Entity\CourseEnrollment;
use Drupal\course\Entity\CourseObject;
use Drupal\course\Entity\CourseObjectFulfillment;

/**
 * Tests for Course deletion.
 *
 * @group course
 */
class CourseDeleteTest extends CourseTestBase {
  protected static $modules = [
    'course_test',
    'block',
  ];

  /**
   * Ensure that data is cleaned up after a course is deleted.
   */
  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');
  }

}

Classes

Namesort descending Description
CourseDeleteTest Tests for Course deletion.