-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtool_cloudmetrics_users_test.php
More file actions
187 lines (171 loc) · 8.58 KB
/
tool_cloudmetrics_users_test.php
File metadata and controls
187 lines (171 loc) · 8.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
// This file is part of Moodle - https://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace tool_cloudmetrics;
use tool_cloudmetrics\metric\metric_item;
use tool_cloudmetrics\metric\new_users_metric;
use tool_cloudmetrics\metric\active_users_metric;
use tool_cloudmetrics\metric\online_users_metric;
use tool_cloudmetrics\metric\yearly_active_users_metric;
/**
* Unit tests to test the builtin user metric types.
*
* @package tool_cloudmetrics
* @author Jason den Dulk <jasondendulk@catalyst-au.net>
* @copyright 2022, Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
final class tool_cloudmetrics_users_test extends \advanced_testcase {
/**
* Set up before each test
*/
protected function setUp(): void {
parent::setUp();
$this->resetAfterTest();
}
/** @var integer Seconds in a day. */
public const SECOND_IN_DAY = 86400;
/** @var array[] Sample user DB data to be used in tests. */
public const USER_DATA = [
['username' => 'a', 'firstaccess' => 11000, 'lastaccess' => 12950, 'lastlogin' => 12560],
['username' => 'b', 'firstaccess' => 1500, 'lastaccess' => 2050, 'lastlogin' => 14200],
['username' => 'c', 'firstaccess' => 11100, 'lastaccess' => 10100, 'lastlogin' => 12871],
['username' => 'd', 'firstaccess' => 11150, 'lastaccess' => 14000, 'lastlogin' => 11401],
['username' => 'e', 'firstaccess' => 11500, 'lastaccess' => 9043, 'lastlogin' => 12940],
['username' => 'f', 'firstaccess' => 12450, 'lastaccess' => 8001, 'lastlogin' => 12790],
['username' => 'g', 'firstaccess' => 12500, 'lastaccess' => 9999, 'lastlogin' => 12999],
['username' => 'h', 'firstaccess' => 12600, 'lastaccess' => 11000, 'lastlogin' => 12777],
['username' => 'i', 'firstaccess' => 12800, 'lastaccess' => 100, 'lastlogin' => 12882],
['username' => 'j', 'firstaccess' => 13000, 'lastaccess' => 12950, 'lastlogin' => 12500],
];
/** @var array[] Sample active user DB data to be used in tests. */
public const ACTIVE_USER_DATA = [
['username' => 'a', 'confirmed' => 1 , 'lastlogin' => self::SECOND_IN_DAY + 1000],
['username' => 'b', 'confirmed' => 1 , 'lastlogin' => (self::SECOND_IN_DAY * 2) + 1000],
['username' => 'c', 'confirmed' => 1 , 'lastlogin' => (self::SECOND_IN_DAY * 2) + 2000],
['username' => 'd', 'confirmed' => 1 , 'lastlogin' => (self::SECOND_IN_DAY * 2) + 3000],
['username' => 'e', 'confirmed' => 1 , 'lastlogin' => (self::SECOND_IN_DAY * 3) + 1000],
['username' => 'f', 'confirmed' => 1 , 'lastlogin' => (self::SECOND_IN_DAY * 3) + 2000],
['username' => 'g', 'confirmed' => 1 , 'lastlogin' => (self::SECOND_IN_DAY * 4) + 1000],
['username' => 'h', 'confirmed' => 1 , 'lastlogin' => (self::SECOND_IN_DAY * 4) + 2000],
['username' => 'i', 'confirmed' => 1 , 'lastlogin' => (self::SECOND_IN_DAY * 5) + 1000],
['username' => 'j', 'confirmed' => 1 , 'lastlogin' => (self::SECOND_IN_DAY * 5) + 2000],
];
/**
* Tests generate_metric_items() for the builtin user metrics.
*
* @dataProvider data_for_test_generate_metrics
* @param string $metricname The name of the metric to be tested.
* @param int $frequency The frequency setting as a metric\manager::FREQ_ value.
* @param array $expected List of metric items that expect to be generated.
* @throws \dml_exception
*/
public function test_generate_metrics(string $metricname, int $frequency, array $expected): void {
global $DB;
foreach (self::USER_DATA as $row) {
$DB->insert_record('user', (object) $row);
}
$metrictypes = metric\manager::get_metrics(false);
$metric = $metrictypes[$metricname];
$this->assertEquals($metricname, $metric->get_name());
$metric->set_frequency($frequency);
set_config($metricname . '_time_window', MINSECS * 5, 'tool_cloudmetrics');
$endtime = 13000;
$time = $endtime - (4 * lib::FREQ_TIMES[$frequency]);
$items = [];
while ($time <= $endtime) {
$items[] = $metric->generate_metric_item(0, $time);
$time = lib::get_next_time($time, $frequency);
}
$this->assertEquals($expected, $items);
}
/**
* Tests test_generate_yearly_active_users_metric() for the builtin user metrics.
*
* @dataProvider data_for_test_generate_yearly_active_users_metric
* @param string $metricname The name of the metric to be tested.
* @param array $expected List of metric items that expect to be generated.
* @throws \dml_exception
*/
public function test_generate_yearly_active_users_metric(string $metricname, array $expected): void {
global $DB;
foreach (self::ACTIVE_USER_DATA as $row) {
$DB->insert_record('user', (object) $row);
}
$time = self::SECOND_IN_DAY * 366;
$endtime = $time + (4 * lib::FREQ_TIMES[metric\manager::FREQ_DAY]);
$metrictypes = metric\manager::get_metrics(false);
$metric = $metrictypes[$metricname];
$this->assertEquals($metricname, $metric->get_name());
while ($time <= $endtime) {
$items[] = $metric->generate_metric_item(0, $time);
$time = lib::get_next_time($time, metric\manager::FREQ_DAY);
}
$this->assertEquals($expected, $items);
}
/**
* Data provider for test_generate_metrics.
*
* @return array[]
*/
public function data_for_test_generate_metrics(): array {
$newusersmetric = new new_users_metric();
$activeusersmetric = new active_users_metric();
$onlineusersmetric = new online_users_metric();
return [
[ 'newusers', metric\manager::FREQ_5MIN, [
new metric_item('newusers', 11800, 0, $newusersmetric),
new metric_item('newusers', 12100, 0, $newusersmetric),
new metric_item('newusers', 12400, 0, $newusersmetric),
new metric_item('newusers', 12700, 3, $newusersmetric),
new metric_item('newusers', 13000, 2, $newusersmetric),
],
],
[ 'activeusers', metric\manager::FREQ_MIN, [
new metric_item('activeusers', 12760, 2, $activeusersmetric),
new metric_item('activeusers', 12820, 3, $activeusersmetric),
new metric_item('activeusers', 12880, 3, $activeusersmetric),
new metric_item('activeusers', 12940, 5, $activeusersmetric),
new metric_item('activeusers', 13000, 6, $activeusersmetric),
],
],
[ 'onlineusers', metric\manager::FREQ_15MIN, [
new metric_item('onlineusers', 9400, 0, $onlineusersmetric),
new metric_item('onlineusers', 10300, 1, $onlineusersmetric),
new metric_item('onlineusers', 11200, 1, $onlineusersmetric),
new metric_item('onlineusers', 12100, 0, $onlineusersmetric),
new metric_item('onlineusers', 13000, 2, $onlineusersmetric),
],
],
];
}
/**
* Data provider for test_generate_yearly_active_users_metric.
*
* @return array[]
*/
public function data_for_test_generate_yearly_active_users_metric(): array {
$yearlyactiveusers = new yearly_active_users_metric();
return [
['yearlyactiveusers', [
new metric_item('yearlyactiveusers', self::SECOND_IN_DAY * 366 , 10, $yearlyactiveusers),
new metric_item('yearlyactiveusers', self::SECOND_IN_DAY * 367, 9, $yearlyactiveusers),
new metric_item('yearlyactiveusers', self::SECOND_IN_DAY * 368, 6, $yearlyactiveusers),
new metric_item('yearlyactiveusers', self::SECOND_IN_DAY * 369, 4, $yearlyactiveusers),
new metric_item('yearlyactiveusers', self::SECOND_IN_DAY * 370, 2, $yearlyactiveusers)],
],
];
}
}