. /** * Webservices test for attendance plugin. * * @package mod_attendance * @copyright 2015 Caio Bressan Doneda * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ if (!defined('MOODLE_INTERNAL')) { die('Direct access to this script is forbidden.'); } global $CFG; // Include the code to test. require_once($CFG->dirroot . '/mod/attendance/classes/attendance_webservices_handler.php'); require_once($CFG->dirroot . '/mod/attendance/classes/structure.php'); /** * This class contains the test cases for the functions in attendance_webservices_handler.php. * @copyright 2015 Caio Bressan Doneda * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class attendance_webservices_tests extends advanced_testcase { /** @var coursecat */ protected $category; /** @var stdClass */ protected $course; /** @var stdClass */ protected $attendance; /** @var stdClass */ protected $teacher; /** @var array */ protected $students; /** @var array */ protected $sessions; /** * Setup class. */ public function setUp() { $this->category = $this->getDataGenerator()->create_category(); $this->course = $this->getDataGenerator()->create_course(array('category' => $this->category->id)); $this->attendance = $this->create_attendance(); $this->create_and_enrol_users(); $this->setUser($this->teacher); $session = new stdClass(); $session->sessdate = time(); $session->duration = 6000; $session->description = ""; $session->descriptionformat = 1; $session->descriptionitemid = 0; $session->timemodified = time(); $session->statusset = 0; $session->groupid = 0; $session->absenteereport = 1; $session->calendarevent = 0; // Creating two sessions. $this->sessions[] = $session; $this->attendance->add_sessions($this->sessions); } /** * Create new attendance activity. */ private function create_attendance() { global $DB; $att = $this->getDataGenerator()->create_module('attendance', array('course' => $this->course->id)); $cm = $DB->get_record('course_modules', array('id' => $att->cmid)); unset($att->cmid); return new mod_attendance_structure($att, $cm, $this->course); } /** Creating 10 students and 1 teacher. */ protected function create_and_enrol_users() { $this->students = array(); for ($i = 0; $i < 10; $i++) { $student = $this->getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($student->id, $this->course->id, 5); // Enrol as student. $this->students[] = $student; } $this->teacher = $this->getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, 3); // Enrol as teacher. } public function test_get_courses_with_today_sessions() { $this->resetAfterTest(true); // Just adding the same session again to check if the method returns the right amount of instances. $this->attendance->add_sessions($this->sessions); $courseswithsessions = attendance_handler::get_courses_with_today_sessions($this->teacher->id); $this->assertTrue(is_array($courseswithsessions)); $this->assertEquals(count($courseswithsessions), 1); $course = array_pop($courseswithsessions); $this->assertEquals($course->fullname, $this->course->fullname); $attendanceinstance = array_pop($course->attendance_instances); $this->assertEquals(count($attendanceinstance['today_sessions']), 2); } public function test_get_courses_with_today_sessions_multiple_instances() { $this->resetAfterTest(true); // Make another attendance. $second = $this->create_attendance(); // Just add the same session. $secondsession = clone $this->sessions[0]; $secondsession->sessdate += 3600; $second->add_sessions([$secondsession]); $courseswithsessions = attendance_handler::get_courses_with_today_sessions($this->teacher->id); $this->assertTrue(is_array($courseswithsessions)); $this->assertEquals(count($courseswithsessions), 1); $course = array_pop($courseswithsessions); $this->assertEquals(count($course->attendance_instances), 2); } public function test_get_session() { $this->resetAfterTest(true); $courseswithsessions = attendance_handler::get_courses_with_today_sessions($this->teacher->id); $course = array_pop($courseswithsessions); $attendanceinstance = array_pop($course->attendance_instances); $session = array_pop($attendanceinstance['today_sessions']); $sessioninfo = attendance_handler::get_session($session->id); $this->assertEquals($this->attendance->id, $sessioninfo->attendanceid); $this->assertEquals($session->id, $sessioninfo->id); $this->assertEquals(count($sessioninfo->users), 10); } public function test_get_session_with_group() { $this->resetAfterTest(true); // Create a group in our course, and add some students to it. $group = new stdClass(); $group->courseid = $this->course->id; $group = $this->getDataGenerator()->create_group($group); for ($i = 0; $i < 5; $i++) { $member = new stdClass; $member->groupid = $group->id; $member->userid = $this->students[$i]->id; $this->getDataGenerator()->create_group_member($member); } // Add a session that's identical to the first, but with a group. $session = clone $this->sessions[0]; $session->groupid = $group->id; $session->sessdate += 3600; // Make sure it appears second in the list. $this->attendance->add_sessions([$session]); $courseswithsessions = attendance_handler::get_courses_with_today_sessions($this->teacher->id); $course = array_pop($courseswithsessions); $attendanceinstance = array_pop($course->attendance_instances); $session = array_pop($attendanceinstance['today_sessions']); $sessioninfo = attendance_handler::get_session($session->id); $this->assertEquals($session->id, $sessioninfo->id); $this->assertEquals($group->id, $sessioninfo->groupid); $this->assertEquals(count($sessioninfo->users), 5); } public function test_update_user_status() { $this->resetAfterTest(true); $courseswithsessions = attendance_handler::get_courses_with_today_sessions($this->teacher->id); $course = array_pop($courseswithsessions); $attendanceinstance = array_pop($course->attendance_instances); $session = array_pop($attendanceinstance['today_sessions']); $sessioninfo = attendance_handler::get_session($session->id); $student = array_pop($sessioninfo->users); $status = array_pop($sessioninfo->statuses); $statusset = $sessioninfo->statusset; attendance_handler::update_user_status($session->id, $student->id, $this->teacher->id, $status->id, $statusset); $sessioninfo = attendance_handler::get_session($session->id); $log = $sessioninfo->attendance_log; $studentlog = $log[$student->id]; $this->assertEquals($status->id, $studentlog->statusid); } }