Differences Between: [Versions 310 and 402] [Versions 39 and 402]
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 namespace core_analytics; 18 19 /** 20 * Unit tests for the analysis class. 21 * 22 * @package core_analytics 23 * @copyright 2019 David MonllaĆ³ {@link http://www.davidmonllao.com} 24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 25 */ 26 class analysis_test extends \advanced_testcase { 27 28 /** 29 * Test fill_firstanalyses_cache. 30 * @return null 31 */ 32 public function test_fill_firstanalyses_cache() { 33 require_once (__DIR__ . '/fixtures/test_timesplitting_upcoming_seconds.php'); 34 $this->resetAfterTest(); 35 36 $modelid = 1; 37 38 $params = ['startdate' => (new \DateTimeImmutable('-5 seconds'))->getTimestamp()]; 39 $course1 = $this->getDataGenerator()->create_course($params); 40 $course2 = $this->getDataGenerator()->create_course($params); 41 $analysable1 = new \core_analytics\course($course1); 42 43 $afewsecsago = time() - 5; 44 $earliest = $afewsecsago - 1; 45 46 $this->insert_used($modelid, $course1->id, 'training', $afewsecsago); 47 48 // Course2 processed after course1. 49 $this->insert_used($modelid, $course2->id, 'training', $afewsecsago + 1); 50 51 // After the first process involving course1. 52 $this->insert_used($modelid, $course1->id, 'prediction', $afewsecsago + 5); 53 54 $firstanalyses = \core_analytics\analysis::fill_firstanalyses_cache($modelid); 55 $this->assertCount(2, $firstanalyses); 56 $this->assertEquals($afewsecsago, $firstanalyses[$modelid . '_' . $course1->id]); 57 $this->assertEquals($afewsecsago + 1, $firstanalyses[$modelid . '_' . $course2->id]); 58 59 // The cached elements get refreshed. 60 $this->insert_used($modelid, $course1->id, 'prediction', $earliest); 61 $firstanalyses = \core_analytics\analysis::fill_firstanalyses_cache($modelid, $course1->id); 62 $this->assertCount(1, $firstanalyses); 63 $this->assertEquals($earliest, $firstanalyses[$modelid . '_' . $course1->id]); 64 65 // Upcoming periodic time-splitting methods can read and process the cached data. 66 $seconds = new \test_timesplitting_upcoming_seconds(); 67 $seconds->set_modelid($modelid); 68 $seconds->set_analysable($analysable1); 69 70 // The generated ranges should start from the cached firstanalysis value, which is $earliest. 71 $ranges = $seconds->get_all_ranges(); 72 $this->assertGreaterThanOrEqual(7, count($ranges)); 73 $firstrange = reset($ranges); 74 $this->assertEquals($earliest, $firstrange['time']); 75 } 76 77 private function insert_used($modelid, $analysableid, $action, $timestamp) { 78 global $DB; 79 80 $obj = new \stdClass(); 81 $obj->modelid = $modelid; 82 $obj->action = $action; 83 $obj->analysableid = $analysableid; 84 $obj->firstanalysis = $timestamp; 85 $obj->timeanalysed = $timestamp; 86 $obj->id = $DB->insert_record('analytics_used_analysables', $obj); 87 } 88 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body