Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

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