MDL-58859 analytics: Add tests to core
[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 *
20 * @package 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
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 *
35 * @package 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
38 */
39class analytics_model_testcase extends advanced_testcase {
40
41 public function setUp() {
42
43 $target = \core_analytics\manager::get_target('test_target_shortname');
44 $indicators = array('test_indicator_max', 'test_indicator_min', 'test_indicator_fullname');
45 foreach ($indicators as $key => $indicator) {
46 $indicators[$key] = \core_analytics\manager::get_indicator($indicator);
47 }
48
49 $this->model = testable_model::create($target, $indicators);
50 $this->modelobj = $this->model->get_model_obj();
51 }
52
53 public function test_enable() {
54 $this->resetAfterTest(true);
55
56 $this->assertEquals(0, $this->model->get_model_obj()->enabled);
57 $this->assertEquals(0, $this->model->get_model_obj()->trained);
58 $this->assertEquals('', $this->model->get_model_obj()->timesplitting);
59
60 $this->model->enable('\\core_analytics\\local\\time_splitting\\quarters');
61 $this->assertEquals(1, $this->model->get_model_obj()->enabled);
62 $this->assertEquals(0, $this->model->get_model_obj()->trained);
63 $this->assertEquals('\\core_analytics\\local\\time_splitting\\quarters', $this->model->get_model_obj()->timesplitting);
64 }
65
66 public function test_create() {
67 $this->resetAfterTest(true);
68
69 $target = \core_analytics\manager::get_target('\tool_models\local\target\course_dropout');
70 $indicators = array(
71 \core_analytics\manager::get_indicator('\core_analytics\local\indicator\any_write_action'),
72 \core_analytics\manager::get_indicator('\core_analytics\local\indicator\read_actions')
73 );
74 $model = \core_analytics\model::create($target, $indicators);
75 $this->assertInstanceOf('\core_analytics\model', $model);
76 }
77
78 public function test_model_manager() {
79 $this->resetAfterTest(true);
80
81 $this->assertCount(3, $this->model->get_indicators());
82 $this->assertInstanceOf('\core_analytics\local\target\binary', $this->model->get_target());
83
84 // Using evaluation as the model is not yet enabled.
85 $this->model->init_analyser(array('evaluation' => true));
86 $this->assertInstanceOf('\core_analytics\local\analyser\base', $this->model->get_analyser());
87
88 $this->model->enable('\core_analytics\local\time_splitting\quarters');
89 $this->assertInstanceOf('\core_analytics\local\analyser\courses', $this->model->get_analyser());
90 }
91
92 public function test_output_dir() {
93 $this->resetAfterTest(true);
94
95 $dir = make_request_directory();
96 set_config('modeloutputdir', $dir, 'analytics');
97
98 $modeldir = $dir . DIRECTORY_SEPARATOR . $this->modelobj->id . DIRECTORY_SEPARATOR . $this->modelobj->version;
99 $this->assertEquals($modeldir, $this->model->get_output_dir());
100 $this->assertEquals($modeldir . DIRECTORY_SEPARATOR . 'asd', $this->model->get_output_dir(array('asd')));
101 }
102
103 public function test_unique_id() {
104 global $DB;
105
106 $this->resetAfterTest(true);
107
108 $originaluniqueid = $this->model->get_unique_id();
109
110 // Same id across instances.
111 $this->model = new testable_model($this->modelobj);
112 $this->assertEquals($originaluniqueid, $this->model->get_unique_id());
113
114 // We will restore it later.
115 $originalversion = $this->modelobj->version;
116
117 // Generates a different id if timemodified changes.
118 $this->modelobj->version = $this->modelobj->version + 10;
119 $DB->update_record('analytics_models', $this->modelobj);
120 $this->model = new testable_model($this->modelobj);
121 $this->assertNotEquals($originaluniqueid, $this->model->get_unique_id());
122
123 // Restore original timemodified to continue testing.
124 $this->modelobj->version = $originalversion;
125 $DB->update_record('analytics_models', $this->modelobj);
126 // Same when updating through an action that changes the model.
127 $this->model = new testable_model($this->modelobj);
128
129 $this->model->mark_as_trained();
130 $this->assertEquals($originaluniqueid, $this->model->get_unique_id());
131
132 $this->model->enable();
133 $this->assertEquals($originaluniqueid, $this->model->get_unique_id());
134
135 // Wait 1 sec so the timestamp changes.
136 sleep(1);
137 $this->model->enable('\core_analytics\local\time_splitting\quarters');
138 $this->assertNotEquals($originaluniqueid, $this->model->get_unique_id());
139
140 }
141}
142
143class testable_model extends \core_analytics\model {
144 public function get_output_dir($subdirs = array()) {
145 return parent::get_output_dir($subdirs);
146 }
147
148 public function init_analyser($options = array()) {
149 return parent::init_analyser($options);
150 }
151}