MDL-79915 backup: Improve the Restore main page
[moodle.git] / analytics / tests / stats_test.php
blob5d8f3019c5c613f48ca0eb4ad9f7b176743f8a56
1 <?php
2 // This file is part of Moodle - https://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.
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 namespace core_analytics;
19 defined('MOODLE_INTERNAL') || die();
21 require_once(__DIR__ . '/fixtures/test_indicator_fullname.php');
22 require_once(__DIR__ . '/fixtures/test_target_shortname.php');
24 /**
25 * Unit tests for the analytics stats.
27 * @package core_analytics
28 * @category test
29 * @copyright 2019 David Mudrák <david@moodle.com>
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 class stats_test extends \advanced_testcase {
34 /**
35 * Set up the test environment.
37 public function setUp(): void {
39 $this->setAdminUser();
42 /**
43 * Test the {@link \core_analytics\stats::enabled_models()} implementation.
45 public function test_enabled_models() {
47 $this->resetAfterTest(true);
49 // By default, sites have {@link \core_course\analytics\target\no_teaching} and
50 // {@link \core_user\analytics\target\upcoming_activities_due} enabled.
51 $this->assertEquals(4, \core_analytics\stats::enabled_models());
53 $model = \core_analytics\model::create(
54 \core_analytics\manager::get_target('\core_course\analytics\target\course_dropout'),
56 \core_analytics\manager::get_indicator('\core\analytics\indicator\any_write_action'),
60 // Purely adding a new model does not make it included in the stats.
61 $this->assertEquals(4, \core_analytics\stats::enabled_models());
63 // New models must be enabled to have them counted.
64 $model->enable('\core\analytics\time_splitting\quarters');
65 $this->assertEquals(5, \core_analytics\stats::enabled_models());
68 /**
69 * Test the {@link \core_analytics\stats::predictions()} implementation.
71 public function test_predictions() {
73 $this->resetAfterTest(true);
75 $model = \core_analytics\model::create(
76 \core_analytics\manager::get_target('test_target_shortname'),
78 \core_analytics\manager::get_indicator('test_indicator_fullname'),
82 $model->enable('\core\analytics\time_splitting\no_splitting');
84 // Train the model.
85 $this->getDataGenerator()->create_course(['shortname' => 'a', 'fullname' => 'a', 'visible' => 1]);
86 $this->getDataGenerator()->create_course(['shortname' => 'b', 'fullname' => 'b', 'visible' => 1]);
87 $model->train();
89 // No predictions yet.
90 $this->assertEquals(0, \core_analytics\stats::predictions());
92 // Get one new prediction.
93 $this->getDataGenerator()->create_course(['shortname' => 'aa', 'fullname' => 'aa', 'visible' => 0]);
94 $result = $model->predict();
96 $this->assertEquals(1, count($result->predictions));
97 $this->assertEquals(1, \core_analytics\stats::predictions());
99 // Nothing changes if there is no new prediction.
100 $result = $model->predict();
101 $this->assertFalse(isset($result->predictions));
102 $this->assertEquals(1, \core_analytics\stats::predictions());
104 // Get two more predictions, we have three in total now.
105 $this->getDataGenerator()->create_course(['shortname' => 'bb', 'fullname' => 'bb', 'visible' => 0]);
106 $this->getDataGenerator()->create_course(['shortname' => 'cc', 'fullname' => 'cc', 'visible' => 0]);
108 $result = $model->predict();
109 $this->assertEquals(2, count($result->predictions));
110 $this->assertEquals(3, \core_analytics\stats::predictions());
114 * Test the {@link \core_analytics\stats::actions()} and {@link \core_analytics\stats::actions_not_useful()} implementation.
116 public function test_actions() {
117 global $DB;
118 $this->resetAfterTest(true);
120 $model = \core_analytics\model::create(
121 \core_analytics\manager::get_target('test_target_shortname'),
123 \core_analytics\manager::get_indicator('test_indicator_fullname'),
127 $model->enable('\core\analytics\time_splitting\no_splitting');
129 // Train the model.
130 $this->getDataGenerator()->create_course(['shortname' => 'a', 'fullname' => 'a', 'visible' => 1]);
131 $this->getDataGenerator()->create_course(['shortname' => 'b', 'fullname' => 'b', 'visible' => 1]);
132 $model->train();
134 // Generate two predictions.
135 $this->getDataGenerator()->create_course(['shortname' => 'aa', 'fullname' => 'aa', 'visible' => 0]);
136 $this->getDataGenerator()->create_course(['shortname' => 'bb', 'fullname' => 'bb', 'visible' => 0]);
137 $model->predict();
139 list($p1, $p2) = array_values($DB->get_records('analytics_predictions'));
141 $p1 = new \core_analytics\prediction($p1, []);
142 $p2 = new \core_analytics\prediction($p2, []);
144 // No actions executed at the start.
145 $this->assertEquals(0, \core_analytics\stats::actions());
146 $this->assertEquals(0, \core_analytics\stats::actions_not_useful());
148 // The user has acknowledged the first prediction.
149 $p1->action_executed(\core_analytics\prediction::ACTION_FIXED, $model->get_target());
150 $this->assertEquals(1, \core_analytics\stats::actions());
151 $this->assertEquals(0, \core_analytics\stats::actions_not_useful());
153 // The user has marked the other prediction as not useful.
154 $p2->action_executed(\core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED, $model->get_target());
155 $this->assertEquals(2, \core_analytics\stats::actions());
156 $this->assertEquals(1, \core_analytics\stats::actions_not_useful());