Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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 calculation info cache.
  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_indicator_max.php');
  28  require_once (__DIR__ . '/fixtures/test_indicator_min.php');
  29  
  30  /**
  31   * Unit tests for the calculation info cache.
  32   *
  33   * @package   core_analytics
  34   * @copyright 2017 David MonllaĆ³ {@link http://www.davidmonllao.com}
  35   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class analytics_calculation_info_testcase extends advanced_testcase {
  38  
  39      /**
  40       * test_calculation_info description
  41       *
  42       * @dataProvider provider_test_calculation_info_add_pull
  43       * @param mixed $info1
  44       * @param mixed $info2
  45       * @param mixed $info3
  46       * @param mixed $info4
  47       * @return null
  48       */
  49      public function test_calculation_info_add_pull($info1, $info2, $info3, $info4) {
  50          $this->resetAfterTest();
  51  
  52          $atimesplitting = new \core\analytics\time_splitting\quarters();
  53  
  54          $indicator1 = new test_indicator_min();
  55          $indicator2 = new test_indicator_max();
  56  
  57          $calculationinfo = new \core_analytics\calculation_info();
  58          $calculationinfo->add_shared(111, [111 => $info1]);
  59          $calculationinfo->add_shared(222, [222 => 'should-get-overwritten-in-next-line']);
  60          $calculationinfo->add_shared(222, [222 => $info2]);
  61          $calculationinfo->save($indicator1, $atimesplitting, 0);
  62  
  63          // We also check that the eheheh does not overwrite the value previously stored in the cache
  64          // during the previous save call.
  65          $calculationinfo->add_shared(222, [222 => 'eheheh']);
  66          $calculationinfo->save($indicator1, $atimesplitting, 0);
  67  
  68          // The method save() should clear the internal attrs in \core_analytics\calculation_info
  69          // so it is fine to reuse the same calculation_info instance.
  70          $calculationinfo->add_shared(111, [111 => $info3]);
  71          $calculationinfo->add_shared(333, [333 => $info4]);
  72          $calculationinfo->save($indicator2, $atimesplitting, 0);
  73  
  74          // We pull data in rangeindex '0' for samples 111, 222 and 333.
  75          $predictionrecords = [
  76              '111-0' => (object)['sampleid' => '111'],
  77              '222-0' => (object)['sampleid' => '222'],
  78              '333-0' => (object)['sampleid' => '333'],
  79          ];
  80          $info = \core_analytics\calculation_info::pull_info($predictionrecords);
  81  
  82          $this->assertCount(3, $info);
  83          $this->assertCount(2, $info[111]);
  84          $this->assertCount(1, $info[222]);
  85          $this->assertCount(1, $info[333]);
  86          $this->assertEquals($info1, $info[111]['test_indicator_min:extradata'][111]);
  87          $this->assertEquals($info2, $info[222]['test_indicator_min:extradata'][222]);
  88          $this->assertEquals($info3, $info[111]['test_indicator_max:extradata'][111]);
  89          $this->assertEquals($info4, $info[333]['test_indicator_max:extradata'][333]);
  90  
  91          // The calculationinfo cache gets emptied.
  92          $this->assertFalse(\core_analytics\calculation_info::pull_info($predictionrecords));
  93      }
  94  
  95      /**
  96       * provider_test_calculation_info_add_pull
  97       *
  98       * @return mixed[]
  99       */
 100      public function provider_test_calculation_info_add_pull() {
 101          return [
 102              'mixed-types' => ['asd', true, [123, 123, 123], (object)['asd' => 'fgfg']],
 103          ];
 104      }
 105  }