MDL-59630 analytics: Move get_analysables to abstract class
[moodle.git] / analytics / tests / model_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 the model.
19 *
413f19bc 20 * @package core_analytics
ff656bae
DM
21 * @copyright 2017 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
27require_once(__DIR__ . '/fixtures/test_indicator_max.php');
28require_once(__DIR__ . '/fixtures/test_indicator_min.php');
29require_once(__DIR__ . '/fixtures/test_indicator_fullname.php');
30require_once(__DIR__ . '/fixtures/test_target_shortname.php');
31
32/**
33 * Unit tests for the model.
34 *
413f19bc 35 * @package core_analytics
ff656bae
DM
36 * @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class analytics_model_testcase extends advanced_testcase {
40
41 public function setUp() {
42
1611308b
DM
43 $this->setAdminUser();
44
ff656bae
DM
45 $target = \core_analytics\manager::get_target('test_target_shortname');
46 $indicators = array('test_indicator_max', 'test_indicator_min', 'test_indicator_fullname');
47 foreach ($indicators as $key => $indicator) {
48 $indicators[$key] = \core_analytics\manager::get_indicator($indicator);
49 }
50
51 $this->model = testable_model::create($target, $indicators);
52 $this->modelobj = $this->model->get_model_obj();
53 }
54
55 public function test_enable() {
56 $this->resetAfterTest(true);
57
58 $this->assertEquals(0, $this->model->get_model_obj()->enabled);
59 $this->assertEquals(0, $this->model->get_model_obj()->trained);
60 $this->assertEquals('', $this->model->get_model_obj()->timesplitting);
61
206d7aa9 62 $this->model->enable('\core\analytics\time_splitting\quarters');
ff656bae
DM
63 $this->assertEquals(1, $this->model->get_model_obj()->enabled);
64 $this->assertEquals(0, $this->model->get_model_obj()->trained);
206d7aa9 65 $this->assertEquals('\core\analytics\time_splitting\quarters', $this->model->get_model_obj()->timesplitting);
ff656bae
DM
66 }
67
68 public function test_create() {
69 $this->resetAfterTest(true);
70
206d7aa9 71 $target = \core_analytics\manager::get_target('\core\analytics\target\course_dropout');
ff656bae 72 $indicators = array(
206d7aa9
DM
73 \core_analytics\manager::get_indicator('\core\analytics\indicator\any_write_action'),
74 \core_analytics\manager::get_indicator('\core\analytics\indicator\read_actions')
ff656bae
DM
75 );
76 $model = \core_analytics\model::create($target, $indicators);
77 $this->assertInstanceOf('\core_analytics\model', $model);
78 }
79
80 public function test_model_manager() {
81 $this->resetAfterTest(true);
82
83 $this->assertCount(3, $this->model->get_indicators());
84 $this->assertInstanceOf('\core_analytics\local\target\binary', $this->model->get_target());
85
86 // Using evaluation as the model is not yet enabled.
87 $this->model->init_analyser(array('evaluation' => true));
88 $this->assertInstanceOf('\core_analytics\local\analyser\base', $this->model->get_analyser());
89
206d7aa9
DM
90 $this->model->enable('\core\analytics\time_splitting\quarters');
91 $this->assertInstanceOf('\core\analytics\analyser\site_courses', $this->model->get_analyser());
ff656bae
DM
92 }
93
94 public function test_output_dir() {
95 $this->resetAfterTest(true);
96
97 $dir = make_request_directory();
98 set_config('modeloutputdir', $dir, 'analytics');
99
100 $modeldir = $dir . DIRECTORY_SEPARATOR . $this->modelobj->id . DIRECTORY_SEPARATOR . $this->modelobj->version;
101 $this->assertEquals($modeldir, $this->model->get_output_dir());
102 $this->assertEquals($modeldir . DIRECTORY_SEPARATOR . 'asd', $this->model->get_output_dir(array('asd')));
103 }
104
105 public function test_unique_id() {
106 global $DB;
107
108 $this->resetAfterTest(true);
109
110 $originaluniqueid = $this->model->get_unique_id();
111
112 // Same id across instances.
113 $this->model = new testable_model($this->modelobj);
114 $this->assertEquals($originaluniqueid, $this->model->get_unique_id());
115
116 // We will restore it later.
117 $originalversion = $this->modelobj->version;
118
119 // Generates a different id if timemodified changes.
120 $this->modelobj->version = $this->modelobj->version + 10;
121 $DB->update_record('analytics_models', $this->modelobj);
122 $this->model = new testable_model($this->modelobj);
123 $this->assertNotEquals($originaluniqueid, $this->model->get_unique_id());
124
125 // Restore original timemodified to continue testing.
126 $this->modelobj->version = $originalversion;
127 $DB->update_record('analytics_models', $this->modelobj);
128 // Same when updating through an action that changes the model.
129 $this->model = new testable_model($this->modelobj);
130
131 $this->model->mark_as_trained();
132 $this->assertEquals($originaluniqueid, $this->model->get_unique_id());
133
134 $this->model->enable();
135 $this->assertEquals($originaluniqueid, $this->model->get_unique_id());
136
137 // Wait 1 sec so the timestamp changes.
138 sleep(1);
206d7aa9 139 $this->model->enable('\core\analytics\time_splitting\quarters');
ff656bae 140 $this->assertNotEquals($originaluniqueid, $this->model->get_unique_id());
e709e544
DM
141 }
142
143 /**
144 * test_exists
145 *
146 * @return void
147 */
148 public function test_exists() {
149 $this->resetAfterTest(true);
150
151 global $DB;
152
cab7abec 153 $count = $DB->count_records('analytics_models');
e709e544
DM
154
155 // No new models added if the builtin ones already exist.
156 \core_analytics\manager::add_builtin_models();
cab7abec 157 $this->assertCount($count, $DB->get_records('analytics_models'));
ff656bae 158
e709e544
DM
159 $target = \core_analytics\manager::get_target('\core\analytics\target\no_teaching');
160 $this->assertTrue(\core_analytics\model::exists($target));
ff656bae
DM
161 }
162}
163
413f19bc
DM
164/**
165 * Testable version to change methods' visibility.
166 *
167 * @package core_analytics
168 * @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
169 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
170 */
ff656bae 171class testable_model extends \core_analytics\model {
413f19bc
DM
172
173 /**
174 * get_output_dir
175 *
176 * @param array $subdirs
177 * @return string
178 */
ff656bae
DM
179 public function get_output_dir($subdirs = array()) {
180 return parent::get_output_dir($subdirs);
181 }
182
413f19bc
DM
183 /**
184 * init_analyser
185 *
186 * @param array $options
187 * @return void
188 */
ff656bae 189 public function init_analyser($options = array()) {
413f19bc 190 parent::init_analyser($options);
ff656bae
DM
191 }
192}