Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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/>.
16
 
17
namespace core_analytics;
18
 
19
/**
20
 * Unit tests for course.
21
 *
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
25
 */
1441 ariadna 26
final class course_test extends \advanced_testcase {
1 efrain 27
 
28
    /** @var \stdClass Course record. */
29
    protected $course;
30
 
31
    /** @var \stdClass Student 1 user record. */
32
    protected $stu1;
33
 
34
    /** @var \stdClass Student 2 user record. */
35
    protected $stu2;
36
 
37
    /** @var \stdClass Student both user record. */
38
    protected $both;
39
 
40
    /** @var \stdClass Editing teacher user record. */
41
    protected $editingteacher;
42
 
43
    /** @var \stdClass Teacher user record. */
44
    protected $teacher;
45
 
46
    /** @var int Student role ID record. */
47
    protected $studentroleid;
48
 
49
    /** @var int Editing teacher role ID record. */
50
    protected $editingteacherroleid;
51
 
52
    /** @var int Teacher role ID record. */
53
    protected $teacherroleid;
54
 
55
    public function setUp(): void {
56
        global $DB;
1441 ariadna 57
        parent::setUp();
1 efrain 58
 
59
        $this->course = $this->getDataGenerator()->create_course(['startdate' => 0]);
60
        $this->stu1 = $this->getDataGenerator()->create_user();
61
        $this->stu2 = $this->getDataGenerator()->create_user();
62
        $this->both = $this->getDataGenerator()->create_user();
63
        $this->editingteacher = $this->getDataGenerator()->create_user();
64
        $this->teacher = $this->getDataGenerator()->create_user();
65
 
66
        $this->studentroleid = $DB->get_field('role', 'id', array('shortname' => 'student'));
67
        $this->editingteacherroleid = $DB->get_field('role', 'id', array('shortname' => 'editingteacher'));
68
        $this->teacherroleid = $DB->get_field('role', 'id', array('shortname' => 'teacher'));
69
 
70
        $this->getDataGenerator()->enrol_user($this->stu1->id, $this->course->id, $this->studentroleid);
71
        $this->getDataGenerator()->enrol_user($this->stu2->id, $this->course->id, $this->studentroleid);
72
        $this->getDataGenerator()->enrol_user($this->both->id, $this->course->id, $this->studentroleid);
73
        $this->getDataGenerator()->enrol_user($this->both->id, $this->course->id, $this->editingteacherroleid);
74
        $this->getDataGenerator()->enrol_user($this->editingteacher->id, $this->course->id, $this->editingteacherroleid);
75
        $this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, $this->teacherroleid);
76
    }
77
 
78
    /**
79
     * Users tests.
80
     */
11 efrain 81
    public function test_users(): void {
1 efrain 82
        global $DB;
83
 
84
        $this->resetAfterTest(true);
85
 
86
        $courseman = new \core_analytics\course($this->course->id);
87
        $this->assertCount(3, $courseman->get_user_ids(array($this->studentroleid)));
88
        $this->assertCount(2, $courseman->get_user_ids(array($this->editingteacherroleid)));
89
        $this->assertCount(1, $courseman->get_user_ids(array($this->teacherroleid)));
90
 
91
        // Distinct is applied.
92
        $this->assertCount(3, $courseman->get_user_ids(array($this->editingteacherroleid, $this->teacherroleid)));
93
        $this->assertCount(4, $courseman->get_user_ids(array($this->editingteacherroleid, $this->studentroleid)));
94
    }
95
 
96
    /**
97
     * Course validation tests.
98
     *
99
     * @return void
100
     */
11 efrain 101
    public function test_course_validation(): void {
1 efrain 102
        global $DB;
103
 
104
        $this->resetAfterTest(true);
105
 
106
        $courseman = new \core_analytics\course($this->course->id);
107
        $this->assertFalse($courseman->was_started());
108
        $this->assertFalse($courseman->is_finished());
109
 
110
        // Nothing should change when assigning as teacher.
111
        for ($i = 0; $i < 10; $i++) {
112
            $user = $this->getDataGenerator()->create_user();
113
            $this->getDataGenerator()->enrol_user($user->id, $this->course->id, $this->teacherroleid);
114
        }
115
        $courseman = new \core_analytics\course($this->course->id);
116
        $this->assertFalse($courseman->was_started());
117
        $this->assertFalse($courseman->is_finished());
118
 
119
        // More students now.
120
        for ($i = 0; $i < 10; $i++) {
121
            $user = $this->getDataGenerator()->create_user();
122
            $this->getDataGenerator()->enrol_user($user->id, $this->course->id, $this->studentroleid);
123
        }
124
        $courseman = new \core_analytics\course($this->course->id);
125
        $this->assertFalse($courseman->was_started());
126
        $this->assertFalse($courseman->is_finished());
127
 
128
        // Valid start date unknown end date.
129
        $this->course->startdate = gmmktime('0', '0', '0', 10, 24, 2015);
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
 
135
        // Valid start and end date.
136
        $this->course->enddate = gmmktime('0', '0', '0', 8, 27, 2016);
137
        $DB->update_record('course', $this->course);
138
        $courseman = new \core_analytics\course($this->course->id);
139
        $this->assertTrue($courseman->was_started());
140
        $this->assertTrue($courseman->is_finished());
141
 
142
        // Valid start and ongoing course.
143
        $this->course->enddate = gmmktime('0', '0', '0', 8, 27, 2286);
144
        $DB->update_record('course', $this->course);
145
        $courseman = new \core_analytics\course($this->course->id);
146
        $this->assertTrue($courseman->was_started());
147
        $this->assertFalse($courseman->is_finished());
148
    }
149
 
150
    /**
151
     * Get the minimum time that is considered valid according to guess_end logic.
152
     *
153
     * @param int $time
154
     * @return int
155
     */
156
    protected function time_greater_than($time) {
157
        return $time - (WEEKSECS * 2);
158
    }
159
 
160
    /**
161
     * Get the maximum time that is considered valid according to guess_end logic.
162
     *
163
     * @param int $time
164
     * @return int
165
     */
166
    protected function time_less_than($time) {
167
        return $time + (WEEKSECS * 2);
168
    }
169
}