Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

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