MDL-59030 analytics: Social breadth accepting more than level 2
[moodle.git] / analytics / tests / course_test.php
CommitLineData
ff656bae
DM
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/**
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 */
24
25defined('MOODLE_INTERNAL') || die();
26
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 */
34class core_analytics_course_testcase extends advanced_testcase {
35
36 public function setUp() {
37 global $DB;
38
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();
45
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'));
49
ff656bae
DM
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);
ff656bae
DM
56 }
57
58 /**
59 * Users tests.
60 */
61 public function test_users() {
62 global $DB;
63
64 $this->resetAfterTest(true);
65
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)));
70
413f19bc 71 // Distinct is applied.
ff656bae
DM
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)));
74 }
75
76 /**
77 * Course validation tests.
78 *
79 * @return void
80 */
81 public function test_course_validation() {
82 global $DB;
83
84 $this->resetAfterTest(true);
85
86 $courseman = new \core_analytics\course($this->course->id);
87 $this->assertFalse($courseman->was_started());
88 $this->assertFalse($courseman->is_finished());
89 $this->assertFalse($courseman->is_valid());
90
91 // Nothing should change when assigning as teacher.
92 for ($i = 0; $i < 10; $i++) {
93 $user = $this->getDataGenerator()->create_user();
94 $this->getDataGenerator()->enrol_user($user->id, $this->course->id, $this->teacherroleid);
95 }
96 $courseman = new \core_analytics\course($this->course->id);
97 $this->assertFalse($courseman->is_valid());
98
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);
103 }
104 $courseman = new \core_analytics\course($this->course->id);
105 $this->assertFalse($courseman->is_valid());
106
107 // Valid start date unknown end date.
108 $this->course->startdate = gmmktime('0', '0', '0', 10, 24, 2015);
109 $DB->update_record('course', $this->course);
110 $courseman = new \core_analytics\course($this->course->id);
111 $this->assertTrue($courseman->was_started());
112 $this->assertFalse($courseman->is_finished());
113 $this->assertFalse($courseman->is_valid());
114
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());
121 $this->assertTrue($courseman->is_valid());
122
123 // Valid start and ongoing course.
124 $this->course->enddate = gmmktime('0', '0', '0', 8, 27, 2286);
125 $DB->update_record('course', $this->course);
126 $courseman = new \core_analytics\course($this->course->id);
127 $this->assertTrue($courseman->was_started());
128 $this->assertFalse($courseman->is_finished());
129 $this->assertFalse($courseman->is_valid());
130 }
131
132 /**
133 * Get the minimum time that is considered valid according to guess_end logic.
134 *
135 * @param int $time
136 * @return int
137 */
138 protected function time_greater_than($time) {
139 return $time - (WEEKSECS * 2);
140 }
141
142 /**
143 * Get the maximum time that is considered valid according to guess_end logic.
144 *
145 * @param int $time
146 * @return int
147 */
148 protected function time_less_than($time) {
149 return $time + (WEEKSECS * 2);
150 }
ff656bae 151}