Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

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