2020use tool_cloudmetrics \metric \new_users_metric ;
2121use tool_cloudmetrics \metric \active_users_metric ;
2222use tool_cloudmetrics \metric \online_users_metric ;
23+ use tool_cloudmetrics \metric \yearly_active_users_metric ;
2324
2425/**
2526 * Unit tests to test the builtin user metric types.
2930 * @copyright 2022, Catalyst IT
3031 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3132 */
32- class tool_cloudmetrics_users_test extends \advanced_testcase {
33+ final class tool_cloudmetrics_users_test extends \advanced_testcase {
3334
3435 /**
3536 * Set up before each test
@@ -38,6 +39,8 @@ protected function setUp(): void {
3839 parent ::setUp ();
3940 $ this ->resetAfterTest ();
4041 }
42+ /** @var integer Seconds in a day. */
43+ public const SECOND_IN_DAY = 86400 ;
4144
4245 /** @var array[] Sample user DB data to be used in tests. */
4346 public const USER_DATA = [
@@ -53,6 +56,20 @@ protected function setUp(): void {
5356 ['username ' => 'j ' , 'firstaccess ' => 13000 , 'lastaccess ' => 12950 , 'lastlogin ' => 12500 ],
5457 ];
5558
59+ /** @var array[] Sample active user DB data to be used in tests. */
60+ public const ACTIVE_USER_DATA = [
61+ ['username ' => 'a ' , 'confirmed ' => 1 , 'lastlogin ' => self ::SECOND_IN_DAY + 1000 ],
62+ ['username ' => 'b ' , 'confirmed ' => 1 , 'lastlogin ' => (self ::SECOND_IN_DAY * 2 ) + 1000 ],
63+ ['username ' => 'c ' , 'confirmed ' => 1 , 'lastlogin ' => (self ::SECOND_IN_DAY * 2 ) + 2000 ],
64+ ['username ' => 'd ' , 'confirmed ' => 1 , 'lastlogin ' => (self ::SECOND_IN_DAY * 2 ) + 3000 ],
65+ ['username ' => 'e ' , 'confirmed ' => 1 , 'lastlogin ' => (self ::SECOND_IN_DAY * 3 ) + 1000 ],
66+ ['username ' => 'f ' , 'confirmed ' => 1 , 'lastlogin ' => (self ::SECOND_IN_DAY * 3 ) + 2000 ],
67+ ['username ' => 'g ' , 'confirmed ' => 1 , 'lastlogin ' => (self ::SECOND_IN_DAY * 4 ) + 1000 ],
68+ ['username ' => 'h ' , 'confirmed ' => 1 , 'lastlogin ' => (self ::SECOND_IN_DAY * 4 ) + 2000 ],
69+ ['username ' => 'i ' , 'confirmed ' => 1 , 'lastlogin ' => (self ::SECOND_IN_DAY * 5 ) + 1000 ],
70+ ['username ' => 'j ' , 'confirmed ' => 1 , 'lastlogin ' => (self ::SECOND_IN_DAY * 5 ) + 2000 ],
71+ ];
72+
5673 /**
5774 * Tests generate_metric_items() for the builtin user metrics.
5875 *
@@ -62,7 +79,7 @@ protected function setUp(): void {
6279 * @param array $expected List of metric items that expect to be generated.
6380 * @throws \dml_exception
6481 */
65- public function test_generate_metrics (string $ metricname , int $ frequency , array $ expected ) {
82+ public function test_generate_metrics (string $ metricname , int $ frequency , array $ expected ): void {
6683 global $ DB ;
6784
6885 foreach (self ::USER_DATA as $ row ) {
@@ -85,6 +102,33 @@ public function test_generate_metrics(string $metricname, int $frequency, array
85102 $ this ->assertEquals ($ expected , $ items );
86103 }
87104
105+
106+ /**
107+ * Tests test_generate_yearly_active_users_metric() for the builtin user metrics.
108+ *
109+ * @dataProvider data_for_test_generate_yearly_active_users_metric
110+ * @param string $metricname The name of the metric to be tested.
111+ * @param array $expected List of metric items that expect to be generated.
112+ * @throws \dml_exception
113+ */
114+ public function test_generate_yearly_active_users_metric (string $ metricname , array $ expected ): void {
115+ global $ DB ;
116+
117+ foreach (self ::ACTIVE_USER_DATA as $ row ) {
118+ $ DB ->insert_record ('user ' , (object ) $ row );
119+ }
120+ $ time = self ::SECOND_IN_DAY * 366 ;
121+ $ endtime = $ time + (4 * lib::FREQ_TIMES [metric \manager::FREQ_DAY ]);
122+ $ metrictypes = metric \manager::get_metrics (false );
123+ $ metric = $ metrictypes [$ metricname ];
124+ $ this ->assertEquals ($ metricname , $ metric ->get_name ());
125+ while ($ time <= $ endtime ) {
126+ $ items [] = $ metric ->generate_metric_item (0 , $ time );
127+ $ time = lib::get_next_time ($ time , metric \manager::FREQ_DAY );
128+ }
129+ $ this ->assertEquals ($ expected , $ items );
130+
131+ }
88132 /**
89133 * Data provider for test_generate_metrics.
90134 *
@@ -101,24 +145,43 @@ public function data_for_test_generate_metrics(): array {
101145 new metric_item ('newusers ' , 12400 , 0 , $ newusersmetric ),
102146 new metric_item ('newusers ' , 12700 , 3 , $ newusersmetric ),
103147 new metric_item ('newusers ' , 13000 , 2 , $ newusersmetric ),
104- ]
148+ ],
105149 ],
106150 [ 'activeusers ' , metric \manager::FREQ_MIN , [
107151 new metric_item ('activeusers ' , 12760 , 2 , $ activeusersmetric ),
108152 new metric_item ('activeusers ' , 12820 , 3 , $ activeusersmetric ),
109153 new metric_item ('activeusers ' , 12880 , 3 , $ activeusersmetric ),
110154 new metric_item ('activeusers ' , 12940 , 5 , $ activeusersmetric ),
111155 new metric_item ('activeusers ' , 13000 , 6 , $ activeusersmetric ),
112- ]
156+ ],
113157 ],
114158 [ 'onlineusers ' , metric \manager::FREQ_15MIN , [
115159 new metric_item ('onlineusers ' , 9400 , 0 , $ onlineusersmetric ),
116160 new metric_item ('onlineusers ' , 10300 , 1 , $ onlineusersmetric ),
117161 new metric_item ('onlineusers ' , 11200 , 1 , $ onlineusersmetric ),
118162 new metric_item ('onlineusers ' , 12100 , 0 , $ onlineusersmetric ),
119163 new metric_item ('onlineusers ' , 13000 , 2 , $ onlineusersmetric ),
120- ]
164+ ],
121165 ],
122166 ];
123167 }
168+
169+ /**
170+ * Data provider for test_generate_yearly_active_users_metric.
171+ *
172+ * @return array[]
173+ */
174+ public function data_for_test_generate_yearly_active_users_metric (): array {
175+ $ yearlyactiveusers = new yearly_active_users_metric ();
176+
177+ return [
178+ ['yearlyactiveusers ' , [
179+ new metric_item ('yearlyactiveusers ' , self ::SECOND_IN_DAY * 366 , 10 , $ yearlyactiveusers ),
180+ new metric_item ('yearlyactiveusers ' , self ::SECOND_IN_DAY * 367 , 9 , $ yearlyactiveusers ),
181+ new metric_item ('yearlyactiveusers ' , self ::SECOND_IN_DAY * 368 , 6 , $ yearlyactiveusers ),
182+ new metric_item ('yearlyactiveusers ' , self ::SECOND_IN_DAY * 369 , 4 , $ yearlyactiveusers ),
183+ new metric_item ('yearlyactiveusers ' , self ::SECOND_IN_DAY * 370 , 2 , $ yearlyactiveusers )],
184+ ],
185+ ];
186+ }
124187}
0 commit comments