NOBUG: Fixed SVG browser compatibility
[moodle.git] / analytics / tests / course_test.php
blobbff564b4ead3cd3dcd904d2e469bf7d884ffcd3d
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 namespace core_analytics;
19 /**
20 * Unit tests for course.
22 * @package core_analytics
23 * @copyright 2016 David MonllaĆ³ {@link http://www.davidmonllao.com}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 class course_test extends \advanced_testcase {
28 /** @var \stdClass Course record. */
29 protected $course;
31 /** @var \stdClass Student 1 user record. */
32 protected $stu1;
34 /** @var \stdClass Student 2 user record. */
35 protected $stu2;
37 /** @var \stdClass Student both user record. */
38 protected $both;
40 /** @var \stdClass Editing teacher user record. */
41 protected $editingteacher;
43 /** @var \stdClass Teacher user record. */
44 protected $teacher;
46 /** @var int Student role ID record. */
47 protected $studentroleid;
49 /** @var int Editing teacher role ID record. */
50 protected $editingteacherroleid;
52 /** @var int Teacher role ID record. */
53 protected $teacherroleid;
55 public function setUp(): void {
56 global $DB;
58 $this->course = $this->getDataGenerator()->create_course(['startdate' => 0]);
59 $this->stu1 = $this->getDataGenerator()->create_user();
60 $this->stu2 = $this->getDataGenerator()->create_user();
61 $this->both = $this->getDataGenerator()->create_user();
62 $this->editingteacher = $this->getDataGenerator()->create_user();
63 $this->teacher = $this->getDataGenerator()->create_user();
65 $this->studentroleid = $DB->get_field('role', 'id', array('shortname' => 'student'));
66 $this->editingteacherroleid = $DB->get_field('role', 'id', array('shortname' => 'editingteacher'));
67 $this->teacherroleid = $DB->get_field('role', 'id', array('shortname' => 'teacher'));
69 $this->getDataGenerator()->enrol_user($this->stu1->id, $this->course->id, $this->studentroleid);
70 $this->getDataGenerator()->enrol_user($this->stu2->id, $this->course->id, $this->studentroleid);
71 $this->getDataGenerator()->enrol_user($this->both->id, $this->course->id, $this->studentroleid);
72 $this->getDataGenerator()->enrol_user($this->both->id, $this->course->id, $this->editingteacherroleid);
73 $this->getDataGenerator()->enrol_user($this->editingteacher->id, $this->course->id, $this->editingteacherroleid);
74 $this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, $this->teacherroleid);
77 /**
78 * Users tests.
80 public function test_users() {
81 global $DB;
83 $this->resetAfterTest(true);
85 $courseman = new \core_analytics\course($this->course->id);
86 $this->assertCount(3, $courseman->get_user_ids(array($this->studentroleid)));
87 $this->assertCount(2, $courseman->get_user_ids(array($this->editingteacherroleid)));
88 $this->assertCount(1, $courseman->get_user_ids(array($this->teacherroleid)));
90 // Distinct is applied.
91 $this->assertCount(3, $courseman->get_user_ids(array($this->editingteacherroleid, $this->teacherroleid)));
92 $this->assertCount(4, $courseman->get_user_ids(array($this->editingteacherroleid, $this->studentroleid)));
95 /**
96 * Course validation tests.
98 * @return void
100 public function test_course_validation() {
101 global $DB;
103 $this->resetAfterTest(true);
105 $courseman = new \core_analytics\course($this->course->id);
106 $this->assertFalse($courseman->was_started());
107 $this->assertFalse($courseman->is_finished());
109 // Nothing should change when assigning as teacher.
110 for ($i = 0; $i < 10; $i++) {
111 $user = $this->getDataGenerator()->create_user();
112 $this->getDataGenerator()->enrol_user($user->id, $this->course->id, $this->teacherroleid);
114 $courseman = new \core_analytics\course($this->course->id);
115 $this->assertFalse($courseman->was_started());
116 $this->assertFalse($courseman->is_finished());
118 // More students now.
119 for ($i = 0; $i < 10; $i++) {
120 $user = $this->getDataGenerator()->create_user();
121 $this->getDataGenerator()->enrol_user($user->id, $this->course->id, $this->studentroleid);
123 $courseman = new \core_analytics\course($this->course->id);
124 $this->assertFalse($courseman->was_started());
125 $this->assertFalse($courseman->is_finished());
127 // Valid start date unknown end date.
128 $this->course->startdate = gmmktime('0', '0', '0', 10, 24, 2015);
129 $DB->update_record('course', $this->course);
130 $courseman = new \core_analytics\course($this->course->id);
131 $this->assertTrue($courseman->was_started());
132 $this->assertFalse($courseman->is_finished());
134 // Valid start and end date.
135 $this->course->enddate = gmmktime('0', '0', '0', 8, 27, 2016);
136 $DB->update_record('course', $this->course);
137 $courseman = new \core_analytics\course($this->course->id);
138 $this->assertTrue($courseman->was_started());
139 $this->assertTrue($courseman->is_finished());
141 // Valid start and ongoing course.
142 $this->course->enddate = gmmktime('0', '0', '0', 8, 27, 2286);
143 $DB->update_record('course', $this->course);
144 $courseman = new \core_analytics\course($this->course->id);
145 $this->assertTrue($courseman->was_started());
146 $this->assertFalse($courseman->is_finished());
150 * Get the minimum time that is considered valid according to guess_end logic.
152 * @param int $time
153 * @return int
155 protected function time_greater_than($time) {
156 return $time - (WEEKSECS * 2);
160 * Get the maximum time that is considered valid according to guess_end logic.
162 * @param int $time
163 * @return int
165 protected function time_less_than($time) {
166 return $time + (WEEKSECS * 2);