Merge branch 'install_master' of https://git.in.moodle.com/amosbot/moodle-install
[moodle.git] / analytics / tests / course_test.php
blob00cfea336917e1859d0fd099b6c6d95389cc8dc1
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 /**
18 * Unit tests for course.
20 * @package core_analytics
21 * @copyright 2016 David MonllaĆ³ {@link http://www.davidmonllao.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 /**
28 * Unit tests for course.
30 * @package core_analytics
31 * @copyright 2016 David MonllaĆ³ {@link http://www.davidmonllao.com}
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class core_analytics_course_testcase extends advanced_testcase {
36 public function setUp() {
37 global $DB;
39 $this->course = $this->getDataGenerator()->create_course(['startdate' => 0]);
40 $this->stu1 = $this->getDataGenerator()->create_user();
41 $this->stu2 = $this->getDataGenerator()->create_user();
42 $this->both = $this->getDataGenerator()->create_user();
43 $this->editingteacher = $this->getDataGenerator()->create_user();
44 $this->teacher = $this->getDataGenerator()->create_user();
46 $this->studentroleid = $DB->get_field('role', 'id', array('shortname' => 'student'));
47 $this->editingteacherroleid = $DB->get_field('role', 'id', array('shortname' => 'editingteacher'));
48 $this->teacherroleid = $DB->get_field('role', 'id', array('shortname' => 'teacher'));
50 $this->getDataGenerator()->enrol_user($this->stu1->id, $this->course->id, $this->studentroleid);
51 $this->getDataGenerator()->enrol_user($this->stu2->id, $this->course->id, $this->studentroleid);
52 $this->getDataGenerator()->enrol_user($this->both->id, $this->course->id, $this->studentroleid);
53 $this->getDataGenerator()->enrol_user($this->both->id, $this->course->id, $this->editingteacherroleid);
54 $this->getDataGenerator()->enrol_user($this->editingteacher->id, $this->course->id, $this->editingteacherroleid);
55 $this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, $this->teacherroleid);
58 /**
59 * Users tests.
61 public function test_users() {
62 global $DB;
64 $this->resetAfterTest(true);
66 $courseman = new \core_analytics\course($this->course->id);
67 $this->assertCount(3, $courseman->get_user_ids(array($this->studentroleid)));
68 $this->assertCount(2, $courseman->get_user_ids(array($this->editingteacherroleid)));
69 $this->assertCount(1, $courseman->get_user_ids(array($this->teacherroleid)));
71 // Distinct is applied.
72 $this->assertCount(3, $courseman->get_user_ids(array($this->editingteacherroleid, $this->teacherroleid)));
73 $this->assertCount(4, $courseman->get_user_ids(array($this->editingteacherroleid, $this->studentroleid)));
76 /**
77 * Course validation tests.
79 * @return void
81 public function test_course_validation() {
82 global $DB;
84 $this->resetAfterTest(true);
86 $courseman = new \core_analytics\course($this->course->id);
87 $this->assertFalse($courseman->was_started());
88 $this->assertFalse($courseman->is_finished());
90 // Nothing should change when assigning as teacher.
91 for ($i = 0; $i < 10; $i++) {
92 $user = $this->getDataGenerator()->create_user();
93 $this->getDataGenerator()->enrol_user($user->id, $this->course->id, $this->teacherroleid);
95 $courseman = new \core_analytics\course($this->course->id);
96 $this->assertFalse($courseman->was_started());
97 $this->assertFalse($courseman->is_finished());
99 // More students now.
100 for ($i = 0; $i < 10; $i++) {
101 $user = $this->getDataGenerator()->create_user();
102 $this->getDataGenerator()->enrol_user($user->id, $this->course->id, $this->studentroleid);
104 $courseman = new \core_analytics\course($this->course->id);
105 $this->assertFalse($courseman->was_started());
106 $this->assertFalse($courseman->is_finished());
108 // Valid start date unknown end date.
109 $this->course->startdate = gmmktime('0', '0', '0', 10, 24, 2015);
110 $DB->update_record('course', $this->course);
111 $courseman = new \core_analytics\course($this->course->id);
112 $this->assertTrue($courseman->was_started());
113 $this->assertFalse($courseman->is_finished());
115 // Valid start and end date.
116 $this->course->enddate = gmmktime('0', '0', '0', 8, 27, 2016);
117 $DB->update_record('course', $this->course);
118 $courseman = new \core_analytics\course($this->course->id);
119 $this->assertTrue($courseman->was_started());
120 $this->assertTrue($courseman->is_finished());
122 // Valid start and ongoing course.
123 $this->course->enddate = gmmktime('0', '0', '0', 8, 27, 2286);
124 $DB->update_record('course', $this->course);
125 $courseman = new \core_analytics\course($this->course->id);
126 $this->assertTrue($courseman->was_started());
127 $this->assertFalse($courseman->is_finished());
131 * Get the minimum time that is considered valid according to guess_end logic.
133 * @param int $time
134 * @return int
136 protected function time_greater_than($time) {
137 return $time - (WEEKSECS * 2);
141 * Get the maximum time that is considered valid according to guess_end logic.
143 * @param int $time
144 * @return int
146 protected function time_less_than($time) {
147 return $time + (WEEKSECS * 2);