You are here

CourseTestBase.php in Course 3.x

File

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

namespace Drupal\Tests\course\Functional;

use Drupal\course\Entity\CourseObject;
use Drupal\course\Entity\Course;
use Drupal\Tests\BrowserTestBase;
use stdClass;
use function course_get_course_object;

/**
 * Master class for Course tests.
 */
abstract class CourseTestBase extends BrowserTestBase {

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->admin_user = $this
      ->drupalCreateUser(array(
      'bypass node access',
      'administer course',
      'administer courses',
    ));
    $this->student_user = $this
      ->createStudentUser();
    $this
      ->drupalLogin($this->admin_user);
  }
  protected function createStudentUser() {
    return $this
      ->drupalCreateUser();
  }

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * {@inheritdoc}
   */
  protected static $modules = array(
    'course_test',
  );

  /**
   * Create a course node using the test content type.
   *
   * @return \Drupal\course\Entity\Course
   */
  function createCourse(array $extra = array()) {
    $defaults = array(
      'type' => 'course',
    );
    $course = Course::create(array_merge_recursive($defaults, $extra));
    $course
      ->save();
    return $course;
  }

  /**
   * Create a new persistent course object on a Course.
   *
   * @param Course $course
   * @return Course
   */
  function createCourseObject(Course $course, $graded = FALSE) {
    static $weight = 0;
    if ($graded) {
      $courseObject = CourseObject::create([
        'object_type' => 'course_test_graded_object',
      ]);
    }
    else {
      $courseObject = CourseObject::create([
        'object_type' => 'course_test_object',
      ]);
    }
    $courseObject
      ->setCourse($course);
    $courseObject
      ->setOption('weight', $weight++);
    $courseObject
      ->save();
    return $courseObject;
  }

}

Classes

Namesort descending Description
CourseTestBase Master class for Course tests.