Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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   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
23
 */
24
 
25
namespace core_analytics;
26
 
1441 ariadna 27
use core_analytics\tests\mlbackend_helper_trait;
28
 
1 efrain 29
defined('MOODLE_INTERNAL') || die();
30
 
31
require_once(__DIR__ . '/fixtures/test_indicator_max.php');
32
require_once(__DIR__ . '/fixtures/test_indicator_min.php');
33
require_once(__DIR__ . '/fixtures/test_indicator_fullname.php');
34
require_once(__DIR__ . '/fixtures/test_target_shortname.php');
35
require_once(__DIR__ . '/fixtures/test_static_target_shortname.php');
36
require_once(__DIR__ . '/fixtures/test_target_course_level_shortname.php');
37
require_once(__DIR__ . '/fixtures/test_analysis.php');
38
 
39
/**
40
 * Unit tests for the model.
41
 *
42
 * @package   core_analytics
43
 * @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
44
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45
 */
1441 ariadna 46
final class model_test extends \advanced_testcase {
47
    use mlbackend_helper_trait;
1 efrain 48
 
49
    /** @var model Store Model. */
50
    protected $model;
51
 
52
    /** @var \stdClass Store model object. */
53
    protected $modelobj;
54
 
55
    public function setUp(): void {
1441 ariadna 56
        parent::setUp();
1 efrain 57
 
58
        $this->setAdminUser();
59
 
60
        $target = \core_analytics\manager::get_target('test_target_shortname');
61
        $indicators = array('test_indicator_max', 'test_indicator_min', 'test_indicator_fullname');
62
        foreach ($indicators as $key => $indicator) {
63
            $indicators[$key] = \core_analytics\manager::get_indicator($indicator);
64
        }
65
 
66
        $this->model = testable_model::create($target, $indicators);
67
        $this->modelobj = $this->model->get_model_obj();
68
    }
69
 
11 efrain 70
    public function test_enable(): void {
1 efrain 71
        $this->resetAfterTest(true);
72
 
73
        $this->assertEquals(0, $this->model->get_model_obj()->enabled);
74
        $this->assertEquals(0, $this->model->get_model_obj()->trained);
75
        $this->assertEquals('', $this->model->get_model_obj()->timesplitting);
76
 
77
        $this->model->enable('\core\analytics\time_splitting\quarters');
78
        $this->assertEquals(1, $this->model->get_model_obj()->enabled);
79
        $this->assertEquals(0, $this->model->get_model_obj()->trained);
80
        $this->assertEquals('\core\analytics\time_splitting\quarters', $this->model->get_model_obj()->timesplitting);
81
    }
82
 
11 efrain 83
    public function test_create(): void {
1 efrain 84
        $this->resetAfterTest(true);
85
 
86
        $target = \core_analytics\manager::get_target('\core_course\analytics\target\course_dropout');
87
        $indicators = array(
88
            \core_analytics\manager::get_indicator('\core\analytics\indicator\any_write_action'),
89
            \core_analytics\manager::get_indicator('\core\analytics\indicator\read_actions')
90
        );
91
        $model = \core_analytics\model::create($target, $indicators);
92
        $this->assertInstanceOf('\core_analytics\model', $model);
93
    }
94
 
95
    /**
96
     * test_delete
97
     */
11 efrain 98
    public function test_delete(): void {
1 efrain 99
        global $DB;
100
 
1441 ariadna 101
        if (!self::is_mlbackend_python_configured()) {
102
            $this->markTestSkipped('mlbackend_python is not configured.');
103
        }
104
 
1 efrain 105
        $this->resetAfterTest(true);
106
        set_config('enabled_stores', 'logstore_standard', 'tool_log');
107
 
1441 ariadna 108
        // Create some courses.
109
        $this->generate_courses(2, ['visible' => 0]);
110
        $this->generate_courses(2, ['visible' => 1]);
1 efrain 111
 
112
        $this->model->enable('\core\analytics\time_splitting\single_range');
113
 
114
        $this->model->train();
115
        $this->model->predict();
116
 
117
        // Fake evaluation results record to check that it is actually deleted.
118
        $this->add_fake_log();
119
 
120
        // Generate a prediction action to confirm that it is deleted when there is an important update.
121
        $predictions = $DB->get_records('analytics_predictions');
122
        $prediction = reset($predictions);
123
        $prediction = new \core_analytics\prediction($prediction, array('whatever' => 'not used'));
124
        $prediction->action_executed(\core_analytics\prediction::ACTION_FIXED, $this->model->get_target());
125
 
126
        $this->model->delete();
127
        $this->assertEmpty($DB->count_records('analytics_models', array('id' => $this->modelobj->id)));
128
        $this->assertEmpty($DB->count_records('analytics_models_log', array('modelid' => $this->modelobj->id)));
129
        $this->assertEmpty($DB->count_records('analytics_predictions'));
130
        $this->assertEmpty($DB->count_records('analytics_prediction_actions'));
131
        $this->assertEmpty($DB->count_records('analytics_train_samples'));
132
        $this->assertEmpty($DB->count_records('analytics_predict_samples'));
133
        $this->assertEmpty($DB->count_records('analytics_used_files'));
134
 
135
        set_config('enabled_stores', '', 'tool_log');
136
        get_log_manager(true);
137
    }
138
 
139
    /**
140
     * test_clear
141
     */
11 efrain 142
    public function test_clear(): void {
1 efrain 143
        global $DB;
144
 
1441 ariadna 145
        if (!self::is_mlbackend_python_configured()) {
146
            $this->markTestSkipped('mlbackend_python is not configured.');
147
        }
148
 
1 efrain 149
        $this->resetAfterTest(true);
150
        set_config('enabled_stores', 'logstore_standard', 'tool_log');
151
 
1441 ariadna 152
        // Create some courses.
153
        $this->generate_courses(2, ['visible' => 0]);
154
        $this->generate_courses(2, ['visible' => 1]);
1 efrain 155
 
156
        $this->model->enable('\core\analytics\time_splitting\single_range');
157
 
158
        $this->model->train();
159
        $this->model->predict();
160
 
161
        // Fake evaluation results record to check that it is actually deleted.
162
        $this->add_fake_log();
163
 
164
        // Generate a prediction action to confirm that it is deleted when there is an important update.
165
        $predictions = $DB->get_records('analytics_predictions');
166
        $prediction = reset($predictions);
167
        $prediction = new \core_analytics\prediction($prediction, array('whatever' => 'not used'));
168
        $prediction->action_executed(\core_analytics\prediction::ACTION_FIXED, $this->model->get_target());
169
 
170
        $modelversionoutputdir = $this->model->get_output_dir();
171
        $this->assertTrue(is_dir($modelversionoutputdir));
172
 
173
        // Update to an empty time splitting method to force model::clear execution.
174
        $this->model->clear();
175
 
176
        // Check that most of the stuff got deleted.
177
        $this->assertEquals(1, $DB->count_records('analytics_models', array('id' => $this->modelobj->id)));
178
        $this->assertEquals(1, $DB->count_records('analytics_models_log', array('modelid' => $this->modelobj->id)));
179
        $this->assertEmpty($DB->count_records('analytics_predictions'));
180
        $this->assertEmpty($DB->count_records('analytics_prediction_actions'));
181
        $this->assertEmpty($DB->count_records('analytics_train_samples'));
182
        $this->assertEmpty($DB->count_records('analytics_predict_samples'));
183
        $this->assertEmpty($DB->count_records('analytics_used_files'));
184
 
185
        // Check that the model is marked as not trained after clearing (as it is not a static one).
186
        $this->assertEquals(0, $DB->get_field('analytics_models', 'trained', array('id' => $this->modelobj->id)));
187
 
188
        set_config('enabled_stores', '', 'tool_log');
189
        get_log_manager(true);
190
    }
191
 
192
    /**
193
     * Test behaviour of {\core_analytics\model::clear()} for static models.
194
     */
11 efrain 195
    public function test_clear_static(): void {
1 efrain 196
        global $DB;
197
        $this->resetAfterTest();
198
 
199
        $statictarget = new \test_static_target_shortname();
200
        $indicators['test_indicator_max'] = \core_analytics\manager::get_indicator('test_indicator_max');
201
        $model = \core_analytics\model::create($statictarget, $indicators, '\core\analytics\time_splitting\quarters');
202
        $modelobj = $model->get_model_obj();
203
 
204
        // Static models are always considered trained.
205
        $this->assertEquals(1, $DB->get_field('analytics_models', 'trained', array('id' => $modelobj->id)));
206
 
207
        $model->clear();
208
 
209
        // Check that the model is still marked as trained even after clearing.
210
        $this->assertEquals(1, $DB->get_field('analytics_models', 'trained', array('id' => $modelobj->id)));
211
    }
212
 
11 efrain 213
    public function test_model_manager(): void {
1 efrain 214
        $this->resetAfterTest(true);
215
 
216
        $this->assertCount(3, $this->model->get_indicators());
217
        $this->assertInstanceOf('\core_analytics\local\target\binary', $this->model->get_target());
218
 
219
        // Using evaluation as the model is not yet enabled.
220
        $this->model->init_analyser(array('evaluation' => true));
221
        $this->assertInstanceOf('\core_analytics\local\analyser\base', $this->model->get_analyser());
222
 
223
        $this->model->enable('\core\analytics\time_splitting\quarters');
224
        $this->assertInstanceOf('\core\analytics\analyser\site_courses', $this->model->get_analyser());
225
    }
226
 
11 efrain 227
    public function test_output_dir(): void {
1 efrain 228
        $this->resetAfterTest(true);
229
 
230
        $dir = make_request_directory();
231
        set_config('modeloutputdir', $dir, 'analytics');
232
 
233
        $modeldir = $dir . DIRECTORY_SEPARATOR . $this->modelobj->id . DIRECTORY_SEPARATOR . $this->modelobj->version;
234
        $this->assertEquals($modeldir, $this->model->get_output_dir());
235
        $this->assertEquals($modeldir . DIRECTORY_SEPARATOR . 'testing', $this->model->get_output_dir(array('testing')));
236
    }
237
 
11 efrain 238
    public function test_unique_id(): void {
1 efrain 239
        global $DB;
240
 
241
        $this->resetAfterTest(true);
242
 
243
        $originaluniqueid = $this->model->get_unique_id();
244
 
245
        // Same id across instances.
246
        $this->model = new testable_model($this->modelobj);
247
        $this->assertEquals($originaluniqueid, $this->model->get_unique_id());
248
 
249
        // We will restore it later.
250
        $originalversion = $this->modelobj->version;
251
 
252
        // Generates a different id if timemodified changes.
253
        $this->modelobj->version = $this->modelobj->version + 10;
254
        $DB->update_record('analytics_models', $this->modelobj);
255
        $this->model = new testable_model($this->modelobj);
256
        $this->assertNotEquals($originaluniqueid, $this->model->get_unique_id());
257
 
258
        // Restore original timemodified to continue testing.
259
        $this->modelobj->version = $originalversion;
260
        $DB->update_record('analytics_models', $this->modelobj);
261
        // Same when updating through an action that changes the model.
262
        $this->model = new testable_model($this->modelobj);
263
 
264
        $this->model->mark_as_trained();
265
        $this->assertEquals($originaluniqueid, $this->model->get_unique_id());
266
 
267
        // Wait for the current timestamp to change.
268
        $this->waitForSecond();
269
        $this->model->enable('\core\analytics\time_splitting\deciles');
270
        $this->assertNotEquals($originaluniqueid, $this->model->get_unique_id());
271
        $uniqueid = $this->model->get_unique_id();
272
 
273
        // Wait for the current timestamp to change.
274
        $this->waitForSecond();
275
        $this->model->enable('\core\analytics\time_splitting\quarters');
276
        $this->assertNotEquals($originaluniqueid, $this->model->get_unique_id());
277
        $this->assertNotEquals($uniqueid, $this->model->get_unique_id());
278
    }
279
 
280
    /**
281
     * test_exists
282
     *
283
     * @return void
284
     */
11 efrain 285
    public function test_exists(): void {
1 efrain 286
        $this->resetAfterTest(true);
287
 
288
        $target = \core_analytics\manager::get_target('\core_course\analytics\target\no_teaching');
289
        $this->assertTrue(\core_analytics\model::exists($target));
290
 
291
        foreach (\core_analytics\manager::get_all_models() as $model) {
292
            $model->delete();
293
        }
294
 
295
        $this->assertFalse(\core_analytics\model::exists($target));
296
    }
297
 
298
    /**
299
     * test_model_timelimit
300
     *
301
     * @return null
302
     */
11 efrain 303
    public function test_model_timelimit(): void {
1 efrain 304
        global $DB;
305
 
306
        $this->resetAfterTest(true);
307
 
308
        set_config('modeltimelimit', 2, 'analytics');
309
 
310
        $courses = array();
311
        for ($i = 0; $i < 5; $i++) {
312
            $course = $this->getDataGenerator()->create_course();
313
            $analysable = new \core_analytics\course($course);
314
            $courses[$analysable->get_id()] = $course;
315
        }
316
 
317
        $target = new \test_target_course_level_shortname();
318
        $analyser = new \core\analytics\analyser\courses(1, $target, [], [], []);
319
 
320
        $result = new \core_analytics\local\analysis\result_array(1, false, []);
321
        $analysis = new \test_analysis($analyser, false, $result);
322
 
323
        // Each analysable element takes 0.5 secs minimum (test_analysis), so the max (and likely) number of analysable
324
        // elements that will be processed is 2.
325
        $analysis->run();
326
        $params = array('modelid' => 1, 'action' => 'prediction');
327
        $this->assertLessThanOrEqual(2, $DB->count_records('analytics_used_analysables', $params));
328
 
329
        $analysis->run();
330
        $this->assertLessThanOrEqual(4, $DB->count_records('analytics_used_analysables', $params));
331
 
332
        // Check that analysable elements have been processed following the analyser order
333
        // (course->sortorder here). We can not check this nicely after next get_unlabelled_data round
334
        // because the first analysed element will be analysed again.
335
        $analysedelems = $DB->get_records('analytics_used_analysables', $params, 'timeanalysed ASC');
336
        // Just a default for the first checked element.
337
        $last = (object)['sortorder' => PHP_INT_MAX];
338
        foreach ($analysedelems as $analysed) {
339
            if ($courses[$analysed->analysableid]->sortorder > $last->sortorder) {
340
                $this->fail('Analysable elements have not been analysed sorted by course sortorder.');
341
            }
342
            $last = $courses[$analysed->analysableid];
343
        }
344
 
345
        // No time limit now to process the rest.
346
        set_config('modeltimelimit', 1000, 'analytics');
347
 
348
        $analysis->run();
349
        $this->assertEquals(5, $DB->count_records('analytics_used_analysables', $params));
350
 
351
        // New analysable elements are immediately pulled.
352
        $this->getDataGenerator()->create_course();
353
        $analysis->run();
354
        $this->assertEquals(6, $DB->count_records('analytics_used_analysables', $params));
355
 
356
        // Training and prediction data do not get mixed.
357
        $result = new \core_analytics\local\analysis\result_array(1, false, []);
358
        $analysis = new \test_analysis($analyser, false, $result);
359
        $analysis->run();
360
        $params = array('modelid' => 1, 'action' => 'training');
361
        $this->assertLessThanOrEqual(2, $DB->count_records('analytics_used_analysables', $params));
362
    }
363
 
364
    /**
365
     * Test model_config::get_class_component.
366
     */
11 efrain 367
    public function test_model_config_get_class_component(): void {
1 efrain 368
        $this->resetAfterTest(true);
369
 
370
        $this->assertEquals('core',
371
            \core_analytics\model_config::get_class_component('\\core\\analytics\\indicator\\read_actions'));
372
        $this->assertEquals('core',
373
            \core_analytics\model_config::get_class_component('core\\analytics\\indicator\\read_actions'));
374
        $this->assertEquals('core',
375
            \core_analytics\model_config::get_class_component('\\core_course\\analytics\\indicator\\completion_enabled'));
376
        $this->assertEquals('mod_forum',
377
            \core_analytics\model_config::get_class_component('\\mod_forum\\analytics\\indicator\\cognitive_depth'));
378
 
379
        $this->assertEquals('core', \core_analytics\model_config::get_class_component('\\core_class'));
380
    }
381
 
382
    /**
383
     * Test that import_model import models' configurations.
384
     */
11 efrain 385
    public function test_import_model_config(): void {
1441 ariadna 386
        if (!self::is_mlbackend_python_configured()) {
387
            $this->markTestSkipped('mlbackend_python is not configured.');
388
        }
389
 
1 efrain 390
        $this->resetAfterTest(true);
391
 
392
        $this->model->enable('\\core\\analytics\\time_splitting\\quarters');
393
        $zipfilepath = $this->model->export_model('yeah-config.zip');
394
 
395
        $this->modelobj = $this->model->get_model_obj();
396
 
397
        $importedmodelobj = \core_analytics\model::import_model($zipfilepath)->get_model_obj();
398
 
399
        $this->assertSame($this->modelobj->target, $importedmodelobj->target);
400
        $this->assertSame($this->modelobj->indicators, $importedmodelobj->indicators);
401
        $this->assertSame($this->modelobj->timesplitting, $importedmodelobj->timesplitting);
402
 
403
        $predictionsprocessor = $this->model->get_predictions_processor();
404
        $this->assertSame('\\' . get_class($predictionsprocessor), $importedmodelobj->predictionsprocessor);
405
    }
406
 
407
    /**
408
     * Test can export configuration
409
     */
11 efrain 410
    public function test_can_export_configuration(): void {
1 efrain 411
        $this->resetAfterTest(true);
412
 
413
        // No time splitting method.
414
        $this->assertFalse($this->model->can_export_configuration());
415
 
416
        $this->model->enable('\\core\\analytics\\time_splitting\\quarters');
417
        $this->assertTrue($this->model->can_export_configuration());
418
 
419
        $this->model->update(true, [], false);
420
        $this->assertFalse($this->model->can_export_configuration());
421
 
422
        $statictarget = new \test_static_target_shortname();
423
        $indicators['test_indicator_max'] = \core_analytics\manager::get_indicator('test_indicator_max');
424
        $model = \core_analytics\model::create($statictarget, $indicators, '\\core\\analytics\\time_splitting\\quarters');
425
        $this->assertFalse($model->can_export_configuration());
426
    }
427
 
428
    /**
429
     * Test export_config
430
     */
11 efrain 431
    public function test_export_config(): void {
1441 ariadna 432
        if (!self::is_mlbackend_python_configured()) {
433
            $this->markTestSkipped('mlbackend_python is not configured.');
434
        }
435
 
1 efrain 436
        $this->resetAfterTest(true);
437
 
438
        $this->model->enable('\\core\\analytics\\time_splitting\\quarters');
439
 
440
        $modelconfig = new \core_analytics\model_config($this->model);
441
 
442
        $method = new \ReflectionMethod('\\core_analytics\\model_config', 'export_model_data');
443
 
444
        $modeldata = $method->invoke($modelconfig);
445
 
446
        $this->assertArrayHasKey('core', $modeldata->dependencies);
447
        $this->assertIsFloat($modeldata->dependencies['core']);
448
        $this->assertNotEmpty($modeldata->target);
449
        $this->assertNotEmpty($modeldata->timesplitting);
450
        $this->assertCount(3, $modeldata->indicators);
451
 
452
        $indicators['test_indicator_max'] = \core_analytics\manager::get_indicator('test_indicator_max');
453
        $this->model->update(true, $indicators, false);
454
 
455
        $modeldata = $method->invoke($modelconfig);
456
 
457
        $this->assertCount(1, $modeldata->indicators);
458
    }
459
 
460
    /**
461
     * Test the implementation of {@link \core_analytics\model::inplace_editable_name()}.
462
     */
11 efrain 463
    public function test_inplace_editable_name(): void {
1 efrain 464
        global $PAGE;
465
 
466
        $this->resetAfterTest();
467
 
468
        $output = new \core_renderer($PAGE, RENDERER_TARGET_GENERAL);
469
 
470
        // Check as a user with permission to edit the name.
471
        $this->setAdminUser();
472
        $ie = $this->model->inplace_editable_name();
473
        $this->assertInstanceOf(\core\output\inplace_editable::class, $ie);
474
        $data = $ie->export_for_template($output);
475
        $this->assertEquals('core_analytics', $data['component']);
476
        $this->assertEquals('modelname', $data['itemtype']);
477
 
478
        // Check as a user without permission to edit the name.
479
        $this->setGuestUser();
480
        $ie = $this->model->inplace_editable_name();
481
        $this->assertInstanceOf(\core\output\inplace_editable::class, $ie);
482
        $data = $ie->export_for_template($output);
483
        $this->assertArrayHasKey('displayvalue', $data);
484
    }
485
 
486
    /**
487
     * Test how the models present themselves in the UI and that they can be renamed.
488
     */
11 efrain 489
    public function test_get_name_and_rename(): void {
1 efrain 490
        global $PAGE;
491
 
492
        $this->resetAfterTest();
493
 
494
        $output = new \core_renderer($PAGE, RENDERER_TARGET_GENERAL);
495
 
496
        // By default, the model exported for template uses its target's name in the name inplace editable element.
497
        $this->assertEquals($this->model->get_name(), $this->model->get_target()->get_name());
498
        $data = $this->model->export($output);
499
        $this->assertEquals($data->name['displayvalue'], $this->model->get_target()->get_name());
500
        $this->assertEquals($data->name['value'], '');
501
 
502
        // Rename the model.
503
        $this->model->rename('Nějaký pokusný model');
504
        $this->assertEquals($this->model->get_name(), 'Nějaký pokusný model');
505
        $data = $this->model->export($output);
506
        $this->assertEquals($data->name['displayvalue'], 'Nějaký pokusný model');
507
        $this->assertEquals($data->name['value'], 'Nějaký pokusný model');
508
 
509
        // Undo the renaming.
510
        $this->model->rename('');
511
        $this->assertEquals($this->model->get_name(), $this->model->get_target()->get_name());
512
        $data = $this->model->export($output);
513
        $this->assertEquals($data->name['displayvalue'], $this->model->get_target()->get_name());
514
        $this->assertEquals($data->name['value'], '');
515
    }
516
 
517
    /**
518
     * Tests model::get_potential_timesplittings()
519
     */
11 efrain 520
    public function test_potential_timesplittings(): void {
1 efrain 521
        $this->resetAfterTest();
522
 
523
        $this->assertArrayNotHasKey('\core\analytics\time_splitting\no_splitting', $this->model->get_potential_timesplittings());
524
        $this->assertArrayHasKey('\core\analytics\time_splitting\single_range', $this->model->get_potential_timesplittings());
525
        $this->assertArrayHasKey('\core\analytics\time_splitting\quarters', $this->model->get_potential_timesplittings());
526
    }
527
 
528
    /**
529
     * Tests model::get_samples()
530
     *
531
     * @return null
532
     */
11 efrain 533
    public function test_get_samples(): void {
1 efrain 534
        $this->resetAfterTest();
535
 
536
        if (!PHPUNIT_LONGTEST) {
537
            $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
538
        }
539
 
1441 ariadna 540
        // 10000 should be enough to make mssql fail, if we want pgsql to fail we need around 70000
1 efrain 541
        // users, that is a few minutes just to create the users.
542
        $nusers = 10000;
543
 
544
        $userids = [];
545
        for ($i = 0; $i < $nusers; $i++) {
546
            $user = $this->getDataGenerator()->create_user();
547
            $userids[] = $user->id;
548
        }
549
 
550
        $upcomingactivities = null;
551
        foreach (\core_analytics\manager::get_all_models() as $model) {
552
            if (get_class($model->get_target()) === 'core_user\\analytics\\target\\upcoming_activities_due') {
553
                $upcomingactivities = $model;
554
            }
555
        }
556
 
557
        list($sampleids, $samplesdata) = $upcomingactivities->get_samples($userids);
558
        $this->assertCount($nusers, $sampleids);
559
        $this->assertCount($nusers, $samplesdata);
560
 
561
        $subset = array_slice($userids, 0, 100);
562
        list($sampleids, $samplesdata) = $upcomingactivities->get_samples($subset);
563
        $this->assertCount(100, $sampleids);
564
        $this->assertCount(100, $samplesdata);
565
 
566
        $subset = array_slice($userids, 0, 2);
567
        list($sampleids, $samplesdata) = $upcomingactivities->get_samples($subset);
568
        $this->assertCount(2, $sampleids);
569
        $this->assertCount(2, $samplesdata);
570
 
571
        $subset = array_slice($userids, 0, 1);
572
        list($sampleids, $samplesdata) = $upcomingactivities->get_samples($subset);
573
        $this->assertCount(1, $sampleids);
574
        $this->assertCount(1, $samplesdata);
575
 
576
        // Unexisting, so nothing returned, but still 2 arrays.
577
        list($sampleids, $samplesdata) = $upcomingactivities->get_samples([1231231231231231]);
578
        $this->assertEmpty($sampleids);
579
        $this->assertEmpty($samplesdata);
580
 
581
    }
582
 
583
    /**
584
     * Generates a model log record.
585
     */
586
    private function add_fake_log() {
587
        global $DB, $USER;
588
 
589
        $log = new \stdClass();
590
        $log->modelid = $this->modelobj->id;
591
        $log->version = $this->modelobj->version;
592
        $log->target = $this->modelobj->target;
593
        $log->indicators = $this->modelobj->indicators;
594
        $log->score = 1;
595
        $log->info = json_encode([]);
596
        $log->dir = 'not important';
597
        $log->timecreated = time();
598
        $log->usermodified = $USER->id;
599
        $DB->insert_record('analytics_models_log', $log);
600
    }
601
}
602
 
603
/**
604
 * Testable version to change methods' visibility.
605
 *
606
 * @package   core_analytics
607
 * @copyright 2017 David Monllaó {@link http://www.davidmonllao.com}
608
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
609
 */
610
class testable_model extends \core_analytics\model {
611
 
612
    /**
613
     * init_analyser
614
     *
615
     * @param array $options
616
     * @return void
617
     */
618
    public function init_analyser($options = array()) {
619
        parent::init_analyser($options);
620
    }
621
}