diff --git a/tests/base_test.php b/tests/base_test.php new file mode 100644 index 0000000..74e32c7 --- /dev/null +++ b/tests/base_test.php @@ -0,0 +1,107 @@ +. + +/** + * Base class for unit tests for mod_assign. + * + * @package blocks_completion_progress + * @category phpunit + * @copyright 2017 onwards Nelson Moller {@link http://moodle.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once($CFG->dirroot . '/mod/assign/locallib.php'); + +class mod_assign_base_testcase extends advanced_testcase { + + /** @const Default number of students to create */ + const DEFAULT_STUDENT_COUNT = 3; + /** @const Default number of teachers to create */ + const DEFAULT_TEACHER_COUNT = 1; + + /** + * Setup function - we will create a course and add an assign instance to it. + */ + protected function setUp() { + global $DB; + + $this->resetAfterTest(true); + + set_config('enablecompletion', 1); + + $this->course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1)); + $this->teachers = array(); + for ($i = 0; $i < self::DEFAULT_TEACHER_COUNT; $i++) { + array_push($this->teachers, $this->getDataGenerator()->create_user()); + } + + $this->students = array(); + for ($i = 0; $i < self::DEFAULT_STUDENT_COUNT; $i++) { + array_push($this->students, $this->getDataGenerator()->create_user()); + } + + + $teacherrole = $DB->get_record('role', array('shortname'=>'teacher')); + foreach ($this->teachers as $i => $teacher) { + $this->getDataGenerator()->enrol_user($teacher->id, + $this->course->id, + $teacherrole->id); + } + + + $studentrole = $DB->get_record('role', array('shortname'=>'student')); + foreach ($this->students as $i => $student) { + $this->getDataGenerator()->enrol_user($student->id, + $this->course->id, + $studentrole->id); + } + } + + /** + * Convenience function to create a testable instance of an assignment. + * + * @param array $params Array of parameters to pass to the generator + * @return testable_assign Testable wrapper around the assign class. + */ + protected function create_instance($params=array()) { + $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign'); + $params['course'] = $this->course->id; + $instance = $generator->create_instance($params); + $cm = get_coursemodule_from_instance('assign', $instance->id); + $context = context_module::instance($cm->id); + return new assign($context, $cm, $this->course); + } + + public function test_assign_get_completion_state() { + global $DB; + $assign = $this->create_instance(array('submissiondrafts' => 0, 'completionsubmit' => 1)); + + $this->setUser($this->students[0]); + $result = assign_get_completion_state($this->course, $assign->get_course_module(), $this->students[0]->id, false); + $this->assertFalse($result); + $submission = $assign->get_user_submission($this->students[0]->id, true); + $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED; + $DB->update_record('assign_submission', $submission); + + $result = assign_get_completion_state($this->course, $assign->get_course_module(), $this->students[0]->id, false); + + $this->assertTrue($result); + } +} \ No newline at end of file