Merge branch 'MDL-61949-master-privacy-upgrade-txt' of git://github.com/mudrd8mz...
[moodle.git] / analytics / tests / manager_test.php
blobf4c2a1c06bffe8627a34e6fb6e4ef8c0b9842955
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.
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 /**
18 * Unit tests for the manager.
20 * @package core_analytics
21 * @copyright 2017 David MonllaĆ³ {@link http://www.davidmonllao.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 require_once(__DIR__ . '/fixtures/test_indicator_max.php');
28 require_once(__DIR__ . '/fixtures/test_indicator_min.php');
29 require_once(__DIR__ . '/fixtures/test_indicator_fullname.php');
30 require_once(__DIR__ . '/fixtures/test_target_course_level_shortname.php');
32 /**
33 * Unit tests for the manager.
35 * @package core_analytics
36 * @copyright 2017 David MonllaĆ³ {@link http://www.davidmonllao.com}
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class analytics_manager_testcase extends advanced_testcase {
41 /**
42 * test_deleted_context
44 public function test_deleted_context() {
45 global $DB;
47 $this->resetAfterTest(true);
48 $this->setAdminuser();
49 set_config('enabled_stores', 'logstore_standard', 'tool_log');
51 $target = \core_analytics\manager::get_target('test_target_course_level_shortname');
52 $indicators = array('test_indicator_max', 'test_indicator_min', 'test_indicator_fullname');
53 foreach ($indicators as $key => $indicator) {
54 $indicators[$key] = \core_analytics\manager::get_indicator($indicator);
57 $model = \core_analytics\model::create($target, $indicators);
58 $modelobj = $model->get_model_obj();
60 $coursepredict1 = $this->getDataGenerator()->create_course(array('visible' => 0));
61 $coursepredict2 = $this->getDataGenerator()->create_course(array('visible' => 0));
62 $coursetrain1 = $this->getDataGenerator()->create_course(array('visible' => 1));
63 $coursetrain2 = $this->getDataGenerator()->create_course(array('visible' => 1));
65 $model->enable('\core\analytics\time_splitting\no_splitting');
67 $model->train();
68 $model->predict();
70 // Generate a prediction action to confirm that it is deleted when there is an important update.
71 $predictions = $DB->get_records('analytics_predictions');
72 $prediction = reset($predictions);
73 $prediction = new \core_analytics\prediction($prediction, array('whatever' => 'not used'));
74 $prediction->action_executed(\core_analytics\prediction::ACTION_FIXED, $model->get_target());
76 $predictioncontextid = $prediction->get_prediction_data()->contextid;
78 $npredictions = $DB->count_records('analytics_predictions', array('contextid' => $predictioncontextid));
79 $npredictionactions = $DB->count_records('analytics_prediction_actions',
80 array('predictionid' => $prediction->get_prediction_data()->id));
81 $nindicatorcalc = $DB->count_records('analytics_indicator_calc', array('contextid' => $predictioncontextid));
83 \core_analytics\manager::cleanup();
85 // Nothing is incorrectly deleted.
86 $this->assertEquals($npredictions, $DB->count_records('analytics_predictions',
87 array('contextid' => $predictioncontextid)));
88 $this->assertEquals($npredictionactions, $DB->count_records('analytics_prediction_actions',
89 array('predictionid' => $prediction->get_prediction_data()->id)));
90 $this->assertEquals($nindicatorcalc, $DB->count_records('analytics_indicator_calc',
91 array('contextid' => $predictioncontextid)));
93 // Now we delete a context, the course predictions and prediction actions should be deleted.
94 $deletedcontext = \context::instance_by_id($predictioncontextid);
95 delete_course($deletedcontext->instanceid, false);
97 \core_analytics\manager::cleanup();
99 $this->assertEmpty($DB->count_records('analytics_predictions', array('contextid' => $predictioncontextid)));
100 $this->assertEmpty($DB->count_records('analytics_prediction_actions',
101 array('predictionid' => $prediction->get_prediction_data()->id)));
102 $this->assertEmpty($DB->count_records('analytics_indicator_calc', array('contextid' => $predictioncontextid)));
104 set_config('enabled_stores', '', 'tool_log');
105 get_log_manager(true);
109 * test_deleted_analysable
111 public function test_deleted_analysable() {
112 global $DB;
114 $this->resetAfterTest(true);
115 $this->setAdminuser();
116 set_config('enabled_stores', 'logstore_standard', 'tool_log');
118 $target = \core_analytics\manager::get_target('test_target_course_level_shortname');
119 $indicators = array('test_indicator_max', 'test_indicator_min', 'test_indicator_fullname');
120 foreach ($indicators as $key => $indicator) {
121 $indicators[$key] = \core_analytics\manager::get_indicator($indicator);
124 $model = \core_analytics\model::create($target, $indicators);
125 $modelobj = $model->get_model_obj();
127 $coursepredict1 = $this->getDataGenerator()->create_course(array('visible' => 0));
128 $coursepredict2 = $this->getDataGenerator()->create_course(array('visible' => 0));
129 $coursetrain1 = $this->getDataGenerator()->create_course(array('visible' => 1));
130 $coursetrain2 = $this->getDataGenerator()->create_course(array('visible' => 1));
132 $model->enable('\core\analytics\time_splitting\no_splitting');
134 $model->train();
135 $model->predict();
137 $npredictsamples = $DB->count_records('analytics_predict_samples');
138 $ntrainsamples = $DB->count_records('analytics_train_samples');
140 // Now we delete an analysable, stored predict and training samples should be deleted.
141 $deletedcontext = \context_course::instance($coursepredict1->id);
142 delete_course($coursepredict1, false);
144 \core_analytics\manager::cleanup();
146 $this->assertEmpty($DB->count_records('analytics_predict_samples', array('analysableid' => $coursepredict1->id)));
147 $this->assertEmpty($DB->count_records('analytics_train_samples', array('analysableid' => $coursepredict1->id)));
149 set_config('enabled_stores', '', 'tool_log');
150 get_log_manager(true);