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] [Versions 39 and 310]

   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 indicator API.
  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_discrete.php');
  29  require_once (__DIR__ . '/fixtures/test_indicator_min.php');
  30  
  31  /**
  32   * Unit tests for the model.
  33   *
  34   * @package   core_analytics
  35   * @copyright 2017 David MonllaĆ³ {@link http://www.davidmonllao.com}
  36   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class analytics_indicator_testcase extends advanced_testcase {
  39  
  40      /**
  41       * test_validate_calculated_value
  42       *
  43       * @param string $indicatorclass
  44       * @param array $returnedvalue
  45       * @dataProvider validate_calculated_value
  46       * @return null
  47       */
  48      public function test_validate_calculated_value($indicatorclass, $returnedvalue) {
  49          $indicator = new $indicatorclass();
  50          list($values, $unused) = $indicator->calculate([1], 'notrelevanthere');
  51          $this->assertEquals($returnedvalue, $values[0]);
  52      }
  53  
  54      /**
  55       * Data provider for test_validate_calculated_value
  56       *
  57       * @return array
  58       */
  59      public function validate_calculated_value() {
  60          return [
  61              'max' => ['test_indicator_max', [1]],
  62              'min' => ['test_indicator_min', [-1]],
  63              'discrete' => ['test_indicator_discrete', [0, 0, 0, 0, 1]],
  64          ];
  65      }
  66  
  67      /**
  68       * test_validate_calculated_value_exceptions
  69       *
  70       * @param string $indicatorclass
  71       * @param string $willreturn
  72       * @dataProvider validate_calculated_value_exceptions
  73       * @return null
  74       */
  75      public function test_validate_calculated_value_exceptions($indicatorclass, $willreturn) {
  76  
  77          $indicator = new $indicatorclass();
  78          $indicatormock = $this->getMockBuilder(get_class($indicator))
  79              ->setMethods(['calculate_sample'])
  80              ->getMock();
  81          $indicatormock->method('calculate_sample')->willReturn($willreturn);
  82          $this->expectException(coding_exception::class);
  83          list($values, $unused) = $indicatormock->calculate([1], 'notrelevanthere');
  84  
  85      }
  86  
  87      /**
  88       * Data provider for test_validate_calculated_value_exceptions
  89       *
  90       * @return array
  91       */
  92      public function validate_calculated_value_exceptions() {
  93          return [
  94              'max' => ['test_indicator_max', 2],
  95              'min' => ['test_indicator_min', -2],
  96              'discrete' => ['test_indicator_discrete', 7],
  97          ];
  98      }
  99  }