MDL-59010 analytics: Logstores initialised during tests
[moodle.git] / analytics / tests / course_test.php
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.
13 //
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.
19  *
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
23  */
25 defined('MOODLE_INTERNAL') || die();
27 /**
28  * Unit tests for course.
29  *
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
33  */
34 class core_analytics_course_testcase extends advanced_testcase {
36     public function setUp() {
37         global $DB;
39         $this->course = $this->getDataGenerator()->create_course();
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'));
51         $this->getDataGenerator()->enrol_user($this->stu1->id, $this->course->id, $this->studentroleid);
52         $this->getDataGenerator()->enrol_user($this->stu2->id, $this->course->id, $this->studentroleid);
53         $this->getDataGenerator()->enrol_user($this->both->id, $this->course->id, $this->studentroleid);
54         $this->getDataGenerator()->enrol_user($this->both->id, $this->course->id, $this->editingteacherroleid);
55         $this->getDataGenerator()->enrol_user($this->editingteacher->id, $this->course->id, $this->editingteacherroleid);
56         $this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, $this->teacherroleid);
59         set_config('studentroles', $this->studentroleid, 'analytics');
60         set_config('teacherroles', $this->editingteacherroleid . ',' . $this->teacherroleid, 'analytics');
61     }
63     /**
64      * Users tests.
65      */
66     public function test_users() {
67         global $DB;
69         $this->resetAfterTest(true);
71         $courseman = new \core_analytics\course($this->course->id);
72         $this->assertCount(3, $courseman->get_user_ids(array($this->studentroleid)));
73         $this->assertCount(2, $courseman->get_user_ids(array($this->editingteacherroleid)));
74         $this->assertCount(1, $courseman->get_user_ids(array($this->teacherroleid)));
76         // Distinct is applied
77         $this->assertCount(3, $courseman->get_user_ids(array($this->editingteacherroleid, $this->teacherroleid)));
78         $this->assertCount(4, $courseman->get_user_ids(array($this->editingteacherroleid, $this->studentroleid)));
79     }
81     /**
82      * Course validation tests.
83      *
84      * @return void
85      */
86     public function test_course_validation() {
87         global $DB;
89         $this->resetAfterTest(true);
91         $courseman = new \core_analytics\course($this->course->id);
92         $this->assertFalse($courseman->was_started());
93         $this->assertFalse($courseman->is_finished());
94         $this->assertFalse($courseman->is_valid());
96         // Nothing should change when assigning as teacher.
97         for ($i = 0; $i < 10; $i++) {
98             $user = $this->getDataGenerator()->create_user();
99             $this->getDataGenerator()->enrol_user($user->id, $this->course->id, $this->teacherroleid);
100         }
101         $courseman = new \core_analytics\course($this->course->id);
102         $this->assertFalse($courseman->is_valid());
104         // More students now.
105         for ($i = 0; $i < 10; $i++) {
106             $user = $this->getDataGenerator()->create_user();
107             $this->getDataGenerator()->enrol_user($user->id, $this->course->id, $this->studentroleid);
108         }
109         $courseman = new \core_analytics\course($this->course->id);
110         $this->assertFalse($courseman->is_valid());
112         // Valid start date unknown end date.
113         $this->course->startdate = gmmktime('0', '0', '0', 10, 24, 2015);
114         $DB->update_record('course', $this->course);
115         $courseman = new \core_analytics\course($this->course->id);
116         $this->assertTrue($courseman->was_started());
117         $this->assertFalse($courseman->is_finished());
118         $this->assertFalse($courseman->is_valid());
120         // Valid start and end date.
121         $this->course->enddate = gmmktime('0', '0', '0', 8, 27, 2016);
122         $DB->update_record('course', $this->course);
123         $courseman = new \core_analytics\course($this->course->id);
124         $this->assertTrue($courseman->was_started());
125         $this->assertTrue($courseman->is_finished());
126         $this->assertTrue($courseman->is_valid());
128         // Valid start and ongoing course.
129         $this->course->enddate = gmmktime('0', '0', '0', 8, 27, 2286);
130         $DB->update_record('course', $this->course);
131         $courseman = new \core_analytics\course($this->course->id);
132         $this->assertTrue($courseman->was_started());
133         $this->assertFalse($courseman->is_finished());
134         $this->assertFalse($courseman->is_valid());
135     }
137     /**
138      * Get the minimum time that is considered valid according to guess_end logic.
139      *
140      * @param int $time
141      * @return int
142      */
143     protected function time_greater_than($time) {
144         return $time - (WEEKSECS * 2);
145     }
147     /**
148      * Get the maximum time that is considered valid according to guess_end logic.
149      *
150      * @param int $time
151      * @return int
152      */
153     protected function time_less_than($time) {
154         return $time + (WEEKSECS * 2);
155     }